Skip to main content

Gas Estimation

The estimateGas interface estimates gas for write operations via eth_estimateGas. Use this to predict costs and detect reverts before sending transactions.
This uses the Contract pattern - a copyable implementation you add to your codebase.

Basic Usage

How It Works

Gas estimation simulates the transaction and returns the gas that would be used:

Revert Detection

If a transaction would revert, estimateGas throws:
Use this to validate transactions before sending:

Gas Buffers

Estimates are approximate. Add a buffer for safety:
20% buffer is a good default. Some complex transactions (DEX swaps, flash loans) may need higher buffers due to state changes between estimation and execution.

Estimation Options

Override sender or value:

Cost Calculation

Calculate transaction cost in ETH:
For EIP-1559:

Batch Estimation

Estimate multiple operations:

Common Patterns

Estimate Before Approval

Retry on Failure