Skip to content

Commit

Permalink
fetchLogs bulk handling improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecaan committed Nov 8, 2023
1 parent 7c8023b commit 3c6f555
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions sdk/src/events/fetchLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,39 @@ function mapTransactionResponseToLog(
};
}

function batchArrays(arrays : any[], batchSize: number) {
const result = [];
let batch = [];

for (const array of arrays) {
batch.push(array);
if (batch.length === batchSize) {
result.push(batch);
batch = [];
}
}

if (batch.length > 0) {
result.push(batch);
}

return result;
}

export async function sleep(ms:number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function fetchLogs(
connection: Connection,
address: PublicKey,
finality: Finality,
beforeTx?: TransactionSignature,
untilTx?: TransactionSignature,
limit?: number,
batchSize = 25
signatureChunkSize = 25,
parallelFetchBatchSize = 10,
fetchDelayMs = 1000,
): Promise<FetchLogsResponse> {
const signatures = await connection.getSignaturesForAddress(
address,
Expand All @@ -61,19 +86,25 @@ export async function fetchLogs(
return undefined;
}

const chunkedSignatures = chunk(filteredSignatures, batchSize);

const transactionLogs = (
await Promise.all(
chunkedSignatures.map(async (chunk) => {
return await fetchTransactionLogs(
connection,
chunk.map((confirmedSignature) => confirmedSignature.signature),
finality
);
})
)
).flat();
const chunkedSignatures = chunk(filteredSignatures, signatureChunkSize);

const fetchBatches = batchArrays(chunkedSignatures, parallelFetchBatchSize);

let transactionLogs : Log[] = [];

for (const batch of fetchBatches) {
const logs = await Promise.all(batch.map(async (chunk) => {
return await fetchTransactionLogs(
connection,
chunk.map((confirmedSignature) => confirmedSignature.signature),
finality
);
}));

await sleep(fetchDelayMs);

transactionLogs = transactionLogs.concat(logs);
}

const earliest = filteredSignatures[0];
const mostRecent = filteredSignatures[filteredSignatures.length - 1];
Expand Down

0 comments on commit 3c6f555

Please sign in to comment.