|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 |
| - "crypto/ecdsa" |
6 | 4 | "encoding/json"
|
7 |
| - "errors" |
| 5 | + _tx "ethereum-wallet-tool/pkg/tx" |
8 | 6 | "fmt"
|
9 |
| - "github.com/ethereum/go-ethereum" |
10 |
| - "github.com/ethereum/go-ethereum/common" |
11 |
| - "github.com/ethereum/go-ethereum/core/types" |
12 |
| - "github.com/ethereum/go-ethereum/crypto" |
13 | 7 | "github.com/ethereum/go-ethereum/ethclient"
|
14 |
| - "log" |
15 |
| - "math/big" |
16 | 8 | "os"
|
17 | 9 | "strings"
|
18 | 10 | )
|
@@ -67,81 +59,9 @@ func Transfer(val string) {
|
67 | 59 | }
|
68 | 60 |
|
69 | 61 | func transfer(mpk string, w Wallet, val string, ec *ethclient.Client) (TxRecord, error) {
|
70 |
| - privateKeyHex := mpk |
71 |
| - privateKey, err := crypto.HexToECDSA(privateKeyHex) |
72 |
| - if err != nil { |
73 |
| - log.Fatalf("Failed to decode private key: %v", err) |
74 |
| - } |
75 |
| - |
76 |
| - // 获取账户地址 |
77 |
| - publicKey := privateKey.Public() |
78 |
| - publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) |
79 |
| - if !ok { |
80 |
| - log.Fatal("error casting public key to ECDSA") |
81 |
| - } |
82 |
| - fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) |
83 |
| - |
84 |
| - // 获取账户的nonce |
85 |
| - nonce, err := ec.PendingNonceAt(context.Background(), fromAddress) |
86 |
| - if err != nil { |
87 |
| - log.Fatalf("Failed to get account nonce: %v", err) |
88 |
| - } |
89 |
| - |
90 |
| - // 设置转账金额(0.001 ETH) |
91 |
| - wei, ok := EthToWei(val) |
92 |
| - if !ok { |
93 |
| - fmt.Println("invalid eth value:", val) |
94 |
| - return TxRecord{}, errors.New(fmt.Sprintf("invalid eth value: %s", val)) |
95 |
| - } |
96 |
| - |
97 |
| - gasTipCap, err := ec.SuggestGasTipCap(context.Background()) |
98 |
| - if err != nil { |
99 |
| - log.Fatalf("Failed to suggest gas tip cap: %v", err) |
100 |
| - } |
101 |
| - |
102 |
| - // 设置 maxFeePerGas。这通常是 baseFeePerGas + maxPriorityFeePerGas,但要留有余地以适应 baseFee 的变动 |
103 |
| - baseFee, err := ec.HeaderByNumber(context.Background(), nil) |
104 |
| - if err != nil { |
105 |
| - log.Fatalf("Failed to get latest block header: %v", err) |
106 |
| - } |
107 |
| - maxFeePerGas := new(big.Int).Add(baseFee.BaseFee, gasTipCap) |
108 |
| - |
109 |
| - // 设置接收方地址 |
110 |
| - toAddress := common.HexToAddress(w.Address) |
111 |
| - |
112 |
| - // 估算 Gas 量 |
113 |
| - msg := ethereum.CallMsg{ |
114 |
| - From: fromAddress, |
115 |
| - To: &toAddress, |
116 |
| - Gas: 0, |
117 |
| - GasPrice: maxFeePerGas, |
118 |
| - Value: wei, |
119 |
| - Data: nil, |
120 |
| - } |
121 |
| - |
122 |
| - estimatedGas, err := ec.EstimateGas(context.Background(), msg) |
123 |
| - if err != nil { |
124 |
| - return TxRecord{}, fmt.Errorf("failed to estimate gas: %v", err) |
125 |
| - } |
126 |
| - fmt.Println("maxPriorityFeePerGas:", gasTipCap, "GasFeeCap:", maxFeePerGas, "Gas:", estimatedGas) |
127 |
| - tx := &types.DynamicFeeTx{ |
128 |
| - ChainID: big.NewInt(1), |
129 |
| - Nonce: nonce, |
130 |
| - GasTipCap: gasTipCap, // maxPriorityFeePerGas |
131 |
| - GasFeeCap: maxFeePerGas, // max Fee |
132 |
| - Gas: estimatedGas, |
133 |
| - To: &toAddress, |
134 |
| - Value: wei, |
135 |
| - } |
136 |
| - |
137 |
| - // 创建交易 |
138 |
| - signedTx, err := types.SignTx(types.NewTx(tx), types.NewCancunSigner(tx.ChainID), privateKey) |
139 |
| - if err != nil { |
140 |
| - return TxRecord{}, errors.New(fmt.Sprintf("signTx err:%s", err.Error())) |
141 |
| - } |
142 |
| - err = ec.SendTransaction(context.Background(), signedTx) |
| 62 | + txHash, err := _tx.Transfer(mpk, w.Address, val, nil, ec) |
143 | 63 | txR := TxRecord{
|
144 |
| - Hash: signedTx.Hash().String(), |
| 64 | + Hash: txHash, |
145 | 65 | Address: w.Address,
|
146 | 66 | PK: w.PrivateKey,
|
147 | 67 | Value: val,
|
|
0 commit comments