eth_estimateGas
https://api.triport.io/v1/ethereumEstimates the amount of gas a transaction will consume, without broadcasting it to the network.
eth_estimateGas runs a transaction against the current state and returns a
hex-encoded estimate of the gas it would consume, without submitting the
transaction or changing any state. Use it to set the gas limit before signing
and sending a transaction with eth_sendRawTransaction, or to check whether a
call would revert.
The estimate is an upper bound derived from a binary search over a simulated
execution; it is not guaranteed to equal the gas the transaction actually
uses once mined. State can change between the estimate and inclusion (storage
slots, balances, nonces), and some contracts branch on gasleft(), so the real
gas used may be lower — and in rare cases a transaction that estimated cleanly
can still revert. A common practice is to add a small safety margin (for
example 10–20%) on top of the returned value when setting the transaction's gas
limit.
If the simulated execution reverts, the call returns an error rather than a gas value; the revert reason, when available, is surfaced in the error data.
Parameters
Positional params array: [transaction, blockTag?].
transaction (, required)objectfromstring (20-byte hex address)optionaltostring (20-byte hex address)optionaldatastring (hex)optionalvaluestring (hex quantity)optionalgasstring (hex quantity)optionalgasPricestring (hex quantity)optionalblockTag (optional)objectblockTagstringoptionallatest, pending, earliest, safe, or finalized. Defaults to latest.Response
Response fields
| Field | Type | Description |
|---|---|---|
result | string (hex quantity) | Estimated gas the transaction would consume. 0x5208 is 21000, the cost of a plain value transfer. |
Errors
| Code | Meaning | When it happens |
|---|---|---|
-32001 | trial_expired | The API key's trial period has ended; upgrade to a paid tier to continue. |
-32003 | rate_limited | Requests exceeded the tier's RPS limit (10 RPS on free). Retry after backing off. |
Errors follow the shared JSON-RPC error envelope — see Errors for the full structure and handling guidance.
Examples
JavaScript (fetch)
const res = await fetch("https://api.triport.io/v1/ethereum", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.TRIPORT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "eth_estimateGas",
params: [
{
from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
to: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
value: "0x9184e72a",
},
],
}),
});
const { result } = await res.json();
const gas = parseInt(result, 16); // 21000TypeScript SDK (@triport/sdk)
import { Triport } from "@triport/sdk";
const triport = new Triport({ apiKey: process.env.TRIPORT_API_KEY! });
const gasHex = await triport.ethereum.estimateGas({
from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
to: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
value: "0x9184e72a",
});
const gas = parseInt(gasHex, 16); // 21000Python (triport-sdk)
import os
from triport import Triport
triport = Triport(api_key=os.environ["TRIPORT_API_KEY"])
gas_hex = triport.ethereum.estimate_gas(
{
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"value": "0x9184e72a",
}
)
gas = int(gas_hex, 16) # 21000Notes
- The returned value is hex-encoded — decode with
parseInt(result, 16)(JS) orint(result, 16)(Python) before use. - Add a safety margin (≈10–20%) when applying the estimate as a transaction's
gaslimit; the actual gas used at inclusion time can differ from the estimate. - A reverting simulation returns an error, not a gas figure — use this to detect calls that would fail before paying to broadcast them.
- Related methods:
eth_call(read-only simulation returning result data),eth_gasPriceandeth_feeHistory(current pricing), andeth_sendRawTransaction(broadcast a signed transaction).