Skip to content

Commit b721a9c

Browse files
ChaituVRbonustrack
andauthored
Multicall pagination (#421)
* Multicall pagination * Update utils.ts Co-authored-by: Fabien <[email protected]>
1 parent f2423f1 commit b721a9c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/utils.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,26 @@ export async function multicall(
4747
);
4848
const itf = new Interface(abi);
4949
try {
50-
const [, res] = await multi.aggregate(
51-
calls.map((call) => [
52-
call[0].toLowerCase(),
53-
itf.encodeFunctionData(call[1], call[2])
54-
]),
55-
options || {}
50+
const max = options.limit || 1000;
51+
const pages = Math.ceil(calls.length / max);
52+
const promises: any = [];
53+
Array.from(Array(pages)).forEach((x, i) => {
54+
const callsInPage = calls.slice(max * i, max * (i + 1));
55+
promises.push(
56+
multi.aggregate(
57+
callsInPage.map((call) => [
58+
call[0].toLowerCase(),
59+
itf.encodeFunctionData(call[1], call[2])
60+
]),
61+
options || {}
62+
)
63+
);
64+
});
65+
let results: any = await Promise.all(promises);
66+
results = results.reduce((prev: any, [, res]: any) => prev.concat(res), []);
67+
return results.map((call, i) =>
68+
itf.decodeFunctionResult(calls[i][1], call)
5669
);
57-
return res.map((call, i) => itf.decodeFunctionResult(calls[i][1], call));
5870
} catch (e) {
5971
return Promise.reject(e);
6072
}

0 commit comments

Comments
 (0)