Example

Let's take an example of a transaction where you want to transfer 0.1 ETH to your friend. The following are the values for the transaction fields you would have to fill while creating a transaction:

Field name Value
From 0xff899af34214b0d777bcd3c230ef637b763f0b01
To 0xc4fe5518f0168da7bbafe375cd84d30f64cda491
Value 0.1 ether
Gas limit 30,000
Gas price 0.000000021 ether (21 gwei)

 

You can also refer to Chapter 4, Learning MetaMask and Remix, where we have demonstrated how to use MetaMask to initiate transactions. 

Once the transaction is sent and confirmed on blockchain, you can see its status on block explorer (etherscan.io) as follows:

Screenshot from etherscan.io of the transaction

Let's calculate the values using these formulas: 

  • TxFeeMax = 30,000 * 0.000000021 = 0.00063 ether
  • TxFeePaid = 21,000 * 0.000000021 = 0.000441 ether
  • TxFeeReturned = 0.00063 - 0.000441 = 0.000189 ether
  • GasUnused = 30,000 - 21,000 = 9,000 units of gas
  • BalanceRequiredAtTransactionInitiation = 0.1 + 0.00063 = 0.10063 ether

Let's go through the preceding calculations. When you initiate the preceding ether transfer transaction, it must have a balance of 0.10063 ether. If the balance in your wallet is lower than 0.10063 ether, then you will not be allowed to initiate transactions and this will be rejected by the client itself as it's an invalid transaction.

If you have sufficient balance and the transaction is initiated, then, along with your transaction, the BalanceRequiredAtTransactionInitiation amount will be locked. So, in this example, 0.10063 ether is locked.

Now, your transaction is processed, 21,000 gas has been consumed in order to execute the transaction on the EVM, and the transaction executed successfully. As it consumed only 21,000 gas, it refunds 9,000 units of gas. This means 0.000189 ether will be refunded back to your wallet and you have paid 0.000441 ether for your transaction processing.

You can remember this GasLimit and GasPrice with a car and fuel example. Let's say you want to go from city A to city B and the distance between these two cities is 500 km. Your car fuel average is 20 km/liter (GasRequired). There is no fuel station on the way when you travel, so you would have to get it filled before you start. You need 500/20 = 25 liters (TxFeeRequired) of fuel to reach city B. If you filled your car fuel tank with 30 liters (TxFeeMax), then you would successfully reach city B and you will still have 5 liters (GasUnused) left with you that you can use later on. However, if you had filled it with 20 liters when you started the journey, then you would have stopped at 400 km and you couldn't have done anything. This would be called a failed transaction and your gas/fuel is consumed so there's no gas refund.

An ether transfer requires a fixed 21,000 gas limit per transaction. If you also include data along with the ether value, it may require a greater gas limit to execute the transaction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.65.208