solana_exec/utilities/
open_token_account_check.rs

1use crate::constants::COMPUTE_UNITS;
2use crate::types::swap_config::SwapConfig;
3use solana_central::SwapDirection;
4use solana_central::constants::SOLANA_PROGRAMS;
5use solana_sdk::instruction::Instruction;
6use solana_sdk::pubkey::Pubkey;
7use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
8
9/// Add instructions to open token accounts for swap outputs if needed. Iterates through swap
10/// configs and adds create-associated-token-account idempotent (not failing if already open)
11/// instructions for any swaps where `open_token_account` is true. Updates the compute unit budget
12/// accordingly.
13pub fn open_token_account_check(
14  swap_configs: &Vec<SwapConfig>,
15  instructions: &mut Vec<Instruction>,
16  compute_unit_budget: &mut u32,
17) {
18  for swap_config in swap_configs {
19    let pool = swap_config.pool.read().unwrap();
20    if swap_config.open_token_account {
21      let mut token_address: &Pubkey = pool.token_b_address();
22      if swap_config.direction == SwapDirection::BToA {
23        token_address = pool.token_a_address();
24      }
25      instructions.push(create_associated_token_account_idempotent(
26        &swap_config.wallet,
27        &swap_config.wallet,
28        token_address,
29        &SOLANA_PROGRAMS.token_program,
30      ));
31      *compute_unit_budget += COMPUTE_UNITS.open_token_account;
32    }
33  }
34}