-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
233 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.turbo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "example-starknet-indexer", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "jiti ./src/main.ts", | ||
"typecheck": "tsc --noEmit", | ||
"lint": "biome check .", | ||
"lint:fix": "pnpm lint --write", | ||
"format": "biome format . --write" | ||
}, | ||
"dependencies": { | ||
"@apibara/indexer": "workspace:*", | ||
"@apibara/protocol": "workspace:*", | ||
"@apibara/starknet": "workspace:*", | ||
"@opentelemetry/api": "^1.9.0", | ||
"@opentelemetry/exporter-trace-otlp-proto": "^0.52.0", | ||
"@opentelemetry/resources": "^1.25.0", | ||
"@opentelemetry/sdk-node": "^0.52.0", | ||
"@opentelemetry/sdk-trace-base": "^1.25.0", | ||
"@opentelemetry/semantic-conventions": "^1.25.0", | ||
"citty": "^0.1.6", | ||
"consola": "^3.2.3", | ||
"csv-stringify": "^6.5.0", | ||
"sqlite": "^5.1.1", | ||
"viem": "^2.12.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.12.12", | ||
"jiti": "^1.21.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import assert from "node:assert"; | ||
import { getReceipt, getTransaction, StarknetStream } from "@apibara/starknet"; | ||
import { defineIndexer, useIndexerContext } from "@apibara/indexer"; | ||
import consola from "consola"; | ||
|
||
export function createIndexerConfig(streamUrl: string) { | ||
return defineIndexer(StarknetStream)({ | ||
streamUrl, | ||
finality: "accepted", | ||
startingCursor: { | ||
orderKey: 300_000n, | ||
}, | ||
filter: { | ||
events: [ | ||
{ | ||
fromAddress: "0x00000005dd3D2F4429AF886cD1a3b08289DBcEa99A294197E9eB43b0e0325b4b", | ||
includeReceipt: true, | ||
includeTransaction: true, | ||
} | ||
] | ||
}, | ||
async transform({ block: { header, events, transactions, receipts } }) { | ||
const ts = header?.timestamp!; | ||
for (const event of events) { | ||
// Use helpers to access transaction and receipt | ||
const tx = getTransaction(event.transactionIndex!, transactions ?? []); | ||
const receipt = getReceipt(event.transactionIndex!, receipts ?? []); | ||
|
||
consola.info({ | ||
ts, | ||
eventIndex: event.eventIndex, | ||
actualFee: receipt?.meta?.actualFee, | ||
txType: tx?.transaction?._tag, | ||
}); | ||
} | ||
return []; | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createIndexer, run } from "@apibara/indexer"; | ||
import { createClient } from "@apibara/protocol"; | ||
import { defineCommand, runMain } from "citty"; | ||
import consola from "consola"; | ||
|
||
import { createIndexerConfig } from "./indexer"; | ||
|
||
const command = defineCommand({ | ||
meta: { | ||
name: "example-starknet-indexer", | ||
version: "1.0.0", | ||
description: "Example showing how to run an indexer", | ||
}, | ||
args: { | ||
stream: { | ||
type: "string", | ||
default: "http://127.0.0.1:7007", | ||
description: "Starknet stream URL", | ||
}, | ||
authToken: { | ||
type: "string", | ||
description: "DNA auth token", | ||
}, | ||
}, | ||
async run({ args }) { | ||
consola.info("Connecting to Starknet stream", args.stream); | ||
|
||
const indexer = createIndexer(createIndexerConfig(args.stream)); | ||
const client = createClient( | ||
indexer.streamConfig, | ||
indexer.options.streamUrl, | ||
); | ||
|
||
await run(client, indexer); | ||
}, | ||
}); | ||
|
||
runMain(command); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"declarationDir": "dist", | ||
"noEmit": false, | ||
"rootDir": "src", | ||
"types": ["node"] | ||
}, | ||
"include": ["src/"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { Transaction, TransactionReceipt } from "./block"; | ||
|
||
/** Returns the transaction receipt for the given transaction index. */ | ||
export function getReceipt( | ||
transactionIndex: number, | ||
receipts: readonly TransactionReceipt[], | ||
): TransactionReceipt | undefined { | ||
return binarySearch( | ||
transactionIndex, | ||
receipts, | ||
(receipt) => receipt.meta?.transactionIndex ?? 0, | ||
); | ||
} | ||
|
||
/** Returns the transaction for the given transaction index. */ | ||
export function getTransaction( | ||
transactionIndex: number, | ||
transactions: readonly Transaction[], | ||
): Transaction | undefined { | ||
return binarySearch( | ||
transactionIndex, | ||
transactions, | ||
(transaction) => transaction.meta?.transactionIndex ?? 0, | ||
); | ||
} | ||
|
||
function binarySearch<T>( | ||
index: number, | ||
arr: readonly T[], | ||
getIndex: (item: T) => number, | ||
): T | undefined { | ||
let left = 0; | ||
let right = arr.length - 1; | ||
|
||
while (left <= right) { | ||
const mid = Math.floor((left + right) / 2); | ||
const item = arr[mid]; | ||
const itemIndex = getIndex(item); | ||
|
||
if (itemIndex === index) { | ||
return item; | ||
} | ||
|
||
if (itemIndex < index) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.