From efe2fee40e0088e06b6d4bad823432983b0aab4d Mon Sep 17 00:00:00 2001 From: Moon Man Date: Sat, 18 Jan 2025 09:01:50 -0500 Subject: [PATCH] more env functions, create deploy --- hardhat.config.ts | 17 +++++++++++++---- ignition/modules/TenGransToken.ts | 27 +++++++++++++++++++++++++++ lib/common.ts | 22 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 ignition/modules/TenGransToken.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index bcabd79..f9a7013 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -15,24 +15,25 @@ import { env } from "./lib/common"; }); const TEST_MNEMONIC = "test test test test test test test test test test test junk"; +const LOCAL_NODE_URL = "http://127.0.0.1:8545"; const config: HardhatUserConfig = { solidity: "0.8.28", networks: { localhost: { - url: "http://127.0.0.1:8545", + url: LOCAL_NODE_URL, accounts: { mnemonic: process.env.MNEMONIC || TEST_MNEMONIC } }, mainnet: { - url: env("MAINNET_RPC_URL"), + url: env("MAINNET_RPC_URL", LOCAL_NODE_URL), accounts: { mnemonic: env("MNEMONIC", TEST_MNEMONIC) } }, testnet: { - url: env("TESTNET_RPC_URL"), + url: env("TESTNET_RPC_URL", LOCAL_NODE_URL), accounts: { mnemonic: env("MNEMONIC", TEST_MNEMONIC) } @@ -44,7 +45,15 @@ const config: HardhatUserConfig = { sourcify: { enabled: true }, - defaultNetwork: "localhost" + defaultNetwork: "localhost", + ignition: { + strategyConfig: { + create2: { + // To learn more about salts, see the CreateX documentation + salt: env("SALT", "0x0000000000000000000000000000000000000000000000000000000000000000"), + }, + }, + } }; export default config; diff --git a/ignition/modules/TenGransToken.ts b/ignition/modules/TenGransToken.ts new file mode 100644 index 0000000..9e0cf35 --- /dev/null +++ b/ignition/modules/TenGransToken.ts @@ -0,0 +1,27 @@ +import { config as dotenvConfig } from "dotenv"; +import { resolve } from "path"; +import { buildModule } from "@nomicfoundation/hardhat-ignition/modules"; +import { env, envAsBigInt, envAsAddress } from "../../lib/common"; + +[ + `.env.${process.env.APP_ENV}.contracts`, + `.env.${process.env.APP_ENV}.data`, + `.env.${process.env.APP_ENV}` +] + .forEach((dotenvConfigPath) => { + const path = resolve(__dirname, dotenvConfigPath); + dotenvConfig({ path, override: true }) + }); + +export default buildModule("TenGransToken", (m) => { + const tenGransToken = m.contract("TenGransToken", [ + env("TOKEN_NAME"), + env("TOKEN_SYMBOL"), + envAsBigInt("TOKEN_AMOUNT"), + envAsBigInt("CAP_AMOUNT"), + envAsAddress("SIGNER"), + envAsBigInt("NATIVE_CHAIN_ID") + ]); + + return { tenGransToken }; +}); diff --git a/lib/common.ts b/lib/common.ts index deb047b..ef8796d 100644 --- a/lib/common.ts +++ b/lib/common.ts @@ -6,6 +6,28 @@ export const env = (variable: string, defaultValue?: string): string => { else throw new Error(`Environment variable: "${variable}" not set`); }; +export const envAsBigInt = (variable: string, defaultValue?: bigint): bigint => { + const key = process.env[variable]; + if (!key) { + if (defaultValue === undefined) throw new Error(`Environment variable: "${variable}" not set`); + else return defaultValue; + } + + if (/^\d+$/.test(key)) return BigInt(key); + else throw new Error(`Environment variable: ${key} is not a number`); +}; + +export const envAsAddress = (variable: string, defaultValue?: `0x${string}`): `0x${string}` => { + if (process.env[variable]) { + const x = process.env[variable] as string; + // TODO: checksum. + if (/0x[0-9a-f]{40}/i.test(x)) return x as `0x${string}`; + else throw new Error(`Environment variable: ${variable} is not an address`); + } + else if (defaultValue) return defaultValue; + else throw new Error(`Environment variable: "${variable}" not set`); +}; + export class DataRecorder { public readonly filename;