简体   繁体   English

如何使用 web3py 调用合约公共变量?

[英]How to call contract public variable using web3py?

I tried to call solidity smart contract public variable using python web3.我尝试使用 python web3 调用solidity 智能合约公共变量。 But when I called, it didn't work and showed the error: 500 (Internal Server Error).但是当我打电话时,它不起作用并显示错误:500(内部服务器错误)。

Code summary is like this:代码摘要是这样的:

  • smart contract part:智能合约部分:
pragma solidity ^0.8.7;
...
contract TestNFT is ERC721Enumerable, Ownable, Pausable {
...
   mapping(uint => bool) public locked;
...
}
  • python code:蟒蛇代码:

fashionItemAddress and abi are from at first line of this code and they are working well. fashionItemAddressabi来自此代码的第一行,它们运行良好。

@api_view(['POST'])
def sendTxHashUndress(request):
    transaction_hash = request.POST["transaction_hash"]

    INFURA_URL = "https://rinkeby.infura.io/v3/9eda0366d20f4627860299a5ac514808"
    web3 = Web3(Web3.HTTPProvider(INFURA_URL))
    fContract = web3.eth.contract(fashionItemAddress, abi=ABI)
    print(fContract, '2222222222222')
    print(fContract.call().locked(), '==============')
    try:
        receiptments = web3.eth.wait_for_transaction_receipt(transaction_hash, 120, 0.1)
        print("receiptments", receiptments.status)
        if receiptments.status == 1:
            return _undressIcongirl(request)

        return JsonResponse({'result': 'failed_transaction'})
    except Exception as error:
        print(str(error))
        return JsonResponse({'result': str(error)})

the result in console is like this:控制台中的结果是这样的:

0xe0ffc7d34f98803e7744cc587f80df19e93a010a760f7af1253c1405ff2faded 11111111
<web3._utils.datatypes.Contract object at 0x0000024679478A60> 2222222222222
[28/Jun/2022 18:47:53] "POST /api/sendTxHashUndress HTTP/1.1" 500 145

在此处输入图像描述

I think the problem is to get fContract .我认为问题在于获得fContract If not, I am not sure what's wrong.如果没有,我不确定出了什么问题。

trying as several times and checking code deeply, I never tried to send param in locked() function.多次尝试并深入检查代码,我从未尝试在locked()函数中发送参数。

I mean I have to send param as token_id in locked(token_id) like this.我的意思是我必须像这样在 lock locked(token_id)中将参数作为token_id发送。

This works for me and this was solved.这对我有用,这已经解决了。

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

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