solana_central/central_context/
load_cpmm_pool_configs.rs

1use crate::central_context::central_context::CentralContext;
2use crate::constants::RAYDIUM_CONSTANTS;
3use crate::protocol_idls::raydium::CpmmPoolConfigIdl;
4use borsh::BorshDeserialize;
5use solana_account_decoder::UiAccountEncoding;
6use solana_client::rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig};
7use solana_client::rpc_filter::RpcFilterType;
8
9impl CentralContext {
10  /// Load Raydium CPMM pool configurations from on-chain data
11  ///
12  /// Fetches all Raydium CPMM pool config accounts and populates the `raydium_cpmm_fee_rates_lp`
13  /// map with fee rates. This should be called during initialization before processing pools.
14  pub fn load_cpmm_pool_configs(&mut self) {
15    // Fetch the Raydium CPMM pool configs and make a hash map of the pool addresses to their config
16    let configs = self
17      .json_rpc_client
18      .get_program_accounts_with_config(
19        &RAYDIUM_CONSTANTS.cpmm_program,
20        RpcProgramAccountsConfig {
21          // The size of the Raydium CPMM pool config account
22          filters: Some(vec![RpcFilterType::DataSize(236)]),
23
24          account_config: RpcAccountInfoConfig {
25            // ask the node to send account.data as base64
26            encoding: Some(UiAccountEncoding::Base64),
27            // you can leave these as default if you don’t need them
28            data_slice: None,
29            commitment: None,
30            min_context_slot: None,
31          },
32          // fill in the rest with defaults
33          ..RpcProgramAccountsConfig::default()
34        },
35      )
36      .unwrap();
37
38    for (pubkey, account) in configs {
39      let decoded_layout: CpmmPoolConfigIdl =
40        CpmmPoolConfigIdl::try_from_slice(&account.data).unwrap();
41
42      self
43        .raydium_cpmm_fee_rates_lp
44        .insert(pubkey, decoded_layout.trade_fee_rate * 1000);
45    }
46    println!(
47      "INIT app_context: Found {} Raydium CPMM pool configs. Loaded into global app context.",
48      self.raydium_cpmm_fee_rates_lp.len()
49    );
50  }
51}