Calculating what proportion of each token users are eligible to withdraw based on the liquidity they provided initially.
Disclaimer: with the current UI, users can only provide or remove liquidity equally on both sides
Single-sided liquidity provision
Quick recap: it is possible to add liquidity on one side of the pool. The pool will track the user's initial exposure, and by the time the user removes liquidity, the withdrawal position will reflect the initial exposure within a new distribution of assets. It means that it is possible (and likely) to withdraw percentages of both assets.
The remove liquidity function allows users to choose what percentage of the amount provided they wish to withdraw at a given instant i . The only user who can remove liquidity is a user who provided liquidity before and still has funds in the pool. r is the proportion of the user's initial deposit that it wishes to withdraw now. Removing "1" corresponds to remove 100% of the funds previously added.
rA≤1 and rB≤1
The event of removing liquidity requires the following information:
1. rAProportion of initial exposure of token A that wants to withdraw.
2. rB Proportion of initial exposure of token B that wants to withdraw.
1. Calculate factors
1.1 Calculate Option Price
For simplicity, let's acknowledge that the option price is a function that required a MarketData and an internal vector (IV) as input.
Pi=fp(IVi−1,MarketDatai)
For more details about the pricing formula and its contract implementation, check this section.
ABPrice variable on _addLiquidity at AMM.sol
1.2 Calculate the Pool's Value Factor (Fvi)
Fvi=DBAi−1⋅Pi+DBBi−1TBAi−1⋅Pi+TBBi−1
The pool's opening factor works as a factor that will update the pool's amortization as time passes.
1.3 Calculate multipliers
The redemption multipliers are defined as:
mAAi=DBAi−1min(Fvi⋅DBAi−1;TBAi−1)
mBBi=DBBi−1min(Fvi⋅DBBi−1;TBBi−1)
mABi=DBAi−1TBBi−1−mBBi⋅DBBi−1
mBAi=DBBi−1TBAi−1−mAAi⋅DBAi−1
getting multiplier struct on _removeLiquidity at AMM.sol
After the withdraw, the contract calculates the updated user balance for each token.
UBAu=UBAui−1⋅(1−rA)
UBBu=UBBui−1⋅(1−rB)
on _removeLiquidity at AMM.sol
1.6 Update new deamortized balance for each token
DBAi=DBAi−1−rA⋅FvduUBAui−1
DBBi=DBBi−1−rB⋅FvduUBBui−1
on _removeLiquidity at AMM.sol
1.7 Update new pool balance for each token
The contract will calculate the new total balance for each token considering the recent withdrawal.
TBAi=TBAi−1+Ai
TBBi=TBBi−1+Bi
At the contract level, an ERC20 transfer is happening. Total balance (TB) is just a mathematical representation. The Total balance is checked by consulting the balanceOf() of the pool contract of the respective token (tokenA or tokenB)