Math is the Fundamental Science at the Heart of DeFi's Complex Mechanisms

The first publicly recorded exchange of bitcoins for fiat currency took place in September 2009. Since then, a wide range of trading platforms, such as centralized exchanges (CEX) and decentralized exchanges (DEX), have been available to users. While the User Interface (UI) is clear and familiar, not everyone knows how the backend of these two fundamentally different platforms works.

The backend part of CEX is built on familiar software for WEB 2.0 application development. The only built-in part of Web 3.0 in CEX: cold or hot wallets (blockchain addresses). The balances operated by users are nothing more than records in a “traditional” database based on deposits made or trading activity.

The DEX backend, on the other hand, is made entirely of Web 3.0 technology and consists of a set of smart contracts executed on an EVM or other type of blockchain. It is the backend component that DEX applications value so much, because if something happened to the UI or developers disabled it intentionally, the smart contracts and public blockchains are unstoppable and don't go anywhere.

However, there are different concepts of how DEX works. Globally, we can divide DEX into 2 categories: Automatic Market Maker (AMM) based, which involves liquidity pools - a collective reserve; and Order Book based trading. Below we will consider these two mechanisms, as well as their varieties.

AMM

AMM is a mathematical formula-based algorithm that is designed to control liquidity and pricing on DEX. AMM requires liquidity pools, which are smart contracts for depositing tradable assets into them in exchange for various financial incentives. Users who deposit their assets into a liquidity pool are called liquidity providers (LPs).

AMM calculates a price based on the ratio of assets in the liquidity pool. Below is a list of different mathematical models for AMM. General formulas will be provided only for the first three, as describing CLMM and more complex variants would require multiple detailed formulas.

Let us introduce the basic elements of equations to understand the formulas:

In the 7 years since the first DEX was introduced, several AMM models have been developed

Used on DEX such as: Balancer, Beethoven X, __Composable Finance__and others.

Virtually any DEX on which different types of assets are traded is hybrid, using different AMM models for pools.
Protocols such as Bancor apply different mathematical models for buying and selling even within the same pool.

CLMM allows LPs to receive more commissions with less capital. Traders also benefit as slippage is reduced. However, contrary to popular belief, CLMM does not protect against impermanent losses. On the contrary, when the price goes out of the specified range, LP stops receiving commission, but at the same time their funds remain in the pool and are subject to non-permanent losses.

There is a study showing that in the first 6 months after the release of Uniswap v3, LP losses were significant. Despite the fact that the study was conducted by a competing Wapsor protocol, CLMM does require LP vendors to monitor market conditions and their positions more closely and re-balance assets more frequently. According to other studies, active LPs are increasingly benefiting from CLMM.

Since Uniswap v3 moved to the GPL license in 2023, and has good documentation, many DEXs used this protocol as the core for their counterparts: SushiSwap, PancakeSwap, Dex223 and others.

There are alternative protocols that claim to use unique approaches to CLMM: Cetus, Trader Joe's, Bancor, and others.

The Bancor protocol works with__one-way liquidity__. Bancor claims that their v3 protocol protects users from volatile losses due to its mechanics.

You can clearly see that many protocols appear to use several AMM models at once, both in different protocol versions (v1, v2, v3...), and within the same protocol version and even within the same pool.

Liquidity Aggregators

In fact, it is not a separate type of AMM, but it has its own peculiarity. Liquidity aggregators may not have their own liquidity pools at all, but connect to the pools of other DEX in the network, thereby increasing the liquidity ratio and providing an optimal route for making a transaction.

The largest and most successful representative of__1inch__, being the most advanced in terms of both user base and development of innovation. In addition to aggregating liquidity within a single blockchain, 1inch is actively exploring the direction of cross-chain exchanges.

Other examples are DODO, OpenOcean, Velora, Rango Exchange, KyberSwap Aggregator and others.

Limit Orders on DEX with AMM

It is worth mentioning separately the implementation of limit orders on top of AMM. DEX based AMM has often been criticized for not providing users with the ability to create orders different from the current market price, as implemented in the order book on CEX. Challenged, a limit order mechanism on top of the AMM has been developed and implemented.

The way this one works is as follows: the user sets the order parameters in the UI, signs a transaction to authorize spending from his address to the Router's smart contract. Once the price in the specified pair reaches the specified value, the smart contract will execute the order.

Some DEXs, like PancakeSwap, lock the user's funds in the contract when a limit order is formed, similar to locking funds in a stock stack on CEX. 1inch, on the contrary, deducts funds only at the time of the transaction and if there are no funds on the user's address at the time of the transaction, the order will simply not be triggered.

As such, there is no traditional order book on aDEX with AMM. Therefore, traders do not see each other's orders as it is realized on traditional CEX.

Almost all popular and active DEXs are used, as in the case of CLMM, the presence of limit orders is now a standard for DEXs. It is easier to find an exchange that has not yet implemented limit orders or announced their introduction. DEXs such as OpenOcean aggregate not only DEXs but also CeFi.

DEX with Different Asset Standards in the Liquidity Pool

Most liquidity pools are designed to work with ERC-20 tokens. However, there are DEXs that support incompatible token standards.

It should be clarified that trading native blockchain coin or ERC-677, ERC-777 tokens does not support different standards. In fact, DEX works through the conversion of native coin to ERC-20 wrapper token and back. And the standard ERC-677 and similar have in their set of functions similar to ERC-20, so such hybrids for DEX are indistinguishable from ERC-20.

At the moment there is only one standard of interchangeable tokens having different architecture from ERC-20, it is ERC-223. And only one AMM-based DEX capable of working with the ERC-223 reference is DEX223.

DEX223 name alone indicates that it is focused on the ERC-223 standard, but its liquidity pools contain two token standards at once: ERC-223 and ERC-20. For seamless operation of different token standards and to reduce liquidity fragmentation, EIP-7417 token converter is included in the algorithm. DEX223 is based on the Uniswap v3 protocol and as a consequence the CLMM model.

DEX223 utilizes the interesting concept of heterogeneous liquidity pools in terms of interchangeable token standards. Given the increasing tokenization of real-world assets, this approach may be in demand in the future.

Order Book

Order book trading on DEX is similar to that on CEX. Instead of liquidity pools, users fill the order book's exchange stacks with orders. Each action: placing an order, removing an order, “trading on the market” is a transaction that transfers data and assets into a contract and of course requires a gas payment. However, unlike CEX, the user is the owner of its assets and can remove them from the contract even if the UI is unavailable.

Formally, there are quite a few order book based DEXs, for example: dYdX, Serum, Loopring, 2bears.exchange. However, dYdX cannot be called a DEX, because it uses Central Limit Order Book (CLOB), the actual analog of CEX. In addition, it censors users by their location.

An interesting example of DEX based on order book is 2bears.exchange:

In addition, 2bears has a cinematic creation story “from a small garage”. 2bears.exchange was created by just two engineers, without raising any capital through grants and investments.

Conclusion

In the 7 years since the launch of the first AMM-based DEX, and according to Bancor in 8 years, DEX remains the main product of the DeFi segment.

The competition of DEX among themselves and with CEX forces progress to move forward, so:

All of these processes, however, are nothing but the development of human culture and technology perfected earlier. For example, an early description of CFMM was published by economist Robin Hanson in his paper “Logarithmic Market Valuation Rules for Modular Combinatorial Information Aggregation” in 2002. And the first DEXs are actually not Bancor and Uniswap, but NXT Asset Exchange launched in 2013.

Undoubtedly, the development and evolution of the DeFi segment will continue. It’s likely that great mathematicians such as Pythagoras and Archimedes would have admired how their foundational discoveries are now transforming the financial landscape – truly illustrating mathematics' enduring power to shape our world.