我有一个简单的聪明的合同
pragma solidity ^0.4.17;
contract Inbox {
string public message;
constructor() public payable {
message = "Hi there!";
}
function setMessage(string newMessage) public {
message = newMessage;
}
}我已经将它部署到本地py-evm实例中。我试图执行message()方法。I做以下工作:
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)在computation.output中我得到了: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'
我能看到“嗨!”但是为什么会有额外的字节呢?如何获得清洁输出?
发布于 2018-08-21 17:14:25
通常,您可能希望通过使用Web3.py Contract来处理所有ABI类型转换来与节点交互。但既然你直接用py-evm ..。(在几周后的下一个版本之前,trinity不会支持eth_call ):
您可以使用eth-abi v1.1.1解码数据:
from eth_abi import decode_abi
decoded_result = decode_abi(['string'], computation.output)
assert decoded_result == (b'Hi there!',)请注意,下一个主要版本的eth还将使用bytes将str解码为utf-8。在此之前,你可以:
decoded_str = decoded_result[0].decode()
assert decoded_str == 'Hi there!'https://ethereum.stackexchange.com/questions/57061
复制相似问题