-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
159 lines (123 loc) · 3.82 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
package ethrpc
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/KyberNetwork/logger"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
)
const (
MethodCall = "call"
MethodAggregate = "aggregate"
MethodTryAggregate = "tryAggregate"
MethodGetCurrentBlockTimestamp = "getCurrentBlockTimestamp"
MethodTryBlockAndAggregate = "tryBlockAndAggregate"
)
var zeroHash common.Hash
type (
// RequestMiddleware type is for request middleware, called before a request is sent
RequestMiddleware func(*Client, *Request) error
// ResponseMiddleware type is for response middleware, called after a response has been received
ResponseMiddleware func(*Client, *Response) error
)
type Client struct {
ethClient *ethclient.Client
gethClient *gethclient.Client
multiCallContract common.Address
beforeRequest []RequestMiddleware
afterResponse []ResponseMiddleware
}
func (c *Client) SetMulticallContract(multiCallContract common.Address) *Client {
c.multiCallContract = multiCallContract
return c
}
func (c *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return c.ethClient.SuggestGasPrice(ctx)
}
func (c *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
return c.ethClient.EstimateGas(ctx, msg)
}
func (c *Client) GetBlockNumber(ctx context.Context) (uint64, error) {
return c.ethClient.BlockNumber(ctx)
}
func (c *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
return c.ethClient.BalanceAt(ctx, account, blockNumber)
}
func (c *Client) R() *Request {
r := &Request{
client: c,
}
return r
}
func (c *Client) NewRequest() *Request {
return c.R()
}
func (c *Client) getStorageAt(ctx context.Context, account common.Address, key common.Hash, abi abi.Arguments) ([]interface{}, error) {
resp, err := c.ethClient.StorageAt(ctx, account, key, nil)
if err != nil {
logger.Errorf("failed to call StorageAt to %v at %v, err: %v", account, key, err)
return nil, err
}
logger.Debugf("raw response %v", common.Bytes2Hex(resp))
res, err := abi.Unpack(resp)
if err != nil {
logger.Errorf("failed to unpack StorageAt to %v at %v, err: %v", account, key, err)
return nil, err
}
return res, nil
}
func (c *Client) execute(req *Request) (*Response, error) {
var err error
// Apply Request middlewares
for _, f := range c.beforeRequest {
if err = f(c, req); err != nil {
return nil, err
}
}
var resp []byte
// we don't support block hash and overrides at the same time
if req.BlockHash != zeroHash && len(req.Overrides) > 0 {
logger.Errorf("block hash and overrides are not supported at the same time")
return nil, ErrWrongCallParam
}
if req.BlockHash != zeroHash {
resp, err = c.ethClient.CallContractAtHash(req.Context(), req.RawCallMsg, req.BlockHash)
} else if req.Overrides != nil {
resp, err = c.gethClient.CallContract(req.Context(), req.RawCallMsg, req.BlockNumber, &req.Overrides)
} else {
resp, err = c.ethClient.CallContract(req.Context(), req.RawCallMsg, req.BlockNumber)
}
if err != nil {
logger.Errorf("failed to call multicall, err: %v", err)
return nil, err
}
response := &Response{
Request: req,
RawResponse: resp,
}
// Apply Response middleware
for _, f := range c.afterResponse {
if err = f(c, response); err != nil {
break
}
}
return response, err
}
func createClient(ec *ethclient.Client) *Client {
c := &Client{
ethClient: ec,
gethClient: gethclient.New(ec.Client()),
}
// default before request middlewares
c.beforeRequest = []RequestMiddleware{
parseRequestCallParam,
}
// default after response middlewares
c.afterResponse = []ResponseMiddleware{
parseResponse,
}
return c
}