Skip to main content
contract.estimateGas.<fn>(...args, options?) simulates a nonpayable or payable function via eth_estimateGas and returns the estimated gas units as a bigint. If the simulated execution reverts, the call rejects before any transaction is broadcast.

Basic Usage

Always pass { from } when estimating functions that check msg.sender, token balances, or allowances. Without from, nodes simulate from the zero address (0x0000...0000), causing balance checks to revert.

Buffers and Revert Detection

Estimates reflect state at the latest block. Storage transitions, cold-to-warm slot differences, or concurrent mempool activity can increase actual execution gas. Apply an integer safety multiplier before sending:

Payable Calls

Pass { from, value } for functions that require msg.value:

Computing EIP-1559 Cost Budgets

Combine eth_estimateGas with eth_getBlockByNumber('latest', false) and eth_maxPriorityFeePerGas to compute an upper bound on transaction cost in wei:

Retrying Transient Estimation Failures

Wrap estimation in retryWithBackoff from @tevm/voltaire/utils, skipping retries on execution reverts (code === 3 or execution reverted):