Understanding the add liquidity event step-by-step.
Add Liquidity
The event of adding liquidity requires the following information from the user:
1.
Adu
Amount of token A to be added
2.
Bdu
Amount of token B to be added
3. Owner
After the information was supplied, the add liquidity function will perform the following activities:
1. Calculate factors
1.1 Calculate Option Price
This happens if
i=0
. If
i=0
, it is not required to calculate the option price in the very first liquidity provision in a pool.
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 or its contract implementation for pricing the options, check this section.
ABPrice variable on _addLiquidity at AMM.sol
1.2 Calculate the Pool's Value Factor (
Fvi
)
If
i=0
,
Fvi=1
If
i=0
,
Fvi=DBAi−1⋅Pi+DBBi−1TBAi−1⋅Pi+TBBi−1
Since this is the first liquidity provision of the pool, this is
i=0
and therefore,
Fvi=1
.
PoolValue factor variable on _addLiquidity at AMM.sol
FImp function at AMM.sol
2. Updates
2.1 Update Deamortized Balance of the pool for each token
By the time the pool is created (
i=0
) , the
DBAi
and
DBBi
will be equal to the
Adu
and
Bdu
since:
Fvi=1
DBAi−1=0
DBBi−1=0
DBAi=DBAi−1+FviAdu
DBBi=DBBi−1+FviBdu
deamortizedBalance on _addLiquidity at AMM.sol
2.2 Update the User Balances for each token and the Pool Factor previously calculated
Updating this factor is essential, especially when there is a re-add liquidity event.
UBAu=Adu
UBBu=Bdu
The
UBfu
works as if it was a snapshot of the pool's factor at the moment of this user's deposit. This factor will be updated in the case of re-add liquidity by the user.
UBFu=Fvdu
Updating User Balance (userDepositSnapshot) at _addLiquidity on AMM.sol
2.3 Update Total Balance of the pool for each token
TBAi=TBAi−1+Adu
TBBi=TBBi−1+Bdu
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)