raise_on_revert_middleware

Documentation for eth_defi.middleware.raise_on_revert_middleware function.

raise_on_revert_middleware(make_request, web3)[source]

Automatically show the transaction revert reason in Python traceback.

  • Designed to make writing unit tests more productive

  • Transaction will already revert in eth_estimateGas call unless you have manually set the gas limit for your transaction

  • If a transaction fails, this middleware display its revert reason in Python exception message

  • Tested with Anvil testing backend

  • May interfere with http_retry_request_with_sleep_middleware(), others, so don’t use in production

from eth_defi.middleware import revert_reason_middleware

# Fix the web3.py stock gas estimate middlware with smarted one
web3.middleware_onion.replace("gas_estimate", revert_reason_aware_buffered_gas_estimate_middleware)

# Now you check the revert reason as the following
Parameters
  • make_request (Callable[[web3.types.RPCEndpoint, Any], Any]) –

  • web3 (web3.main.Web3) –

Return type

Callable[[web3.types.RPCEndpoint, Any], Any]