Skip to content
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

Indexer refactoring: Constants #1396

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 55 additions & 9 deletions indexer/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { padString } from "./utils/hex.ts";
import { hash } from "./deps.ts";
import { Config, hash, NetworkOptions, SinkOptions } from "./deps.ts";

// Get Sink Type or returns "console" if the value is null or undefined
export const SINK_TYPE = (() => {
export const SINK_TYPE: "console" | "mongo" = (() => {
const addr = Deno.env.get("SINK_TYPE") ?? "console";
if (addr !== "console" && addr !== "mongo") {
throw new Error("Invalid SINK_TYPE");
Expand All @@ -11,17 +11,23 @@ export const SINK_TYPE = (() => {
})();

// Get the sink options from the sink type
export const SINK_OPTIONS = SINK_TYPE === "mongo"
export const SINK_OPTIONS: {
connectionString?: string;
database?: string;
collectionNames: string[];
} = SINK_TYPE === "mongo"
? {
connectionString: Deno.env.get("MONGO_CONNECTION_STRING") ??
"mongodb://mongo:mongo@mongo:27017",
database: Deno.env.get("MONGO_DATABASE_NAME") ?? "kakarot-test-db",
collectionNames: ["headers", "transactions", "receipts", "logs"],
}
: {};
: {
collectionNames: [],
};

// Get the starting block or returns 0 if the value is null or undefined
export const STARTING_BLOCK = (() => {
export const STARTING_BLOCK: number = (() => {
const startingBlock = Number(Deno.env.get("STARTING_BLOCK") ?? 0);
return Number.isSafeInteger(startingBlock) && startingBlock >= 0
? startingBlock
Expand All @@ -31,16 +37,17 @@ export const STARTING_BLOCK = (() => {
})();

// Get authentication token from Apibara or returns an empty string if the value is null or undefined
export const AUTH_TOKEN = Deno.env.get("APIBARA_AUTH_TOKEN") ?? "";
export const AUTH_TOKEN: string = Deno.env.get("APIBARA_AUTH_TOKEN") ?? "";

// Get stream URL or returns "http://localhost:7171" if the value is null or undefined
export const STREAM_URL = Deno.env.get("STREAM_URL") ?? "http://localhost:7171";
export const STREAM_URL: string = Deno.env.get("STREAM_URL") ??
"http://localhost:7171";

// Creates string that starts with "0x" and is padded to a total length of 64 chars
export const NULL_HASH = padString("0x", 32);
export const NULL_HASH: string = padString("0x", 32);

// Get the hash selector from the transaction executed
export const TRANSACTION_EXECUTED = hash.getSelectorFromName(
export const TRANSACTION_EXECUTED: string = hash.getSelectorFromName(
"transaction_executed",
);

Expand All @@ -50,3 +57,42 @@ export const KAKAROT_ADDRESS: string = (() => {
if (!kakarotAddress) throw new Error("ENV: KAKAROT_ADDRESS is not set");
return kakarotAddress;
})();

// A default block gas limit in case the call to get_block_gas_limit fails.
export const DEFAULT_BLOCK_GAS_LIMIT: string = (() => {
const defaultBlockGasLimitStr = Deno.env.get("DEFAULT_BLOCK_GAS_LIMIT");
if (!defaultBlockGasLimitStr) {
throw new Error("ENV: DEFAULT_BLOCK_GAS_LIMIT is not set");
}
return defaultBlockGasLimitStr;
})();

// Events containing these keys are not
// ETH logs and should be ignored.
export const IGNORED_KEYS: bigint[] = [
BigInt(hash.getSelectorFromName("transaction_executed")),
BigInt(hash.getSelectorFromName("evm_contract_deployed")),
BigInt(hash.getSelectorFromName("Transfer")),
BigInt(hash.getSelectorFromName("Approval")),
BigInt(hash.getSelectorFromName("OwnershipTransferred")),
];

export const config: Config<NetworkOptions, SinkOptions> = {
streamUrl: STREAM_URL,
authToken: AUTH_TOKEN,
startingBlock: STARTING_BLOCK,
network: "starknet",
finality: "DATA_STATUS_PENDING",
filter: {
header: { weak: false },
// Filters are unions
events: [
{
keys: [TRANSACTION_EXECUTED],
},
],
transactions: [{ includeReverted: true }],
},
sinkType: SINK_TYPE,
sinkOptions: SINK_OPTIONS,
};
33 changes: 1 addition & 32 deletions indexer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import {
} from "./utils/filter.ts";

// Constants
import {
AUTH_TOKEN,
NULL_HASH,
SINK_OPTIONS,
SINK_TYPE,
STARTING_BLOCK,
STREAM_URL,
TRANSACTION_EXECUTED,
} from "./constants.ts";
import { NULL_HASH } from "./constants.ts";

// Types
import {
Expand All @@ -34,12 +26,9 @@ import { Collection, StoreItem } from "./types/storeItem.ts";
// Starknet
import {
BlockHeader,
Config,
EventWithTransaction,
hexToBytes,
JsonRpcTx,
NetworkOptions,
SinkOptions,
TransactionWithReceipt,
} from "./deps.ts";
// Eth
Expand All @@ -50,26 +39,6 @@ import {
ProcessedTransaction,
} from "./types/interfaces.ts";

export const config: Config<NetworkOptions, SinkOptions> = {
streamUrl: STREAM_URL,
authToken: AUTH_TOKEN,
startingBlock: STARTING_BLOCK,
network: "starknet",
finality: "DATA_STATUS_PENDING",
filter: {
header: { weak: false },
// Filters are unions
events: [
{
keys: [TRANSACTION_EXECUTED],
},
],
transactions: [{ includeReverted: true }],
},
sinkType: SINK_TYPE,
sinkOptions: SINK_OPTIONS,
};

export default async function transform({
header,
events,
Expand Down
7 changes: 2 additions & 5 deletions indexer/src/types/header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import {
PrefixedHexString,
} from "../deps.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import {
DEFAULT_BLOCK_GAS_LIMIT,
JsonRpcBlock,
toEthHeader,
} from "./header.ts";
import { JsonRpcBlock, toEthHeader } from "./header.ts";
import { DEFAULT_BLOCK_GAS_LIMIT } from "../constants.ts";
import { padString } from "../utils/hex.ts";
import sinon from "npm:sinon";
import { KAKAROT } from "../provider.ts";
Expand Down
11 changes: 1 addition & 10 deletions indexer/src/types/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ import {
import { KAKAROT } from "../provider.ts";

// Constant
import { NULL_HASH } from "../constants.ts";

// A default block gas limit in case the call to get_block_gas_limit fails.
export const DEFAULT_BLOCK_GAS_LIMIT = (() => {
const defaultBlockGasLimitStr = Deno.env.get("DEFAULT_BLOCK_GAS_LIMIT");
if (!defaultBlockGasLimitStr) {
throw new Error("ENV: DEFAULT_BLOCK_GAS_LIMIT is not set");
}
return defaultBlockGasLimitStr;
})();
import { DEFAULT_BLOCK_GAS_LIMIT, NULL_HASH } from "../constants.ts";

/**
* Converts a Starknet block header to an Ethereum block header in JSON RPC format.
Expand Down
4 changes: 2 additions & 2 deletions indexer/src/types/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { fromJsonRpcLog, IGNORED_KEYS, JsonRpcLog, toEthLog } from "./log.ts";
import { fromJsonRpcLog, JsonRpcLog, toEthLog } from "./log.ts";
import { bigIntToHex, Event, JsonRpcTx } from "../deps.ts";
import { KAKAROT_ADDRESS } from "../constants.ts";
import { IGNORED_KEYS, KAKAROT_ADDRESS } from "../constants.ts";

// Mock for hexToBytes
const mockHexToBytes = (hex: string): Uint8Array => {
Expand Down
12 changes: 1 addition & 11 deletions indexer/src/types/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { padBigint } from "../utils/hex.ts";

// Constants
import { KAKAROT_ADDRESS, NULL_HASH } from "../constants.ts";
import { IGNORED_KEYS, KAKAROT_ADDRESS, NULL_HASH } from "../constants.ts";

// Starknet
import { Event, hash } from "../deps.ts";
Expand All @@ -16,16 +16,6 @@ import {
PrefixedHexString,
} from "../deps.ts";

// Events containing these keys are not
// ETH logs and should be ignored.
export const IGNORED_KEYS = [
BigInt(hash.getSelectorFromName("transaction_executed")),
BigInt(hash.getSelectorFromName("evm_contract_deployed")),
BigInt(hash.getSelectorFromName("Transfer")),
BigInt(hash.getSelectorFromName("Approval")),
BigInt(hash.getSelectorFromName("OwnershipTransferred")),
];

/**
* @param transaction - A Ethereum transaction.
* @param event - A Starknet event.
Expand Down
Loading