简体   繁体   English

使用 web3.py 解码智能合约的返回值?

[英]Decode the return value from a smart contract with web3.py?

I'm reposting the question since it wasn't well describe.我重新发布了这个问题,因为它描述得不好。

I'm working on a smart contract that is suppose to return 1 when I call it with a python script using web3.py, but instead of having a 1 in my python scirpt I receive an hexbytes object.我正在研究一个智能合约,当我使用 web3.py 使用 python 脚本调用它时,它假设返回 1,但我的 python 脚本中没有 1,而是收到一个六字节 ZA8CFDE6331BD59EB26666F891C41C41。 I suppose I need to decode it using the ABI and web3.py but I don't know how?我想我需要使用 ABI 和 web3.py 对其进行解码,但我不知道如何?

I have a function like this in solidity:我有一个像这样的 function :

pragma solidity ^0.5.10;

contract test {
    function test(int a) public returns (int) {
            if(a > 0){
                return 1;
            }
        }
}

When I called it with my python script:当我用我的 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())

I have result like this:我有这样的结果:

<class 'hexbytes.main.HexBytes'>
0x3791e76f3c1244722e60f72ac062765fca0c00c25ac8d5fcb22c5a9637c3706d

Can someone help?有人可以帮忙吗?

The transact() method submits the transaction and returns transaction hash. transact()方法提交交易并返回交易 hash。 You should first wait for the transaction to be mined, and get the transaction receipt using w3.eth.waitForTransactionReceipt .您应该首先等待交易被挖掘,然后使用w3.eth.waitForTransactionReceipt获取交易收据。 If you intend to use a transaction instead of a call you can get the result of your function by mutating the state and then reading the resulting state by calling a view function or mutating the state and generating an event . If you intend to use a transaction instead of a call you can get the result of your function by mutating the state and then reading the resulting state by calling a view function or mutating the state and generating an event .

In your case you don't mutate the state so you can mark your function as view :在您的情况下,您不会改变 state 因此您可以将 function 标记为view

function test(int a) view public returns (int)

and then use call instead of generating a transaction:然后使用call而不是生成事务:

contract.functions.test(52).call()

You can read here about the difference between a transaction and a call .您可以在此处阅读有关事务和调用之间的区别

Also the official web3py documentation has many examples of invoking smart contract functions.官方的 web3py 文档也有很多调用智能合约函数的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM