From 7526e66ceaf5c217642f8acdd98693ddaf5e04c7 Mon Sep 17 00:00:00 2001 From: 0xkenj1 Date: Thu, 22 Aug 2024 18:40:56 -0300 Subject: [PATCH] feat: move contract variables to env --- apps/api/.env.example | 9 +++++++-- apps/api/src/common/config/index.ts | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/api/.env.example b/apps/api/.env.example index d11a207..f25f87d 100644 --- a/apps/api/.env.example +++ b/apps/api/.env.example @@ -1,6 +1,11 @@ PORT=3000 # Port to run the API server on -L1_RPC_URLS="" #CSV list of L2 RPC URLs -L2_RPC_URLS="" #CSV list of L1 RPC URLs + +BRIDGE_HUB_ADDRESS="" +SHARED_BRIDGE_ADDRESS="" +STATE_MANAGER_ADDRESSES="" #CSV list of State managers addresses + +L1_RPC_URLS="" #CSV list of L1 RPC URLs +L2_RPC_URLS="" #CSV list of L2 RPC URLs COINGECKO_API_KEY='' # CoinGecko API key COINGECKO_BASE_URL='' # CoinGecko API base URL for the API version you are using diff --git a/apps/api/src/common/config/index.ts b/apps/api/src/common/config/index.ts index a104055..ac603eb 100644 --- a/apps/api/src/common/config/index.ts +++ b/apps/api/src/common/config/index.ts @@ -1,5 +1,5 @@ import dotenv from "dotenv"; -import { Address } from "viem"; +import { Address, isAddress } from "viem"; import { mainnet, zksync } from "viem/chains"; import { z } from "zod"; @@ -9,6 +9,16 @@ dotenv.config(); const logger = Logger.getInstance(); +const addressArraySchema = z + .string() + .transform((str) => str.split(",")) + .refine((addresses) => addresses.every((address) => isAddress(address)), { + message: "Must be a comma-separated list of valid Addresses", + }); +const addressSchema = z.string().refine((address) => isAddress(address), { + message: "Must be a valid Address", +}); + const urlArraySchema = z .string() .transform((str) => str.split(",")) @@ -18,6 +28,9 @@ const urlArraySchema = z const validationSchema = z.object({ PORT: z.coerce.number().positive().default(3000), + BRIDGE_HUB_ADDRESS: addressSchema, + SHARED_BRIDGE_ADDRESS: addressSchema, + STATE_MANAGER_ADDRESSES: addressArraySchema, L1_RPC_URLS: urlArraySchema, L2_RPC_URLS: z .union([z.literal(""), urlArraySchema]) @@ -54,9 +67,9 @@ export const config = { chain: zksync, } : undefined, - bridgeHubAddress: "0x303a465B659cBB0ab36eE643eA362c509EEb5213" as Address, - sharedBridgeAddress: "0xD7f9f54194C633F36CCD5F3da84ad4a1c38cB2cB" as Address, - stateTransitionManagerAddresses: ["0xc2eE6b6af7d616f6e27ce7F4A451Aedc2b0F5f5C"] as Address[], + bridgeHubAddress: envData.BRIDGE_HUB_ADDRESS as Address, + sharedBridgeAddress: envData.SHARED_BRIDGE_ADDRESS as Address, + stateTransitionManagerAddresses: envData.STATE_MANAGER_ADDRESSES as Address[], pricing: { cacheOptions: { ttl: envData.CACHE_TTL,