ethers.js:provider.getGasPrice()

Posted

技术标签:

【中文标题】ethers.js:provider.getGasPrice()【英文标题】:ethers.js: provider.getGasPrice() 【发布时间】:2021-08-12 13:02:15 【问题描述】:

ethers.js 中,您可以使用provider.getGasPrice() 来获得您应该支付的gas 的最佳估计。有什么简单的方法我可以调用或转换到这个值,以获得相当于“快速”交易而不是“平均”速度交易的东西吗?

【问题讨论】:

【参考方案1】:

getGasPrice() 查询您的提供者 JSON-RPC 方法 eth_gasPrice 并简单地代理您的提供者返回的任何内容。


为了计算“快速”和“平均”速度,您需要定义您认为这些指标的含义。例如,您可以收集最近 20 个区块(大约 5 分钟)内的所有交易,并在历史数据中查找百分位数:

gas 价格的前 10% 是“快速” gas 价格的前 50% 是“平均”

您可以使用getBlockWithTransactions()ethers.js 方法在一个块中获取交易。在每笔交易中,您都在寻找 gasPrice 属性。

【讨论】:

以上是关于ethers.js:provider.getGasPrice()的主要内容,如果未能解决你的问题,请参考以下文章

ethers.js:provider.getGasPrice()

如何将 ethers.js 与元掩码连接起来?

区块链-前端交互第四篇:认识 ethers.js并运行测试代码

区块链-前端交互第四篇:认识 ethers.js并运行测试代码

如何以编程方式将 ethers.js 库与 Rinkeby 连接?

Ethers.js,向智能合约汇款(接收功能)