Skip to content

Commit 9952124

Browse files
committed
add rust litesvm test for close account example
1 parent c80856b commit 9952124

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 =
17+
include_bytes!("../../../../../target/deploy/close_account_native_program.so");
18+
19+
svm.add_program(program_id, program_bytes).unwrap();
20+
21+
let payer = Keypair::new();
22+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
23+
24+
let test_account_pubkey =
25+
Pubkey::find_program_address(&[b"USER".as_ref(), &payer.pubkey().as_ref()], &program_id).0;
26+
27+
// create user ix
28+
let data = borsh::to_vec(&MyInstruction::CreateUser(User {
29+
name: "Jacob".to_string(),
30+
}))
31+
.unwrap();
32+
33+
let ix = Instruction {
34+
program_id,
35+
accounts: vec![
36+
AccountMeta::new(test_account_pubkey, false),
37+
AccountMeta::new(payer.pubkey(), true),
38+
AccountMeta::new(solana_system_interface::program::ID, false),
39+
],
40+
data,
41+
};
42+
43+
let tx = Transaction::new_signed_with_payer(
44+
&[ix],
45+
Some(&payer.pubkey()),
46+
&[&payer],
47+
svm.latest_blockhash(),
48+
);
49+
50+
let _ = svm.send_transaction(tx).is_ok();
51+
52+
// clsose user ix
53+
let data = borsh::to_vec(&MyInstruction::CloseUser).unwrap();
54+
55+
let ix = Instruction {
56+
program_id,
57+
accounts: vec![
58+
AccountMeta::new(test_account_pubkey, false),
59+
AccountMeta::new(payer.pubkey(), true),
60+
AccountMeta::new(solana_system_interface::program::ID, false),
61+
],
62+
data,
63+
};
64+
65+
let tx = Transaction::new_signed_with_payer(
66+
&[ix],
67+
Some(&payer.pubkey()),
68+
&[&payer],
69+
svm.latest_blockhash(),
70+
);
71+
72+
let _ = svm.send_transaction(tx).is_ok();
73+
}

0 commit comments

Comments
 (0)