solana_exec/utilities/
make_swap_instructions.rs

1use crate::pumpfun::push_pumpfun_swap_instruction::push_pumpfun_swap_instruction;
2use crate::pumpswap::push_pumpswap_swap_instruction::push_pumpswap_swap_instruction;
3use crate::raydium::push_raydium_ammv4_swap_instruction::push_raydium_ammv4_swap_instruction;
4use crate::raydium::push_raydium_cpmm_swap_instruction::push_raydium_cpmm_swap_instruction;
5use crate::raydium::push_raydium_launchpad_swap_instruction::push_raydium_launchpad_swap_instruction;
6use crate::types::swap_config::SwapConfig;
7use crate::utilities::close_token_account_check::close_token_account_check;
8use crate::utilities::open_token_account_check::open_token_account_check;
9use solana_central::CentralContext;
10use solana_central::PfBondingCurve;
11use solana_central::PumpswapPool;
12use solana_central::RaydiumAmmV4Pool;
13use solana_central::RaydiumCpmmPool;
14use solana_central::RaydiumLaunchpad;
15use solana_sdk::instruction::Instruction;
16use std::sync::Arc;
17
18/// Create swap instructions from a list of swap configs. Processes swap configs in order and
19/// creates protocol-specific swap instructions. Also handles opening token accounts before swaps
20/// and closing them after. Updates the compute unit budget based on operations performed.
21pub fn make_swap_instructions(
22  swap_configs: &Vec<SwapConfig>,
23  central_context: &Arc<CentralContext>,
24  compute_unit_budget: &mut u32,
25) -> Vec<Instruction> {
26  let mut instructions: Vec<Instruction> = Vec::new();
27  open_token_account_check(swap_configs, &mut instructions, compute_unit_budget);
28  for config in swap_configs {
29    let pool = config.pool.read().unwrap();
30
31    if let Some(pool) = pool.as_any().downcast_ref::<PumpswapPool>() {
32      push_pumpswap_swap_instruction(
33        config,
34        false,
35        0,
36        0,
37        &mut instructions,
38        compute_unit_budget,
39        pool,
40      );
41    } else if let Some(pool) = pool.as_any().downcast_ref::<PfBondingCurve>() {
42      push_pumpfun_swap_instruction(
43        config,
44        central_context,
45        &mut instructions,
46        compute_unit_budget,
47        pool,
48      );
49    } else if let Some(pool) = pool.as_any().downcast_ref::<RaydiumAmmV4Pool>() {
50      push_raydium_ammv4_swap_instruction(
51        config,
52        false,
53        0,
54        &mut instructions,
55        compute_unit_budget,
56        pool,
57      );
58    } else if let Some(pool) = pool.as_any().downcast_ref::<RaydiumCpmmPool>() {
59      push_raydium_cpmm_swap_instruction(
60        config,
61        false,
62        0,
63        &mut instructions,
64        compute_unit_budget,
65        pool,
66      )
67    } else if let Some(pool) = pool.as_any().downcast_ref::<RaydiumLaunchpad>() {
68      push_raydium_launchpad_swap_instruction(config, &mut instructions, compute_unit_budget, pool)
69    }
70  }
71  close_token_account_check(swap_configs, &mut instructions, compute_unit_budget);
72  instructions
73}