Withdraw
Removes liquidity from a pool on the CoolDEX.
Instruction
Parameters
amount
u64
Amount of LP tokens to burn
min_coin_amount
Option<u64>
Minimum acceptable coin token amount (slippage protection)
min_pc_amount
Option<u64>
Minimum acceptable PC token amount (slippage protection)
Account Setup
1
Token program
SPL Token program
TOKEN_PROGRAM_ID
2
AMM info
AMM account
[program.toBuffer(), marketId.toBuffer(), "amm_associated_seed"]
3
AMM authority
AMM authority PDA
[Buffer.from("amm authority")]
4
AMM LP mint
LP token mint
[program.toBuffer(), marketId.toBuffer(), "lp_mint_associated_seed"]
5
AMM coin vault
Coin token vault
[program.toBuffer(), marketId.toBuffer(), "coin_vault_associated_seed"]
6
AMM PC vault
PC token (SOL) vault
[program.toBuffer(), marketId.toBuffer(), "pc_vault_associated_seed"]
7
User source LP
User's LP token account
getAssociatedTokenAddress(lpMint, userWallet, false)
8
User dest coin
User's coin token account
getAssociatedTokenAddress(coinMint, userWallet, false)
9
User dest PC
User's PC token (SOL) account
getAssociatedTokenAddress(NATIVE_MINT, userWallet, false)
10
Source LP owner
Transaction signer (User wallet)
-
Function Logic
Verifies the transaction is signed by the LP token owner
Checks that the AMM is in a valid state for withdrawals
Verifies that the LP token amount is not zero or greater than the user's balance
Verifies that the LP token amount is less than the total supply (can't withdraw everything)
Calculates coin and PC amounts based on the proportion of LP tokens being burned
Verifies that calculated amounts meet minimum specified amounts (slippage check)
Transfers coin and PC tokens from respective vaults to the user
Burns the LP tokens
Updates the AMM's LP token supply
Token Amount Calculation
The amounts of coin and PC tokens to withdraw are calculated using the proportion of LP tokens being burned:
Where:
amount
is the LP token amount being burnedamm.lp_amount
is the current total supply of LP tokenstotal_coin_without_take_pnl
is the total coin amount in the pooltotal_pc_without_take_pnl
is the total PC amount in the pool
Notes
Withdrawals always maintain the current ratio of assets in the pool
LP tokens are burned in exchange for the underlying assets
Slippage protection can be used to set minimum acceptable amounts
Cannot withdraw the entire pool liquidity (must leave some LP tokens)
Cannot withdraw if the calculated amounts exceed the vault balances
Example Usage
Last updated