/
claim-rewards.go
477 lines (409 loc) · 12.9 KB
/
claim-rewards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package node
import (
"fmt"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
"github.com/Seb369888/poolsea-go/network"
"github.com/Seb369888/poolsea-go/node"
"github.com/Seb369888/poolsea-go/rewards"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/settings/protocol"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/Seb369888/smartnode/shared/services"
"github.com/Seb369888/smartnode/shared/services/config"
rprewards "github.com/Seb369888/smartnode/shared/services/rewards"
"github.com/Seb369888/smartnode/shared/services/state"
"github.com/Seb369888/smartnode/shared/types/api"
"github.com/Seb369888/smartnode/shared/utils/eth1"
rputils "github.com/Seb369888/smartnode/shared/utils/rp"
)
func getRewardsInfo(c *cli.Context) (*api.NodeGetRewardsInfoResponse, error) {
// Get services
if err := services.RequireNodeWallet(c); err != nil {
return nil, err
}
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.NodeGetRewardsInfoResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Check if Atlas is deployed
isAtlasDeployed, err := state.IsAtlasDeployed(rp, nil)
if err != nil {
return nil, fmt.Errorf("error checking if Atlas is deployed: %w", err)
}
response.IsAtlasDeployed = isAtlasDeployed
response.Registered, err = node.GetNodeExists(rp, nodeAccount.Address, nil)
if err != nil {
return nil, err
}
if !response.Registered {
return &response, nil
}
// Get the claimed and unclaimed intervals
unclaimed, claimed, err := rprewards.GetClaimStatus(rp, nodeAccount.Address)
if err != nil {
return nil, err
}
response.ClaimedIntervals = claimed
// Get the info for each unclaimed interval
for _, unclaimedInterval := range unclaimed {
intervalInfo, err := rprewards.GetIntervalInfo(rp, cfg, nodeAccount.Address, unclaimedInterval)
if err != nil {
return nil, err
}
if !intervalInfo.TreeFileExists || !intervalInfo.MerkleRootValid {
response.InvalidIntervals = append(response.InvalidIntervals, intervalInfo)
continue
}
if intervalInfo.NodeExists {
response.UnclaimedIntervals = append(response.UnclaimedIntervals, intervalInfo)
}
}
// Get collateral info for restaking
var totalMinipools int
var finalizedMinipools int
details, err := getNodeMinipoolCountDetails(rp, nodeAccount.Address)
if err == nil {
totalMinipools = len(details)
for _, mpDetails := range details {
if mpDetails.Finalised {
finalizedMinipools++
}
}
}
activeMinipools := totalMinipools - finalizedMinipools
response.ActiveMinipools = activeMinipools
// Sync
var wg errgroup.Group
wg.Go(func() error {
var err error
response.RplStake, err = node.GetNodeRPLStake(rp, nodeAccount.Address, nil)
return err
})
wg.Go(func() error {
var err error
response.RplPrice, err = network.GetRPLPrice(rp, nil)
return err
})
wg.Go(func() error {
var err error
response.MinimumRplStake, err = node.GetNodeMinimumRPLStake(rp, nodeAccount.Address, nil)
return err
})
wg.Go(func() error {
var err error
response.MaximumRplStake, err = node.GetNodeMaximumRPLStake(rp, nodeAccount.Address, nil)
return err
})
wg.Go(func() error {
var err error
response.EffectiveRplStake, err = node.GetNodeEffectiveRPLStake(rp, nodeAccount.Address, nil)
return err
})
// Wait for data
if err := wg.Wait(); err != nil {
return nil, err
}
if activeMinipools > 0 {
if isAtlasDeployed {
var wg2 errgroup.Group
var minStakeFraction *big.Int
var maxStakeFraction *big.Int
wg2.Go(func() error {
var err error
minStakeFraction, err = protocol.GetMinimumPerMinipoolStakeRaw(rp, nil)
return err
})
wg2.Go(func() error {
var err error
maxStakeFraction, err = protocol.GetMaximumPerMinipoolStakeRaw(rp, nil)
return err
})
wg2.Go(func() error {
var err error
response.EthMatched, response.EthMatchedLimit, response.PendingMatchAmount, err = rputils.CheckCollateral(rp, nodeAccount.Address, nil)
return err
})
// Wait for data
if err := wg2.Wait(); err != nil {
return nil, err
}
// Calculate the *real* minimum, including the pending bond reductions
trueMinimumStake := big.NewInt(0).Add(response.EthMatched, response.PendingMatchAmount)
trueMinimumStake.Mul(trueMinimumStake, minStakeFraction)
trueMinimumStake.Div(trueMinimumStake, response.RplPrice)
// Calculate the *real* maximum, including the pending bond reductions
trueMaximumStake := eth.EthToWei(32_000_000)
trueMaximumStake.Mul(trueMaximumStake, big.NewInt(int64(activeMinipools)))
trueMaximumStake.Sub(trueMaximumStake, response.EthMatched)
trueMaximumStake.Sub(trueMaximumStake, response.PendingMatchAmount) // (32_000_000 * activeMinipools - ethMatched - pendingMatch)
trueMaximumStake.Mul(trueMaximumStake, maxStakeFraction)
trueMaximumStake.Div(trueMaximumStake, response.RplPrice)
response.MinimumRplStake = trueMinimumStake
response.MaximumRplStake = trueMaximumStake
if response.EffectiveRplStake.Cmp(trueMinimumStake) < 0 {
response.EffectiveRplStake.SetUint64(0)
} else if response.EffectiveRplStake.Cmp(trueMaximumStake) > 0 {
response.EffectiveRplStake.Set(trueMaximumStake)
}
response.BondedCollateralRatio = eth.WeiToEth(response.RplPrice) * eth.WeiToEth(response.RplStake) / (float64(activeMinipools)*32_000_000.0 - eth.WeiToEth(response.EthMatched) - eth.WeiToEth(response.PendingMatchAmount))
response.BorrowedCollateralRatio = eth.WeiToEth(response.RplPrice) * eth.WeiToEth(response.RplStake) / (eth.WeiToEth(response.EthMatched) + eth.WeiToEth(response.PendingMatchAmount))
} else {
// Legacy behavior
response.BorrowedCollateralRatio = eth.WeiToEth(response.RplPrice) * eth.WeiToEth(response.RplStake) / (float64(activeMinipools) * 16_000_000.0)
}
} else {
response.BorrowedCollateralRatio = -1
}
return &response, nil
}
func canClaimRewards(c *cli.Context, indicesString string) (*api.CanNodeClaimRewardsResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.CanNodeClaimRewardsResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Get the rewards
indices, amountRPL, amountETH, merkleProofs, err := getRewardsForIntervals(rp, cfg, nodeAccount.Address, indicesString)
if err != nil {
return nil, err
}
// Get gas estimate
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
gasInfo, err := rewards.EstimateClaimGas(rp, nodeAccount.Address, indices, amountRPL, amountETH, merkleProofs, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
return &response, nil
}
func claimRewards(c *cli.Context, indicesString string) (*api.NodeClaimRewardsResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.NodeClaimRewardsResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Get the rewards
indices, amountRPL, amountETH, merkleProofs, err := getRewardsForIntervals(rp, cfg, nodeAccount.Address, indicesString)
if err != nil {
return nil, err
}
// Get transactor
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
// Override the provided pending TX if requested
err = eth1.CheckForNonceOverride(c, opts)
if err != nil {
return nil, fmt.Errorf("Error checking for nonce override: %w", err)
}
// Claim rewards
hash, err := rewards.Claim(rp, nodeAccount.Address, indices, amountRPL, amountETH, merkleProofs, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
// Return response
return &response, nil
}
func canClaimAndStakeRewards(c *cli.Context, indicesString string, stakeAmount *big.Int) (*api.CanNodeClaimAndStakeRewardsResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.CanNodeClaimAndStakeRewardsResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Get the rewards
indices, amountRPL, amountETH, merkleProofs, err := getRewardsForIntervals(rp, cfg, nodeAccount.Address, indicesString)
if err != nil {
return nil, err
}
// Get gas estimate
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
gasInfo, err := rewards.EstimateClaimAndStakeGas(rp, nodeAccount.Address, indices, amountRPL, amountETH, merkleProofs, stakeAmount, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
return &response, nil
}
func claimAndStakeRewards(c *cli.Context, indicesString string, stakeAmount *big.Int) (*api.NodeClaimAndStakeRewardsResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.NodeClaimAndStakeRewardsResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Get transactor
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
// Get the rewards
indices, amountRPL, amountETH, merkleProofs, err := getRewardsForIntervals(rp, cfg, nodeAccount.Address, indicesString)
if err != nil {
return nil, err
}
// Override the provided pending TX if requested
err = eth1.CheckForNonceOverride(c, opts)
if err != nil {
return nil, fmt.Errorf("Error checking for nonce override: %w", err)
}
// Claim rewards
hash, err := rewards.ClaimAndStake(rp, nodeAccount.Address, indices, amountRPL, amountETH, merkleProofs, stakeAmount, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
// Return response
return &response, nil
}
// Get the rewards for the provided interval indices
func getRewardsForIntervals(rp *rocketpool.RocketPool, cfg *config.RocketPoolConfig, nodeAddress common.Address, indicesString string) ([]*big.Int, []*big.Int, []*big.Int, [][]common.Hash, error) {
// Get the indices
seenIndices := map[uint64]bool{}
elements := strings.Split(indicesString, ",")
indices := []*big.Int{}
for _, element := range elements {
index, err := strconv.ParseUint(element, 0, 64)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("cannot convert index %s to a number: %w", element, err)
}
// Ignore duplicates
_, exists := seenIndices[index]
if !exists {
indices = append(indices, big.NewInt(0).SetUint64(index))
seenIndices[index] = true
}
}
// Read the tree files to get the details
amountRPL := []*big.Int{}
amountETH := []*big.Int{}
merkleProofs := [][]common.Hash{}
// Populate the interval info for each one
for _, index := range indices {
intervalInfo, err := rprewards.GetIntervalInfo(rp, cfg, nodeAddress, index.Uint64())
if err != nil {
return nil, nil, nil, nil, err
}
// Validate
if !intervalInfo.TreeFileExists {
return nil, nil, nil, nil, fmt.Errorf("rewards tree file '%s' doesn't exist", intervalInfo.TreeFilePath)
}
if !intervalInfo.MerkleRootValid {
return nil, nil, nil, nil, fmt.Errorf("merkle root for rewards tree file '%s' doesn't match the canonical merkle root for interval %d", intervalInfo.TreeFilePath, index.Uint64())
}
// Get the rewards from it
if intervalInfo.NodeExists {
rplForInterval := big.NewInt(0)
rplForInterval.Add(rplForInterval, &intervalInfo.CollateralRplAmount.Int)
rplForInterval.Add(rplForInterval, &intervalInfo.ODaoRplAmount.Int)
ethForInterval := big.NewInt(0)
ethForInterval.Add(ethForInterval, &intervalInfo.SmoothingPoolEthAmount.Int)
amountRPL = append(amountRPL, rplForInterval)
amountETH = append(amountETH, ethForInterval)
merkleProofs = append(merkleProofs, intervalInfo.MerkleProof)
}
}
// Return
return indices, amountRPL, amountETH, merkleProofs, nil
}