forked from distribworks/xk6-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc_tmp.go
54 lines (43 loc) · 1.23 KB
/
jsonrpc_tmp.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
package ethereum
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/ybbus/jsonrpc/v3"
)
// ClientTmp is a jsonrpc client, we use it temporarily until the bug NDEV-3224 is fixed
type ClientTmp struct {
client jsonrpc.RPCClient
ctx context.Context
}
func (c *ClientTmp) GasPrice() (uint64, error) {
g, err := c.client.Call(c.ctx, "eth_gasPrice")
if err != nil {
return 0, fmt.Errorf("failed to get gasPrice: %w", err)
}
return parseResponse("eth_gasPrice", g)
}
func (c *ClientTmp) BlockNumber() (uint64, error) {
g, err := c.client.Call(c.ctx, "eth_blockNumber")
if err != nil {
return 0, fmt.Errorf("failed to get blockNumber: %w", err)
}
return parseResponse("eth_blockNumber", g)
}
func parseResponse(methodName string, response *jsonrpc.RPCResponse) (uint64, error) {
value, ok := response.Result.(string)
if !ok {
return 0, fmt.Errorf("failed to parse hex value to uint64 %s", methodName)
}
s, ok := strings.CutPrefix(value, "0x")
if !ok {
return 0, fmt.Errorf("failed to cut prefix value %s", s)
}
result, err := strconv.ParseUint(s, 16, 64)
if err != nil {
fmt.Printf("failed to convert string value into uint64: %s", methodName)
return 0, fmt.Errorf("error message: %w", err)
}
return result, nil
}