What Exactly Is Ethereum Gas?

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 nota sub currency of Ether. Gas is more so an “internal” token within the EVM (Ethereum Virtual Machine). It is used by Ethereum to place arelative coston each operation within a Smart Contract.

For example, a contract can have the following types of operations and associated costs

Single Execution Step: 1 Gas

Store A Value: 100 Gas ​

Call Another Contract : 20 Gas​

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:

Gas Price​The “Gas Price” is the amount of Ether you are willing to pay for each unit of Gas.

Gas LimitThe “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:

Clean a Bedroom: 1 MaidToken

Clean a Living Room: 3 MaidTokens

Clean a Bathroom: 10 MaidTokens

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:

A Maid can say, “I’m charging $25 per MaidToken”​

You can reply with,“Well, I’m willing to pay only $15 per MaidToken”​

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"

You’re saying: “I’m willing to pay only 0.00002 Ether per Gas”​​

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?!

MaidToken Limit

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.

MaidToken Price: $20

She asks,"how many rooms does the house have?"

You reply: “Uh… I don’t know...”

“Well, how do you expect me to quote you a price then?”,she replies.

You think about it (and your finances), and say: “How about this… I’ll pay for up to 100 MaidTokens. If the house needs more, just stop cleaning. If it needs less. I’ll pay you only the MaidTokens it needs.”

She agrees, and you’ve now agreed to the following:MaidToken Price: $20 per MaidTokenMaidToken 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

Over here, you’re telling the miner:I’ll pay for up to 10,000 Gas. If the Smart Contract needs more, just stop. If it needs less. I’ll pay for the Gas you used”

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.

Wrapping Up

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.