Skip to content

Commit 1b4653a

Browse files
committed
add rust test for transfer sol example
1 parent f3e8e8d commit 1b4653a

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-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.

basics/repository-layout/native/program/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ solana-program = "2.0"
1212
crate-type = ["cdylib", "lib"]
1313

1414
[features]
15-
anchor-debug = []
1615
custom-heap = []
1716
custom-panic = []
1817

basics/transfer-sol/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 = "0.8.1"
24+
solana-instruction = "3.0.0"
25+
solana-keypair = "3.0.1"
26+
solana-native-token = "3.0.0"
27+
solana-pubkey = "3.0.0"
28+
solana-transaction = "3.0.1"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use litesvm::LiteSVM;
2+
use solana_instruction::{AccountMeta, Instruction};
3+
use solana_keypair::{Keypair, Signer};
4+
use solana_program::native_token::LAMPORTS_PER_SOL;
5+
use solana_pubkey::Pubkey;
6+
use solana_system_interface::instruction::create_account;
7+
use solana_transaction::Transaction;
8+
use transfer_sol_program::processor::TransferInstruction;
9+
10+
#[test]
11+
fn test_transfer_sol() {
12+
let mut svm = LiteSVM::new();
13+
14+
let program_id = Pubkey::new_unique();
15+
let program_bytes = include_bytes!("../../../../../target/deploy/transfer_sol_program.so");
16+
17+
svm.add_program(program_id, program_bytes).unwrap();
18+
19+
let payer = Keypair::new();
20+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
21+
22+
let test_recipient1 = Keypair::new();
23+
let test_recipient2 = Keypair::new();
24+
let test_recipient3 = Keypair::new();
25+
26+
let payer_balance_before = svm.get_balance(&payer.pubkey()).unwrap();
27+
let recipient_balance_before = svm.get_balance(&test_recipient1.pubkey()).unwrap_or(0);
28+
29+
let data = borsh::to_vec(&TransferInstruction::CpiTransfer(LAMPORTS_PER_SOL)).unwrap();
30+
31+
let ix = Instruction {
32+
program_id,
33+
accounts: vec![
34+
AccountMeta::new(payer.pubkey(), true),
35+
AccountMeta::new(test_recipient1.pubkey(), false),
36+
AccountMeta::new(solana_system_interface::program::ID, false),
37+
],
38+
data,
39+
};
40+
41+
let tx = Transaction::new_signed_with_payer(
42+
&[ix],
43+
Some(&payer.pubkey()),
44+
&[&payer],
45+
svm.latest_blockhash(),
46+
);
47+
48+
let _ = svm.send_transaction(tx).is_ok();
49+
50+
let payer_balance_after = svm.get_balance(&payer.pubkey()).unwrap();
51+
let recipient_balance_after = svm.get_balance(&test_recipient1.pubkey()).unwrap_or(0);
52+
53+
assert!(payer_balance_before > payer_balance_after);
54+
assert!(recipient_balance_before < recipient_balance_after);
55+
56+
let create_ix = create_account(
57+
&payer.pubkey(),
58+
&test_recipient2.pubkey(),
59+
2 * LAMPORTS_PER_SOL,
60+
0,
61+
&program_id,
62+
);
63+
64+
let tx = Transaction::new_signed_with_payer(
65+
&[create_ix],
66+
Some(&payer.pubkey()),
67+
&[&payer, &test_recipient2],
68+
svm.latest_blockhash(),
69+
);
70+
71+
let _ = svm.send_transaction(tx).is_ok();
72+
73+
let data = borsh::to_vec(&TransferInstruction::ProgramTransfer(LAMPORTS_PER_SOL)).unwrap();
74+
75+
let ix = Instruction {
76+
program_id,
77+
accounts: vec![
78+
AccountMeta::new(test_recipient2.pubkey(), true),
79+
AccountMeta::new(test_recipient3.pubkey(), false),
80+
AccountMeta::new(solana_system_interface::program::ID, false),
81+
],
82+
data,
83+
};
84+
85+
let tx = Transaction::new_signed_with_payer(
86+
&[ix],
87+
Some(&payer.pubkey()),
88+
&[&payer, &test_recipient2],
89+
svm.latest_blockhash(),
90+
);
91+
92+
let _ = svm.send_transaction(tx).is_ok();
93+
}

0 commit comments

Comments
 (0)