eth_estimateGas - BSC

This method generates an estimate of how much gas is necessary to complete a transaction on the BNB Smart Chain. This is essential for setting appropriate gas limits and calculating transaction costs. BSC's low gas prices make accurate estimation important for cost optimization.

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 in wei
datastringNoContract call data

Request Example

cURL

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_estimateGas",
    "params": [{
        "from": "0x8D97689C9818892B700e27F316cc3E41e17fBeb9",
        "to": "0x55d398326f99059fF775485246999027B3197955",
        "value": "0x0",
        "data": "0xa9059cbb0000000000000000000000008626f6940e2eb28930efb4cef49b2d1f2c9c11990000000000000000000000000000000000000000000000000de0b6b3a7640000"
    }],
    "id": "getblock.io"
}'

request.js

javascriptconst axios = require('axios');

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

const payload = {
    jsonrpc: '2.0',
    method: 'eth_estimateGas',
    params: [{
        from: '0x8D97689C9818892B700e27F316cc3E41e17fBeb9',
        to: '0x55d398326f99059fF775485246999027B3197955',
        value: '0x0',
        data: '0xa9059cbb0000000000000000000000008626f6940e2eb28930efb4cef49b2d1f2c9c11990000000000000000000000000000000000000000000000000de0b6b3a7640000'
    }],
    id: 'getblock.io'
};

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

request.py

pythonimport requests
import json

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

payload = {
    "jsonrpc": "2.0",
    "method": "eth_estimateGas",
    "params": [{
        "from": "0x8D97689C9818892B700e27F316cc3E41e17fBeb9",
        "to": "0x55d398326f99059fF775485246999027B3197955",
        "value": "0x0",
        "data": "0xa9059cbb0000000000000000000000008626f6940e2eb28930efb4cef49b2d1f2c9c11990000000000000000000000000000000000000000000000000de0b6b3a7640000"
    }],
    "id": "getblock.io"
}

response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
gas_estimate = int(response.json()["result"], 16)
print(f"Gas Estimate: {gas_estimate}")

request.rs

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",
        "method": "eth_estimateGas",
        "params": [{
            "from": "0x8D97689C9818892B700e27F316cc3E41e17fBeb9",
            "to": "0x55d398326f99059fF775485246999027B3197955",
            "value": "0x0"
        }],
        "id": "getblock.io"
    });

    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": "0xc350"
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC version (2.0)
idstringRequest identifier
resultstringEstimated gas in hex (0xc350 = 50,000)

Use Cases

  • Calculate transaction costs before sending
  • Set appropriate gas limits for BEP-20 transfers
  • Budget gas for PancakeSwap swaps
  • Optimize DeFi 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: '0x8D97689C9818892B700e27F316cc3E41e17fBeb9',
    to: '0x55d398326f99059fF775485246999027B3197955',
    value: ethers.parseEther('0.1')
});
console.log('Gas Estimate:', gasEstimate.toString());

viem.js

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

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

const gasEstimate = await client.estimateGas({
    account: '0x8D97689C9818892B700e27F316cc3E41e17fBeb9',
    to: '0x55d398326f99059fF775485246999027B3197955',
    value: parseEther('0.1')
});
console.log('Gas Estimate:', gasEstimate);