-
Notifications
You must be signed in to change notification settings - Fork 29
refactor(common,evm): cli-utils.ts #1004
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
Open
nbayindirli
wants to merge
14
commits into
main
Choose a base branch
from
refactor/cli-utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−423
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8571809
refactor(common,evm): cli-utils.ts
nbayindirli 90f64bb
prettier
nbayindirli b551531
account for JS consumers of cli-utils.ts
nbayindirli 32cfa52
use module.exports for fns used by JS
nbayindirli 1d4a200
Merge branch 'main' into refactor/cli-utils
nbayindirli 184ed0a
Merge branch 'main' into refactor/cli-utils
nbayindirli 7aa5ed8
Merge branch 'main' into refactor/cli-utils
nbayindirli 53e9f41
update ./cli-utils.js to ts
nbayindirli 85bdaf1
Merge branch 'main' into refactor/cli-utils
nbayindirli 2318115
updated package-lock.json
nbayindirli 0769c4e
add shims
nbayindirli bfeeb2c
nullish coallesce + rm strict
nbayindirli 2cf5c3b
add addTopUpOptions return type
nbayindirli d79b336
Merge branch 'main' into refactor/cli-utils
nbayindirli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,2 @@ | ||
'use strict'; | ||
|
||
require('dotenv').config(); | ||
|
||
const fs = require('fs'); | ||
const { Option } = 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 addEnvOption = (program, defaultValue) => { | ||
program.addOption( | ||
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('--gasOptions <gasOptions>', 'gas options cli override')); | ||
|
||
if (!options.ignoreChainNames) { | ||
program.addOption( | ||
new Option('-n, --chainNames <chainNames>', 'chains to run the script over').makeOptionMandatory(true).env('CHAINS'), | ||
); | ||
program.addOption(new Option('--skipChains <skipChains>', 'chains to skip over')); | ||
program.addOption( | ||
new Option( | ||
'--startFromChain <startFromChain>', | ||
'start from a specific chain onwards in the config, useful when a cmd fails for an intermediate chain', | ||
), | ||
); | ||
} | ||
|
||
if (!options.ignorePrivateKey) { | ||
program.addOption(new Option('-p, --privateKey <privateKey>', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); | ||
} | ||
|
||
if (options.address) { | ||
program.addOption(new Option('-a, --address <address>', 'override address')); | ||
} | ||
|
||
return program; | ||
}; | ||
|
||
// `optionMethod` is a method such as `addBaseOptions` | ||
// `options` is an option object for optionMethod | ||
const addOptionsToCommands = (program, optionMethod, options) => { | ||
if (program.commands.length > 0) { | ||
program.commands.forEach((command) => { | ||
optionMethod(command, options); | ||
}); | ||
} | ||
}; | ||
|
||
const addStoreOptions = (program) => { | ||
program.addOption( | ||
new Option( | ||
'-a, --artifact-dir <artifactDir>', | ||
'Path to the contract artifact directory to upload (required if --version is not used)', | ||
).env('ARTIFACT_DIR'), | ||
); | ||
|
||
program.addOption( | ||
new Option( | ||
'-v, --version <contractVersion>', | ||
'Specify a released version (X.Y.Z) or a commit hash to upload (required if --artifact-dir is not used)', | ||
).env('CONTRACT_VERSION'), | ||
); | ||
|
||
program.hook('preAction', async (thisCommand) => { | ||
const opts = thisCommand.opts(); | ||
|
||
if (!opts.artifactDir && !opts.version) { | ||
throw new Error('Either --artifact-dir or --version is required'); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
addEnvOption, | ||
addBaseOptions, | ||
addOptionsToCommands, | ||
addStoreOptions, | ||
}; | ||
// TODO Remove shim after TS migration | ||
module.exports = require('../dist/common/cli-utils'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Command, Option } from 'commander'; | ||
import * as dotenv from 'dotenv'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
dotenv.config(); | ||
|
||
// Resolve the path relative to the package root, accounting for both source and dist directories | ||
const resolveFromRoot = (relativePath: string) => { | ||
// Check if we're in dist directory | ||
if (__dirname.includes('dist')) { | ||
return path.join(__dirname, '../../', relativePath); | ||
} | ||
// We're in the source directory | ||
return path.join(__dirname, '../', relativePath); | ||
}; | ||
|
||
const CHAIN_CONFIG_PATH = resolveFromRoot('axelar-chains-config/info'); | ||
const CHAIN_ENVIRONMENTS = fs.readdirSync(CHAIN_CONFIG_PATH).map((chainName: string) => chainName.split('.')[0]); | ||
|
||
interface BaseOptions { | ||
ignoreChainNames?: boolean; | ||
ignorePrivateKey?: boolean; | ||
address?: boolean; | ||
} | ||
|
||
const addEnvOption = (program: Command, defaultValue?: string): void => { | ||
program.addOption( | ||
new Option('-e, --env <env>', 'environment') | ||
.choices(CHAIN_ENVIRONMENTS) | ||
.default(defaultValue ?? 'testnet') | ||
.makeOptionMandatory(true) | ||
.env('ENV'), | ||
); | ||
}; | ||
|
||
const addBaseOptions = (program: Command, options: BaseOptions = {}): Command => { | ||
addEnvOption(program); | ||
|
||
program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES')); | ||
program.addOption(new Option('--parallel', 'run script in parallel wrt chains')); | ||
program.addOption(new Option('--gasOptions <gasOptions>', 'gas options cli override')); | ||
|
||
if (!options.ignoreChainNames) { | ||
program.addOption( | ||
new Option('-n, --chainNames <chainNames>', 'chains to run the script over').makeOptionMandatory(true).env('CHAINS'), | ||
); | ||
program.addOption(new Option('--skipChains <skipChains>', 'chains to skip over')); | ||
program.addOption( | ||
new Option( | ||
'--startFromChain <startFromChain>', | ||
'start from a specific chain onwards in the config, useful when a cmd fails for an intermediate chain', | ||
), | ||
); | ||
} | ||
|
||
if (!options.ignorePrivateKey) { | ||
program.addOption(new Option('-p, --privateKey <privateKey>', 'private key').makeOptionMandatory(true).env('PRIVATE_KEY')); | ||
} | ||
|
||
if (options.address) { | ||
program.addOption(new Option('-a, --address <address>', 'override address')); | ||
} | ||
|
||
return program; | ||
}; | ||
|
||
const addOptionsToCommands = <T>(program: Command, optionMethod: (command: Command, options: T) => void, options: T): void => { | ||
if (program.commands.length > 0) { | ||
program.commands.forEach((command) => { | ||
optionMethod(command, options); | ||
}); | ||
} | ||
}; | ||
|
||
const addStoreOptions = (program: Command): void => { | ||
program.addOption( | ||
new Option( | ||
'-a, --artifact-dir <artifactDir>', | ||
'Path to the contract artifact directory to upload (required if --version is not used)', | ||
).env('ARTIFACT_DIR'), | ||
); | ||
|
||
program.addOption( | ||
new Option( | ||
'-v, --version <contractVersion>', | ||
'Specify a released version (X.Y.Z) or a commit hash to upload (required if --artifact-dir is not used)', | ||
).env('CONTRACT_VERSION'), | ||
); | ||
|
||
program.hook('preAction', async (thisCommand) => { | ||
const opts = thisCommand.opts(); | ||
|
||
if (!opts.artifactDir && !opts.version) { | ||
throw new Error('Either --artifact-dir or --version is required'); | ||
} | ||
}); | ||
}; | ||
|
||
export { addEnvOption, addBaseOptions, addOptionsToCommands, addStoreOptions }; | ||
export type { BaseOptions }; | ||
|
||
module.exports = { | ||
addEnvOption, | ||
addBaseOptions, | ||
addOptionsToCommands, | ||
addStoreOptions, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,2 @@ | ||
const { Option } = require('commander'); | ||
const { addBaseOptions, ...exportedCliUtils } = require('../common/cli-utils'); | ||
|
||
const addEvmOptions = (program, options = {}) => { | ||
addBaseOptions(program, options); | ||
|
||
program.addOption(new Option('-v, --verify', 'verify the deployed contract on the explorer').env('VERIFY')); | ||
|
||
if (options.artifactPath) { | ||
program.addOption(new Option('--artifactPath <artifactPath>', 'artifact path')); | ||
} | ||
|
||
if (options.contractName) { | ||
program.addOption(new Option('-c, --contractName <contractName>', 'contract name').makeOptionMandatory(true)); | ||
} | ||
|
||
if (options.deployMethod) { | ||
program.addOption( | ||
new Option('-m, --deployMethod <deployMethod>', 'deployment method') | ||
.choices(['create', 'create2', 'create3']) | ||
.default(options.deployMethod), | ||
); | ||
} | ||
|
||
if (options.salt) { | ||
program.addOption(new Option('-s, --salt <salt>', 'salt to use for create2 deployment').env('SALT')); | ||
} | ||
|
||
if (options.skipExisting) { | ||
program.addOption(new Option('-x, --skipExisting', 'skip existing if contract was already deployed on chain').env('SKIP_EXISTING')); | ||
} | ||
|
||
if (options.upgrade) { | ||
program.addOption(new Option('-u, --upgrade', 'upgrade a deployed contract').env('UPGRADE')); | ||
} | ||
|
||
if (options.predictOnly) { | ||
program.addOption(new Option('--predictOnly', 'output the predicted changes only').env('PREDICT_ONLY')); | ||
} | ||
|
||
return program; | ||
}; | ||
|
||
const addTopUpOptions = (program) => { | ||
program.addOption(new Option('-t, --target <target>', 'target balance for each account').makeOptionMandatory(true)); | ||
program.addOption( | ||
new Option('--threshold <threshold>', 'top up accounts only if the balance is below this threshold').makeOptionMandatory(true), | ||
); | ||
program.addOption(new Option('-u, --units', 'amounts are set in smallest unit')); | ||
program.addOption( | ||
new Option( | ||
'--addresses-to-derive <addresses-to-derive>', | ||
'number of addresses to derive from mnemonic. Derived addresses will be added to the list of addresses to fund set by using --addresses option', | ||
).env('DERIVE_ACCOUNTS'), | ||
); | ||
program.addOption( | ||
new Option('--addresses <addresses>', 'comma separated list of addresses to top up') | ||
.default([]) | ||
.argParser((addresses) => addresses.split(',').map((address) => address.trim())), | ||
); | ||
program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').env('MNEMONIC')); | ||
}; | ||
|
||
module.exports = { | ||
...exportedCliUtils, | ||
addBaseOptions, | ||
addEvmOptions, | ||
addTopUpOptions, | ||
}; | ||
// TODO Remove shim after TS migration | ||
module.exports = require('../dist/evm/cli-utils'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Command, Option } from 'commander'; | ||
|
||
import { BaseOptions, addBaseOptions, addEnvOption, addOptionsToCommands, addStoreOptions } from '../common/cli-utils'; | ||
|
||
interface EvmOptions extends BaseOptions { | ||
artifactPath?: boolean; | ||
contractName?: boolean; | ||
deployMethod?: string; | ||
salt?: boolean; | ||
skipExisting?: boolean; | ||
upgrade?: boolean; | ||
predictOnly?: boolean; | ||
} | ||
|
||
const addEvmOptions = (program: Command, options: EvmOptions = {}): Command => { | ||
nbayindirli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addBaseOptions(program, options); | ||
|
||
program.addOption(new Option('-v, --verify', 'verify the deployed contract on the explorer').env('VERIFY')); | ||
|
||
if (options.artifactPath) { | ||
program.addOption(new Option('--artifactPath <artifactPath>', 'artifact path')); | ||
} | ||
|
||
if (options.contractName) { | ||
program.addOption(new Option('-c, --contractName <contractName>', 'contract name').makeOptionMandatory(true)); | ||
} | ||
|
||
if (options.deployMethod) { | ||
program.addOption( | ||
new Option('-m, --deployMethod <deployMethod>', 'deployment method') | ||
.choices(['create', 'create2', 'create3']) | ||
.default(options.deployMethod), | ||
); | ||
} | ||
|
||
if (options.salt) { | ||
program.addOption(new Option('-s, --salt <salt>', 'salt to use for create2 deployment').env('SALT')); | ||
} | ||
|
||
if (options.skipExisting) { | ||
program.addOption(new Option('-x, --skipExisting', 'skip existing if contract was already deployed on chain').env('SKIP_EXISTING')); | ||
} | ||
|
||
if (options.upgrade) { | ||
program.addOption(new Option('-u, --upgrade', 'upgrade a deployed contract').env('UPGRADE')); | ||
} | ||
|
||
if (options.predictOnly) { | ||
program.addOption(new Option('--predictOnly', 'output the predicted changes only').env('PREDICT_ONLY')); | ||
} | ||
|
||
return program; | ||
}; | ||
|
||
const addTopUpOptions = (program: Command): void => { | ||
program.addOption(new Option('-t, --target <target>', 'target balance for each account').makeOptionMandatory(true)); | ||
program.addOption( | ||
new Option('--threshold <threshold>', 'top up accounts only if the balance is below this threshold').makeOptionMandatory(true), | ||
); | ||
program.addOption(new Option('-u, --units', 'amounts are set in smallest unit')); | ||
program.addOption( | ||
new Option( | ||
'--addresses-to-derive <addresses-to-derive>', | ||
'number of addresses to derive from mnemonic. Derived addresses will be added to the list of addresses to fund set by using --addresses option', | ||
).env('DERIVE_ACCOUNTS'), | ||
); | ||
program.addOption( | ||
new Option('--addresses <addresses>', 'comma separated list of addresses to top up') | ||
.default([]) | ||
.argParser((addresses: string): string[] => addresses.split(',').map((address) => address.trim())), | ||
); | ||
program.addOption(new Option('-m, --mnemonic <mnemonic>', 'mnemonic').env('MNEMONIC')); | ||
}; | ||
|
||
module.exports = { | ||
addEnvOption, | ||
addBaseOptions, | ||
addOptionsToCommands, | ||
addStoreOptions, | ||
addEvmOptions, | ||
addTopUpOptions, | ||
}; | ||
|
||
export type { BaseOptions, EvmOptions }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.