/
client_provider.go
278 lines (250 loc) · 9.11 KB
/
client_provider.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package eth
import (
"context"
"errors"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)
// ErrClientNotFound is an error that is returned when a client is not found.
var (
ErrClientNotFound = errors.New("client not found")
)
// ChainProviderImpl is an implementation of the ChainProvider interface.
type ChainProviderImpl struct {
ConnectionPool
rpcTimeout time.Duration
}
// NewChainProviderImpl creates a new ChainProviderImpl with the given ConnectionPool.
func NewChainProviderImpl(pool ConnectionPool, cfg ConnectionPoolConfig) (Client, error) {
c := &ChainProviderImpl{ConnectionPool: pool, rpcTimeout: cfg.DefaultTimeout}
if c.rpcTimeout == 0 {
c.rpcTimeout = defaultRPCTimeout
}
return c, nil
}
// ==================================================================
// Implementations of Reader and Writer
// ==================================================================
// BlockByNumber returns the block for the given number.
func (c *ChainProviderImpl) BlockByNumber(
ctx context.Context, num *big.Int) (*types.Block, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.BlockByNumber(ctxWithTimeout, num)
}
return nil, ErrClientNotFound
}
// BlockReceipts returns the receipts for the given block number or hash.
func (c *ChainProviderImpl) BlockReceipts(
ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.BlockReceipts(ctxWithTimeout, blockNrOrHash)
}
return nil, ErrClientNotFound
}
// TransactionReceipt returns the receipt for the given transaction hash.
func (c *ChainProviderImpl) TransactionReceipt(
ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.TransactionReceipt(ctxWithTimeout, txHash)
}
return nil, ErrClientNotFound
}
// SubscribeNewHead subscribes to new head events.
func (c *ChainProviderImpl) SubscribeNewHead(
ctx context.Context) (chan *types.Header, ethereum.Subscription, error) {
if client, ok := c.GetWS(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.SubscribeNewHead(ctxWithTimeout)
}
return nil, nil, ErrClientNotFound
}
// BlockNumber returns the current block number.
func (c *ChainProviderImpl) BlockNumber(ctx context.Context) (uint64, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.BlockNumber(ctxWithTimeout)
}
return 0, ErrClientNotFound
}
// ChainID returns the current chain ID.
func (c *ChainProviderImpl) ChainID(ctx context.Context) (*big.Int, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.ChainID(ctxWithTimeout)
}
return nil, ErrClientNotFound
}
// BalanceAt returns the balance of the given address at the given block number.
func (c *ChainProviderImpl) BalanceAt(
ctx context.Context, address common.Address, blockNumber *big.Int) (*big.Int, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.BalanceAt(ctxWithTimeout, address, blockNumber)
}
return nil, ErrClientNotFound
}
// CodeAt returns the code of the given account at the given block number.
func (c *ChainProviderImpl) CodeAt(
ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.CodeAt(ctxWithTimeout, account, blockNumber)
}
return nil, ErrClientNotFound
}
// EstimateGas estimates the gas needed to execute a specific transaction.
func (c *ChainProviderImpl) EstimateGas(
ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.EstimateGas(ctxWithTimeout, msg)
}
return 0, ErrClientNotFound
}
// FilterLogs returns the logs that satisfy the given filter query.
func (c *ChainProviderImpl) FilterLogs(
ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.FilterLogs(ctxWithTimeout, q)
}
return nil, ErrClientNotFound
}
// HeaderByNumber returns the header of the block with the given number.
func (c *ChainProviderImpl) HeaderByNumber(
ctx context.Context, number *big.Int) (*types.Header, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.HeaderByNumber(ctxWithTimeout, number)
}
return nil, ErrClientNotFound
}
// PendingCodeAt returns the code of the given account in the pending state.
func (c *ChainProviderImpl) PendingCodeAt(
ctx context.Context, account common.Address) ([]byte, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.PendingCodeAt(ctxWithTimeout, account)
}
return nil, ErrClientNotFound
}
// PendingNonceAt returns the nonce of the given account in the pending state.
func (c *ChainProviderImpl) PendingNonceAt(
ctx context.Context, account common.Address) (uint64, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.PendingNonceAt(ctxWithTimeout, account)
}
return 0, ErrClientNotFound
}
// PendingNonceAt returns the nonce of the given account in the pending state.
func (c *ChainProviderImpl) NonceAt(
ctx context.Context, account common.Address, bn *big.Int) (uint64, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.NonceAt(ctxWithTimeout, account, bn)
}
return 0, ErrClientNotFound
}
// SendTransaction sends the given transaction.
func (c *ChainProviderImpl) SendTransaction(
ctx context.Context, tx *types.Transaction) error {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.SendTransaction(ctxWithTimeout, tx)
}
return ErrClientNotFound
}
// SubscribeFilterLogs subscribes to new log events that satisfy the given filter query.
func (c *ChainProviderImpl) SubscribeFilterLogs(
ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log,
) (ethereum.Subscription, error) {
if client, ok := c.GetWS(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.SubscribeFilterLogs(ctxWithTimeout, q, ch)
}
return nil, ErrClientNotFound
}
// SuggestGasPrice suggests a gas price.
func (c *ChainProviderImpl) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.SuggestGasPrice(ctxWithTimeout)
}
return nil, ErrClientNotFound
}
// CallContract calls a contract with the given message at the given block number.
func (c *ChainProviderImpl) CallContract(
ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int,
) ([]byte, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.CallContract(ctxWithTimeout, msg, blockNumber)
}
return nil, ErrClientNotFound
}
// SuggestGasTipCap suggests a gas tip cap.
func (c *ChainProviderImpl) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.SuggestGasTipCap(ctxWithTimeout)
}
return nil, ErrClientNotFound
}
// TransactionByHash returns the transaction with the given hash.
func (c *ChainProviderImpl) TransactionByHash(
ctx context.Context, hash common.Hash,
) (*types.Transaction, bool, error) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.TransactionByHash(ctxWithTimeout, hash)
}
return nil, false, ErrClientNotFound
}
func (c *ChainProviderImpl) TxPoolContent(ctx context.Context) (
map[string]map[string]map[string]*types.Transaction, error,
) {
if client, ok := c.GetHTTP(); ok {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
defer cancel()
return client.TxPoolContent(ctxWithTimeout)
}
return nil, ErrClientNotFound
}
func (c *ChainProviderImpl) Health() bool {
httpOk, wsOk := false, false
if client, ok := c.GetHTTP(); ok {
httpOk = client.Healthy()
}
if client, ok := c.GetWS(); ok {
wsOk = client.Healthy()
}
return httpOk && wsOk
}