Skip to content

Commit

Permalink
example: add indexers test in cli example
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Dec 4, 2024
1 parent 9c64da3 commit 461f830
Show file tree
Hide file tree
Showing 10 changed files with 1,349 additions and 3,446 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "indexer: upgrade drizzle & fix vcr",
"packageName": "@apibara/indexer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/apibara-b7f05b76-9746-4610-880e-19cd603487eb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "cli: fix infinite type instantiation errors in apibara config",
"packageName": "apibara",
"email": "[email protected]",
"dependentChangeType": "patch"
}
311 changes: 311 additions & 0 deletions examples/cli/cassettes/ethereum-usdc-transfers.json

Large diffs are not rendered by default.

3,432 changes: 0 additions & 3,432 deletions examples/cli/cassettes/simple-test.json

This file was deleted.

710 changes: 710 additions & 0 deletions examples/cli/cassettes/starknet-usdc-transfers.json

Large diffs are not rendered by default.

136 changes: 136 additions & 0 deletions examples/cli/test/ethereum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { createVcr } from "@apibara/indexer/testing";
import { drizzle } from "drizzle-orm/pglite";
import { beforeAll, describe, expect, it } from "vitest";
import { createIndexer } from "../indexers/1-evm.indexer";
import { ethereumUsdcTransfers } from "../lib/schema";
import * as schema from "../lib/schema";
import { migratePglite } from "./helper";

const vcr = createVcr();

const database = drizzle({
schema,
connection: {
dataDir: "memory://ethereum",
},
});

describe("Ethereum USDC Transfers indexer", () => {
beforeAll(async () => {
await migratePglite(database);
});

it("should work", async () => {
const indexer = createIndexer({ database });

await vcr.run("ethereum-usdc-transfers", indexer, {
fromBlock: 10_000_000n,
toBlock: 10_000_005n,
});
const rows = await database.select().from(ethereumUsdcTransfers);

expect(rows).toMatchInlineSnapshot(`
[
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000001,
"mask": 36,
"upper": null,
},
},
"hash": "0xfc80de5a3b766eece5c5a7f7858a9d537a8fefa8186c71fa7766a2bae939b816",
"number": 10000001,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000001,
"mask": 36,
"upper": null,
},
},
"hash": "0x8a38d2a2de4821f6d13d393c364fced4b0280dc66976cd43b7f9c3dd4aeebae6",
"number": 10000001,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000002,
"mask": 36,
"upper": null,
},
},
"hash": "0xa20537a561c41855e6719f687d060217cfe105f6314c2facd68accbb197b7a72",
"number": 10000002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000002,
"mask": 36,
"upper": null,
},
},
"hash": "0xec921d30995ee6e19c67712d264311fffc1936262d6bc99db44e93682866838f",
"number": 10000002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000003,
"mask": 36,
"upper": null,
},
},
"hash": "0x2894671b55148dc194b9ad859ca231fa3ff8a7e0a3cde2e5c7bb21915a448104",
"number": 10000003,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000003,
"mask": 36,
"upper": null,
},
},
"hash": "0x77b58d6769d43bd21379dea89d65bd860127753de7e7b607cec267d4ddcbf9c6",
"number": 10000003,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000003,
"mask": 36,
"upper": null,
},
},
"hash": "0x46d3e295575268ddb5cb778d52dd2b2f66e6e4dc984bdaa1b3ae8459d3c0ccb3",
"number": 10000003,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000003,
"mask": 36,
"upper": null,
},
},
"hash": "0x0862e1e01a91390133574765808540617de4dc3faf66c3acc6b45fe3eb478b0d",
"number": 10000003,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 10000005,
"mask": 36,
"upper": null,
},
},
"hash": "0xd50111f6b29419d975446d9f1ce05386f4778a1532ab43267e88f33fb71e01e2",
"number": 10000005,
},
]
`);
});
});
7 changes: 7 additions & 0 deletions examples/cli/test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { PgliteDatabase } from "drizzle-orm/pglite";
import { migrate } from "drizzle-orm/pglite/migrator";
import type * as schema from "../lib/schema";

export async function migratePglite(database: PgliteDatabase<typeof schema>) {
return await migrate(database, { migrationsFolder: "./drizzle" });
}
181 changes: 169 additions & 12 deletions examples/cli/test/starknet.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,180 @@
import { createVcr } from "@apibara/indexer/testing";
import { describe, expect, it } from "vitest";

import Database from "better-sqlite3";
import starknetIndexer from "../indexers/2-starknet.indexer";
import { drizzle } from "drizzle-orm/pglite";
import { beforeAll, describe, expect, it } from "vitest";
import { createIndexer } from "../indexers/2-starknet.indexer";
import { starknetUsdcTransfers } from "../lib/schema";
import * as schema from "../lib/schema";
import { migratePglite } from "./helper";

const vcr = createVcr();

describe("Starknet indexer", () => {
const database = drizzle({
schema,
connection: {
dataDir: "memory://starknet",
},
});

describe("Starknet USDC Transfers indexer", () => {
beforeAll(async () => {
await migratePglite(database);
});

it("should work", async () => {
const databasePath = "/tmp/my-db.sqlite";
const indexer = starknetIndexer({ databasePath });
await vcr.run("simple-test", indexer, {
const indexer = createIndexer({ database });

await vcr.run("starknet-usdc-transfers", indexer, {
fromBlock: 800_000n,
toBlock: 800_005n,
});
const rows = await database.select().from(starknetUsdcTransfers);

const database = new Database(databasePath);
const rows = database.prepare("SELECT * FROM test").all();
// TODO: update indexer to insert data
expect(rows).toMatchInlineSnapshot("[]");
expect(rows).toMatchInlineSnapshot(`
[
{
"_cursor": Int8Range {
"range": Range {
"lower": 800001,
"mask": 36,
"upper": null,
},
},
"hash": "0x006dcfffad0aebb8e496bf1af95b97ab181e9b86bd61a07c2926407ea70c0d0e",
"number": 800001,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x07b2c7d48d92eb6110b5039a112110b4ee5b59b9249764d22441c988f2a3730d",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x0288e4f00a93f4ea8f61bc0c218df8070c2a4ecd219e18a8fe39d473e62dff08",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x029959d46cfadb2b0c8cc823a9e4c03b7f22f8c596c424c9e11db62ee7fa96ba",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x0247f793e0525ed7d9d369b1b701942e55c4d41a3116733f0c77af3c0dfd21ad",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x06683c568f2e977e3d4b6f4da70b3926950c83f5a7c6119b3f2a643643db8fed",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x041a41f4fdda921c6049188312aaaa28f63e6bb628d6dd604891449d4d76b395",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x0393e0fbc25313322c088d5d457961f72bf6f6f31cc846f3e8aa96c1e53922c3",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x05042aeb03be2ec2ae48d78dcc69e9d79221807cc70eedcf083db70adfd88eac",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800002,
"mask": 36,
"upper": null,
},
},
"hash": "0x06ecfe783686b0dc2747acfcc77e7dcda2b2baea6e65bc8624537e14ca024343",
"number": 800002,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800003,
"mask": 36,
"upper": null,
},
},
"hash": "0x0359aeed2843c282006cff7326fdce682efd3e6c43170df8fad02f1e0d1a9446",
"number": 800003,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800004,
"mask": 36,
"upper": null,
},
},
"hash": "0x0725361f6a2bafeb496b0b879ed7711d8ae3afd9dfd973bfac97ff0e3cb0cd4b",
"number": 800004,
},
{
"_cursor": Int8Range {
"range": Range {
"lower": 800005,
"mask": 36,
"upper": null,
},
},
"hash": "0x079bdefcacc75dd1db6dfcdf3b29e9c30d2b70002d9d85a3d1b2032e2c72251c",
"number": 800005,
},
]
`);
});
});
2 changes: 1 addition & 1 deletion examples/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"citty": "^0.1.6",
"consola": "^3.2.3",
"csv-stringify": "^6.5.0",
"drizzle-orm": "^0.35.0",
"drizzle-orm": "^0.37.0",
"postgres": "^3.4.4",
"viem": "^2.12.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/starknet-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"citty": "^0.1.6",
"consola": "^3.2.3",
"csv-stringify": "^6.5.0",
"drizzle-orm": "^0.35.0",
"drizzle-orm": "^0.37.0",
"postgres": "^3.4.4",
"sqlite": "^5.1.1",
"viem": "^2.12.4"
Expand Down

0 comments on commit 461f830

Please sign in to comment.