Skip to content

Commit

Permalink
Add startingBlock option (#138)
Browse files Browse the repository at this point in the history
This is a shortcut for the most common case (specifying only the
starting block without the hash).
  • Loading branch information
fracek authored Jan 20, 2025
2 parents 2093bc4 + 9653510 commit 069644d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "indexer: add `startingBlock` option",
"packageName": "@apibara/indexer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 1 addition & 3 deletions examples/cli/indexers/1-evm.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export function createIndexer<
return defineIndexer(EvmStream)({
streamUrl: "https://ethereum.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 215_30_000n,
},
startingBlock: 215_30_000n,
filter: {
logs: [
{
Expand Down
4 changes: 1 addition & 3 deletions examples/cli/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export function createIndexer<
return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 10_30_000n,
},
startingBlock: 10_30_000n,
plugins: [
drizzleStorage({
db: database,
Expand Down
4 changes: 1 addition & 3 deletions examples/cli/indexers/3-starknet-factory.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
finality: "accepted",
startingCursor: {
orderKey: 800_000n,
},
startingBlock: 800_000n,
filter: {
events: [
{
Expand Down
43 changes: 33 additions & 10 deletions packages/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,24 @@ export interface IndexerHooks<TFilter, TBlock> {
"message:systemMessage": ({ message }: { message: SystemMessage }) => void;
}

export interface IndexerConfig<TFilter, TBlock> {
export type IndexerStartingCursor =
| {
startingCursor?: never;
startingBlock: bigint;
}
| {
startingCursor: Cursor;
startingBlock?: never;
}
| {
startingCursor?: never;
startingBlock?: never;
};

export type IndexerConfig<TFilter, TBlock> = {
streamUrl: string;
filter: TFilter;
finality?: DataFinality;
startingCursor?: Cursor;
factory?: ({
block,
context,
Expand All @@ -84,12 +97,14 @@ export interface IndexerConfig<TFilter, TBlock> {
hooks?: NestedHooks<IndexerHooks<TFilter, TBlock>>;
plugins?: ReadonlyArray<IndexerPlugin<TFilter, TBlock>>;
debug?: boolean;
}
} & IndexerStartingCursor;

export interface IndexerWithStreamConfig<TFilter, TBlock>
extends IndexerConfig<TFilter, TBlock> {
export type IndexerWithStreamConfig<TFilter, TBlock> = IndexerConfig<
TFilter,
TBlock
> & {
streamConfig: StreamConfig<TFilter, TBlock>;
}
};

export function defineIndexer<TFilter, TBlock>(
streamConfig: StreamConfig<TFilter, TBlock>,
Expand Down Expand Up @@ -203,16 +218,25 @@ export async function run<TFilter, TBlock>(

await indexer.hooks.callHook("run:before");

// Check if the it's factory mode or not
const isFactoryMode = indexer.options.factory !== undefined;

// if factory mode we add a empty filter
// Give priority to startingCursor over startingBlock.
let startingCursor: Cursor | undefined;
if (indexer.options.startingCursor) {
startingCursor = indexer.options.startingCursor;
} else if (indexer.options.startingBlock !== undefined) {
startingCursor = {
orderKey: indexer.options.startingBlock,
};
}

// if factory mode we add a empty filter at the end of the filter array.
const request = indexer.streamConfig.Request.make({
filter: isFactoryMode
? [indexer.options.filter, {} as TFilter]
: [indexer.options.filter],
finality: indexer.options.finality,
startingCursor: indexer.options.startingCursor,
startingCursor,
});

const options: StreamDataOptions = {};
Expand All @@ -225,7 +249,6 @@ export async function run<TFilter, TBlock>(
mainFilter = request.filter[1];
}

// create stream
let stream: AsyncIterator<
StreamDataResponse<TBlock>,
StreamDataResponse<TBlock>
Expand Down

0 comments on commit 069644d

Please sign in to comment.