|
| 1 | +use crate::{constants::MINIMUM_LIQUIDITY, errors::AmmError, state::Pool}; |
| 2 | +use borsh::{BorshDeserialize, BorshSerialize}; |
| 3 | +use fixed::types::I64F64; |
| 4 | +use solana_program::{ |
| 5 | + account_info::{next_account_info, AccountInfo}, |
| 6 | + entrypoint::ProgramResult, |
| 7 | + program::{invoke, invoke_signed}, |
| 8 | + program_error::ProgramError, |
| 9 | + program_pack::Pack, |
| 10 | + pubkey::Pubkey, |
| 11 | +}; |
| 12 | +use spl_token::{ |
| 13 | + instruction::{burn, transfer}, |
| 14 | + state::{Account as TokenAccount, Mint}, |
| 15 | +}; |
| 16 | + |
| 17 | +#[derive(BorshDeserialize, BorshSerialize, Debug)] |
| 18 | +pub struct WithdrawLiquidityArgs { |
| 19 | + amount: u64, |
| 20 | +} |
| 21 | + |
| 22 | +pub fn process_withdraw_liquidity( |
| 23 | + program_id: &Pubkey, |
| 24 | + accounts: &[AccountInfo], |
| 25 | + args: WithdrawLiquidityArgs, |
| 26 | +) -> ProgramResult { |
| 27 | + let accounts_iter = &mut accounts.iter(); |
| 28 | + |
| 29 | + let pool = next_account_info(accounts_iter)?; |
| 30 | + let pool_authority = next_account_info(accounts_iter)?; |
| 31 | + let depositor = next_account_info(accounts_iter)?; |
| 32 | + let mint_liquidity = next_account_info(accounts_iter)?; |
| 33 | + let mint_a = next_account_info(accounts_iter)?; |
| 34 | + let mint_b = next_account_info(accounts_iter)?; |
| 35 | + let pool_account_a = next_account_info(accounts_iter)?; |
| 36 | + let pool_account_b = next_account_info(accounts_iter)?; |
| 37 | + let depositor_account_liquidity = next_account_info(accounts_iter)?; |
| 38 | + let depositor_account_a = next_account_info(accounts_iter)?; |
| 39 | + let depositor_account_b = next_account_info(accounts_iter)?; |
| 40 | + let token_program = next_account_info(accounts_iter)?; |
| 41 | + |
| 42 | + // Check that the pool corresponds to the target mints |
| 43 | + let pool_data = Pool::try_from_slice(&pool.data.borrow())?; |
| 44 | + if &pool_data.mint_a != mint_a.key || &pool_data.mint_b != mint_b.key { |
| 45 | + return Err(ProgramError::InvalidAccountData); |
| 46 | + } |
| 47 | + |
| 48 | + // Verify pool_authority PDA |
| 49 | + let pool_authority_seeds = &[ |
| 50 | + Pool::AUTHORITY_PREFIX.as_ref(), |
| 51 | + pool_data.amm.as_ref(), |
| 52 | + mint_a.key.as_ref(), |
| 53 | + mint_b.key.as_ref(), |
| 54 | + ]; |
| 55 | + let (pool_authority_pda, pool_authority_bump) = |
| 56 | + Pubkey::find_program_address(pool_authority_seeds, program_id); |
| 57 | + if pool_authority.key != &pool_authority_pda { |
| 58 | + return Err(AmmError::InvalidAuthority.into()); |
| 59 | + } |
| 60 | + |
| 61 | + // Transfer tokens from the pool |
| 62 | + let pool_token_account_data_a = TokenAccount::unpack(&pool_account_a.data.borrow())?; |
| 63 | + let pool_token_account_data_b = TokenAccount::unpack(&pool_account_b.data.borrow())?; |
| 64 | + let mint_liquidity_data = Mint::unpack(&mint_liquidity.data.borrow())?; |
| 65 | + let amount_a = I64F64::from_num(args.amount) |
| 66 | + .checked_mul(I64F64::from_num(pool_token_account_data_a.amount)) |
| 67 | + .unwrap() |
| 68 | + .checked_div(I64F64::from_num( |
| 69 | + mint_liquidity_data.supply + MINIMUM_LIQUIDITY, |
| 70 | + )) |
| 71 | + .unwrap() |
| 72 | + .floor() |
| 73 | + .to_num::<u64>(); |
| 74 | + |
| 75 | + invoke_signed( |
| 76 | + &transfer( |
| 77 | + token_program.key, |
| 78 | + pool_account_a.key, |
| 79 | + depositor_account_a.key, |
| 80 | + pool_authority.key, |
| 81 | + &[], |
| 82 | + amount_a, |
| 83 | + )?, |
| 84 | + &[ |
| 85 | + pool_account_a.clone(), |
| 86 | + depositor_account_a.clone(), |
| 87 | + pool_authority.clone(), |
| 88 | + token_program.clone(), |
| 89 | + ], |
| 90 | + &[&[ |
| 91 | + Pool::AUTHORITY_PREFIX.as_ref(), |
| 92 | + pool_data.amm.as_ref(), |
| 93 | + mint_a.key.as_ref(), |
| 94 | + mint_b.key.as_ref(), |
| 95 | + &[pool_authority_bump], |
| 96 | + ]], |
| 97 | + )?; |
| 98 | + |
| 99 | + let amount_b = I64F64::from_num(args.amount) |
| 100 | + .checked_mul(I64F64::from_num(pool_token_account_data_b.amount)) |
| 101 | + .unwrap() |
| 102 | + .checked_div(I64F64::from_num( |
| 103 | + mint_liquidity_data.supply + MINIMUM_LIQUIDITY, |
| 104 | + )) |
| 105 | + .unwrap() |
| 106 | + .floor() |
| 107 | + .to_num::<u64>(); |
| 108 | + |
| 109 | + invoke_signed( |
| 110 | + &transfer( |
| 111 | + token_program.key, |
| 112 | + pool_account_b.key, |
| 113 | + depositor_account_b.key, |
| 114 | + pool_authority.key, |
| 115 | + &[], |
| 116 | + amount_b, |
| 117 | + )?, |
| 118 | + &[ |
| 119 | + pool_account_b.clone(), |
| 120 | + depositor_account_b.clone(), |
| 121 | + pool_authority.clone(), |
| 122 | + token_program.clone(), |
| 123 | + ], |
| 124 | + &[&[ |
| 125 | + Pool::AUTHORITY_PREFIX.as_ref(), |
| 126 | + pool_data.amm.as_ref(), |
| 127 | + mint_a.key.as_ref(), |
| 128 | + mint_b.key.as_ref(), |
| 129 | + &[pool_authority_bump], |
| 130 | + ]], |
| 131 | + )?; |
| 132 | + |
| 133 | + // Burn the liquidity tokens |
| 134 | + // It will fail if the amount is invalid |
| 135 | + invoke( |
| 136 | + &burn( |
| 137 | + token_program.key, |
| 138 | + depositor_account_liquidity.key, |
| 139 | + mint_liquidity.key, |
| 140 | + depositor.key, |
| 141 | + &[], |
| 142 | + args.amount, |
| 143 | + )?, |
| 144 | + &[ |
| 145 | + depositor.clone(), |
| 146 | + depositor_account_liquidity.clone(), |
| 147 | + mint_liquidity.clone(), |
| 148 | + token_program.clone(), |
| 149 | + ], |
| 150 | + )?; |
| 151 | + |
| 152 | + Ok(()) |
| 153 | +} |
0 commit comments