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: 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. Estimates are served directly from nodes, we're not doing anything special to the value so the rest of the network is likely seeing the same.

Parameters

  • Object - The transaction call object

    • from: DATA, 20 Bytes - (optional) The address the transaction is sent from.

    • to: DATA, 20 Bytes - The address the transaction is directed to.

    • gas: QUANTITY - (optional) Integer of the gas provided for the transaction execution. eth_estimateGas consumes zero gas, but this parameter may be needed by some executions.

    • gasPrice: QUANTITY - (optional) Integer of the gasPrice used for each paid gas.

    • value: QUANTITY - (optional) Integer of the value sent with this transaction

    • data: DATA - (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI

  • QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.

Returns

QUANTITY - the amount of gas used.

Request

const Web3 = require("web3");

async function main() {

	const Web3 = require('web3')
        const rpcURL = '1DLT-IP-ADDRESS' // Your RPC URL goes here
        const web3 = new Web3(rpcURL)
	
	// Query the blockchain (replace example parameters)
    	const estGas = await web3.eth.estimateGas({
	    from: "0xge61df",
	    to: "0x087a5c",
	    data: "0xa9059c",
	    gasPrice: "0xa994f8",
	  }) 

	// Print the output to console
  	console.log(estGas);
   }

main();

Result

{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0x5208" // 21000
}

Last updated