I am using web3.py and trying to get the return value of a smart contract method after calling it. Of course, I can do it like this:

value = smart_contract_instance.someFunction()

but this does not give me any information about how much gas was used. If I use the transact method like this:

tx_hash = smart_contract_instance.transact({'to': w3.eth.accounts[0]}).someFunction()

I can access the receipt with w3.eth.getTransactionReceipt(tx_hash), but this does not give me the result. How can I access the result of the call without having to invoke the smart contract twice (as in using both code snippets from above)?


Transactions don't have return values. If the function you're calling returns a value, that's simply ignored. The typical pattern is to log an event from the function and look for that event in the transaction receipt.

Note that if you call a function without sending a transaction (probably what's happening in your first code snippet), then you do get a return value, but the call has no side effects (no state changes or ether transfers).

  • Thanks, I'll look at the event documentation of web3.py. Regarding calls: What happens when I call a function where the return value depends on the input? Is the code still executed, but the state changes discarded? – Andreas Unterweger Jan 27 '18 at 18:24
  • Yes, the code is executed locally on the node you're connected to, and then the side effects are discarded. – smarx Jan 27 '18 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.