Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example: update cli example #131

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions examples/cli/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ services:
environment:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data

volumes:
postgres:
- "5432:5432"
25 changes: 0 additions & 25 deletions examples/cli/drizzle/0000_great_hobgoblin.sql

This file was deleted.

11 changes: 11 additions & 0 deletions examples/cli/drizzle/0000_plain_omega_red.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS "ethereum_usdc_transfers" (
"_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"number" bigint,
"hash" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "starknet_usdc_transfers" (
"_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"number" bigint,
"hash" text
);
101 changes: 15 additions & 86 deletions examples/cli/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
{
"id": "f33ac3ed-f308-44cf-a4b1-64871d409835",
"id": "9d645b77-7634-42a6-9849-92477d936c41",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.checkpoints": {
"name": "checkpoints",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"order_key": {
"name": "order_key",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"unique_key": {
"name": "unique_key",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.ethereum_usdc_transfers": {
"name": "ethereum_usdc_transfers",
"schema": "",
"columns": {
"_id": {
"name": "_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"number": {
"name": "number",
"type": "bigint",
Expand All @@ -50,12 +26,6 @@
"type": "text",
"primaryKey": false,
"notNull": false
},
"_cursor": {
"name": "_cursor",
"type": "int8range",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
Expand All @@ -66,52 +36,17 @@
"checkConstraints": {},
"isRLSEnabled": false
},
"public.filters": {
"name": "filters",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"filter": {
"name": "filter",
"type": "text",
"primaryKey": false,
"notNull": true
},
"from_block": {
"name": "from_block",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"to_block": {
"name": "to_block",
"type": "integer",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"filters_id_from_block_pk": {
"name": "filters_id_from_block_pk",
"columns": ["id", "from_block"]
}
},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.starknet_usdc_transfers": {
"name": "starknet_usdc_transfers",
"schema": "",
"columns": {
"_id": {
"name": "_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"number": {
"name": "number",
"type": "bigint",
Expand All @@ -123,12 +58,6 @@
"type": "text",
"primaryKey": false,
"notNull": false
},
"_cursor": {
"name": "_cursor",
"type": "int8range",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
Expand Down
4 changes: 2 additions & 2 deletions examples/cli/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1733315820544,
"tag": "0000_great_hobgoblin",
"when": 1735741739192,
"tag": "0000_plain_omega_red",
"breakpoints": true
}
]
Expand Down
31 changes: 18 additions & 13 deletions examples/cli/indexers/1-evm.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EvmStream } from "@apibara/evm";
import { defineIndexer, useSink } from "@apibara/indexer";
import { drizzlePersistence } from "@apibara/indexer/plugins/drizzle-persistence";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { drizzleSink } from "@apibara/indexer/sinks/drizzle";
import { defineIndexer } from "@apibara/indexer";
import { drizzleStorage, useDrizzleStorage } from "@apibara/plugin-drizzle";

import type { ApibaraRuntimeConfig } from "apibara/types";
import type {
Expand All @@ -14,6 +12,7 @@ import { encodeEventTopics, parseAbi } from "viem";

import { db } from "@/lib/db";
import { ethereumUsdcTransfers } from "@/lib/schema";
import { useLogger } from "@apibara/indexer/plugins";

const abi = parseAbi([
"event Transfer(address indexed from, address indexed to, uint256 value)",
Expand All @@ -40,7 +39,7 @@ export function createIndexer<
streamUrl: "https://ethereum.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 10_000_000n,
orderKey: 215_30_000n,
},
filter: {
logs: [
Expand All @@ -56,18 +55,24 @@ export function createIndexer<
],
},
plugins: [
drizzlePersistence({ database, indexerName: "evm-usdc-transfers" }),
drizzleStorage({
db: database,
persistState: true,
idColumn: "_id",
indexerName: "evm-usdc-transfers",
}),
],
sink: drizzleSink({
database,
tables: [ethereumUsdcTransfers],
}),
async transform({ endCursor, context, block }) {
async transform({ endCursor, context, block, finality }) {
const logger = useLogger();
const { db } = useSink({ context });
const { db } = useDrizzleStorage();
const { logs } = block;

logger.info("Transforming block ", endCursor?.orderKey);
logger.info(
"Transforming block | orderKey: ",
endCursor?.orderKey,
" | finality: ",
finality,
);

for (const log of logs) {
await db.insert(ethereumUsdcTransfers).values({
Expand Down
29 changes: 19 additions & 10 deletions examples/cli/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { defineIndexer, useSink } from "@apibara/indexer";
import { drizzlePersistence } from "@apibara/indexer/plugins/drizzle-persistence";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { drizzleSink } from "@apibara/indexer/sinks/drizzle";
import { defineIndexer } from "@apibara/indexer";

import { StarknetStream } from "@apibara/starknet";

import type { ApibaraRuntimeConfig } from "apibara/types";
Expand All @@ -14,6 +12,8 @@ import { hash } from "starknet";

import { db } from "@/lib/db";
import { starknetUsdcTransfers } from "@/lib/schema";
import { useLogger } from "@apibara/indexer/plugins";
import { drizzleStorage, useDrizzleStorage } from "@apibara/plugin-drizzle";

// USDC Transfers on Starknet
export default function (runtimeConfig: ApibaraRuntimeConfig) {
Expand All @@ -34,12 +34,16 @@ export function createIndexer<
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 800_000n,
orderKey: 10_30_000n,
},
plugins: [
drizzlePersistence({ database, indexerName: "starknet-usdc-transfers" }),
drizzleStorage({
db: database,
idColumn: "_id",
persistState: true,
indexerName: "starknet-usdc-transfers",
}),
],
sink: drizzleSink({ database, tables: [starknetUsdcTransfers] }),
filter: {
events: [
{
Expand All @@ -49,12 +53,17 @@ export function createIndexer<
},
],
},
async transform({ endCursor, block, context }) {
async transform({ endCursor, block, context, finality }) {
const logger = useLogger();
const { db } = useSink({ context });
const { db } = useDrizzleStorage();
const { events } = block;

logger.info("Transforming block ", endCursor?.orderKey);
logger.info(
"Transforming block | orderKey: ",
endCursor?.orderKey,
" | finality: ",
finality,
);

const transactionHashes = new Set<string>();

Expand Down
2 changes: 1 addition & 1 deletion examples/cli/indexers/3-starknet-factory.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineIndexer } from "@apibara/indexer";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { useLogger } from "@apibara/indexer/plugins";
import { StarknetStream } from "@apibara/starknet";

import type { ApibaraRuntimeConfig } from "apibara/types";
Expand Down
15 changes: 5 additions & 10 deletions examples/cli/lib/schema.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { pgIndexerTable } from "@apibara/indexer/sinks/drizzle";
import { bigint, pgTable, text, uuid } from "drizzle-orm/pg-core";

import { bigint, text } from "drizzle-orm/pg-core";

export const starknetUsdcTransfers = pgIndexerTable("starknet_usdc_transfers", {
export const starknetUsdcTransfers = pgTable("starknet_usdc_transfers", {
_id: uuid("_id").primaryKey().defaultRandom(),
number: bigint("number", { mode: "number" }),
hash: text("hash"),
});

export const ethereumUsdcTransfers = pgIndexerTable("ethereum_usdc_transfers", {
export const ethereumUsdcTransfers = pgTable("ethereum_usdc_transfers", {
_id: uuid("_id").primaryKey().defaultRandom(),
number: bigint("number", { mode: "number" }),
hash: text("hash"),
});

export {
checkpoints,
filters,
} from "@apibara/indexer/plugins/drizzle-persistence";
1 change: 1 addition & 0 deletions examples/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@apibara/evm": "workspace:*",
"@apibara/indexer": "workspace:*",
"@apibara/plugin-drizzle": "workspace:*",
"@apibara/protocol": "workspace:*",
"@apibara/starknet": "workspace:*",
"@electric-sql/pglite": "^0.2.14",
Expand Down
Loading
Loading