如何使用Web3py调用合同公共变量?
我试图使用Python Web3调用Solidity Smart Contract公共变量。 但是当我打电话给它时,它不起作用并显示出错误:500(内部服务器错误)。
代码摘要如下:
- 智能合约部分:
pragma solidity ^0.8.7;
...
contract TestNFT is ERC721Enumerable, Ownable, Pausable {
...
mapping(uint => bool) public locked;
...
}
- Python代码:
fashionitemaddress
和abi
来自此代码的第一行,它们运行良好。
@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
I think the problem is to get fContract
. If not, I am not sure what's wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试几次并深入检查代码,我从未尝试在
locked()
函数中发送param。我的意思是,我必须将param作为
token_id
inlocked(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
inlocked(token_id)
like this.This works for me and this was solved.