以太坊gas 费率计算

以太坊的任何一笔交易或操作,都需要gas,如果gas 不足,会导致交易失败,但消耗的gas 不会返回.

实际花费的gas 我们不知道,只能计算预期的gas 费率,txFee = GAS_PRICE * GAS_LIMIT;

例如:

 BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);//这里表示转账的gas 价格,0.000000022 Ether (22 Gwei)
        BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);

这里GAS_PRICE  的单位是wei,换算成ether 是 0.000000022 * 4300000 = 0.0946 这里设置预期的txFee 是 0.0946ether ,实际花费可在etherscan 根据事务id ,可以查看到使用的Gas Used By Txn 有多少,然后用 Gas Used By Txn * GAS_PRICE(换算成ETH) = Actual Tx Cost/Fee 实际事务花费gas,多余的gas会退回

以太坊中代币数量的计量单位说明

单位   换算关系(wei)
wei 1 1
Kwei (babbage) 1e3 wei 1,000
Mwei (lovelace) 1e6 wei 1,000,000
Gwei (shannon) 1e9 wei 1,000,000,000
microether (szabo) 1e12 wei 1,000,000,000,000
milliether (finney) 1e15 wei 1,000,000,000,000,000
ether 1e18 wei 1,000,000,000,000,000,000

猜你喜欢

转载自blog.csdn.net/kzcming/article/details/81531131
今日推荐