# Re-add Liquidity

## Re-add Liquidity

This event can only happen for users that provided liquidity before. The event of re-adding liquidity requires the following information from the user:\
1\. $$A\_{du}$$ Amount of token A to be added\
2\. $$B\_{du}$$ Amount of token B to be added\
3\. Owner

&#x20;After the information was supplied, the `re-add liquidity` function will perform the following activities:

### 1. Calculate factors

### 1.1 Calculate Option Price&#x20;

For simplicity, let's acknowledge that the option price is a function that required a $$MarketData$$ and an internal vector ($$IV$$) as input.

$$P\_i=f\_p(IV\_{i-1},MarketData\_i)$$&#x20;

For more details about the pricing formula or its contract implementation, check [this section](https://app.gitbook.com/@pods-finance-1/s/teste/~/drafts/-MNH5EfMIG3zYkpvevUc/options-amm-overview/optionamm/pricing). .&#x20;

![ABPrice variable on \_addLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu4GqXGOusUGlX4Sgq%2F-MQu4nFlTcBWTvQd_jHa%2FScreen%20Shot%202021-01-13%20at%2001.52.38.png?alt=media\&token=ca740243-5994-447d-ae98-f85e0924bcc7)

### 1.2 Calculate the Pool's Value Factor ($$F\_{v\_{i}}$$)&#x20;

Since the re-add liquidity happens in a given moment$$i≠0$$, $$F\_{v\_{i}}$$may not be equal to 1, so there should be a  $$F\_{v\_{i}}$$calculation. The following formula will trigger the calculation:

$$\displaystyle F\_{v\_{i}}= \frac{TB\_{A\_{i-1}}\cdot P\_i+TB\_{B\_{i-1}}}{DB\_{A\_{i-1}} \cdot P\_i+DB\_{B\_{i-1}}}$$&#x20;

![FimpOpening variable on \_addLiquidity function at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQtoHzc8GyuIXApkQlp%2F-MQtoqSEnGAFHunBNDEQ%2FScreen%20Shot%202021-01-13%20at%2000.38.57.png?alt=media\&token=bd03352e-85a0-4043-8d12-21d610889c65)

![FImp function at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQtoHzc8GyuIXApkQlp%2F-MQtqzKe4-6dxJBkTaVx%2FScreen%20Shot%202021-01-13%20at%2000.47.34.png?alt=media\&token=397c8d52-790d-44d2-bb8b-5cffcb431d3e)

### 2. Updates

### 2.1 Update Deamortized Balance of the pool for each token

In this step, the AMM saves how much it owes the user at the current Pool Value Factor "expense."&#x20;

$$\displaystyle DB\_{A\_{i}}=DB\_{A\_{i-1}} +\frac{A\_{du}}{F\_{v\_{i}}}$$&#x20;

$$\displaystyle DB\_{B\_{i}}=DB\_{B\_{i-1}} +\frac{B\_{du}}{F\_{v\_{i}}}$$&#x20;

![deamortizedBalance on \_addLiquidity at AMM.sol ](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQs-XP2e1_rjBATIWCl%2F-MQsUEOVd0nNa05RUkSA%2FScreen%20Shot%202021-01-12%20at%2018.24.45.png?alt=media\&token=ec97b078-5c5b-40fc-beb5-e48eacc07663)

### 2.2 Update the User Balances for each token and the Pool Factor previously calculated

Updating this factor represents combining both deposits considering the time each entered the pool. $$\displaystyle UB\_{A\_{u}}=UB\_{A\_{u\_{i-1}}}\cdot \frac {F\_{v\_{i}}}{UB\_{F\_{u}}}+A\_{du}$$&#x20;

$$\displaystyle UB\_{B\_{u}}=UB\_{B\_{u\_{i-1}}}\cdot \frac {F\_{v\_{i}}}{UB\_{F\_{u}}}+B\_{du}$$&#x20;

The $$UB\_{f\_{u}}$$ works as if it was a picture of the pool's factor at the moment of this user's deposit. This factor will be updated again whenever there is a `re-add liquidity` event by the same user.&#x20;

$$UB\_{F\_{u}}=Fv\_i$$&#x20;

![Updating User Balance (userDepositSnapshot) at \_addLiquidity on AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQtl_Qvl5n5XpfO5UWY%2F-MQtnKp-AcxlCkVXnPWV%2FScreen%20Shot%202021-01-13%20at%2000.32.20.png?alt=media\&token=c6f295e0-5411-4a56-82d3-528d6a08a655)

### 2.3 Update Total Balance of the pool for each token

$$TB\_{A\_{i}}=TB\_{A\_{i-1}} +A\_{du}$$&#x20;

$$TB\_{B\_{i}}=TB\_{B\_{i-1}} +B\_{du}$$&#x20;

{% hint style="info" %}
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)
{% endhint %}

![](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQtl_Qvl5n5XpfO5UWY%2F-MQtnZCGVSnCjA2K5iuJ%2FScreen%20Shot%202021-01-13%20at%2000.33.20.png?alt=media\&token=44b0b6fe-e311-4335-a70b-dea51c076470)

{% hint style="success" %}
Re-add liquidity ✅
{% endhint %}
