Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from blockvigil/prompt-constructor-inputs
Browse files Browse the repository at this point in the history
Prompt for constructor inputs
  • Loading branch information
anomit authored Jul 13, 2020
2 parents 6c6517c + 79ba3ac commit 662b488
Show file tree
Hide file tree
Showing 8 changed files with 713 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ Deploying tx: 0x17a8009565731f45a1621905a7e85e84a6330b485ac3e7e450d90f126b6c3006
```
Observe that we are setting `--constructorInputs`. It is optional for contracts that have no constructor inputs programmed.

If you do not pass the `--constructorInputs` argument, you shall be prompted for the same.

```
ev-cli deploy contracts/ERC20Mintable.sol --contractName='ERC20Mintable'
Enter constructor inputs...
name(string): TestToken
symbol(string): TTK
decimals(uint8): 18
Contract ERC20Mintable deployed successfully
Contract Address: 0x9290b03870b0c4c99cc3c1e1dfcfa1ff789af6c0
Deploying tx: 0x699af417f4349f9e29d63dbc894874b5ae865fefe8e7a6bb2365339fab774211
```

### SignerControlBase.sol

This contract forms the base of [EthVigil's Proxy+Signer Control contract](https://medium.com/blockvigil/signer-control-cum-proxy-smart-contract-a-look-at-ethvigils-latest-offering-9ad6c098c095). Without going into the logic of the contract, let us take a look at the constructor as written in the contract.
Expand Down
30 changes: 27 additions & 3 deletions click_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pwd
from eth_utils import to_normalized_address
from solidity_parser import parser
from utils.EVContractUtils import extract_abi, ABIParser

CONTEXT_SETTINGS = dict(
help_option_names=['-h', '--help']
Expand Down Expand Up @@ -222,7 +223,7 @@ def importsettings(importfile, verbose):
@click.option('--contractName', 'contract_name', required=True,
help='name of the contract to be deployed. For eg. FixedSupplyToken')
@click.option('--constructorInputs', 'inputs',
help='constructor input values as a JSON list. '
help='constructor input values as a JSON list. OPTIONAL. If you do not specify, you shall be prompted for the same. '
'Eg: \'["abced", "0x008604d4997a15a77f00CA37aA9f6A376E129DC5"]\' '
'for constructor inputs of type (string, address). '
'Can be left empty if there are no inputs accepted by the constructor')
Expand All @@ -235,15 +236,16 @@ def deploy(ctx_obj, contract_name, inputs, verbose, contract):
CONTRACT: path to the solidity file
Usage example: ev-cli deploy ../token.sol --contractName=FixedSupplyToken --constructorInputs='JSON representation of the constructor arguments'
Usage example: ev-cli deploy contracts/Microblog.sol --contractName=Microblog --constructorInputs='JSON representation of the constructor arguments in an array'
"""
contract_src = ""
constructor_input_prompt = False
if verbose:
click.echo('Got constructor inputs: ')
click.echo(inputs)
if inputs:
c_inputs = json.loads(inputs)
else:
constructor_input_prompt = True
c_inputs = list() # an empty list
sources = dict()
if contract[0] == '~':
Expand Down Expand Up @@ -281,6 +283,28 @@ def deploy(ctx_obj, contract_name, inputs, verbose, contract):
break
contract_src += chunk
sources[f'ev-cli/{import_location[2:]}'] = {'content': contract_src}

if len(c_inputs) == 0 and constructor_input_prompt:
abi_json = extract_abi(ctx_obj['settings'], {'sources': sources, 'sourceFile': f'ev-cli/{contract_file_name}'})
abp = ABIParser(abi_json=abi_json)
abp.load_abi()
if len(abp.constructor_params()) > 0:
click.echo('Enter constructor inputs...')
for idx, each_param in enumerate(abp.constructor_params()):
param_type = abp._constructor_mapping["constructor"]["input_types"][idx]
param_type_cat = abp.type_category(param_type)
arg = click.prompt(f'{each_param}({param_type})')
if param_type_cat == 'integer':
arg = int(arg)
elif param_type_cat == 'array':
# check if it can be deserialized into a python dict
try:
arg_dict = json.loads(arg)
except json.JSONDecodeError:
click.echo(f'Parameter {each_param} of type {param_type} '
f'should be correctly passed as a JSON array', err=True)
sys.exit(1)
c_inputs.append(arg)
msg = "Trying to deploy"
message_hash = encode_defunct(text=msg)
# deploy from alpha account
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ PyInstaller==3.6
pytest==4.4.1
pytest-click==0.3
solidity_parser==0.0.7
tenacity==6.2.0
antlr4-python3-runtime>=4.7,<4.8
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
'requests == 2.22.0',
'eth-account == 0.4.0',
'solidity_parser == 0.0.7',
'click == 7.0'
'click == 7.0',
"tenacity==6.2.0",
"antlr4-python3-runtime>=4.7,<4.8"
],
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
Loading

0 comments on commit 662b488

Please sign in to comment.