Skip to content

Commit e2e8f99

Browse files
committed
add rust test to pda example
1 parent f3e8e8d commit e2e8f99

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

Cargo.lock

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

basics/program-derived-addresses/native/program/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ solana-system-interface.workspace = true
1313
crate-type = ["cdylib", "lib"]
1414

1515
[features]
16-
anchor-debug = []
1716
custom-heap = []
1817
custom-panic = []
1918

2019
[lints.rust]
2120
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
21+
22+
[dev-dependencies]
23+
litesvm = "0.8.1"
24+
solana-instruction = "3.0.0"
25+
solana-keypair = "3.0.1"
26+
solana-pubkey = "3.0.0"
27+
solana-transaction = "3.0.1"
28+
solana-native-token = "3.0.0"
29+
solana-rent = "3.0.0"
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use borsh::BorshDeserialize;
2+
use litesvm::LiteSVM;
3+
use program_derived_addresses_native_program::state::{IncrementPageVisits, PageVisits};
4+
use solana_instruction::{AccountMeta, Instruction};
5+
use solana_keypair::{Keypair, Signer};
6+
use solana_native_token::LAMPORTS_PER_SOL;
7+
use solana_pubkey::Pubkey;
8+
use solana_rent::Rent;
9+
use solana_system_interface::instruction::create_account;
10+
use solana_transaction::Transaction;
11+
12+
#[test]
13+
fn test_pda() {
14+
let mut svm = LiteSVM::new();
15+
16+
let program_id = Pubkey::new_unique();
17+
let program_bytes =
18+
include_bytes!("../../../../../target/deploy/program_derived_addresses_native_program.so");
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_user = Keypair::new();
25+
26+
let rent = Rent::default();
27+
28+
let create_ix = create_account(
29+
&payer.pubkey(),
30+
&test_user.pubkey(),
31+
solana_rent::Rent::minimum_balance(&rent, 0),
32+
0,
33+
&solana_system_interface::program::ID,
34+
);
35+
36+
let tx = Transaction::new_signed_with_payer(
37+
&[create_ix],
38+
Some(&payer.pubkey()),
39+
&[&payer, &test_user],
40+
svm.latest_blockhash(),
41+
);
42+
43+
let _ = svm.send_transaction(tx).is_ok();
44+
45+
let (pda, bump) =
46+
Pubkey::find_program_address(&[b"page_visits", test_user.pubkey().as_ref()], &program_id);
47+
48+
let data = borsh::to_vec(&PageVisits {
49+
page_visits: 0,
50+
bump,
51+
})
52+
.unwrap();
53+
54+
let ix = Instruction {
55+
program_id,
56+
accounts: vec![
57+
AccountMeta::new(pda, false),
58+
AccountMeta::new(test_user.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+
74+
let data = borsh::to_vec(&IncrementPageVisits {}).unwrap();
75+
76+
let ix = Instruction {
77+
program_id,
78+
accounts: vec![
79+
AccountMeta::new(pda, false),
80+
AccountMeta::new(payer.pubkey(), true),
81+
],
82+
data,
83+
};
84+
85+
let tx = Transaction::new_signed_with_payer(
86+
&[ix],
87+
Some(&payer.pubkey()),
88+
&[&payer],
89+
svm.latest_blockhash(),
90+
);
91+
92+
let _ = svm.send_transaction(tx).is_ok();
93+
94+
// read page visits
95+
let account_info = svm.get_account(&pda).unwrap();
96+
let read_page_visits = PageVisits::try_from_slice(&account_info.data).unwrap();
97+
assert_eq!(read_page_visits.page_visits, 1);
98+
}

0 commit comments

Comments
 (0)