Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
[backend] ensure we never forget any event
Browse files Browse the repository at this point in the history
  • Loading branch information
YBadiss committed Mar 24, 2024
1 parent e4eb527 commit a76aa70
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 3 additions & 4 deletions fc-community-backend/apps/indexer/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ export const supportedNetworks = [
},
];

export const getNetworkFromBlocks = async (): Promise<NetworkBlock[]> => {
export const getNetworkToBlocks = async (): Promise<NetworkBlock[]> => {
const networkFromBlocks: NetworkBlock[] = await Promise.all(
supportedNetworks.map(async (network) => {
const provider = new ethers.JsonRpcProvider(network.rpcUrl);
Expand All @@ -1711,6 +1711,7 @@ export const getNetworkFromBlocks = async (): Promise<NetworkBlock[]> => {
export const getEventsForNetwork = async (
networkName: string,
fromBlock: number,
toBlock: number,
): Promise<FactchainEvent[]> => {
const network = supportedNetworks.find(
(network) => network.name === networkName,
Expand All @@ -1720,11 +1721,9 @@ export const getEventsForNetwork = async (
}
const provider = new ethers.JsonRpcProvider(network.rpcUrl);

// keep track of block timestamps to avoid querying the same block multiple times
console.log(`Getting events for network ${networkName} from block ${fromBlock} to block ${toBlock}`);
const maxBlockSpan = 10000;
let currentBlock = fromBlock;
const toBlock = await provider.getBlockNumber();
console.log(`Current block: ${toBlock}`);
const events: any[] = [];
while (currentBlock < toBlock) {
const nextBlock = Math.min(currentBlock + maxBlockSpan, toBlock);
Expand Down
13 changes: 10 additions & 3 deletions fc-community-backend/apps/indexer/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { readNetworkBlocks, writeEvents, writeNetworkBlocks } from "./mongo";
import { getEventsForNetwork, getNetworkFromBlocks } from "./events";
import { getEventsForNetwork, getNetworkToBlocks } from "./events";
import { NetworkBlock } from "./types";

async function run() {
const networkFromBlocks: NetworkBlock[] = await readNetworkBlocks();
// Compute the new network blocks once and use it for all networks
const networkToBlocks: NetworkBlock[] = await getNetworkToBlocks();

const events = (
await Promise.all(
networkFromBlocks.map(async (networkFromBlock) =>
getEventsForNetwork(
networkFromBlock.networkName,
networkFromBlock.fromBlock,
// Find the toBlock
networkToBlocks.find(
(networkToBlock) =>
networkToBlock.networkName === networkFromBlock.networkName
)!.fromBlock
),
),
)
).flat();

await writeEvents(events);
const newNetworkFromBlocks: NetworkBlock[] = await getNetworkFromBlocks();
await writeNetworkBlocks(newNetworkFromBlocks);
await writeNetworkBlocks(networkToBlocks);
}
run().catch(console.dir);

0 comments on commit a76aa70

Please sign in to comment.