eth_estimateGas

Ethereum RPC

Overview

eth_estimateGas method in Ethereum's JSON-RPC API is used to estimate the amount of gas that would be required to execute a transaction without actually sending it. This method is particularly useful for determining the appropriate gas limit for a transaction before it is submitted to the network, helping to avoid out-of-gas errors and ensuring that transactions are processed efficiently.

Parameters

eth_estimateGas method accepts a single parameter, which is an object containing the details of the transaction you wish to estimate. The object can include the following fields:

NameTypeRequiredDescription
fromStringYesThe address that the transaction is sent from.
toStringYesThe address the transaction is sent to.
gasStringNoThe maximum amount of gas provided for the transaction.
gasPriceStringNoThe price of gas in wei.
valueStringNoThe amount of ether to send in the transaction, in wei.
dataStringNoThe data payload of the transaction, typically used for contract function calls or contract deployment.
nonceStringNoA fake nonce to set for the account before executing the call.
codeStringNoThe code to be executed.
stateStringNoThe state to be used for the execution.
stateDiffStringNoThe state diff to be used for the execution

Returns

The response from the eth_estimateGas method is a single value representing the estimated amount of gas that would be used by the transaction. This value is returned as a hexadecimal string.

Request Example

curl --location 'https://api.tatum.io/v3/blockchain/node/ethereum-mainnet/API_KEY/' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {API_KEY}' \
--data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_estimateGas",
  "params": [
    {
      "from": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
      "to": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
      "value": "0xde0b6b3a7640000",
      "data": "0x606060"
    }
  ]
}'

// yarn add @tatumio/tatum

import { TatumSDK, Ethereum, Network } from '@tatumio/tatum'

const tatum = (await TatumSDK.init) < Ethereum > { network: Network.ETHEREUM }

const estimate = await tatum.rpc.estimateGas({
  from: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
  to: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
  value: '0xde0b6b3a7640000',
  data: '0x606060',
})

await tatum.destroy() // Destroy Tatum SDK - needed for stopping background jobs