【发布时间】:2020-08-26 05:17:33
【问题描述】:
我重新发布了这个问题,因为它描述得不好。
我正在开发一个智能合约,当我使用 web3.py 使用 python 脚本调用它时,它假设返回 1,但我的 python 脚本中没有 1,而是收到一个 hexbytes 对象。我想我需要使用 ABI 和 web3.py 对其进行解码,但我不知道如何?
我在solidity中有这样的功能:
pragma solidity ^0.5.10;
contract test {
function test(int a) public returns (int) {
if(a > 0){
return 1;
}
}
}
当我用我的 python 脚本调用它时:
import json
import web3
from web3 import Web3
#To connect to ganache blockchain:
ganache_url = "http://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))
#this script will be the account number 1 on ganache blockchain:
web3.eth.defaultAccount = web3.eth.accounts[1]
#smart contract: abi, address and bytecode
abi = json.loads('....')
address = web3.toChecksumAddress("0x4A4AaA64857aa08a709A3470A016a516d3da40bf")
bytecode = "..."
#refering to the deploy coontract
contract = web3.eth.contract(address = address, abi = abi, bytecode = bytecode)
con = contract.functions.test(52).transact()
print(con.hex())
我有这样的结果:
<class 'hexbytes.main.HexBytes'>
0x3791e76f3c1244722e60f72ac062765fca0c00c25ac8d5fcb22c5a9637c3706d
有人可以帮忙吗?
【问题讨论】:
标签: python hex ethereum solidity smartcontracts