From d0c95ca10fad0ec6c742ebc4163d78a741277cc9 Mon Sep 17 00:00:00 2001 From: stepollo2 Date: Tue, 17 Sep 2024 11:52:22 +0200 Subject: [PATCH] batch multicalls --- adapters/vfat/hourly_blocks.csv | 2 +- adapters/vfat/src/sdk/nile/lensDetails.ts | 47 ++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/adapters/vfat/hourly_blocks.csv b/adapters/vfat/hourly_blocks.csv index afdfb69f..bb659a24 100644 --- a/adapters/vfat/hourly_blocks.csv +++ b/adapters/vfat/hourly_blocks.csv @@ -1,2 +1,2 @@ number,timestamp -5703320,1718801232 \ No newline at end of file +9397663,1726210798 \ No newline at end of file diff --git a/adapters/vfat/src/sdk/nile/lensDetails.ts b/adapters/vfat/src/sdk/nile/lensDetails.ts index b5d9934e..f2e4263f 100644 --- a/adapters/vfat/src/sdk/nile/lensDetails.ts +++ b/adapters/vfat/src/sdk/nile/lensDetails.ts @@ -13,6 +13,7 @@ export interface VoteResponse { result: VoteRequest; } +// Function to fetch user votes with batching export const fetchUserVotes = async ( blockNumber: bigint, userAddress: string, @@ -45,11 +46,13 @@ export const fetchUserVotes = async ( }); } - const userTokensCalls = await multicall( + const userTokensCalls = await batchMulticall( publicClient, veNILEAbi as Abi, calls, blockNumber, + 500, + 200 ); const detailsCall = userTokensCalls.map((call) => { @@ -60,11 +63,13 @@ export const fetchUserVotes = async ( }; }); - const res = (await multicall( + const res = (await batchMulticall( publicClient, veNILEAbi as Abi, detailsCall, blockNumber, + 500, + 200 )) as any; return res.map((r: any) => { @@ -72,6 +77,44 @@ export const fetchUserVotes = async ( }) as VoteResponse[]; }; +// Batch multicall function with a delay +async function batchMulticall( + publicClient: PublicClient, + abi: Abi, + calls: any[], + blockNumber: bigint, + batchSize: number, + delay: number, +) { + const results = []; + + for (let i = 0; i < calls.length; i += batchSize) { + const batch = calls.slice(i, i + batchSize); + + const call: MulticallParameters = { + contracts: batch.map((call) => ({ + address: call.address as Address, + abi, + functionName: call.name, + args: call.params, + })), + blockNumber, + }; + + // Send the batch of requests + const res = await publicClient.multicall(call); + results.push(...res); + + // Introduce delay before sending the next batch + if (i + batchSize < calls.length) { + await new Promise((resolve) => setTimeout(resolve, delay)); + } + } + + return results; +} + +// Regular multicall function function multicall( publicClient: PublicClient, abi: Abi,