diff options
author | Fabio Berger <me@fabioberger.com> | 2017-12-06 06:18:36 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-12-06 06:18:36 +0800 |
commit | 08168c6e7d52711aeb46e27444ba26970e16e244 (patch) | |
tree | 40a006de279221009d0ee05d73bfbb74f0a9ea91 /packages/contracts/deploy/cli.ts | |
parent | b5030df4e3afe17b4e652b438d655edda79c5f54 (diff) | |
parent | 4441d76725af4e83f90eeb373983b600b6903e8e (diff) | |
download | dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.gz dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.bz2 dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.lz dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.xz dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.tar.zst dexon-sol-tools-08168c6e7d52711aeb46e27444ba26970e16e244.zip |
Merge branch 'development' into feature/addSubproviders
* development: (50 commits)
Add PR number to changelog
Address feedback
Add requestId to subscription messages and update json-schemas
Remove isomorphic-fetch types from contracts package
Update README
Regenerate files
Make it private
Change package name
Update README
Make fileExtension configurable
Rename abi-gen to typed-contracts
Add docs for typed-contracts
Remove TODOs
Introduce separate ContextData type and rework it
Check ABI is defined
Introduce a const for 'contract.mustache'
Improve error message
Reuse util
Fix a typo
Introduce a const for 'function'
...
# Conflicts:
# yarn.lock
Diffstat (limited to 'packages/contracts/deploy/cli.ts')
-rw-r--r-- | packages/contracts/deploy/cli.ts | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/packages/contracts/deploy/cli.ts b/packages/contracts/deploy/cli.ts new file mode 100644 index 000000000..73a43b247 --- /dev/null +++ b/packages/contracts/deploy/cli.ts @@ -0,0 +1,157 @@ +import * as _ from 'lodash'; +import * as path from 'path'; +import * as yargs from 'yargs'; + +import {commands} from './src/commands'; +import {network} from './src/utils/network'; +import { + CliOptions, + CompilerOptions, + DeployerOptions, +} from './src/utils/types'; + +const DEFAULT_OPTIMIZER_ENABLED = false; +const DEFAULT_CONTRACTS_DIR = path.resolve('contracts'); +const DEFAULT_ARTIFACTS_DIR = `${path.resolve('build')}/artifacts/`; +const DEFAULT_NETWORK_ID = 50; +const DEFAULT_JSONRPC_PORT = 8545; +const DEFAULT_GAS_PRICE = ((10 ** 9) * 2).toString(); + +/** + * Compiles all contracts with options passed in through CLI. + * @param argv Instance of process.argv provided by yargs. + */ +async function onCompileCommand(args: CliOptions): Promise<void> { + const opts: CompilerOptions = { + contractsDir: args.contractsDir, + networkId: args.networkId, + optimizerEnabled: args.shouldOptimize ? 1 : 0, + artifactsDir: args.artifactsDir, + }; + await commands.compileAsync(opts); +} +/** + * Compiles all contracts and runs migration script with options passed in through CLI. + * Uses network ID of running node. + * @param argv Instance of process.argv provided by yargs. + */ +async function onMigrateCommand(argv: CliOptions): Promise<void> { + const networkIdIfExists = await network.getNetworkIdIfExistsAsync(argv.jsonrpcPort); + const compilerOpts: CompilerOptions = { + contractsDir: argv.contractsDir, + networkId: networkIdIfExists, + optimizerEnabled: argv.shouldOptimize ? 1 : 0, + artifactsDir: argv.artifactsDir, + }; + await commands.compileAsync(compilerOpts); + + const defaults = { + gasPrice: argv.gasPrice, + from: argv.account, + }; + const deployerOpts: DeployerOptions = { + artifactsDir: argv.artifactsDir, + jsonrpcPort: argv.jsonrpcPort, + networkId: networkIdIfExists, + defaults, + }; + await commands.migrateAsync(deployerOpts); +} +/** + * Deploys a single contract with provided name and args. + * @param argv Instance of process.argv provided by yargs. + */ +async function onDeployCommand(argv: CliOptions): Promise<void> { + const networkIdIfExists = await network.getNetworkIdIfExistsAsync(argv.jsonrpcPort); + const compilerOpts: CompilerOptions = { + contractsDir: argv.contractsDir, + networkId: networkIdIfExists, + optimizerEnabled: argv.shouldOptimize ? 1 : 0, + artifactsDir: argv.artifactsDir, + }; + await commands.compileAsync(compilerOpts); + + const defaults = { + gasPrice: argv.gasPrice, + from: argv.account, + }; + const deployerOpts: DeployerOptions = { + artifactsDir: argv.artifactsDir, + jsonrpcPort: argv.jsonrpcPort, + networkId: networkIdIfExists, + defaults, + }; + const deployerArgsString = argv.args; + const deployerArgs = deployerArgsString.split(','); + await commands.deployAsync(argv.contract, deployerArgs, deployerOpts); +} +/** + * Provides extra required options for deploy command. + * @param yargsInstance yargs instance provided in builder function callback. + */ +function deployCommandBuilder(yargsInstance: any) { + return yargsInstance + .option('contract', { + type: 'string', + description: 'name of contract to deploy, exluding .sol extension', + }) + .option('args', { + type: 'string', + description: 'comma separated list of constructor args to deploy contract with', + }) + .demandOption(['contract', 'args']) + .help() + .argv; +} + +(() => { + return yargs + .option('contracts-dir', { + type: 'string', + default: DEFAULT_CONTRACTS_DIR, + description: 'path of contracts directory to compile', + }) + .option('network-id', { + type: 'number', + default: DEFAULT_NETWORK_ID, + description: 'mainnet=1, kovan=42, testrpc=50', + }) + .option('should-optimize', { + type: 'boolean', + default: DEFAULT_OPTIMIZER_ENABLED, + description: 'enable optimizer', + }) + .option('artifacts-dir', { + type: 'string', + default: DEFAULT_ARTIFACTS_DIR, + description: 'path to write contracts artifacts to', + }) + .option('jsonrpc-port', { + type: 'number', + default: DEFAULT_JSONRPC_PORT, + description: 'port connected to JSON RPC', + }) + .option('gas-price', { + type: 'string', + default: DEFAULT_GAS_PRICE, + description: 'gasPrice to be used for transactions', + }) + .option('account', { + type: 'string', + description: 'account to use for deploying contracts', + }) + .command('compile', + 'compile contracts', + _.noop, + onCompileCommand) + .command('migrate', + 'compile and deploy contracts using migration scripts', + _.noop, + onMigrateCommand) + .command('deploy', + 'deploy a single contract with provided arguments', + deployCommandBuilder, + onDeployCommand) + .help() + .argv; +})(); |