-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
394 lines (338 loc) · 10.3 KB
/
helper.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package web3protocol
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
goEthereumRpc "github.com/ethereum/go-ethereum/rpc"
// log "github.com/sirupsen/logrus"
)
// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
// isHexCharacter returns bool of c being a valid hexadecimal.
func isHexCharacter(c byte) bool {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
}
// isHex validates whether each byte is valid hexadecimal string.
func isHex(str string) bool {
if len(str)%2 != 0 {
return false
}
for _, c := range []byte(str) {
if !isHexCharacter(c) {
return false
}
}
return true
}
// Used for auto mode returning JSON :
// For a given ABI type and a value, convert it to a string, with the data formatted
// according to the spec
func JsonEncodeAbiTypeValue(arg abi.Type, value interface{}) (result interface{}, err error) {
switch arg.T {
case abi.StringTy:
result = value
case abi.BoolTy:
result = value
case abi.IntTy, abi.UintTy:
result = fmt.Sprintf("0x%x", value)
case abi.FixedPointTy, abi.AddressTy:
result = fmt.Sprintf("%v", value)
case abi.BytesTy, abi.FixedBytesTy, abi.HashTy:
result = fmt.Sprintf("0x%x", value)
case abi.SliceTy, abi.ArrayTy:
ty := *arg.Elem
result = make([]interface{}, 0)
rv := reflect.ValueOf(value)
for i := 0; i < rv.Len(); i++ {
subResult, err := JsonEncodeAbiTypeValue(ty, rv.Index(i).Interface())
if err != nil {
return result, err
}
result = append(result.([]interface{}), subResult)
}
case abi.TupleTy:
result = make([]interface{}, 0)
rv := reflect.ValueOf(value)
for i := 0; i < rv.NumField(); i++ {
subResult, err := JsonEncodeAbiTypeValue(*arg.TupleElems[i], rv.Field(i).Interface())
if err != nil {
return result, err
}
result = append(result.([]interface{}), subResult)
}
default:
err = errors.New(fmt.Sprintf("Unsupported type: 0x%x", arg.T))
}
return
}
// For a method signature and the actual arguments, generate the calldata
func methodCallToCalldata(methodName string, methodArgTypes []abi.Type, methodArgValues []interface{}) (calldata []byte, err error) {
// ABI-encode the arguments
abiArguments := abi.Arguments{}
for _, methodArgType := range methodArgTypes {
abiArguments = append(abiArguments, abi.Argument{Type: methodArgType})
}
calldataArgumentsPart, err := abiArguments.Pack(methodArgValues...)
if err != nil {
return
}
// Determine method signature
methodSignature := methodName + "("
for i, methodArgType := range methodArgTypes {
methodSignature += methodArgType.String()
if i < len(methodArgTypes)-1 {
methodSignature += ","
}
}
methodSignature += ")"
methodSignatureHash := crypto.Keccak256Hash([]byte(methodSignature))
// Compute the calldata
calldata = append(methodSignatureHash[0:4], calldataArgumentsPart...)
return
}
// Find an available RPC for the chain. If no available RPC, find one in tooManyRequests state. If
// none are available, return an error.
func (client *Client) findAvailableRpc(chain int, allowTooManyRequestsRPCs bool) (rpc *Rpc, err error) {
client.RpcsMutex.RLock()
defer client.RpcsMutex.RUnlock()
rpcs, ok := client.Rpcs[chain]
if !ok {
err = errors.New(fmt.Sprintf("No RPCs found for chain %d", chain))
return
}
for _, rpc = range rpcs {
if rpc.State == RpcStateAvailable {
return
}
}
if allowTooManyRequestsRPCs {
for _, rpc = range rpcs {
if rpc.State == RpcStateTooManyRequests {
return
}
}
}
err = errors.New(fmt.Sprintf("No available RPCs found for chain %d", chain))
return
}
// Get the RPC to be used by system workers (e.g. ERC-7774), by chain
func (client *Client) GetSystemRpcUrl(chain int) (rpcUrl string, err error) {
chainConfig, ok := client.Config.Chains[chain]
if !ok {
err = errors.New(fmt.Sprintf("No chain found for chain %d", chain))
return
}
rpcUrl = chainConfig.SystemRPC
if rpcUrl == "" {
rpc, err := client.findAvailableRpc(chain, true)
if err != nil {
return rpcUrl, err
}
rpcUrl = rpc.Config.Url
}
return
}
// Call a contract with calldata
func (client *Client) callContract(contract common.Address, chain int, calldata []byte) (contractReturn []byte, err error) {
// Find an available RPC for the chain. If no available RPC, find one in tooManyRequests state. If
// none are available, return an error.
rpc, err := client.findAvailableRpc(chain, true)
if err != nil {
return contractReturn, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: err}
}
// Create connection
ethClient, err := ethclient.Dial(rpc.Config.Url)
if err != nil {
return contractReturn, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: err}
}
defer ethClient.Close()
// Prepare the ethereum message to send
callMessage := ethereum.CallMsg{
From: common.HexToAddress("0x0000000000000000000000000000000000000000"),
To: &contract,
Gas: 0,
GasPrice: nil,
GasFeeCap: nil,
GasTipCap: nil,
Data: calldata,
Value: nil,
}
//
// Loop: For a given time, we try to call the contract.
// As long as the RPC is 429, we wait for it to be available
// until the maxRpcWaitedDuration is reached
//
// Wait for one request slot to be available
rpc.RequestSemaphone <- struct{}{}
defer func() {<-rpc.RequestSemaphone}()
// How long did we wait for the RPC to be available
rpcWaitedDuration := 0 * time.Second
// How often do we check if the RPC is available
rpcWaitInterval := 1 * time.Second
// The maximum time we wait for the RPC to be available
maxRpcWaitedDuration := 30 * time.Second
for notExecuted := true; notExecuted; notExecuted = (err != nil) {
//
// If the RPC is not available, wait for it to be available
//
// If the RPC is right now not available, wait for it to be available
// Basic polling loop, no channels yet
client.RpcsMutex.RLock()
rpcState := rpc.State
client.RpcsMutex.RUnlock()
for rpcState != RpcStateAvailable {
// 429 State
if rpcState == RpcStateTooManyRequests {
// Wait for the RPC to be available
rpcWaitedDuration += rpcWaitInterval
if rpcWaitedDuration > maxRpcWaitedDuration {
return contractReturn, &Web3ProtocolError{
Type: Web3ProtocolErrorTypeRPCError,
HttpCode: http.StatusBadRequest,
RpcHttpCode: http.StatusTooManyRequests,
Err: errors.New("RPC is returning 429, waited too long for it to be available"),
}
}
time.Sleep(rpcWaitInterval)
// Check if the RPC is available
client.RpcsMutex.RLock()
rpcState = rpc.State
client.RpcsMutex.RUnlock()
// 401 State (Would be weird to switch from 429 to 401, but anyway let's check)
} else if rpcState == RpcStateUnauthorized {
return contractReturn, &Web3ProtocolError{
Type: Web3ProtocolErrorTypeRPCError,
HttpCode: http.StatusBadRequest,
RpcHttpCode: http.StatusUnauthorized,
Err: errors.New("RPC is returning 401 Unauthorized"),
}
}
}
//
// Do the contract call
//
// Do the call
contractReturn, err = ethClient.CallContract(context.Background(), callMessage, nil)
//
// Handle errors of the call execution
//
if err != nil {
// fmt.Printf("callContract Error %+v\n", err)
// fmt.Printf("callContract Error type: %T\n", err)
// If the error is of type rpc.jsonError (RPC call succeeded, but JSON returned is an error)
if jsonError, ok := err.(JsonError); ok {
return contractReturn, &Web3ProtocolError{
Type: Web3ProtocolErrorTypeRPCJsonError,
HttpCode: http.StatusBadRequest,
JsonErrorCode: jsonError.ErrorCode(),
JsonErrorData: jsonError.ErrorData(),
Err: err,
}
}
// If error is not of type rpc.HTTPError, we return with an error
if _, ok := err.(goEthereumRpc.HTTPError); !ok {
return contractReturn, &Web3ProtocolError{
HttpCode: http.StatusBadRequest,
Err: err,
}
}
// Get the RPC error
rpcErr := err.(goEthereumRpc.HTTPError)
// If the error is a 401 Unauthorized, switch the RPC to unauthorized
if rpcErr.StatusCode == http.StatusUnauthorized {
client.RpcsMutex.Lock()
rpc.State = RpcStateUnauthorized
client.RpcsMutex.Unlock()
return contractReturn, &Web3ProtocolError{
Type: Web3ProtocolErrorTypeRPCError,
HttpCode: http.StatusBadRequest,
RpcHttpCode: rpcErr.StatusCode,
Err: err,
}
}
// IF the RPC is not 429, return with an error
if rpcErr.StatusCode != http.StatusTooManyRequests {
return contractReturn, &Web3ProtocolError{
Type: Web3ProtocolErrorTypeRPCError,
HttpCode: http.StatusBadRequest,
RpcHttpCode: rpcErr.StatusCode,
Err: err,
}
}
// If the RPC is 429, switch the RPC to tooManyRequests, and we restart the loop,
// waiting for the RPC to be available
client.RpcsMutex.Lock()
if rpc.State != RpcStateTooManyRequests {
rpc.State = RpcStateTooManyRequests
// Start a goroutine to check if the RPC is available again
go client.CheckTooManyRequestsStateWorker(rpc)
}
client.RpcsMutex.Unlock()
}
}
return
}
// URL.parseQuery does not preserve the order of query attributes
// This is a version which keep order
type QueryParameter struct {
Name string
Value string
}
type QueryParameters []QueryParameter
func ParseQuery(query string) (params QueryParameters, err error) {
params = []QueryParameter{}
for query != "" {
var key string
key, query, _ = strings.Cut(query, "&")
if strings.Contains(key, ";") {
err = fmt.Errorf("invalid semicolon separator in query")
continue
}
if key == "" {
continue
}
key, value, _ := strings.Cut(key, "=")
key, err1 := url.QueryUnescape(key)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
value, err1 = url.QueryUnescape(value)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
params = append(params, QueryParameter{
Name: key,
Value: value,
})
}
return
}
func (params *QueryParameters) getLastByNames(names []string) (value QueryParameter) {
for i := len(*params) - 1; i >= 0; i-- {
for _, name := range names {
if (*params)[i].Name == name {
value = (*params)[i]
return
}
}
}
return
}