eth_estimateGas - Somnia

This method generates an estimate of how much gas is necessary to complete a transaction on the Somnia network. This is essential for setting appropriate gas limits. Somnia's IceDB provides deterministic performance, enabling more accurate gas estimation than traditional blockchains.

Parameters

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

Transaction Object

FieldTypeRequiredDescription
fromstringNoSender address
tostringNoRecipient address
gasstringNoGas limit
gasPricestringNoGas price
valuestringNoValue to send
datastringNoTransaction data

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"
    }
  ]
}'

JavaScript (Axios)

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));

Python

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())

Rust

rustuse reqwest::Client;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let payload = json!({
        "jsonrpc": "2.0",
        "id": "getblock.io",
        "method": "eth_estimateGas",
        "params": [{
            "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45",
            "to": "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199",
            "value": "0xde0b6b3a7640000"
        }]
    });

    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&payload)
        .send()
        .await?;

    let result: serde_json::Value = response.json().await?;
    println!("{:#?}", result);
    
    Ok(())
}

Response Example

Response (JSON)

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

Response Parameters

ParameterTypeDescription
resultstringGas estimate in hex (0x5208 = 21000 for simple transfer)

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.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());

Viem

javascriptimport { createPublicClient, http, parseEther } from 'viem';
import { somnia } from 'viem/chains';

const client = createPublicClient({
  chain: somnia,
  transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

const gasEstimate = await client.estimateGas({
  account: '0x742d35Cc6634C0532925a3b844Bc9e7595f8bB45',
  to: '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199',
  value: parseEther('1')
});
console.log(gasEstimate);