/
transaction.go
113 lines (89 loc) · 2.69 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
package ethereumWallet
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/Amirilidan78/ethereum-wallet/geth"
"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/ethereum/go-ethereum/params"
"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 := uint64(21000)
gasFeeCap := big.NewInt(1500 * params.GWei)
gasTipCap := big.NewInt(100 * params.GWei)
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
}
fmt.Println(transaction)
return transaction.Hash().Hex(), nil
}
func createERC20Transaction(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)
return &bind.TransactOpts{
From: fromAddress,
Nonce: big.NewInt(int64(n)),
Signer: func(addr common.Address, localTx *types.Transaction) (*types.Transaction, error) {
return types.SignTx(localTx, signer, privateRCDSA)
},
}, nil
}