aroooo
aroooo

Reputation: 5076

ethers.js: provider.getGasPrice()

In ethers.js you can use provider.getGasPrice() to get a best-guess estimate of the gas you should pay. Is there anything simple I can call or transform onto this value in order to get what an equivalent of a "Fast" transaction would be instead of an "Average" speed transaction?

Upvotes: 3

Views: 14350

Answers (2)

Kane Chan
Kane Chan

Reputation: 41

Maybe right now you have got the answer:

  • Before (ethers v5) you use: const gasPrice = await provider.getGasPrice()
  • Now (ethers v6): const gasPrice = ((await provider.getFeeData()).gasPrice

Upvotes: 3

Petr Hejda
Petr Hejda

Reputation: 43581

The getGasPrice() queries your provider JSON-RPC method eth_gasPrice and simply proxies whathever your provider returned.


In order to calculate the "Fast" and "Average" speed, you'll need to define what you consider these metrics. For example you can collect all transactions within the last 20 blocks (approx. 5 minutes) and look for percentiles in the historic data:

  • Top 10% percentil of gas prices is "Fast"
  • Top 50% percentil of gas prices is "Average"

You can get transactions in a block using the getBlockWithTransactions() ethers.js method. In each transaction you're looking for the gasPrice property.

Upvotes: 4

Related Questions