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
72    let path = format!("{}/light_system_program_pinocchio.so", light_bin_path);
73    program_test
74        .add_program_from_file(
75            light_sdk::constants::LIGHT_SYSTEM_PROGRAM_ID.into(),
76            path.clone(),
77        )
78        .inspect_err(|_| {
79            println!(
80                "Program light_system_program_pinocchio bin not found in {}",
81                path
82            );
83        })?;
84
85    let registered_program = registered_program_test_account_system_program();
86    program_test
87        .set_account(
88            Pubkey::new_from_array(REGISTERED_PROGRAM_PDA),
89            registered_program,
90        )
91        .map_err(|e| {
92            RpcError::CustomError(format!("Setting registered program account failed {}", e))
93        })?;
94    let registered_program = registered_program_test_account_registry_program();
95    program_test
96        .set_account(
97            get_registered_program_pda(&light_registry::ID),
98            registered_program,
99        )
100        .map_err(|e| {
101            RpcError::CustomError(format!("Setting registered program account failed {}", e))
102        })?;
103    if let Some(programs) = additional_programs {
104        for (name, id) in programs {
105            let path = format!("{}/{}.so", project_root_target_deploy_path, name);
106            program_test
107                .add_program_from_file(id, path.clone())
108                .inspect_err(|_| {
109                    println!("Program {} bin not found in {}", name, path);
110                })?;
111        }
112    }
113    Ok(program_test)
114}