Welcome to the 6th and final article of EVM: Zero to Hero. This series helps you learn about technical web3 concepts in a simple and fun way.
In case you missed it, here’s what we covered the last article on transactions:
The transaction life cycle in the EVM
The different types of transactions
The anatomy of a transaction
On that last bullet point, we covered transaction anatomy but didn’t explain one key component: gas.
Gas is often the subject of complaints and confusion on EVM-compatible blockchains. Here’s a meme for proof:
By the end of this article, you’ll not only understand this meme but will also be well-equipped to complain about gas yourself. Let’s get to it!
What is Gas?
At a high level, gas is a measurement of work on EVM-compatible blockchains. Every transaction on the blockchain costs some amount of gas.
The basic Native Transfer transaction costs 21000 units of gas. We’ll explain this number in more detail soon. However, calculating the amount of gas needed for Contract Executions is slightly more complicated. As a reminder:
Contract Executions interact with smart contract functions. They include data about which function of the smart contract to call.
When a contract execution transaction calls a smart contract function, the EVM executes a set of Operation Codes (opcodes). You can think of opcodes as EVM-friendly instructions that tell the underlying hardware of the computer what to do. This process is similar to how programs work in general – our computers take human-friendly programming languages like Java and convert it into machine-friendly assembly.
There are a set amount of EVM opcodes, each of which costs a fixed amount of gas. As a result, transactions calling complicated smart contracts cost more gas units since they use more opcodes (and therefore take more work to process)!
🧠 More information 🧠
Remember that 21000 gas units for Native Transfer transactions? That seemingly arbitrary number is determined by the opcodes required by the EVM to transfer ETH from one account to another.
Why Do We Need Gas?
There are two key reasons why gas is important: economic incentives and spam prevention.
Economics
Gas maintains the economic incentives of the decentralized blockchain network. Every time you send a transaction, the gas you pay goes to the network of nodes that are validating and adding your transaction to a block. The nodes need to get paid for the work they’re doing!
Spam Prevention
Without gas, people could freely spam the blockchain and overwhelm the finite resources of each node. There have been many instances in Ethereum history where the network was overwhelmed because an attacker found a specific opcode that was underpriced. From the Ethereum blog in 2016:
The network is under attack… This is due to the EXTCODESIZE opcode, which has a fairly low gasprice… the attack transactions are calling this opcode roughly 50,000 times per block… The consequence of this is that the network is greatly slowing down.
An interesting parallel is the amount of spam we all get in our email.
This may sound obvious, but by attaching a cost to some action, the quality of actions increases.
Gas Limits and Gas prices
Transactions have two major fields related to gas: gas limit and gas price.
Gas Limit
The gas limit is the count of units of gas you’re willing to pay for a transaction. If you buy too little gas, your transaction will fail. However, if you buy too much, your account will be refunded the additional gas that you purchased.
So why would you not set a super high gas limit every time? There are two reasons:
Nodes are less likely to pick transactions that look like a lot of work, which is signaled by the gas price.
Setting a reasonable bound on the gas limit protects you from poor smart contract code that uses excessive gas. You’d rather have your transaction fail than accidentally spend all your money on gas!
Luckily, dApps and other free tools help you estimate gas limits automatically so that you don’t have to figure it out yourself.
Gas Price
The gas price is how much you’re willing to pay per unit of gas. Wait… what? How are you able to pick the price of gas that you pay?! Well, EVM-blockchains are set up such that the nodes processing transactions are incentivized to pick ones with the highest gas prices.
The result is a classic supply and demand equation. As more people want to use the blockchain, the gas price increases, and vice versa. The bottom line:
Gas prices are denoted in gwei, which is one billionth of one ETH.
The EIP1159 Update
One issue with allowing users to pick the entirety of the gas price is that it makes gas prices unstable at times of high demand. This is because users feel pressure to add to their gas price arbitrarily, resulting in a gas price “arms race” since it’s difficult to determine the ideal gas price in real time.
Ethereum Improvement Proposal (EIP) 1159, released in August 2021, aimed to make gas prices more predictable by breaking down the traditional gas price field into three separate fields: base fee, max priority fee, and max fee.
The base fee (in gwei) is determined by the blockchain network and is the absolute minimum that users must pay per unit of gas. The base fee is dynamic and reacts to real time demand, resulting in a more stable gas price over time since it’s set globally for all users.
On top of the base fee, users can choose to include a tip to blockchain nodes in the form of the max priority fee (also in gwei). The somewhat counterintuitive idea of the max fee dictates the absolute maximum gas price you’re willing to pay in the case that the base fee increases while your transaction is in flight.
To drive this home, let’s analyze today’s gas price tracker. Specifically, we’ll analyze the “high” gas price.
The “high” designation means that if you were to pay a base fee of 33 and a max priority fee of 3, your transaction would be processed quickly.
Recap
In today’s article, we covered:
What gas is and how its calculated
Why gas is crucial for the health of blockchains
How EIP 1159 is stabilizing gas prices
As always, feel free to reach out on Twitter or via email at shekar@ramaswamy.org with any questions or comments.
What’s Next For Coinsights?
We’ve officially wrapped up the EVM: Zero to Hero series. The six articles in this series should give you a solid foundation in the technical foundations of the EVM.
We’d love to hear from you about what content you’d like to see in the future. Please answer this poll and give us your thoughts!