forked from daoleno/uniswapv3-sdk
/
tx.go
62 lines (53 loc) 路 1.6 KB
/
tx.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
package helper
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
//SendTx Send a real transaction to the blockchain.
func SendTX(client *ethclient.Client, toAddress common.Address, value *big.Int,
data []byte, w *Wallet) (*types.Transaction, error) {
signedTx, err := TryTX(client, toAddress, value, data, w)
if err != nil {
return nil, err
}
return signedTx, client.SendTransaction(context.Background(), signedTx)
}
//Trytx Trying to send a transaction, it just return the transaction hash if success.
func TryTX(client *ethclient.Client, toAddress common.Address, value *big.Int,
data []byte, w *Wallet) (*types.Transaction, error) {
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
From: w.PublicKey,
To: &toAddress,
GasPrice: gasPrice,
Value: value,
Data: data,
})
if err != nil {
return nil, err
}
fmt.Printf("gasLimit=%d, gasPrice=%d\n", gasLimit, gasPrice.Uint64())
nounc, err := client.NonceAt(context.Background(), w.PublicKey, nil)
if err != nil {
return nil, err
}
tx := types.NewTransaction(nounc, toAddress, value,
gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
if err != nil {
return nil, err
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), w.PrivateKey)
if err != nil {
return nil, err
}
return signedTx, nil
}