3

My smart contract has the following piece of code:

# check if the election has begun. begin and end time must be in the future and in unix timestamp
assert block.timestamp < self.elections[_election_id].begin_unix_timestamp, "The election has already started"

On the block explorer (https://goerli.etherscan.io/tx/0xef2d5fe12514ecfe582e191b97316a4c99f5b24c4213ea0a1940dde179ec8f1e) I can see that the transaction has failed with the error message:

Status: Fail with error 'The election has already started'

How can I access this error message using web3? All I have access to is the status code in the transaction receipt, but not the error message:

signed_transaction = w3.eth.account.sign_transaction(transaction, private_key=goerli_account_private_key)
sent_raw_transaction = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
receipt = w3.eth.waitForTransactionReceipt(sent_raw_transaction)
print(receipt.status)
# print(some_transaction_object.fail_message) # I'm looking for something like this

1 Answer 1

0

This works using a w3 connection to infura:

# fetch a reverted transaction:
tx = w3.eth.get_transaction('0xdc9978af3e19475ee019cc21fd8513db74a98539ab373a9eb768423096ee2...')
# build a new transaction to replay:
replay_tx = {
    'to': tx['to'],
    'from': tx['from'],
    'value': tx['value'],
    'data': tx['input'],
}

# replay the transaction locally:
try:
    w3.eth.call(replay_tx, tx.blockNumber - 1)
except Exception as e: 
    print(e)
1
  • so, e contains the error message that I can see on the block explorer? Commented Jan 4, 2023 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.