简体   繁体   English

估计 ETH 转账和 ERC20 转账 gas 总是返回相同的值

[英]Estimating ETH transfer and ERC20 transfer gas always returns same value

I am trying to estimate erc20 transfer fee and eth transfer fee.我正在尝试估算 erc20 转账费和 eth 转账费。 However, the gasestimate always return the same value for both ethTransferGasEstimate and erc20TransferGasEstimate然而,gasstimate 总是为 ethTransferGasEstimate 和 erc20TransferGasEstimate 返回相同的值

  • ethTransferGasEstimate always = 21000 ethTransferGasEstimate 总是 = 21000
  • erc20TransferGasEstimate always = 68408 erc20TransferGasEstimate 始终 = 68408
     [ ethTransferGasEstimate, erc20TransferGasEstimate, feeHistory, feeHistoryPendingBlock ] = await Promise.all([
                await web3.eth.estimateGas(ethTransferCallData),
                await usdc_contract.estimateGas.transfer(TO_ADDRESS_ETH_MAINNET, 100,{
                    from: FROM_ADDRESS_ETH_MAINNET
                }),
                await web3.eth.getFeeHistory(10, "latest",[10, 50, 90]),
                await web3.eth.getFeeHistory(1, "pending",[10, 50, 90])
            ])

const baseFeePerGas = parseInt(feeHistoryPendingBlock.baseFeePerGas,16)
            const rewardPerGas = feeHistory.reward.map(b => parseInt(b[1],16)).reduce((a, v) => a + v);
            const priorityFeePerGasEstimate = Math.round(rewardPerGas/feeHistory.reward.length);
            const ethTransferFee = ((baseFeePerGas+priorityFeePerGasEstimate)*ethTransferGasEstimate)/10**18
            const erc20TransferFee = ((baseFeePerGas+priorityFeePerGasEstimate)*erc20TransferGasEstimate.toNumber())/10**18
            
            console.log("--------Ethers.js----------")
            console.log("gas estimate eth transfer",ethTransferGasEstimate.toString())
            console.log("total fee", ethTransferFee)
            console.log("gas estimate usdc_contract transfer", erc20TransferGasEstimate.toString())
            console.log("total fee", erc20TransferFee)

Each low-level operation (read from memory, write to storage, ...) of the contract has a predetermined price in gas units.合约的每个低级操作(从 memory 读取,写入存储,...)都有一个预先确定的 gas 单位价格。 When the estimator adds up all of these operations, you get the amount of gas units in your ethTransferGasEstimate and erc20TransferGasEstimate variables.当估算器将所有这些操作相加时,您会在ethTransferGasEstimateerc20TransferGasEstimate变量中获得气体单位的数量。

Note: Some contract logic and the resulting final list of operations might depend on data that is not known before executing the transaction, that's why it's just an estimation.注意:一些合约逻辑和最终的操作列表可能取决于执行交易之前未知的数据,这就是为什么它只是一个估计。

Gas units are then automatically bought for ETH - this is the transaction fee.然后会自动为 ETH 购买气体单位——这是交易费用。 Their price is market driven, you can see the current prices for example at https://etherscan.io/gastracker or you can get your node provider's estimation of current market price with provider.getGasPrice() .他们的价格是市场驱动的,您可以在https://etherscan.io/gastracker中查看当前价格,或者您可以使用provider.getGasPrice()获取节点提供商对当前市场价格的估计。

Currently, the market gas price is around 7 Gwei (= 7 million wei) per each gas unit.目前,市场 gas 价格约为每 gas 单位 7 Gwei(= 700 wei)。 So with this price, the ETH transfer would cost you 21,000 gas units == 147,000,000,000 wei == 0.000000147 ETH.因此,以这个价格,ETH 转移将花费你 21,000 个气体单位 == 147,000,000,000 wei == 0.000000147 ETH。 And the ERC20 transfer would currently cost you 68,408 gas units == 478,856,000,000 wei == 0.000000478856 ETH. ERC20 转账目前将花费您 68,408 个气体单位 == 478,856,000,000 wei == 0.000000478856 ETH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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