How do Ethereum transactions work? Gas price/limit explained.
Ethereum is a platform designed to store information, run DAPPS (Decentralized Apps) and execute smart contracts for just about anything, on an open source public blockchain using EVM (Ethereum Virtual Machine) software.
Anyone can create a DAPP to run on Ethereum, for example CryptoKitties is such a Dapp that had taken the crypto world by storm EOY 2017. So how can the blockchain be protected from bad code? What if a script was executed to run in a continuous loop, potentially permanently stalling mining of the blockchain? This is where GAS comes into play.
Transaction (TX) fees and Gas
The Ethereum Network is powered by Ether (price ticker ETH), the native cryptocurrency for the Ethereum Blockchain (link). In order to deploy a contract or execute an operation it must first be fueled by ETHER. The price is usually set by the miners and you can agree to pay this price and ensure your transaction is included in the next Block to be mined, or you can opt to pay more, or to pay less. Paying more will bump your transaction/event to the top and ensure it is fast-tracked in the next block, but pay too little, and the miners may decide not to mine it at all and your transaction will likely not be executed.
But how does this prevent miners being caught in a continuous loop from bad code? Gas is calculated based on how much work is needed to be done to process your transaction, or execute your contract. You could think of Gas as the fuel in an car’s engine and the car is the Ethereum Network; the more miles the car has to do drive (the more calculations have to be made), the more “liters” of fuel (Gas) is needed. The PRICE of these “liters” of fuel/GAS is decided by market fluctuation (in other words: the demand for transactions on the Ethereum Network) and paid for in ETHER to the Miners.
If you do not buy enough Gas/Ether to fuel your transaction it will run out of fuel and it will stop. You will not receive your ETH back, as the miners have still done the work and still need to be paid; they will still collect the fee. Your transaction has now FAILED, it will still be recorded on the blockchain but your funds will not reach their destination or your contract will not be executed. This is what is referred to as; running out of Gas. In this way any bad code will be halted and cannot run indefinitely.
It is easy to assign a set amount of Gas to transfer ETH as a currency between addresses, but for more complex executions such a deployment of a smart contract it may not be so easy to work out how much gas is required. You do not want to run out of gas part way through execution and have your process fail, or to pay way over the odds. In this way you can set a gas limit. This is the maximum amount of gas you are willing to pay, anything unused will be refunded to your wallet, such as MIST or MyEtherWallet, but if the gas limit is reached you will forfeit the amount. Having a limit is another way to prevent script from running indefinitely on the network.
So how does GAS work in practice?
In order to generate a standard transaction on the Ethereum Network USUALLY your wallet software will automatically calculated and pre-enter the needed Gas Price (taken from the default settings in your wallet) and Gas Limit, so under normal circumstances you don’t have to worry much about this. BUT, in certain cases, for example when the Ethereum Network is overloaded and would require a higher amount of Gas. Also when you need to make more complex transaction, for example with a smart contract (usually when contributing to ICO’s), it might be needed to adjust the default settings in your wallet, and this is what you need to know:
What is GWEI?
First of all: when dealing with the cost of Gas you will see something called GWEI. What is that, you may ask? Just like in Bitcoin, where the smallest denomination of a Bitcoin, 1/100millionth of a Bitcoin, is called a “Satoshi”, the smallest denomination in Ether (the currency of Ethereum) is called WEI and is 1/Billionth of 1/Billionth of 1 Ether, so that means 18 decimals after 0 (0.000000000000000001 ETH = 1 WEI). Currently however Gas is calculated in GWEI, which is 1 Billion WEI or equally 1/Billionth of 1 Ether (0.000000001 ETH = 1 GWEI). So in short it looks like this:
1 WEI = 0.000000000000000001 ETH
1 GWEI = 1/Billionth ETH (or: 0.000000001 ETH)
As explained before, every type of transaction on the Ethereum Network requires a number of calculations the network needs to perform to execute the transaction. Every type of calculation has a preset cost in Gas that and if you add all the required calculations for a transaction up, you get the total amount of Gas that is needed to be paid from the transaction fee (TX fee). The price in Gas per calculation is a generally fixed for the network, however the price you need to pay per Gas (the Gas Price) can fluctuate based on demand or workload on the Ethereum Network (or how many transactions need to be processes in total).
The Gas Price influences how long it may take before your transaction is included in a next block (when your transaction is processed). If the Gas Price that is set in your wallet’s settings is too low it might take a long time before it is processed (which in some cases might not be a problem) or even not be processed at all (this you don’t want to happen). In other cases you may need to have your transaction to be processed faster or as fast as possible, preferably in the next block; in that case you need to set a higher Gas Price. A higher Gas Price motivates the Miners to include your transaction in an earlier Block than someone else’s transaction.
To know how high the Gas Price in your settings (or customized for a particular transaction) should be on average, you can visit https://ethgasstation.info/ to see the latest recommendations for Gas Prices.
As explained before Gas can be viewed as the “liters of fuel” the Ethereum Virtual Machine needs to run calculations to perform transactions, so in that sense 1 Gas is 1 “liter of fuel”. And the more complex a transaction, the more calculations need to be processed, the more of those “liters” of Gas is needed. A simple transaction in Ether itself requires the lowest amount of Gas, currently (2018) it is 21,000 Gas. Transactions with the (ERC20) tokens that run on the Ethereum Blockchain Network are more complex and require more Gas (usually around 55,000 Gas). If you need to interact with a smart contract (for example an ICO), even more calculations are needed for a transaction, which means even higher amounts of Gas: 150,000 Gas or more.
The Gas Limit prevents the indefinite spending on a transaction (for example when there is a loop in the code of the script), when the Limit is reached, you have run out of Gas and the transaction fails (even though you have spent the Gas). Usually for standard transactions your wallet knows how much Gas Limit should be used, so generally you don’t have to worry about it. For ICO contributions it is very important to know how much the Gas Limit should be in order to not run out of Gas before the transaction is processed.
Side note: if you would set a higher Limit than the transaction requires, the miners will only use the Gas needed and refund the remaining ETH of what was held back to fund the transaction fee.
How is a transaction fee calculated and examples?
The ethereum transaction fee is actually calculated very simply: multiply the amount of Gas used with the Gas Price in GWEI. When you send a transaction you will not know yet how much the fee will exactly be but the maximal fee that can be spent on the transaction would be your Gas Limit multiplied by the Gas Price. To know how much the cost of Gas actually was in ETH, you can divide the total amount of GWEI spent on Gas by 1 Billion, your cost of gas will be displayed in ETH. To know how much it was in Dollars, you need to multiply the cost of Gas in ETH by de USD price of ETH. After your transaction is processed you can easily find all that info on an Ethereum Blockchain Explorer, for example https://etherscan.io.
So to summarize these calculations:
Cost of Gas in GWEI = Gas Limit (or Gas used) x Gas Price in GWEI
Cost of Gas in ETH = Cost of Gas in GWEI / 1 Billion
Cost of Gas in USD = Cost of Gas in ETH x price of ETH in USD
Example of a standard transaction with Ether:
Gas Price = 30 GWEI
Gas Limit = 21,000 Gas
Price of Ether = $ 1,250
Cost of Gas = 30 x 21,000 = 630,000 GWEI
Cost of Gas in ETH = 630,000 / 1 Billion = 0.00063 ETH
Cost of Gas in USD = 0.00063 ETH x $1,250 = $ 0.79
Example of a standard transaction with an ERC20 token:
Gas Price = 30 GWEI
Gas Limit = 55,000 Gas
Price of ETH = $ 1,250
Cost of Gas = 30 x 55,000 = 1,650,000 GWEI
Cost of Gas in ETH = 1,650,000 / 1 Billion = 0.00165 ETH
Cost of Gas in USD = $2.06
Summary and TLDR
To process ethereum transactions on the Blockchain of Ethereum Network a transaction fee is required to pay to the miners to process the transaction and to prevent spamming and protect against loops in scripts. The transaction fee is needed to pay for the fuel (Gas) for the Network and different types of transactions need different amounts of fuel/Gas. The maximum amount of Gas that can be used for a transaction is set at Gas Limit. The price you pay per Gas is Gas Price and is displayed in GWEI currently. A GWEI is 1 Billionth of 1 ETH.
If you pay a higher Gas Price per Gas your transaction will be included sooner in a block than with a lower Gas Price. If the Gas Price is too low, it might never be processed. If the Gas Limit is too low, your transaction may run out of Gas before it gets processed (reach its destination) and fail. Adjusting the correct Gas Limit is sometimes needed, but the Gas Limit for standard transaction are usually calculated automatically by your wallet software.
This article was written to the best of our knowledge with the information available to us. We do not guarantee that every bit of information is completely accurate or up-to-date. Please use this information as a complement to your own research. Nothing we write in any of our articles is intended as investment advice nor as an endorsement to buy/sell/hold anything. Cryptocurrency investments are inherently risky so you should never invest more than you can afford to lose.