-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
235 lines (189 loc) · 6.14 KB
/
middleware.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
package ethrpc
import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/KyberNetwork/logger"
)
func parseRequestCallParam(c *Client, req *Request) error {
switch req.Method {
case MethodCall:
if len(req.Calls) != 1 {
return ErrWrongCallParam
}
call := req.Calls[0]
callData, err := call.ABI.Pack(call.Method, call.Params...)
if err != nil {
logger.Errorf("failed to pack api, err: %v", err)
return err
}
target := common.HexToAddress(call.Target)
msg := ethereum.CallMsg{To: &target, Data: callData}
req.RawCallMsg = msg
return nil
case MethodAggregate:
var multiCallParams []MultiCallParam
for _, c := range req.Calls {
callData, err := c.ABI.Pack(c.Method, c.Params...)
if err != nil {
logger.Errorf("failed to build call data for target=%s method=%s, err: %v", c.Target, c.Method, err)
return err
}
multiCallParams = append(
multiCallParams, MultiCallParam{
Target: common.HexToAddress(c.Target),
CallData: callData,
},
)
}
callData, err := multicallABI.Pack(MethodAggregate, multiCallParams)
if err != nil {
logger.Errorf("failed to build multi call data, err: %v", err)
return err
}
msg := ethereum.CallMsg{To: &c.multiCallContract, Data: callData}
req.RawCallMsg = msg
return nil
case MethodTryAggregate:
var multiCallParams []MultiCallParam
for _, call := range req.Calls {
callData, err := call.ABI.Pack(call.Method, call.Params...)
if err != nil {
logger.Errorf("failed to build call data for target=%s method=%s, err: %v", call.Target, call.Method, err)
return err
}
multiCallParams = append(
multiCallParams, MultiCallParam{
Target: common.HexToAddress(call.Target),
CallData: callData,
},
)
}
callData, err := multicallABI.Pack(MethodTryAggregate, req.RequireSuccess, multiCallParams)
if err != nil {
logger.Errorf("failed to build multi call data, err: %v", err)
return err
}
msg := ethereum.CallMsg{To: &c.multiCallContract, Data: callData}
req.RawCallMsg = msg
return nil
case MethodGetCurrentBlockTimestamp:
callData, err := multicallABI.Pack(MethodGetCurrentBlockTimestamp)
if err != nil {
logger.Errorf("failed to build call data, err: %v", err)
return err
}
msg := ethereum.CallMsg{To: &c.multiCallContract, Data: callData}
req.RawCallMsg = msg
return nil
case MethodTryBlockAndAggregate:
var multiCallParams []MultiCallParam
for _, call := range req.Calls {
callData, err := call.ABI.Pack(call.Method, call.Params...)
if err != nil {
logger.Errorf("failed to build call data for target=%s method=%s, err: %v", call.Target, call.Method, err)
return err
}
multiCallParams = append(
multiCallParams, MultiCallParam{
Target: common.HexToAddress(call.Target),
CallData: callData,
},
)
}
callData, err := multicallABI.Pack(MethodTryBlockAndAggregate, req.RequireSuccess, multiCallParams)
if err != nil {
logger.Errorf("failed to build multi call data, err: %v", err)
return err
}
msg := ethereum.CallMsg{To: &c.multiCallContract, Data: callData}
req.RawCallMsg = msg
return nil
default:
return ErrMethodNotSupported
}
}
func parseResponse(_ *Client, res *Response) (err error) {
switch res.Request.Method {
case MethodCall:
if len(res.Request.Calls) != 1 {
return ErrWrongCallParam
}
call := res.Request.Calls[0]
if err = call.ABI.UnpackIntoInterface(call.Output[0], call.Method, res.RawResponse); err != nil {
logger.Errorf("failed to unpack call %s, err: %v", call.Method, err)
return err
}
return nil
case MethodAggregate:
var result AggregateResult
err = multicallABI.UnpackIntoInterface(&result, res.Request.Method, res.RawResponse)
if err != nil || len(result.ReturnData) != len(res.Request.Calls) {
logger.Errorf("failed to unpack aggregate response, err: %v", err)
return err
}
for i, c := range res.Request.Calls {
// result will always be true if it can reach this far
res.Result = append(res.Result, true)
if err = c.ABI.UnpackIntoInterface(c.Output[0], c.Method, result.ReturnData[i]); err != nil {
logger.Errorf("failed to unpack target=%s method=%s, err: %v", c.Target, c.Method, err)
return NewUnPackMulticallError(err)
}
}
res.BlockNumber = result.BlockNumber
return nil
case MethodTryAggregate:
var result TryAggregateResult
err = multicallABI.UnpackIntoInterface(&result, res.Request.Method, res.RawResponse)
if err != nil || len(result) != len(res.Request.Calls) {
logger.Errorf("failed to unpack tryAggregate response, err: %v", err)
return err
}
for i, c := range res.Request.Calls {
res.Result = append(res.Result, result[i].Success)
if result[i].Success {
for j, unpackABI := range c.UnpackABI {
if err = unpackABI.UnpackIntoInterface(c.Output[j], c.Method, result[i].ReturnData); err == nil {
break
}
if j == len(c.UnpackABI)-1 {
logger.Errorf("failed to unpack target=%s method=%s, err: %v", c.Target, c.Method, err)
if res.Request.RequireSuccess {
return NewUnPackMulticallError(err)
}
}
}
}
}
return nil
case MethodGetCurrentBlockTimestamp:
// do nothing
return nil
case MethodTryBlockAndAggregate:
var result TryBlockAndAggregateResult
err = multicallABI.UnpackIntoInterface(&result, res.Request.Method, res.RawResponse)
if err != nil || len(result.ReturnData) != len(res.Request.Calls) {
logger.Errorf("failed to unpack tryAggregate response, err: %v", err)
return err
}
for i, c := range res.Request.Calls {
res.Result = append(res.Result, result.ReturnData[i].Success)
if result.ReturnData[i].Success {
for j, unpackABI := range c.UnpackABI {
if err = unpackABI.UnpackIntoInterface(c.Output[j], c.Method, result.ReturnData[i].ReturnData); err == nil {
break
}
if j == len(c.UnpackABI)-1 {
logger.Errorf("failed to unpack target=%s method=%s, err: %v", c.Target, c.Method, err)
if res.Request.RequireSuccess {
return NewUnPackMulticallError(err)
}
}
}
}
}
res.BlockNumber = result.BlockNumber
return nil
default:
return ErrMethodNotSupported
}
}