2

I have simple smart contract

pragma solidity ^0.4.17;

contract Inbox {
    string public message;

    constructor()  public payable {
        message = "Hi there!";
    }

    function setMessage(string newMessage) public {
       message = newMessage;
    }
}

I've deployed it to my local py-evm instance. I tried to execute message() method. I com do following:

vm = chain.get_vm()                                                     

nonce = vm.state.account_db.get_nonce(SENDER)                           

vm_tx = vm.create_unsigned_transaction(                                 
         nonce=nonce,                                                        
         gas_price=gasprice,                                                 
         gas=startgas,                                                       
         to=decode_hex('febb9c06ccc7d378059c03f0c50f848c39d96fb6'),  
         value=value,                                                        
         data=payload,                                                       
     )                                                                       
signed_tx = vm_tx.as_signed_transaction(SENDER_PRIVATE_KEY)             
new_block, receipt, computation = chain.apply_transaction(signed_tx)

and in computation.output I got: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\tHi there!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

I can see "Hi there!" but why there are extra bytes? How to get clean output?

1 Answer 1

0

In general, you'll probably want to interact with a node by using a Web3.py Contract to handle all the ABI type conversions. But since you're using py-evm directly... (and trinity won't support eth_call until the next release in a couple weeks):

You can use eth-abi v1.1.1 to decode the data:

from eth_abi import decode_abi

decoded_result = decode_abi(['string'], computation.output)

assert decoded_result == (b'Hi there!',)

Note that the next major version of eth-abi will also decode the bytes to a str, using utf-8. Until then, you can:

decoded_str = decoded_result[0].decode()

assert decoded_str == 'Hi there!'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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