Skip to content

Commit

Permalink
Add environment variables validation
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Sep 6, 2024
1 parent edd98df commit 87844af
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 113 deletions.
8 changes: 4 additions & 4 deletions .env.test.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
DEPLOYER_PRIVATE_KEY=
ETHERSCAN_API_KEY=
DEPLOYER_PRIVATE_KEY=1234567890ABCDEF1234567890abcde1234567890ABCDEF1234567890abcdeff
ETHERSCAN_API_KEY=1234567890ABCDEF1234567890abcdef12
ROOT_CHAIN_RPC=
ROOT_GAS_PRICE=
ROOT_GAS_PRICE=0
CHILD_CHAIN_RPC=
CHILD_GAS_PRICE=
CHILD_GAS_PRICE=0
STAKE_MANAGER=
MATIC_TOKEN=
MANAGER=
Expand Down
49 changes: 0 additions & 49 deletions environment.ts

This file was deleted.

77 changes: 42 additions & 35 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as dotenv from "dotenv";
import {
HardhatNetworkMiningConfig,
HardhatRuntimeEnvironment,
Expand All @@ -14,20 +15,16 @@ import "hardhat-contract-sizer";
import "hardhat-gas-reporter";
import "solidity-coverage";
import { deployDirect, deployProxy, verify } from "./scripts/tasks";
import {
DEPLOYER_PRIVATE_KEY,
ETHERSCAN_API_KEY,
ROOT_CHAIN_RPC,
ROOT_GAS_PRICE,
DEFENDER_TEAM_API_KEY,
DEFENDER_TEAM_API_SECRET_KEY,
CHILD_CHAIN_RPC,
CHILD_GAS_PRICE,
FX_ROOT,
FX_CHILD,
CHECKPOINT_MANAGER,
REPORT_GAS,
} from "./environment";
import { extractEnvironmentVariables } from "./utils/environment";

const envSuffix = process.env.NODE_ENV === "main" ? "" : ".test";
const exampleSuffix = process.env.CI ? ".example" : "";

dotenv.config({
path: `.env${envSuffix}${exampleSuffix}`,
});

const envVars = extractEnvironmentVariables();

task("verifyMaticX", "MaticX contracts verification").setAction(
async (args, hre: HardhatRuntimeEnvironment) => {
Expand All @@ -37,20 +34,24 @@ task("verifyMaticX", "MaticX contracts verification").setAction(

task("deployFxStateChildTunnel", "Deploy FxStateChildTunnel").setAction(
async (args, hre: HardhatRuntimeEnvironment) => {
if (!isChildNetwork(hre.network.name)) return;
await deployDirect(hre, "FxStateChildTunnel", FX_CHILD);
if (!isChildNetwork(hre.network.name)) {
return;
}
await deployDirect(hre, "FxStateChildTunnel", envVars.FX_CHILD);
}
);

task("deployFxStateRootTunnel", "Deploy FxStateRootTunnel")
.addPositionalParam("maticX")
.setAction(async ({ maticX }, hre: HardhatRuntimeEnvironment) => {
if (!isRootNetwork(hre.network.name)) return;
if (!isRootNetwork(hre.network.name)) {
return;
}
await deployDirect(
hre,
"FxStateRootTunnel",
CHECKPOINT_MANAGER,
FX_ROOT,
envVars.CHECKPOINT_MANAGER,
envVars.FX_ROOT,
maticX
);
});
Expand All @@ -59,14 +60,18 @@ task("deployRateProvider", "Deploy RateProvider")
.addPositionalParam("fxStateChildTunnel")
.setAction(
async ({ fxStateChildTunnel }, hre: HardhatRuntimeEnvironment) => {
if (!isChildNetwork(hre.network.name)) return;
if (!isChildNetwork(hre.network.name)) {
return;
}
await deployDirect(hre, "RateProvider", fxStateChildTunnel);
}
);

task("deployMaticXImpl", "Deploy MaticX Implementation only").setAction(
async (args, hre: HardhatRuntimeEnvironment) => {
if (!isRootNetwork(hre.network.name)) return;
if (!isRootNetwork(hre.network.name)) {
return;
}
await deployDirect(hre, "MaticX");
}
);
Expand Down Expand Up @@ -114,7 +119,9 @@ task("deployChildPoolProxy", "Deploy ChildPool Proxy only")

task("deployChildPoolImpl", "Deploy ChildPool Implementation only").setAction(
async (args, hre: HardhatRuntimeEnvironment) => {
if (!isChildNetwork(hre.network.name)) return;
if (!isChildNetwork(hre.network.name)) {
return;
}
await deployDirect(hre, "ChildPool");
}
);
Expand Down Expand Up @@ -170,19 +177,19 @@ const config: HardhatUserConfig = {
mining,
},
testnet: {
url: ROOT_CHAIN_RPC,
accounts: [DEPLOYER_PRIVATE_KEY],
gasPrice: Number(ROOT_GAS_PRICE),
url: envVars.ROOT_CHAIN_RPC,
accounts: [envVars.DEPLOYER_PRIVATE_KEY],
gasPrice: envVars.ROOT_GAS_PRICE,
},
mainnet: {
url: ROOT_CHAIN_RPC,
accounts: [DEPLOYER_PRIVATE_KEY],
gasPrice: Number(ROOT_GAS_PRICE),
url: envVars.ROOT_CHAIN_RPC,
accounts: [envVars.DEPLOYER_PRIVATE_KEY],
gasPrice: envVars.ROOT_GAS_PRICE,
},
matic: {
url: CHILD_CHAIN_RPC,
accounts: [DEPLOYER_PRIVATE_KEY],
gasPrice: Number(CHILD_GAS_PRICE),
url: envVars.CHILD_CHAIN_RPC,
accounts: [envVars.DEPLOYER_PRIVATE_KEY],
gasPrice: envVars.CHILD_GAS_PRICE,
},
},
typechain: {
Expand All @@ -194,11 +201,11 @@ const config: HardhatUserConfig = {
timeout: "1h",
},
etherscan: {
apiKey: ETHERSCAN_API_KEY,
apiKey: envVars.ETHERSCAN_API_KEY,
},
defender: {
apiKey: DEFENDER_TEAM_API_KEY,
apiSecret: DEFENDER_TEAM_API_SECRET_KEY,
apiKey: envVars.DEFENDER_TEAM_API_KEY,
apiSecret: envVars.DEFENDER_TEAM_API_SECRET_KEY,
},
contractSizer: {
alphaSort: false,
Expand All @@ -216,7 +223,7 @@ const config: HardhatUserConfig = {
},
gasReporter: {
currency: "USD",
enabled: REPORT_GAS,
enabled: envVars.REPORT_GAS,
excludeContracts: [
"@openzeppelin/",
"interfaces/",
Expand Down
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"hardhat-contract-sizer": "^2.5.1",
"hardhat-gas-reporter": "^2.2.1",
"husky": "^9.1.5",
"joi": "^17.13.3",
"prettier": "^3.3.3",
"prettier-plugin-solidity": "^1.4.1",
"rimraf": "^6.0.1",
Expand Down
39 changes: 17 additions & 22 deletions scripts/deployers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import * as fs from "fs";
import * as fs from "node:fs";
import path from "node:path";
import { Contract, Wallet } from "ethers";
import { ethers, network, upgrades } from "hardhat";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

import { predictContractAddress } from "./utils";
import { ValidatorRegistry, MaticX } from "../typechain";
import {
STAKE_MANAGER,
MATIC_TOKEN,
MANAGER,
INSTANT_POOL_OWNER,
TREASURY,
} from "../environment";
import path from "path";
import { extractEnvironmentVariables } from "../utils/environment";

type DeploymentData = {
Network: string;
Expand All @@ -30,6 +23,8 @@ type ContractNames =

type DeploymentOrder = Record<ContractNames, number>;

const envVars = extractEnvironmentVariables();

const deploymentOrder: DeploymentOrder = {
ProxyAdmin: 0,
ValidatorRegistryImplementation: 1,
Expand Down Expand Up @@ -127,22 +122,22 @@ export class MaticXDeployer
private deployValidatorRegistry = async () => {
return this.rootDeployer.deployProxy<ValidatorRegistry>(
"ValidatorRegistry",
STAKE_MANAGER,
MATIC_TOKEN,
envVars.STAKE_MANAGER,
envVars.MATIC_TOKEN,
this.data.MaticX,
MANAGER
envVars.MANAGER
);
};

private deployMaticX = async () => {
return this.rootDeployer.deployProxy<MaticX>(
"MaticX",
this.data.ValidatorRegistry,
STAKE_MANAGER,
MATIC_TOKEN,
MANAGER,
INSTANT_POOL_OWNER,
TREASURY
envVars.STAKE_MANAGER,
envVars.MATIC_TOKEN,
envVars.MANAGER,
envVars.INSTANT_POOL_OWNER,
envVars.TREASURY
);
};

Expand All @@ -157,10 +152,10 @@ export class MaticXDeployer
network: chainId,
multisig_upgrader: { address: "0x", owners: [] },
root_deployer: this.rootDeployer.signer.address,
manager: MANAGER,
treasury: TREASURY,
matic_erc20_address: MATIC_TOKEN,
matic_stake_manager_proxy: STAKE_MANAGER,
manager: envVars.MANAGER,
treasury: envVars.TREASURY,
matic_erc20_address: envVars.MATIC_TOKEN,
matic_stake_manager_proxy: envVars.STAKE_MANAGER,
proxy_admin: this.data.ProxyAdmin,
maticX_proxy: this.data.MaticX,
maticX_impl: this.data.MaticXImplementation,
Expand Down
1 change: 0 additions & 1 deletion scripts/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";

import * as GOERLI_DEPLOYMENT_DETAILS from "../testnet-deployment-info.json";

const verifyContract = async (
Expand Down
4 changes: 2 additions & 2 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "node:fs";
import path from "node:path";
import { Contract, ethers } from "ethers";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { publicKeyCreate } from "secp256k1";
import { DeployDetails } from "./types";
import fs from "fs";
import path from "path";

export const getPublicKey = (privateKey: string): Uint8Array => {
const privKeyBytes = ethers.utils.arrayify(privateKey);
Expand Down
Loading

0 comments on commit 87844af

Please sign in to comment.