The Math Behind

PMM Price Curve Definition #

The price curve described by the PMM is mathematically defined as follows:

P=iRP=iR

If B<B0B<B_0, thenR=1โˆ’k+(B0B)2kR=1-k+(\frac{B_0}{B})^2k

If Q<Q0Q<Q_0, thenR=1/(1โˆ’k+(Q0Q)2k)RR=1/(1-k+(\frac{Q_0}{Q})^2k)R

Otherwise, R=1R=1.

where

  • ii is the initial "guide price"
  • kk is the "slippage factor"
  • BB denotes the current base token supply
  • B0{B_0} denotes the equilibrium base token supply
  • QQ denotes the current quote token supply
  • Q0{Q_0} denotes the equilibrium quote token supply

Price Curve Integral #

The most important thing for a trader is the average transaction price. The average transaction price is the integral of the marginal price PP. Let's examine the case where there is a shortage of base tokens.

This equation describes the case where the user has B1B_1 base tokens and wishes to purchase enough to have B2B_2 tokens. To do this, the user needs to calculate the average price they will pay per token purchased.

ฮ”Q=โˆซB1B2PdB\Delta Q =\int^{B_2}_{B_1}PdB

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

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

The above equation can be rearranged to obtain the average token price as follows:

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

Solving the Quadratic Equation for Trading#

Let's derive how to calculate the price when there is a shortage of base tokens and only the number of quote tokens you want to buy or sell with (i.e. ฮ”Qฮ”Q) is 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})

Now that ฮ”Q,B0โ€‹,B1ฮ”Q, B_0โ€‹, B_1 are given, we need to calculate B2โ€‹B_2โ€‹, which is found by solving a quadratic equation. Transforming the equation into standard form:

(1โˆ’k)B22+(kB02B1โˆ’B1+kB1โˆ’ฮ”Qi)B2โˆ’kB02(1-k)B_2^2+(\frac{kB_0^2}{B_1}-B_1+kB_1-\frac{\Delta Q}{i})B_2-kB_0^2

letย a=1โˆ’k,ย b=kB02B1โˆ’B1+kB1โˆ’ฮ”Qi,ย c=โˆ’kB02let \ a=1-k, \ b=\frac{kB_0^2}{B_1}-B_1+kB_1-\frac{\Delta Q}{i}, \ c=-kB_0^2

Since B2>=0B_2>=0, we discard the negative root, yielding

B2=โˆ’b+b2โˆ’4ac2aB_2=\frac{-b+\sqrt{b^2-4ac}}{2a}

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.

Variables#

BBandQQ represent the current token supply, i.e. the dependent variables. In order to determine this price curve, we also need to know the four parameters ii, kk, B0{B_0}, and Q0{Q_0}.

However, these four parameters are not independent of each other, and given any three of them, the fourth one can be calculated.

The process of calculating the fourth parameter is called "regression" and is done whenever the parameters change. DODO's PMM operates under a "long regression" rule, which is a rule that aims to return the overvalued asset to its equilibrium value.

When the token supply changes, one of the two assets of the trading pair must be more than the equilibrium value and the other less than the equilibrium value. This corresponds to the pool holding a long position in one asset and a short position in the other asset.

The "long regression" rule then acts to return the long-side asset to its equilibrium value. Once the long-side asset returns, the asset value is deemed to be the new equilibrium value, regardless of the short-side asset's new value. That is, the short side takes a profit or loss.

Calculating the Regression Target #

Suppose the current state is B<B0,Q>Q0B<B_0, Q>Q_0. Here, QQ is a long-side asset andBB is a short-side asset. We know the parameters i,k,Q0i,k,Q_0 and need to solve for the last parameter B0B_0 - the regression target. To calculate the regression target at a certain oracle price, we make the following derivation:

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

By definition, when the supply returns to equilibrium, the new inventory will be B2B_2, which will be identical to the original equilibrium supply i.e.B0=B2B_0=B_2 . Therefore, it is possible to organize the formula into the standard form of the quadratic equation.

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

Eliminating the negative roots yields the regression target B0B_0.

B0=B1+B12kโˆ—(1+4kฮ”QiB1โˆ’1)B_0=B_1+\frac{B_1}{2k}*(\sqrt{1+\frac{4k\Delta Q}{iB_1}}-1)

Similarly, if BB becomes the long-side asset, then we can to solve for Q0Q_0 instead, yielding:

Q0=Q1+Q12kโˆ—(1+4kiฮ”BQ1โˆ’1)Q_0=Q_1+\frac{Q_1}{2k}*(\sqrt{1+\frac{4ki\Delta B}{Q_1}}-1)

Accrued Gains and Losses#

When the system parameters are constant, the price curve is also constant. Even if the supply changes, the supply will return to equilibrium eventually. In other words, the gain or loss due to supply changes is just a "floating" gain or loss, and the term "impermanent loss" has been coined to describe this phenomenon.

The same is true for the PMM algorithm, which is similar to an AMM algorithm if the parameters remain unchanged. However, in practice, one often needs to make adjustments to the price curve. If the "long regression" rule is executed, the equilibrium value on the short side gets reset. The reset equilibrium value may be larger, which is equivalent to a profit. It may also become smaller, which is equivalent to a loss. Each regression converts an unrealized floating profit or loss into a real profit or loss.

Each time you change a parameter, it should ideally eliminate extra positions in time to avoid excessive losses. As for when and how to adjust the curve, it is subjective, and we hope that developers and traders can make the most of this framework to suit their own market-making strategy.