light_program_test/utils/
setup_light_programs.rs

1use light_client::rpc::RpcError;
2use light_compressed_account::constants::REGISTERED_PROGRAM_PDA;
3use light_registry::account_compression_cpi::sdk::get_registered_program_pda;
4use litesvm::LiteSVM;
5use solana_compute_budget::compute_budget::ComputeBudget;
6use solana_pubkey::Pubkey;
7
8use crate::{
9    accounts::{
10        registered_program_accounts::{
11            registered_program_test_account_registry_program,
12            registered_program_test_account_system_program,
13        },
14        test_accounts::NOOP_PROGRAM_ID,
15    },
16    utils::find_light_bin::find_light_bin,
17};
18
19/// Creates ProgramTestContext with light protocol and additional programs.
20///
21/// Programs:
22/// 1. light_registry program
23/// 2. account_compression program
24/// 3. light_compressed_token program
25/// 4. light_system_program program
26pub fn setup_light_programs(
27    additional_programs: Option<Vec<(&'static str, Pubkey)>>,
28) -> Result<LiteSVM, RpcError> {
29    let program_test = LiteSVM::new();
30    let program_test = program_test.with_compute_budget(ComputeBudget {
31        compute_unit_limit: 1_400_000,
32        ..Default::default()
33    });
34    let mut program_test = program_test.with_transaction_history(0);
35    let project_root_target_deploy_path = std::env::var("SBF_OUT_DIR")
36        .map_err(|_| RpcError::CustomError("SBF_OUT_DIR not set.".to_string()))?;
37    // find path to bin where light cli stores program binaries.
38    let light_bin_path = find_light_bin().ok_or(RpcError::CustomError(
39        "Failed to find light binary path. To use light-program-test zk compression cli needs to be installed and light system programs need to be downloaded. Light system programs are downloaded the first time light test-validator is run.".to_string(),
40    ))?;
41    let light_bin_path = light_bin_path
42        .to_str()
43        .ok_or(RpcError::CustomError(format!(
44            "Found invalid light binary path {:?}",
45            light_bin_path
46        )))?;
47    let path = format!("{}/light_registry.so", light_bin_path);
48    program_test
49        .add_program_from_file(light_registry::ID, path.clone())
50        .inspect_err(|_| {
51            println!("Program light_registry bin not found in {}", path);
52        })?;
53    let path = format!("{}/account_compression.so", light_bin_path);
54    program_test
55        .add_program_from_file(account_compression::ID, path.clone())
56        .inspect_err(|_| {
57            println!("Program account_compression bin not found in {}", path);
58        })?;
59    let path = format!("{}/light_compressed_token.so", light_bin_path);
60    program_test
61        .add_program_from_file(light_compressed_token::ID, path.clone())
62        .inspect_err(|_| {
63            println!("Program light_compressed_token bin not found in {}", path);
64        })?;
65    let path = format!("{}/spl_noop.so", light_bin_path);
66    program_test
67        .add_program_from_file(NOOP_PROGRAM_ID, path.clone())
68        .inspect_err(|_| {
69            println!("Program spl_noop bin not found in {}", path);
70        })?;
71    #[cfg(feature = "devenv")]
72    {
73        let path = format!("{}/light_system_program_pinocchio.so", light_bin_path);
74        program_test
75            .add_program_from_file(
76                light_sdk::constants::LIGHT_SYSTEM_PROGRAM_ID.into(),
77                path.clone(),
78            )
79            .inspect_err(|_| {
80                println!(
81                    "Program light_system_program_pinocchio bin not found in {}",
82                    path
83                );
84            })?;
85    }
86
87    #[cfg(not(feature = "devenv"))]
88    {
89        let path = format!("{}/light_system_program.so", light_bin_path);
90        program_test.add_program_from_file(
91            Pubkey::from(light_sdk::constants::LIGHT_SYSTEM_PROGRAM_ID),
92            path,
93        )?;
94    }
95
96    let registered_program = registered_program_test_account_system_program();
97    program_test
98        .set_account(
99            Pubkey::new_from_array(REGISTERED_PROGRAM_PDA),
100            registered_program,
101        )
102        .map_err(|e| {
103            RpcError::CustomError(format!("Setting registered program account failed {}", e))
104        })?;
105    let registered_program = registered_program_test_account_registry_program();
106    program_test
107        .set_account(
108            get_registered_program_pda(&light_registry::ID),
109            registered_program,
110        )
111        .map_err(|e| {
112            RpcError::CustomError(format!("Setting registered program account failed {}", e))
113        })?;
114    if let Some(programs) = additional_programs {
115        for (name, id) in programs {
116            let path = format!("{}/{}.so", project_root_target_deploy_path, name);
117            program_test
118                .add_program_from_file(id, path.clone())
119                .inspect_err(|_| {
120                    println!("Program {} bin not found in {}", name, path);
121                })?;
122        }
123    }
124    Ok(program_test)
125}