Skip to content

Commit

Permalink
chore: reuse addEnvOptions (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
npty authored Aug 22, 2024
1 parent ce6fe54 commit 54fcdc4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 41 deletions.
19 changes: 16 additions & 3 deletions common/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

require('dotenv').config();

const fs = require('fs');
const { Option } = require('commander');

const addBaseOptions = (program, options = {}) => {
// 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 addEnvOption = (program, defaultValue) => {
program.addOption(
new Option('-e, --env <env>', 'environment')
.choices(['local', 'devnet', 'devnet-amplifier', 'devnet-verifiers', 'stagenet', 'testnet', 'mainnet'])
.default('testnet')
.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) {
Expand Down Expand Up @@ -93,6 +105,7 @@ const addExtendedOptions = (program, options = {}) => {
};

module.exports = {
addEnvOption,
addBaseOptions,
addExtendedOptions,
};
4 changes: 2 additions & 2 deletions cosmwasm/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('dotenv').config();

const { addExtendedOptions } = require('../common');
const { addExtendedOptions, addEnvOption } = require('../common');
const { governanceAddress } = require('./utils');

const { Option } = require('commander');
Expand All @@ -20,8 +20,8 @@ const addCommonAmplifierOptions = (program, options = {}) => {
...options,
};

addEnvOption(program);
addExtendedOptions(program, ops);

program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC'));
program.addOption(new Option('-a, --artifactPath <artifactPath>', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH'));
program.addOption(new Option('--aarch64', 'aarch64').env('AARCH64').default(false));
Expand Down
5 changes: 5 additions & 0 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require('dotenv').config();
const { isNil } = require('lodash');

const { addEnvOption } = require('../common');
const { isNumber, printInfo, loadConfig, saveConfig, prompt } = require('../evm/utils');
const {
prepareWallet,
Expand Down Expand Up @@ -114,6 +115,10 @@ const programHandler = () => {

addCommonAmplifierOptions(program);

program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC'));
program.addOption(new Option('-a, --artifactPath <artifactPath>', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH'));
program.addOption(new Option('-c, --contractName <contractName>', 'contract name').makeOptionMandatory(true));
program.addOption(new Option('-n, --chainNames <chainNames>', 'chain names').default('none'));
program.addOption(new Option('-r, --reuseCodeId', 'reuse code Id'));
program.addOption(
new Option(
Expand Down
18 changes: 18 additions & 0 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
governanceAddress,
} = require('./utils');
const { isNumber, saveConfig, loadConfig, printInfo, prompt } = require('../evm/utils');
const { addEnvOption } = require('../common');
const {
StoreCodeProposal,
StoreAndInstantiateContractProposal,
Expand Down Expand Up @@ -251,6 +252,23 @@ const programHandler = () => {

addCommonAmplifierOptions(program, { predictOnly: true });

// TODO: combine deploy-contract and submit-proposal options to remove duplicates
program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC'));
program.addOption(new Option('-a, --artifactPath <artifactPath>', 'artifact path').makeOptionMandatory(true).env('ARTIFACT_PATH'));
program.addOption(new Option('-c, --contractName <contractName>', 'contract name').makeOptionMandatory(true));
program.addOption(new Option('-n, --chainNames <chainNames>', 'chain names').default('none'));

program.addOption(new Option('-s, --salt <salt>', 'salt for instantiate2. defaults to contract name').env('SALT'));
program.addOption(
new Option('--admin <address>', 'when instantiating contract, set an admin address. Defaults to governance module account').default(
governanceAddress,
),
);
program.addOption(new Option('--instantiate2', 'use instantiate2 for constant address deployment'));
program.addOption(new Option('-l, --label <label>', 'contract instance label'));
program.addOption(new Option('--aarch64', 'aarch64').env('AARCH64').default(false));
program.addOption(new Option('-y, --yes', 'skip prompt confirmation').env('YES'));

program.addOption(new Option('-t, --title <title>', 'title of proposal').makeOptionMandatory(true));
program.addOption(new Option('-d, --description <description>', 'description of proposal').makeOptionMandatory(true));
program.addOption(new Option('--deposit <deposit>', 'the proposal deposit').makeOptionMandatory(true));
Expand Down
9 changes: 2 additions & 7 deletions stellar/balances.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Command, Option } = require('commander');
const { getWallet } = require('./utils');
const { loadConfig } = require('../evm/utils');
const { addEnvOption } = require('../common');
require('./cli-utils');

async function processCommand(options, _, chain) {
Expand All @@ -12,13 +13,7 @@ if (require.main === module) {

program.name('balances').description('Wallet balance');

program.addOption(
new Option('-e, --env <env>', 'environment')
.choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet'])
.default('testnet')
.makeOptionMandatory(true)
.env('ENV'),
);
addEnvOption(program);

program.addOption(new Option('-p, --privateKey <privateKey>', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY'));

Expand Down
9 changes: 2 additions & 7 deletions stellar/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 <env>', 'environment')
.choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet'])
.default('testnet')
.makeOptionMandatory(true)
.env('ENV'),
);
addEnvOption(program);
program.addOption(new Option('-p, --privateKey <privateKey>', 'private key').env('PRIVATE_KEY'));
program.addOption(new Option('-v, --verbose', 'verbose output').default(false));
program.addOption(new Option('--initialize', 'initialize the contract'));
Expand Down
9 changes: 2 additions & 7 deletions stellar/generate-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { execSync } = require('child_process');
const { loadConfig } = require('../evm/utils');
const path = require('path');
const { getNetworkPassphrase } = require('./utils');
const { addEnvOption } = require('../common');
require('./cli-utils');

function processCommand(options, _, chain) {
Expand All @@ -27,13 +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 <env>', 'environment')
.choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet'])
.default('testnet')
.makeOptionMandatory(true)
.env('ENV'),
);
addEnvOption(program);
program.addOption(new Option('--wasmPath <wasmPath>', 'path to the WASM file').makeOptionMandatory(true));
program.addOption(new Option('--contractId <contractId>', 'contract ID').makeOptionMandatory(true));
program.addOption(
Expand Down
10 changes: 2 additions & 8 deletions stellar/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { addEnvOption } = require('../common');
require('./cli-utils');

async function processCommand(options, _, chain) {
Expand Down Expand Up @@ -115,14 +116,7 @@ if (require.main === module) {

program.name('operators').description('Operators contract management');

program.addOption(
new Option('-e, --env <env>', 'environment')
.choices(['local', 'devnet', 'stagenet', 'testnet', 'mainnet'])
.default('testnet')
.makeOptionMandatory(true)
.env('ENV'),
);

addEnvOption(program);
program.addOption(new Option('-p, --privateKey <privateKey>', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY'));
program.addOption(new Option('-v, --verbose', 'verbose output').default(false));
program.addOption(
Expand Down
9 changes: 2 additions & 7 deletions sui/utils/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ require('dotenv').config();

const { Option, InvalidArgumentError } = require('commander');
const { getUnitAmount } = require('./amount-utils');
const { addEnvOption } = require('../../common');

const addBaseOptions = (program, options = {}) => {
program.addOption(
new Option('-e, --env <env>', 'environment')
.choices(['local', 'devnet', 'devnet-amplifier', 'devnet-verifiers', 'stagenet', 'testnet', 'mainnet'])
.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 <gasOptions>', 'gas options cli override'));

Expand Down

0 comments on commit 54fcdc4

Please sign in to comment.