13

As per my understanding when I invoke a constant function without a transaction in a contract and if the function returns some result I am able to get it in EthereumJ or Go-Ethereum, But when a transaction is performed on a function it is returning a hex value. Please suggest how to get a return value from a function on which a transaction has been performed.

9

You need to use an Event, see example:

contract answer{
  // ...
  event VoteEvent(string ID, bool returnValue);

  function vote(string ID, uint qNum, uint ans) returns (bool) {
    // ...
    VoteEvent(ID, true);
    return true;
  }
}
0

You can obtain the value returned from a transaction by calling debug_traceTransaction and looking at the final step in the trace. It is typically a RETURN opcode, so the top 2 stack values give the offset and length of the expected return data within the memory. You can then use the ABI to format this data accordingly.

Here is some sample code to achieve this in Python. step is the final step in the stack trace obtained by calling debug_traceTransaction, abi is the abi for the contract method being called.

from eth_abi import decode_abi
from hexbytes import HexBytes


def get_return_value(step, abi):
    offset = int(step['stack'][-1], 16) * 2
    length = int(step['stack'][-2], 16) * 2
    memory = HexBytes("".join(step['memory'])[offset:offset+length])
    return decode_abi([i['type'] for i in abi['outputs']], memory)

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.