Skip to content

Eth.estimateGas

Returns the gas a transaction would need, found by running it against the state of a block without sending it.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Eth.estimateGas({ from: FROM, to: TO, value: 10n ** 18n });
const gas = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// 21000n

A transfer of ether between two accounts costs 21000 gas. The examples below leave the transport out.

Put the calldata in data, as hex. Call.encode builds it from a call of a contract.

import { Effect } from "effect";
import { Address, Call, Erc20 } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const RECIPIENT = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const gas = Effect.gen(function* () {
// transfer(address,uint256): the selector, then one 32-byte word per argument
const { data } = yield* Call.encode(Erc20.at(TOKEN).transfer(RECIPIENT, 10n ** 18n));
return yield* Eth.estimateGas({ from: FROM, to: TOKEN, data });
});

Estimate, then send the same request with the result as its gas.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const sent = Effect.gen(function* () {
const request = { from: FROM, to: TO, value: 10n ** 18n };
const gas = yield* Eth.estimateGas(request);
return yield* Eth.sendTransaction({ ...request, gas });
});

A node or wallet estimates the gas itself when a request has no gas, so this step is for when you want the number: to show a cost, or to add a margin.

Effect<bigint, Method.Error, Transport>

The gas the transaction needs, in units of gas.

The transaction to estimate. Every field is optional, and an estimate uses these:

Field Type Description
from Address the account that would send it
to Address the recipient or the contract, left out to create a contract
value bigint wei sent with it
data Hex the calldata, or the creation code of a contract
gas bigint a ceiling for the estimate
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const gas = Eth.estimateGas({ from: FROM, to: TO, value: 1n });

The block whose state the estimate runs on: a number, a tag, or an object that names the block by hash.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const gas = Eth.estimateGas({ from: FROM, to: TO, value: 1n }, "pending");

State the estimate pretends is on chain, per account.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const gas = Eth.estimateGas(
{ from: FROM, to: TO, value: 10n ** 18n },
"latest",
{ [FROM]: { balance: 10n ** 19n } },
);
// 21000n

The account holds nothing on a local anvil. With the override the estimate succeeds, and without it the node rejects the request.

Header fields the estimate pretends its block has.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const gas = Eth.estimateGas(
{ from: FROM, to: TO, value: 1n },
"latest",
{},
{ time: 2_000_000_000n },
);
  • An estimate for one block. The state can change before the transaction is mined, and the gas it uses with it.
  • A revert fails the estimate. The error is the one Eth.call gives: an RpcError whose reason is ExecutionReverted, with the revert data.
  • gas is a ceiling. A request that needs more than its gas fails instead of returning a higher number.
  • No default block. Left out, the param is not sent and the node chooses the block: see Naming a block.
  • Params are positional. To pass a blockOverride without a state override, pass {} as stateOverride.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError ExecutionReverted the transaction would revert: reason.data is the revert data
RpcError TransactionRejected from does not hold the value
RpcError TransactionRejected the transaction needs more gas than the gas given
RpcError InvalidParams the block number is above the chain’s head
RpcError ResourceNotFound no block has the given blockHash
CodecError on: "params" a quantity is negative: nothing is sent

eth_estimateGas