3

I am trying to catch require() errors when the transaction is reverted , but I get transaction's hash, I am using 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)

Solidity method:

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

the function should throw errors says

"weight cannot be null"

TylerH
  • 20,799
  • 66
  • 75
  • 101
Wael Golli
  • 87
  • 2
  • 8
  • Could you point to web3.py documentation which shows it is expected to behave as you've described? – Ed Noepel Aug 16 '19 at 17:19
  • @EdNoepel , in the documentation there is no example on errors,but i had an experience using web3.php and i got the expected results. – Wael Golli Aug 19 '19 at 13:02

3 Answers3

0

I followed the answer in a similar question found here: Solidity "require" error message in Python

You can use the fetch_transaction_revert_reason method of the web3-ethereum-defi. You simply import the module and if the transaction is not executed because of a require() error you can use the following command:

fetch_transaction_revert_reason(web3, tx_hash)

This will create a string containing the text 'execution reverted' followed by the error message provided by the require method.

In your case you could add before the receipt command the following line:

fetch_transaction_revert_reason(web3, result)

This way this should be printed in your console:

execution reverted: weight cannot be null

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 24 '22 at 07:56
0

Just use this:

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)

   if result.status == 0:
      try:
          result = w3.eth.call(tx)
      except ValueError as e:
          print(e)
Rego
  • 1,118
  • 1
  • 18
  • 40
-1
from web3 import exceptions

try:
    result = addParticipant(......)
    web3.eth.waitForTransactionReceipt(result)
    print(result)
except exceptions.SolidityError as error:
    print(error)
j809809jkdljfja
  • 767
  • 2
  • 9
  • 25
  • 1
    Please explain how this answers the question (code-only answers are not that great)… – aschipfl Dec 16 '20 at 14:03
  • Hi, reverted transaction throws SolidityError, you can catch it with Exceptions handling https://www.w3schools.com/python/python_try_except.asp and handle the error as you wish. In the code above error is just printed. – j809809jkdljfja Jan 13 '21 at 08:19
  • Unfortunately this answer is factually incorrect, because this is not how it works. – Mikko Ohtamaa Jun 25 '22 at 09:12