Skip to content

Commit

Permalink
example: add factory mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Dec 2, 2024
1 parent 8431a8f commit 646bf15
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/cli/indexers/3-starknet-factory.indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { defineIndexer } from "@apibara/indexer";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { StarknetStream } from "@apibara/starknet";
import type { ApibaraRuntimeConfig } from "apibara/types";
import { hash } from "starknet";

const PAIR_CREATED = hash.getSelectorFromName("PairCreated") as `0x${string}`;
const SWAP = hash.getSelectorFromName("Swap") as `0x${string}`;

export default function (runtimeConfig: ApibaraRuntimeConfig) {
return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 800_000n,
},
filter: {
header: "always",
events: [
{
address:
"0x00dad44c139a476c7a17fc8141e6db680e9abc9f56fe249a105094c44382c2fd",
keys: [PAIR_CREATED],
},
],
},
async factory({ block: { events } }) {
const logger = useLogger();

const poolEvents = (events ?? []).flatMap((event) => {
const pairAddress = event.data?.[2];

logger.log(`Factory: PairAddress - ${pairAddress}`);
return {
address: pairAddress,
keys: [SWAP],
includeReceipt: false,
};
});

logger.log("poolEvents", poolEvents);

return {
filter: {
header: "always",
events: poolEvents,
},
};
},
async transform({ block, endCursor }) {
const logger = useLogger();
logger.log("Transforming ", endCursor?.orderKey);
logger.log("Events ", block.events);
},
});
}

0 comments on commit 646bf15

Please sign in to comment.