程序部署

完整测试

tests/program_test.rs
use litesvm::LiteSVM;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
};
#[test]
fn test_program_deployment() {
let mut svm = LiteSVM::new();
// Deploy the program
let program_id = Pubkey::new_unique();
let program_bytes = include_bytes!("../target/deploy/hello_world.so");
svm.add_program(program_id, program_bytes).unwrap();
// Create payer account
let payer = Keypair::new();
svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();
// Create instruction to call program
let instruction = Instruction {
program_id,
accounts: vec![
AccountMeta::new(payer.pubkey(), true),
],
data: vec![], // Program-specific data
};
// Send transaction
let tx = Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
&[&payer],
svm.latest_blockhash(),
);
let result = svm.send_transaction(tx).unwrap();
// Verify program was called
assert!(result.logs.iter().any(|log|
log.contains(&format!("Program {} invoke", program_id))
));
println!("Program called successfully!");
println!("Logs:\n{}", result.pretty_logs());
}

关键要点

  1. 程序部署:使用 add_program() 从字节部署程序
  2. include_bytes!:在编译时嵌入已编译的程序
  3. 程序 ID:使用来自 target/deploy/ 的 keypair
  4. 指令:使用程序 ID、账户和数据创建自定义指令
  5. 日志验证:检查交易日志以验证程序执行情况

有关更详细的部署信息,请阅读 本节

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有