Skip to content

Commit

Permalink
batch multicalls
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChupaCabra committed Sep 17, 2024
1 parent 6db44a4 commit d0c95ca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion adapters/vfat/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
number,timestamp
5703320,1718801232
9397663,1726210798
47 changes: 45 additions & 2 deletions adapters/vfat/src/sdk/nile/lensDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface VoteResponse {
result: VoteRequest;
}

// Function to fetch user votes with batching
export const fetchUserVotes = async (
blockNumber: bigint,
userAddress: string,
Expand Down Expand Up @@ -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) => {
Expand All @@ -60,18 +63,58 @@ 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) => {
return { result: { amount: r.result[0], userAddress } };
}) 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,
Expand Down

0 comments on commit d0c95ca

Please sign in to comment.