-
Notifications
You must be signed in to change notification settings - Fork 105
/
send.go
304 lines (262 loc) · 7.96 KB
/
send.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
package node
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
"github.com/rocket-pool/smartnode/shared/services"
"github.com/rocket-pool/smartnode/shared/types/api"
"github.com/rocket-pool/smartnode/shared/utils/eth1"
)
func canNodeSend(c *cli.Context, amountWei *big.Int, token string, to common.Address) (*api.CanNodeSendResponse, error) {
// Get services
if err := services.RequireNodeWallet(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
ec, err := services.GetEthClient(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
// Response
response := api.CanNodeSendResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Get the sending opts
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
// Get the well-known contracts
rplContract, err := rp.GetContract("rocketTokenRPL", nil)
if err != nil {
return nil, fmt.Errorf("error getting RPL contract address: %w", err)
}
fsrplContract, err := rp.GetContract("rocketTokenRPLFixedSupply", nil)
if err != nil {
return nil, fmt.Errorf("error getting legacy RPL contract address: %w", err)
}
rethContract, err := rp.GetContract("rocketTokenRETH", nil)
if err != nil {
return nil, fmt.Errorf("error getting rETH contract address: %w", err)
}
// Error out if the recipient is one of the contract addresses
if to == *rplContract.Address {
return nil, fmt.Errorf("sending tokens to the RPL contract address is prohibited for safety")
}
if to == *fsrplContract.Address {
return nil, fmt.Errorf("sending tokens to the legacy RPL contract address is prohibited for safety")
}
if to == *rethContract.Address {
return nil, fmt.Errorf("sending tokens to the rETH contract address is prohibited for safety")
}
// Handle explicit token addresses
if strings.HasPrefix(token, "0x") {
tokenAddress := common.HexToAddress(token)
// Error out if using one of the well-known ones
if tokenAddress == *rplContract.Address {
return nil, fmt.Errorf("sending RPL via the token address is prohibited for safety; please use 'rpl' as the token to send instead of its address")
}
if tokenAddress == *fsrplContract.Address {
return nil, fmt.Errorf("sending legacy RPL via the token address is prohibited for safety; please use 'fsrpl' as the token to send instead of its address")
}
if tokenAddress == *rethContract.Address {
return nil, fmt.Errorf("sending rETH via the token address is prohibited for safety; please use 'reth' as the token to send instead of its address")
}
// Create the ERC20 binding
contract, err := eth.NewErc20Contract(tokenAddress, ec, nil)
if err != nil {
return nil, fmt.Errorf("error creating ERC20 contract binding: %w", err)
}
response.TokenName = contract.Name
response.TokenSymbol = contract.Symbol
// Get the balance
balance, err := contract.BalanceOf(nodeAccount.Address, nil)
if err != nil {
return nil, fmt.Errorf("error getting ERC20 balance: %w", err)
}
response.Balance = balance
response.InsufficientBalance = (amountWei.Cmp(balance) > 0)
// Get the gas info
gasInfo, err := contract.EstimateTransferGas(to, amountWei, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
} else {
// Handle well-known token types
switch token {
case "eth":
// Check node ETH balance
ethBalanceWei, err := ec.BalanceAt(context.Background(), nodeAccount.Address, nil)
if err != nil {
return nil, err
}
response.Balance = ethBalanceWei
response.InsufficientBalance = (amountWei.Cmp(ethBalanceWei) > 0)
gasInfo, err := eth.EstimateSendTransactionGas(ec, to, nil, false, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
case "rpl":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Check node RPL balance
rplBalanceWei, err := tokens.GetRPLBalance(rp, nodeAccount.Address, nil)
if err != nil {
return nil, err
}
response.Balance = rplBalanceWei
response.InsufficientBalance = (amountWei.Cmp(rplBalanceWei) > 0)
gasInfo, err := tokens.EstimateTransferRPLGas(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
case "fsrpl":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Check node fixed-supply RPL balance
fixedSupplyRplBalanceWei, err := tokens.GetFixedSupplyRPLBalance(rp, nodeAccount.Address, nil)
if err != nil {
return nil, err
}
response.Balance = fixedSupplyRplBalanceWei
response.InsufficientBalance = (amountWei.Cmp(fixedSupplyRplBalanceWei) > 0)
gasInfo, err := tokens.EstimateTransferFixedSupplyRPLGas(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
case "reth":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Check node rETH balance
rethBalanceWei, err := tokens.GetRETHBalance(rp, nodeAccount.Address, nil)
if err != nil {
return nil, err
}
response.Balance = rethBalanceWei
response.InsufficientBalance = (amountWei.Cmp(rethBalanceWei) > 0)
gasInfo, err := tokens.EstimateTransferRETHGas(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
}
}
// Update & return response
response.CanSend = !response.InsufficientBalance
return &response, nil
}
func nodeSend(c *cli.Context, amountWei *big.Int, token string, to common.Address) (*api.NodeSendResponse, error) {
// Get services
if err := services.RequireNodeWallet(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
ec, err := services.GetEthClient(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
// Response
response := api.NodeSendResponse{}
// 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)
}
// Handle explicit token addresses
if strings.HasPrefix(token, "0x") {
tokenAddress := common.HexToAddress(token)
contract, err := eth.NewErc20Contract(tokenAddress, ec, nil)
if err != nil {
return nil, fmt.Errorf("error creating ERC20 contract binding: %w", err)
}
tx, err := contract.Transfer(to, amountWei, opts)
if err != nil {
return nil, err
}
response.TxHash = tx.Hash()
} else {
// Handle token type
switch token {
case "eth":
// Transfer ETH
opts.Value = amountWei
hash, err := eth.SendTransaction(ec, to, w.GetChainID(), nil, false, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
case "rpl":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Transfer RPL
hash, err := tokens.TransferRPL(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
case "fsrpl":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Transfer fixed-supply RPL
hash, err := tokens.TransferFixedSupplyRPL(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
case "reth":
// Get RocketStorage
if err := services.RequireRocketStorage(c); err != nil {
return nil, err
}
// Transfer rETH
hash, err := tokens.TransferRETH(rp, to, amountWei, opts)
if err != nil {
return nil, err
}
response.TxHash = hash
}
}
// Return response
return &response, nil
}