MCDEX
Search…
⌃K

# 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
$P_i$
(Index Price)
Index Price from Oracle.
$P_b$
(Base Price)
AMM’s base price without including additional charges.
$N$
(Net position)
AMM Net Position.
$N>0$
means that AMM is on long position,
$N<0$
means that AMM is on short position.
$M_c$
(Cash)
The perpetual swap margin of AMM deposited by all LPs.
$M_b$
(Margin Balance)
Margin balance of the AMM’s account.
$M_b=M_c+P_i N$
.
$M_p$
(Position value)
Position value.
$M_p=｜P_i N｜$
.
$M$
(pool Margin)
The evaluation of the value of shared liquidity pool. Check formula
.
$R_f$
(Funding Rate)
Funding rate. When
$R_f>0$
, long position pays funding fee to short position. When
$R_f<0$
, short position pays funding fee to long position.
$S$
(Share)
Total amount of pool share.
$\cal{M}$
(Market set)
The market set in a shared liquidity pool.
$m$
(current Market)
The current trading market in the shared liquidity pool.
$m∈\cal{M}$
.
$T_\text{hold}$
(Holding Time)
Position holding time.
$T_\text{8h}$
(8 hours)
8 hours.
$α$
$0≤α<1$
.
$β_1$
(Open Slippage)
Risk parameter: slippage.
$0<β_2≤β_1$
. A bigger
$β_1$
increases slippage when open position.
$β_2$
(Close Slippage)
Risk parameter: slippage.
$0<β_2≤β_1$
. A bigger
$β_2$
increases slippage when close position.
$γ$
(Funding)
Risk parameter: funding rate factor.
$γ≥0$
. A bigger
$γ$
increases the funding rate.
$Γ$
(maximum Funding)
Risk parameter: funding rate limit.
$Γ≥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 shares margin between perpetuals 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
$P_i$
and its risk exposure, AMM completes base average fill price:
$-ΔN$
$ΔN$
contracts.
$\overline{P_b}$
is the average fill price.
$P_i$
is the index price provided by Oracle.
$β$
is the slippage parameter satisfying
$β>0$
, in which a bigger
$β$
indicates a bigger slippage.
$N_1$
is AMM’s position.
$M$
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 process of formula
and
are shown in Appendix 1 and Appendix 2 respectively.
MCDEX enables multiple AMMs to share the same liquidity pool.
$\cal{M}$
is the set of markets in a shared liquidity pool. Each AMM with index
$j$
has its own position
${N_1}_j$
and shares the same margin
$M$
.
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$
). If AMM holds short (
$N<0$
), the risk exposure is increasing. If AMM holds long (
$N>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$
). the price will be:
Note that formula
and
contains 2 segments respectively. A trading may cross 2 segments. For example,
$N_1=10,ΔN=-15$
. The trading can be considered as 2 parts
$ΔN_{seg1}=-10,ΔN_{seg2}=-5$
and denote prices as
$\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:
$α$
$P_\text{mid}$
is the mid-price of AMM which is determined by the current AMM position
$N_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.
$ϕ$
. Let
$Δ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$
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<0$
), and funding rate is positive. Otherwise, if most traders go short, then AMM goes long (
$N>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
$R_f$
as:
The funding rate
$R_f$
is hard limited by
$±Γ$
.
$γ$
is the coefficient of funding rate. Funding rate
$R_f$
is the ratio of position value that every position needs to collect per 8 hours. Hence if a trader holds
$N_1$
amount of long position over
$ΔT$
amount of time, this trader needs to make a funding payment of:
Among which
$T_\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
$P_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
$P_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
$N$
held by AMM increases, the AMM risk exposure will increase as well. Thus, the AMM pricing will gradually moves away from index price
$P_i$
, and the slippage depends on the current utilization rate
$\frac{P_i N} {M}$
. When the utilization rate is low, the price will be close to
$P_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
$P_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.

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. 1.
$P_i$
from Oracle. Realize the funding payment using formula
.
2. 2.
The margin
$M$
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
$M$
.
3. 3.
Based on AMM position and trader’s operating direction, there are 4 possibilities. Use formula
or
to choose slippage parameter
$β$
.
4. 4.
Use
to calculate the average fill price.
5. 5.
Make sure that the new position does not exceed the upper limit defined by formula
.
6. 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
$M$
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
$M$
as same as share tokens. Given the depositing collateral
$w$
and the total shares
$S$
, AMM mints
$s$
shares. The AMM’s margin
$M$
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
$M_1=M(M_c,N)$
and calculate the new margin
$M_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
$w$
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
$P_i$
and its risk exposure, AMM completes base price:
During the transaction, the more important thing is the average fill price when trading
$ΔN$
positions. Let the AMM position before and after trade be
$N_1$
and
$N_2$
. The average fill price can be considered as the used position value divided by the trading positions:
$-ΔN$
$ΔN$
contracts. So
$N_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
$P_i$
from Oracle remains unchanged, the trade does not affect the margin
$M$
, so the curve of the pricing function stays the same. But
$M$
will be affected in the following situations: AMM has the PNL when index price
$P_i$
changes; AMM receives funding payment; LP add or remove liquidity. Therefore, AMM needs to keep
$M$
updated.
For example, let’s say that AMM holds long (
$N>0$
), if the index price
$P_i$
decreases, there will be a loss on the long side. This loss will drag down
$M$
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
$M$
is to simulate trades that make AMM close its position. The resulted
$M_c$
will be the new
$M$
. Assume the current position is
$N_1$
, and becomes
$N_2=0$
after a simulated closing. Use
to calculate the change of
$M_c$
for each market. We get:
Solve equation
we get:
Among them
$\Delta=M_b^2-2(\sum_{j \in \cal{M}} \beta_j {P_i}_j^2 N_j^2)$
Due to the fact that margin balance is positive,
$\Delta \geq 0$
can also be detected by checking
$M_c$
When
$\Delta \geq 0$
, there are 2 solutions, among which formula
has an unfavorable characteristic: if
$N>0$
$M$
decreases when
$P_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
$\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
$m$
. 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
$\Delta = 0$
and we can get:
The
$\beta_\text{safe}$
has a minimum value
$min\{\beta_\text{safe}\}=0$
. For the sake of simplicity, we set
$\beta=0$
. Substitute the
$\beta$
into the price formula
, we get
$\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
$N_2$
at this moment will be the maximum positions it can holds.
Let
$N_1=0, M_{c1}=M$
to simulate the situation when AMM’s position is 0. We assume only the current trading market
$m$
is increasing the position until
$N_2$
while the other markets
$j,j∈M,j≠m$
keep their positions unchanged. The final cash
$M_{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$
in formula
while substitute
$M_c$
as
$M_{c2}$
, we get the maximum long position is:
The maximum short position is:
If
$2M^2-\sum_{j \in \cal{M}, j≠m} \beta_j {P_i}_j^2 N_j^2<0$
, the other markets
$j, 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
$N_\text{max}$
and maximum short position
$N_\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
$M$
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
$w$
, the AMM’s position is
$N$
, the total shares is
$S$
and AMM’s margin is
$M$
. AMM first calculates the current margin
$M_1=M(M_c,N)$
and calculate the new margin
$M_2=M_1 \frac{S-s} {S}$
. We can get the withdrawal collateral
$w$
by solving
$M(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
$M$
in formula
by substitute
$M_c$
in it to formula
. So that
$M_2$
must larger than the minimum
$M$
to satisfies formula
.