What is gas?
Gas represents a unit that measures the volume of computational work required to perform transactions or execute operations on a blockchain network.
The maximum amount of gas a user is willing to spend is the gas limit. The network sets the block gas limit, which is the maximum amount of gas allowed in one block. The gas fee is the amount of gas used multiplied by the cost per unit of gas (plus any additional priority and/or base fee according to the network). It must be paid regardless of whether a transaction succeeds or fails. Any unspent gas is refunded.
Gas fees are paid using the network's native cryptocurrency. For example, transactions on Ethereum must be paid in ether (ETH).
What is gas used for?
Gas is used to allocate network resources efficiently and ensure fair compensation for validators. It sets a computational limit on operations, preventing excessive resource usage and potential abuse, such as infinite loops in smart contracts. Gas costs help to maintain stability by making it financially unfeasible to spam the network.
How are gas fees calculated?
Gas fees are typically determined by the total gas used (units consumed) by a transaction multiplied by the gas price (cost per unit of gas). The exact formula varies depending on the network. Here are a couple of examples.
Ethereum:
- gas fee = units of gas used * (base fee + priority fee)
- Fees are denominated in gwei, where 1 gwei = one-billionth (0.000000001) ETH. some text
- The smallest denomination of ETH is wei.
- 1 gwei = 1 billion wei.
- Wei allows gas prices to be calculated with more precision.
- Here is a simple gwei converter to calculate the differences.
- Total gas paid is divided into two categories, base fee and priority fee.
- The base fee is set by the protocol and dynamically adjusts up or down based on network congestion.
- The priority fee is set by users as a tip to validators.
- Priority fees are not mandatory, but help entice validators to include a transaction in the next block.
Example: A user wants to execute a transaction on Ethereum that consumes 21,000 gas units, with the following fee structure:
- Base Fee: 10 gwei
- Priority Fee: 2 gwei
Calculation:
- Find gas fee per unit:
Base Fee + Priority Fee = 10 gwei + 2 gwei = 12 gwei/unit
- Calculate total gas fee:
Total Gas Fee = Units of Gas Used * Gas Fee Per Unit
Total Gas Fee = 21,000 gas units * 12 gwei/unit = 252,000 gwei
- Convert to ETH:
Since 1 gwei = 0.000000001 ETH, the total gas fee in ETH is:
252,000 gwei * 0.000000001 ETH = 0.000252 ETH
The total gas fee for this transaction is 0.000252 ETH.
Base (OP Stack):
- An Ethereum L2 built on Optimism (OP Stack)
- Uses a FastLZ compression estimator to more accurately charge L1 data usage per transaction
- Gas fees are the sum of:some text
- Execution gas fee: Identical to Ethereum - gas fee = units of gas used * (base fee + priority fee).
- L1 data fee: Covers the cost for an OP transaction on Ethereum and is influenced by Ethereum’s current base fee.
L1 data fee calculation:
The L1 data fee consists of:
- Estimated transaction size, determined using the compressed size of the signed transaction and the Fjord linear model.
- estimatedSizeScaled = max(minTransactionSize × 10^6, intercept + fastlzCoef × fastlzSize)some text
- takes highest number (minTransactionSize × 10^6) or (intercept+fastlzCoef×fastlzSize)
- minTransactoinSize: Minimum transaction size in bytes
- interceprt: Fixed parameter that can be positive or negative
- fastlzCoef: FastLZ coefficient
- fastlzSize: The size of the FastLZ compressed signed transaction
- Weighted gas price multiplier, which depends on Ethereum's base fee and blob base fee.
- l1FeeScaled = baseFeeScalar × l1BaseFee × 16 + blobFeeScalar × l1BlobBaseFeesome text
- blob: Large chunk of data stored temporarily and used to improve scalability
- baseFeeScalar: Scales Ethereum base fee by 10^6
- l1BaseFee: Current Ethereum base fee
- blobFeeScalar: Scales Ethereum blob fee by 10^6
- l1BlobBaseFee: Current Ethereum blog gas price
- The final L1 data fee is computed using the scaled values of transaction size (estimatedSizeScaled ) and weighted gas price multiplier (l1FeeScaled ).
- l1Cost = (estimatedSizeScaled × l1FeeScaled)/10^12
Example:
Suppose the following arbitrary parameters:
- minTransactionSize: 128 bytes
- intercept: 10,000
- fastlzCoef: 500
- fastlzSize: 150 bytes
- baseFeeScalar: 2,000,000
- blobFeeScalar: 1,000,000
- l1BaseFee: 20 gwei
- l1BlobBaseFee: 15 gwei
1. Estimate Transaction Size:
estimatedSizeScaled = max(128 × 10^6, 10,000 + 500 × 150)
estimatedSizeScaled = max(128,000,000, 85,000)
estimatedSizeScaled = 128,000,000
2. Calculate Weighted Gas Price Multiplier:
l1FeeScaled = 2,000,000 × 20 gwei × 16 + 1,000,000 × 15 gwei
l1FeeScaled = 640,000,000 + 15,000,000 = 655,000,000 gwei
3. Calculate Final L1 Data Fee:
l1Cost = (128,000,000 × 655,000,000 gwei)/10^12
l1Cost = 83,840 gwei
Convert to ETH by dividing the gwei amount by 1,000,000,000
1 ETH = 1,000,000,000 gwei 83,840 gwei/1,000,000,000 = 0.00008384 ETH
L1 data fee = 0.00008384 ETH
Total cost would be the L1 data fee (0.00008384 ETH) + the execution fee.
What makes gas fees increase?
Various factors can influence gas fees. A few contributors include:
- High transaction volume: Congestion during periods of intense activity, such as NFT drops, token launches, or general market volatility.
- Complex smart contracts: Operations requiring significant computation or storage consume more gas.
- Dynamic base fees: Some protocols adjust base fees according to network usage, with higher activity resulting in higher fees.
- Block size: Networks like Ethereum have a target block size (15 million gas) that can increase or decrease according to network demand. Larger block sizes result in higher base fees, and vice versa.
Strategies to reduce gas fees
- Optimize transaction timing: Execute transactions during off-peak periods when network congestion is lower.
- Track real-time gas prices using tools like Etherscan gas tracker.
- Use layer 2 solutions: Utilize scaling technologies such as ZKsync, Arbitrum, or Optimism to benefit from reduced fees.
- Adjust gas price: Set a lower gas price if the transaction is non-urgent, but note the risk of delays.
- Optimize gas efficiency: Follow best practices to optimize gas usage and minimize unnecessary computations and storage in contract logic.