Math is the Fundamental Science at the Heart of DeFi's Complex Mechanisms 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. backend backend 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. WEB 2.0 application WEB 2.0 application cold or hot wallets cold or hot wallets 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. EVM EVM 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. Automatic Market Maker Automatic Market Maker Order Book Order Book AMM 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. 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. 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. The Constant Sum Market Maker The Constant Sum Market Maker 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 and Curve and others. It is a part of CFMM, CLMM and other AMMs. 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 and Curve and others. It is a part of CFMM, CLMM and other AMMs. 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. Constant product value market maker Constant product value market maker The most common version of AMM, appeared already in Uniswap v1. It is used by almost all popular DEX: SushiSwap, PancakeSwap, Uniswap, Balancer and Curve and others. It is a part of CFMM, CLMM and other AMMs. Uniswap v1 Uniswap v1 SushiSwap SushiSwap PancakeSwap PancakeSwap Uniswap Uniswap Balancer Balancer Curve Curve 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. 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, Beethoven X, __Composable Finance__and others. Beethoven X Beethoven X Composable Finance 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. 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. 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. A hybrid market maker A hybrid market maker The clearest example is the StableSwap function on PancakeSwap combines the CPMM and CSMM formulas for stablecoin trading pairs. StableSwap StableSwap Used on: Shell Protocol, Curve, PancakeSwap, Balancer and others. Shell Protocol Shell Protocol 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. 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. Concentrated Liquidity Market Maker Concentrated Liquidity Market Maker 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. study study Uniswap v3 Uniswap v3 studies studies 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. Cetus Cetus Trader Joe's Trader Joe's Bancor Bancor The Bancor protocol works with__one-way liquidity__. Bancor claims that their v3 protocol protects users from volatile losses due to its mechanics. one-way liquidity its mechanics its mechanics 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. 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. 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. Dynamic Automated Market Maker Dynamic Automated Market Maker Uses: Bancor,KyberSwap Elastic, Trader Joe, Shell Protocol and others. KyberSwap Elastic KyberSwap Elastic 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. 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. 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. A proactive market maker A proactive market maker Uses:DODO (the most famous and original, built entirely on PMM), iZUMi Finance, __WOOTRADE__and others. DODO DODO iZUMi Finance iZUMi Finance WOOTRADE 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. 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. Virtual Automated Market Makers Virtual Automated Market Makers Perpetual Protocol v1 Perpetual Protocol v1 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. 1inch development of innovation development of innovation Other examples are DODO, OpenOcean, Velora, Rango Exchange, KyberSwap Aggregator and others. OpenOcean OpenOcean Velora Velora Rango Exchange Rango Exchange 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. token standards 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 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. EIP-7417 EIP-7417 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. dYdX dYdX Serum Serum Loopring Loopring 2bears.exchange 2bears.exchange Central Limit Order Book Central Limit Order Book 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). 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. Uniswap has already released the v4 protocol, extending 'hooks' to third-party developers. v4 protocol v4 protocol 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 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. Robin Hanson Robin Hanson NXT Asset Exchange NXT Asset Exchange 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.