eth_estimateGas
Estimates the gas necessary to execute a transaction on MegaETH.
Why MegaETH? Build on the first real-time blockchain with sub-millisecond latency and 100,000+ TPS with sub-millisecond transaction streaming with 100,000+ sustained TPS and full EVM compatibility.
Use Cases#
The eth_estimateGas method is essential for:
- Transaction preparation - Set appropriate gas limits
- Cost estimation - Calculate transaction costs before sending
- Error detection - Identify reverts before spending gas
- DeFi operations - Estimate costs for high-frequency trading, real-time gaming, instant payments, and latency-sensitive applications
Gas Limit Restriction#
Gas Limit on MegaETH
On MegaETH, eth_estimateGas requests are subject to a maximum gas limit of 10,000,000. Complex transactions exceeding this limit should be optimized or restructured.
For transactions requiring more than 10M gas:
// Split complex operations into multiple transactions
const operation1 = await contract.step1();
await operation1.wait();
const operation2 = await contract.step2();
await operation2.wait();
Parameters#
| Parameter | Type | Required | Description |
|---|---|---|---|
from | DATA | No | Sender address |
to | DATA | No | Recipient address |
gas | QUANTITY | No | Gas limit |
gasPrice | QUANTITY | No | Gas price |
value | QUANTITY | No | Value in wei |
data | DATA | No | Transaction data |
Request#
{
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [{
"from": "0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f",
"to": "0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f",
"value": "0x1"
}],
"id": 1
}
Returns#
| Type | Description |
|---|---|
QUANTITY | Estimated gas amount in hexadecimal |
Response#
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x5208"
}
Note: 0x5208 = 21000 gas (standard ETH transfer)
Code Examples#
- cURL
- JavaScript
- Python
- Go
curl -X POST https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [{
"from": "0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f",
"to": "0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f",
"value": "0x1"
}],
"id": 1
}'
import { JsonRpcProvider, parseEther } from 'ethers';
const provider = new JsonRpcProvider('https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY');
// Estimate simple transfer
async function estimateTransfer(to, value) {
const gasEstimate = await provider.estimateGas({
to: to,
value: parseEther(value)
});
console.log('Estimated gas:', gasEstimate.toString());
return gasEstimate;
}
// Estimate contract call
async function estimateContractCall(contract, method, args) {
const gasEstimate = await contract[method].estimateGas(...args);
console.log('Estimated gas:', gasEstimate.toString());
// Add 20% buffer for safety
return gasEstimate * 120n / 100n;
}
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY'))
def estimate_transfer(to, value_in_ether):
gas_estimate = w3.eth.estimate_gas({
'to': to,
'value': w3.to_wei(value_in_ether, 'ether')
})
print(f'Estimated gas: {gas_estimate}')
return gas_estimate
def estimate_contract_call(contract, method, args):
func = getattr(contract.functions, method)
gas_estimate = func(*args).estimate_gas()
# Add 20% buffer
return int(gas_estimate * 1.2)
# Estimate simple transfer
gas = estimate_transfer('0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f', 0.1)
package main
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://api-megaeth-mainnet.n.dwellir.com/YOUR_API_KEY")
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress("0x0E0f4Dd25ae8AB20E1583D9E8EDFf319A88e1d3f")
msg := ethereum.CallMsg{
To: &toAddress,
Value: big.NewInt(1000000000000000000),
}
gasLimit, err := client.EstimateGas(context.Background(), msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Estimated gas: %d\n", gasLimit)
}
Error Handling#
| Error Code | Message | Description |
|---|---|---|
| -32000 | Execution reverted | Transaction would fail |
| -32602 | Invalid params | Invalid transaction parameters |
Tip: If estimation fails, the transaction would likely revert if sent.
Related Methods#
eth_gasPrice- Get current gas priceeth_sendRawTransaction- Send transaction