> For the complete documentation index, see [llms.txt](https://daofair.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://daofair.gitbook.io/doc/problem-solving.md).

# DAOFAIR Problem Solving

**Problem Solving the quadratic equation for trading**

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

\Delta B = \frac{1}{i}(Q\_2-Q\_1)\*(1-k+k\frac{Q\_0^2}{Q\_1Q\_2})ΔB=i1​(Q2​−Q1​)∗(1−k+kQ1​Q2​Q02​​)

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. \Delta BΔB) is given.

Now that \Delta B, Q\_0, Q\_1ΔB,Q0​,Q1​ are given, we need to calculate Q\_2Q2​, which is found by solving a quadratic equation. Transforming the equation into standard form:

(1-k)Q\_2^2+(\frac{kQ\_0^2}{Q\_1}-Q\_1+kQ\_1-i\Delta B)Q\_2-kQ\_0^2=0(1−k)Q22​+(Q1​kQ02​​−Q1​+kQ1​−iΔB)Q2​−kQ02​=0

let \ a=1-k, \ b=\frac{kQ\_0^2}{Q\_1}-Q\_1+kQ\_1-i\Delta B, \ c=-kQ\_0^2let a=1−k, b=Q1​kQ02​​−Q1​+kQ1​−iΔB, c=−kQ02​

Because Q\_2>=0Q2​>=0, we discard the negative root, and so

Q\_2=\frac{-b+\sqrt{b^2-4ac}}{2a}Q2​=2a−b+b2−4ac​​

It can be proven that:

* When \Delta B>0ΔB>0, Q\_2>Q\_1Q2​>Q1​; trader buy base token, and should pay Q\_2-Q\_1Q2​−Q1​
* When \Delta B<0ΔB<0, Q\_2\<Q\_1Q2​\<Q1​; trader sell base token, and will receive Q\_1-Q\_2Q1​−Q2​
* When \Delta B=0ΔB=0, Q\_2=Q\_1Q2​=Q1​.

At the same time, DAOFAIR V2 focus on verifying the special case of k=0, and k=1 to support the constant price of selling and the bonding curve of the standard AMM.

### Problem 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:

Given \Delta Q = i(B\_2-B\_1)\*(1-k+k\frac{B\_0^2}{B\_1B\_2})ΔQ=i(B2​−B1​)∗(1−k+kB1​B2​B02​​)

Since we are doing regression, B\_2=B\_0B2​=B0​. Rearraging the equation with respect to B\_0B0​ gives

\frac{k}{B\_1}B\_0^2+(1-2k)B\_0-\[(1-k)B\_1+\frac{\Delta Q}{i}] = 0B1​k​B02​+(1−2k)B0​−\[(1−k)B1​+iΔQ​]=0

The negative root does not make sense and is discarded, so B\_0B0​ is:

B\_0=B\_1+B\_1\frac{\sqrt{1+\frac{4k\Delta Q}{B\_1 i}}-1}{2k}B0​=B1​+B1​2k1+B1​i4kΔQ​​−1​

In this case, \Delta Q=Q-Q\_0ΔQ=Q−Q0​. It can be proven that, when \Delta Q \ge 0ΔQ≥0, B\_0\ge B\_1B0​≥B1​.

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 Q\_0Q0​ is

Q\_0=Q\_1+Q\_1\*\frac{\sqrt{1+\frac{4k\Delta B i}{Q\_1}}-1}{2k}Q0​=Q1​+Q1​∗2k1+Q1​4kΔBi​​−1​

## Peripheral

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

### Trades

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.

### Deposit

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 B\_0B0​ derived above B\_0=B\_1+B\_1\*\frac{\sqrt{1+\frac{4k\Delta Q}{B\_1 i}}-1}{2k}B0​=B1​+B1​∗2k1+B1​i4kΔQ​​−1​

After an LP deposit bb base tokens, B\_1B1​ increases by bb, and B\_0B0​ increases more than than bb'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 \Delta QΔ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 equilibrium state.

**NOTE**

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

### Withdrawal

Similarly, after an LP withdraws bb base tokens, B\_1B1​ decreases by bb, and B\_0B0​ decreases by more than bb'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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://daofair.gitbook.io/doc/problem-solving.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
