eth_estimateGas - Celo

This method generates an estimate of how much gas is necessary to complete a transaction on the Celo network. This is essential for setting appropriate gas limits. Celo's low gas costs make accurate estimation important for optimal fee management.

Use eth_estimateGas to calculate gas requirements before sending transactions to avoid out-of-gas errors and set appropriate gas limits.

Parameters

ParameterTypeRequiredDescription
transactionobjectYesTransaction call object
blockNumberstringNoBlock number in hex, or "latest" (default)

Request Example

cURL

bashcurl -X POST https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/ \
-H "Content-Type: application/json" \
-d '{
  "jsonrpc": "2.0",
  "id": "getblock.io",
  "method": "eth_estimateGas",
  "params": [
    {
      "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45",
      "to": "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199",
      "value": "0xde0b6b3a7640000"
    }
  ]
}'

estimateGas.js

javascriptconst axios = require('axios');

const url = 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/';

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

axios.post(url, payload, {
  headers: { 'Content-Type': 'application/json' }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));

estimate_gas.py

pythonimport requests
import json

url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"

payload = {
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "method": "eth_estimateGas",
    "params": [
        {
            "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45",
            "to": "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199",
            "value": "0xde0b6b3a7640000"
        }
    ]
}

response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
print(response.json())

Response Example

response.json

json{
  "jsonrpc": "2.0",
  "id": "getblock.io",
  "result": "0x5208"
}

Response Definition

FieldTypeDescription
resultstringEstimated gas amount in hexadecimal

Use Cases

  • Calculate transaction costs before sending
  • Set appropriate gas limits
  • Budget gas for complex contract interactions
  • Optimize transaction parameters
  • Prevent out-of-gas errors

Error Handling

Error CodeDescription
-32602Invalid params - malformed transaction object
-32603Internal error - execution reverted
-32000Execution error - insufficient funds or revert

SDK Integration

ethers-estimate.js

javascriptconst { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

const gasEstimate = await provider.estimateGas({
  from: '0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45',
  to: '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199',
  value: ethers.parseEther('1.0')
});
console.log(gasEstimate.toString());

contractkit-estimate.js

javascriptconst { newKit } = require('@celo/contractkit');

const kit = newKit('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

const gasEstimate = await kit.web3.eth.estimateGas({
  from: '0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45',
  to: '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199',
  value: kit.web3.utils.toWei('1', 'ether')
});
console.log(gasEstimate);