You might be asking yourself questions like, “What is gas?” “Why are transaction fees so high at the moment?” “How can I lower transaction costs?” TheBitcamp has you covered. After reading this piece, you’ll understand how Ethereum transactions are priced and how you can even manage your transaction fees.
Overview For Fast Readers
- Gas fees are a part of life on Ethereum.
- Gas fees ensure the Ethereum network is secure.
- The total cost of a transaction (the “transaction fee”) is the Gas Limit, which you might think of as the liters/gallons/units of gas a car holds.
- Gas price is the cost of that liter/gallon/unit of gas.
Ether and gas are easily confused. Ethereum is a platform for decentralized applications, also known as dapps. When you write a dapp, you write it in the form of smart contracts. When you write a smart contract, it communicates or calls another smart contract. This melange of contracts are what your dapp is composed of. Smart contracts and dapps are written in Solidity, the object-oriented programming language native to Ethereum.
When you compile your code from Solidity into a machine code, which is a low-level programming language to control processing units, the function then gets executed by the Ethereum Virtual Machine (EVM), which can be seen as one single entity maintained by thousands of connected computers running an Ethereum client.
Ethereum is designed to act as a single computer. Think of EVM as the blockchain that executes your contracts. We can, however, pretend that EVM is a single computer, but, in reality, it is this blockchain system, as well as all the miners in it. Programmers craft code in Solidity, and it gets compiled into machine code, which gets executed on the blockchain or EVM.
Gas is a unit for measuring the amount of computational effort required to perform a task on the Ethereum blockchain. The separate unit for measuring computation effort, gas, decouples the cost of transaction from the ETH price.
Think of how gasoline fuels a car so you can drive it. Similarly, gas on the Ethereum network fuels transactions and operations. Every operation on the Ethereum Virtual Machine has a cost in terms of gas.
What Is Gas?
Each operation on Ethereum has an operation code and pertaining gas cost to run. In order to execute your smart contract, you need to pay for your operations. You pay for your smart contracts with ether, and gas is a unit that translates into ether. The amount of gas you must pay can then be translated into any number of ethers.
What Is The Difference Between Gas and Ether?
The unit known as gas was created so as to decouple the price of an operation from the market price of ether. While the gas price for each operation is constant, it cannot be changed so easily. Gas costs in terms of Ether, however, can be.
If the price of ether goes through the roof, the amount of ether then that represents one gas can be lowered accordingly. While each operation will be priced in gas, which is a constant number, the value of gas can be adjusted by miners and the Ethereum blockchain.
With each operation you execute in machine code, you will be on the hook for a required fixed number of gas. The list of operation codes, and the cost of each in gas terms, can be found in the Ethereum Yellow Paper.
Why Do I Need To Pay Gas Fees?
Smart contracts often consist of multiple operations that cost sometimes hundreds or thousands of gas. In order to calculate the transaction fee, multiply the gas cost by the ETH price. The gas price is measured as a smaller unit than ether. As stated in the Ethereum docs:
Gas prices are denoted in Gwei, which itself is a denomination of ETH – each Gwei is equal to 0.000000001 ETH (10-9 ETH). For example, instead of saying that your gas costs 0.000000001 Ether, you can say your gas costs 1 Gwei
We can think about it as a major and minor unit, similar to dollars (major) and cents (minor). If you want to send from your own hard wallet, such as MetaMask, estimate necessary gas prices, in order to choose between fast, medium, and slow transaction confirmation speeds.
As an abstract unit, which only exists on the Ethereum Virtual Machine, gas is used by users to pay for ETH transactions. The increase in the ETH price should not change the transaction cost. If network activity stays constant, as the price increases, the gas price should then fall, with the final transaction cost measured in ETH staying the same in dollar value.
ETH price increases often correlates with an increase in Ethereum network activity, causing prices to increase starting with transactions sent to the Ethereum network, which can be seen in the mempool, where all pending transactions wait for the miners to verify and include them in the next Ethereum block. Incentivized to pick up transactions with the higher gas prices first, miners conduct a fixed unit of work for a better price. Miners are limited in the number of transactions they can include in a single block, determined by the maximum gas limit per block.
Gas limit refers to the maximum price a cryptocurrency will pay when sending a transaction or conducting a smart contract function on the Ethereum blockchain. Gas limit is effectively the maximum value.
The current fee model is based on a simple auction mechanism. The users who want to have the transactions picked up by miners first must outbid other people, in turn driving the gas price higher, especially at times when a lot of users have urgent transactions than they want to confirm.
Gas Is Needed Because Ethereum Is Turing Complete
EVM is a Turing complete machine allowing for the execution of arbitrary code, making Ethereum vulnerable to the halting problem, which refers to whether the program finishes running or continues to run forever. Turing complete is a term describing abstract machines, known as automata. These so-called computationally universal Turing machines can recognize or decide data-manipulation rule sets.
If Ethereum didn’t feature gas, a user could execute a never ending program either by mistake or by being malicious. Therefore, Ethereum needed a gas cost associated with each operation so that a program wouldn’t run forever, thus clogging the network.
Alongside the gas price, each transaction has a gas limit that is either higher to the anticipated amount of computation needed for successfully executing a transaction. Prior to executing each operation, EVM checks to see if there is enough gas left for that operation. In the event there is not, the whole transaction reverts and all state changes are reversed. The user still pays the transaction fee for the amount of work conducted by the miner, even if the transaction fails. This is so as to avoid attacks on the Ethereum network.
If the transaction consumes less gas than thought, the remaining is converted to an ETH amount and returned to the sender. All operations on Ethereum have the correct gas cost in relation to each other. If not, they would represent another attack vector. One such attack transpired in 2016, causing the hard fork that repriced certain low level operations.
How Much Gas Should I Send?
The Ethereum network today is busier than ever due to the success of DeFi. Ethereum’s popularity increases demand for block space on the blockchain, resulting in high transaction costs. Sometimes a simple ERC20 token transfer can cost $10. On Uniswap, a transaction can cost as much as $100. This prices people with fewer resources out of using Ethereum. Today we know a bit more about gas, to be sure, and have learned from periods of high transaction fees. The industry has developed strategies to lower the transaction costs.
At EthGasStation you can view how low of a gas price miners are willing to accept. With EthGasStation’s Calculator, you can estimate how long it will be before your transaction is accepted at a specific gas price.
Transaction fees go to the miner who mines your block. When miners mine a block, they have to decide which transactions to include. So that miners continue to include transactions in blocks, you want to set a ‘gas price’ high enough to make them want to include it. Most miners include transactions they received sorted from highest gas to lowest gas paid until the block is either full or they reach a level lower than they are willing to verify.
You want to set the gas price high enough so that a miner includes your transaction in a block. If you are in a hurry, you can set the gas price even higher so you can be put towards the top of the line, for low gas prices take longer to send. With the increased price of ETH in USD terms, a transaction that once cost half a cent, may now cost a few cents.
The Future Of Gas
Certain solutions are being developed, such as Layer 2 scaling solutions, designed to help increase the capabilities of the main Ethereum chain by handling transactions off chain. Layer 2 solutions reduce transaction fees. Loopring is a good example of a decentralized exchange built on a Layer Two. Polygon (formerly Matic) features Plasma, which could allow DeFi smart contracts to interact with each other on a second layer the same way they do on Layer One.
Such technologies could decrease the cost of transactions even further as users transfer their ETH directly from an exchange to a layer two solution like Loopring, which runs on Ethereum and attempts to incentivize a global network of users to operate a platform that enables the creation of new types of crypto asset exchanges.