-
Notifications
You must be signed in to change notification settings - Fork 105
/
stake-rpl.go
324 lines (270 loc) · 9.27 KB
/
stake-rpl.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
package node
import (
"fmt"
"math/big"
"strconv"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
"github.com/rocket-pool/smartnode/shared/services/gas"
"github.com/rocket-pool/smartnode/shared/services/rocketpool"
cliutils "github.com/rocket-pool/smartnode/shared/utils/cli"
"github.com/rocket-pool/smartnode/shared/utils/math"
)
func nodeStakeRpl(c *cli.Context) error {
// Get RP client
rp, err := rocketpool.NewClientFromCtx(c).WithReady()
if err != nil {
return err
}
defer rp.Close()
// Get node status
status, err := rp.NodeStatus()
if err != nil {
return err
}
// If a custom nonce is set, print the multi-transaction warning
if c.GlobalUint64("nonce") != 0 {
cliutils.PrintMultiTransactionNonceWarning()
}
// Check for fixed-supply RPL balance
rplBalance := *(status.AccountBalances.RPL)
if status.AccountBalances.FixedSupplyRPL.Cmp(big.NewInt(0)) > 0 {
// Confirm swapping RPL
if c.Bool("swap") || cliutils.Confirm(fmt.Sprintf("The node has a balance of %.6f old RPL. Would you like to swap it for new RPL before staking?", math.RoundDown(eth.WeiToEth(status.AccountBalances.FixedSupplyRPL), 6))) {
// Check allowance
allowance, err := rp.GetNodeSwapRplAllowance()
if err != nil {
return err
}
if allowance.Allowance.Cmp(status.AccountBalances.FixedSupplyRPL) < 0 {
fmt.Println("Before swapping legacy RPL for new RPL, you must first give the new RPL contract approval to interact with your legacy RPL.")
fmt.Println("This only needs to be done once for your node.")
// If a custom nonce is set, print the multi-transaction warning
if c.GlobalUint64("nonce") != 0 {
cliutils.PrintMultiTransactionNonceWarning()
}
// Calculate max uint256 value
maxApproval := big.NewInt(2)
maxApproval = maxApproval.Exp(maxApproval, big.NewInt(256), nil)
maxApproval = maxApproval.Sub(maxApproval, big.NewInt(1))
// Get approval gas
approvalGas, err := rp.NodeSwapRplApprovalGas(maxApproval)
if err != nil {
return err
}
// Assign max fees
err = gas.AssignMaxFeeAndLimit(approvalGas.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm("Do you want to let the new RPL contract interact with your legacy RPL?")) {
fmt.Println("Cancelled.")
return nil
}
// Approve RPL for swapping
response, err := rp.NodeSwapRplApprove(maxApproval)
if err != nil {
return err
}
hash := response.ApproveTxHash
fmt.Printf("Approving legacy RPL for swapping...\n")
cliutils.PrintTransactionHash(rp, hash)
if _, err = rp.WaitForTransaction(hash); err != nil {
return err
}
fmt.Println("Successfully approved access to legacy RPL.")
// If a custom nonce is set, increment it for the next transaction
if c.GlobalUint64("nonce") != 0 {
rp.IncrementCustomNonce()
}
}
// Check RPL can be swapped
canSwap, err := rp.CanNodeSwapRpl(status.AccountBalances.FixedSupplyRPL)
if err != nil {
return err
}
if !canSwap.CanSwap {
fmt.Println("Cannot swap RPL:")
if canSwap.InsufficientBalance {
fmt.Println("The node's old RPL balance is insufficient.")
}
return nil
}
fmt.Println("RPL Swap Gas Info:")
// Assign max fees
err = gas.AssignMaxFeeAndLimit(canSwap.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm(fmt.Sprintf("Are you sure you want to swap %.6f old RPL for new RPL?", math.RoundDown(eth.WeiToEth(status.AccountBalances.FixedSupplyRPL), 6)))) {
fmt.Println("Cancelled.")
return nil
}
// Swap RPL
swapResponse, err := rp.NodeSwapRpl(status.AccountBalances.FixedSupplyRPL)
if err != nil {
return err
}
fmt.Printf("Swapping old RPL for new RPL...\n")
cliutils.PrintTransactionHash(rp, swapResponse.SwapTxHash)
if _, err = rp.WaitForTransaction(swapResponse.SwapTxHash); err != nil {
return err
}
// Log
fmt.Printf("Successfully swapped %.6f old RPL for new RPL.\n", math.RoundDown(eth.WeiToEth(status.AccountBalances.FixedSupplyRPL), 6))
fmt.Println("")
// If a custom nonce is set, increment it for the next transaction
if c.GlobalUint64("nonce") != 0 {
rp.IncrementCustomNonce()
}
// Get new account RPL balance
rplBalance.Add(status.AccountBalances.RPL, status.AccountBalances.FixedSupplyRPL)
}
}
// Get stake amount
var amountWei *big.Int
if c.String("amount") == "min8" {
// Set amount to min per 8 ETH minipool RPL stake
rplPrice, err := rp.RplPrice()
if err != nil {
return err
}
amountWei = rplPrice.MinPer8EthMinipoolRplStake
} else if c.String("amount") == "min16" {
// Set amount to min per 16 ETH minipool RPL stake
rplPrice, err := rp.RplPrice()
if err != nil {
return err
}
amountWei = rplPrice.MinPer16EthMinipoolRplStake
} else if c.String("amount") == "all" {
// Set amount to node's entire RPL balance
amountWei = &rplBalance
} else if c.String("amount") != "" {
// Parse amount
stakeAmount, err := strconv.ParseFloat(c.String("amount"), 64)
if err != nil {
return fmt.Errorf("Invalid stake amount '%s': %w", c.String("amount"), err)
}
amountWei = eth.EthToWei(stakeAmount)
} else {
// Get min/max per minipool RPL stake amounts
rplPrice, err := rp.RplPrice()
if err != nil {
return err
}
minAmount8 := rplPrice.MinPer8EthMinipoolRplStake
minAmount16 := rplPrice.MinPer16EthMinipoolRplStake
// Prompt for amount option
amountOptions := []string{
fmt.Sprintf("The minimum minipool stake amount for an 8-ETH minipool (%.6f RPL)?", math.RoundUp(eth.WeiToEth(minAmount8), 6)),
fmt.Sprintf("The minimum minipool stake amount for a 16-ETH minipool (%.6f RPL)?", math.RoundUp(eth.WeiToEth(minAmount16), 6)),
fmt.Sprintf("Your entire RPL balance (%.6f RPL)?", math.RoundDown(eth.WeiToEth(&rplBalance), 6)),
"A custom amount",
}
selected, _ := cliutils.Select("Please choose an amount of RPL to stake:", amountOptions)
switch selected {
case 0:
amountWei = minAmount8
case 1:
amountWei = minAmount16
case 2:
amountWei = &rplBalance
}
// Prompt for custom amount
if amountWei == nil {
inputAmount := cliutils.Prompt("Please enter an amount of RPL to stake:", "^\\d+(\\.\\d+)?$", "Invalid amount")
stakeAmount, err := strconv.ParseFloat(inputAmount, 64)
if err != nil {
return fmt.Errorf("Invalid stake amount '%s': %w", inputAmount, err)
}
amountWei = eth.EthToWei(stakeAmount)
}
}
// Check allowance
allowance, err := rp.GetNodeStakeRplAllowance()
if err != nil {
return err
}
if allowance.Allowance.Cmp(amountWei) < 0 {
fmt.Println("Before staking RPL, you must first give the staking contract approval to interact with your RPL.")
fmt.Println("This only needs to be done once for your node.")
// If a custom nonce is set, print the multi-transaction warning
if c.GlobalUint64("nonce") != 0 {
cliutils.PrintMultiTransactionNonceWarning()
}
// Calculate max uint256 value
maxApproval := big.NewInt(2)
maxApproval = maxApproval.Exp(maxApproval, big.NewInt(256), nil)
maxApproval = maxApproval.Sub(maxApproval, big.NewInt(1))
// Get approval gas
approvalGas, err := rp.NodeStakeRplApprovalGas(maxApproval)
if err != nil {
return err
}
// Assign max fees
err = gas.AssignMaxFeeAndLimit(approvalGas.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm("Do you want to let the staking contract interact with your RPL?")) {
fmt.Println("Cancelled.")
return nil
}
// Approve RPL for staking
response, err := rp.NodeStakeRplApprove(maxApproval)
if err != nil {
return err
}
hash := response.ApproveTxHash
fmt.Printf("Approving RPL for staking...\n")
cliutils.PrintTransactionHash(rp, hash)
if _, err = rp.WaitForTransaction(hash); err != nil {
return err
}
fmt.Println("Successfully approved staking access to RPL.")
// If a custom nonce is set, increment it for the next transaction
if c.GlobalUint64("nonce") != 0 {
rp.IncrementCustomNonce()
}
}
// Check RPL can be staked
canStake, err := rp.CanNodeStakeRpl(amountWei)
if err != nil {
return err
}
if !canStake.CanStake {
fmt.Println("Cannot stake RPL:")
if canStake.InsufficientBalance {
fmt.Println("The node's RPL balance is insufficient.")
}
return nil
}
fmt.Println("RPL Stake Gas Info:")
// Assign max fees
err = gas.AssignMaxFeeAndLimit(canStake.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm(fmt.Sprintf("Are you sure you want to stake %.6f RPL? You will not be able to unstake this RPL until you exit your validators and close your minipools, or reach over 100%% collateral!", math.RoundDown(eth.WeiToEth(amountWei), 6)))) {
fmt.Println("Cancelled.")
return nil
}
// Stake RPL
stakeResponse, err := rp.NodeStakeRpl(amountWei)
if err != nil {
return err
}
fmt.Printf("Staking RPL...\n")
cliutils.PrintTransactionHash(rp, stakeResponse.StakeTxHash)
if _, err = rp.WaitForTransaction(stakeResponse.StakeTxHash); err != nil {
return err
}
// Log & return
fmt.Printf("Successfully staked %.6f RPL.\n", math.RoundDown(eth.WeiToEth(amountWei), 6))
return nil
}