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:
- Created 6 years ago
- Comments:12 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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:
Also, the arg
transaction
of the methodcall
is optional (default to None), so if we assignmy_contract
a proper contract address, we can omit the argtransaction
.Hope it makes sense to you.