/
writeMethod.go
163 lines (145 loc) · 4.99 KB
/
writeMethod.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package writer
import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
"os"
"time"
ethchain "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
)
const DefaultGasLimit = 4500000
func SendContractTransaction(client *ethclient.Client, from, toAddress common.Address, value *big.Int, privateKey *ecdsa.PrivateKey, input []byte, gasLimitSetting uint64) (common.Hash, error) {
// Ensure a valid value field and resolve the account nonce
logger := log.New("func", "SendContractTransaction")
nonce, err := client.PendingNonceAt(context.Background(), from)
if err != nil {
logger.Error("PendingNonceAt", "error", err)
return common.Hash{}, err
}
gasPrice, err := client.SuggestGasPrice(context.Background())
//gasPrice = big.NewInt(1000 000 000 000)
if err != nil {
logger.Error("SuggestGasPrice", "error", err)
return common.Hash{}, err
}
gasLimit := uint64(DefaultGasLimit) // in units
//If the contract surely has code (or code is not needed), estimate the transaction
msg := ethchain.CallMsg{From: from, To: &toAddress, GasPrice: gasPrice, Value: value, Data: input}
gasLimit, err = client.EstimateGas(context.Background(), msg)
if err != nil {
logger.Error("Contract exec failed", "error", err)
return common.Hash{}, err
}
if gasLimit < 1 {
//gasLimit = 866328
gasLimit = 2100000
}
gasLimit = uint64(DefaultGasLimit)
if gasLimitSetting != 0 {
gasLimit = gasLimitSetting // in units
}
// Create the transaction, sign it and schedule it for execution
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, input)
chainID, _ := client.ChainID(context.Background())
logger.Info("Tx Info", "from", from, "to", toAddress, "value", value, "nonce ", nonce, " gasLimit ", gasLimit, " gasPrice ", gasPrice, " chainID ", chainID)
signer := types.LatestSignerForChainID(chainID)
signedTx, err := types.SignTx(tx, signer, privateKey)
if err != nil {
logger.Error("SignTx", "error", err)
return common.Hash{}, err
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
logger.Error("SendTransaction", "error", err)
return common.Hash{}, err
}
return signedTx.Hash(), nil
}
func GetResult(conn *ethclient.Client, txHash common.Hash, contract bool) {
logger := log.New("func", "GetResult")
logger.Info("Please waiting ", " txHash ", txHash.String())
for {
time.Sleep(time.Second)
_, isPending, err := conn.TransactionByHash(context.Background(), txHash)
if err != nil {
logger.Error("TransactionByHash", "error", err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if !isPending {
break
}
logger.Info("Please waiting, Transaction is in pending status")
}
var (
err error
receipt *types.Receipt
)
for {
time.Sleep(time.Second)
receipt, err = conn.TransactionReceipt(context.Background(), txHash)
if err == nil {
break
}
logger.Error("TransactionReceipt", "error", err)
}
if receipt.Status == types.ReceiptStatusSuccessful {
logger.Info("Transaction Success", "number", receipt.BlockNumber.Uint64())
} else if receipt.Status == types.ReceiptStatusFailed {
logger.Error("Transaction Failed ", "number", receipt.BlockNumber.Uint64())
os.Exit(1)
}
}
func queryTx(conn *ethclient.Client, txHash common.Hash, contract bool, pending bool) {
logger := log.New("func", "queryTx")
if pending {
_, isPending, err := conn.TransactionByHash(context.Background(), txHash)
if err != nil {
logger.Error("TransactionByHash", "error", err)
}
if isPending {
println("In tx_pool no validator process this, please query later")
os.Exit(0)
}
}
receipt, err := conn.TransactionReceipt(context.Background(), txHash)
if err != nil {
for {
time.Sleep(time.Millisecond * 200)
receipt, err = conn.TransactionReceipt(context.Background(), txHash)
if err == nil {
break
}
}
logger.Error("TransactionReceipt", "error", err)
}
if receipt.Status == types.ReceiptStatusSuccessful {
logger.Info("Transaction Success", "block Number", receipt.BlockNumber.Uint64())
} else if receipt.Status == types.ReceiptStatusFailed {
logger.Info("Transaction Failed ", "Block Number", receipt.BlockNumber.Uint64())
}
}
func (w Writer) handleUnpackMethodSolveType3(m Message) {
msg := ethchain.CallMsg{From: m.from, To: &m.to, Data: m.input, GasFeeCap: big.NewInt(3000000000000)}
output, err := w.conn.CallContract(context.Background(), msg, nil)
if err != nil {
log.Error("method CallContract error", "error", err)
}
err = m.abi.UnpackIntoInterface(&m.ret, m.abiMethod, output)
if err != nil {
log.Error("handleUnpackMethodSolveType3", "err", err)
}
}
func (w Writer) handleUnpackMethodSolveType4(m Message) {
msg := ethchain.CallMsg{From: m.from, To: &m.to, Data: m.input, GasFeeCap: big.NewInt(3000000000000)}
output, err := w.conn.CallContract(context.Background(), msg, nil)
if err != nil {
log.Error("method CallContract error", "error", err)
}
m.solveResult(output)
}