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 . Let's take the base token shortage scenario as an example.
This tells the trader how much they should pay if they buy amount of base tokens.
Rearranging the equation above, the average transaction price is thus:
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 .
Solving the quadratic equation for trading
Without the loss of generality, the integral becomes the following when there is a shorage of quote tokens:
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. ) is given.
Now that are given, we need to calculate , which is found by solving a quadratic equation. Transforming the equation into standard form:
Because , we discard the negative root, and so
It can be proven that:
- When , ; trader buy base token, and should pay
- When , ; trader sell base token, and will receive
- When , .
Solving the quadratic equation for regression targets
When the system is not in the equilibrium state, changes to the oracle price will bring profit or loss. For example, assume that shortage of base tokens is the current state, and 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:
Since we are doing regression, . Rearraging the equation with respect to gives
The negative root does not make sense and is discarded, so is:
In this case, . It can be proven that, when , .
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 is
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 depend on the regression target. So in every trade, we should calculate the regression target well in advance to make the price curve fixed.
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 token when there is a shortage of base tokens, or quote tokens when there is a shortage of quote token, 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.
We will analyze what happens when an LP makes a deposit when there is a shortage of base tokens.
According to the calculation formula of derived above
After an LP deposit base tokens, increases by , and increases more than than 's magnitude. It means that this deposit helps all LPs who provided base token make a profit. The reason why is that the deposit makes the price curve smoother, and the same amount of 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 equilibrium state.
It is important to note that deposit rewards are not risk-free arbitrage trading opportunities.
Similarly, after an LP withdraws base tokens, decreases by , and decreases by more than 's magnitude. This withdrawal causes all LPs who owes 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 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/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.