Cover photo

Gas Fees in zkSync. Guide

Gas fees in zkSync are divided into two main components: off-chain fees and on-chain fees. The off-chain fee covers the cost of storing the state and generating a cryptographic proof. This fee remains fixed at approximately $0.001 per transaction. The state represents information about all accounts and balances in zkSync, while the proof serves as cryptographic evidence that transactions are valid and consistent with the current state. Importantly, the off-chain fee remains constant and depends on the utilization of hardware resources.

On the other hand, the on-chain fee includes the costs associated with proof verification and the publication of the state delta on the Ethereum network. The state delta represents the difference between the old and new states after applying a batch of transactions. Unlike the off-chain fee, the on-chain fee is subject to the current gas price in the Ethereum network, which fluctuates based on network congestion and demand. Despite this dependency, the on-chain fee proves to be considerably more cost-effective compared to conventional ETH/ERC20 transfers on the Ethereum network.

Payment Mechanism in zkSync

zkSync employs a payment mechanism similar to Ethereum but with certain differences. In Ethereum, transactions involve a gas limit and gas price. The gas limit denotes the maximum gas consumption for a transaction, and the gas price signifies the amount of ETH a user is willing to pay per gas unit. The total fee is determined by the product of the gas limit and gas price.

In zkSync, each transaction incorporates a fixed off-chain fee and a variable on-chain fee. The off-chain fee is unaffected by the transaction's complexity or resource consumption and is solely determined by hardware resource usage. Conversely, the on-chain fee relies on the prevailing gas price in Ethereum and the volume of data required for network publication.

Comparing zkSync with Ethereum

Unlike Ethereum, where gas costs for opcodes vary, zkSync maintains a consistent gas cost for most opcodes. Arithmetic operations, excluding those affecting storage, are cost-effective, while storage updates remain the most expensive. zkSync optimizes storage updates by publishing only the state deltas, reducing the data that needs to be published and subsequently lowering on-chain fees. Moreover, if the same storage slot undergoes multiple updates in one block, only the last update is published on Ethereum and charged accordingly.

The introduction of an additional parameter, gas_per_pubdata, in zkSync plays a crucial role in calculating transaction costs. It represents the gas price for publishing one byte of data on Ethereum, specifically the state difference between zkSync blocks, not all transactions. This approach results in cost savings on data storage in Ethereum. Gas_per_pubdata is determined by the zkSync operator based on the current gas price in the Ethereum network and exclusively influences the on-chain segment of the zkSync transaction fee.

ZkSync provides an option for utilizing a paymaster contract, a smart contract capable of covering transaction fees for its users. Developers can empower users to execute transactions either for free or by paying in their ERC-20 tokens. This is achieved by creating a custom paymaster contract and defining rules for its usage.

Future Plans for Fee Optimization

In the future, optimization efforts like EIP-4844 could potentially reduce transaction fees in zkSync. This improvement allows rollups to utilize more data in Ethereum blocks without incurring additional costs, reducing the need for data compression and increasing transaction processing capacity. It's important to note that manually reducing the gas limit when signing a transaction may not effectively lower costs, as it could result in a lower refund and prevent the transaction from executing.

Conclusion

Transaction fees in zkSync cover the costs of execution and proof generation. Despite their modest size, even for complex operations, these fees are subject to the gas price in Ethereum. Therefore, they depend not only on network load but also on the prevailing gas price in Ethereum. For a more detailed understanding of gas fees in zkSync, we recommend referring to the official documentation.