简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM