Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch multicalls -vfat #298

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading