Cointime

Download App
iOS & Android

Clober:On-Chain Order Book DEX for The Ethereum Ecosystem

Abstract

Clober presents a new algorithm for order book DEX “LOBSTER — Limit Order Book with Segment Tree for Efficient oRder-matching” that enables on-chain order matching and settlement on decentralized smart contract platforms. With Clober, market participants can place limit and market orders in a fully decentralized, trustless way at a manageable cost.

DEX 2.0 — On-chain Order Book DEX

The emergence of AMM and the dawn of DeFi

Exchanging between assets is the most foundational component of financial activities. In the early days of Ethereum, there were many projects trying to build decentralized financial applications. However, before we had the means to exchange assets effectively in a decentralized way, their adoption was limited since any financial activities accompanying swaps between assets should have relied on centralized infrastructures. When Uniswap launched in 2019, it introduced a new way of providing liquidity for swaps between assets efficiently on a low-throughput execution environment and opened up a new era for the blockchain industry called “DeFi.”

AMM enabled us to make a swap between arbitrary sets of assets in a decentralized way — which means that it is non-custodial, trustless, and permissionless. Leveraging this infrastructure, many protocols such as DeFi protocols or DAOs were able to mint their own governance token and provide liquidity for it — to build community, raise capital, incentivize contributors, etc. Also, a certain form of AMM specialized for swaps between low-volatility pairs — which is called a “Stable Swap” — was introduced, which significantly contributed to the growth of the decentralized stablecoins by reducing their volatility.

Limitations of AMM-centric DeFi ecosystem

However, as the DeFi ecosystem grew, the pitfalls of AMMs emerged as well. The fundamental limitation of AMMs lies in the fact that users cannot place limit orders on them. This means that market makers have no control over the price range on which to provide liquidity or the margin they take from providing liquidity — i.e., the bid-ask spread.

For example, for high-volatility pairs, the risk of ‘Impermanent Loss’ might exceed the expected revenue from liquidity providing. Under a traditional order book environment, market makers would have increased the bid-ask spread to offset the volatility risk. However, it is not possible on AMM protocols. This is why many DeFi protocols provided a massive volume of inflation rewards to the liquidity providers of their governance tokens, which imposed a strong sell pressure on their prices and eventually made them all look like a bunch of decentralized Ponzi schemes.

Stable swaps handle liquidity quite efficiently when all things are good, but when a poorly designed stablecoin starts to depeg it creates severe losses for those who supplied the liquidity.

The future of DEX beyond AMMs

For more sophisticated DeFi protocols to emerge and expand the territory of the decentralized economy, there must be an on-chain order book infrastructure as their backbone, which enables a generalized form of exchanges between assets with a high degree of freedom.

In the case of Solana, powered by its high throughput, an on-chain order book DEX called “Serum” has already secured its dominant place in the ecosystem, becoming a necessary infrastructure that most of the DeFi protocols integrate with. Solana’s had-been leading perpetual swap protocol “Mango Market” wouldn’t have had-been so successful had there been no order book infrastructure like Serum.

For another example, the Dydx protocol has become the most successful decentralized perpetual futures platform by leveraging the order book system — although it’s off-chain. Many alternative on-chain perpetual futures protocols utilize AMMs as their exchange infrastructure, but most are far from successful.

It is undeniable that order book DEXs will eventually replace AMMs sooner or later, as smart contract platforms scale and gas costs are reduced. With the emergence of the on-chain order book DEX, a new era of DeFi will open.

Why on-chain

Off-chain order books require centralized operators to run the matching engine off-chain. This means that all kinds of typical risks of centralization apply to them. For example, if the operators get sanctioned by a government authority, the platform might not be able to be further maintained, or its users’ assets might get censored. Also, a malicious operator — or a hacker who compromised the security of the operator — can censor or manipulate users’ trading activities.

In addition, one of the most important merits of DeFi is that it is composable. Each DeFi protocol can leverage other DeFi protocols as its infrastructure, which creates a strong network effect between protocols. For example, one may combine a lending protocol and a DEX protocol to create a margin trading platform. A DEX aggregator can aggregate multiple on-chain DEX protocols to provide large liquidity with no liquidity of its own. An open maker order on an on-chain order book itself can be considered as a stand-alone asset that can be utilized in other protocols — e.g., borrowing USDC against a sell order on the order book. Conversely, off-chain or app-chain-based DEX protocols cannot provide such properties. Smart contracts cannot call off-chain functions. Positions on off-chain DEX protocols cannot be utilized in on-chain protocols.

In short, off-chain order books are inferior in terms of trustlessness, censorship resistance, and interoperability, therefore cannot replace AMM DEXs.

Why it is difficult to build an on-chain order book

Under the typical implementation of order books, each taker order transaction should process the settlement of an arbitrary number of counterparty orders that are matched with itself. This means that the computation cost of a taker order increases proportionately to the number of maker orders it is matched with. Because there’s no limit on this number, the gas cost of a taker order can grow very high — can even exceed the block gas limit, in which case the transaction will fail. For example, let’s say there are 100,000 sell orders of size 1 ETH at a price of 1,500 USDC. If someone tries to buy 50,000 ETH with 75,000,000 USDC, the transaction is likely to be rejected because of its high gas cost, even though there are enough counterparty orders to be matched with it. This is why Serum, a leading order book DEX protocol on Solana, has a parameter to limit the number of matching orders in its taker order interface.

Clober’s solution

We want to reduce the linear overhead of taker orders. To do so, we make taker orders only keep track of the “Total Claimable Amount,” which is the sum of taken orders at each price point, instead of processing all the matching orders. We then let market makers claim the proceed of their filled orders themselves by simply comparing the “Claim Range” of their order and the “Total Claimable Amount”. “Claim Range” is an interval representing the position of a maker order in the order queue at its price point. If the “Total Claimable Amount” is bigger or in between an order’s “Claim Range”, the order is fully or partially claimable.

The non-trivial part of this implementation is keeping all of the Claim Ranges up to date. For example, if my Claim Range is [10, 13] and an order with Claim Range [8, 10] is canceled, my order would need to be updated to [8, 11] and all orders behind mine would also need to be updated. If a canceling transaction had to process all the range updates, it would be too expensive and possible to fail. We can overcome this by adopting a data structure called “Segment Tree.”

A segment tree is a data structure that stores a list of numeric values, which enables efficient “range queries” — calculations on the partial sum of its subarrays. The complexities of insertion, deletion, and range query of the Segment Tree are all logarithmic. Leveraging the Segment Tree, we can keep track of the Claim Ranges of maker orders at each price point with low gas costs.

Based on this architecture, along with some additional optimizations, we were able to reduce the gas cost of both maker and taker orders, similar to that of a transaction on Uniswap.

For more information about our tech, please find our technical litepaper here.

Closing Remarks

AMMs, while possible, are not utilized in CeFi because of their poor performance in providing liquidity. They only exist in DeFi because of their passive market-making abilities, alleviating the need to constantly pay obsessive gas fees. However, as layer 2 technologies develop, the trend is projecting ever-lower gas fees, opening up a market for order books to replace swaps done on AMMs. Eventually, order books will become the preferred way of swapping assets, just like it always has been for CeFi.

Comments

All Comments

Recommended for you

  • NFT transactions on the Bitcoin chain exceeded $55 million in the past 7 days

    According to CryptoSlam data, the Bitcoin on-chain NFT transaction volume in the past seven days reached 55.02 million US dollars, Ethereum NFT transaction volume reached 48.7 million US dollars, and Solana NFT transaction volume reached 24.62 million US dollars.

  • USDe issuance is nearly 2.3 billion

    According to Etherscan data reported, the stablecoin USDe issued by Ethena Labs has reached a circulation of 2,292,060,769. USDe is a stablecoin based on Ethereum, which is collateralized by derivatives and achieves price stability through delta-neutral hedging in centralized and decentralized venues. To create USDe, Ethena allows users to use USD, ETH, or liquidity collateral tokens as collateral.

  • CEL breaks through $0.55, with a 24-hour increase of more than 75%

    According to market data, CEL has broken through $0.55 and is currently priced at $0.5651, with a 24-hour increase of over 75%. The market is volatile, so please be prepared for risk control.

  • SlowMist: The total loss from security incidents last week (April 28-May 4, 2024) exceeded US$71.4 million

    According to the weekly security report (April 28 - May 4, 2024) released by SlowMist, the total loss this week exceeded $71,399,000. An incident this week resulted in losses rising from the nine-digit range to the astonishing ten-digit range. Surprisingly, this was not due to complex technical flaws or sophisticated phishing scams, but a simple error that could have been easily avoided by implementing a whitelist. Multiple security incidents include:

  • Tether issued USDT worth $240 million on May 4 and redeemed USDT worth $8.6 million

    According to ChainArgos monitoring, on May 4th, Tether conducted a large-scale issuance and redemption activity. A total of $240 million USDT was issued that day, while $8.6 million USDT was redeemed.

  • Ethereum stablecoin transaction volume breaks monthly record in April

    The total transaction volume of stablecoins on Ethereum last month was significantly higher than any previous month, but most of the trading volume was contributed by a single stablecoin, DAI. As previously reported by The Block, DAI is increasingly being used for complex MEV transactions, usually involving flash loans, with a large portion of DAI being minted and returned in a single transaction. Just one of these transactions increased DAI trading volume by nearly $1 billion.

  • The Ethereum network has currently destroyed more than 4.28 million ETH

    According to Ultrasound data, as of now, the Ethereum network has burned a total of 4,287,144.91 ETH. Note: After the Ethereum London upgrade introduced EIP-1559, the Ethereum network will dynamically adjust the BaseFee of each transaction based on transaction demand and block size, and this portion of the fee will be directly burned and destroyed.

  • Cointime May 5th News Express

    1.The Federal Reserve reduced its balance sheet by $77 billion in April, and the size of its balance sheet fell below $7.4 trillion2.Former Bitmex CEO: Bitcoin will trade between $60,000 and $70,000 before August 3.SLERF total destruction exceeds 7 million USD4.ether.fi large staker initiates pledge withdrawal application for 37,140 ETH5.Web3 digital asset company Alpha Transform Holdings makes strategic investments in Arhasi and Cloudbench 6.A trader spent 402 ETH to buy 732,326 FRIEND, with an unrealized profit of $653,0007.A certain address has sold a total of 677,197 FRIEND airdrops through BunnySwap, making a profit of approximately $1.15 million8.A multi-signature wallet withdrew 915.85 billion PEPE from Binance9.The NFT project Blob team engraved the rune EPIC•EPIC•EPIC•EPIC on the Epic Satoshi block of Bitcoin’s fourth halving10.On-Chain Analyst Predicts Six to Twelve Months of 'Parabolic Advance' for Bitcoin

  • Cointime May 4th News Express

    1. Hong Kong Bitcoin Spot ETF has held 4,218 BTC since its listing three days ago

  • Blockchain Asset Management announces launch of a dedicated blockchain fund for accredited investors

    Blockchain Asset Management, a cryptocurrency fund with a scale of $100 million, announced the launch of an exclusive blockchain fund for qualified investors. The specific amount of funds raised by the fund has not been disclosed yet, but it is said to have reached "eight figures", which means it is in the tens of millions of dollars. In addition, the investment threshold for the new fund is $100,000, and all investors are required to meet the approved standards (annual income exceeding $200,000, net assets exceeding $1 million).