Skip to content

Commit

Permalink
refactor: only fetch targeted events
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekhmet committed May 13, 2024
1 parent 488e615 commit 55722a8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/providers/evm/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type BlockWithTransactions = Awaited<ReturnType<Provider['getBlockWithTransactio
type Transaction = BlockWithTransactions['transactions'][number];
type EventsMap = Record<string, Log[]>;

const MAX_BLOCKS_PER_REQUEST = 10;
const MAX_BLOCKS_PER_REQUEST = 100;

export class EvmProvider extends BaseProvider {
private readonly provider: Provider;
Expand Down Expand Up @@ -241,7 +241,7 @@ export class EvmProvider extends BaseProvider {
}, {});
}

async getLogs(fromBlock: number, toBlock: number) {
async getLogs(fromBlock: number, toBlock: number, address: string) {
const result = [] as Log[];

let currentFrom = fromBlock;
Expand All @@ -250,12 +250,13 @@ export class EvmProvider extends BaseProvider {
try {
const logs = await this.provider.getLogs({
fromBlock: currentFrom,
toBlock: currentTo
toBlock: currentTo,
address
});

result.push(...logs);

if (currentTo === toBlock) return result;
if (currentTo === toBlock) break;
currentFrom = currentTo + 1;
currentTo = Math.min(toBlock, currentFrom + MAX_BLOCKS_PER_REQUEST);
} catch (e: any) {
Expand All @@ -271,14 +272,21 @@ export class EvmProvider extends BaseProvider {
);
}
}
}

async getCheckpointsRange(fromBlock: number, toBlock: number): Promise<CheckpointRecord[]> {
const logs = await this.getLogs(fromBlock, toBlock);

return logs.map(log => ({
return result.map(log => ({
blockNumber: log.blockNumber,
contractAddress: log.address
}));
}

async getCheckpointsRange(fromBlock: number, toBlock: number): Promise<CheckpointRecord[]> {
const events: CheckpointRecord[] = [];

for (const source of this.instance.getCurrentSources(fromBlock)) {
const addressEvents = await this.getLogs(fromBlock, toBlock, source.contract);
events.push(...addressEvents);
}

return events;
}
}

0 comments on commit 55722a8

Please sign in to comment.