Guide
Updates
Analysis

Fueling Gas Spells on Dune part 2

In this post, we continue our research into fee mechanisms we focus on gas fees and incentives mechanisms for Layer 2 blockchain solutions indexed on Dune!

Table of Contents
Share on twitter iconShare on linkedin iconShare on reddit icon

In this article we're going to teach you everything you need to know about gas fees on Optimism. By the end you'll grasp the mechanism fully, and be able to cast gas spells of your own.

In part 1 of this series we focused on layer one (L1) chains - looking at Ethereum, BNB & Avalanche in turn. Today we're going up a level and looking at layer two (L2), in particular Optimism, and explaining how gas fees work on the network as well as its various incentive mechanisms. Firstly though, let's start with the basics, what is an L2?

Layer 2 (L2) is a collective term for a set of solutions designed to address scalability issues on L1 blockchains like Ethereum. The issues originate from a well-known trade-off between security, decentralization, and performance, often referred to as the blockchain trilemma.

Source

On the one hand, L1s need to keep a high degree of decentralization and security, which means that individuals should be able to run nodes independently so they can compute and verify the state of the blockchain (low trust, low cost) without relying on trusted third parties. On the other hand, this comes at the cost of performance since individual nodes don’t have access to the infrastructure capable of handling a high throughput of transactions with low latency.

One way to address this problem is to use separate blockchains, L2 solutions, that extend L1 blockchains like Ethereum while inheriting their security guarantees.

In this series we're going to breakdown two different categories of L2s: rollups like Optimism and Arbitrum, and side chains like Polygon and Gnosis chain. In this article though, we're going to look at Optimism in depth.

Firstly though, let's explain what rollups are more generally.

What is a rollup?

Rollups are currently the preferred L2 solution for scaling Ethereum. The overall approach is to bundle (or “roll up”) hundreds of transactions into a single transaction on L1, which distributes the transaction fees across everyone in the rollup, making it cheaper for each user. There are two different approaches to rollups, optimistic and zero-knowledge, that differ primarily on how transaction data is posted to L1.

“Optimistic” rollups like Optimism and Arbitrum propose to introduce a third party, called a sequencer, that is responsible for:

  1. Storing and executing transactions off-chain (i.e., on L2)
  2. Periodically (every 2 to 10 min) posting the (compressed) input data and results from these batches of transactions on-chain (i.e., on L1)

The initial, optimistic assumption is that transactions submitted by the sequencer and posted on L1 are valid. However, during a time called a dispute period, any party (i.e., verifier) can replay the sequence of events that led to a certain state on L1, and compare this resulting state with the one that was published by the sequencer.

If there is a mismatch between L1 and L2, verifiers can publish a “fraud proof”. The publication of a fraud proof on-chain leads to a deliberation process between the party claiming fraud and other Ethereum validators to determine whether the fraud proof is valid. If that’s the case, the state transition caused by the problematic transaction is canceled.

The main advantage of fraud proofs is that they require few computational resources since they are not needed for every state transition, only when things supposedly break down. But it also means there is a time (1 week) during which users can’t perform operations on the L1 blockchain like withdrawing their funds.

The other alternative is to use zero-knowledge rollups. In that case, “validity proofs” are sent to a smart contract on L1 to attest that off-chain computations were run to verify the validity of a given transaction using cryptographic methods (check ZK-SNARK and ZK-STARK publications for more details on these methods).

With validity proofs, there's no need to wait for a dispute period to be over. The L1 blockchain will always reflect a correct L2 state, which can be relied upon and used immediately. However, cryptographic off-chain verifications can be computationally intensive, and need to be run for every state transition, which negatively impacts scalability. We'll be covering ZK-rollups in more detail in a future post.

In this blog post, we're going to focus on Optimism, and the strategies it uses to scale Ethereum by:

  1. Increasing throughput since transactions are executed off-chain
  2. Reducing gas fees since sequencers only periodically post compressed data for batches of transactions on-chain.

Before we dive in, now is probably a good time to check part 1 of this series if you haven’t already, or if you just want a quick recap on how gas fees work on major L1 chains like Ethereum, BNB and Avalanche. 

Optimism

Optimism is an optimistic rollup founded in 2019 by the Optimism Foundation with the clear objective of scaling the Ethereum protocol. The Optimism protocol launched its open mainnet in December 2021 and is aligned with the Ethereum Virtual Machine (EVM) specification. They recently launched a governance ERC-20 token, $OP, which was airdropped to community members for the first time on June 1st, 2022. 

OP governance mechanisms

On top of being a rollup, Optimism has designed a sophisticated funding and governance mechanism based on the OP token, which allows holders (i.e., the Optimism Collective) to vote on protocol governance proposals, and serves as a value stream to fund public goods on Optimism.

The Collective's model of governance consists of two houses: the Token House and the Citizens' House.

One the one hand, holding the OP governance token grants the role of Token House member, and confers rights to submit, deliberate and vote on various proposals including protocol upgrades, treasury appropriations, and inflation adjustments. On the other hand, 20% of the OP supply as well as some of the sequencer revenue generated on Optimism will be dedicated to retroactive public goods funding (RPGF), and handled by the Citizens’ house.

The idea behind RGPF is simply to allocate funds to projects that have already proven to be useful for the Optimism ecosystem instead of trying to determine the impact projects will have in the future. The citizens’ house will be composed of "soulbound" NFTs holders (i.e., citizens) whose role is granted by the foundation and by proposals to the Token House. Their role is to manage the allocation of funds to Optimism initiatives based on their past utility for the Optimism and Ethereum ecosystems. 

Source

One prime example of a public good infrastructure project is the OP Stack, which was recently announced and released during DevCon 2022. This allows for a set of modular components that can be assembled to build custom chains using the Optimism codebase to fit any use-case like OPcraft, a fully on-chain 3D voxel world built on top of the OP stack.

OPcraft screenshot, Source

Back to the real world and onto rollup economics, let’s dive into gas fee mechanisms on Optimism! 

Gas fees on Optimism

In the introduction, we saw that transactions are executed off-chain (i.e., on the L2 optimistic rollup), batched, and posted to Ethereum L1 by the sequencer. This process drastically reduces transaction fees since gas costs are limited to publishing the transaction data (i.e., L1 data fee) to Ethereum. This roughly scales with the size of the transaction (in bytes) - but the logic of how L1 data fees are computed on Ethereum remains the same, and can be summarized as such:

  • Pre EIP-1159: L1 data fees = Gas units used * Gas price per unit
  • Post EIP-1159: L1 data fee = Gas units used *(Base fee + Priority Fee)

To understand the inner workings of gas fees on Optimism, we also have to take into account additional parameters and upgrades that were implemented by the rollup over time, most of which were introduced to reduce calldata L1 fees. 

Timeline of upgrades impacting gas fee calculations

As a reminder, using Optimism allows you to only pay gas prices for the portion of your transaction data that is submitted to L1 in a transaction batch, instead of paying for the entire transaction execution on L1 Ethereum. This cost includes the actual calldata (input data) of your transaction, but Optimism added two parameters to cover additional costs expected by the Optimism team:

  1. The fixed overhead cost, which reflects the additional processing required to add another transaction to the larger batch, now set to 2100 gas units
  2. A Dynamic overhead (fee scalar) premium, which serves as a buffer in case L1 prices rapidly increase

Note that all excess funds collected via the fee scalar are redirected towards public goods funded by the citizen’s house, and that it has been set to 1 since April 5th, 2022.  

In the end, post EIP-1559, L1 data fees can be computed using this formula:

L1 data fees = (Call Data Gas units used + Fix overhead) * Dynamic Overhead * (L1 Base fee)

Another upgrade we can talk about is one that was implemented by the Optimism team on March 24th, 2022 to reduce the amount of data posted on L1 (Zlib batch compression). Note that it doesn’t affect gas fees calculations per se, but led to a 30-40% transaction fee reduction. To check out its impact, make sure to look at Msilb7’s Dune chart here, showing the amount of gas used to post data on L1 before and after the compression upgrade: 

All that’s left to do to get the total transaction fees is adding costs related to transaction execution on Optimism, called L2 execution fees, computed the exact same way as transactions on Ethereum:

L2 execution fees = L2 Gas units used * L2 Gas price per unit.

In practice, L2 gas prices are very low (set to ~0.001 Gwei as a default), which means fees are actually negligible, but we’ll still include them on our spells and queries for the sake of completeness. 

It’s also worth noting that Dune actually makes it easy to compute transactions fees by introducing the l1_fees column, which takes into account all the parameters needed to compute L1 data costs, and includes all changes and upgrades made by the Optimism team to reduce fees overtime: 

L1_fees = (Gas units used + Fix overhead)*Dynamic Overhead*Base fee

To compute the total transaction fees, including L1 data fees and L2 execution fees, here’s the code we’ll use on Dune:


SELECT
	'optimism' as blockchain,
  l1_fee + (l2_txns.gas_used * l2_txns.gas_price) as tx_fees
FROM optimism.transactions l2_txns

To run this query and check out the other columns we’re including in the optimism gas fees spell, you can click here

If you’ve made it this far, congratulations! You now have all the pieces needed to compute total transaction fees on Optimism, and we can now build the Optimism gas fees spell on Dune! 

For convenience, note that we chose to build our spell starting after the Optimism Regenesis that took place on November 11th 2021, and released upgrades to reach full EVM equivalence (see timeline, and check out the Optimism blog post here for more details). But if you’re feeling adventurous and want to complement the Optimism gas fees spell by adding pre-regenesis data, remember that every spell built on Dune is open-source, so just head over to the Spellbook repo and open a PR!

Finally, here are a couple of Dune and non-Dune resources to explore the Optimism ecosystem further:

Mastered gas spells for Optimism? Stay tuned for the next instalment of this series where we're going to teach you all there is to know about gas on Arbitrum......

Subscribe to our newsletter!

The revolution will not be reported quarterly.
Celebrating Dune wizards. Dashboards, stories, alpha dropped in your inbox weekly.