eth_estimateGas

Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain. Note that the estimate may be significantly more than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.

Parameters

See eth_call parameters, except that all properties are optional. If no gas limit is specified geth uses the block gas limit from the pending block as an upper bound. As a result the returned estimate might not be enough to executed the call/transaction when the amount of gas is higher than the pending block gas limit.

Returns

QUANTITY - the amount of gas used.

Example

NOTE: In this example we are using public Ethereum endpoint. Ideally, for your own projects, you should use your own endpoint, which you can generate for free by connecting to your wallet here.

Query

NOTE: ethers used below is a well-known web3 library, check it out here.

import { ethers } from 'ethers';

const provider =
  new ethers
    .providers
    .JsonRpcProvider("https://www.noderpc.xyz/rpc-mainnet/public")

const info = async ()=> {
  const result = await provider.estimateGas({
    "from": "0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5",
    "to": "0xb69f8e3f4a4eef5d929de86653ab9768af093a34",
    "value": "0x186a0"})
  console.log(result);
}
info();

Result

{
    "type": "BigNumber",
    "hex": "0x5208"
}

Last updated