Skip to content

Commit

Permalink
Add script
Browse files Browse the repository at this point in the history
  • Loading branch information
wcgcyx committed Nov 1, 2023
1 parent 57cb487 commit 75a3173
Show file tree
Hide file tree
Showing 12 changed files with 1,554 additions and 25 deletions.
29 changes: 29 additions & 0 deletions bootstrap/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Set prior to 1_deployer_funding.js
CHILD_CHAIN_NAME=
CHILD_RPC_URL=
CHILD_CHAIN_ID=
ROOT_CHAIN_NAME=
ROOT_RPC_URL=
ROOT_CHAIN_ID=
IMX_ROOT_ADDR=
WETH_ROOT_ADDR=
## Use private key string or "ledger" if using hardware wallet
ADMIN_EOA_SECRET=
AXELAR_EOA=
AXELAR_FUND=

# Set prior to 2_deployment_validation.js
CHILD_GATEWAY_ADDRESS=
CHILD_GAS_SERVICE_ADDRESS=
MULTISIG_CONTRACT_ADDRESS=
ROOT_GATEWAY_ADDRESS=
ROOT_GAS_SERVICE_ADDRESS=

# Set prior to 5_child_initialisation.js
CHILD_BRIDGE_ADDRESS=
CHILD_ADAPTER_ADDRESS=
WRAPPED_IMX_ADDRESS=
CHILD_TOKEN_TEMPLATE=
ROOT_BRIDGE_ADDRESS=
ROOT_ADAPTER_ADDRESS=
ROOT_TOKEN_TEMPLATE=
83 changes: 83 additions & 0 deletions bootstrap/1_deployer_funding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Deployer funding
'use strict';
require('dotenv').config();
const { ethers } = require("ethers");
const { LedgerSigner } = require('@ethersproject/hardware-wallets')

async function run() {
// Check environment variables
let childRPCURL = requireEnv("CHILD_RPC_URL");
let childChainID = requireEnv("CHILD_CHAIN_ID");
let adminEOASecret = requireEnv("ADMIN_EOA_SECRET");
let axelarEOA = requireEnv("AXELAR_EOA");
let axelarFund = requireEnv("AXELAR_FUND");

// Get admin address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
let adminWallet;
if (adminEOASecret == "ledger") {
adminWallet = new LedgerSigner(childProvider);
} else {
adminWallet = new ethers.Wallet(adminEOASecret, childProvider);
}
let adminAddr = await adminWallet.getAddress();
console.log("Admin address is: ", adminAddr);

// Check duplicates
if (hasDuplicates([adminAddr, axelarEOA, axelarFund])) {
throw("Duplicate address detected!");
}

// Execute
console.log("Fund deployer in...");
for (let i = 10; i >= 0; i--) {
console.log(i)
await delay(1000);
}
let feeData = await adminWallet.getFeeData();
let baseFee = feeData.lastBaseFeePerGas;
let gasPrice = feeData.gasPrice;
let priorityFee = Math.round(gasPrice * 150 / 100);
let maxFee = Math.round(1.13 * baseFee + priorityFee);

let txn = {
to: axelarEOA,
value: ethers.utils.parseEther(axelarFund),
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
}
let resp = await adminWallet.sendTransaction(txn)

let receipt;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(resp.hash)
await delay(1000);
}
console.log(receipt);

// Check target balance
let balance = await childProvider.getBalance(axelarEOA)
console.log("Axelar EOA now has: ", ethers.utils.formatEther(balance));
}

run();

// Helper functions
function requireEnv(envName) {
let val = process.env[envName];
if (val == null || val == "") {
throw(envName + " not set!");
}
if (!envName.includes("SECRET")) {
console.log(envName + ": ", val);
}
return val
}

function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}

function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
62 changes: 62 additions & 0 deletions bootstrap/2_deployment_validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Deployment validation
'use strict';
require('dotenv').config();
const { ethers } = require("ethers");

async function run() {
// Check environment variables
let childRPCURL = requireEnv("CHILD_RPC_URL");
let childChainID =requireEnv("CHILD_CHAIN_ID");
let rootRPCURL = requireEnv("ROOT_RPC_URL");
let rootChainID = requireEnv("ROOT_CHAIN_ID");
let childGatewayAddr = requireEnv("CHILD_GATEWAY_ADDRESS");
let childGasServiceAddr = requireEnv("CHILD_GAS_SERVICE_ADDRESS");
let multisigAddr = requireEnv("MULTISIG_CONTRACT_ADDRESS");
let rootGatewayAddr = requireEnv("ROOT_GATEWAY_ADDRESS");
let rootGasService = requireEnv("ROOT_GAS_SERVICE_ADDRESS");

// Check duplicates
if (hasDuplicates([childGatewayAddr, childGasServiceAddr, multisigAddr])) {
throw("Duplicate address detected!");
}
if (hasDuplicates([rootGatewayAddr, rootGasService])) {
throw("Duplicate address detected!");
}

const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));

// Check child chain.
console.log("Check contracts on child chain...");
await requireNonEmptyCode(childProvider, childGatewayAddr);
await requireNonEmptyCode(childProvider, childGasServiceAddr);
await requireNonEmptyCode(childProvider, multisigAddr);

// Check root chain.
console.log("Check contracts on root chain...");
await requireNonEmptyCode(rootProvider, rootGatewayAddr);
await requireNonEmptyCode(rootProvider, rootGasService);
}

run();

// Helper functions
function requireEnv(envName) {
let val = process.env[envName];
if (val == null || val == "") {
throw(envName + " not set!");
}
console.log(envName + " is set to be: ", val);
return val;
}

async function requireNonEmptyCode(provider, addr) {
if (await provider.getCode(addr) == "0x") {
throw(addr + " has empty code!");
}
console.log(addr + " has code.");
}

function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
168 changes: 168 additions & 0 deletions bootstrap/3_child_deployment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Deploy child contracts
'use strict';
require('dotenv').config();
const { ethers, ContractFactory } = require("ethers");
const fs = require('fs');

async function run() {
// Check environment variables
let rootChainName = requireEnv("ROOT_CHAIN_NAME");
let childRPCURL = requireEnv("CHILD_RPC_URL");
let childChainID = requireEnv("CHILD_CHAIN_ID");
let adminEOASecret = requireEnv("ADMIN_EOA_SECRET");
let childGatewayAddr = requireEnv("CHILD_GATEWAY_ADDRESS");
let childGasServiceAddr = requireEnv("CHILD_GAS_SERVICE_ADDRESS");

// Get admin address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
let adminWallet;
if (adminEOASecret == "ledger") {
adminWallet = new LedgerSigner(childProvider);
} else {
adminWallet = new ethers.Wallet(adminEOASecret, childProvider);
}
let adminAddr = await adminWallet.getAddress();
console.log("Admin address is: ", adminAddr);

// Execute
let childBridgeContractObj = JSON.parse(fs.readFileSync('../out/ChildERC20Bridge.sol/ChildERC20Bridge.json', 'utf8'));
console.log("Deploy child bridge contract in...")
for (let i = 10; i >= 0; i--) {
console.log(i)
await delay(1000);
}
let factory = new ContractFactory(childBridgeContractObj.abi, childBridgeContractObj.bytecode, adminWallet);

let feeData = await adminWallet.getFeeData();
let baseFee = feeData.lastBaseFeePerGas;
let gasPrice = feeData.gasPrice;
let priorityFee = Math.round(gasPrice * 150 / 100);
let maxFee = Math.round(1.13 * baseFee + priorityFee);
let childBridge = await factory.deploy({
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
});

let receipt;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(childBridge.deployTransaction.hash)
await delay(1000);
}
console.log(receipt);
console.log("Deployed to CHILD_BRIDGE_ADDRESS: ", childBridge.address);

let wIMXContractObj = JSON.parse(fs.readFileSync('../out/WIMX.sol/WIMX.json', 'utf8'));
console.log("Deploy WIMX contract in...")
for (let i = 10; i >= 0; i--) {
console.log(i)
await delay(1000);
}
factory = new ContractFactory(wIMXContractObj.abi, wIMXContractObj.bytecode, adminWallet);

feeData = await adminWallet.getFeeData();
baseFee = feeData.lastBaseFeePerGas;
gasPrice = feeData.gasPrice;
priorityFee = Math.round(gasPrice * 150 / 100);
maxFee = Math.round(1.13 * baseFee + priorityFee);
let WIMX = await factory.deploy({
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
});

receipt = null;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(WIMX.deployTransaction.hash)
await delay(1000);
}
console.log(receipt);
console.log("Deployed to WRAPPED_IMX_ADDRESS: ", WIMX.address);

let adapterContractObj = JSON.parse(fs.readFileSync('../out/ChildAxelarBridgeAdaptor.sol/ChildAxelarBridgeAdaptor.json', 'utf8'));
console.log("Deploy child adapter contract in...")
for (let i = 10; i >= 0; i--) {
console.log(i)
await delay(1000);
}
factory = new ContractFactory(adapterContractObj.abi, adapterContractObj.bytecode, adminWallet);

feeData = await adminWallet.getFeeData();
baseFee = feeData.lastBaseFeePerGas;
gasPrice = feeData.gasPrice;
priorityFee = Math.round(gasPrice * 150 / 100);
maxFee = Math.round(1.13 * baseFee + priorityFee);
let childAdapter = await factory.deploy(childGatewayAddr, childBridge.address, {
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
});

receipt = null;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(childAdapter.deployTransaction.hash)
await delay(1000);
}
console.log(receipt);
console.log("Deployed to CHILD_ADAPTER_ADDRESS: ", childAdapter.address);

let templateContractObj = JSON.parse(fs.readFileSync('../out/ChildERC20.sol/ChildERC20.json', 'utf8'));
console.log("Deploy child template contract in...")
for (let i = 10; i >= 0; i--) {
console.log(i)
await delay(1000);
}
factory = new ContractFactory(templateContractObj.abi, templateContractObj.bytecode, adminWallet);

feeData = await adminWallet.getFeeData();
baseFee = feeData.lastBaseFeePerGas;
gasPrice = feeData.gasPrice;
priorityFee = Math.round(gasPrice * 150 / 100);
maxFee = Math.round(1.13 * baseFee + priorityFee);
let childTemplate = await factory.deploy({
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
});

receipt = null;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(childTemplate.deployTransaction.hash)
await delay(1000);
}
console.log(receipt);
console.log("Deployed to CHILD_TOKEN_TEMPLATE: ", childTemplate.address);

feeData = await adminWallet.getFeeData();
baseFee = feeData.lastBaseFeePerGas;
gasPrice = feeData.gasPrice;
priorityFee = Math.round(gasPrice * 150 / 100);
maxFee = Math.round(1.13 * baseFee + priorityFee);
let resp = await childTemplate.initialize("000000000000000000000000000000000000007B", "TEMPLATE", "TPT", 18, {
maxPriorityFeePerGas: priorityFee,
maxFeePerGas: maxFee,
});
receipt = null;
while (receipt == null) {
receipt = await childProvider.getTransactionReceipt(resp.hash)
await delay(1000);
}
console.log(receipt);


fs.writeFileSync("./3.out.tmp", "CHILD_BRIDGE_ADDRESS:" + childBridge.address + "\n" + "WRAPPED_IMX_ADDRESS:" + WIMX.address + "\n" + "CHILD_ADAPTER_ADDRESS:" + childAdapter.address + "\n" + "CHILD_TOKEN_TEMPLATE:" + childTemplate.address);
}

run();

// Helper functions
function requireEnv(envName) {
let val = process.env[envName];
if (val == null || val == "") {
throw(envName + " not set!");
}
if (!envName.includes("SECRET")) {
console.log(envName + ": ", val);
}
return val
}

function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
Loading

0 comments on commit 75a3173

Please sign in to comment.