Skip to content

Commit

Permalink
starknet: prepare for beta release
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jul 22, 2024
1 parent 316f5b7 commit 611d584
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 30 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.turbo
32 changes: 5 additions & 27 deletions examples/starknet-client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,19 @@ const command = defineCommand({
console.log(response);

const filter = Filter.make({
transactions: [
events: [
{
includeEvents: true,
includeMessages: true,
fromAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
includeReceipt: true,
includeReverted: true,
},
],
// header: {
// always: true,
// },
// events: [
// {
// // fromAddress: "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
// // fromAddress: "0x07b696af58c967c1b14c9dde0ace001720635a660a8e90c565ea459345318b30",
// // fromAddress: "0x0",
// // includeReceipt: true,
// // includeTransaction: true,
// // includeSiblings: true,
// // includeMessages: true,
// // includeReverted: true,
// // keys: [null]
// // keys: []
// }
// ],
// transactions: [{
// includeEvents: true,
// }],
}
]
});

const request = StarknetStream.Request.make({
filter: [filter],
finality: "accepted",
startingCursor: {
orderKey: 500_000n,
orderKey: 80_000n,
},
});

Expand Down
32 changes: 32 additions & 0 deletions examples/starknet-indexer/package.json
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"
}
}
39 changes: 39 additions & 0 deletions examples/starknet-indexer/src/indexer.ts
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 [];
},
});
}
38 changes: 38 additions & 0 deletions examples/starknet-indexer/src/main.ts
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);
11 changes: 11 additions & 0 deletions examples/starknet-indexer/tsconfig.json
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/"]
}
2 changes: 1 addition & 1 deletion packages/indexer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/indexer",
"version": "2.0.0",
"version": "2.0.0-beta.0",
"type": "module",
"source": "./src/index.ts",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/protocol",
"version": "2.0.0",
"version": "2.0.0-beta.0",
"type": "module",
"source": "./src/index.ts",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apibara/starknet",
"version": "2.0.0",
"version": "2.0.0-beta.0",
"type": "module",
"source": "./src/index.ts",
"main": "./src/index.ts",
Expand Down
50 changes: 50 additions & 0 deletions packages/starknet/src/access.ts
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;
}
}
}
2 changes: 2 additions & 0 deletions packages/starknet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export * from "./common";
export * from "./filter";
export * from "./block";

export * from "./access";

export const StarknetStream = new StreamConfig(
FilterFromBytes,
BlockFromBytes,
Expand Down
52 changes: 52 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 611d584

Please sign in to comment.