Solana ecosystem AMM research

This article will introduce several important AMM implementations in the Solana ecosystem.

Written by: ZAN

In the current state of the Web3 industry, products related to DeFi occupy an absolute market share. Among them, AMM (Automated Market Maker) plays a critical role and is a strong driver of changes in Web3 finance. This article will introduce several important AMM implementations in the Solana ecosystem, hoping to provide some assistance to LP (Liquidity Provider) on how to choose their investment strategies.

CPMM

CPMM (Constant Product Market Maker) is the most basic implementation of AMM, and it has been implemented in many products. Here, we take the constant product AMM launched by Raydium as an example. The constant product means that the supply of the two tokens in the pool has a fixed product: X * Y = k.

For liquidity providers, when anyone adds liquidity (adds assets) to the pool, the CPMM automatically creates an associated account for the wallet address and issues LP Tokens (each token pair has its own LP Token Mint). This LP Token is used to prove that the wallet address holds a share of a certain pool and will be destroyed when the liquidity provider withdraws the corresponding Tokens.

The on-chain program of CPMM is developed using Anchor, and the program code can be found at . Now let's briefly confirm how it achieves constant product.

First, when users use Raydium's CPMM to swap tokens, swap-related commands will be triggered.

For example (for illustrative purposes only, this article is not responsible for any Token): when a user wants to exchange USDC for TRUMP, they can do so through the TRUMP-USDC pool.

For example, let's look at this transaction. To keep it simple, let's ignore the other instructions in the transaction and only look at the Raydium part. 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 AMM of the Solana ecosystem, the LP Pair can be simply represented by the Token Account without the need to create and deploy a new contract program (such as the factory contracts commonly used in Ethereum). When a trade occurs, it directly interacts with Raydium's CPMM Program, and the Solana program will modify the state of the corresponding Token Account for the swap operation by passing in the pool address, token address, and other parameters.

For example, the swapBaseInput instruction mentioned above can be found in the code here. After a series of preliminary checks, the specific amount of target tokens that can be exchanged is calculated in ConstantProductCurve.swap_base_input_without_fees:

The formula used is:

After the total amounts of TokenX and TokenY change, their product should remain unchanged. The left side of the formula represents the product after the change, while the right side represents the product before the change.

The above equation can be transformed mathematically to obtain the conversion formula for Δy (i.e., the number of y tokens we can exchange for):

That is the part in the code which is delta_y = (delta_x * y) / (x + delta_x). Note that the calculation here does not include transaction fees, as they have already been deducted in the preceding logic of swap_base_input.

CLMM

CLMM ( Concentrated Liquidity Market Maker ) is also an AMM launched by Raydium. It is similar to Uniswap V3, with each token pair having multiple fee tiers, allowing for the creation of a corresponding pool for each tier.

Since the implementation of CLMM references Uniswap V3, many concepts and implementation methods can be referenced from Uniswap's implementation when studying it. It also inherits Uniswap's concepts such as tick, multiple fee tiers, and concentrated liquidity, etc. More content 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 chain, 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 centralized liquidity pools, tokens exist on both sides of the current price. The price range selected in the above image includes the current price, so funds will be allocated to both types of tokens in the pool.

We can also inject only a certain type of token to provide what is called unilateral liquidity (as shown in the figure below). This is somewhat similar to limit orders in traditional financial models, where LP's funds are only utilized when the token price reaches a certain range, but this model also has more risk points to consider.

Generally speaking, for pools with small price fluctuations, LPs tend to choose a smaller range; conversely, for pools with very large price fluctuations, they tend to choose a larger range. The purpose of this is to minimize the occurrence of impermanent loss by keeping the current price within the chosen price range.

It is important to note that while concentrated liquidity can increase the capital efficiency of LPs, it also raises higher requirements for LPs' financial awareness. LPs need to manage their liquidity more actively. If LPs fail to respond adequately, frequent on-chain fluctuations can easily lead to significant impermanent loss for LPs.

DLMM

DLMM ( Dynamic Liquidity Market Maker ) is an AMM product launched by Meteora. It is also a type of 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, DLMM has some differences in its specific implementation and offers some unique features.

The concept of Bin is provided in DLMM, where the pool starts from the base price and exists as a Bin for every small Bin step. If a trade occurs within the same Bin, traders will enjoy zero slippage. This can greatly increase trading volume and success rates, theoretically allowing LPs to earn more trading fees.

Similar to CLMM, the tokens in the pool are also distributed on both sides of the current price, and a single token only needs to provide unilateral liquidity. However, according to the concept of Bin, the currently activated Bin (which indicates the current exchange price) has two types of tokens. That is:

  • Current activated Bin: There are two types of tokens present, and exchanging tokens in the current Bin will be done at a fixed price with 0 slippage;
  • Other Bins: Distributed on both sides of the currently activated Bin, each containing only a single token.

When the amount of tokens in the currently activated Bin changes, if one party's tokens decrease to 0, DLMM will set the currently activated Bin to the next Bin on its left or right, based on the actual situation in the pool. This is done to drive price changes in the pool.

When providing liquidity in LP, DLMM offers 3 strategies: Spot, Curve, and Bid Ask.

Among them, Spot is the most universal, and basically all liquidity pools are relatively suitable, which can be regarded as the simplest liquidity strategy.

Curve is more suitable for pools where price changes are very small, such as stablecoin pairs. The price fluctuations in these pools are minimal, as indicated by its shape, concentrating LP's funds within this range to maximize transaction fees.

Bid Ask is more suitable for pools with very high price volatility. People tend to engage in more arbitrage trading in such pools, where prices can be difficult to concentrate within a small range. This strategy usually requires LPs to frequently adjust their positions to avoid prices drifting away from the capital range they have set. This typically involves a judgment of the market, which is not usually easy.

Summary

AMM, as an important component of the Web3 financial sector, promotes the popularization and development of decentralized finance through its unique mechanisms and innovations. With continuous technological advancements and the improvement of the ecosystem, AMM is expected to play a greater role in the future, further changing the landscape of traditional finance.

View Original
The content is for reference only, not a solicitation or offer. No investment, tax, or legal advice provided. See Disclaimer for more risks disclosure.
  • Reward
  • Comment
  • Share
Comment
0/400
No comments
  • Pin
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)