ZeroSwap: DEX based on ZK-Rollup


 611 total views

In the DeFi field, DEX is the most important component. According to Duneanalytics’ statistics, DEX trading volume in the past 7 days has exceeded US$4 billion. Uniswap’s transaction volume on the same day also exceeded 300 million US dollars. These transactions are all real transactions recorded on the chain and cannot be faked.

At the beginning of this year, the amount of assets locked on Uniswap was only about 15 million U.S. dollars. Even on July 25, two months ago, it was only more than 80 million U.S. dollars. Only more than two months later, Uniswap’s locked assets exceeded 2 billion. The US dollar shows its rapid development.

(Uniswap’s locked assets soared from less than 100 million to over 2 billion US dollars in two months)

The problem of DEX

Although DEX is developing rapidly, it is not without its flaws under the grand occasion. Today’s DEX trading volume is often driven by speculative trading, mining and other factors, which cover up some issues that need attention. These problems include high cost, slow speed, low throughput, and poor user experience. Users who have participated in mining have a deep understanding that during the peak period of congestion, mining gas costs tens of dollars, or even hundreds of dollars.

ZeroSwap:基于ZK-Rollup的 DEX (High gas costs on Ethereum during congestion)

Since most of the DEX execution in the AMM mode is completed on the chain, each transaction and each operation need to wait for the block confirmation, which causes the transaction speed to be too slow. For users, swap is meant to be exchange, but the exchange experience is not ideal.

At present, most DEXs are based on Ethereum. The TPS of Ethereum is about less than 20. For the increasing transaction demand, this level of throughput is obviously not enough.

DEX’s technical outlet

The current DEX users are still relatively small crypto users, even so, they are overwhelmed. Assuming that DEX wants to compete with CEX, to form sufficient competitive pressure on CEX, it also needs to solve the aforementioned infrastructure lag problem.

In order to solve these problems, there are currently two options. One is to leave Ethereum and enter other public chains for construction, just as Serum currently builds DEX on Solana; the other is to stay on Ethereum and use Layer 2 technology to expand , While waiting for the sharding of Ethereum.

The advantage of building DEX on other public chains is that problems such as cost, speed, throughput, etc. can be greatly alleviated; the disadvantage is that it takes a long time to build from scratch, because DeFi already has enough on Ethereum Many Lego technologies have formed a certain degree of network effect, and Ethereum is also higher in network security.

There is a second solution that is still based on Ethereum, but makes full use of Layer 2 technology to solve the existing problems of DEX. The Blue Fox Note also mentioned the Layer 2 track before. Among them, Zk-Rollup has its advantages in security, and it can also solve problems such as cost and throughput, which is suitable for the construction of DEX.

ZeroSwap’s Layer 2 solution

ZeroSwap is a DEX based on Zk-Rollup technology. ZK-Rollup is a Layer 2 solution. ZeroSwap attempts to solve the above-mentioned problems of DEX. Its core is to realize low-cost and fast exchange experience through the application of Zk-Rollup technology, and it also has a high level of security.

Use Zk-Rollup technology to transfer ERC20 tokens to Layer 2. All token transactions and exchanges take place in layer 2. However, since it can ensure the consistency of the state of layer 1 and layer 2 through continuously generated zero-knowledge proofs, There is no need to wait for the block confirmation time, no need to pay high gas fees, to achieve faster exchanges, and at the same time have layer 1 level of security, which is a token exchange experience close to CEX.

Specifically, ZeroSwap utilizes the Zk-Rollup technology of zkSync. zkSync is an implementation of Zk-Rollup technology. The first is that the user submits the signed transaction to the verifier. The verifier performs rollup operations on multiple transactions, merges them into one block, and sends the root hash of the updated contract state tree and the corresponding SNARK proof to the contract on the chain. The verifier will also send the state increment corresponding to the transaction to the chain, so that people can reconstruct the state after the transaction.

Since the SNARK proof and the state increment will be verified by the contract on the chain, it can prove the legality of all transactions and the availability of block data. Therefore, the verifier cannot tamper with the state, nor can it embezzle the funds on Layer 2, because all state changes require corresponding proofs, which cannot be forged.

In addition, in this process, funds are not escrowed to a third party, and the private key is still in the control of the user. From the implementation of Zk-Rollup of zkSync, it can be seen that the core idea of ​​Zk-Rollup is to analyze and verify these transactions through a large number of transaction aggregation and smart contracts on the chain, and use zero-knowledge proof technology to put the proof of these aggregated transactions on the chain. So as to realize the token transaction at the layer 2 level.

In this process, most of the calculation and storage takes place off-chain, the data stored on the chain is greatly reduced, and funds are locked in smart contracts. At the same time, because the SNARK proof and transaction status increments are stored on the chain, even if the verifier is not working, users can restore the transaction through the data on the chain and retrieve the locked tokens. This can ensure the security of the token, and the user still controls the private key. In this respect, it is no different from the current Layer 1 DEX. Under the premise of ensuring security, it can greatly reduce transaction costs, accelerate transaction speed, and achieve several levels of experience improvement.

The gas consumption of SNARK verification is much lower than that of verifying a large number of transactions, and the cost of storing the complete state off-chain is also much lower than storing on-chain. In theory, Zk-Rollup can realize the scalability of Ethereum through layer 2 while greatly reducing transaction costs.

Currently, ZeroSwap implements layer 2 technology based on ZK-Rollup of zkSync, which is suitable for constructing a secure decentralized exchange protocol. It supports basic token exchange, including depositing tokens, transfer and exchange, and withdrawing tokens. Depositing tokens refers to the transfer of tokens from Layer 1 to Layer 2 of zkSync; transfer and exchange refers to the transfer of tokens and token transactions on Layer 2. The advantage is that there is basically no handling fee; the last is Token withdrawal, transfer the tokens on Layer 2 back to the Layer 1 account.

Security of ZK-Rollup

ZeroSwap builds a decentralized DEX protocol based on Zk-Rollup technology. On the surface, ZeroSwap is a DEX similar to Uniswap’s AMM model, but from the perspective of the underlying technology implementation and user experience, there are certain differences between them.

The biggest difference between it and the current Uniswap is that it can greatly reduce transaction costs, greatly increase transaction speed, and achieve an exchange experience close to CEX, while also ensuring Layer 1 security. Taking into account other Layer 2 solutions, such as Optimistic rollups and Validium technologies, although they can also greatly increase transaction speed and reduce transaction fees, they have made a greater balance in security.

ZeroSwap:基于ZK-Rollup的 DEX (Comprehensive comparison of six layer2 technologies, Source: Alex Gluchowski)

In view of ZK-Rollup’s emphasis on safety, DEX based on Zk-Rollup is the future development trend. From the perspective of ZeroSwap’s plan, it is expected to be launched at the end of October. Since Uniswap’s Layer 2 has not yet been launched, ZeroSwap may become the first DEX to launch an AMM model based on ZK-Rollup.

The overall framework of ZeroSwap

In terms of the big framework, the ZeroSwap system includes on-chain smart contracts, off-chain ZeroSwap server, zero-knowledge proof system, and front-end user interface. The details are as follows:

ZeroSwap:基于ZK-Rollup的 DEX (The overall framework of ZeroSwap, Source: ZeroSwap white paper)

On-chain smart contracts are a set of smart contracts deployed on Ethereum. The user’s tokens will be deposited in it, and it will also record and verify Layer 2 status updates and corresponding proofs. It is a bridge connecting the chain and the chain. The Layer 2 server of ZeroSwap processes off-chain transactions. On the one hand, it interacts with users through interfaces, and on the other hand, it monitors transactions on the Ethereum chain.

The transaction request will be put into the memory pool of ZeroSwap, the block proposer will rollup the transaction to generate a new block, and the state Keeper will update the status of the token in Layer 2 and send it to Committer. Committer communicates with Prove server to obtain proof of corresponding transaction. These states and the corresponding SNARK proof will be sent to the ZeroSwap smart contract on the chain.

ZeroSwap’s zero-knowledge proof system has two characteristics. One is that it has a distributed architecture. Its Prove server supports multiple Prove, and multiple Prove can query the Prove server’s proof task, and then return to the Prove server after generating the proof. The second is to use PLONK’s zero-knowledge proof algorithm, which can greatly reduce the threshold for using zero-knowledge proof.

PLONK’s global trust setting only needs to be generated once, and the circuit scale can be reused within a certain range. The transaction process of ZeroSwap will be reflected in the changes of its state tree. ZeroSwap’s state tree is a Merkel tree with a height of 34, which is used to record the balance status of all accounts in the current system.

The child nodes of the root node Root are all account nodes in the system. Account nodes include ordinary account nodes and Pair account nodes. Ordinary account nodes are used to record the status of all tokens in the account. There can be multiple leaf nodes, and each leaf node represents a type of token and its quantity. The Pair account node is used to record the status of a certain transaction pair fund pool in ZeroSwap. It only contains two leaf nodes, and each leaf node represents the balance and type of one of the tokens in the fund pool.

ZeroSwap:基于ZK-Rollup的 DEX (State tree on ZeroSwap, Source: ZeroSwap white paper)

In summary, ZeroSwap has its own characteristics in technology. For example, it uses the Plonk certification system. Its ZK certification circuit can support different sizes. Its initial credible setting only needs to be set once, and it can be upgraded on existing settings; in Layer 2 The exchange logic of Uniswap is realized at the level, and it may become the first DEX to implement the AMM model based on ZK-Rollup. If it is successfully implemented, it can greatly reduce user fees and speed up transactions.

Behind the user operation of ZeroSwap

Compared with the current DEX on Layer 1, what operations are available on ZeroSwap based on Layer 2? What is the difference between these operations and the DEX on Layer 1? From the perspective of user front-end operations, ZeroSwap has more operations for depositing and withdrawing tokens than Uniswap, and other operations are basically the same.

From a trader’s perspective, what operations are required to complete a transaction on ZeroSwap? What is behind these operations?

Compared with DEX transactions on Layer 1, DEX transactions on Layer 2 need to deposit tokens from the Layer 1 account to the Layer 2 account before the token can be exchanged or transferred. This will involve the deposit, transfer, exchange and withdrawal operations of tokens.

The first is to deposit tokens into the Layer 2 account. The user deposits the tokens on the Ethereum chain into the ZeroSwap contract. Once ZeroSwap detects that the user has transferred the tokens to the ZeroSwap contract on the chain, it will update the state tree according to the details of the transaction. ZeroSwap will find the corresponding account based on the transaction and update the balance of the token in the account based on the deposited amount. If the account does not have a leaf node of the corresponding token, the leaf node of the token will be created first, and then the status will be updated. The status update of the leaf node will cause the hash of the root node to change. The root node hash will be sent to the ZeroSwap contract on the chain together with the SNARK proof deposited in the transaction.

The second is to transfer and trade on Layer 2. Transferring on ZeroSwap is the process of sending a token from one account to another. The transfer is also initiated by the user on Layer-2. After the ZeroSwap Server receives the transfer request, it finds the corresponding sending and receiving account and updates the status of the token balance under these two accounts. The hash of the root node of the updated state tree and the SNARK proof corresponding to the transfer are sent to the ZeroSwap smart contract.

However, it is not a token transfer on the chain, because the token is still in the ZeroSwap contract. Users conduct transactions on Layer 2, which essentially exchanges tokens in the fund pool with their own tokens and tokens in Layer 2. The user sends a certain token under the ZeroSwap account to the corresponding token pair account, and ZeroSwap calculates the number of corresponding tokens that the user can obtain according to the AMM algorithm, and sends it to the user account.

At the same time, ZeroSwap sends the updated hash of the root node of the state tree and the SNARK certificate corresponding to the exchange operation to the ZeroSwap contract. After the token exchange, just like the transfer operation, the status of the token on the chain will not change because the token is still in the ZeroSwap contract.ZeroSwap:基于ZK-Rollup的 DEX (Exchange operation process on ZeroSwap, Source: ZeroSwap white paper)

Finally, unlike the DEX on Layer 1, the DEX tokens on Layer 2 must be returned to Layer 1, and the user also needs to withdraw the tokens from the Layer 2 account to Layer 1. When a user withdraws tokens from ZeroSwap, first the user initiates on Layer 2, then the ZeroSwap server will receive the user’s withdrawal request, and then update the corresponding token status under the user account and update the root of the status tree The node hash and the SNARK proof of the extraction operation are sent to the ZeroSwap contract on the chain. The contract is verified, and then the corresponding tokens locked in the contract are sent to the user’s account on the chain.

This is similar to the traditional CEX withdrawal operation, but the difference is that users can freely control the withdrawal of ZeroSwap DEX, while the withdrawal of CEX is limited to the CEX institution. In the DEX of the AMM model, a trader must first have a liquidity pool of token pairs to complete a transaction.

So from the perspective of a liquidity provider, how to provide liquidity on ZeroSwap and how to remove liquidity? What is the principle behind it?

How do liquidity providers add liquidity?

First, the user initiates a request for adding a certain token to liquidity on Layer 2. After receiving the request, the ZeroSwap server will find the account of the corresponding requestor and the token pair account, and then according to the AMM algorithm requirements, the two types of user accounts will be The tokens are transferred to the token pair account. The system calculates the number of LP tokens that users can get based on the current amount of funds in the token pool. LP tokens represent the user’s equity share in the liquidity pool. The hash of the root node of the state tree after the state update is sent to the ZeroSwap contract together with the corresponding proof of added liquidity.

ZeroSwap:基于ZK-Rollup的 DEX (Inject liquidity into the token pool, Source: ZeroSwap white paper)

How do liquidity providers remove liquidity?

First, the user initiates a request to remove liquidity on Layer 2. After receiving the user’s request, the ZeroSwap server will find the account of the corresponding requester and destroy the corresponding amount of LP tokens. At the same time, the token pair account will return the two tokens to the requester’s account in proportion to the number of burned LPs. The hash of the root node of the state tree after the state update is sent to the ZeroSwap contract together with the corresponding proof of liquidity removal operation.

Finally, there is also an operation to withdraw liquidity on ZeroSwap. It does not remove liquidity, but withdraws LP tokens in the liquidity pool from Layer 2 to Layer 1. From the perspective of the operation process, it is consistent with the withdrawal of other tokens from Layer 2 back to Layer 1. But the difference is that LP tokens also have a “minting”, which needs to be generated in Layer 1 and sent to the corresponding account.

When will ZeroSwap be launched? According to the plan released by ZeroSwap, it can be used on the Ethereum testnet on October 10, 2020. It supports the creation and exchange of Layer 2 transaction pools, as well as the addition and removal of liquidity; on October 20, 2020, it supports multiple transaction pools. The combination. It was officially launched on October 30, 2020 and launched on the Ethereum mainnet, supporting various functions similar to Uniswap. On December 30, 2020, realize the circuit optimization of the Plonk proof system and the GPU optimization of the proof performance; start liquid mining at the beginning of November 2020.

Layer 2 and the development of DEX

DEX is not only an important development of DeFi, but also one of the largest developments in the entire encryption field. In the field of encryption, CEX is the biggest value capturer. Today’s DEX has gradually evolved into a substantial opponent of CEX.

However, the current DEX still has shortcomings in terms of scalability, transaction fees, transaction speed, transaction experience, etc., and cannot reach the level of CEX. However, with the launch of Layer 2 solutions, especially the launch of Zk-Rollup technology, DEX will gradually resolve its shortcomings. Once these technical shortcomings are resolved, DEX has the opportunity to move towards large-scale crowd adoption, and in fact become a true opponent of CEX.


Risk warning: All articles of Blue Fox Note cannot be used as investment advice or recommendation. Investment is risky. Investment should consider personal risk tolerance. It is recommended to conduct in-depth investigation of the project and make your own investment decisions carefully.