Skip to content

Commit

Permalink
feat: Add preload app address logic to handle v2 factory event Applic…
Browse files Browse the repository at this point in the history
…ationCreated.
  • Loading branch information
brunomenezes committed Nov 15, 2024
1 parent e97e193 commit 27a9877
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions preloaders/ApplicationLoader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { EvmBatchProcessor } from '@subsquid/evm-processor';
import { Database, LocalDest } from '@subsquid/file-store';
import { createLogger } from '@subsquid/logger';
import { events as CartesiApplicationFactory } from '../src/abi/CartesiApplicationFactory';
import {
events as CartesiDAppFactory,
events,
} from '../src/abi/CartesiDAppFactory';
import { CartesiDAppFactoryAddress, getConfig } from '../src/config';
import {
CartesiDAppFactoryAddress,
getConfig,
RollupsAddressBook,
} from '../src/config';

type Metadata = {
height: number;
Expand All @@ -24,10 +29,11 @@ if (!process.env.CHAIN_ID) {

const chainId = parseInt(process.env.CHAIN_ID ?? 0);
const config = getConfig(chainId);
const supportV2 = config.v2 !== undefined && config.v2 !== null;

logger.info(`Processing chain_id: ${chainId}`);
logger.info(`Processing chainId: ${chainId}`);

const processor = new EvmBatchProcessor()
let processor = new EvmBatchProcessor()
.setGateway(config.dataSource.archive!)
.setRpcDataIngestionSettings({ disabled: true })
.setFinalityConfirmation(config.finalityConfirmation ?? 10)
Expand All @@ -44,8 +50,16 @@ const processor = new EvmBatchProcessor()
topic0: [CartesiDAppFactory.ApplicationCreated.topic],
});

if (supportV2) {
processor = processor.addLog({
address: [RollupsAddressBook.v2.ApplicationFactory],
topic0: [CartesiApplicationFactory.ApplicationCreated.topic],
});
}

const appFilename = `applications-${chainId}.json` as const;

let appsByFactoryAddress: Record<string, string[]> = {};
let applications: string[] = [];
let isInit = false;

Expand All @@ -61,7 +75,7 @@ const database = new Database({
.then(JSON.parse);

if (!isInit) {
applications = addresses[CartesiDAppFactoryAddress];
appsByFactoryAddress = addresses;
isInit = true;
}

Expand All @@ -74,7 +88,7 @@ const database = new Database({
let metadata: Metadata = {
...info,
addresses: {
[CartesiDAppFactoryAddress]: applications,
...appsByFactoryAddress,
},
};

Expand All @@ -83,6 +97,16 @@ const database = new Database({
},
});

const addAppAddressFor = (factoryAddress: string, appAddress: string) => {
const id = appAddress.toLowerCase();
const list = appsByFactoryAddress[factoryAddress] ?? [];

list.push(id);
appsByFactoryAddress[factoryAddress] = list;
};

const ApplicationFactoryAddress = RollupsAddressBook.v2.ApplicationFactory;

processor.run(database, async (ctx) => {
for (const block of ctx.blocks) {
for (const log of block.logs) {
Expand All @@ -91,10 +115,19 @@ processor.run(database, async (ctx) => {
log.topics[0] === events.ApplicationCreated.topic
) {
const { application } = events.ApplicationCreated.decode(log);
const id = application.toLowerCase();
addAppAddressFor(CartesiDAppFactoryAddress, application);
ctx.log.info(`${application} (Application v1) preloaded`);
} else if (
log.address === ApplicationFactoryAddress &&
log.topics[0] ===
CartesiApplicationFactory.ApplicationCreated.topic
) {
const { appContract } =
CartesiApplicationFactory.ApplicationCreated.decode(log);

addAppAddressFor(ApplicationFactoryAddress, appContract);

applications.push(id);
ctx.log.info(`${id} (Application) preloaded`);
ctx.log.info(`${appContract} (Application v2) preloaded`);
}
}
}
Expand Down

0 comments on commit 27a9877

Please sign in to comment.