如何“捕获web3.py中的错误”

时间:2019-08-10 19:59:30

标签: python blockchain ethereum web3

我试图在恢复交易时捕获 require()错误,但是我得到了交易的哈希值,我正在使用 web3.py

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

函数应该抛出错误,提示“重量不能为空”

1 个答案:

答案 0 :(得分:0)

from web3 import exceptions

try:
    result = addParticipant(......)
    web3.eth.waitForTransactionReceipt(result)
    print(result)
except exceptions.SolidityError as error:
    print(error)