Download this document as PDF: Shared Liquidity AMM of Mai Protocol V3.pdf
AMM is the counterparty of all trades. Major questions to think about while designing an AMM include how to improve the capital efficiency and the expected profit of LP (Liquidity Provider).
To improve the capital efficiency, AMM should decrease the slippage to attract more traders. Our resolution is the assembling of more funds near the index price. Furthermore, MCDEX involves the concept of "shared liquidity pool" - multiple AMMs with the same collateral can use one liquidity pool.
The profit of LP has to be maximized in order to attract more LP to provide liquidity. There are two ways to increase LP profit: One is to increase trader’s trading fee, and the other one is to reduce arbitrage opportunities. The MCDEX AMM V3 is more inclined towards the second one. MCDEX AMM V3 also applies other means to increase LP profit, including but not limited to the adjustments of spread and slippage, and the funding payment which is unique in perpetual swaps.
Index Price from Oracle.
AMM’s base price without including additional charges.
AMM Net Position. means that AMM is on long position, means that AMM is on short position.
The perpetual swap margin of AMM deposited by all LPs.
Margin balance of the AMM’s account. .
Position value. .
The evaluation of the value of shared liquidity pool. Check formula .
Funding rate. When , long position pays funding fee to short position. When , short position pays funding fee to long position.
Total amount of pool share.
The market set in a shared liquidity pool.
The current trading market in the shared liquidity pool. .
Position holding time.
Risk parameter: half spread. .
Risk parameter: slippage. . A bigger increases slippage when open position.
Risk parameter: slippage. . A bigger increases slippage when close position.
Risk parameter: funding rate factor. . A bigger increases the funding rate.
Risk parameter: funding rate limit. . A bigger increases the maximum funding rate.
Risk parameter: max discount of price when close position.
Risk parameter: trading fee. In practice, the trading fee consists of Vault Fee, Operator Fee and LP Fee.
Risk parameter: target leverage of AMM’s margin account.
MCDEX AMM V3 uses a price function to provide more liquidity near the index price. It uses a risk control function to adjust the spread and slippage. Meanwhile, AMM applies a virtual margin to further improve the capital efficiency. Finally, AMM provides the funding rate to adjust the premium/discount.
Price function. The AMM V3’s price is based on the index price and its risk exposure, AMM completes base average fill price:
If a trader trades contracts, AMM trades contracts. is the average fill price. is the index price provided by Oracle. is the slippage parameter satisfying , in which a bigger indicates a bigger slippage. is AMM’s position. is the pool margin. This price function helps AMM follows the spot market price without arbitrageur. The slippage automatic decreases when LPs deposit more collateral. The derivation of formula can be found in Appendix 1.
MCDEX enables multiple AMMs to share the same liquidity pool. is the set of markets in a shared liquidity pool. Each AMM with index has its own position and shares the same margin .
Risk control function. We believe that LP takes lots of risk when holding positions. Therefore, we add several risk parameters to control the slippage, spread, trading fees and max positions.
AMM applies different slippage parameter when the risk exposure is increasing or decreasing. When trader buys/longs against AMM, AMM is the short side (). If AMM holds short (), the risk exposure is increasing. If AMM holds long (), the risk exposure is decreasing. The price will be:
is max discount when AMM closes positions.
When trader sell/short against AMM, AMM is the long side (). the price will be:
Note that formula and contains 2 segments respectively. A trading may cross 2 segments. For example, . The trading can be considered as 2 parts and denote prices as . The average price over segments is:
AMM also controls a price spread. The spread is the difference between best ask and best bid price. We define the best ask/bid price as:
is the half-spread parameter. is the mid-price of AMM which is determined by the current AMM position . The final trading price is:
The figure below shows the curves of the prices.
Figure 1: Fill price for Trader. The slippage increases as AMM risk increases. Vice versa, the slippage decreases as AMM risk decreases.
After adjusting the spread and slippage, the trader pays trading fee to the AMM with the fee rate . Let be the trading positions, the transaction of the fee can be described as:
The maximum position of AMM is defined by calculating the AMM’s position margin:
sets AMM’s max leverage of each market. AMM can open a position only if margin balance M_b is larger than the AMM’s position margin. Otherwise, AMM will cease providing liquidity and opening positions.
Funding rate. AMM provides the funding rate to perpetual swap. Funding payment is a significant component when perpetual swap anchors the spot price. Funding rate is the interest rate of the funding payment. If funding rate is positive, the long side in the perpetual swap pays interest to the short side. Vice versa, if funding rate is negative, the short side pays interest to the long side.
Because AMM is the counterparty of all traders, if most traders go long, then AMM goes short (), and funding rate is positive. Otherwise, if most traders go short, then AMM goes long (), and funding rate is negative. If there is a balance in the market, then AMM has no position, and funding rate is zero. We define funding rate as:
The funding rate is hard limited by . is the coefficient of funding rate. Funding rate is the ratio of position value that every position needs to collect per 8 hours. Hence if a trader holds amount of long position over amount of time, this trader needs to make a funding payment of:
Among which .
Bear in mind that the funding rate provided by AMM in formula is always into the favorable direction for AMM. Specifically, AMM collects funding payments when it longs or shorts. AMM does not collect funding payment only when there is a tie (AMM has no position).
In formula we defined the base price of AMM V3. The formula has 3 major features: 1) The introduction of index price , so that AMM’s price automatically follows the market price; 2) The assembling of liquidity near the index price, decreasing the slippage; 3) The solely dependent on margin balance to avoid the situation in which LP provides two types of assets.
The introduction of index price. This function introduces index price to direct AMM pricing. For instance, when the spot price increases drastically, AMM would not have to rely on arbitrageurs to give price but make use of the updated market price instead. In such way arbitrage opportunities can be reduced to avoid unnecessary loss in LP investment.
Slippage increases along with the increment of risk exposure. When the net position held by AMM increases, the AMM risk exposure will increase as well. Thus, the AMM pricing will gradually moves away from index price , and the slippage depends on the current utilization rate . When the utilization rate is low, the price will be close to . Otherwise, the slippage will increase.
Slippage is decreased near the index price. AMM V3 introduces the slippage parameter to drastically decrease the slippage. In the constant product AMM function, the slippage is relatively high in order to make sure that AMM covers 0 to ∞ price range. The V3 AMM function flattens the price curve near index price. While it improves user experience, capital efficiency is also maximized so that the fund provided by LP could be concentrated to fulfill the trading demand near the index price.
Figure 2: Comparison with the constant product formula. The blue line is the AMM V3 price which decreases the slippage.
LP only provides collateral into margin account. In perpetual swap, trader and LP only need to provide collateral into margin account but not the underlying asset. This differentiates perpetual swap from spot trade.
In an ETH-USDC perpetual swap, the collateral is USDC, and the underlying asset is ETH. When trader longs or shorts, the fluctuation of PNL is reflected by the collateral USDC in the margin account instead of ETH.
An essential characteristic of AMM V3 is that LP only needs to provide the collateral token to add liquidity. This characteristic simplifies the operation of LP and calculation of PNL. With constant product AMM, LP needs to provide two types of tokens, hence the PNL of LP depends on the value of these two assets, making the PNL calculation of LP unclear.
Limited Market Making Depth. The market making depth of AMM V3 is limited by the max leverage. When AMM’s position reaches the upper limit, AMM will only provide one side of liquidity. For example, if the long position of AMM reaches the limit, AMM will refuse the trader to sell anymore.
LP profit mainly comes from spread, funding payment, and transaction fee.
Spread. There is a spread between the AMM’s best bid and best ask. When traders and arbitrageurs trade against AMM, this spread will cause AMM to profit. As N increases, AMM’s risk increases. A higher risk is designed to result in a larger spread, which then lead to a higher profit for LP.
Funding payment. Funding payment is the interest that trader pays to LP when trader goes long or short. When the long side and the short side happen to be identical, it can be interpreted as the two sides lending money from each other and the interests cancel out. When the amount of long side and short side are different, AMM is the only asset lender. Therefore, AMM deserves the interest from net position.
Trading fee. Most AMMs charge fee at a fixed ratio when trader trades with AMM. MCDEX AMM V3 is inclined towards decreasing the trading fee and arranging the distribution of profit through the methods discussed earlier.
Other income. Technically LP receives liquidation penalty as well. But since it is a function of the perpetual swap, we will not discussion it in this passage.
The major risk of LP comes from the position it holds. If the index price moves toward an unfavorable direction for AMM post trade, AMM will suffer a lost. Certain traders could take advantage of the publicity of AMM. MCDEX AMM V3 introduces the concept of an "operator". An operator is the creator of the perpetual swap as well as the one who manages risk parameters so that the risk on LP can be limited. In return, Operator receives part of LP’s profit.
This section is a complete step-by-step trading procedure of AMM. If a trader trades -ΔN amount of perpetual swaps, AMM, the counterparty thus trades ΔN amount of perpetual swaps. Then:
Read price from Oracle. Realize the funding payment using formula .
The margin could change in circumstances like AMM has a PNL caused by the price change or AMM receives funding payment or trading fee. Use formula to calculate this PNL and update the .
Based on AMM position and trader’s operating direction, there are 4 possibilities. Use formula or to choose slippage parameter .
Use to calculate the average fill price.
Make sure that the new position does not exceed the upper limit defined by formula .
Pay the fee. Use formula .
There are some peripheral issues with the pricing strategy.
LP deposits collateral token to the liquidity pool to earn trading fees and market making profits. AMM’s margin increases and slippage decreases when LP deposits.
In order to track the proportion of LP in the pool, AMM mints share tokens when LP deposits into AMM. When LP withdraw from AMM, AMM burns share tokens and transfer collateral tokens back to the LP.
AMM keeps the increasing ratio of as same as share tokens. Given the depositing collateral and the total shares , AMM mints shares. The AMM’s margin satisfies:
Withdrawing from liquidity pool increases the slippage and causes LP to suffer losses. AMM reduces the losses by charging withdrawal penalty fees.
When LP withdraw from AMM, AMM first calculates the current margin and calculate the new margin . Then we can calculate the withdrawal collateral w:
If AMM’s position is 0, all collateral of AMM can be withdrawn. But if the position is not 0, the will be smaller. We consider this reduced collateral as withdrawal penalty. The derivation can be found in Appendix 4.
The AMM V3’s price is based on the index price and its risk exposure, AMM completes base price:
During the transaction, the more important thing is the average fill price when trading positions. Let the AMM position before and after trade be and . The average fill price can be considered as the used position value divided by the trading positions:
When a trader trades contracts, AMM trades contracts. So . We can now express the average fill price as:
Assume no trading fees are taken, when from Oracle remains unchanged, the trade does not affect the margin , so the curve of the pricing function stays the same. But will be affected in the following situations: AMM has the PNL when index price changes; AMM receives funding payment; LP add or remove liquidity. Therefore, AMM needs to keep updated.
For example, let’s say that AMM holds long (), if the index price decreases, there will be a loss on the long side. This loss will drag down and the slippage will increase. If trader goes long to make AMM suffer a margin closeout, AMM will have to take another loss due to the change of slippage. These two losses will result in the loss of LP.
The mechanism of updating is to simulate trades that make AMM close its position. The resulted will be the new . Assume the current position is , and becomes after a simulated closing. Use to calculate the change of for each market. We get:
Solve equation we get:
Due to the fact that margin balance is positive, can also be detected by checking before every trading:
When , there are 2 solutions, among which formula has an unfavorable characteristic: if decreases when increases, which is inappropriate for long position. So, formula is disregarded and formula is favored. AMM will avoid this situation by limiting the maximum position, we will discuss this method in Appendix 3.
When , the huge loss of AMM makes it incapable of going back to the zero position status using . The solution is to specify a by the algorithm just like adding a slippage to the current trading market . Meanwhile, the AMM would stop adding more position to prevent further loss. The purpose changing is to allow AMM to provide single sided liquidity in dangerous situation. To get a safe , let and we can get:
The has a minimum value . For the sake of simplicity, we set . Substitute the into the price formula , we get .
AMM has a limited market making depth to keep the position under the max leverage when the position increases. The maximum position is subject to:
Condition 1: keep a positive price in .
Condition 2: The margin balance is limited by leverages according to .
Condition 3: Prevent Δ<0 in formula .
The first condition is already solved the first edge point:
To find the other edge points, we can simulate an opening position process assuming the AMM has no position until one of the above conditions is break. The position at this moment will be the maximum positions it can holds.
Let to simulate the situation when AMM’s position is 0. We assume only the current trading market is increasing the position until while the other markets keep their positions unchanged. The final cash will be changed to:
Solving condition 2: The effect of max leverage can be expressed as:
Solve this equation we get the maximum long position is:
The maximum short position is:
Solving condition 3: Let in formula while substitute as , we get the maximum long position is:
The maximum short position is:
If , the other markets already reach the maximum positions. In this case, the current trading market cannot open position anymore.
In summary: The maximum long position and maximum short position are:
When AMM’s position does not equal to 0, there is a risk-free means for LP to arbitrage: The LP can go long, withdraw, go short, he will profit without bearing any risk. To prevent this situation, we can fix before and after withdrawal (except for the withdrawal part).
Given the amount of shares that LP wishes to burn is s and the collateral he will withdraw is , the AMM’s position is , the total shares is and AMM’s margin is . AMM first calculates the current margin and calculate the new margin . We can get the withdrawal collateral by solving :
The withdrawal request may fail if LP removes too much liquidity and violates formula . We can get the minimum in formula by substitute in it to formula . So that must larger than the minimum to satisfies formula .