Skip to content
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: 7 additions & 0 deletions change/apibara-3c1e3504-7ac7-41b7-bb7f-02c09deb8cf9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "feat: integrate runtime configuration serialization into rolldown",
"packageName": "apibara",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
6 changes: 6 additions & 0 deletions examples/cli/apibara.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { defineConfig } from "apibara/config";

export default defineConfig({
runtimeConfig: {
evm: {
startingBlock: 215_30_000n,
},
starknet: {
startingBlock: 10_30_000n,
},
connectionString: process.env["POSTGRES_CONNECTION_STRING"] ?? "memory://",
},
presets: {
Expand Down
7 changes: 5 additions & 2 deletions examples/cli/indexers/1-evm.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const abi = parseAbi([

// USDC Transfers on Ethereum
export default function (runtimeConfig: ApibaraRuntimeConfig) {
const { connectionString } = runtimeConfig;
const {
connectionString,
evm: { startingBlock },
} = runtimeConfig;

const database = drizzle({
schema: {
Expand All @@ -27,7 +30,7 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
return defineIndexer(EvmStream)({
streamUrl: "https://ethereum.preview.apibara.org",
finality: "accepted",
startingBlock: 215_30_000n,
startingBlock,
filter: {
logs: [
{
Expand Down
7 changes: 5 additions & 2 deletions examples/cli/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { hash } from "starknet";

// USDC Transfers on Starknet
export default function (runtimeConfig: ApibaraRuntimeConfig) {
const { connectionString } = runtimeConfig;
const {
connectionString,
starknet: { startingBlock },
} = runtimeConfig;

const database = drizzle({
schema: {
Expand All @@ -22,7 +25,7 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingBlock: 10_30_000n,
startingBlock,
plugins: [
drizzleStorage({
db: database,
Expand Down
6 changes: 5 additions & 1 deletion examples/cli/test/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const connectionString = "memory://ethereum";

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

const testResult = await vcr.run("ethereum-usdc-transfers", indexer, {
fromBlock: 10_000_000n,
Expand Down
6 changes: 5 additions & 1 deletion examples/cli/test/starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const connectionString = "memory://starknet";

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

const testResult = await vcr.run("starknet-usdc-transfers", indexer, {
fromBlock: 800_000n,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"dependencies": {
"@apibara/indexer": "workspace:*",
"@apibara/protocol": "workspace:*",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-virtual": "^3.0.2",
"c12": "^1.11.1",
"chokidar": "^3.6.0",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default defineCommand({
apibara = await createApibara(
{
rootDir,
preset: args.preset,
},
{
watch: true,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default defineCommand({

const apibara = await createApibara({
rootDir,
preset,
});

apibara.logger.start(
Expand Down
23 changes: 21 additions & 2 deletions packages/cli/src/core/config/resolvers/runtime-config.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import type { ApibaraOptions } from "apibara/types";
import { serialize } from "../../../utils/helper";

export async function resolveRuntimeConfigOptions(options: ApibaraOptions) {
options.runtimeConfig = { ...options.runtimeConfig };
process.env.APIBARA_RUNTIME_CONFIG = JSON.stringify(options.runtimeConfig);
const { preset, presets } = options;
let runtimeConfig: Record<string, unknown> = { ...options.runtimeConfig };

if (preset) {
if (presets === undefined) {
throw new Error(
`Specified preset "${preset}" but no presets were defined`,
);
}

if (presets[preset] === undefined) {
throw new Error(`Specified preset "${preset}" but it was not defined`);
}

const presetValue = presets[preset] as {
runtimeConfig: Record<string, unknown>;
};
runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
}
process.env.APIBARA_RUNTIME_CONFIG = serialize(runtimeConfig);
}
3 changes: 2 additions & 1 deletion packages/cli/src/hooks/useRuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ApibaraRuntimeConfig } from "apibara/types";
import { deserialize } from "../utils/helper";

export function useRuntimeConfig(): ApibaraRuntimeConfig {
return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG || "{}");
return deserialize(process.env.APIBARA_RUNTIME_CONFIG || "{}");
}
25 changes: 25 additions & 0 deletions packages/cli/src/rolldown/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from "node:fs";
import { builtinModules } from "node:module";
import replace from "@rollup/plugin-replace";
import type { Apibara } from "apibara/types";
import defu from "defu";
import { join } from "pathe";
Expand All @@ -8,6 +9,7 @@ import type {
RolldownOptions,
RolldownPluginOption,
} from "rolldown";
import { serialize } from "../utils/helper";
import { appConfig } from "./plugins/config";
import { indexers } from "./plugins/indexers";

Expand Down Expand Up @@ -76,8 +78,31 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
},
);

rolldownConfig.plugins?.push(
replace({
preventAssignment: true,
values: {
"process.env.APIBARA_CONFIG": getSerializedRuntimeConfig(apibara),
},
}) as RolldownPluginOption,
);
rolldownConfig.plugins?.push(indexers(apibara));
rolldownConfig.plugins?.push(appConfig(apibara));

return rolldownConfig;
}

function getSerializedRuntimeConfig(apibara: Apibara) {
try {
return serialize({
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.",
{ cause: error },
);
}
}
16 changes: 14 additions & 2 deletions packages/cli/src/rolldown/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ import type { RolldownPluginOption } from "rolldown";
export function appConfig(apibara: Apibara) {
return virtual({
"#apibara-internal-virtual/config": `
import * as projectConfig from '${apibara.options._c12.configFile}';
const serializedConfig = \`process.env.APIBARA_CONFIG\`;

export const config = projectConfig.default;
if (serializedConfig === undefined || serializedConfig === "") {
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);
`,
}) as RolldownPluginOption;
}
25 changes: 17 additions & 8 deletions packages/cli/src/runtime/internal/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,28 @@ export function createIndexer(indexerName: string, preset?: string) {
? indexerModule(runtimeConfig)
: indexerModule;

let reporter: ConsolaReporter = createLogger({
const reporter: ConsolaReporter = createLogger({
indexer: indexerName,
preset,
indexers: availableIndexers,
});

if (config.logger) {
reporter = config.logger({
indexer: indexerName,
preset,
indexers: availableIndexers,
});
}
// TODO: Add support for custom logger
//
// Custom logger support is temporarily disabled to prevent bundling issues.
// Previously, when using the config object directly, Rollup would bundle everything
// referenced in apibara.config, including plugins. Now we serialize only the
// essential config properties (runtimeConfig, preset, presets) during build time,
// which prevents unwanted bundling but also means we can't access the logger
// configuration.
//
// if (config.logger) {
// reporter = config.logger({
// indexer: indexerName,
// preset,
// indexers: availableIndexers,
// });
// }

// Put the in-memory persistence plugin first so that it can be overridden by any user-defined
// persistence plugin.
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/types/virtual/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { ApibaraConfig } from "apibara/types";

export const config: ApibaraConfig = {};
export const config: Pick<
ApibaraConfig,
"runtimeConfig" | "preset" | "presets"
> = {};
15 changes: 15 additions & 0 deletions packages/cli/src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function deserialize<T>(str: string): T {
return JSON.parse(str, (_, value) =>
typeof value === "string" && value.match(/^\d+n$/)
? BigInt(value.slice(0, -1))
: value,
) as T;
}

export function serialize<T extends Record<string, unknown>>(obj: T): string {
return JSON.stringify(
obj,
(_, value) => (typeof value === "bigint" ? `${value.toString()}n` : value),
"\t",
);
}
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.