For the complete documentation index, see llms.txt. This page is also available as Markdown.

eth_estimateGas - Optimism

Example code for the eth_estimateGas json-rpc method. Сomplete guide on how to use eth_estimateGas json-rpc in GetBlock.io Web3 documentation.

This method simulates a transaction and returns an estimate of the L2 gas that would be consumed. The estimate may be higher than the actual gas used because it accounts for the worst-case execution path.

On Optimism, this only estimates L2 execution gas. The total transaction cost also includes L1 data fees. For a complete cost estimate, use the GasPriceOracle contract or the Optimism SDK.

Parameters

Parameter
Type
Description
Required

transaction

Object

Transaction call object with from (optional), to (optional), gas (optional), gasPrice (optional), value (optional), and data (optional) fields.

Yes

block

QUANTITY|TAG

(Optional) Block number in hex, or 'latest', 'earliest', 'pending'.

No

Request. Sample

curl --location --request POST 'https://go.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_estimateGas",
    "params": [{"from": "0x742d35Cc6634C0532925a3b844Bc9e7595f7bD5e", 
                "to": "0x8ba1f109551bD432803012645Ac136dff489ca2c", "value": "0xde0b6b3a7640000"}],
    "id": "getblock.io"
}'
const axios = require('axios');

const url = "https://go.getblock.io/<ACCESS-TOKEN>/";
const headers = { "Content-Type": "application/json" };

const payload = {
    jsonrpc: "2.0",
    method: "eth_estimateGas",
    params: [{"from": "0x742d35Cc6634C0532925a3b844Bc9e7595f7bD5e", "to": "0x8ba1f109551bD432803012645Ac136dff489ca2c", "value": "0xde0b6b3a7640000"}],
    id: "getblock.io"
};

axios.post(url, payload, { headers })
    .then(response => {
        if (response.status === 200) {
            console.log("eth_estimateGas result:", response.data.result);
        } else {
            console.error("Error:", response.status, response.statusText);
        }
    })
    .catch(error => {
        console.error("Error:", error.response ? error.response.data : error.message);
    });

Response

A successful response returns the following:

Response Parameters

  • id: A unique request identifier, matching the id sent in the request body.

  • jsonrpc: Specifies the use of JSON-RPC version 2.0.

  • result: The estimated L2 gas amount as a hexadecimal string. 0x5208 equals 21,000 gas (standard ETH transfer).

Use Case

The eth_estimateGas method is commonly used for:

  • Transaction cost estimation

  • Gas limit setting

  • Pre-transaction validation

  • DApp UX optimization

Error handling

Status Code
Error Message
Cause

404

Not Found

Missing or invalid ACCESS_TOKEN.

-32000

insufficient funds for transfer

Integration with Web3

The eth_estimateGas can help developers:

  • Allows dApps to simulate actions safely

  • Helps builders understand live contract state without spending gas

  • Useful for DeFi dashboards, explorers, and analytics tools

  • Enables wallets to preview costs before sending a transaction

  • Supports trustless frontends because the estimation is done by the blockchain directly

  • Test interactions like swaps, mints, or transfers without actual execution

Last updated

Was this helpful?