-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtransfer.go
216 lines (182 loc) · 6.71 KB
/
transfer.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
package cmd
import (
"context"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/shopspring/decimal"
"github.com/spf13/cobra"
)
var transferUnit string
var transferNotCheck bool
var transferHexData string
func init() {
transferCmd.Flags().StringVarP(&transferUnit, "unit", "u", "ether", "wei | gwei | ether, unit of amount")
transferCmd.Flags().BoolVarP(&transferNotCheck, "not-check", "", false, "don't check result, return immediately after send transaction")
transferCmd.Flags().StringVarP(&transferHexData, "hex-data", "", "", "the payload hex data when transfer")
}
func validationTransferCmdOpts() bool {
// validation
if !contains([]string{unitWei, unitGwei, unitEther}, transferUnit) {
log.Fatalf("invalid option for --unit: %v", transferUnit)
return false
}
if globalOptPrivateKey == "" {
log.Fatalf("--private-key is required for transfer command")
return false
}
if !isValidHexString(transferHexData) {
log.Fatalf("--hex-data must hex string")
return false
}
return true
}
// https://docs.optimism.io/builders/tools/build/oracles#gas-oracle
var l1GasPriceOracle = common.HexToAddress("0x420000000000000000000000000000000000000F")
const gasUsedByTransferEth = 21000 // The gas used by ETH transfer tx is 21000
func getGasPrice(client *ethclient.Client) (*big.Int, error) {
var gasPrice *big.Int
if globalOptGasPrice != "" {
gasPriceDecimal, err := decimal.NewFromString(globalOptGasPrice)
if err != nil {
return nil, err
}
// convert from gwei to wei
return gasPriceDecimal.Mul(decimal.RequireFromString("1000000000")).BigInt(), nil
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if globalOptChain == nodeMainnet {
// in case of mainnet, get gap price from ethgasstation
gasPrice, err = getGasPriceFromEthGasStation()
if err != nil {
log.Fatalf("getGasPrice fail: %s", err)
}
}
log.Printf("gas price %v wei", gasPrice)
return gasPrice, nil
}
var transferCmd = &cobra.Command{
Use: "transfer <target-address> <amount>",
Short: "Transfer native token",
Long: "Transfer AMOUNT of eth to TARGET-ADDRESS, special word `all` is valid amount. unit is ether, can be changed by --unit.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("requires target-address and amount")
}
if len(args) == 1 {
return fmt.Errorf("requires amount")
}
if len(args) > 2 {
return fmt.Errorf("too many args")
}
targetAddress := args[0]
transferAmt := args[1]
if !isValidEthAddress(targetAddress) {
return fmt.Errorf("%v is not a valid eth address", targetAddress)
}
if transferAmt == "all" {
return nil
} else {
_, err := decimal.NewFromString(transferAmt)
if err != nil {
return fmt.Errorf("%v is not a valid amount", transferAmt)
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if !validationTransferCmdOpts() {
_ = cmd.Help()
os.Exit(1)
}
targetAddress := args[0]
transferAmt := args[1]
InitGlobalClient(globalOptNodeUrl)
ctx := context.Background()
gasPrice, err := getGasPrice(globalClient.EthClient)
checkErr(err)
var amount decimal.Decimal
var amountInWei decimal.Decimal
if transferAmt == "all" {
// transfer all balance (only reserve some gas just pay for this tx) to target address
fromAddr := extractAddressFromPrivateKey(hexToPrivateKey(globalOptPrivateKey))
// Get current balance
balance, err := globalClient.EthClient.BalanceAt(ctx, fromAddr, nil)
checkErr(err)
log.Printf("balance of %v is %v wei", fromAddr.String(), balance.String())
var gasLimit = globalOptGasLimit
if globalOptGasLimit == 0 {
// If globalOptGasLimit is not specified, use the default value gasUsedByTransferEth
gasLimit = gasUsedByTransferEth
}
gasMayUsed := big.NewInt(0).Mul(gasPrice, big.NewInt(int64(gasLimit)))
if gasMayUsed.Cmp(balance) > 0 {
log.Fatalf("insufficient balance %v, can not pay for gas %v", balance, gasMayUsed)
}
l1GasPriceOracleExisted, err := isContractAddress(globalClient.EthClient, l1GasPriceOracle)
if err != nil {
log.Fatalf("isContractAddress failed %v", err)
}
// If l1GasPriceOracle is existed in current chain, the current chain is probability L2 chain, and need L1 fee when submit tx
// We must subtract `L2 fee + L1 fee` if use want transfer 'all' native token
if l1GasPriceOracleExisted {
// Estimate L1 Fee
privateKey := hexToPrivateKey(globalOptPrivateKey)
fromAddress := extractAddressFromPrivateKey(privateKey)
toAddr := common.HexToAddress(targetAddress)
signedTx, err := BuildSignedTx(globalClient.EthClient, privateKey, &fromAddress, &toAddr, big.NewInt(0).Sub(balance, gasMayUsed), gasPrice, common.FromHex(transferHexData), nil)
if err != nil {
log.Fatalf("BuildSignedTx fail: %v", err)
}
rawTx, err := GenRawTx(signedTx)
if err != nil {
log.Fatalf("GenRawTx fail: %v", err)
}
l1Fee, err := getL1Fee(globalClient.RpcClient, rawTx)
if err != nil {
log.Fatalf("getL1Fee failed %v", err)
}
log.Printf("L2 fee %v", gasMayUsed.String())
log.Printf("L1 fee %v", l1Fee.String())
// L2 fee + L1 fee
gasMayUsed.Add(gasMayUsed, l1Fee)
}
amountBigInt := big.NewInt(0).Sub(balance, gasMayUsed)
amount = bigIntToDecimal(amountBigInt)
amountInWei = amount
} else {
amount = decimal.RequireFromString(transferAmt)
amountInWei = unify2Wei(amount, transferUnit)
}
if tx, err := TransferHelper(globalClient.RpcClient, globalClient.EthClient, globalOptPrivateKey, targetAddress, amountInWei.BigInt(), gasPrice, common.FromHex(transferHexData)); err != nil {
log.Fatalf("transfer fail: %v", err)
} else {
log.Printf("transfer finished, tx = %v", tx)
}
},
}
func TransferHelper(rcpClient *rpc.Client, client *ethclient.Client, privateKeyHex string, toAddress string, amountInWei *big.Int, gasPrice *big.Int, data []byte) (string, error) {
log.Printf("transfer %v ether (%v wei) from %v to %v",
wei2Other(bigIntToDecimal(amountInWei), unitEther).String(),
amountInWei.String(),
extractAddressFromPrivateKey(hexToPrivateKey(privateKeyHex)).String(),
toAddress)
var toAddr = common.HexToAddress(toAddress)
return Transact(rcpClient, client, hexToPrivateKey(privateKeyHex), &toAddr, amountInWei, gasPrice, data)
}
// getL1Fee call contract function getL1Fee to get the L1 fee
func getL1Fee(rcpClient *rpc.Client, rawTx string) (*big.Int, error) {
txInputData, err := buildTxInputData("getL1Fee(bytes)", []string{rawTx})
if err != nil {
return nil, err
}
output, err := Call(rcpClient, l1GasPriceOracle, txInputData)
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(output), nil
}