I m trying to estimate what is current gas price with Web3.js library on zkSync layer2 network however I m constantly getting same result: 250000000 for multiple days so I assume it s not working correctly. Is web3.js not working with zkSync? https://web3js.readthedocs.io/en/v1.10.0/web3-eth.html?highlight=getGasPrice#getgasprice
const Web3 = require("web3"); // Web3 library
const web3 = new Web3("https://mainnet.era.zksync.io");
let gasPrice = await web3.eth.getGasPrice();
getGasPrice()returns the median gas price (calculated from previous few blocks). https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#getgaspriceYou can retrieve that by:
As to why its
250000000, that's because that's thebaseFeePerGasset: https://github.com/matter-labs/foundry-zksync#get-latest-blockIf you want to estimate the gas price for a transaction you want to make, you can use the
estimateGasfunction. https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#estimategasYou can do something like:
The input to
estimateGasfunction is a transaction object.To add a bit further to this answer,
Which is why I recommended to use
estimateGas()function.