-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
139 lines (111 loc) · 3.2 KB
/
transaction.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
package ethereumWallet
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ranjbar-dev/ethereum-wallet/geth"
"math/big"
)
func createTransactionInput(node Node, fromAddressHex string, toAddressHex string, amountInWei *big.Int) (*types.Transaction, error) {
fromAddress := common.HexToAddress(fromAddressHex)
toAddress := common.HexToAddress(toAddressHex)
client, err := geth.GetGETHClient(node.Http)
if err != nil {
return nil, err
}
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err
}
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
To: &toAddress,
})
if err != nil {
return nil, err
}
gasFeeCap, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
gasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
return nil, err
}
chainID, err := client.NetworkID(context.Background())
if err != nil {
return nil, err
}
return types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
To: &toAddress,
Value: amountInWei,
Gas: gasLimit,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Data: nil,
}), nil
}
func signTransaction(node Node, transaction *types.Transaction, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
client, err := geth.GetGETHClient(node.Http)
if err != nil {
return nil, err
}
chainID, err := client.NetworkID(context.Background())
if err != nil {
return nil, err
}
signer := types.LatestSignerForChainID(chainID)
return types.SignTx(transaction, signer, privateKey)
}
func broadcastTransaction(node Node, transaction *types.Transaction) (string, error) {
client, err := geth.GetGETHClient(node.Http)
if err != nil {
return "", err
}
err = client.SendTransaction(context.Background(), transaction)
if err != nil {
return "", err
}
return transaction.Hash().Hex(), nil
}
func createERC20Transaction(node Node, toAddressHex string, c *ethclient.Client, ew *EthereumWallet) (*bind.TransactOpts, error) {
privateRCDSA, err := ew.PrivateKeyRCDSA()
if err != nil {
return nil, fmt.Errorf("RCDSA private key error: %v", err)
}
fromAddress := common.HexToAddress(ew.Address)
n, err := c.NonceAt(context.Background(), fromAddress, nil)
if err != nil {
return nil, err
}
chainID, err := c.NetworkID(context.Background())
if err != nil {
return nil, err
}
signer := types.LatestSignerForChainID(chainID)
gasFeeCap, err := c.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
gasTipCap, err := c.SuggestGasTipCap(context.Background())
if err != nil {
return nil, err
}
gasLimit := uint64(70000)
return &bind.TransactOpts{
From: fromAddress,
Nonce: big.NewInt(int64(n)),
GasLimit: gasLimit,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Signer: func(addr common.Address, localTx *types.Transaction) (*types.Transaction, error) {
return types.SignTx(localTx, signer, privateRCDSA)
},
}, nil
}