githubEdit

eth_estimateGas - Polygon

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

The eth_estimateGas method generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain. The estimate may be significantly more than the amount of gas actually used by the transaction due to a variety of reasons including EVM mechanics and node performance.

Parameters

Parameter
Type
Required
Description

transaction

object

Yes

Transaction call object

blockParameter

string

No

Block number in hex, or "latest", "earliest", "pending" (default: "latest")

Transaction Object

Field
Type
Required
Description

from

string

No

The address the transaction is sent from

to

string

No

The destination address (required for non-contract creation)

gas

string

No

Upper limit of gas for the estimation (hex)

gasPrice

string

No

Gas price in wei (hex)

value

string

No

Value to send in wei (hex)

data

string

No

Contract code or encoded function call

Request

curl
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": "0x742d35Cc6634C0532925a3b844Bc9e7595f5bEb1",
        "to": "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0",
        "value": "0x0",
        "data": "0xa9059cbb000000000000000000000000d8dA6BF26964aF9D7eEd9e03E53415D37aA960450000000000000000000000000000000000000000000000056bc75e2d63100000"
    }],
    "id": "getblock.io"
}'

Response

Response Parameters

Field
Type
Description

jsonrpc

string

JSON-RPC version (2.0)

id

string

Request identifier

result

string

Estimated gas amount in hexadecimal (0x5208 = 21000 for simple transfer)

Use Case

The eth_estimateGas method is essential for:

  • Transaction preparation: Determine gas limit before sending transactions

  • Cost estimation: Calculate transaction costs for users

  • Contract interaction: Estimate gas for complex smart contract calls

  • Budget planning: Ensure sufficient funds for transaction execution

  • Error prevention: Detect transactions that would fail before sending

Example: Calculate Transaction Cost

Error Handling

Status Code
Error Message
Cause

403

Forbidden

Missing or invalid ACCESS-TOKEN

-32000

execution reverted

Transaction would fail

-32000

gas required exceeds allowance

Gas limit too low or infinite loop

-32602

Invalid params

Invalid transaction parameters

Web3 Integration

Last updated

Was this helpful?