Skip to content

Commit b697acb

Browse files
authored
feat: add rust litesvm test for close account example (#437)
* add rust litesvm test for close account example * Update test.rs
1 parent bf961af commit b697acb

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/close-account/native/program/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ custom-panic = []
1919

2020
[lints.rust]
2121
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
22+
23+
[dev-dependencies]
24+
litesvm = "0.8.1"
25+
solana-instruction = "3.0.0"
26+
solana-keypair = "3.0.1"
27+
solana-native-token = "3.0.0"
28+
solana-pubkey = "3.0.0"
29+
solana-transaction = "3.0.1"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use close_account_native_program::state::user::User;
2+
use litesvm::LiteSVM;
3+
use solana_instruction::{AccountMeta, Instruction};
4+
use solana_keypair::{Keypair, Signer};
5+
use solana_native_token::LAMPORTS_PER_SOL;
6+
use solana_pubkey::Pubkey;
7+
use solana_transaction::Transaction;
8+
9+
use close_account_native_program::processor::MyInstruction;
10+
11+
#[test]
12+
fn test_close_account() {
13+
let mut svm = LiteSVM::new();
14+
15+
let program_id = Pubkey::new_unique();
16+
let program_bytes = include_bytes!("../../tests/fixtures/close_account_native_program.so");
17+
18+
svm.add_program(program_id, program_bytes).unwrap();
19+
20+
let payer = Keypair::new();
21+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
22+
23+
let test_account_pubkey =
24+
Pubkey::find_program_address(&[b"USER".as_ref(), &payer.pubkey().as_ref()], &program_id).0;
25+
26+
// create user ix
27+
let data = borsh::to_vec(&MyInstruction::CreateUser(User {
28+
name: "Jacob".to_string(),
29+
}))
30+
.unwrap();
31+
32+
let ix = Instruction {
33+
program_id,
34+
accounts: vec![
35+
AccountMeta::new(test_account_pubkey, false),
36+
AccountMeta::new(payer.pubkey(), true),
37+
AccountMeta::new(solana_system_interface::program::ID, false),
38+
],
39+
data,
40+
};
41+
42+
let tx = Transaction::new_signed_with_payer(
43+
&[ix],
44+
Some(&payer.pubkey()),
45+
&[&payer],
46+
svm.latest_blockhash(),
47+
);
48+
49+
let _ = svm.send_transaction(tx).is_ok();
50+
51+
// clsose user ix
52+
let data = borsh::to_vec(&MyInstruction::CloseUser).unwrap();
53+
54+
let ix = Instruction {
55+
program_id,
56+
accounts: vec![
57+
AccountMeta::new(test_account_pubkey, false),
58+
AccountMeta::new(payer.pubkey(), true),
59+
AccountMeta::new(solana_system_interface::program::ID, false),
60+
],
61+
data,
62+
};
63+
64+
let tx = Transaction::new_signed_with_payer(
65+
&[ix],
66+
Some(&payer.pubkey()),
67+
&[&payer],
68+
svm.latest_blockhash(),
69+
);
70+
71+
let _ = svm.send_transaction(tx).is_ok();
72+
}

0 commit comments

Comments
 (0)