Technical Deep Dive: What is Gas?
A fee included in every Ethereum transaction that is commonly misunderstood
When I first approached learning Ethereum, one of the more difficult topics I had learned was about gas fees. A Gas fee is a unit that measures the computational effort required to execute specific operations on the Ethereum network and is charged to the user who submits a transaction request. Why gas is important to understand is because it is present in every transaction and is priced differently each time. It scales with network demand and the amount of computing power a transaction needs. This means that it might be cheap one day and astronomically priced another. This is sure to annoy you when you are frequently transacting on the blockchain, but the reason that a gas fee is necessary is for network security.
Gas fees provide security from bad actors and bad code. Since there is a fee owed by the sending address to create a transaction and miners are incentivized to keep the network secure, gas helps prevent people from spamming the network and ensures that people store transactions with purpose. It is a scalable approach to security and one that makes Ethereum such an attractive blockchain to use.
Gas Fee
The formula to calculate gas is Gas Limit * (Base fee + Tip). Gas units are in gwei, a denomination of ether. Each gwei is equal to 0.000000001 ETH (10-9 ETH). In the sections below I will dissect each part of this equation and how the value is determined.
Gas Limit
Gas Limit is defined by the address initiating the transaction. A standard ETH transfer requires a minimum of 21,000 gwei. The gas limit is used to guarantee that the transaction will be executed without needing to know the exact gas price. If the actual gas used in a transaction is less than the limit the user sets, then the difference between the limit and the actual price is returned back to the address. In cases where the limit is lower than the actual gas price on the transaction, then the transaction fails and no ETH is returned. This might sound unfair, but a miner used computing power in an attempt to execute the transaction and paid for it. What this means is that you should make sure your limit is a degree higher than the estimated amount of gas the transaction will use. This can be manually set by you, but typically apps and wallets will automatically set it based on their own estimations of the gas required.
The technical formula behind the gas limit is
gasLimit = G(transaction) + G(txdatanonzero) × dataByteLength
Where
G(transaction) = User-specified limit
G(txdatanonzero) = Calldata, which is 16 gas per byte
dataByteLength = Your transaction data size in bytes (data involved in the transaction)
Example
gasLimit = 21,000 + (16 * 8) = 21,000 + 128 = 21,128
Base Fee
The Base fee is determined by the Ethereum network and cannot be adjusted by the address initiating the transaction. The fee is calculated based on the blocks mined before it and the target block size. The goal of this fee is to ensure that blocks are completed at 50% of the target size. The fee will automatically increase or decrease by 12.5% depending on if the previous block size is greater or less than 50%. If the target block size is exceeded, then the base fee will continue to exponentially increase by a maximum of 12.5% per block. This exponential growth becomes economically unsustainable for block sizes to remain high forever, so eventually, the network demand will recede. This fee is not given to the miner and is burned by the network.
Tip (Priority Fee)
Tips are included in gas to incentivize miners to include a transaction in the block. It is the only reward that miners receive from the gas and make transaction selections preferential based on the tip size. Higher tips are necessary to outbid competing transactions in the event that the network is seeing high demand. If tips did not exist, miners would mine empty blocks because they would receive the same block reward as a block that used more computing power to mine multiple transactions. Most wallets and services will provide a suggested tip depending on the speed at which the user needs the transaction to be executed and the current network demand.
How smart contract developers are impacted
Where contracts come into play with the gas fee formula is in the dataByteLength portion of the gas limit equation. As you can interpret from the definition, this part of the fee is based on how much data is trying to be stored on the blockchain. The more data needed, the more computing power it takes to execute and the more expensive the gas fee will be. How much data your contract is using can be directionally computed by looking at the compiled solidity code logs to see how the code is translated into the binary EVM code. The two most relevant sections of information within this log are the gas estimates and assembly sections.
Gas Estimates
The Gas Estimates section of the compilation details will provide you with a rough estimate of what to anticipate gas to cost when it is published to the network. The values presented are in wei.
Assembly
The Assembly is how each part of your contract code is translated to EVM binary code. This can be helpful when trying to optimize your code as you can look up the EVM opcode reference in the Ethereum yellow paper and see the amount of memory that is used for each type of code.
How to Think about Transactions and Your Smart Contracts
Optimizing your contracts for gas fees is a delicate balancing act of storage reduction and user experience. While you have full control over trying out different patterns to minimize your gas usage with your contracts, at a certain point there isn’t much else you can change at the risk of removing logic that has value by being stored on the blockchain. You definitely don’t want to be in this situation and the changes are less likely to significantly impact the gas fee as much as biggest variable in the gas equation, network demand.
At the time of writing this, the demand has been higher than ever before, creating unsustainable fees and generating a hailstorm of negative public responses on social media. The scaling issues that Ethereum faces today make it hard for a user to justify making a transaction unless the perceived value of that transaction outweighs the short-term cost. This means that decisions are being made based on value generated by transacting with the contract, rather than the size of the contract’s impact on the fee. This means if you aren’t providing value with your contract then you aren’t likely to see people engage with it. Focus on the experience more than the data reduction to start.
Gas Fees and the Future
Gas fees are a problem the network faces today, but when Ethereum 2.0 is launched, it is expected to alleviate its network pains and eliminate this conversation altogether. This upgrade is expected to come next year so it is hard to have me push hard on contract optimization when your time could be better spent on improving the logic to add value to the network. I think that network wealth creation will drive users to transact with your application and/or contracts regardless of the impact the storage or even network has on gas fees. While I’m not saying you should avoid optimizing your contract, I think it should take a backseat to user experience so it is adopted and utilized.