-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathethereum.go
71 lines (59 loc) · 1.83 KB
/
ethereum.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
package bridge
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// Function to send a transaction
func SendTransaction(client *ethclient.Client, key *keystore.Key, password string, calldata []byte, to string, value *big.Int) (*types.Transaction, error) {
chainID, chainIDErr := client.ChainID(context.Background())
if chainIDErr != nil {
return nil, chainIDErr
}
recipientAddress := common.HexToAddress(to)
callMsg := ethereum.CallMsg{
From: key.Address,
To: &recipientAddress,
Value: value,
Data: calldata,
}
gasLimit, gasLimitErr := client.EstimateGas(context.Background(), callMsg)
if gasLimitErr != nil {
return nil, gasLimitErr
}
baseFee, baseFeeErr := client.SuggestGasPrice(context.Background())
if baseFeeErr != nil {
return nil, baseFeeErr
}
gasTipCap, gasTipCapErr := client.SuggestGasTipCap(context.Background())
if gasTipCapErr != nil {
return nil, gasTipCapErr
}
nonce, nonceErr := client.PendingNonceAt(context.Background(), key.Address)
if nonceErr != nil {
return nil, nonceErr
}
rawTransaction := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasTipCap: gasTipCap,
GasFeeCap: baseFee,
Gas: gasLimit,
To: &recipientAddress,
Value: value,
Data: calldata,
})
signedTransaction, signedTransactionErr := types.SignTx(rawTransaction, types.NewLondonSigner(chainID), key.PrivateKey)
if signedTransactionErr != nil {
return nil, signedTransactionErr
}
sendTransactionErr := client.SendTransaction(context.Background(), signedTransaction)
if sendTransactionErr != nil {
return nil, sendTransactionErr
}
return signedTransaction, nil
}