/
client.go
183 lines (157 loc) · 5.63 KB
/
client.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package txmgr
import (
"context"
"errors"
"fmt"
"math"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/O1MaGnUmO1/chainlink-common/pkg/logger"
"github.com/O1MaGnUmO1/chainlink-common/pkg/utils"
commonclient "github.com/O1MaGnUmO1/erinaceus-vrf/common/client"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/chains/evm/client"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/chains/evm/gas"
evmtypes "github.com/O1MaGnUmO1/erinaceus-vrf/core/chains/evm/types"
)
var _ TxmClient = (*evmTxmClient)(nil)
type evmTxmClient struct {
client client.Client
}
func NewEvmTxmClient(c client.Client) *evmTxmClient {
return &evmTxmClient{client: c}
}
func (c *evmTxmClient) PendingSequenceAt(ctx context.Context, addr common.Address) (evmtypes.Nonce, error) {
return c.PendingNonceAt(ctx, addr)
}
func (c *evmTxmClient) ConfiguredChainID() *big.Int {
return c.client.ConfiguredChainID()
}
func (c *evmTxmClient) BatchSendTransactions(
ctx context.Context,
attempts []TxAttempt,
batchSize int,
lggr logger.SugaredLogger,
) (
codes []commonclient.SendTxReturnCode,
txErrs []error,
broadcastTime time.Time,
successfulTxIDs []int64,
err error,
) {
// preallocate
codes = make([]commonclient.SendTxReturnCode, len(attempts))
txErrs = make([]error, len(attempts))
reqs, broadcastTime, successfulTxIDs, batchErr := batchSendTransactions(ctx, attempts, batchSize, lggr, c.client)
err = errors.Join(err, batchErr) // this error does not block processing
// safety check - exits before processing
if len(reqs) != len(attempts) {
lenErr := fmt.Errorf("Returned request data length (%d) != number of tx attempts (%d)", len(reqs), len(attempts))
err = errors.Join(err, lenErr)
lggr.Criticalw("Mismatched length", "err", err)
return
}
// for each batched tx convert response to standard error code
var wg sync.WaitGroup
wg.Add(len(reqs))
processingErr := make([]error, len(attempts))
for index := range reqs {
go func(i int) {
defer wg.Done()
// convert to tx for logging purposes - exits early if error occurs
tx, signedErr := GetGethSignedTx(attempts[i].SignedRawTx)
if signedErr != nil {
signedErrMsg := fmt.Sprintf("failed to process tx (index %d)", i)
lggr.Errorw(signedErrMsg, "err", signedErr)
processingErr[i] = fmt.Errorf("%s: %w", signedErrMsg, signedErr)
return
}
sendErr := reqs[i].Error
codes[i] = client.ClassifySendError(sendErr, lggr, tx, attempts[i].Tx.FromAddress, c.client.IsL2())
txErrs[i] = sendErr
}(index)
}
wg.Wait()
err = errors.Join(err, errors.Join(processingErr...)) // merge errors together
return
}
func (c *evmTxmClient) SendTransactionReturnCode(ctx context.Context, etx Tx, attempt TxAttempt, lggr logger.SugaredLogger) (commonclient.SendTxReturnCode, error) {
signedTx, err := GetGethSignedTx(attempt.SignedRawTx)
if err != nil {
lggr.Criticalw("Fatal error signing transaction", "err", err, "etx", etx)
return commonclient.Fatal, err
}
return c.client.SendTransactionReturnCode(ctx, signedTx, etx.FromAddress)
}
func (c *evmTxmClient) PendingNonceAt(ctx context.Context, fromAddress common.Address) (n evmtypes.Nonce, err error) {
nextNonce, err := c.client.PendingNonceAt(ctx, fromAddress)
if err != nil {
return n, err
}
if nextNonce > math.MaxInt64 {
return n, fmt.Errorf("nonce overflow, got: %v", nextNonce)
}
return evmtypes.Nonce(nextNonce), nil
}
func (c *evmTxmClient) SequenceAt(ctx context.Context, addr common.Address, blockNum *big.Int) (evmtypes.Nonce, error) {
return c.client.SequenceAt(ctx, addr, blockNum)
}
func (c *evmTxmClient) BatchGetReceipts(ctx context.Context, attempts []TxAttempt) (txReceipt []*evmtypes.Receipt, txErr []error, funcErr error) {
var reqs []rpc.BatchElem
for _, attempt := range attempts {
res := &evmtypes.Receipt{}
req := rpc.BatchElem{
Method: "eth_getTransactionReceipt",
Args: []interface{}{attempt.Hash},
Result: res,
}
txReceipt = append(txReceipt, res)
reqs = append(reqs, req)
}
if err := c.client.BatchCallContext(ctx, reqs); err != nil {
return nil, nil, fmt.Errorf("EthConfirmer#batchFetchReceipts error fetching receipts with BatchCallContext: %w", err)
}
for _, req := range reqs {
txErr = append(txErr, req.Error)
}
return txReceipt, txErr, nil
}
// sendEmptyTransaction sends a transaction with 0 Eth and an empty payload to the burn address
// May be useful for clearing stuck nonces
func (c *evmTxmClient) SendEmptyTransaction(
ctx context.Context,
newTxAttempt func(seq evmtypes.Nonce, feeLimit uint32, fee gas.EvmFee, fromAddress common.Address) (attempt TxAttempt, err error),
seq evmtypes.Nonce,
gasLimit uint32,
fee gas.EvmFee,
fromAddress common.Address,
) (txhash string, err error) {
defer utils.WrapIfError(&err, "sendEmptyTransaction failed")
attempt, err := newTxAttempt(seq, gasLimit, fee, fromAddress)
if err != nil {
return txhash, err
}
signedTx, err := GetGethSignedTx(attempt.SignedRawTx)
if err != nil {
return txhash, err
}
_, err = c.client.SendTransactionReturnCode(ctx, signedTx, fromAddress)
return signedTx.Hash().String(), err
}
func (c *evmTxmClient) CallContract(ctx context.Context, a TxAttempt, blockNumber *big.Int) (rpcErr fmt.Stringer, extractErr error) {
_, errCall := c.client.CallContract(ctx, ethereum.CallMsg{
From: a.Tx.FromAddress,
To: &a.Tx.ToAddress,
Gas: uint64(a.Tx.FeeLimit),
GasPrice: a.TxFee.Legacy.ToInt(),
GasFeeCap: a.TxFee.DynamicFeeCap.ToInt(),
GasTipCap: a.TxFee.DynamicTipCap.ToInt(),
Value: nil,
Data: a.Tx.EncodedPayload,
AccessList: nil,
}, blockNumber)
return client.ExtractRPCError(errCall)
}