<100 subscribers
Share Dialog
Share Dialog


In the current state of the Web3 industry, products related to DeFi dominate the market. Among these, AMM (Automated Market Maker) plays a crucial role and is a powerful driver of change in Web3 finance. This article will introduce several important AMM implementations in the Solana ecosystem, hoping to provide some guidance for LPs (Liquidity Providers) on how to choose their investment strategies.
CPMM CPMM (Constant Product Market Maker) is the most basic AMM implementation and is realized in many products. Here, we use Raydium's constant product-based AMM as an example. The constant product means that the supplies of the two tokens in the pool have a fixed product: X * Y = k.
For liquidity providers, when anyone adds liquidity (assets) to the pool, CPMM automatically creates an associated account for the wallet address and issues LP Tokens (each token pair has its own LP Token Mint). These LP Tokens are used to prove that the wallet address holds a share of a particular pool and will be destroyed when the liquidity provider withdraws funds.
The on-chain program of CPMM is developed using Anchor, and the program code can be found at raydium-io/raydium-cp-swap. Let's briefly examine how it implements the constant product.
Firstly, when users use Raydium's CPMM to swap tokens, the swap-related instruction will be triggered.
For example (only for illustrative purposes, this article is not responsible for any Token): when a user wants to swap USDC for TRUMP, they can do so through the TRUMP-USDC pool.
Pool
TRUMP
USDC
7XzVsjqTebULfkUofTDH5gDdZDmxacPmPuTfHa1n9kuh
6p6xgHyF7AeE6TZkSmFsko444wqoP15icUSqi2jfGiPN
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
For simplicity, let's ignore other instructions in the transaction and focus only on the Raydium part. We find Raydium CPMM: swapBaseInput.
In the Input Accounts, we can see that the input token is USDC and the output token is TRUMP. In the Solana ecosystem's AMM, LP pairs can be simply represented by Token Accounts, without the need to create and deploy a new contract program (such as the factory contract commonly used in Ethereum). When a transaction occurs, it directly interacts with Raydium's CPMM Program. The Solana program will modify the state of the corresponding Token Accounts through the pool address, token address, and other inputs to perform the swap operation.
For example, the code for the above swapBaseInput instruction can be found here. After a series of preliminary checks, the amount of the target token that can be obtained is calculated in ConstantProductCurve.swap_base_input_without_fees:
The formula used is:
That is, after the total amounts of TokenX and TokenY change, their product should remain constant. The left side of the equation is the product after the change, and the right side is the product before the change.
After mathematical transformation, we can obtain the conversion formula for Δy (the amount of y tokens we can obtain):
Which is the delta_y = (delta_x * y) / (x + delta_x) part in the code. Note that this calculation does not include fees, which have already been deducted in the preliminary logic of swap_base_input.
CLMM CLMM (Concentrated Liquidity Market Maker) is another AMM launched by Raydium. It is similar to Uniswap V3, where each token pair also has multiple fee tiers, and a corresponding pool can be created for each tier.
Since CLMM's implementation references Uniswap V3, many concepts and implementation methods can be referred to from Uniswap's implementation when learning it. It also inherits concepts from Uniswap, such as ticks, multiple fee tiers, and concentrated liquidity. More details can be found in the DEX development course launched by ZAN: DEX Development Practice - Uniswap Code Analysis - How Uniswap works - ZAN.
However, it is important to note that, similar to CPMM, due to the characteristics of the Solana blockchain, Raydium CLMM does not require a separate contract to be deployed for each pool, so there is no concept of a factory contract. This is different from Uniswap.
CLMM allows liquidity providers to select a price range when injecting funds, and the funds will only be distributed within the selected range:
For concentrated liquidity pools, tokens are distributed on both sides of the current price. The selected price range in the above figure includes the current price, so funds are allocated to both tokens in the pool.
We can also inject only one type of token to provide so-called one-sided liquidity (as shown in the figure below). This is somewhat similar to limit orders in traditional finance, where LP funds are only utilized when the token price reaches a certain range. However, this mode also involves more risk points to consider.
Generally, for pools with small price fluctuations, LPs tend to choose a smaller range; conversely, for pools with very volatile prices, they tend to choose a larger range. The goal is to prevent the current price from deviating too much from the selected range and causing too much impermanent loss.
It should be noted that while concentrated liquidity can improve the capital efficiency of LPs, it also demands a higher level of financial awareness from LPs. LPs need to actively manage their liquidity. If LPs are not responsive, frequent on-chain fluctuations can easily lead to significant impermanent losses for LPs.
DLMM DLMM (Dynamic Liquidity Market Maker) is an AMM product launched by Meteora. It also belongs to Uniswap V3 and is very similar to the CLMM mentioned above. DLMM also allows LPs to concentrate their funds within a certain range near the current price. However, there are some differences in the specific implementation of DLMM, and it provides some unique features.
DLMM introduces the concept of Bins. The pool starts from the base price, and every small segment, called a Bin step, represents a Bin. If a trade occurs within the same Bin, traders will enjoy zero slippage. This can greatly increase trading volume and success rate, and theoretically, LPs can earn more trading fees.
Similar to CLMM, tokens in the pool are also distributed on both sides of the current price. Only one type of token is needed to provide one-sided liquidity. However, according to the Bin concept, the currently activated Bin (indicating the current exchange price) contains both types of tokens. That is:
The currently activated Bin: It contains both types of tokens. Swapping tokens within this Bin will be done at a fixed price with zero slippage.
Other Bins: Distributed on both sides of the currently activated Bin, each containing only one type of token.
When the amount of tokens in the currently activated Bin changes, if one side of the token decreases to 0, DLMM will set the currently activated Bin to the next Bin on its left or right side based on the actual situation in the pool. This is how the price in the pool changes.
When LPs provide liquidity, DLMM offers three strategies: Spot, Curve, and Bid Ask.
Spot is the most universal and suitable for almost all liquidity pools. It is the simplest liquidity strategy.
Curve is more suitable for pools with very small price changes, such as stablecoin pairs. The price fluctuations in these pools are very small. As its shape suggests, concentrating LP funds within this range can maximize trading fees.
Bid Ask is more suitable for pools with very large price fluctuations. People tend to conduct more arbitrage trades in such pools, and the price is unlikely to concentrate within a small range. This strategy usually requires LPs to frequently adjust their positions to avoid the price deviating from the set range. Since it involves market judgment, this is not easy.
Summary AMM, as an important part of Web3 finance, promotes the popularization and development of decentralized finance through its unique mechanisms and innovations. With continuous technological progress and the improvement of the ecosystem, AMM is expected to play a greater role in the future and further change the traditional financial landscape.
In the current state of the Web3 industry, products related to DeFi dominate the market. Among these, AMM (Automated Market Maker) plays a crucial role and is a powerful driver of change in Web3 finance. This article will introduce several important AMM implementations in the Solana ecosystem, hoping to provide some guidance for LPs (Liquidity Providers) on how to choose their investment strategies.
CPMM CPMM (Constant Product Market Maker) is the most basic AMM implementation and is realized in many products. Here, we use Raydium's constant product-based AMM as an example. The constant product means that the supplies of the two tokens in the pool have a fixed product: X * Y = k.
For liquidity providers, when anyone adds liquidity (assets) to the pool, CPMM automatically creates an associated account for the wallet address and issues LP Tokens (each token pair has its own LP Token Mint). These LP Tokens are used to prove that the wallet address holds a share of a particular pool and will be destroyed when the liquidity provider withdraws funds.
The on-chain program of CPMM is developed using Anchor, and the program code can be found at raydium-io/raydium-cp-swap. Let's briefly examine how it implements the constant product.
Firstly, when users use Raydium's CPMM to swap tokens, the swap-related instruction will be triggered.
For example (only for illustrative purposes, this article is not responsible for any Token): when a user wants to swap USDC for TRUMP, they can do so through the TRUMP-USDC pool.
Pool
TRUMP
USDC
7XzVsjqTebULfkUofTDH5gDdZDmxacPmPuTfHa1n9kuh
6p6xgHyF7AeE6TZkSmFsko444wqoP15icUSqi2jfGiPN
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
For simplicity, let's ignore other instructions in the transaction and focus only on the Raydium part. We find Raydium CPMM: swapBaseInput.
In the Input Accounts, we can see that the input token is USDC and the output token is TRUMP. In the Solana ecosystem's AMM, LP pairs can be simply represented by Token Accounts, without the need to create and deploy a new contract program (such as the factory contract commonly used in Ethereum). When a transaction occurs, it directly interacts with Raydium's CPMM Program. The Solana program will modify the state of the corresponding Token Accounts through the pool address, token address, and other inputs to perform the swap operation.
For example, the code for the above swapBaseInput instruction can be found here. After a series of preliminary checks, the amount of the target token that can be obtained is calculated in ConstantProductCurve.swap_base_input_without_fees:
The formula used is:
That is, after the total amounts of TokenX and TokenY change, their product should remain constant. The left side of the equation is the product after the change, and the right side is the product before the change.
After mathematical transformation, we can obtain the conversion formula for Δy (the amount of y tokens we can obtain):
Which is the delta_y = (delta_x * y) / (x + delta_x) part in the code. Note that this calculation does not include fees, which have already been deducted in the preliminary logic of swap_base_input.
CLMM CLMM (Concentrated Liquidity Market Maker) is another AMM launched by Raydium. It is similar to Uniswap V3, where each token pair also has multiple fee tiers, and a corresponding pool can be created for each tier.
Since CLMM's implementation references Uniswap V3, many concepts and implementation methods can be referred to from Uniswap's implementation when learning it. It also inherits concepts from Uniswap, such as ticks, multiple fee tiers, and concentrated liquidity. More details can be found in the DEX development course launched by ZAN: DEX Development Practice - Uniswap Code Analysis - How Uniswap works - ZAN.
However, it is important to note that, similar to CPMM, due to the characteristics of the Solana blockchain, Raydium CLMM does not require a separate contract to be deployed for each pool, so there is no concept of a factory contract. This is different from Uniswap.
CLMM allows liquidity providers to select a price range when injecting funds, and the funds will only be distributed within the selected range:
For concentrated liquidity pools, tokens are distributed on both sides of the current price. The selected price range in the above figure includes the current price, so funds are allocated to both tokens in the pool.
We can also inject only one type of token to provide so-called one-sided liquidity (as shown in the figure below). This is somewhat similar to limit orders in traditional finance, where LP funds are only utilized when the token price reaches a certain range. However, this mode also involves more risk points to consider.
Generally, for pools with small price fluctuations, LPs tend to choose a smaller range; conversely, for pools with very volatile prices, they tend to choose a larger range. The goal is to prevent the current price from deviating too much from the selected range and causing too much impermanent loss.
It should be noted that while concentrated liquidity can improve the capital efficiency of LPs, it also demands a higher level of financial awareness from LPs. LPs need to actively manage their liquidity. If LPs are not responsive, frequent on-chain fluctuations can easily lead to significant impermanent losses for LPs.
DLMM DLMM (Dynamic Liquidity Market Maker) is an AMM product launched by Meteora. It also belongs to Uniswap V3 and is very similar to the CLMM mentioned above. DLMM also allows LPs to concentrate their funds within a certain range near the current price. However, there are some differences in the specific implementation of DLMM, and it provides some unique features.
DLMM introduces the concept of Bins. The pool starts from the base price, and every small segment, called a Bin step, represents a Bin. If a trade occurs within the same Bin, traders will enjoy zero slippage. This can greatly increase trading volume and success rate, and theoretically, LPs can earn more trading fees.
Similar to CLMM, tokens in the pool are also distributed on both sides of the current price. Only one type of token is needed to provide one-sided liquidity. However, according to the Bin concept, the currently activated Bin (indicating the current exchange price) contains both types of tokens. That is:
The currently activated Bin: It contains both types of tokens. Swapping tokens within this Bin will be done at a fixed price with zero slippage.
Other Bins: Distributed on both sides of the currently activated Bin, each containing only one type of token.
When the amount of tokens in the currently activated Bin changes, if one side of the token decreases to 0, DLMM will set the currently activated Bin to the next Bin on its left or right side based on the actual situation in the pool. This is how the price in the pool changes.
When LPs provide liquidity, DLMM offers three strategies: Spot, Curve, and Bid Ask.
Spot is the most universal and suitable for almost all liquidity pools. It is the simplest liquidity strategy.
Curve is more suitable for pools with very small price changes, such as stablecoin pairs. The price fluctuations in these pools are very small. As its shape suggests, concentrating LP funds within this range can maximize trading fees.
Bid Ask is more suitable for pools with very large price fluctuations. People tend to conduct more arbitrage trades in such pools, and the price is unlikely to concentrate within a small range. This strategy usually requires LPs to frequently adjust their positions to avoid the price deviating from the set range. Since it involves market judgment, this is not easy.
Summary AMM, as an important part of Web3 finance, promotes the popularization and development of decentralized finance through its unique mechanisms and innovations. With continuous technological progress and the improvement of the ecosystem, AMM is expected to play a greater role in the future and further change the traditional financial landscape.
Richard.M.Lu
Richard.M.Lu
No comments yet