Triển Khai Chương Trình

Kiểm Tra Hoàn Chỉnh

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());
}

Những Điểm Chính

  1. Triển Khai Chương Trình: Sử dụng add_program() để triển khai một chương trình từ các byte
  2. include_bytes!: Nhúng chương trình đã biên dịch tại thời điểm biên dịch
  3. Program ID: Sử dụng keypair từ target/deploy/
  4. Instructions: Tạo các instruction tùy chỉnh với program ID, các tài khoản, và dữ liệu
  5. Xác Minh Log: Kiểm tra log giao dịch để xác minh việc thực thi chương trình

Để biết thêm thông tin triển khai chi tiết, hãy đọc phần này.

Is this page helpful?

Mục lục

Chỉnh sửa trang