From 86a09e356caf15c5111206fe4db8f93c79c515c3 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 3 Dec 2024 01:23:00 +0530 Subject: [PATCH 1/4] cli: add @electric-sql/pglite as external dependency --- packages/cli/src/rollup/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/rollup/config.ts b/packages/cli/src/rollup/config.ts index 8be71cf..353cd3c 100644 --- a/packages/cli/src/rollup/config.ts +++ b/packages/cli/src/rollup/config.ts @@ -12,7 +12,7 @@ import { appConfig } from "./plugins/config"; import { esmShim } from "./plugins/esm-shim"; import { indexers } from "./plugins/indexers"; -const runtimeDependencies = ["better-sqlite3"]; +const runtimeDependencies = ["better-sqlite3", "@electric-sql/pglite"]; export function getRollupConfig(apibara: Apibara): RollupConfig { const extensions: string[] = [ From 8431a8f0126eb10a034a3cff72b7a91defed2ab2 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 3 Dec 2024 01:23:52 +0530 Subject: [PATCH 2/4] example: add pglite persistence --- .gitignore | 3 ++- examples/cli/indexers/2-starknet.indexer.ts | 20 ++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index b83eb57..98ae658 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist .turbo bun.lockb .vscode -.apibara \ No newline at end of file +.apibara +.persistence diff --git a/examples/cli/indexers/2-starknet.indexer.ts b/examples/cli/indexers/2-starknet.indexer.ts index 339a9a5..0557b15 100644 --- a/examples/cli/indexers/2-starknet.indexer.ts +++ b/examples/cli/indexers/2-starknet.indexer.ts @@ -1,4 +1,4 @@ -import { defineIndexer, useSink } from "@apibara/indexer"; +import { defineIndexer } from "@apibara/indexer"; import { drizzlePersistence } from "@apibara/indexer/plugins/drizzle-persistence"; import { useLogger } from "@apibara/indexer/plugins/logger"; import { sqlite } from "@apibara/indexer/sinks/sqlite"; @@ -6,13 +6,10 @@ import { StarknetStream } from "@apibara/starknet"; import type { ApibaraRuntimeConfig } from "apibara/types"; import Database from "better-sqlite3"; import { sql } from "drizzle-orm"; -import { drizzle } from "drizzle-orm/node-postgres"; -import { Client } from "pg"; +import { drizzle } from "drizzle-orm/pglite"; import { hash } from "starknet"; export default function (runtimeConfig: ApibaraRuntimeConfig) { - console.log("--> Starknet Indexer Runtime Config: ", runtimeConfig); - // Sink Database const database = new Database(runtimeConfig.databasePath); database.exec("DROP TABLE IF EXISTS test"); @@ -21,10 +18,9 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) { ); // Persistence Database - const client = new Client({ - connectionString: "postgres://postgres:postgres@localhost:5432/postgres", + const persistDatabase = drizzle("./.persistence", { + logger: true, }); - const persistDatabase = drizzle(client); return defineIndexer(StarknetStream)({ streamUrl: "https://starknet.preview.apibara.org", @@ -52,7 +48,7 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) { async transform({ endCursor, block: { header }, context }) { const logger = useLogger(); logger.info("Transforming block ", endCursor?.orderKey); - const { writer } = useSink({ context }); + // const { writer } = useSink({ context }); // writer.insert([{ // number: header?.blockNumber.toString(), @@ -61,8 +57,6 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) { }, hooks: { async "run:before"() { - await client.connect(); - // Normally user will do migrations of both tables, which are defined in // ``` // import { checkpoints, filters } from "@apibara/indexer/plugins/drizzle-persistence" @@ -75,7 +69,9 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) { order_key INTEGER NOT NULL, unique_key TEXT ); - + `); + + await persistDatabase.execute(sql` CREATE TABLE IF NOT EXISTS filters ( id TEXT NOT NULL, filter TEXT NOT NULL, From 674a348b4dd88aa41c0e7e9485af86c925183438 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 3 Dec 2024 15:48:15 +0530 Subject: [PATCH 3/4] example: add factory mode --- ...-5146fc27-9558-48ae-819b-7000820431b8.json | 7 +++ .../indexers/3-starknet-factory.indexer.ts | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 change/apibara-5146fc27-9558-48ae-819b-7000820431b8.json create mode 100644 examples/cli/indexers/3-starknet-factory.indexer.ts diff --git a/change/apibara-5146fc27-9558-48ae-819b-7000820431b8.json b/change/apibara-5146fc27-9558-48ae-819b-7000820431b8.json new file mode 100644 index 0000000..c162d4a --- /dev/null +++ b/change/apibara-5146fc27-9558-48ae-819b-7000820431b8.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "cli: add @electric-sql/pglite as external dependency", + "packageName": "apibara", + "email": "jadejajaipal5@gmail.com", + "dependentChangeType": "patch" +} diff --git a/examples/cli/indexers/3-starknet-factory.indexer.ts b/examples/cli/indexers/3-starknet-factory.indexer.ts new file mode 100644 index 0000000..ddf7bf3 --- /dev/null +++ b/examples/cli/indexers/3-starknet-factory.indexer.ts @@ -0,0 +1,52 @@ +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, + }; + }); + return { + filter: { + header: "always", + events: poolEvents, + }, + }; + }, + async transform({ block, endCursor }) { + const logger = useLogger(); + logger.log("Transforming ", endCursor?.orderKey); + }, + }); +} From 31f98968840c178b5030194f5b03da15c79248e8 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 3 Dec 2024 18:20:14 +0530 Subject: [PATCH 4/4] example: add formatting in logs of factory --- .../indexers/3-starknet-factory.indexer.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/cli/indexers/3-starknet-factory.indexer.ts b/examples/cli/indexers/3-starknet-factory.indexer.ts index ddf7bf3..6ad1742 100644 --- a/examples/cli/indexers/3-starknet-factory.indexer.ts +++ b/examples/cli/indexers/3-starknet-factory.indexer.ts @@ -6,6 +6,8 @@ import { hash } from "starknet"; const PAIR_CREATED = hash.getSelectorFromName("PairCreated") as `0x${string}`; const SWAP = hash.getSelectorFromName("Swap") as `0x${string}`; +const shortAddress = (addr?: string) => + addr ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : ""; export default function (runtimeConfig: ApibaraRuntimeConfig) { return defineIndexer(StarknetStream)({ @@ -30,23 +32,34 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) { const poolEvents = (events ?? []).flatMap((event) => { const pairAddress = event.data?.[2]; - logger.log(`Factory: PairAddress - ${pairAddress}`); + logger.log( + "Factory: PairAddress : ", + `\x1b[35m${pairAddress}\x1b[0m`, + ); return { address: pairAddress, keys: [SWAP], - includeReceipt: false, }; }); return { filter: { - header: "always", events: poolEvents, }, }; }, async transform({ block, endCursor }) { const logger = useLogger(); - logger.log("Transforming ", endCursor?.orderKey); + const { events } = block; + + logger.log("Transforming... : ", endCursor?.orderKey); + for (const event of events) { + logger.log( + "Event Address : ", + shortAddress(event.address), + "| Txn hash :", + shortAddress(event.transactionHash), + ); + } }, }); }