Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amplifier): refactor amplifier script options #341

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions common/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <env>', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'),
new Option('-e, --env <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'));
program.addOption(new Option('--parallel', 'run script parallely wrt chains'));
program.addOption(new Option('--saveChainSeparately', 'save chain info separately'));
Expand Down Expand Up @@ -45,7 +54,6 @@ const addBaseOptions = (program, options = {}) => {
};

module.exports = {
CHAIN_CONFIG_PATH,
CHAIN_ENVIRONMENTS,
addEnvOption,
addBaseOptions,
};
33 changes: 33 additions & 0 deletions cosmwasm/cli-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

require('dotenv').config();

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

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

const addAmplifierOptions = (program) => {
addEnvOption(program);

program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC'));
program.addOption(new Option('-a, --artifactPath <artifactPath>', 'artifact path').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').env('CHAINS'));

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('--fetchCodeId', 'fetch code id from the chain by comparing to the uploaded code hash'));
program.addOption(new Option('-l, --label <label>', 'contract instance label'));

program.addOption(new Option('-y, --yes', 'skip prompt confirmation').env('YES'));
};

module.exports = {
addAmplifierOptions,
};
24 changes: 3 additions & 21 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require('dotenv').config();
const { isNil } = require('lodash');

const { CHAIN_ENVIRONMENTS } = require('../common');
const { isNumber, printInfo, loadConfig, saveConfig, prompt, getChainConfig } = require('../evm/utils');
const {
prepareWallet,
Expand All @@ -13,10 +12,10 @@ const {
uploadContract,
instantiateContract,
makeInstantiateMsg,
governanceAddress,
} = require('./utils');

const { Command, Option } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const upload = (client, wallet, chainName, config, options) => {
const { reuseCodeId, contractName, fetchCodeId } = options;
Expand Down Expand Up @@ -113,32 +112,15 @@ const programHandler = () => {

program.name('upload-contract').description('Upload CosmWasm contracts');

program.addOption(
new Option('-e, --env <env>', 'environment').choices(CHAIN_ENVIRONMENTS).default('testnet').makeOptionMandatory(true).env('ENV'),
);
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'));
addAmplifierOptions(program);

program.addOption(new Option('-r, --reuseCodeId', 'reuse code Id'));
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(
'-u, --uploadOnly',
'upload the contract without instantiating. prints expected contract address if --instantiate2 is passed',
),
);
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 deployment prompt confirmation').env('YES'));

program.addOption(new Option('--fetchCodeId', 'fetch code id from the chain by comparing to the uploaded code hash'));

program.action((options) => {
main(options);
Expand Down
31 changes: 4 additions & 27 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const {
governanceAddress,
} = require('./utils');
const { isNumber, saveConfig, loadConfig, printInfo, prompt } = require('../evm/utils');
const { CHAIN_ENVIRONMENTS } = require('../common');
const {
StoreCodeProposal,
StoreAndInstantiateContractProposal,
Expand All @@ -32,6 +31,7 @@ const {
} = require('cosmjs-types/cosmwasm/wasm/v1/proposal');

const { Command, Option } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const updateContractConfig = (contractConfig, chainConfig, key, value) => {
if (chainConfig) {
Expand Down Expand Up @@ -249,29 +249,7 @@ const programHandler = () => {

program.name('submit-proposal').description('Submit governance proposals');

// TODO: combine deploy-contract and submit-proposal options to remove duplicates
program.addOption(
new Option('-e, --env <env>', 'environment')
.choices(CHAIN_ENVIRONMENTS)
.default('devnet-amplifier')
.makeOptionMandatory(true)
.env('ENV'),
);
program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').makeOptionMandatory(true).env('MNEMONIC'));
program.addOption(new Option('-a, --artifactPath <artifactPath>', 'artifact path').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'));
addAmplifierOptions(program);

program.addOption(new Option('-t, --title <title>', 'title of proposal').makeOptionMandatory(true));
program.addOption(new Option('-d, --description <description>', 'description of proposal').makeOptionMandatory(true));
Expand All @@ -286,7 +264,6 @@ const programHandler = () => {
.choices(['store', 'storeInstantiate', 'instantiate', 'execute'])
.makeOptionMandatory(true),
);
program.addOption(new Option('--predictOnly', 'output the predicted changes only').env('PREDICT_ONLY'));

program.addOption(new Option('--source <source>', "a valid HTTPS URI to the contract's source code"));
program.addOption(
Expand All @@ -296,10 +273,10 @@ const programHandler = () => {
new Option('-i, --instantiateAddresses <instantiateAddresses>', 'comma separated list of addresses allowed to instantiate'),
);

program.addOption(new Option('--fetchCodeId', 'fetch code id from the chain by comparing to the uploaded code hash'));

program.addOption(new Option('--msg <msg>', 'json encoded message to submit with an execute contract proposal'));

program.addOption(new Option('--predictOnly', 'output the predicted changes only').env('PREDICT_ONLY'));

program.action((options) => {
main(options);
});
Expand Down
3 changes: 1 addition & 2 deletions cosmwasm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ const getSalt = (salt, contractName, chainNames) => fromHex(getSaltFromKey(salt

const getLabel = ({ contractName, label }) => label || contractName;

const readWasmFile = ({ artifactPath, contractName, aarch64 }) =>
readFileSync(`${artifactPath}/${pascalToSnake(contractName)}${aarch64 ? '-aarch64' : ''}.wasm`);
const readWasmFile = ({ artifactPath, contractName }) => readFileSync(`${artifactPath}/${pascalToSnake(contractName)}.wasm`);

const getChains = (config, { chainNames, instantiate2 }) => {
let chains = chainNames.split(',').map((str) => str.trim());
Expand Down
6 changes: 2 additions & 4 deletions stellar/balances.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -13,9 +13,7 @@ if (require.main === module) {

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

program.addOption(
new Option('-e, --env <env>', 'environment').choices(CHAIN_ENVIRONMENTS).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
6 changes: 2 additions & 4 deletions stellar/generate-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 <env>', 'environment').choices(CHAIN_ENVIRONMENTS).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
7 changes: 2 additions & 5 deletions stellar/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -116,10 +116,7 @@ if (require.main === module) {

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

program.addOption(
new Option('-e, --env <env>', 'environment').choices(CHAIN_ENVIRONMENTS).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
6 changes: 2 additions & 4 deletions sui/utils/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 <gasOptions>', 'gas options cli override'));

Expand Down
Loading