1

Using Web3, we are able to estimate the amount of gas needed for a transaction

let estimatedGas = web3.eth.estimateGas({
    to: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
    data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"
})

contractObject.deploy().send({gas:estimatedGas, gasPrice: 2e9, ...})

Question: How can we estimate the gasPrice to use, instead of defining a static value like 2 Gwei?

1

You could use ETHGasStation.info by sending a GET request to this URL of their API:

https://ethgasstation.info/json/ethgasAPI.json

This yields a response like this:

{
  "average": 46,
  "fastestWait": 0.5,
  "fastWait": 0.7,
  "fast": 131,
  "safeLowWait": 2.9,
  "blockNum": 6545673,
  "avgWait": 2.6,
  "block_time": 15.095744680851064,
  "speed": 0.5118320026004425,
  "fastest": 400,
  "safeLow": 40
}

The average field contains the GasPrice you should use for an average transaction wait time. safeLow contains the minimum price you should use at the moment. If you're in a rush, use fastest.

1

I think that we can just simply use web3.eth.gasPrice to get the suggest gas price.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.