# mintExactA(_a, min_amount)
> mint `_a` of a, pay no more than `max_amount` of baseToken
The following relationship always exist in iGain:
\begin{gather*}
1\ a+ 1\ b = 1\ DAI
\end{gather*}
To mint exact `_a` of $a, the following steps will be taken:
1. pay $x$ $DAI to the system
2. $x$ $DAI -> $x$ $a + $x$ $b (According to the above relationship )
3. swap $x$ $b to $y$ $a
4. total amount of $a will be $x+y$
From the above steps, we can write down the following relationship:
\begin{gather*}
\_a = x + y\\
\end{gather*}
_a: the amount of $a we want to mint; `variable: _a in code`
x: the amount of $b needs to be swapped for $a; the amount of $DAI we paid; `variable: amount in code`
y: the amount of $b we have after swapping; `variable: y in code`
According to the constant product market-making model, there are the following relationships:
\begin{gather*}
A*B =(A{\color{black}{-}}y)(B{\color{black}{+}}f\times x)
\end{gather*}
A: `poolA`; how many $a in the pool; `reserveOut`
B: `poolB`; how many $b in the pool; `reserveIn`
f: `fee()/1e18`; dynamic trading fee
Solving the simultaneous equations of the above two equations, we can get:
\begin{split}
x &= \frac{\sqrt{(f(A{\color{black}{-}}a)+B)^2\color{black}{+}4aBf}+af{\color{black}{-}}B{\color{black}{-}}Af}{2f}\\
y &= \_a-x
\end{split}
(for simplicity, set _a = a here.)
*Note:
The math we use in burnExactA() is almost the same as the previously audited function, burnA(), just swap poolA & poolB. For a better understanding, I quote part of the previous documents below for reference.*
# burnA(_a, min_amount)
> burn `_a` of a, receive more than `min_amount` of baseToken
The following relationship always exist in iGain:
\begin{gather*}
1\ a+ 1\ b = 1\ USDC
\end{gather*}
When burning `_a` of $a to $USDC, in order to get the same amount of $a and $b, part of $a should be swapped for $b
\begin{gather*}
\_a - x = y\\
\end{gather*}
_a: the amount of $a we want to burn
x: the amount of $a needs to be swapped for $b
y: the amount of $b we have after swapping
According to the constant product market-making model, there are the following relationships:
\begin{gather*}
A*B =(A{\color{black}{+}}f\times x)(B{\color{black}{-}}y)
\end{gather*}
A: `poolA`; how many $a in the pool
B: `poolB`; how many $b in the pool
f: `fee()/1e18`; dynamic trading fee
Solving the simultaneous equations of the above two equations, we can get:
\begin{split}
x &= \frac{\sqrt{(f(B{\color{black}{-}}a)+A)^2\color{red}{+}4aAf}+af{\color{black}{-}}A{\color{black}{-}}Bf}{2f}\\
y &= \_a-x
\end{split}
(for simplicity, set _a = a here.)
# burnLP(lp, min_amount)
> burn `lp` of liquidity provider share, recieve more than `min_amount` of baseToken
the amount of lp token is proportional to the square root of A*B:
\begin{gather*}
lp\propto \sqrt{AB}
\end{gather*}
If we want to burn lp to get USDC, we need to calculate how many $a and $b in the pool need to be burned
\begin{gather*}
\frac{final\ state}{initial\ state}=\frac{L - f\times lp}{L} = \sqrt{\frac{(A-x)(B-x)}{AB}}
\end{gather*}
A: `poolA`; how many $a in the pool
B: `poolB`; how many $b in the pool
L: `totalSupply` of $iGLP (iGain LP token)
lp: the amount of lp token to be burned
f: `fee()/1e18`; dynamic trading fee
x: the amount of $a and $b neeed to be bruned
solve the equation above for x, we get:
\begin{gather*}
x = \frac{1}{2}[A+B-\sqrt{(A+B)^2-\frac{4ABf\times lp(2-\frac{f\times lp}{L})}{L}}]
\end{gather*}