more env functions, create deploy

This commit is contained in:
Moon Man 2025-01-18 09:01:50 -05:00
parent 45b056acce
commit efe2fee40e
3 changed files with 62 additions and 4 deletions

View File

@ -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;

View File

@ -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 };
});

View File

@ -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;