# Remove Liquidity

{% hint style="info" %}
Disclaimer: with the current UI, users can only provide or remove liquidity equally on both sides
{% endhint %}

**Single-sided liquidity provision**\
\&#xNAN;*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.

$$r\_A≤1$$ and $$r\_B≤ 1$$&#x20;

The event of removing liquidity requires the following information:\
1\. $$r\_A$$Proportion of initial exposure of token A that wants to withdraw.\
2\. $$r\_B$$ Proportion of initial exposure of token B that wants to withdraw.

### 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 and 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;

$$\displaystyle Fv\_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;

The pool's opening factor works as a factor that will update the pool's amortization as time passes.&#x20;

### 1.3 Calculate multipliers

The redemption multipliers are defined as:

$$\displaystyle mAA\_i= \frac{min(Fv\_i\cdot DB\_{A\_{i-1}};TB\_{A\_{i-1}})}{DB\_{A\_{i-1}}}$$&#x20;

$$\displaystyle mBB\_i= \frac{min(Fv\_i\cdot DB\_{B\_{i-1}};TB\_{B\_{i-1}})}{DB\_{B\_{i-1}}}$$&#x20;

$$\displaystyle mAB\_i= \frac{TB\_{B\_{i-1}}-mBB\_i\cdot DB\_{B\_{i-1}}}{DB\_{A\_{i-1}}}$$&#x20;

$$\displaystyle mBA\_i= \frac{TB\_{A\_{i-1}}-mAA\_i\cdot DB\_{A\_{i-1}}}{DB\_{B\_{i-1}}}$$&#x20;

![getting multiplier struct on \_removeLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuE53SCFX_WwU3WedD%2FScreen%20Shot%202021-01-13%20at%2002.33.30.png?alt=media\&token=116b0bdf-b2a4-488b-90aa-dde59bf2d4de)

![function \_getMultiplers responsible for ](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuELStNdRS1wz5h2S-%2FScreen%20Shot%202021-01-13%20at%2002.34.44.png?alt=media\&token=0f6f19c5-7062-467b-99c2-658b6477eef1)

### 1.4 Calculate withdrawal amount of each token

$$\displaystyle A\_i=-\[mAA\_i\cdot r\_A\cdot \frac{UB\_{A\_{u}}}{F\_{v\_{du}}}+mBA\_i\cdot r\_B\cdot \frac {UB\_{B\_{u}}}{F\_{v\_{du}}}]$$&#x20;

$$\displaystyle B\_i=-\[mBB\_i\cdot r\_B\cdot \frac{UB\_{B\_{u}}}{F\_{v\_{du}}} + mAB\_i \cdot r\_A\cdot \frac {UB\_{A\_{u}}}{F\_{v\_{du}}}]$$&#x20;

![on \_removeLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuFrnY2VhciFMJV8KW%2FScreen%20Shot%202021-01-13%20at%2002.41.20.png?alt=media\&token=8c049183-e697-4e20-b781-fad47e99ad98)

### 1.5 Update new user balance for each token

After the withdraw, the contract calculates the updated user balance for each token.

$$UB\_{A\_u}=UB\_{A\_{u\_{i-1}}}\cdot (1-r\_A)$$&#x20;

$$UB\_{B\_u}=UB\_{B\_{u\_{i-1}}}\cdot (1-r\_B)$$&#x20;

![on \_removeLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuGMSWtsT2FLuYBOHy%2FScreen%20Shot%202021-01-13%20at%2002.43.34.png?alt=media\&token=4db39c0e-b5c1-45aa-92a9-027ec47b04bf)

### 1.6 Update new deamortized balance for each token

$$\displaystyle DB\_{A\_i}=DB\_{A\_{i-1}}-r\_A\cdot \frac{UB\_{A\_{u\_{i-1}}}}{F\_{v\_{du}}}$$&#x20;

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

![on \_removeLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuGVJL9mzbAmgKx3vV%2FScreen%20Shot%202021-01-13%20at%2002.44.11.png?alt=media\&token=6c18231a-7d71-4e60-8293-3239bbe49f47)

### 1.7 Update new pool balance for each token

The contract will calculate the new total balance for each token considering the recent withdrawal.

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

$$TB\_{B\_i}=TB\_{B\_{i-1}}+B\_i$$&#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 %}

![on \_removeLiquidity at AMM.sol](https://1385084817-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTZ6iLmulhA48zQv8n%2F-MQu8jKt4stv_HHPd8Fj%2F-MQuGeE0O6wyXtpaGZOa%2FScreen%20Shot%202021-01-13%20at%2002.44.50.png?alt=media\&token=a137ede3-2f7c-4fb1-9e37-5b43aace255b)

{% hint style="success" %}
Remove Liquidity ✅
{% endhint %}
