Skip to content

Commit

Permalink
Merge branch 'main' into eth_client
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Aug 29, 2024
2 parents bdf901b + 1a677d0 commit 1a6e85c
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions indexer/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
import { padString } from "./utils/hex.ts";
import { hash } from "./deps.ts";

// Helper function to get environment variables with validation
const getEnvVariable = <T>(
key: string,
defaultValue: T,
validator?: (value: T) => boolean,
): T => {
const value = Deno.env.get(key) as T ?? defaultValue;
if (validator && !validator(value)) {
throw new Error(`Invalid ${key}`);
// Get Sink Type or returns "console" if the value is null or undefined
export const SINK_TYPE = (() => {
const addr = Deno.env.get("SINK_TYPE") ?? "console";
if (addr !== "console" && addr !== "mongo") {
throw new Error("Invalid SINK_TYPE");
}
return value;
};

// Define the sink type (console or mongo) from environment variable
export const SINK_TYPE = getEnvVariable(
"SINK_TYPE",
"console",
(value) => ["console", "mongo"].includes(value as string),
);
return addr;
})();

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

// Get the starting block or returns 0 if the value is null or undefined
export const STARTING_BLOCK = getEnvVariable(
"STARTING_BLOCK",
0,
(value) => Number.isSafeInteger(Number(value)) && Number(value) >= 0,
);
export const STARTING_BLOCK = (() => {
const startingBlock = Number(Deno.env.get("STARTING_BLOCK") ?? 0);
return Number.isSafeInteger(startingBlock) && startingBlock >= 0
? startingBlock
: (() => {
throw new Error("Invalid STARTING_BLOCK");
})();
})();

// Get authentication token from Apibara or returns an empty string if the value is null or undefined
export const AUTH_TOKEN = getEnvVariable("APIBARA_AUTH_TOKEN", "");
export const AUTH_TOKEN = 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 = getEnvVariable("STREAM_URL", "http://localhost:7171");
export const STREAM_URL = 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_BLOCK_HASH = padString("0x", 32);

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

// Get the Kakarot address from environment variable (0x1)
export const KAKAROT_ADDRESS = getEnvVariable(
"KAKAROT_ADDRESS",
"",
(value) => !!value,
);
// Get the Kakarot Address 0x1
export const KAKAROT_ADDRESS: string = (() => {
const kakarotAddress = Deno.env.get("KAKAROT_ADDRESS");
if (!kakarotAddress) throw new Error("ENV: KAKAROT_ADDRESS is not set");
return kakarotAddress;
})();

0 comments on commit 1a6e85c

Please sign in to comment.