smart-contracts icon indicating copy to clipboard operation
smart-contracts copied to clipboard

pass in bytes32

Open ciasom opened this issue 7 years ago • 4 comments

For a constructor function with bytes32 parameter, how can I pass in the data (e.g. 0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c) when deploying it via the yml file. I tried 0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c with/without quotes. Both didn't work.

ciasom avatar Jan 09 '18 05:01 ciasom

Which parameter is this? I think you are doing it correctly, but I am not sure.

miohtama avatar Jan 10 '18 14:01 miohtama

Can you also provide a traceback.

miohtama avatar Jan 10 '18 14:01 miohtama

The console log is as follows when single quotes used. The function is_encodable in web3 module (web3.utils.abi) returns false since it requires the input as a string with length<=32 for type bytes32. But I think this is fine. I guess the problem should be at the stage of reading the data from yml file. There should be something to convert such value into bytes data, instead of treating such value as a normal string.

If without single quotes, the value was treated as a number and converted to decimal number. The log printed something like: Starting PresaleManager deployment, with arguments {'_commitment': 11252061538261140815797529001414997560767999792685399466878375241369984680076} And it can't pass the check in is_encodable either since the value is not a string.

The italic bold text was something from web3 since I added some prints.

(venv) $ deploy-contracts --deployment-file=crowdsales/presale.yml --deployment-name=testnet --address=0x627306090abaB3A6e1400e9345bC60c78a8BEf57 Web3 provider is RPC connection http://127.0.0.1:7545 Owner address is 0x627306090abaB3A6e1400e9345bC60c78a8BEf57 Owner balance is 100 ETH Starting PresaleManager deployment, with arguments {'_commitment': '0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c'} [{'name': b'_commitment', 'type': b'bytes32'}] ('0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c',) Types: [b'bytes32'] type: b'bytes32' . value: 0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c 2: bytes 32 [] 66 32 One or more arguments could not be encoded to the necessary ABI type. Expected types are: bytes32 Traceback (most recent call last): File "Downloads/ico-master/ico/deploy.py", line 66, in deploy_contract contract, txhash = chain.provider.deploy_contract(contract_name, deploy_transaction=transaction, deploy_kwargs=kwargs) File "Downloads/ico-master/venv/lib/python3.5/site-packages/populus/contracts/provider.py", line 121, in deploy_contract kwargs=deploy_kwargs, File "Downloads/ico-master/venv/lib/python3.5/site-packages/web3/contract.py", line 307, in deploy deploy_transaction['data'] = cls._encode_constructor_data(args, kwargs) File "Downloads/ico-master/venv/lib/python3.5/site-packages/eth_utils/string.py", line 89, in inner return force_obj_to_text(fn(*args, **kwargs)) File "Downloads/ico-master/venv/lib/python3.5/site-packages/web3/contract.py", line 748, in _encode_constructor_data cls._encode_abi(constructor_abi, arguments, data=cls.bytecode) File "Downloads/ico-master/venv/lib/python3.5/site-packages/web3/contract.py", line 703, in _encode_abi ', '.join(argument_types), TypeError: One or more arguments could not be encoded to the necessary ABI type. Expected types are: bytes32

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "Downloads/ico-master/venv/bin/deploy-contracts", line 9, in load_entry_point('ico', 'console_scripts', 'deploy-contracts')() File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 722, in call return self.main(*args, **kwargs) File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 697, in main rv = self.invoke(ctx) File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 895, in invoke return ctx.invoke(self.callback, **ctx.params) File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 535, in invoke return callback(*args, **kwargs) File "/usr/lib/python3.5/contextlib.py", line 77, in exit self.gen.throw(type, value, traceback) File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 87, in augment_usage_errors yield File "Downloads/ico-master/venv/lib/python3.5/site-packages/click/core.py", line 535, in invoke return callback(*args, **kwargs) File "Downloads/ico-master/ico/cmd/deploycontracts.py", line 31, in main deploy_crowdsale_from_file(project, deployment_file, deployment_name, address) File "Downloads/ico-master/ico/deploy.py", line 289, in deploy_crowdsale_from_file return _deploy_contracts(project, chain, web3, yaml_filename, chain_data, deploy_address) File "Downloads/ico-master/ico/deploy.py", line 273, in _deploy_contracts runtime_data, statistics, contracts = deploy_crowdsale(project, chain, yaml_filename, chain_data, deploy_address) File "Downloads/ico-master/ico/deploy.py", line 134, in deploy_crowdsale contracts[name] = deploy_contract(project, chain, deploy_address, expanded_contract_def, chain_name, need_unlock=need_unlock) File "Downloads/ico-master/ico/deploy.py", line 69, in deploy_contract raise RuntimeError("Could not deploy contract {}, constructor arguments {}".format(contract_name, kwargs)) from e RuntimeError: Could not deploy contract PresaleManager, constructor arguments {'_commitment': '0x18e070eea80451e1210b1e9755b04c20af10d531c0a0e9fbdd446563c22fc08c'}

ciasom avatar Jan 11 '18 00:01 ciasom

I've been running into the exact same error with just a simple token contract. With the following code:

with project.get_chain(chain_name) as chain:
    web3 = chain.web3
#     print(web3.eth.accounts)
#     print(web3.eth.blockNumber)

    Contract = get_contract_by_name(chain, "CrowdsaleToken")
    contract = Contract(address=contract_address)
    
    pp.pprint(contract.abi)

I get this response:

[   {   'constant': False,
        'inputs': [   {'name': 'addr', 'type': 'address'},
                      {'name': 'state', 'type': 'bool'}],
        'name': 'setTransferAgent',
        'outputs': [],
        'payable': False,
        'stateMutability': 'nonpayable',
        'type': 'function'},
    {   'constant': True,
        'inputs': [],
        'name': 'mintingFinished',
        'outputs': [{'name': '', 'type': 'bool'}],
        'payable': False,
        'stateMutability': 'view',
        'type': 'function'},
    {   'constant': True,
        'inputs': [],
        'name': 'decimals',
        'outputs': [{'name': '', 'type': 'uint256'}],
        'payable': False,
        'stateMutability': 'view',
        'type': 'function'},
    {   'constant': False,
        'inputs': [   {'name': 'receiver', 'type': 'address'},
                      {'name': 'amount', 'type': 'uint256'}],
        'name': 'mint',
        'outputs': [],
        'payable': False,
        'stateMutability': 'nonpayable',
        'type': 'function'},
    {   'constant': True,
        'inputs': [{'name': '', 'type': 'address'}],
        'name': 'mintAgents',
        'outputs': [{'name': '', 'type': 'bool'}],
        'payable': False,
        'stateMutability': 'view',
        'type': 'function'},
    {   'constant': False,
        'inputs': [   {'name': 'addr', 'type': 'address'},
                      {'name': 'state', 'type': 'bool'}],
        'name': 'setMintAgent',
        'outputs': [],
        'payable': False,
        'stateMutability': 'nonpayable',
        'type': 'function'},
    {   'constant': False,
        'inputs': [{'name': 'value', 'type': 'uint256'}],
        'name': 'upgrade',
        'outputs': [],
        'payable': False,
        'stateMutability': 'nonpayable',
        'type': 'function'},
    {   'constant': False,
        'inputs': [   {'name': '_name', 'type': 'string'},
                      {'name': '_symbol', 'type': 'string'}],
        'name': 'setTokenInformation',
        'outputs': [],
        'payable': False,
        'stateMutability': 'nonpayable',
        'type': 'function'},
    {   'constant': True,
        'inputs': [],
        'name': 'released',
        'outputs': [{'name': '', 'type': 'bool'}],
        'payable': False,
        'stateMutability': 'view',
        'type': 'function'},
    {   'constant': True,
        'inputs': [],
        'name': 'canUpgrade',
        'outputs': [{'name': '', 'type': 'bool'}],
        'payable': False,
        'stateMutability': 'view',
        'type': 'function'}
    ]

This would suggest that the ABI has all the functions I need, but when I call a contract I get the following:

with project.get_chain(chain_name) as chain:
    web3 = chain.web3
#     print(web3.eth.accounts)
#     print(web3.eth.blockNumber)

    Contract = get_contract_by_name(chain, "CrowdsaleToken")
    contract = Contract(address=contract_address)
    
    print(contract.call().canUpgrade())
#     pp.pprint(contract.call().totalSupply)

We get:

---------------------------------------------------------------------------
InsufficientDataBytes                     Traceback (most recent call last)
~/ico/venv/lib/python3.5/site-packages/web3/contract.py in call_contract_function(contract, function_name, transaction, *args, **kwargs)
    778     try:
--> 779         output_data = decode_abi(output_types, return_data)
    780     except DecodingError as e:

~/ico/venv/lib/python3.5/site-packages/eth_abi/abi.py in decode_abi(types, data)
    107     stream = BytesIO(data)
--> 108     return decoder(stream)

~/ico/venv/lib/python3.5/site-packages/eth_abi/decoding.py in __call__(self, stream)
    101     def __call__(self, stream):
--> 102         return self.decode(stream)
    103 

~/ico/venv/lib/python3.5/site-packages/eth_utils/functional.py in inner(*args, **kwargs)
     32         def inner(*args, **kwargs):
---> 33             return callback(fn(*args, **kwargs))
     34 

~/ico/venv/lib/python3.5/site-packages/eth_abi/decoding.py in decode(cls, stream)
    139             else:
--> 140                 yield decoder(stream)
    141 

~/ico/venv/lib/python3.5/site-packages/eth_abi/decoding.py in __call__(self, stream)
    101     def __call__(self, stream):
--> 102         return self.decode(stream)
    103 

~/ico/venv/lib/python3.5/site-packages/eth_abi/decoding.py in decode(cls, stream)
    164     def decode(cls, stream):
--> 165         raw_data = cls.read_data_from_stream(stream)
    166         data, padding_bytes = cls.split_data_and_padding(raw_data)

~/ico/venv/lib/python3.5/site-packages/eth_abi/decoding.py in read_data_from_stream(cls, stream)
    246                     cls.data_byte_size,
--> 247                     len(data),
    248                 )

InsufficientDataBytes: Tried to read 32 bytes.  Only got 0 bytes

The above exception was the direct cause of the following exception:

BadFunctionCallOutput                     Traceback (most recent call last)
<ipython-input-48-1255d7268996> in <module>()
      7     contract = Contract(address=contract_address)
      8 
----> 9     print(contract.call().canUpgrade())
     10 #     pp.pprint(contract.call().totalSupply)

~/ico/venv/lib/python3.5/site-packages/web3/contract.py in call_contract_function(contract, function_name, transaction, *args, **kwargs)
    799                 )
    800             )
--> 801         raise_from(BadFunctionCallOutput(msg), e)
    802 
    803     normalized_data = [

~/ico/venv/lib/python3.5/site-packages/web3/utils/exception_py3.py in raise_from(my_exception, other_exception)
      1 def raise_from(my_exception, other_exception):
----> 2     raise my_exception from other_exception

BadFunctionCallOutput: Could not transact with/call contract function, is contract deployed correctly and chain synced?

kivo360 avatar Feb 15 '18 17:02 kivo360