assert_transaction_success_with_explanation

Documentation for eth_defi.trace.assert_transaction_success_with_explanation function.

assert_transaction_success_with_explanation(web3, tx_hash, RaisedException=<class 'eth_defi.trace.TransactionAssertionError'>, tracing=False)[source]

Checks if a transaction succeeds and give a verbose explanation why not..

Designed to be used on Anvil backend based tests.

If it’s a failure then print

  • The revert reason string

  • Solidity stack trace where the transaction reverted

Example usage:

tx_hash = contract.functions.myFunction().transact({"from": fund_owner, "gas": 1_000_000})
assert_transaction_success_with_explaination(web3, tx_hash)

Example output:

E           AssertionError: Transaction failed: AttributeDict({'hash': HexBytes('0xaa70b2f76ad9f32f7c722390535d5a806b4d815f3d8d460e5d18cdba3b1c8c2d'), 'nonce': 2, 'blockHash': HexBytes('0x1d2a1d36185bebb373639e1eb4ddbe9f7f3347fa6dd7bcbbe5e5905fe6a1f4ed'), 'blockNumber': 3, 'transactionIndex': 0, 'from': '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'to': '0x5FbDB2315678afecb367f032d93F642f64180aa3', 'value': 0, 'gasPrice': 768647811, 'gas': 500000, 'input': '0x25ad8c83000000000000000000000000e7f1725e7734ce288f8367e1bb143e90bb3f0512', 'v': 1, 'r': HexBytes('0x43336f08be93aec7ecf456c724d3c29c6cebc589ab3fe6199ee783a627bbcda8'), 's': HexBytes('0x74002a6cdd84b81932e36ac0725591460b09eaaa6b0dd615c0c5d43171467c8a'), 'type': 2, 'accessList': [], 'maxPriorityFeePerGas': 0, 'maxFeePerGas': 1768647811, 'chainId': 31337})
E           Revert reason: execution reverted: Big bada boom
E           Solidity stack trace:
E           CALL: RevertTest.revert2(second=0xe7f1725e7734ce288f8367e1bb143e90bb3f0512) [3284 gas]
E           └── CALL: RevertTest2.boom() [230 gas]

See also print_symbolic_trace().

Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • tx_hash (hexbytes.main.HexBytes | str) –

    A transaction (mined/not mined) we want to make sure has succeeded.

    Gas limit must have been set for this transaction.

  • RaisedException – Raise a custom exception instead of TransactionAssertionError.

  • tracing (bool) – Force turn on transaction tracing to use in e.g testing.

Raises

TransactionAssertionError – Outputs a verbose AssertionError on what went wrong.

Return tx_receipt

Output transaction receipt if no error is raised

Return type

web3.types.TxReceipt