Skip to main content

The Math Behind PMM

Core PMM#

The core of PMM is essentially calculating one integral and solving two quadratic equations. The smart contract implementation can be found here.

The Price Curve Integral#

For traders, the most important thing is the average transaction price. The average transaction price is the integral of the marginal price PmarginP_{margin}. Let's take the base token shortage scenario as an example.

Ξ”Q=∫B1B2PmargindB\Delta Q =\int^{B_2}_{B_1}P_{margin}dB

=∫B1B2(1βˆ’k)i+i(B0/B)2kdB= \int^{B_2}_{B_1}(1-k)i+i(B_0/B)^2kdB

=i(B2βˆ’B1)βˆ—(1βˆ’k+kB02B1B2)= i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})

This tells the trader how much they should pay if they buy B2βˆ’B1B_2-B_1 base tokens.

Rearranging the equation above, the average transaction price is thus: P=Ξ”QB2βˆ’B1=iβˆ—(1βˆ’k+kB02B1B2)P=\frac{\Delta Q}{B_2-B_1}=i*(1-k+k\frac{B_0^2}{B_1B_2})

We found that the average transaction price is only dependent on the state of the system before and after the transaction, so the price calculation methods for both buying and selling are the same: integrating PmarginP_{margin}.

Solving the quadratic equation for trading#

Without the loss of generality, the integral becomes the following when there is a shortage of quote tokens:

Ξ”B=1i(Q2βˆ’Q1)βˆ—(1βˆ’k+kQ02Q1Q2)\Delta B = \frac{1}{i}(Q_2-Q_1)*(1-k+k\frac{Q_0^2}{Q_1Q_2})

Let's derive how to calculate the price when there is a shortage of quote tokens and only the number of base tokens you want to buy or sell (i.e. Ξ”B\Delta B) is given.

Now that Ξ”B,Q0,Q1\Delta B, Q_0, Q_1 are given, we need to calculate Q2Q_2, which is found by solving a quadratic equation. Transforming the equation into standard form:

(1βˆ’k)Q22+(kQ02Q1βˆ’Q1+kQ1βˆ’iΞ”B)Q2βˆ’kQ02=0(1-k)Q_2^2+(\frac{kQ_0^2}{Q_1}-Q_1+kQ_1-i\Delta B)Q_2-kQ_0^2=0

letΒ a=1βˆ’k,Β b=kQ02Q1βˆ’Q1+kQ1βˆ’iΞ”B,Β c=βˆ’kQ02let \ a=1-k, \ b=\frac{kQ_0^2}{Q_1}-Q_1+kQ_1-i\Delta B, \ c=-kQ_0^2

Because Q2>=0Q_2>=0, we discard the negative root, and so


It can be proven that:

  • When Ξ”B>0\Delta B>0, Q2>Q1Q_2>Q_1; trader buy base tokens, and should pay Q2βˆ’Q1Q_2-Q_1
  • When Ξ”B<0\Delta B<0, Q2<Q1Q_2<Q_1; trader sell base tokens, and will receive Q1βˆ’Q2Q_1-Q_2
  • When Ξ”B=0\Delta B=0, Q2=Q1Q_2=Q_1.

DODO V2 focuses on verifying the special case of k=0, and k=1 to support a constant selling price and the bonding curve of the standard AMM.

Solving the quadratic equation for regression targets#

When the system is not in the equilibrium state, changes to the oracle price can bring profit or loss. For example, assume that shortage of base tokens is the current state, and then the oracle price goes up. It is clear that the excess quote tokens cannot buy enough base tokens to return the base token balance to the base token regression target. Thus, LPs who deposited base tokens will suffer a loss. Conversely, if the oracle price drops, the excess quote tokens can buy more base tokens, causing the base token balance to exceed the base token regression target, and LPs who deposited base tokens will make a profit.

In summary, the regression target is influenced by the oracle price. To calculate the regression target at a certain oracle price, we make the following derivation:

Given Ξ”Q=i(B2βˆ’B1)βˆ—(1βˆ’k+kB02B1B2)\Delta Q = i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})

Since we are doing regression, B2=B0B_2=B_0. Rearraging the equation with respect to B0B_0 gives

kB1B02+(1βˆ’2k)B0βˆ’[(1βˆ’k)B1+Ξ”Qi]=0\frac{k}{B_1}B_0^2+(1-2k)B_0-[(1-k)B_1+\frac{\Delta Q}{i}] = 0

The negative root does not make sense and is discarded, so B0B_0 is:

B0=B1+B11+4kΞ”QB1iβˆ’12kB_0=B_1+B_1\frac{\sqrt{1+\frac{4k\Delta Q}{B_1 i}}-1}{2k}

In this case, Ξ”Q=Qβˆ’Q0\Delta Q=Q-Q_0. It can be proven that, when Ξ”Qβ‰₯0\Delta Q \ge 0, B0β‰₯B1B_0\ge B_1.

This fact is extremely important, because it ensures that the base token balance and the quote token balance will never be greater than the regression target simultaneously, or less than the regression target simultaneously. This means that PMM will only switch between the three states discussed in the Core Concepts section.

Similarly, the formula for quote token regression target Q0Q_0 is

Q0=Q1+Q1βˆ—1+4kΞ”BiQ1βˆ’12kQ_0=Q_1+Q_1*\frac{\sqrt{1+\frac{4k\Delta B i}{Q_1}}-1}{2k}


This section will deal with the math pertaining to the peripheral functioning of PMM.


As mentioned above, the regression target depends on the oracle price, and the price curve in turn depends on the regression target. We should therefore calculate the regression target for each trade well in advance to fix the price curve.

In addition, since the price curve given by PMM is segmented, if a transaction involves different states (for example, when a trader sells an astronomical amount of base tokens during a base token shortage and forces the state into a quote token shortage), the price needs to be calculated in segments as well.

Please be advised that this calculation requires a high degree of accuracy. The smart contract provides six trading functions for the three possible states. You can find the most important logic of cross-state trading here.


Depositing and withdrawing base tokens when there is a shortage of base tokens, or quote tokens when there is a shortage of quote tokens, will change the price curve. This requires us to process the deposit and withdrawal with caution and care in order to keep the capital pool sustainable and fair.

Let's analyze what happens when an LP makes a deposit when there is a shortage of base tokens.

According to the calculation formula of B0B_0 derived above,

B0=B1+B1βˆ—1+4kΞ”QB1iβˆ’12kB_0=B_1+B_1*\frac{\sqrt{1+\frac{4k\Delta Q}{B_1 i}}-1}{2k}

After an LP deposit bb base tokens, B1B_1 increases by bb, and B0B_0 increases more than than bb's magnitude. This means that this deposit helps make a profit for all LPs who provided base tokens. The reason is that the deposit makes the price curve smoother, and the same amount of Ξ”Q\Delta Q can now buy more base tokens.

In this case, as soon as the LP makes a deposit, the LP makes a profit. This is referred to as the deposit reward. The essential source of this reward is the slippage paid by the trader who made the system deviate from the equilibrium state.


It is important to note that deposit rewards are not risk-free arbitrage trading opportunities.


Similarly, after an LP withdraws bb base tokens, B1B_1 decreases by bb, and B0B_0 decreases by more than bb's magnitude. This withdrawal causes all LPs who owe base tokens to suffer losses. This is because this withdrawal makes the price curve more steep, and the excess quote tokens have less purchasing power in terms of base tokens.

The PMM algorithm stipulates that a withdrawal fee is required to withdraw tokens in this case. The magnitude of the fee is equal to the aggregate loss of all LPs caused by the withdrawal. This fee will be directly distributed to all LPs that have not yet withdrawn.

Factoring in the deposit reward from the previous section, if an LP makes a withdrawal immediately after depositing, the withdrawal fee will be greater than the deposit reward, thus eliminating any possibility of risk-free arbitrage trading.

It is worth noting that both the deposit reward and withdrawal fee are only significant when the system deviates very far from the equilibrium state and the deposit/withdrawal amount is large. Traders thus often overlook the existence of this gain or loss. Of course, traders are also welcome to extract value from the system by exploiting this if they so wish. In order to do that, they can first deposit to earn deposit rewards when the system deviates from the equilibrium, and then withdraw once the system returns to the equilibrium to avoid the withdrawal fee.