solana_kite/
program.rs

1//! Program deployment utilities for Solana programs.
2
3use crate::error::SolanaKiteError;
4use litesvm::LiteSVM;
5use solana_pubkey::Pubkey;
6use std::fs;
7
8/// Deploys a program to the LiteSVM test environment.
9///
10/// This function reads a program binary from the filesystem and deploys it to the
11/// specified program ID in the LiteSVM instance. The program will be marked as executable
12/// and owned by the BPF loader.
13///
14/// # Arguments
15///
16/// * `litesvm` - Mutable reference to the LiteSVM instance
17/// * `program_id` - The public key where the program should be deployed
18/// * `program_path` - Path to the compiled program binary (.so file)
19///
20/// # Returns
21///
22/// Returns `Ok(())` on successful deployment, or a [`SolanaKiteError`] on failure.
23///
24/// # Errors
25///
26/// This function will return an error if:
27/// - The program binary file cannot be read
28/// - The program deployment to LiteSVM fails
29///
30/// # Example
31///
32/// ```rust
33/// use solana_kite::deploy_program;
34/// use litesvm::LiteSVM;
35/// use solana_pubkey::Pubkey;
36///
37/// let mut litesvm = LiteSVM::new();
38/// let program_id = Pubkey::new_unique();
39/// 
40/// // Deploy a program (this would fail in tests without an actual .so file)
41/// // deploy_program(&mut litesvm, &program_id, "./target/deploy/my_program.so")?;
42/// ```
43pub fn deploy_program(
44    litesvm: &mut LiteSVM,
45    program_id: &Pubkey,
46    program_path: &str,
47) -> Result<(), SolanaKiteError> {
48    let program_bytes = fs::read(program_path)
49        .map_err(|e| SolanaKiteError::ProgramDeploymentFailed(format!("Failed to read program binary at {}: {}", program_path, e)))?;
50    
51    litesvm
52        .set_account(
53            *program_id,
54            solana_account::Account {
55                lamports: litesvm.minimum_balance_for_rent_exemption(program_bytes.len()),
56                data: program_bytes,
57                owner: solana_program::bpf_loader::ID,
58                executable: true,
59                rent_epoch: 0,
60            },
61        )
62        .map_err(|e| SolanaKiteError::ProgramDeploymentFailed(format!("Failed to deploy program: {:?}", e)))?;
63    
64    Ok(())
65}