-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.go
109 lines (92 loc) · 2.74 KB
/
blockchain.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
package multicall
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
// readContract makes a call to a contract and returns the returned bytecode.
func readContract(client *ethclient.Client, from, to *common.Address, encodedCall []byte) ([]byte, error) {
if from == nil {
from = &ZERO_ADDRESS
}
result, err := client.CallContract(context.Background(),
ethereum.CallMsg{
From: *from,
To: to,
Data: encodedCall,
},
nil,
)
if err != nil {
return nil, err
}
return result, nil
}
// createTransaction creates a new transaction object.
func createTransaction(
client *ethclient.Client,
from *common.Address,
to *common.Address,
msgValue *big.Int,
callData []byte,
) (*types.Transaction, error) {
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
gasLimit, err := client.EstimateGas(
context.Background(),
ethereum.CallMsg{
From: *from, // the sender of the 'transaction'
To: to, // the destination contract (nil for contract creation)
Gas: 0, // if 0, the call executes with near-infinite gas
// GasPrice: gasPrice // wei <-> gas exchange ratio
GasFeeCap: gasPrice, // EIP-1559 fee cap per gas.
GasTipCap: gasPrice, // EIP-1559 tip per gas.
Value: msgValue,
Data: callData,
},
)
if err != nil {
return nil, err
}
nonce, err := client.PendingNonceAt(context.Background(), *from)
if err != nil {
return nil, err
}
return types.NewTransaction(nonce, *to, msgValue, gasLimit, gasPrice, nil), nil
}
// sendSignedTransaction sends a signed transaction
func sendSignedTransaction(client *ethclient.Client, tx *types.Transaction) (*types.Receipt, error) {
err := client.SendTransaction(context.Background(), tx)
if err != nil {
return nil, fmt.Errorf("error sending transaction (txHash=%v): %v", tx.Hash(), err)
}
// @note implement retry to bump gas
d := time.Now().Add(MINING_WAIT_DURATION)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
receipt, err := bind.WaitMined(ctx, client, tx)
if err != nil {
return nil, fmt.Errorf("error while waiting for receipt (txHash=%v): %v", tx.Hash(), err)
}
return receipt, nil
}
func parseRevertData(err error) ([]byte, bool) {
var ec rpc.Error
var ed rpc.DataError
if errors.As(err, &ec) && errors.As(err, &ed) && ec.ErrorCode() == 3 {
revertData := hexutil.MustDecode(ed.ErrorData().(string))
return revertData, true
}
return nil, false
}