eth_gasPrice
方法返回以 wei 为单位的当前 gas 价格。此值代表最近区块的中位数 gas 价格,为开发者提供交易费用计算的可靠基准,并帮助用户避免因 gas 价格过低导致的交易失败。
此方法提供了一种简单的方式来查询当前网络 gas 价格。它不需要参数,返回一个表示 wei 单位 gas 价格的十六进制值。
以 wei 为单位的当前 gas 价格(十六进制)
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1DCD65000" // 8 Gwei
}
Gas 价格通常以这些单位讨论:
对于用户界面,将返回值转换为 Gwei 通常提供更易读的格式。例如,0x1DCD65000
(8 Gwei)比 8,000,000,000 wei 更容易理解。
// 计算标准转账 gas 成本的示例
const gasPrice = await provider.send('eth_gasPrice', []);
const gasPriceInGwei = parseInt(gasPrice, 16) / 1e9;
const standardTransferGas = 21000;
const costInEth = (parseInt(gasPrice, 16) * standardTransferGas) / 1e18;
eth_feeHistory
确定适当的 maxFeePerGas
和 maxPriorityFeePerGas
值