/
ethereum.go
168 lines (135 loc) · 3.81 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
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
164
165
166
167
168
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package ethereum
import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
geth "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
ethAbiBind "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// NewTransactionOptions created using the user's private key, figuring
// out the chain id using the client.
func NewTransactionOptions(client *ethclient.Client, privateKey *ecdsa.PrivateKey) (*ethAbiBind.TransactOpts, error) {
chainId, err := client.ChainID(context.Background())
if err != nil {
return nil, fmt.Errorf(
"failed to use the client to get the chain id! %v",
err,
)
}
transactionOpts, err := ethAbiBind.NewKeyedTransactorWithChainID(privateKey, chainId)
if err != nil {
return nil, fmt.Errorf(
"failed to create new transaction options! %v",
err,
)
}
return transactionOpts, nil
}
// UpdateValue sets the Value field in the transaction options to 0
func UpdateValue(options *ethAbiBind.TransactOpts) {
options.Value = big.NewInt(0)
}
// GetGasPriceTipHardhat to accomodate hardhat's bug with the gas price
func GetGasPriceTipHardhat(ctx context.Context, client *ethclient.Client) (*big.Int, error) {
header, err := client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, fmt.Errorf(
"failed to get the header by number! %v",
err,
)
}
gasBaseFee := header.BaseFee
gasPrice, err := client.SuggestGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf(
"failed to suggest the gas price using the client! %v",
err,
)
}
var (
gasPriceTip = new(big.Int)
zero = big.NewInt(0)
)
gasPriceTip.Sub(gasPrice, gasBaseFee)
if gasPriceTip.Cmp(zero) <= 0 {
gasPriceTip = gasBaseFee
}
return gasPriceTip, nil
}
// UpdateGasAmounts by suggesting the gas tip cap then setting the gas
// limit to a fixed amount
func UpdateGasAmounts(ctx context.Context, client *ethclient.Client, options *ethAbiBind.TransactOpts) error {
gasTipCap, err := client.SuggestGasTipCap(ctx)
if err != nil {
return fmt.Errorf(
"failed to suggest the gas tip cap! %v",
err,
)
}
options.GasTipCap = gasTipCap
return nil
}
func EstimateGas(client *ethclient.Client, contractAbi *abi.ABI, opts *ethAbiBind.TransactOpts, contractAddress *common.Address, method string, args ...interface{}) (uint64, error) {
var (
from = opts.From
gasPrice = opts.GasPrice
feeCap = opts.GasFeeCap
tipCap = opts.GasTipCap
value = opts.Value
)
data, err := contractAbi.Pack(method, args...)
if err != nil {
return 0, fmt.Errorf(
"Failed to construct a transaction to estimate gas with! %w",
err,
)
}
msg := geth.CallMsg{
From: from,
To: contractAddress,
Gas: 0,
GasPrice: gasPrice,
GasFeeCap: feeCap,
GasTipCap: tipCap,
Value: value,
Data: data,
AccessList: []ethTypes.AccessTuple{},
}
return client.EstimateGas(opts.Context, msg)
}
// Calls a method on a bound contract, first simulating it to see if it reverts
func MakeTransaction(contract *ethAbiBind.BoundContract, opts *ethAbiBind.TransactOpts, method string, args ...interface{}) (*ethTypes.Transaction, error) {
callOptions := ethAbiBind.CallOpts{
Pending: false,
From: opts.From,
BlockNumber: nil,
Context: opts.Context,
}
err := contract.Call(
&callOptions,
nil,
method,
args...,
)
if err != nil {
return nil, fmt.Errorf(
"transaction simulation failed calling method %s! %w",
method,
err,
)
}
transaction, err := contract.Transact(
opts,
method,
args...,
)
return transaction, err
}