HB Wallet Team
A Complete Guide To Ethereum Gas
Everything you need to know about Gas in the Ethereum network.
What are Smart Contracts?
What are NFTs?
Download HB Wallet: iOS | Android | Mac | Windows
Like Bitcoin, Ethereum is a distributed blockchain network consisting of a cluster of computers that connected together.
Aside from the technical differences, Ethereum differs itself from Bitcoin in its purpose and capability. While the Bitcoin blockchain is purely used for tracking ownership of virtual currency (bitcoins), the Ethereum blockchain focuses on running smart contracts of decentralized applications.
Ethereum was released in 2015 by Vitalik Buterin, who believed that Bitcoin was revolutionary and incomplete. Ethereum Virtual Machine (EVM) and its Turing complete programming language Solidity enabled the ability for developers to program any decentralized application with customizable transactions, known as smart contracts.
Smart contracts are self-executing contracts with a specific set of rules and regulations written in its code, which get executed when certain conditions are met. Smart Contacts facilitate the ability of exchanging money, property, shares, or any assets in a transparent and secure way while avoiding the services of intermediaries. To do this, every smart contract requires a certain amount of computing effort to be executed by the network. This is when Gas comes to the picture.
WHAT IS GAS?
Gas is a unit that measures the amount of computational power which will be taken to execute certain operations, from simple ETH transfers to ICO smart contracts in the Ethereum network.
Imagine Gas in Ethereum is similar to gasoline in your car. When a transaction in the Ethereum network runs, it consumes an amount of computing power like your car engine consumes gasoline. The miners (like gas stations) providing their computing power (as gasoline) to verify and execute your transaction that needs to be paid for their service.
In real life you pay money to buy gasoline, in Ethereum you use Ether (ETH) - the native currency of Ethereum, to buy gas.
Thus, to get to the destination, your car requires different amounts of gasoline which depend on whether you are driving on a highway or going through a bumper-to-bumper traffic in a city. Ethereum works almost exactly like this. Different types of transactions consume different amounts of gas to complete. Simple transactions like standard ETH transfers will require less gas to perform than intense smart contracts.
Now, let’s dig deeper to the details.
To get an operation complete in Ethereum, user who requests the transaction must specify a gas limit before they submit it to the network. Gas limit can be understood as the maximum amount of gas you’re willing to spend on a particular transaction.
The more complex the transaction is, the more gas you have to pay as more computational work must be done to get that transaction executed.
For example, this happens when participating in an ICO that requires you to send ETH into its smart contract or when withdrawing your ICO coins to an exchange, the fees of these transactions are much higher than those standard ETH transfers (consume 21,000 gas limit).
When specifying a gas limit, you must consider the following points:
Different operations will have different gas costs.
The miners will stop executing the moment the gas maxes out.
After the execution, the left-over gas will be refunded to the operation generator.
In fact, there are two scenarios that can happen:
The specified gas limit is too low
The specified gas limit is too high
Gas limit acts as a safety mechanism to protect full nodes from attackers. If transactions did not need to specify a gas limit, a malicious user could make them execute effective infinity loops and no one would be able to process it since processing such a transaction take longer than a block interval. If a transaction took longer than one block to process, it could never get included in a block which could lead to a denial-of-service vulnerability.
What if I specify the gas limit too low?
If an operation runs out of gas, it will be reverted back to its original state like nothing happened. However, the gas used for this ‘Failed’ transaction will STILL be kept by the miners as computation costs and the transaction STILL gets added to the blockchain even if it has not been executed.
Back to the road trip story, when your Lightning McQueen hasn’t been filled up with enough gasoline, you won’t be able to reach your destination and have to stop in the middle of nowhere. But even then you still paid the gas station for that amount of gasoline you used, right?
Hey, don't worry about the gas limit value! HB Wallet would automatically set the default gas limits for the types of transactions you want to execute.
What if I specify the gas limit too high?
It sounds right to always set the gas limit high, just in case, since what leftover is refunded to the sender after all, doesn't it? In fact, it doesn't really work exactly like what it was written on the paper.
Miners are limited by the block gas limit, which is currently 8,000,000 gas. Any transaction has a 'base fee' of 21,000 gas to cover the cost of an elliptic curve operation while recovering the sender address from the signature, as well as the space of storing the transaction. Miners can only include transactions which add up to less than or equal to the block gas limit.
For example, transaction A is a simple ETH transfer and has a specified gas at 42,000. Transactions B and C (as ETH transfers) have specified gas limits of 21,000.
Will the miners put in transaction A and refund a huge amount of leftover gas?
Or will they put transaction B and C and refund little or nothing back?
Which option do you think will make more sense for the miners to choose?
Economically, the second option it is. Now you got the picture right?
It is not sensible and necessary to bloat up your transaction to entice the miners. It is more reasonable to specify a gas limit which is just a little higher than the required amount of gas for your transaction.
The other important part of the gas mechanism is the economics of the Gas Price - the amount of Ether (ETH) you're willing to pay for every unit of gas. Gas Price is measured in Gwei with 1 ETH = 1,000,000,000 Gwei.
Sender of a transaction can specify any gas price they prefer to entice the miner to verify the transaction and put it in the block (usually ones that specify the higher gas price). The average gas price is typically on the order of about 20 Gwei (or 0.00000002 ETH), but can increase due to the high network traffic as there are more transactions competing with each other to be included in the next block.
Before specifying a gas price, it's worth to take a look at current gas prices from HB Wallet or ETH Gas Station.
Terms of words you need to understand:
Std Cost for Transfer: Average fees users pay to transfer ETH in USD value for a standard priority transaction (usually a waiting time of fewer than 5 minutes).
Gas Price Std (Gwei): Average fees users pay to transfer ETH in Gwei value for a low priority transaction (usually a waiting time of fewer than 5 minutes) .
SafeLow Cost for Transfer: Average fees users pay to transfer ETH in USD value for a low priority transaction (usually a waiting time of fewer than 30 minutes) .
Gas Price SafeLow (Gwei): Average fees that users pay to transfer ETH in USD value for a low priority transaction (usually a waiting time of fewer than 30 minutes)
Median Wait (s): Average waiting time for a single transaction in seconds
Median Wait (blocks): Average waiting time for a single transaction in blocks
Higher gas price means faster processing. It depends on how you identify the priority level of your transaction. Miners, of course, will pick up an offer that is more inviting. If you want your transaction to be executed faster, give the miners a shout by paying a higher gas price.
HOW TO CALCULATE THE COST OF A TRANSACTION (TX)
It's simple as ABC to calculate the amount of fees you'll eventually pay for a transaction (Tx).
For example, the default amount of gas limit for a standard ETH transaction consumes 21,000 gas, while the gas price chosen by the sender is 30 Gwei. Multiply these two figures together, we will get the actual cost of the transaction execution, amounting to 0.00063 ETH (equal to $0.16821 at the time writing).
Or you can use the ETH Gas Station Calculator if you are not in the mood for solving math problems.
Keep in mind that gas limit can be more than the actual gas used in the transaction as people increase their gas prices to have a better opportunity of confirming their transaction.
Download HB Wallet: iOS | Android | Mac | Windows
Next: What are Smart Contracts?
#Blockchain #Ethereum #Gas #GasPrice #GasLimit #SmartContracts #HBWallet