Skip to main content

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 from a file path.
9///
10/// Reads a compiled program binary (.so file) from disk and deploys it.
11/// For deploying from in-memory bytes (e.g. from `include_bytes!`), use
12/// [`deploy_program_bytes`] instead.
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/// # Errors
21///
22/// Returns an error if the file cannot be read or the deployment fails.
23///
24/// # Example
25///
26/// ```rust,no_run
27/// use solana_kite::deploy_program;
28/// use litesvm::LiteSVM;
29/// use solana_pubkey::Pubkey;
30///
31/// let mut litesvm = LiteSVM::new();
32/// let program_id = Pubkey::new_unique();
33/// deploy_program(&mut litesvm, &program_id, "./target/deploy/my_program.so").unwrap();
34/// ```
35pub fn deploy_program(
36    litesvm: &mut LiteSVM,
37    program_id: &Pubkey,
38    program_path: &str,
39) -> Result<(), SolanaKiteError> {
40    let program_bytes = fs::read(program_path).map_err(|e| {
41        SolanaKiteError::ProgramDeploymentFailed(format!(
42            "Failed to read program binary at {}: {}",
43            program_path, e
44        ))
45    })?;
46
47    deploy_program_bytes(litesvm, program_id, &program_bytes)
48}
49
50/// Deploys a program to the LiteSVM test environment from raw bytes.
51///
52/// This is useful when you have the program binary embedded via `include_bytes!`
53/// or loaded from a non-filesystem source.
54///
55/// # Arguments
56///
57/// * `litesvm` - Mutable reference to the LiteSVM instance
58/// * `program_id` - The public key where the program should be deployed
59/// * `program_bytes` - The compiled program binary as a byte slice
60///
61/// # Errors
62///
63/// Returns an error if the deployment fails.
64///
65/// # Example
66///
67/// ```rust,no_run
68/// use solana_kite::deploy_program_bytes;
69/// use litesvm::LiteSVM;
70/// use solana_pubkey::Pubkey;
71///
72/// let mut litesvm = LiteSVM::new();
73/// let program_id = Pubkey::new_unique();
74/// # let program_bytes: &[u8] = &[];
75/// deploy_program_bytes(&mut litesvm, &program_id, program_bytes).unwrap();
76/// ```
77pub fn deploy_program_bytes(
78    litesvm: &mut LiteSVM,
79    program_id: &Pubkey,
80    program_bytes: &[u8],
81) -> Result<(), SolanaKiteError> {
82    litesvm
83        .set_account(
84            *program_id,
85            solana_account::Account {
86                lamports: litesvm.minimum_balance_for_rent_exemption(program_bytes.len()),
87                data: program_bytes.to_vec(),
88                owner: solana_program::bpf_loader::ID,
89                executable: true,
90                rent_epoch: 0,
91            },
92        )
93        .map_err(|e| {
94            SolanaKiteError::ProgramDeploymentFailed(format!("Failed to deploy program: {}", e))
95        })?;
96
97    Ok(())
98}