Skip to content

Commit 30854ee

Browse files
committed
add rust test for pda rent payer
1 parent f3e8e8d commit 30854ee

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
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.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ spl-token-2022-interface = "2.0.0"
6767
# pinocchio
6868
pinocchio = "=0.8.1"
6969
pinocchio-log = "0.4.0"
70+
71+
# testing
72+
litesvm = "0.8.1"
73+
solana-instruction = "3.0.0"
74+
solana-keypair = "3.0.1"
75+
solana-pubkey = "3.0.0"
76+
solana-transaction = "3.0.1"
77+
solana-native-token = "3.0.0"

basics/pda-rent-payer/native/program/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ 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.workspace = true
24+
solana-instruction.workspace = true
25+
solana-keypair.workspace = true
26+
solana-pubkey.workspace = true
27+
solana-transaction.workspace = true
28+
solana-native-token.workspace = true

basics/pda-rent-payer/native/program/src/instructions/init_rent_vault.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::state::RentVault;
1212

1313
#[derive(BorshDeserialize, BorshSerialize)]
1414
pub struct InitRentVaultArgs {
15-
fund_lamports: u64,
15+
pub fund_lamports: u64,
1616
}
1717

1818
pub fn init_rent_vault(
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use litesvm::LiteSVM;
2+
use pda_rent_payer_program::instructions::InitRentVaultArgs;
3+
use pda_rent_payer_program::processor::MyInstruction;
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_transaction::Transaction;
9+
10+
#[test]
11+
fn test_pda_rent_payer() {
12+
let program_id = Pubkey::new_unique();
13+
let program_bytes = include_bytes!("../../../../../target/deploy/pda_rent_payer_program.so");
14+
15+
let mut svm = LiteSVM::new();
16+
svm.add_program(program_id, program_bytes).unwrap();
17+
18+
let payer = Keypair::new();
19+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
20+
21+
let rent_value_pda = Pubkey::find_program_address(&[b"rent_vault"], &program_id).0;
22+
23+
let args = InitRentVaultArgs {
24+
fund_lamports: 1000000000,
25+
};
26+
27+
let data = borsh::to_vec(&MyInstruction::InitRentVault(args)).unwrap();
28+
29+
let ix = Instruction {
30+
program_id,
31+
accounts: vec![
32+
AccountMeta::new(rent_value_pda, false),
33+
AccountMeta::new(payer.pubkey(), true),
34+
AccountMeta::new(solana_system_interface::program::ID, false),
35+
],
36+
data,
37+
};
38+
39+
let tx = Transaction::new_signed_with_payer(
40+
&[ix],
41+
Some(&payer.pubkey()),
42+
&[&payer],
43+
svm.latest_blockhash(),
44+
);
45+
46+
let _ = svm.send_transaction(tx).is_ok();
47+
48+
let new_account = Keypair::new();
49+
50+
let data = borsh::to_vec(&MyInstruction::CreateNewAccount).unwrap();
51+
52+
let ix = Instruction {
53+
program_id,
54+
accounts: vec![
55+
AccountMeta::new(new_account.pubkey(), true),
56+
AccountMeta::new(rent_value_pda, false),
57+
AccountMeta::new(solana_system_interface::program::ID, false),
58+
],
59+
data,
60+
};
61+
62+
let tx = Transaction::new_signed_with_payer(
63+
&[ix],
64+
Some(&payer.pubkey()),
65+
&[&payer, &new_account],
66+
svm.latest_blockhash(),
67+
);
68+
69+
let _ = svm.send_transaction(tx).is_ok();
70+
}

0 commit comments

Comments
 (0)