Ethereum
eth_estimateGas
eth_estimateGas dry-runs a transaction through the EVM on Ethereum mainnet and tells you how much gas it would consume, without ever broadcasting it or spending ETH. The node executes your transaction object against current state, measures the gas the execution path actually touched, and returns that figure (usually with a small safety margin baked in). You use it to fill the gas limit field before signing — a transfer is famously 21,000, but a Uniswap swap or an NFT mint can run an order of magnitude higher, and only a simulation tells you the real number. Send the request to https://ethereum.therpc.io/YOUR_API_KEY (chain ID 1); the gas limit you set determines your worst-case cost in ETH, even though the actual charge reflects gas used.
Use cases
- Set the gas limit. Get the figure for the exact calldata you're about to send so the transaction carries enough gas to finish but doesn't overpay headroom.
- Price a deployment. Omit
toand pass deployment bytecode indatato learn the gas a new contract's constructor will burn before you commit ETH to it. - Pre-flight success. Because a doomed transaction reverts during simulation and errors out, a clean estimate is a strong signal the real send will land.
- Wallet fee display. Multiply the estimate by a fee per gas drawn from
eth_feeHistoryto show users an ETH cost before they approve.
Parameters
| # | Name | Type | Required | Description |
|---|---|---|---|---|
| 1 | transaction | object | Yes | Transaction object. `to` is required for calls; omit `to` for contract deployments. Optional: `from`, `gas`, `gasPrice`/`maxFeePerGas`/`maxPriorityFeePerGas`, `value`, `data`. |
| 2 | blockTag | string | No | Block state to simulate against. Defaults to `latest`.Default: latest |
Response
| Type | Description |
|---|---|
| string | Hex-encoded gas estimate. The node typically adds a small safety buffer above the raw simulation result. |
Example request
Try it live in the Ethereum playground.
Errors & troubleshooting
| Code | Message | Cause |
|---|---|---|
3 | execution reverted | The simulated transaction reverted; the estimate cannot be produced. |
-32000 | gas required exceeds allowance | Estimated gas exceeded the block gas limit or the supplied gas cap. |
-32602 | invalid argument | Malformed transaction object. |
Common pitfalls
- An estimate is a measurement, not a promise. Pad it — a ×1.2 buffer is common — so a contract whose gas use depends on a branch you didn't hit during simulation doesn't run out of gas and revert on chain, eating your ETH for nothing.
- If the simulated transaction reverts, you get an error (code
3, "execution reverted") instead of a number. Treat that as "this would fail," not as a transient glitch to retry. - State drifts. Between your estimate and inclusion a few 12-second slots later, someone else's transaction can change storage that alters your gas path — a first-time ERC-20 approval (cold SSTORE) costs far more than a repeat one, for instance.
Supported networks
- Mainnet — Chain ID: 1
- Sepolia — Chain ID: 11155111
See also
Parameters
Transaction object. `to` is required for calls; omit `to` for contract deployments. Optional: `from`, `gas`, `gasPrice`/`maxFeePerGas`/`maxPriorityFeePerGas`, `value`, `data`.
"latest" | "pending" | hex block number