Olive Docs
Smart Contract

Liquidity Pool Smart Contracts

Liquidity Pool Smart Contracts

This page documents the liquidity-pool instruction files and the checks they perform.

add_liquidity.rs

Current flow:

  1. Validate the pool, custody, LP mint, user token account, and remaining oracle accounts.
  2. Calculate the deposit value and LP token amount.
  3. Transfer the user's deposit tokens into the custody token account.
  4. Mint LP tokens to the user's LP token account.
  5. Increase custody.token_owned by the deposited amount.
  6. Recompute pool AUM and update pool accounting.

Liquidity check:

  1. No free-liquidity check is needed because the user is adding tokens to the pool.

Extra checks:

  1. The instruction checks the minimum LP amount out, so the user is protected from receiving fewer LP tokens than expected.

remove_liquidity.rs

Current flow:

  1. Validate the pool, custody, LP mint, user LP token account, and receiving token account.
  2. Calculate the token amount returned to the user.
  3. Calculate the withdrawal fee.
  4. Require the returned token amount to be at least min_amount_out.
  5. Calculate withdrawal_amount = transfer_amount + fee_amount.
  6. Check custody.token_owned - custody.token_locked >= withdrawal_amount.
  7. Transfer transfer_amount from custody to the user.
  8. Burn the user's LP tokens.
  9. Subtract withdrawal_amount from custody.token_owned.
  10. Recompute pool AUM and update pool accounting.

Liquidity check:

  1. This file correctly checks free liquidity before LP withdrawal.
  2. It never allows LP withdrawal to use locked position backing.

Extra checks:

  1. It checks min_amount_out.
  2. It includes the withdrawal fee in the free-liquidity check.

add_pool.rs

Current flow:

  1. Create and initialize a pool account.
  2. Store pool metadata such as name, authority, ratios, AUM fields, and open interest fields.
  3. Initialize long and short open interest to zero.
  4. Emit the pool-created event.

Liquidity check:

  1. No token payout happens.
  2. No free-liquidity check is needed.

add_custody.rs

Current flow:

  1. Add a custody account to an existing pool.
  2. Store the custody mint, decimals, oracle information, and custody configuration.
  3. Initialize custody accounting fields.

Liquidity check:

  1. No pool payout happens.
  2. No position lock is created.
  3. No free-liquidity check is needed.

remove_custody.rs

Current flow:

  1. Remove a custody account from the pool configuration.
  2. Validate that the custody can be removed.
  3. Update pool custody metadata.

Liquidity check:

  1. This should only be safe if the custody has no owned or locked funds remaining.
  2. If the file does not enforce that invariant, it needs an explicit no-balance and no-lock check before removal.

create_lp_mint.rs

Current flow:

  1. Create the LP token mint for a pool.
  2. Assign mint authority to the protocol-controlled authority.

Liquidity check:

  1. No pool payout happens.
  2. No position lock is created.
  3. No free-liquidity check is needed.

realloc_pool.rs

Current flow:

  1. Resize or update pool allocation data.
  2. Validate the target ratio input.
  3. Store the new pool ratio configuration.

Liquidity check:

  1. No pool payout happens.
  2. No position lock is created.
  3. No free-liquidity check is needed.

On this page