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:
- Validate the pool, custody, LP mint, user token account, and remaining oracle accounts.
- Calculate the deposit value and LP token amount.
- Transfer the user's deposit tokens into the custody token account.
- Mint LP tokens to the user's LP token account.
- Increase
custody.token_ownedby the deposited amount. - Recompute pool AUM and update pool accounting.
Liquidity check:
- No free-liquidity check is needed because the user is adding tokens to the pool.
Extra checks:
- 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:
- Validate the pool, custody, LP mint, user LP token account, and receiving token account.
- Calculate the token amount returned to the user.
- Calculate the withdrawal fee.
- Require the returned token amount to be at least
min_amount_out. - Calculate
withdrawal_amount = transfer_amount + fee_amount. - Check
custody.token_owned - custody.token_locked >= withdrawal_amount. - Transfer
transfer_amountfrom custody to the user. - Burn the user's LP tokens.
- Subtract
withdrawal_amountfromcustody.token_owned. - Recompute pool AUM and update pool accounting.
Liquidity check:
- This file correctly checks free liquidity before LP withdrawal.
- It never allows LP withdrawal to use locked position backing.
Extra checks:
- It checks
min_amount_out. - It includes the withdrawal fee in the free-liquidity check.
add_pool.rs
Current flow:
- Create and initialize a pool account.
- Store pool metadata such as name, authority, ratios, AUM fields, and open interest fields.
- Initialize long and short open interest to zero.
- Emit the pool-created event.
Liquidity check:
- No token payout happens.
- No free-liquidity check is needed.
add_custody.rs
Current flow:
- Add a custody account to an existing pool.
- Store the custody mint, decimals, oracle information, and custody configuration.
- Initialize custody accounting fields.
Liquidity check:
- No pool payout happens.
- No position lock is created.
- No free-liquidity check is needed.
remove_custody.rs
Current flow:
- Remove a custody account from the pool configuration.
- Validate that the custody can be removed.
- Update pool custody metadata.
Liquidity check:
- This should only be safe if the custody has no owned or locked funds remaining.
- 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:
- Create the LP token mint for a pool.
- Assign mint authority to the protocol-controlled authority.
Liquidity check:
- No pool payout happens.
- No position lock is created.
- No free-liquidity check is needed.
realloc_pool.rs
Current flow:
- Resize or update pool allocation data.
- Validate the target ratio input.
- Store the new pool ratio configuration.
Liquidity check:
- No pool payout happens.
- No position lock is created.
- No free-liquidity check is needed.