What Exactly Is Gas
There seems to be a lot confusion & questions around Ethereum & Gas – and rightfully so. Blockchain veterans & enthusiasts alike can be thrown off by the terminology. Moreover, people often wonder “why” Ethereum chose this route. Why not keep it simple – like Bitcoin?
There are good answers to these questions. But before we dive into the “why & how” of Gas – let’s briefly go over “what” Gas is.
Contrary to popular belief – Gas is not a sub currency of Ether. Gas is more so an “internal” token within the EVM (Ethereum Virtual Machine). It is used by Ethereum to place a relative cost on each operation within a Smart Contract.
For example, a contract can have the following types of operations and associated costs
This allows Ethereum to “charge” more for contracts that are more complex. This is fair – since a Smart Contract with more/demanding operations will be using more network resources.
When you send a transaction to a Smart Contract, you are required to enter the following:
The “Gas Price” is the amount of Ether you are willing to pay for each unit of Gas.
The “Gas Limit” is the total amount of Gas you’re willing to buy for the execution of this transaction.
I get it – it's still confusing. Understanding how "Gas Price" and "Gas Limit" relate to each other can be a task. So let's use an analogy to clear things out.
The Maid Service Analogy
Suppose you join a new Maid Service Agency. But they decide to give their Maids more freedom & flexibility in how they conduct their business. They wanted the Maids to be allowed to earn more if the “market” deems it worthy.
So, instead of charging you in USD$, they charge you in MaidTokens. MaidTokens can be redeemed for cleaning activities (operations) as such:
These prices are set by the Maid Service Agency – and don’t change. So no matter what – the Maid Service Agency will get a fixed number of MaidTokens for each job (based on the rooms being cleaned).
Flexibility & Freedom
We mentioned that the MaidTokens are being used for more freedom. But how is this freedom achieved?
The flexibility comes from the fact that the Maids can negotiate how much you (the customer) pay for each MaidToken. Here’s an example:
The Maid (Miner) can then either take you up on your offer, and clean your rooms (validate your transaction) – OR look for other customers that will pay more.
This is exactly what you're doing when you set your "Gas Price"
Similarly, a Miner can choose to take your offer or not.
(To automate things – Miners set up a minimum a minimum Gas Price they are willing to accept. They have the freedom to include your Transaction in their block or not. But just like the Maid, they are always looking to maximise profits.)
Okay, awesome.. Using the “MaidToken Price” we have a better understanding of “Gas Price”. Now let’s tackle “Gas Limit” – what in the world is it really?!
Now let’s suppose you rent a cottage and host a huge party. You wake up the next day to a gigantic mess and a colossal headache. You call the Maid Service Agency, and they send over a Maid. You guys negotiate and agree on a MaidToken Price of $20.
She agrees, and you’ve now agreed to the following:
MaidToken Price: $20 per MaidToken
MaidToken Limit: 100 MaidTokens
This is essentially the same type of agreement you get into when you send a transaction with the following:
Gas Price: 20 Gwei (per Gas)
Gas Limit: 10,000 Gas
**Note: Gwei is a subunit of Ether. 1 Ether = 1,000,000,000 Gwei
Hitting The Gas Limit & Losing ETH
Hopefully, 10,000 Gas is enough to complete execution of your Smart Contract. If the operations within the Smart Contract was costlier than you anticipated – then the execution will not complete. And the transaction fails.
Similarly, if your 100 MaidTokens aren’t enough to clean your rooms (the cottage may have had more bathrooms than you thought) – then the Maid will stop cleaning/executing.
The difference in our analogy is that when you hit your “MaidToken Limit” you get to keep the clean rooms. Your house will be partially clean and the maid will leave.
However, we can’t have “partial execution” of a Smart Contract. So if you hit your “Gas Limit” before the contract is completely executed – the contract will roll back. It will be as though the transaction never occurred. And you’ll get an “Out Of Gas” error.
The unfortunate part is that you will have “used up” the Ether (Gwei) you set. Afterall, the contract DID execute – just not entirely.
Tip: If you’re trying to save on transaction fees, set a LOW Gas Price and a higher Gas Limit. Too often people do the opposite.
Gas is used within the EVM to place a cost on various operations. So when you’re conducting a transaction, think of “Gas Price” as the price you’re “offering” to the miner for each unit of Gas. The "Gas Limit" is the total amount of Gas you’re willing to buy for this particular transaction.
That’s pretty much it. In the next post, we’ll discuss why Ethereum has chosen to go this route as opposed to the way BTC does it.
Did this post help you?
Help Us Keep Doing What We Do Best!
Tip Jar 🙂
Did you enjoy this post?
Help Us Keep Doing What We Do Best!
Tip Jar 🙂
Get my upcoming eBook for Free!
"The Mango Guide TO Understanding Blockchain"
Offer Valid For FIRST 500 registrations only