# Providing Liquidity

Liquidity providers (LPs) deposit tokens into Risk Marketplace pools and receive pool shares (BPT - Balancer Pool Tokens) representing their proportional ownership. LPs earn LP fees generated by trading activity in the pool.

**Pool Shares**

When providing liquidity, users receive pool shares calculated based on their contribution relative to the existing pool value. These shares:

* Represent proportional ownership of all tokens in the pool
* Accumulate value as LP fees are retained in the pool
* Can be redeemed for underlying tokens at any time (subject to circuit breakers)

## 1.  Joining a Pool

### *1 a)  Proportional Join (Multi-Asset Deposit)*

Deposit all pool tokens in their current ratio. This method incurs no LP fees since it maintains pool balance.

Calculation:

$$tokenAmountIn\_i = \frac{poolAmountOut}{poolSupply} \times B\_i$$

Where:

* $$poolAmountOut$$ = Pool shares to receive
* $$poolSupply$$ = Current total pool shares
* $$B\_i$$ = Current balance of token (i)

#### Key Functions

* **joinPool Method**: Handles depositing multiple tokens proportionally in exchange for pool\
  shares.
  * Parameters:
    1. `poolAmountOut:` The amount of pool shares  to receive
    2. `maxAmountsIn[]`: Maximum amounts of each token willing to deposit (slippage protection)

#### Process

1. Ratio Calculation
   * The system calculates the ratio of requested pool shares to total supply.
   * This ratio determines how much of each token is required
2. Token Transfer
   * For each token in the pool, the required amount is calculated: `tokenAmountIn = ratio × tokenBalance`.
   * Tokens are transferred from the user to the pool; If any calculated amount exceeds `maxAmountsIn`, the transaction reverts
3. Share Minting
   * Pool shares are minted to the user equal to `poolAmountOut`

*Example*: A pool contains 1,000 ETH and 2,000,000 USDC with 100 BPT total supply. To receive 10 BPT (10% of supply):

* ETH required: 10% × 1,000 = 100 ETH
* USDC required: 10% × 2,000,000 = 200,000 USDC

### *1 b)  Single-Asset Join (Single Token Deposit)*

Deposit only one token. A portion of your deposit is implicitly swapped to acquire the other pool tokens, incurring a LP fee on that portion.

Pool Shares Received:

$$
poolAmountOut = \left( \left( \frac{B\_i + A\_i \times (1 - (1 - W\_i^n) \times LPFee)}{B\_i} \right)^{W\_i^n} - 1 \right) \times poolSupply
$$

Where:

* $$A\_i$$ = Amount of token deposited
* $$B\_i$$ = Current balance of deposit token
* $$W\_i^n$$ = Normalized weight of deposit token&#x20;
* $$LPFee$$ = Dynamic LP fee

#### Key Functions

* **joinswapExternAmountIn Method**: Deposit an exact amount of one token, receive calculated pool\
  shares.
  * Parameters:
    1. `tokenIn`: Address of the token being deposited
    2. `tokenAmountIn`: Exact amount of tokens to deposit
    3. `minPoolAmountOut`: Minimum pool shares to receive (slippage protection)

* **joinswapPoolAmountOut Method**: Specify exact pool shares desired, system calculates required\
  deposit.
  * Parameters:
    1. `tokenIn`: Address of the token being deposited
    2. `poolAmountOut`: Exact amount of pool shares to receive
    3. `maxAmountIn`: Maximum tokens willing to deposit (slippage protection)

#### Process

1. Pool Shares Calculation
   * Uses weighted math to determine fair pool share output
   * Accounts for the implicit swap and associated fees
2. Execution
   * Tokens transferred from user to pool
   * Pool shares minted to user

*Fee Logic: Only the portion that "rebalances" the pool pays the LP fee. If a token has 25% weight, depositing that token implicitly trades 75% of it for other tokens—only that 75% incurs fees (see below example).*

*Example*: Single-asset join with 1,000 USDC into a pool where USDC has 20% weight and LP fee is 0.3%:

* Implicit swap portion: 80% × 1,000 = 800 USDC
* Fee applied: 0.3% × 800 = 2.4 USDC effective fee
* Net contribution used for share calculation: \~997.6 USDC equivalent

## 2.  Exiting a Pool

### *2 a)  Proportional Exit (Multi-Asset Withdrawal)*

Redeem pool shares for all tokens proportionally. This method incurs no LP fees.

Calculation:

$$tokenAmountOut\_i = \frac{poolAmountIn}{poolSupply} \times B\_i$$

Where:

* $$poolAmountIn$$ = Pool shares being redeemed
* $$poolSupply$$ = Current total pool shares
* $$B\_i$$ = Current balance of token (i)

#### Key Functions

* **exitPool Method**: Burn pool shares and receive all underlying tokens proportionally.
  * Parameters:
    1. `minAmountsOut`: Minimum amounts of each token to receive (slippage protection)
    2. `poolAmountIn`: Amount of pool shares (BPT) to redeem

#### Process

1. Ratio Calculation
   * The system calculates the ratio of redeemed shares to total supply which determines how much of each token the user receives
2. Share Burning
   * Pool shares are pulled from the user's balance and are burned, reducing total supply
3. Token Distribution
   * For each token: tokenAmountOut = ratio × tokenBalance  and if any calculated amount is below minAmountsOut, the transaction reverts
   * Tokens are transferred from the pool to the user

*Example*: Redeeming 10 BPT from a pool with 100 BPT supply, 1,000 ETH, and 2,000,000 USDC:

* ETH received: 10% × 1,000 = 100 ETH
* USDC received: 10% × 2,000,000 = 200,000 USDC

### *2 b)  Single-Asset Exit (Single Token Withdrawal)*

Redeem pool shares for only one token. The other tokens are implicitly sold for your desired token, incurring a LP fee.

**Tokens Received:**

$$
tokenAmountOut =
B\_o \times
\left(
1 -
\left(
\frac{poolSupply - poolAmountIn}{poolSupply}
\right)^{\frac{1}{W\_o^n}}
\right)
\times
\left( 1 - (1 - W\_o^n) \times LPFee \right)
$$

**Where:**

* $$B\_o$$ = Current balance of withdrawal token
* $$W\_o^n$$ = Normalized weight of withdrawal token
* $$poolAmountIn$$ = Pool shares being redeemed

#### Key Functions

* **exitswapPoolAmountIn Method**: Burn exact pool shares, receive calculated token amount.
  * Parameters:
    1. `poolAmountIn`: Exact amount of pool shares to redeem
    2. `minAmountOut`: Minimum tokens to receive (slippage protection)
    3. `tokenOut`: Address of the token to receive

* **exitswapExternAmountOut Method**: Specify exact token amount desired, system calculates shares to burn.
  * Parameters:
    1. `tokenOut`: Address of the token to receive
    2. `tokenAmountOut`: Exact amount of tokens to receive
    3. `maxPoolAmountIn`: Maximum pool shares willing to burn (slippage protection)

#### Process

1. Output Calculation
   * Uses weighted math to determine fair token output.
2. Execution
   * Pool shares pulled from user and burned&#x20;
   * Requested token transferred from pool to user

*Fee Logic: The fee applies to the portion being implicitly swapped. Withdrawing a 20%*\
*weight token means 80% of the value comes from selling other tokens—that portion pays*\
*the LP fee.*
