Understanding the add liquidity event step-by-step.
The event of adding liquidity requires the following information from the user:
Amount of token A to be added
Amount of token B to be added
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
, 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
and an internal vector (
) as input.
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 (
Since this is the first liquidity provision of the pool, this is
PoolValue factor variable on _addLiquidity at AMM.sol
FImp function at AMM.sol
2.1 Update Deamortized Balance of the pool for each token
By the time the pool is created (
) , the
will be equal to the
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.
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.
Updating User Balance (userDepositSnapshot) at _addLiquidity on AMM.sol
2.3 Update Total Balance of the pool for each token
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)