Skip to content

Commit

Permalink
Refactor env variable handling and improve code structure (#1348)
Browse files Browse the repository at this point in the history
* Update constants.ts

Refactor environment variable handling and code structure

- Introduce generic getEnvVariable function for consistent env var retrieval
- Simplify logic and remove unnecessary IIFEs
- Improve code readability and maintainability
- Maintain existing functionality and export names for compatibility

* formated constants.ts

* improved the readability of constants.ts

* Update indexer/src/constants.ts

* Update indexer/src/constants.ts

* Update indexer/src/constants.ts

* Update indexer/src/constants.ts

* Update indexer/src/constants.ts

---------

Co-authored-by: Thomas Coratger <[email protected]>
  • Loading branch information
nnlgsakib and tcoratger authored Aug 29, 2024
1 parent f97902f commit edcd1ab
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions indexer/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
import { padString } from "./utils/hex.ts";
import { hash } from "./deps.ts";

// 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");
// 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}`);
}
return addr;
})();
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),
);

// Get the sink options from the sink type
// Set sink options based on the sink type
export const SINK_OPTIONS = SINK_TYPE === "mongo"
? {
connectionString: Deno.env.get("MONGO_CONNECTION_STRING") ??
connectionString: getEnvVariable(
"MONGO_CONNECTION_STRING",
"mongodb://mongo:mongo@mongo:27017",
database: Deno.env.get("MONGO_DATABASE_NAME") ?? "kakarot-test-db",
),
database: getEnvVariable("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 = (() => {
const startingBlock = Number(Deno.env.get("STARTING_BLOCK") ?? 0);
return Number.isSafeInteger(startingBlock) && startingBlock >= 0
? startingBlock
: (() => {
throw new Error("Invalid STARTING_BLOCK");
})();
})();
export const STARTING_BLOCK = getEnvVariable(
"STARTING_BLOCK",
0,
(value) => Number.isSafeInteger(Number(value)) && Number(value) >= 0,
);

// 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 = getEnvVariable("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 = getEnvVariable("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 hash selector from the transaction executed
// Get the selector for "transaction_executed" event
export const TRANSACTION_EXECUTED = hash.getSelectorFromName(
"transaction_executed",
);

// 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;
})();
// Get the Kakarot address from environment variable (0x1)
export const KAKAROT_ADDRESS = getEnvVariable(
"KAKAROT_ADDRESS",
"",
(value) => !!value,
);

0 comments on commit edcd1ab

Please sign in to comment.