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
The backend part of CEX is built on familiar software for
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
However, there are different concepts of how DEX works. Globally, we can divide DEX into 2 categories:
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
-
The Constant Sum Market Maker (CSMM) is simple but rarely used in practice because of its susceptibility to price manipulation and low liquidity in large trades. Nowadays it is almost impossible to encounter CSMM in its pure form, as the arbitrage can completely exhaust one of the assets in the pool if the asset price deviates. It can be found on some exchanges in the iteration of protocols v1. It is used as an element of CFMM.
-
Constant product value market maker (CPMM) provides high liquidity and is resilient to large price drops, but vulnerable to volatile losses to LPs. Does not allow liquidity pools to be depleted completely as the price of the more demanded asset tends to infinity.The most common version of AMM, appeared already in
Uniswap v1 . It is used by almost all popular DEX:SushiSwap ,PancakeSwap ,Uniswap ,Balancer andCurve and others. It is a part of CFMM, CLMM and other AMMs.
- Constant Mean Market Maker (CMMM) allows you to create pools with more than two asset types and uses a weighted geometric mean to maintain balance. Can offer greater flexibility and better capital efficiency for multi-asset pools. The CMMM model can be used for smoothing volatility or even trading between stablecoins.
Used on DEX such as: Balancer,
-
A hybrid market maker (CFMM) combines elements of different AMM models to optimize both liquidity and price stability to reduce problems such as volatile losses.The clearest example is the
StableSwap function on PancakeSwap combines the CPMM and CSMM formulas for stablecoin trading pairs.Used on:
Shell Protocol , Curve, PancakeSwap, Balancer 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.
Concentrated Liquidity Market Maker (CLMM) is a liquidity model that allows LPs to allocate their tokens in a selected price range where they will be actively used. Based on CPMM. It is currently the most popular AMM model, which is being adopted by existing DEXs and taken as a basis by startups. It is easier to find a DEX that does not use CLMM.
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
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:
The Bancor protocol works with__one-way liquidity__. Bancor claims that their v3 protocol protects users from volatile losses due to
-
Dynamic Automated Market Maker (DAMM), DAMM dynamically adjusts pricing and liquidity strategies according to market conditions in an effort to offer better capital efficiency and reduce price impact.Uses: Bancor,
KyberSwap Elastic , Trader Joe, Shell Protocol and others.
-
A proactive market maker (PMM) works by adjusting its prices according to real market trends and expert forecasts. The PMM's goal is to make sure that the prices on these platforms reflect what is happening in the broader financial market.Uses:
DODO (the most famous and original, built entirely on PMM),iZUMi Finance , __WOOTRADE__and others. -
Virtual Automated Market Makers (vAMM) do not contain actual assets, but memorize mathematical formulas to simulate trading and provide liquidity. They are mainly used in derivatives platforms to allow trading without traditional counterparts.
Many protocols that used vAMM in their work have moved to other AMM models in updated versions, such as dYdX. However, some still use it. Some still use vAMM:Perpetual Protocol v1 , ZKPerp. The protocol is heavily dependent on Oracles.
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
Other examples are DODO,
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
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 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,
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:
An interesting example of DEX based on order book is 2bears.exchange:
- does not censor users
- on-chain order book
- can work with both ERC-20 and reference ERC-223
- has built-in grid bots that can be used as a market maker for cheap blockchain transactions.
- verified smart contracts allow interaction via ABI interface without the need for UI.
- It has detailed documentation and comments in the code of smart contracts (though only in Russian so far).
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:
- Uniswap has already released the
v4 protocol , extending 'hooks' to third-party developers. - Bancor is sheltering traditional liquidity providers from volatile losses.
- 1inch is everywhere at once not only in the single blockchain universe but also in parallel universes with their cross-chain swap.
- DEX223 will combine the familiar with the trustworthy.
- 2bears.exchange provides an interesting alternative to other DEX models.
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
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.