Simply put, “Gas” is the fee for performing an operation (such as a transaction or smart contract execution) on the Ethereum network.
While gas is the unit by which transaction costs are measured, it only exists as a unit of measurement and the actual cost must be paid for using Ether (ETH).
The reason for this is to separate the cost of computation from the value of Ether (ETH). Meaning that while the price of Ether (ETH) can fluctuate, the cost of performing transactions on the Ethereum network can remain steady.
Every operation takes a certain amount of gas to complete. This is divided into gas ‘cost’ and gas ‘price’.
‘Gas cost’ is the amount of gas required to complete a transaction. For instance, a standard transfer of Ether (ETH) requires 21,000 gas, while more complex transactions require more.
Users set what is known as a ‘gas limit’, which is the maximum amount of gas they are willing to spend on a transaction.
Applying a gas limit creates a safety net that protects users from burning through all their Ether (ETH) trying to perform an operation with errors or executing a faulty smart contract.
For example, setting a limit of 21,000 gas for a standard transaction ensures there is enough gas for the operation while preventing more from being used if an error occurs.
‘Gas price’ is the amount you are willing to pay for each unit of gas.
When setting gas prices, users are recommended to review current prices to understand how their transaction will be prioritized. Miners tend to prioritize transactions with higher gas prices and may outright ignore ones with too low a price.
So the total cost for an operation is calculated as:
Gas Used x Gas Price
For example, let’s say we wanted to send a friend some Ether (ETH). A standard transfer requires 21,000 gas. If we set the gas price as 6 Gwei (a unit of Ether), the transaction cost would be calculated as:
21,000 x 0.000000006 (6 Gwei)
= 0.000126 ETH