What is the difference between eth_estimateGas & eth_gasPrice? How does Polygon gas station compare to eth_getPrice?s & eth_gasPrice? Polygon gas station compared to eth_getPrice?

Created by Sourajyoti Gupta, Modified on Mon, 08 Jan 2024 at 12:00 PM by Sourajyoti Gupta

In Ethereum, after the EIP 1559, the gas price generally consists of two parts: the base fee and the priority fee, we can call this gas price the “final gas price” which the user pays for their transactions. 

The base fee is the minimum amount of gas that must be paid for the transaction to be included in a block, and the priority fee is an optional fee that the sender can add to incentivize miners to include the transaction in a block more quickly.


The eth_gasPrice method returns the current base fee for the Ethereum/Polygon network in wei (the smallest unit of ether) that a user needs to pay to include a transaction in the next block. 

The final gas price then includes this fetched base fee, which is a dynamically determined fee that is paid to miners for processing a transaction, as well as an optional priority fee that can be added to incentivize faster confirmation. 

Sharing a code snippet below:


const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getGas(priorityFee) {
    const priorityFeeWei = web3.utils.toWei(String(priorityFee), 'gwei');
    const baseFee = await web3.eth.getGasPrice();

    const totalFee = Number(priorityFeeWei) + Number(baseFee)
    return [totalFee, baseFee]
}

async function main() {
  // The priority fee is constant at 10 Gwei, the user can change it.
  const PRIORITY_FEE = 10;
  
  const [totalFee, baseFee] = await getGas(PRIORITY_FEE)
  const baseFeeGwei = web3.utils.fromWei(String(baseFee), 'gwei')
  console.log(`The priority fee: ${PRIORITY_FEE} Gwei.`)
  console.log(`The Polygon base fee at this moment is: ${Number(baseFeeGwei).toFixed(2)} Gwei.`)
  
  const gweiFee = web3.utils.fromWei(String(totalFee), 'gwei')
  console.log(`The total gas fee should be at least: ${Number(gweiFee).toFixed(2)} Gwei.`)
}

main()

On the other hand, the eth_estimateGas is a method that estimates the gas needed for a transaction to be executed. This method does not provide any information about the gas prices, which is what eth_gasPrice provides. It considers the transaction's complexity and the amount of data it involves. 

This is useful when sending transactions to ensure they will not run out of gas. In other words, eth_estimateGas estimates how much gas (unit) a transaction will consume, while the eth_gasPrice provides the current minimum gas price (base fee) required to pay for that unit of gas for that transaction.


Both polygon gas station and eth_getPrice are methods that provide current gas prices, while the eth_getPrice only returns the base fee the polygon gas station returns 3 objects 

  • safeLow

  • standard

  • fast

That the users can use. These response data for the polygon gas station contains 

  • maxPriorityFee

  • maxFee

  • baseFee

  • and other data.

 

For example:


The accuracy of these methods can vary depending on market conditions and network activity, so it's always a good idea to check multiple sources to get a better sense of the current gas price. 


Regarding our experience, we usually go with the Polygon gas station as it generally gives a lot more data than just the base fee. 


We are providing you with a simple code snippet where we utilize the data fetched from the Polygon gas station and estimate the transaction.


const { ethers, providers } = require("ethers");
const { Wallet } = require("@ethersproject/wallet");
const sampleTokenData = require("./ABI/SampleToken.json");
const { getGasDataFromNetwork } = require("./lib/gas.js");
require("dotenv").config();

async function sampleTokenTransfer() {
  // get private key from env
  const privateKey = process.env.PRIVATE_KEY;

  // initialize provider
  const provider = new providers.InfuraProvider(
    "matic",      // "maticmum" for mumbai testnet
    process.env.INFURA_PROJECT_KEY
  );

  // initialize signer
  const signer = new Wallet(privateKey, provider);

  // get contract address
  const contractAddress = sampleTokenData.address;

  // fetch signer nonce
  const nonce = await provider.getTransactionCount(signer.address);
  console.log("nonce: ", nonce);

  // initialize contract instance
  const sampleToken_ContractInstance = new ethers.Contract(
    contractAddress,
    sampleTokenData.abi,
    signer
  );

  // use signer to interact with contract instance
  const sampleToken_Contract = sampleToken_ContractInstance.connect(signer);

  // fetch "fast" gas data from polygon gas station network
  const data = await fetch("https://gasstation-mainnet.matic.network/v2");
  // for mumbai testnet "https://gasstation-mumbai.matic.today/v2"
  const dataJson = await data.json();
  const gas = dataJson["fast"];

  // convert priority fee and max fee from GWEI to WEI
  const priority = Math.trunc(gas.maxPriorityFee * 10 ** 9);
  const max = Math.trunc(gas.maxFee * 10 ** 9);
  console.log("using gasData", priority.toString(), max.toString());
  const maxFeePerGas = max.toString()
  const maxPriorityFeePerGas = priority.toString()
  console.log("maxFeePerGas: ", maxFeePerGas)
  console.log("maxPriorityFeePerGas: ", maxPriorityFeePerGas)

  // estimate gas for the transaction with fetched nonce and maxFee and maxPriorityFee
  const estimatedGas = await sampleToken_Contract.estimateGas.transfer(
    process.env.SENDER_ADDRESS,
    100,
    {
      nonce: nonce,
      gasLimit: 14_999_999,
      maxFeePerGas: maxFeePerGas,
      maxPriorityFeePerGas: maxPriorityFeePerGas,
    }
  );
  console.log(estimatedGas);
  // send the actual transaction with estimated gas, nonce and maxFee and maxPriorityFee
  const response = await sampleToken_Contract.transfer(
    process.env.RECEIVER_ADDRESS,
    process.env.TOKEN_AMOUNT_TO_TRANSFER_IN_WEI,
    {
      nonce: nonce,
      gasLimit: estimatedGas,
      maxFeePerGas: maxFeePerGas,
      maxPriorityFeePerGas: maxPriorityFeePerGas,
    }
  );
  console.log(response);
  await response.wait(2);
  console.log(response);
}

sampleTokenTransfer();

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article