以太坊的任何一筆交易或操作,都需要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 |