如何使用Web3py调用合同公共变量?

发布于 2025-02-11 05:40:57 字数 1624 浏览 2 评论 0原文

我试图使用Python Web3调用Solidity Smart Contract公共变量。 但是当我打电话给它时,它不起作用并显示出错误:500(内部服务器错误)。

代码摘要如下:

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

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)})

控制台中的结果是这样的:

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 tried to call solidity smart contract public variable using python web3.
But when I called, it didn't work and showed the error: 500 (Internal Server Error).

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.

@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

enter image description here

I think the problem is to get fContract. If not, I am not sure what's wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-02-18 05:40:57

尝试几次并深入检查代码,我从未尝试在locked()函数中发送param。

我的意思是,我必须将param作为token_id in locked(token_id)之类的。

这对我有用,这解决了。

trying as several times and checking code deeply, I never tried to send param in locked() function.

I mean I have to send param as token_id in locked(token_id) like this.

This works for me and this was solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文