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/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/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, 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..6ad1742 --- /dev/null +++ b/examples/cli/indexers/3-starknet-factory.indexer.ts @@ -0,0 +1,65 @@ +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}`; +const shortAddress = (addr?: string) => + addr ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : ""; + +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 : ", + `\x1b[35m${pairAddress}\x1b[0m`, + ); + return { + address: pairAddress, + keys: [SWAP], + }; + }); + return { + filter: { + events: poolEvents, + }, + }; + }, + async transform({ block, endCursor }) { + const logger = useLogger(); + 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), + ); + } + }, + }); +} 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[] = [