Skip to content

Commit 93af1e8

Browse files
committed
add decode multicall test
1 parent 7591bbe commit 93af1e8

File tree

1 file changed

+370
-0
lines changed

1 file changed

+370
-0
lines changed
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
//
2+
// ABIDecoderTests.swift
3+
//
4+
//
5+
// Created by liugang zhang on 2023/3/16.
6+
//
7+
8+
import Foundation
9+
import Web3Core
10+
import XCTest
11+
import BigInt
12+
@testable import web3swift
13+
14+
final class ABIDecoderTests: XCTestCase {
15+
16+
func testDecodeMulticall() throws {
17+
// get result from http
18+
//
19+
// let requests = tokenAddress.map { address -> AnyObject in
20+
// let callData = erc20_balanceof.encodeParameters([account as NSString])
21+
// return [address, callData as Any] as AnyObject
22+
// } as AnyObject
23+
//
24+
// let read = contract.createReadOperation(
25+
// "tryAggregate",
26+
// parameters: [false, requests] as [AnyObject]
27+
// )
28+
// let results = try await read?.callContractMethod()
29+
30+
let data = Data(hex: "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000004fe6dab4abca350650")
31+
let contract = try EthereumContract(Self.multiCall2, at: nil)
32+
let erc20_balanceof = try EthereumContract(Web3.Utils.erc20ABI).methods["balanceOf"]!.first!
33+
guard let decodedData = contract.decodeReturnData("tryAggregate", data: data) else {
34+
throw Web3Error.processingError(desc: "Can not decode returned parameters")
35+
}
36+
37+
guard let returnData = decodedData["returnData"] as? Array<Array<Any>> else {
38+
throw Web3Error.dataError
39+
}
40+
var resultArray = [BigUInt]()
41+
for i in 0..<2 {
42+
guard let data = returnData[i][1] as? Data,
43+
let balance = erc20_balanceof.decodeReturnData(data)["0"] as? BigUInt else {
44+
resultArray.append(0)
45+
continue
46+
}
47+
resultArray.append(balance)
48+
}
49+
print(resultArray)
50+
XCTAssert(resultArray.count == 2)
51+
}
52+
53+
public static let multiCall2 = """
54+
[
55+
{
56+
"inputs": [
57+
{
58+
"components": [
59+
{
60+
"internalType": "address",
61+
"name": "target",
62+
"type": "address"
63+
},
64+
{
65+
"internalType": "bytes",
66+
"name": "callData",
67+
"type": "bytes"
68+
}
69+
],
70+
"internalType": "struct Multicall2.Call[]",
71+
"name": "calls",
72+
"type": "tuple[]"
73+
}
74+
],
75+
"name": "aggregate",
76+
"outputs": [
77+
{
78+
"internalType": "uint256",
79+
"name": "blockNumber",
80+
"type": "uint256"
81+
},
82+
{
83+
"internalType": "bytes[]",
84+
"name": "returnData",
85+
"type": "bytes[]"
86+
}
87+
],
88+
"payable": false,
89+
"stateMutability": "view",
90+
"type": "function"
91+
},
92+
{
93+
"inputs": [
94+
{
95+
"components": [
96+
{
97+
"internalType": "address",
98+
"name": "target",
99+
"type": "address"
100+
},
101+
{
102+
"internalType": "bytes",
103+
"name": "callData",
104+
"type": "bytes"
105+
}
106+
],
107+
"internalType": "struct Multicall2.Call[]",
108+
"name": "calls",
109+
"type": "tuple[]"
110+
}
111+
],
112+
"name": "blockAndAggregate",
113+
"outputs": [
114+
{
115+
"internalType": "uint256",
116+
"name": "blockNumber",
117+
"type": "uint256"
118+
},
119+
{
120+
"internalType": "bytes32",
121+
"name": "blockHash",
122+
"type": "bytes32"
123+
},
124+
{
125+
"components": [
126+
{
127+
"internalType": "bool",
128+
"name": "success",
129+
"type": "bool"
130+
},
131+
{
132+
"internalType": "bytes",
133+
"name": "returnData",
134+
"type": "bytes"
135+
}
136+
],
137+
"internalType": "struct Multicall2.Result[]",
138+
"name": "returnData",
139+
"type": "tuple[]"
140+
}
141+
],
142+
"stateMutability": "nonpayable",
143+
"type": "function"
144+
},
145+
{
146+
"inputs": [
147+
{
148+
"internalType": "uint256",
149+
"name": "blockNumber",
150+
"type": "uint256"
151+
}
152+
],
153+
"name": "getBlockHash",
154+
"outputs": [
155+
{
156+
"internalType": "bytes32",
157+
"name": "blockHash",
158+
"type": "bytes32"
159+
}
160+
],
161+
"stateMutability": "view",
162+
"type": "function"
163+
},
164+
{
165+
"inputs": [],
166+
"name": "getBlockNumber",
167+
"outputs": [
168+
{
169+
"internalType": "uint256",
170+
"name": "blockNumber",
171+
"type": "uint256"
172+
}
173+
],
174+
"stateMutability": "view",
175+
"type": "function"
176+
},
177+
{
178+
"inputs": [],
179+
"name": "getCurrentBlockCoinbase",
180+
"outputs": [
181+
{
182+
"internalType": "address",
183+
"name": "coinbase",
184+
"type": "address"
185+
}
186+
],
187+
"stateMutability": "view",
188+
"type": "function"
189+
},
190+
{
191+
"inputs": [],
192+
"name": "getCurrentBlockDifficulty",
193+
"outputs": [
194+
{
195+
"internalType": "uint256",
196+
"name": "difficulty",
197+
"type": "uint256"
198+
}
199+
],
200+
"stateMutability": "view",
201+
"type": "function"
202+
},
203+
{
204+
"inputs": [],
205+
"name": "getCurrentBlockGasLimit",
206+
"outputs": [
207+
{
208+
"internalType": "uint256",
209+
"name": "gaslimit",
210+
"type": "uint256"
211+
}
212+
],
213+
"stateMutability": "view",
214+
"type": "function"
215+
},
216+
{
217+
"inputs": [],
218+
"name": "getCurrentBlockTimestamp",
219+
"outputs": [
220+
{
221+
"internalType": "uint256",
222+
"name": "timestamp",
223+
"type": "uint256"
224+
}
225+
],
226+
"stateMutability": "view",
227+
"type": "function"
228+
},
229+
{
230+
"inputs": [
231+
{
232+
"internalType": "address",
233+
"name": "addr",
234+
"type": "address"
235+
}
236+
],
237+
"name": "getEthBalance",
238+
"outputs": [
239+
{
240+
"internalType": "uint256",
241+
"name": "balance",
242+
"type": "uint256"
243+
}
244+
],
245+
"stateMutability": "view",
246+
"type": "function"
247+
},
248+
{
249+
"inputs": [],
250+
"name": "getLastBlockHash",
251+
"outputs": [
252+
{
253+
"internalType": "bytes32",
254+
"name": "blockHash",
255+
"type": "bytes32"
256+
}
257+
],
258+
"stateMutability": "view",
259+
"type": "function"
260+
},
261+
{
262+
"inputs": [
263+
{
264+
"internalType": "bool",
265+
"name": "requireSuccess",
266+
"type": "bool"
267+
},
268+
{
269+
"components": [
270+
{
271+
"internalType": "address",
272+
"name": "target",
273+
"type": "address"
274+
},
275+
{
276+
"internalType": "bytes",
277+
"name": "callData",
278+
"type": "bytes"
279+
}
280+
],
281+
"internalType": "struct Multicall2.Call[]",
282+
"name": "calls",
283+
"type": "tuple[]"
284+
}
285+
],
286+
"name": "tryAggregate",
287+
"outputs": [
288+
{
289+
"components": [
290+
{
291+
"internalType": "bool",
292+
"name": "success",
293+
"type": "bool"
294+
},
295+
{
296+
"internalType": "bytes",
297+
"name": "returnData",
298+
"type": "bytes"
299+
}
300+
],
301+
"internalType": "struct Multicall2.Result[]",
302+
"name": "returnData",
303+
"type": "tuple[]"
304+
}
305+
],
306+
"payable": false,
307+
"stateMutability": "view",
308+
"type": "function"
309+
},
310+
{
311+
"inputs": [
312+
{
313+
"internalType": "bool",
314+
"name": "requireSuccess",
315+
"type": "bool"
316+
},
317+
{
318+
"components": [
319+
{
320+
"internalType": "address",
321+
"name": "target",
322+
"type": "address"
323+
},
324+
{
325+
"internalType": "bytes",
326+
"name": "callData",
327+
"type": "bytes"
328+
}
329+
],
330+
"internalType": "struct Multicall2.Call[]",
331+
"name": "calls",
332+
"type": "tuple[]"
333+
}
334+
],
335+
"name": "tryBlockAndAggregate",
336+
"outputs": [
337+
{
338+
"internalType": "uint256",
339+
"name": "blockNumber",
340+
"type": "uint256"
341+
},
342+
{
343+
"internalType": "bytes32",
344+
"name": "blockHash",
345+
"type": "bytes32"
346+
},
347+
{
348+
"components": [
349+
{
350+
"internalType": "bool",
351+
"name": "success",
352+
"type": "bool"
353+
},
354+
{
355+
"internalType": "bytes",
356+
"name": "returnData",
357+
"type": "bytes"
358+
}
359+
],
360+
"internalType": "struct Multicall2.Result[]",
361+
"name": "returnData",
362+
"type": "tuple[]"
363+
}
364+
],
365+
"stateMutability": "nonpayable",
366+
"type": "function"
367+
}
368+
]
369+
"""
370+
}

0 commit comments

Comments
 (0)