DODO Docs
Search
⌃K

# The Math Behind the PMM

## PMM Price Curve Definition

The price curve described by the PMM is mathematically defined as follows:
$P=iR$
If
$B
, then
$R=1-k+(\frac{B_0}{B})^2k$
If
$Q
, then
$R=1/(1-k+(\frac{Q_0}{Q})^2k)R$
Otherwise,
$R=1$
.
where
• $i$
is the initial "guide price"
• $k$
is the "slippage factor"
• $B$
denotes the current base token supply
• ${B_0}$
denotes the equilibrium base token supply
• $Q$
denotes the current quote token supply
• ${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
$P$
. Let's examine the case where there is a shortage of base tokens.
This equation describes the case where the user has
$B_1$
base tokens and wishes to purchase enough to have
$B_2$
tokens. To do this, the user needs to calculate the average price they will pay per token purchased.
$\Delta Q =\int^{B_2}_{B_1}PdB$
$= \int^{B_2}_{B_1}(1-k)i+(B_0/B)^2kidB$
$= 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=\frac{\Delta Q}{B_2-B_1}=i*(1-k+k\frac{B_0^2}{B_1B_2})$

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$
) is given.
$\Delta Q = i(B_2-B_1)*(1-k+k\frac{B_0^2}{B_1B_2})$
Now that
$ΔQ, B_0​, B_1$
are given, we need to calculate
$B_2​$
, which is found by solving a quadratic equation. Transforming the equation into standard form:
$(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=\frac{kB_0^2}{B_1}-B_1+kB_1-\frac{\Delta Q}{i}, \ c=-kB_0^2$
Since
$B_2>=0$
, we discard the negative root, yielding
$B_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

$B$
and
$Q$
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
$i$
,
$k$
,
${B_0}$
, and
${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
$BQ_0$
. Here,
$Q$
is a long-side asset and
$B$
is a short-side asset. We know the parameters
$i,k,Q_0$
and need to solve for the last parameter
$B_0$
- the regression target. To calculate the regression target at a certain oracle price, we make the following derivation:
$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
$B_2$
, which will be identical to the original equilibrium supply i.e.
$B_0=B_2$
. Therefore, it is possible to organize the formula into the standard form of the quadratic equation.
$\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
$B_0$
.
$B_0=B_1+\frac{B_1}{2k}*(\sqrt{1+\frac{4k\Delta Q}{iB_1}}-1)$
Similarly, if
$B$
becomes the long-side asset, then we can to solve for
$Q_0$
$Q_0=Q_1+\frac{Q_1}{2k}*(\sqrt{1+\frac{4ki\Delta B}{Q_1}}-1)$