eth_estimateGas - Base
Example code for the eth_estimateGas JSON-RPC method. Complete guide on how to use eth_estimateGas JSON-RPC in GetBlock Web3 documentation.
The eth_estimateGas method generates and returns an estimate of how much gas is necessary to allow a transaction to complete. This estimate may be significantly more than the actual gas used due to EVM mechanics and node performance variations.
Parameters
transaction
object
Yes
Transaction call object
blockParameter
string
No
Block number in hex, or "latest", "earliest", "pending"
Transaction Object
from
string
No
20-byte sender address
to
string
No
20-byte recipient address (optional for contract creation)
gas
string
No
Gas limit (hex)
gasPrice
string
No
Gas price in wei (hex)
value
string
No
Value to send in wei (hex)
data
string
No
Transaction data (hex)
Request
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": "0x742d35Cc6634C0532925a3b844Bc9e7595f5bE21",
"to": "0x4200000000000000000000000000000000000006",
"data": "0xa9059cbb0000000000000000000000001234567890123456789012345678901234567890000000000000000000000000000000000000000000000000016345785d8a0000"
}],
"id": "getblock.io"
}'const axios = require('axios');
const response = await axios.post('https://go.getblock.io/<ACCESS-TOKEN>/', {
jsonrpc: '2.0',
method: 'eth_estimateGas',
params: [{
from: '0x742d35Cc6634C0532925a3b844Bc9e7595f5bE21',
to: '0x4200000000000000000000000000000000000006',
data: '0xa9059cbb0000000000000000000000001234567890123456789012345678901234567890000000000000000000000000000000000000000000000000016345785d8a0000'
}],
id: 'getblock.io'
}, {
headers: { 'Content-Type': 'application/json' }
});
const gasEstimate = parseInt(response.data.result, 16);
console.log('Gas Estimate:', gasEstimate);Response
Response Parameters
jsonrpc
string
JSON-RPC protocol version ("2.0")
id
string
Request identifier matching the request
result
string
Estimated gas required in hex
Use Cases
Gas Limit Setting: Determine appropriate gas limit for transactions
Cost Estimation: Calculate transaction costs before sending
UI Display: Show estimated fees to users before confirmation
Transaction Validation: Pre-validate transactions will succeed
Batch Processing: Estimate gas for multiple operations
Error Handling
-32602
Invalid params
Invalid transaction object
-32603
Internal error
Estimation failed
3
Execution reverted
Transaction would revert
-32000
Execution error
Insufficient balance or other failure
Web3 Integration
Last updated
Was this helpful?