From da16c8c17f9440a4a55b65cd7371bfd9ede59f2f Mon Sep 17 00:00:00 2001 From: npty <78221556+npty@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:26:57 +0700 Subject: [PATCH 1/4] chore: convert hardcoded envs to dynamic values (#343) --- common/cli-utils.js | 21 +++++++++++++++------ cosmwasm/deploy-contract.js | 7 ++----- cosmwasm/submit-proposal.js | 3 ++- stellar/balances.js | 7 ++----- stellar/generate-bindings.js | 7 ++----- stellar/operators.js | 7 ++----- sui/utils/cli-utils.js | 7 ++----- 7 files changed, 27 insertions(+), 32 deletions(-) diff --git a/common/cli-utils.js b/common/cli-utils.js index 88fbc4a9..ffc264cc 100644 --- a/common/cli-utils.js +++ b/common/cli-utils.js @@ -2,15 +2,18 @@ require('dotenv').config(); -const { Option } = require('commander'); +const fs = require('fs'); +const { Option, Command } = require('commander'); + +// A path to the chain configuration files +const CHAIN_CONFIG_PATH = `${__dirname}/../axelar-chains-config/info`; + +// A list of available chain environments which are the names of the files in the CHAIN_CONFIG_PATH +const CHAIN_ENVIRONMENTS = fs.readdirSync(CHAIN_CONFIG_PATH).map((chainName) => chainName.split('.')[0]); const addBaseOptions = (program, options = {}) => { program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'devnet-amplifier', 'devnet-verifiers', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); program.addOption(new Option('--parallel', 'run script parallely wrt chains')); @@ -81,7 +84,13 @@ const addExtendedOptions = (program, options = {}) => { return program; }; +if (require.main === module) { + addBaseOptions(new Command()); +} + module.exports = { + CHAIN_CONFIG_PATH, + CHAIN_ENVIRONMENTS, addBaseOptions, addExtendedOptions, }; diff --git a/cosmwasm/deploy-contract.js b/cosmwasm/deploy-contract.js index 8e68223b..86432027 100644 --- a/cosmwasm/deploy-contract.js +++ b/cosmwasm/deploy-contract.js @@ -3,6 +3,7 @@ require('dotenv').config(); const { isNil } = require('lodash'); +const { CHAIN_ENVIRONMENTS } = require('../common'); const { isNumber, printInfo, loadConfig, saveConfig, prompt } = require('../evm/utils'); const { prepareWallet, @@ -113,11 +114,7 @@ const programHandler = () => { program.name('upload-contract').description('Upload CosmWasm contracts'); program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); diff --git a/cosmwasm/submit-proposal.js b/cosmwasm/submit-proposal.js index ebde299e..d638b470 100644 --- a/cosmwasm/submit-proposal.js +++ b/cosmwasm/submit-proposal.js @@ -22,6 +22,7 @@ const { governanceAddress, } = require('./utils'); const { isNumber, saveConfig, loadConfig, printInfo, prompt } = require('../evm/utils'); +const { CHAIN_ENVIRONMENTS } = require('../common'); const { StoreCodeProposal, StoreAndInstantiateContractProposal, @@ -251,7 +252,7 @@ const programHandler = () => { // TODO: combine deploy-contract and submit-proposal options to remove duplicates program.addOption( new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'devnet-amplifier', 'devnet-verifiers', 'stagenet', 'testnet', 'mainnet']) + .choices(CHAIN_ENVIRONMENTS) .default('devnet-amplifier') .makeOptionMandatory(true) .env('ENV'), diff --git a/stellar/balances.js b/stellar/balances.js index a1a45b92..1622e42a 100644 --- a/stellar/balances.js +++ b/stellar/balances.js @@ -1,6 +1,7 @@ const { Command, Option } = require('commander'); const { getWallet } = require('./utils'); const { loadConfig } = require('../evm/utils'); +const { CHAIN_ENVIRONMENTS } = require('../common'); require('./cli-utils'); async function processCommand(options, _, chain) { @@ -13,11 +14,7 @@ if (require.main === module) { program.name('balances').description('Wallet balance'); program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('-p, --privateKey ', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); diff --git a/stellar/generate-bindings.js b/stellar/generate-bindings.js index 89ff4486..9455be92 100644 --- a/stellar/generate-bindings.js +++ b/stellar/generate-bindings.js @@ -5,6 +5,7 @@ const { execSync } = require('child_process'); const { loadConfig } = require('../evm/utils'); const path = require('path'); const { getNetworkPassphrase } = require('./utils'); +const { CHAIN_ENVIRONMENTS } = require('../common'); require('./cli-utils'); function processCommand(options, _, chain) { @@ -28,11 +29,7 @@ function main() { program.name('Generate TypeScript Bindings for Soroban contract').description('Generates TypeScript bindings for a Soroban contract.'); program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('--wasmPath ', 'path to the WASM file').makeOptionMandatory(true)); program.addOption(new Option('--contractId ', 'contract ID').makeOptionMandatory(true)); diff --git a/stellar/operators.js b/stellar/operators.js index 1e1cb825..063ed291 100644 --- a/stellar/operators.js +++ b/stellar/operators.js @@ -2,6 +2,7 @@ const { Contract, Address, nativeToScVal } = require('@stellar/stellar-sdk'); const { Command, Option } = require('commander'); const { getWallet, prepareTransaction, buildTransaction, sendTransaction, estimateCost } = require('./utils'); const { loadConfig, printInfo, parseArgs, validateParameters } = require('../evm/utils'); +const { CHAIN_ENVIRONMENTS } = require('../common'); require('./cli-utils'); async function processCommand(options, _, chain) { @@ -116,11 +117,7 @@ if (require.main === module) { program.name('operators').description('Operators contract management'); program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('-p, --privateKey ', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); diff --git a/sui/utils/cli-utils.js b/sui/utils/cli-utils.js index 8cf95622..7d55cdd2 100644 --- a/sui/utils/cli-utils.js +++ b/sui/utils/cli-utils.js @@ -4,14 +4,11 @@ require('dotenv').config(); const { Option, InvalidArgumentError } = require('commander'); const { getUnitAmount } = require('./amount-utils'); +const { CHAIN_ENVIRONMENTS } = require('../../common'); const addBaseOptions = (program, options = {}) => { program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'devnet-amplifier', 'devnet-verifiers', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), + new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), ); program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); program.addOption(new Option('--gasOptions ', 'gas options cli override')); From ddd12c1d9e732d1017bf5acba4e7e3fbae3bdaaf Mon Sep 17 00:00:00 2001 From: npty Date: Thu, 22 Aug 2024 17:37:54 +0700 Subject: [PATCH 2/4] chore: reuse addEnvOption --- common/cli-utils.js | 14 ++++++++++++-- cosmwasm/deploy-contract.js | 6 ++---- cosmwasm/submit-proposal.js | 10 ++-------- stellar/balances.js | 6 ++---- stellar/deploy-contract.js | 9 ++------- stellar/generate-bindings.js | 6 ++---- stellar/operators.js | 7 ++----- sui/utils/cli-utils.js | 6 ++---- 8 files changed, 26 insertions(+), 38 deletions(-) diff --git a/common/cli-utils.js b/common/cli-utils.js index 32d00a3c..faca1d31 100644 --- a/common/cli-utils.js +++ b/common/cli-utils.js @@ -11,10 +11,19 @@ const CHAIN_CONFIG_PATH = `${__dirname}/../axelar-chains-config/info`; // A list of available chain environments which are the names of the files in the CHAIN_CONFIG_PATH const CHAIN_ENVIRONMENTS = fs.readdirSync(CHAIN_CONFIG_PATH).map((chainName) => chainName.split('.')[0]); -const addBaseOptions = (program, options = {}) => { +const addEnvOption = (program, defaultValue) => { program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), + new Option('-e, --env ', 'environment') + .choices(CHAIN_ENVIRONMENTS) + .default(defaultValue || 'testnet') + .makeOptionMandatory(true) + .env('ENV'), ); +}; + +const addBaseOptions = (program, options = {}) => { + addEnvOption(program); + program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); if (!options.ignoreParallel) { @@ -102,6 +111,7 @@ if (require.main === module) { module.exports = { CHAIN_CONFIG_PATH, CHAIN_ENVIRONMENTS, + addEnvOption, addBaseOptions, addExtendedOptions, }; diff --git a/cosmwasm/deploy-contract.js b/cosmwasm/deploy-contract.js index 0396852c..e0236c47 100644 --- a/cosmwasm/deploy-contract.js +++ b/cosmwasm/deploy-contract.js @@ -3,7 +3,7 @@ require('dotenv').config(); const { isNil } = require('lodash'); -const { CHAIN_ENVIRONMENTS } = require('../common'); +const { addEnvOption } = require('../common'); const { isNumber, printInfo, loadConfig, saveConfig, prompt } = require('../evm/utils'); const { prepareWallet, @@ -115,9 +115,7 @@ const programHandler = () => { addCommonAmplifierOptions(program); - program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), - ); + addEnvOption(program); program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); program.addOption(new Option('-c, --contractName ', 'contract name').makeOptionMandatory(true)); diff --git a/cosmwasm/submit-proposal.js b/cosmwasm/submit-proposal.js index 1ab2c111..698cfd8e 100644 --- a/cosmwasm/submit-proposal.js +++ b/cosmwasm/submit-proposal.js @@ -22,7 +22,7 @@ const { governanceAddress, } = require('./utils'); const { isNumber, saveConfig, loadConfig, printInfo, prompt } = require('../evm/utils'); -const { CHAIN_ENVIRONMENTS } = require('../common'); +const { addEnvOption } = require('../common'); const { StoreCodeProposal, StoreAndInstantiateContractProposal, @@ -253,13 +253,7 @@ const programHandler = () => { addCommonAmplifierOptions(program, { predictOnly: true }); // TODO: combine deploy-contract and submit-proposal options to remove duplicates - program.addOption( - new Option('-e, --env ', 'environment') - .choices(CHAIN_ENVIRONMENTS) - .default('devnet-amplifier') - .makeOptionMandatory(true) - .env('ENV'), - ); + addEnvOption(program, 'devnet-amplifier'); program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); program.addOption(new Option('-c, --contractName ', 'contract name').makeOptionMandatory(true)); diff --git a/stellar/balances.js b/stellar/balances.js index 1622e42a..601a55b6 100644 --- a/stellar/balances.js +++ b/stellar/balances.js @@ -1,7 +1,7 @@ const { Command, Option } = require('commander'); const { getWallet } = require('./utils'); const { loadConfig } = require('../evm/utils'); -const { CHAIN_ENVIRONMENTS } = require('../common'); +const { addEnvOption } = require('../common'); require('./cli-utils'); async function processCommand(options, _, chain) { @@ -13,9 +13,7 @@ if (require.main === module) { program.name('balances').description('Wallet balance'); - program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), - ); + addEnvOption(program); program.addOption(new Option('-p, --privateKey ', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); diff --git a/stellar/deploy-contract.js b/stellar/deploy-contract.js index 033bd774..9a13d4f1 100644 --- a/stellar/deploy-contract.js +++ b/stellar/deploy-contract.js @@ -5,6 +5,7 @@ const { Command, Option } = require('commander'); const { execSync } = require('child_process'); const { loadConfig, printInfo, saveConfig } = require('../evm/utils'); const { getNetworkPassphrase, getWallet, prepareTransaction, sendTransaction, buildTransaction, estimateCost } = require('./utils'); +const { addEnvOption } = require('../common'); require('./cli-utils'); function getInitializeArgs(chain, contractName, wallet, options) { @@ -108,13 +109,7 @@ function main() { const program = new Command(); program.name('deploy-contract').description('Deploy Axelar Soroban contracts on Stellar'); - program.addOption( - new Option('-e, --env ', 'environment') - .choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet']) - .default('testnet') - .makeOptionMandatory(true) - .env('ENV'), - ); + addEnvOption(program); program.addOption(new Option('-p, --privateKey ', 'private key').env('PRIVATE_KEY')); program.addOption(new Option('-v, --verbose', 'verbose output').default(false)); program.addOption(new Option('--initialize', 'initialize the contract')); diff --git a/stellar/generate-bindings.js b/stellar/generate-bindings.js index 9455be92..608fa77d 100644 --- a/stellar/generate-bindings.js +++ b/stellar/generate-bindings.js @@ -5,7 +5,7 @@ const { execSync } = require('child_process'); const { loadConfig } = require('../evm/utils'); const path = require('path'); const { getNetworkPassphrase } = require('./utils'); -const { CHAIN_ENVIRONMENTS } = require('../common'); +const { addEnvOption } = require('../common'); require('./cli-utils'); function processCommand(options, _, chain) { @@ -28,9 +28,7 @@ function main() { const program = new Command(); program.name('Generate TypeScript Bindings for Soroban contract').description('Generates TypeScript bindings for a Soroban contract.'); - program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), - ); + addEnvOption(program); program.addOption(new Option('--wasmPath ', 'path to the WASM file').makeOptionMandatory(true)); program.addOption(new Option('--contractId ', 'contract ID').makeOptionMandatory(true)); program.addOption( diff --git a/stellar/operators.js b/stellar/operators.js index 063ed291..c99eda1c 100644 --- a/stellar/operators.js +++ b/stellar/operators.js @@ -2,7 +2,7 @@ const { Contract, Address, nativeToScVal } = require('@stellar/stellar-sdk'); const { Command, Option } = require('commander'); const { getWallet, prepareTransaction, buildTransaction, sendTransaction, estimateCost } = require('./utils'); const { loadConfig, printInfo, parseArgs, validateParameters } = require('../evm/utils'); -const { CHAIN_ENVIRONMENTS } = require('../common'); +const { addEnvOption } = require('../common'); require('./cli-utils'); async function processCommand(options, _, chain) { @@ -116,10 +116,7 @@ if (require.main === module) { program.name('operators').description('Operators contract management'); - program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), - ); - + addEnvOption(program); program.addOption(new Option('-p, --privateKey ', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); program.addOption(new Option('-v, --verbose', 'verbose output').default(false)); program.addOption( diff --git a/sui/utils/cli-utils.js b/sui/utils/cli-utils.js index 7d55cdd2..9c8aa924 100644 --- a/sui/utils/cli-utils.js +++ b/sui/utils/cli-utils.js @@ -4,12 +4,10 @@ require('dotenv').config(); const { Option, InvalidArgumentError } = require('commander'); const { getUnitAmount } = require('./amount-utils'); -const { CHAIN_ENVIRONMENTS } = require('../../common'); +const { addEnvOption } = require('../../common'); const addBaseOptions = (program, options = {}) => { - program.addOption( - new Option('-e, --env ', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'), - ); + addEnvOption(program); program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); program.addOption(new Option('--gasOptions ', 'gas options cli override')); From 22f14ad1af807dce1c9df556f5372a97e9afa421 Mon Sep 17 00:00:00 2001 From: npty Date: Thu, 22 Aug 2024 19:30:02 +0700 Subject: [PATCH 3/4] chore: remove testing command --- common/cli-utils.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/common/cli-utils.js b/common/cli-utils.js index faca1d31..189aee26 100644 --- a/common/cli-utils.js +++ b/common/cli-utils.js @@ -3,7 +3,7 @@ require('dotenv').config(); const fs = require('fs'); -const { Option, Command } = require('commander'); +const { Option } = require('commander'); // A path to the chain configuration files const CHAIN_CONFIG_PATH = `${__dirname}/../axelar-chains-config/info`; @@ -104,13 +104,7 @@ const addExtendedOptions = (program, options = {}) => { return program; }; -if (require.main === module) { - addBaseOptions(new Command()); -} - module.exports = { - CHAIN_CONFIG_PATH, - CHAIN_ENVIRONMENTS, addEnvOption, addBaseOptions, addExtendedOptions, From 541caf7e909b7ad14ef3ee93245fa6d2acdaaa4b Mon Sep 17 00:00:00 2001 From: npty Date: Thu, 22 Aug 2024 19:32:14 +0700 Subject: [PATCH 4/4] chore: move addEnvOption in addCommonAmplifierOptions --- cosmwasm/cli-utils.js | 4 ++-- cosmwasm/deploy-contract.js | 1 - cosmwasm/submit-proposal.js | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cosmwasm/cli-utils.js b/cosmwasm/cli-utils.js index e1ef6a40..684f3382 100644 --- a/cosmwasm/cli-utils.js +++ b/cosmwasm/cli-utils.js @@ -2,7 +2,7 @@ require('dotenv').config(); -const { addExtendedOptions } = require('../common'); +const { addExtendedOptions, addEnvOption } = require('../common'); const { governanceAddress } = require('./utils'); const { Option } = require('commander'); @@ -20,8 +20,8 @@ const addCommonAmplifierOptions = (program, options = {}) => { ...options, }; + addEnvOption(program); addExtendedOptions(program, ops); - program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); program.addOption(new Option('--aarch64', 'aarch64').env('AARCH64').default(false)); diff --git a/cosmwasm/deploy-contract.js b/cosmwasm/deploy-contract.js index e0236c47..da49d2f8 100644 --- a/cosmwasm/deploy-contract.js +++ b/cosmwasm/deploy-contract.js @@ -115,7 +115,6 @@ const programHandler = () => { addCommonAmplifierOptions(program); - addEnvOption(program); program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); program.addOption(new Option('-c, --contractName ', 'contract name').makeOptionMandatory(true)); diff --git a/cosmwasm/submit-proposal.js b/cosmwasm/submit-proposal.js index 698cfd8e..e56b941d 100644 --- a/cosmwasm/submit-proposal.js +++ b/cosmwasm/submit-proposal.js @@ -253,7 +253,6 @@ const programHandler = () => { addCommonAmplifierOptions(program, { predictOnly: true }); // TODO: combine deploy-contract and submit-proposal options to remove duplicates - addEnvOption(program, 'devnet-amplifier'); program.addOption(new Option('-m, --mnemonic ', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC')); program.addOption(new Option('-a, --artifactPath ', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH')); program.addOption(new Option('-c, --contractName ', 'contract name').makeOptionMandatory(true));