V3 AMM Design

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.

Definition

Variable

Definition

PiP_i (Index Price)

Index Price from Oracle.

PbP_b (Base Price)

AMM’s base price without including additional charges.

NN (Net position)

AMM Net Position. N>0N>0 means that AMM is on long position, N<0N<0 means that AMM is on short position.

McM_c (Cash)

The perpetual swap margin of AMM deposited by all LPs.

MbM_b (Margin Balance)

Margin balance of the AMM’s account. Mb=Mc+PiNM_b=M_c+P_i N.

MpM_p (Position value)

Position value. Mp=PiNM_p=|P_i N|.

MM (pool Margin)

The evaluation of the value of shared liquidity pool. Check formula .

RfR_f (Funding Rate)

Funding rate. When Rf>0R_f>0, long position pays funding fee to short position. When Rf<0R_f<0, short position pays funding fee to long position.

SS (Share)

Total amount of pool share.

(Market set)

The market set in a shared liquidity pool.

mm (current Market)

The current trading market in the shared liquidity pool. mMm∈M.

TholdT_\text{hold} (Holding Time)

Position holding time.

T8hT_\text{8h} (8 hours)

8 hours.

αα (Spread)

Risk parameter: half spread. 0α<10≤α<1 .

β1β_1 (Open Slippage)

Risk parameter: slippage. 0<β2β10<β_2≤β_1. A bigger β1β_1 increases slippage when open position.

β2β_2 (Close Slippage)

Risk parameter: slippage. 0<β2β10<β_2≤β_1. A bigger β2β_2 increases slippage when close position.

γγ (Funding)

Risk parameter: funding rate factor. γ0γ≥0. A bigger γγ increases the funding rate.

ΓΓ (maximum Funding)

Risk parameter: funding rate limit. Γ0Γ≥0. A bigger ΓΓ increases the maximum funding rate.

δδ (max Discount)

Risk parameter: max discount of price when close position.

ϕϕ (Fee)

Risk parameter: trading fee. In practice, the trading fee consists of Vault Fee, Operator Fee and LP Fee.

λλ (max Leverage)

Risk parameter: target leverage of AMM’s margin account.

MCDEX AMM V3 Pricing Strategy

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 PiP_i and its risk exposure, AMM completes base average fill price:

If a trader trades ΔN-ΔN contracts, AMM trades ΔNΔN contracts. Pb\overline{P_b} is the average fill price. PiP_i is the index price provided by Oracle. ββ is the slippage parameter satisfying β>0β>0, in which a bigger ββ indicates a bigger slippage. N1N_1 is AMM’s position. MM 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 jj has its own position N1j{N_1}_j and shares the same margin MM.

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 (ΔN<0ΔN<0). If AMM holds short (N<0N<0), the risk exposure is increasing. If AMM holds long (N>0N>0), 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 (ΔN>0ΔN>0). the price will be:

Note that formula and contains 2 segments respectively. A trading may cross 2 segments. For example, N1=10,ΔN=15N_1=10,ΔN=-15. The trading can be considered as 2 parts ΔNseg1=10,ΔNseg2=5ΔN_{seg1}=-10,ΔN_{seg2}=-5 and denote prices as Pβseg1,Pβseg2\overline{{P_\beta}_{seg1}}, \overline{{P_\beta}_{seg2}}. 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. PmidP_\text{mid} is the mid-price of AMM which is determined by the current AMM position N1N_1. The final trading price is:

The figure below shows the curves of the prices.

price curves

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 ΔNΔN 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:

λjλ_j 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 (N<0N<0), and funding rate is positive. Otherwise, if most traders go short, then AMM goes long (N>0N>0), 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 RfR_f as:

The funding rate RfR_f is hard limited by ±Γ±Γ. γγ is the coefficient of funding rate. Funding rate RfR_f is the ratio of position value that every position needs to collect per 8 hours. Hence if a trader holds N1N_1 amount of long position over ΔTΔT amount of time, this trader needs to make a funding payment of:

Among which T8h=8 hoursT_\text{8h}=\text{8 hours}.

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).

Features of the Pricing Strategy

In formula we defined the base price of AMM V3. The formula has 3 major features: 1) The introduction of index price PiP_i, 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 PiP_i 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 NN held by AMM increases, the AMM risk exposure will increase as well. Thus, the AMM pricing will gradually moves away from index price PiP_i, and the slippage depends on the current utilization rate PiNM\frac{P_i N} {M}. When the utilization rate is low, the price will be close to PiP_i. 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.

price compare

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 and Risk Exposure

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 PiP_i 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.

Trading Procedure of AMM

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:

  1. Read price PiP_i from Oracle. Realize the funding payment using formula .

  2. The margin MM 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 MM.

  3. Based on AMM position and trader’s operating direction, there are 4 possibilities. Use formula or to choose slippage parameter ββ.

  4. Use to calculate the average fill price.

  5. Make sure that the new position does not exceed the upper limit defined by formula .

  6. Pay the fee. Use formula .

Peripheral

There are some peripheral issues with the pricing strategy.

Deposit

LP deposits collateral token to the liquidity pool to earn trading fees and market making profits. AMM’s margin MM 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 MM as same as share tokens. Given the depositing collateral ww and the total shares SS, AMM mints ss shares. The AMM’s margin MM satisfies:

Withdrawal Penalty

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 M1=M(Mc,N)M_1=M(M_c,N) and calculate the new margin M2=M1SsSM_2=M_1 \frac {S-s} {S}. 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 ww will be smaller. We consider this reduced collateral as withdrawal penalty. The derivation can be found in Appendix 4.

Appendix 1 The Average Fill Price

The AMM V3’s price is based on the index price PiP_i and its risk exposure, AMM completes base price:

During the transaction, the more important thing is the average fill price when trading ΔNΔN positions. Let the AMM position before and after trade be N1N_1 and N2N_2. The average fill price can be considered as the used position value divided by the trading positions:

When a trader trades ΔN-ΔN contracts, AMM trades ΔNΔN contracts. So N2=N1+ΔNN_2=N_1+ΔN. We can now express the average fill price as:

Appendix 2 Update M to Realize PNL Caused by Index Change

Assume no trading fees are taken, when PiP_i from Oracle remains unchanged, the trade does not affect the margin MM, so the curve of the pricing function stays the same. But MM will be affected in the following situations: AMM has the PNL when index price PiP_i changes; AMM receives funding payment; LP add or remove liquidity. Therefore, AMM needs to keep MM updated.

For example, let’s say that AMM holds long (N>0N>0), if the index price PiP_i decreases, there will be a loss on the long side. This loss will drag down MM 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 MM is to simulate trades that make AMM close its position. The resulted McM_c will be the new MM. Assume the current position is N1N_1, and becomes N2=0N_2=0 after a simulated closing. Use to calculate the change of McM_c for each market. We get:

Solve equation we get:

Among them

Due to the fact that margin balance is positive, Δ0\Delta \geq 0 can also be detected by checking McM_c before every trading:

When Δ0\Delta \geq 0, there are 2 solutions, among which formula has an unfavorable characteristic: if N>0N>0 MM decreases when PiP_i 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 Δ<0\Delta < 0, the huge loss of AMM makes it incapable of going back to the zero position status using β\beta . The solution is to specify a β\beta by the algorithm just like adding a slippage to the current trading market mm. Meanwhile, the AMM would stop adding more position to prevent further loss. The purpose changing β\beta is to allow AMM to provide single sided liquidity in dangerous situation. To get a safe β\beta, let Δ=0\Delta = 0 and we can get:

The βsafe\beta_\text{safe} has a minimum value min{βsafe}=0min\{\beta_\text{safe}\}=0. For the sake of simplicity, we set β=0\beta=0. Substitute the β\beta into the price formula , we get P=Pi\overline{P}=P_i.

In summary:

Appendix 3 Maximum AMM Positions

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 N2N_2 at this moment will be the maximum positions it can holds.

Let N1=0,Mc1=MN_1=0, M_{c1}=M to simulate the situation when AMM’s position is 0. We assume only the current trading market mm is increasing the position until N2N_2 while the other markets j,jM,jmj,j∈M,j≠m keep their positions unchanged. The final cash Mc2M_{c2} 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 Δ=0Δ=0 in formula while substitute McM_c as Mc2M_{c2}, we get the maximum long position is:

The maximum short position is:

If , the other markets j,jM,jmj, j∈M,j≠m already reach the maximum positions. In this case, the current trading market cannot open position anymore.

In summary: The maximum long position NmaxN_\text{max} and maximum short position NminN_\text{min} are:

Appendix 4 Withdrawal Penalty

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 MM 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 ww, the AMM’s position is NN, the total shares is SS and AMM’s margin is MM. AMM first calculates the current margin M1=M(Mc,N)M_1=M(M_c,N) and calculate the new margin M2=M1SsSM_2=M_1 \frac{S-s} {S} . We can get the withdrawal collateral ww by solving M(Mcw,N)=M2M(M_c-w,N)=M_2:

The withdrawal request may fail if LP removes too much liquidity and violates formula . We can get the minimum MM in formula by substitute McM_c in it to formula . So that M2M_2 must larger than the minimum MM to satisfies formula .