question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the function inside a deployed contract? [BadFunctionCallOutput]

See original GitHub issue
  • Version: 0.15.3
  • Python: 2.7
  • OS: linux

Here is an example to develop a smart contract by python which is written by myself.

Suppose the RPC port of geth node is 8200, and there are at least 2 accounts in keystore. At least 2 python package are required (web3, py-solc)

sudo pip install web3
sudo pip install py-solc

Input following code into python command console:

source_code = 'contract MyToken {     address issuer;     mapping (address => uint) balances;      event Issue(address account, uint amount);     event Transfer(address from, address to, uint amount);      function MyToken() {         issuer = msg.sender;     }      function issue(address account, uint amount) {         if (msg.sender != issuer) throw;         balances[account] += amount;     }      function transfer(address to, uint amount) {         if (balances[msg.sender] < amount) throw;          balances[msg.sender] -= amount;         balances[to] += amount;          Transfer(msg.sender, to, amount);     }      function getBalance(address account) constant returns (uint) {         return balances[account];     } }'

import web3
obj = web3.Web3(web3.HTTPProvider('http://localhost:8200'))
psn = web3.personal.Personal(obj)
psn.unlockAccount(obj.eth.accounts[0],"123456",1000) #replace 123456 to your Ethereum accounts password

from solc import compile_source
compile_sol = compile_source(source_code)

my_contract = web3.contract.construct_contract_factory(
    web3=obj, 
    abi = compile_sol['<stdin>:MyToken']['abi'],
    code = compile_sol['<stdin>:MyToken']['bin'], 
    code_runtime = compile_sol['<stdin>:MyToken']['bin-runtime'],
    source = source_code
)

my_contract.bytecode = my_contract.code
trans_hash = my_contract.deploy(transaction={'from':obj.eth.accounts[0],'value':120})

The contract should be deploy on the blockchain if this transaction is mined.

But when I try to call the function inside the contract, there are some exceptions occured:

my_contract.call({'to':obj.eth.accounts[0]}).getBalance(obj.eth.accounts[0])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/eth_utils/string.py", line 85, in inner`
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/web3/contract.py", line 779, in call_contract_function
    raise_from(BadFunctionCallOutput(msg), e)
  File "/usr/local/lib/python2.7/dist-packages/web3/contract.py", line 767, in call_contract_function
    output_data = decode_abi(output_types, return_data)
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/abi.py", line 108, in decode_abi
    return decoder(stream)
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/decoding.py", line 102, in __call__
    return self.decode(stream)
  File "/usr/local/lib/python2.7/dist-packages/eth_utils/functional.py", line 22, in inner
    return callback(fn(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/decoding.py", line 140, in decode
    yield decoder(stream)
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/decoding.py", line 102, in __call__
    return self.decode(stream)
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/decoding.py", line 165, in decode
    raw_data = cls.read_data_from_stream(stream)
  File "/usr/local/lib/python2.7/dist-packages/eth_abi/decoding.py", line 247, in read_data_from_stream
    cls.data_byte_size,
web3.exceptions.BadFunctionCallOutput: Could not decode contract function call getBalance return data 0x for output_types [u'uint256']

How to deal with this problem? Is there any examples to reference? Thank you very much!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
weipincommented, Apr 17, 2017

Glad to hear it worked.

@pipermerriam May I suggest to put together a step by step tutorial of web3.py, with code snippets of deploying a contract (likely a standard ERC20 one), executing methods, etc. That would be another issue though.

Cute Animal Picture

fullsizerender

2reactions
weipincommented, Apr 17, 2017

my_contract.call({'to':obj.eth.accounts[0]}).getBalance(obj.eth.accounts[0])

Here seems the account address was passed as to, instead of a contract address.

Assuming we use testrpc and the contract address is available immediately, the code snippet of obtaining a contract address and making a call could be something like this:

trans_hash = my_contract.deploy(...)
txn_receipt = web3.eth.getTransactionReceipt(trans_hash)
contract_address = txn_receipt['contractAddress']
my_contract.call({'to':contract_address}).getBalance(web3.eth.accounts[0])

Also, the arg transaction of the method call is optional (default to None), so if we assign my_contract a proper contract address, we can omit the arg transaction.

my_contract = web3.eth.contract(abi=compile_sol[':MyToken']['abi'], address=contract_address)
my_contract.call().getBalance(web3.eth.accounts[0])

Hope it makes sense to you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not transact with/call contract function, is contract ...
BadFunctionCallOutput : Could not transact with/call contract function, is contract deployed correctly and chain synced? Question: How to use ...
Read more >
web3.py - Error when calling a function from smart contract
I was trying to call the balanceOf function in ...
Read more >
ethereum/web3.py - Gitter
BadFunctionCallOutput : Could not transact with/call contract function, is contract deployed correctly and chain synced? Can someone point out what exactly ...
Read more >
Call Smart Contract From Another Smart Contract | Chainlink
In this developer tutorial, learn how to call a smart contract from ... The constructor, a function that is called only during the...
Read more >
How to call another smart contract from your solidity code
1. Smart contracts are the most popular feature of the Ethereum network. Smart contracts have functions ... 2. Smart contracts are computer code,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found