solana_router_raydium/
processor.rs

1//! Raydium router implementation.
2
3use {
4    crate::{
5        add_liquidity::add_liquidity, remove_liquidity::remove_liquidity, stake::stake, swap::swap,
6        unstake::unstake, user_init::user_init,
7    },
8    solana_farm_sdk::{instruction::amm::AmmInstruction, log::sol_log_params_short},
9    solana_program::{
10        account_info::AccountInfo, entrypoint::ProgramResult, log::sol_log_compute_units, msg,
11        pubkey::Pubkey,
12    },
13};
14
15/// Program's entrypoint.
16///
17/// # Arguments
18/// * `program_id` - Public key of the router.
19/// * `accounts` - Accounts, see particular instruction handler for the list.
20/// * `instructions_data` - Packed AmmInstruction.
21pub fn process_instruction(
22    _program_id: &Pubkey,
23    accounts: &[AccountInfo],
24    instruction_data: &[u8],
25) -> ProgramResult {
26    msg!("Raydium router entrypoint");
27    if cfg!(feature = "debug") {
28        sol_log_params_short(accounts, instruction_data);
29    }
30
31    // Read and unpack instruction data
32    let instruction = AmmInstruction::unpack(instruction_data)?;
33
34    match instruction {
35        AmmInstruction::UserInit => user_init(accounts)?,
36        AmmInstruction::AddLiquidity {
37            max_token_a_amount,
38            max_token_b_amount,
39        } => add_liquidity(accounts, max_token_a_amount, max_token_b_amount)?,
40        AmmInstruction::RemoveLiquidity { amount } => remove_liquidity(accounts, amount)?,
41        AmmInstruction::Swap {
42            token_a_amount_in,
43            token_b_amount_in,
44            min_token_amount_out,
45        } => swap(
46            accounts,
47            token_a_amount_in,
48            token_b_amount_in,
49            min_token_amount_out,
50        )?,
51        AmmInstruction::Stake { amount } => stake(accounts, amount, false)?,
52        AmmInstruction::Unstake { amount } => unstake(accounts, amount)?,
53        AmmInstruction::Harvest => stake(accounts, 0, true)?,
54        _ => {}
55    }
56
57    sol_log_compute_units();
58    msg!("Raydium router end of instruction");
59    Ok(())
60}