Skip to content

chore: qol improvements and fixes #160

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

Merged
merged 10 commits into from
Mar 21, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "chore: upgrade consola package",
"packageName": "@apibara/indexer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-drizzle: use seperate schema and default connectionString to env",
"packageName": "@apibara/plugin-drizzle",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-mongo: propagate error",
"packageName": "@apibara/plugin-mongo",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "protocol: export nice-grpc MetaData class",
"packageName": "@apibara/protocol",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/apibara-d3198897-bb32-4e69-a1de-117791352c44.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "cli: add createAuthenticatedClient and improve runtimeConfig",
"packageName": "apibara",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion examples/beaconchain-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@apibara/protocol": "workspace:*",
"@apibara/beaconchain": "workspace:*",
"citty": "^0.1.6",
"consola": "^3.2.3",
"consola": "^3.4.2",
"nice-grpc": "^2.1.8"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion examples/cli-drizzle/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
POSTGRES_CONNECTION_STRING="postgres://postgres:postgres@localhost:5432/postgres"
POSTGRES_CONNECTION_STRING="postgres://postgres:postgres@localhost:5432/postgres"
# This is optional, it will be merged with the runtimeConfig and preset if provided.
# Priority (Highest to lowest):
# 1. APIBARA_RUNTIME_CONFIG
# 2. Preset
# 3. Defaults
APIBARA_RUNTIME_CONFIG='{"evm": {"startingBlock": 10000000}, "starknet": {"startingBlock": 800000}}'
DNA_TOKEN="your-dna-token"
12 changes: 2 additions & 10 deletions examples/cli-drizzle/apibara.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ import { defineConfig } from "apibara/config";
export default defineConfig({
runtimeConfig: {
evm: {
startingBlock: 215_30_000n,
startingBlock: 215_30_000,
},
starknet: {
startingBlock: 10_30_000n,
},
connectionString: process.env["POSTGRES_CONNECTION_STRING"] ?? "memory://",
},
presets: {
dev: {
runtimeConfig: {
connectionString: "memory://",
},
startingBlock: 12_45_000,
},
},
});
5 changes: 2 additions & 3 deletions examples/cli-drizzle/indexers/1-evm.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ const abi = parseAbi([
// USDC Transfers on Ethereum
export default function (runtimeConfig: ApibaraRuntimeConfig) {
const {
connectionString,
evm: { startingBlock },
} = runtimeConfig;

// connectionString defaults to process.env["POSTGRES_CONNECTION_STRING"](postgresql) ?? "memory://" (in memory pglite)
const database = drizzle({
schema: {
ethereumUsdcTransfers,
},
connectionString,
});

return defineIndexer(EvmStream)({
streamUrl: "https://ethereum.preview.apibara.org",
finality: "accepted",
startingBlock,
startingBlock: BigInt(startingBlock),
filter: {
logs: [
{
Expand Down
17 changes: 10 additions & 7 deletions examples/cli-drizzle/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ import { hash } from "starknet";
// USDC Transfers on Starknet
export default function (runtimeConfig: ApibaraRuntimeConfig) {
const {
connectionString,
starknet: { startingBlock },
} = runtimeConfig;

// connectionString defaults to process.env["POSTGRES_CONNECTION_STRING"](postgresql) ?? "memory://" (in memory pglite)
const database = drizzle({
schema: {
starknetUsdcTransfers,
},
connectionString,
});

return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingBlock,
startingBlock: BigInt(startingBlock),
plugins: [
drizzleStorage({
db: database,
Expand Down Expand Up @@ -67,10 +66,14 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
}

await db.insert(starknetUsdcTransfers).values(
Array.from(transactionHashes).map((transactionHash) => ({
number: Number(endCursor?.orderKey),
hash: transactionHash,
})),
Array.from(transactionHashes)
.map((transactionHash) => ({
number: Number(endCursor?.orderKey),
hash: transactionHash,
}))
.filter(
({ number }) => number !== undefined && !Number.isNaN(number),
),
);
},
});
Expand Down
1 change: 1 addition & 0 deletions examples/cli-drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "apibara build",
"dev": "apibara dev",
"start": "apibara start",
"start:pg": "export POSTGRES_CONNECTION_STRING=\"postgres://postgres:postgres@localhost:5432/postgres\" && pnpm start",
"lint": "biome check .",
"lint:fix": "pnpm lint --write",
"test": "vitest",
Expand Down
7 changes: 2 additions & 5 deletions examples/cli-drizzle/test/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { describe, expect, it } from "vitest";

const vcr = createVcr();

const connectionString = "memory://ethereum";

describe("Ethereum USDC Transfers indexer", () => {
it("should work", async () => {
const indexer = createIndexer({
connectionString,
evm: { startingBlock: 10_000_000n },
starknet: { startingBlock: 800_000n },
evm: { startingBlock: 10_000_000 },
starknet: { startingBlock: 800_000 },
});

const testResult = await vcr.run("ethereum-usdc-transfers", indexer, {
Expand Down
6 changes: 2 additions & 4 deletions examples/cli-drizzle/test/starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import { starknetUsdcTransfers } from "@/lib/schema";
import { getTestDatabase } from "@apibara/plugin-drizzle/testing";

const vcr = createVcr();
const connectionString = "memory://starknet";

describe("Starknet USDC Transfers indexer", () => {
it("should work", async () => {
const indexer = createIndexer({
connectionString,
evm: { startingBlock: 10_000_000n },
starknet: { startingBlock: 800_000n },
evm: { startingBlock: 10_000_000 },
starknet: { startingBlock: 800_000 },
});

const testResult = await vcr.run("starknet-usdc-transfers", indexer, {
Expand Down
2 changes: 1 addition & 1 deletion examples/evm-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@apibara/evm": "workspace:*",
"@apibara/protocol": "workspace:*",
"citty": "^0.1.6",
"consola": "^3.2.3",
"consola": "^3.4.2",
"nice-grpc": "^2.1.8",
"viem": "^2.12.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/starknet-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@apibara/protocol": "workspace:*",
"@apibara/starknet": "workspace:*",
"citty": "^0.1.6",
"consola": "^3.2.3",
"consola": "^3.4.2",
"nice-grpc": "^2.1.8",
"viem": "^2.22.9"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"c12": "^1.11.1",
"chokidar": "^3.6.0",
"citty": "^0.1.6",
"consola": "^3.2.3",
"consola": "^3.4.2",
"defu": "^6.1.4",
"fs-extra": "^11.2.0",
"hookable": "^5.5.3",
Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ export default defineCommand({
stdio: "inherit",
});

childProcess.on("close", (code) => {
if (code !== null) {
apibara.logger.log(
`Indexers process exited with code ${colors.red(code)}`,
);
}
childProcess.on("close", (code, signal) => {
console.log();
apibara.logger.log(
`Indexers process exited${
code !== null ? ` with code ${colors.red(code)}` : ""
}`,
);
process.exit(code ?? 0);
});
});
};
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawn } from "node:child_process";
import { createApibara } from "apibara/core";
import { defineCommand } from "citty";
import { colors } from "consola/utils";
import fse from "fs-extra";
import { resolve } from "pathe";
import { commonArgs } from "../common";
Expand Down Expand Up @@ -55,8 +56,18 @@ export default defineCommand({
...(preset ? ["--preset", preset] : []),
];

spawn("node", childArgs, {
const childProcess = spawn("node", childArgs, {
stdio: "inherit",
});

childProcess.on("close", (code, signal) => {
console.log();
apibara.logger.log(
`Indexers process exited${
code !== null ? ` with code ${colors.red(code)}` : ""
}`,
);
process.exit(code ?? 0);
});
},
});
20 changes: 13 additions & 7 deletions packages/cli/src/core/config/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@ import { loadConfig, watchConfig } from "c12";
import { klona } from "klona/full";
import { ApibaraDefaults } from "./defaults";
import { resolvePathOptions } from "./resolvers/paths.resolver";
import { presetResolver } from "./resolvers/preset.resolver";
import { resolveRuntimeConfigOptions } from "./resolvers/runtime-config.resolver";

const configResolvers = [
resolvePathOptions,
resolveRuntimeConfigOptions,
presetResolver,
] as const;
const configResolvers = [resolvePathOptions] as const;

export async function loadOptions(
configOverrides: ApibaraConfig = {},
opts: LoadConfigOptions = {},
dev = false,
): Promise<ApibaraOptions> {
const options = await _loadUserConfig(configOverrides, opts, dev);

// Check if the runtimeConfig is serializable
try {
JSON.stringify(options.runtimeConfig);
} catch (error) {
throw new Error(
"Non-serializable runtimeConfig. Please ensure the config is serializable.",
{ cause: error },
);
}

for (const resolver of configResolvers) {
await resolver(options);
}

return options;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/core/config/resolvers/preset.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { ApibaraOptions } from "apibara/types";
import defu from "defu";

/**
* @note This resolver is not in use currently, as we resolve presets in runtime files.
*/
export async function presetResolver(options: ApibaraOptions) {
if (options.preset && options.presets?.[options.preset]) {
const new_options = defu(options.presets[options.preset], options);
Expand Down
25 changes: 0 additions & 25 deletions packages/cli/src/core/config/resolvers/runtime-config.resolver.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/cli/src/hooks/useRuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ApibaraRuntimeConfig } from "apibara/types";
import { deserialize } from "../utils/helper";

export function useRuntimeConfig(): ApibaraRuntimeConfig {
return deserialize(process.env.APIBARA_RUNTIME_CONFIG || "{}");
return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG_HOOK_DATA || "{}");
}
5 changes: 2 additions & 3 deletions packages/cli/src/rolldown/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
RolldownOptions,
RolldownPluginOption,
} from "rolldown";
import { serialize } from "../utils/helper";
import { appConfig } from "./plugins/config";
import { indexers } from "./plugins/indexers";
import { instrumentation } from "./plugins/instrumentation";
Expand Down Expand Up @@ -97,14 +96,14 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {

function getSerializedRuntimeConfig(apibara: Apibara) {
try {
return serialize({
return JSON.stringify({
runtimeConfig: apibara.options.runtimeConfig,
preset: apibara.options.preset,
presets: apibara.options.presets,
});
} catch (error) {
throw new Error(
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable. BigInt values are supported, but functions, symbols, etc. are not. Check your configuration for non-serializable values.",
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.",
{ cause: error },
);
}
Expand Down
10 changes: 1 addition & 9 deletions packages/cli/src/rolldown/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ export function appConfig(apibara: Apibara) {
throw new Error("APIBARA_CONFIG is not defined");
}

function deserialize(str) {
return JSON.parse(str, (_, value) =>
typeof value === "string" && value.match(/^\\d+n$/)
? BigInt(value.slice(0, -1))
: value,
);
}

export const config = deserialize(serializedConfig);
export const config = JSON.parse(serializedConfig);
`,
}) as RolldownPluginOption;
}
Loading