【问题标题】:Detect reverted transaction in web3.py在 web3.py 中检测还原的事务
【发布时间】:2019-12-26 00:17:35
【问题描述】:

我正在尝试检测函数调用是否被还原,我总是得到事务哈希并且永远不会失败。

这个错误是针对 Web3.py 的,它没有提供带有事务的回调函数,在 web3.js 中这个未来是启用的。


def addParticipants(request):
    web3 = Web3(HTTPProvider(settings.Blockchain_IP_address, request_kwargs={'timeout': 60}))
    project_address = '0x93aeD90401a182247EE28848229531bC78053cd6'
    project = web3.eth.contract(address=project_address,
                                abi=Project_sol.abi,
                                bytecode=Project_sol.bytecode)
    func_to_call = 'addParticipant'
    addParticipant = project.functions[func_to_call]
    result = addParticipant(settings.ADMIN_ACCOUNT,0).transact(  {'from': settings.ADMIN_ACCOUNT, 'gasLimit': '6000000', 'gasPrice': '0', 'gas': 600000})
    web3.eth.waitForTransactionReceipt(result)
    print(result)

合约函数

function addParticipant(address _Participant, uint _weight)public isOwner returns (bool) {
    require(_weight!=0,"weight cannot be null");
    require(status,"this Donation is closed");

    Participants[_Participant].weight = _weight;
    Participants[_Participant].status = true;
    ParticipantsIndex[ParticipantsIndexSize] = _Participant;
    ParticipantsIndexSize++;

    emit ParticipantAction(_Participant, 'added');
    return true;
  }

我必须测试,

require(_weight!=0,"weight cannot be null");
require(status,"this Donation is closed");

如果不成功,它应该恢复并引发错误,但我总是收到没有错误的事务哈希

【问题讨论】:

    标签: python-3.x blockchain solidity web3


    【解决方案1】:

    您应该检查交易的收据。

    result = addParticipant(settings.ADMIN_ACCOUNT,0).transact(  {'from': settings.ADMIN_ACCOUNT, 'gasLimit': '6000000', 'gasPrice': '0', 'gas': 600000})
    receipt = web3.eth.waitForTransactionReceipt(result)
    print(receipt['status'])
    

    如果status字段为0,则表示交易被撤销。

    【讨论】:

      猜你喜欢
      • 2023-02-01
      • 2021-03-03
      • 2022-08-31
      • 2019-02-13
      • 1970-01-01
      • 2018-12-01
      • 2022-09-28
      • 2021-07-28
      • 2018-12-01
      相关资源
      最近更新 更多