forked from jon4hz/geth-multicall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewcall.go
329 lines (295 loc) · 7.82 KB
/
viewcall.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
package multicall
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"reflect"
"regexp"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/crypto"
)
type ViewCall struct {
id string
target string
method string
arguments []interface{}
}
type ViewCalls []ViewCall
func NewViewCall(id, target, method string, arguments []interface{}) ViewCall {
return ViewCall{
id: id,
target: target,
method: method,
arguments: arguments,
}
}
func (call ViewCall) Validate() error {
if _, err := call.argsCallData(); err != nil {
return err
}
return nil
}
var insideParens = regexp.MustCompile(`\(.*?\)`)
var numericArg = regexp.MustCompile("u?int(256)|(8)")
func (call ViewCall) argumentTypes() []string {
rawArgs := insideParens.FindAllString(call.method, -1)[0]
rawArgs = strings.Replace(rawArgs, "(", "", -1)
rawArgs = strings.Replace(rawArgs, ")", "", -1)
if rawArgs == "" {
return []string{}
}
args := strings.Split(rawArgs, ",")
for index, arg := range args {
args[index] = strings.Trim(arg, " ")
}
return args
}
func (call ViewCall) returnTypes() []string {
rawArgs := insideParens.FindAllString(call.method, -1)[1]
rawArgs = strings.Replace(rawArgs, "(", "", -1)
rawArgs = strings.Replace(rawArgs, ")", "", -1)
args := strings.Split(rawArgs, ",")
for index, arg := range args {
args[index] = strings.Trim(arg, " ")
}
return args
}
func (call ViewCall) callData() ([]byte, error) {
argsSuffix, err := call.argsCallData()
if err != nil {
return nil, err
}
methodPrefix, err := call.methodCallData()
if err != nil {
return nil, err
}
payload := make([]byte, 0)
payload = append(payload, methodPrefix...)
payload = append(payload, argsSuffix...)
return payload, nil
}
func (call ViewCall) methodCallData() ([]byte, error) {
methodParts := strings.Split(call.method, ")(")
var method string
if len(methodParts) > 1 {
method = fmt.Sprintf("%s)", methodParts[0])
} else {
method = methodParts[0]
}
hash := crypto.Keccak256([]byte(method))
return hash[0:4], nil
}
func (call ViewCall) argsCallData() ([]byte, error) {
argTypes := call.argumentTypes()
if len(argTypes) != len(call.arguments) {
return nil, fmt.Errorf("number of argument types doesn't match with number of arguments for %s with method %s", call.id, call.method)
}
argumentValues := make([]interface{}, len(call.arguments))
arguments := make(abi.Arguments, len(call.arguments))
for index, argTypeStr := range argTypes {
argType, err := abi.NewType(argTypeStr, "", nil)
if err != nil {
return nil, err
}
arguments[index] = abi.Argument{Type: argType}
argumentValues[index], err = call.getArgument(index, argTypeStr)
if err != nil {
return nil, err
}
}
return arguments.Pack(argumentValues...)
}
func (call ViewCall) getArgument(index int, argumentType string) (interface{}, error) {
arg := call.arguments[index]
if argumentType == "address" {
address, ok := arg.(string)
if !ok {
return nil, fmt.Errorf("expected address argument to be a string")
}
return toByteArray(address)
} else if numericArg.MatchString(argumentType) {
if num, ok := arg.(json.Number); ok {
if v, err := num.Int64(); err != nil {
return big.NewInt(v), nil
} else if v, err := num.Float64(); err != nil {
return big.NewInt(int64(v)), nil
}
} else {
int64 := reflect.TypeOf(int64(0))
argType := reflect.TypeOf(arg)
kind := argType.Kind()
if kind == reflect.String {
if val, ok := new(big.Int).SetString(call.arguments[index].(string), 10); !ok {
return nil, fmt.Errorf("could not parse %s as a base 10 number", call.arguments[index])
} else {
return val, nil
}
} else if argType.ConvertibleTo(int64) {
return big.NewInt(reflect.ValueOf(arg).Convert(int64).Int()), nil
}
}
}
return arg, nil
}
func (call ViewCall) decode(raw []byte) ([]interface{}, error) {
retTypes := call.returnTypes()
args := make(abi.Arguments, 0)
for index, retTypeStr := range retTypes {
retType, err := abi.NewType(retTypeStr, "", nil)
if err != nil {
return nil, err
}
args = append(args, abi.Argument{Name: fmt.Sprintf("ret%d", index), Type: retType})
}
decoded := make(map[string]interface{})
err := args.UnpackIntoMap(decoded, raw)
if err != nil {
return nil, err
}
returns := make([]interface{}, len(retTypes))
for index := range retTypes {
key := fmt.Sprintf("ret%d", index)
returns[index] = decoded[key]
}
return returns, nil
}
type callArgs struct {
Target [20]byte
CallData []byte
}
func (calls ViewCalls) callData() ([]byte, error) {
payloadArgs := make([]callArgs, 0, len(calls))
for _, call := range calls {
callData, err := call.callData()
if err != nil {
return nil, err
}
targetBytes, err := toByteArray(call.target)
if err != nil {
return nil, err
}
payloadArgs = append(payloadArgs, callArgs{targetBytes, callData})
}
tupleArray, err := abi.NewType("tuple[]", "", []abi.ArgumentMarshaling{
{Type: "address", Name: "Target"},
{Type: "bytes", Name: "CallData"},
})
if err != nil {
return nil, err
}
boolean, err := abi.NewType("bool", "", nil)
if err != nil {
return nil, err
}
args := abi.Arguments{
{Type: tupleArray, Name: "calls"},
{Type: boolean, Name: "strict"},
}
return args.Pack(payloadArgs, false)
}
type retType struct {
Success bool
Data []byte
}
type wrapperRet struct {
BlockNumber *big.Int
Returns []retType
}
func (calls ViewCalls) decodeWrapper(raw string) (*wrapperRet, error) {
rawBytes, err := hex.DecodeString(strings.Replace(raw, "0x", "", -1))
if err != nil {
return nil, err
}
uint256Type, err := abi.NewType("uint256", "", nil)
if err != nil {
return nil, err
}
returnType, err := abi.NewType("tuple[]", "", []abi.ArgumentMarshaling{
{Name: "Success", Type: "bool"},
{Name: "Data", Type: "bytes"},
})
if err != nil {
return nil, err
}
wrapperArgs := abi.Arguments{
{
Name: "BlockNumber",
Type: uint256Type,
},
{
Name: "Returns",
Type: returnType,
},
}
data, err := wrapperArgs.Unpack(rawBytes)
if err != nil {
return nil, err
}
decoded := &wrapperRet{
BlockNumber: data[0].(*big.Int),
}
returns := reflect.ValueOf(data[1])
for i := 0; i < returns.Len(); i++ {
elem := returns.Index(i)
ret := retType{
Success: elem.FieldByName("Success").Bool(),
Data: elem.FieldByName("Data").Bytes(),
}
decoded.Returns = append(decoded.Returns, ret)
}
return decoded, err
}
func (calls ViewCalls) decodeRaw(raw string) (*Result, error) {
decoded, err := calls.decodeWrapper(raw)
if err != nil {
return nil, err
}
result := &Result{}
result.BlockNumber = decoded.BlockNumber.Uint64()
result.Calls = make(map[string]CallResult)
for index, call := range calls {
callResult := CallResult{
Success: decoded.Returns[index].Success,
Raw: decoded.Returns[index].Data,
Decoded: []interface{}{},
}
result.Calls[call.id] = callResult
}
return result, nil
}
func (calls ViewCalls) decode(raw string) (*Result, error) {
decoded, err := calls.decodeWrapper(raw)
if err != nil {
return nil, err
}
result := &Result{}
result.BlockNumber = decoded.BlockNumber.Uint64()
result.Calls = make(map[string]CallResult)
for index, call := range calls {
callResult := CallResult{
Success: decoded.Returns[index].Success,
Raw: decoded.Returns[index].Data,
}
if decoded.Returns[index].Success {
returnValues, err := call.decode(decoded.Returns[index].Data)
if err != nil {
return nil, err
}
callResult.Decoded = returnValues
}
result.Calls[call.id] = callResult
}
return result, nil
}
func toByteArray(address string) ([20]byte, error) {
var addressBytes [20]byte
address = strings.Replace(address, "0x", "", -1)
addressBytesSlice, err := hex.DecodeString(address)
if err != nil {
return addressBytes, err
}
copy(addressBytes[:], addressBytesSlice[:])
return addressBytes, nil
}