-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Refactor env variable handling and improve code structure" (#…
…1357) Revert "Refactor env variable handling and improve code structure (#1348)" This reverts commit edcd1ab.
- Loading branch information
Showing
1 changed file
with
27 additions
and
37 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 |
---|---|---|
@@ -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; | ||
})(); |