# Liquidity Pools

## Constant Product

The AcalaSwap design was inspired by the “constant product formula” first introduced by Uniswap. The exchange rate curve is defined by

x represents the total number of token A and y represents the total number of token B. At any given time, the reported exchange rate is x/y.

The more liquidity of the pool (bigger x and y), the less impact on price when swapping Token A to Token B.

The trading fees of each trade does increase the constant k, and is shared amongst all LPs based on their shares of the pool. Generally speaking, the more liquidity in the pool (TVL), the less impact a particular trade has on the exchange rate (hence less slippage). Trading volume is a good indicator of fees earned.

## Front-Running & Exploit Protection

#### Trader

Acalaswap adds a protective ceiling for swapping tokens to prevent potential exploits e.g. from front-running.

**Swap:**trader can enter input token A amount, with minimum amount of token B to receive**Swap with Exact Target Token:**trader can enter output token B amount, with maximum amount of A token input

#### Liquidity Provider

Acalaswap adds a protective ceiling for adding and withdrawing liquidity, which to some degree prevents exploits

**Add liquidity**: has a target minimum number of LP shares that one will receive**Withdraw liquidity**: has a target minimum number of token A and B that one can redeem

## Trading Example

User 2 swaps 1000 token A (=20% of Total Token A)

It has a significant impact on the exchange rate, resulting in a high slippage

The more liquidity (higher TVL) in the pool relative to the trade size, the lower the slippage

User 1 swaps 500 token A using

`swapWithExactSupply`

Trading fees are payable in the supply token (Token A): 500*0.3% = 1.5

Actual Token B received, taking into account of changed constant product, given by the formula above = 2250-(4500*2250/(4500+500-1.5)) = 224.39

Slippage = 9.97%

Exchange rate after the trade = 5000/2025.6077 = 2.4684

After 4 trades

Liquidity Provider 1 still has 100 LP Tokens, which amounts to 1.11% of LP shares

Liquidity Provider 1 earned 0.05 Token A and 0.01667 Token B

Since the exchange rate has deviated from 2 to 2.1165, impermanent loss exists.

However, the loss is only realized when the liquidity provider withdraws their liquidity.

## Liquidity Provision Example

Assume the pool opens with 2,200 Token A and 1,100 Token B, and subsequently a number of liquidity providers added liquidity.

Liquidity Provider 1 adds 50 Token A, 25 Token B

current exchange rate = 2

LP tokens = 50*1 + 25 *2 = 100

LP Shares, as the pro-rata liquidity an LP contributed, keep changing as new liquidity is added.

Liquidity Provider 1's LP Shares = 100/4500 = 2.22%

## Impermanent Loss

However, there are various risks of being a liquidity provider especially when there are significant fluctuations in the underlying asset exchange rates, which may result in LP’s worse off than simply holding the tokens.

If the exchange ratio of the underlying tokens diverges from the exchange ratio when the LP provides liquidity, then there’s potential loss compared to just holding the tokens. The bigger the exchange ratio deviation, the bigger the potential loss. Of course, the loss is only realized when LP withdraws their liquidity. And this is called the impermanent loss.

This insightful article has an in-depth analysis on the impermanent loss.

In practice, the actual LP gain is a balance of accumulated fees from each trade on the pools, the accumulated stability fee rewards to LPs, and the impermanent loss.

Last updated