-
Notifications
You must be signed in to change notification settings - Fork 3k
/
send.go
111 lines (100 loc) · 3.21 KB
/
send.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
package transactions
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
)
type SendTxOpt func(cfg *sendTxCfg)
type ErrWithData interface {
ErrorData() interface{}
}
type sendTxCfg struct {
receiptStatus uint64
}
func makeSendTxCfg(opts ...SendTxOpt) *sendTxCfg {
cfg := &sendTxCfg{
receiptStatus: types.ReceiptStatusSuccessful,
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
func WithReceiptFail() SendTxOpt {
return func(cfg *sendTxCfg) {
cfg.receiptStatus = types.ReceiptStatusFailed
}
}
func RequireSendTx(t *testing.T, ctx context.Context, client *ethclient.Client, candidate txmgr.TxCandidate, privKey *ecdsa.PrivateKey, opts ...SendTxOpt) {
_, _, err := SendTx(ctx, client, candidate, privKey, opts...)
require.NoError(t, err, "Failed to send transaction")
}
func SendTx(ctx context.Context, client *ethclient.Client, candidate txmgr.TxCandidate, privKey *ecdsa.PrivateKey, opts ...SendTxOpt) (*types.Transaction, *types.Receipt, error) {
cfg := makeSendTxCfg(opts...)
from := crypto.PubkeyToAddress(privKey.PublicKey)
chainID, err := client.ChainID(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get chain ID: %w", err)
}
nonce, err := client.PendingNonceAt(ctx, from)
if err != nil {
return nil, nil, fmt.Errorf("failed to get next nonce: %w", err)
}
latestBlock, err := client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to get latest block: %w", err)
}
gasFeeCap := new(big.Int).Mul(latestBlock.BaseFee, big.NewInt(3))
gasTipCap := big.NewInt(1 * params.GWei)
if gasFeeCap.Cmp(gasTipCap) < 0 {
// gasTipCap can't be higher than gasFeeCap
// Since there's a minimum gasTipCap to be accepted, increase the gasFeeCap. Extra will be refunded anyway.
gasFeeCap = gasTipCap
}
msg := ethereum.CallMsg{
From: from,
To: candidate.To,
Value: candidate.Value,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Data: candidate.TxData,
}
gas, err := client.EstimateGas(ctx, msg)
if err != nil {
var errWithData ErrWithData
if errors.As(err, &errWithData) {
return nil, nil, fmt.Errorf("failed to estimate gas. errdata: %v err: %w", errWithData.ErrorData(), err)
}
return nil, nil, fmt.Errorf("failed to estimate gas: %w", err)
}
tx := types.MustSignNewTx(privKey, types.LatestSignerForChainID(chainID), &types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
To: candidate.To,
Value: candidate.Value,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Data: candidate.TxData,
Gas: gas,
})
err = client.SendTransaction(ctx, tx)
if err != nil {
return nil, nil, fmt.Errorf("failed to send transaction: %w", err)
}
receipt, err := wait.ForReceipt(ctx, client, tx.Hash(), cfg.receiptStatus)
if err != nil {
return nil, nil, fmt.Errorf("failed to find OK receipt: %w", err)
}
return tx, receipt, nil
}