Skip to content

Commit 05aaae0

Browse files
authored
feat: add rust test for transfer sol example (#455)
* add rust test for transfer sol example * change program bytes path * move run rust unit tests * update program names in ts tests
1 parent 202a7d5 commit 05aaae0

File tree

9 files changed

+120
-17
lines changed

9 files changed

+120
-17
lines changed

.github/workflows/solana-native.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,15 @@ jobs:
135135
return 1
136136
fi
137137
138-
# Run Rust unit tests
138+
# Test
139+
if ! pnpm build-and-test; then
140+
echo "::error::tests failed for $project"
141+
echo "$project: tests failed with $solana_version" >> $GITHUB_WORKSPACE/failed_projects.txt
142+
cd - > /dev/null
143+
return 1
144+
fi
145+
146+
# Run Rust unit tests
139147
if [ -d "program" ]; then
140148
echo "Running Rust unit tests for $project"
141149
if ! cargo test --manifest-path=./program/Cargo.toml; then
@@ -146,14 +154,6 @@ jobs:
146154
fi
147155
fi
148156
149-
# Test
150-
if ! pnpm build-and-test; then
151-
echo "::error::tests failed for $project"
152-
echo "$project: tests failed with $solana_version" >> $GITHUB_WORKSPACE/failed_projects.txt
153-
cd - > /dev/null
154-
return 1
155-
fi
156-
157157
echo "Build and tests succeeded for $project with $solana_version version."
158158
cd - > /dev/null
159159
return 0

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/account-data/native/program/tests/tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn test_account_data() {
1919

2020
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
2121

22-
let program_bytes =
23-
include_bytes!("../../../../../target/deploy/account_data_native_program.so");
22+
let program_bytes = include_bytes!("../../tests/fixtures/account_data_native_program.so");
2423

2524
svm.add_program(program_id, program_bytes).unwrap();
2625

basics/checking-accounts/native/program/tests/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ fn test_checking_accounts() {
1616
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
1717

1818
let program_id = Pubkey::new_unique();
19-
let program_bytes =
20-
include_bytes!("../../../../../target/deploy/checking_accounts_native_program.so");
19+
let program_bytes = include_bytes!("../../tests/fixtures/checking_accounts_native_program.so");
2120

2221
svm.add_program(program_id, program_bytes).unwrap();
2322

basics/checking-accounts/native/tests/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { start } from 'solana-bankrun';
44

55
describe('Checking accounts', async () => {
66
const PROGRAM_ID = PublicKey.unique();
7-
const context = await start([{ name: 'checking_accounts_program', programId: PROGRAM_ID }], []);
7+
const context = await start([{ name: 'checking_accounts_native_program', programId: PROGRAM_ID }], []);
88
const client = context.banksClient;
99
const payer = context.payer;
1010
const rent = await client.getRent();

basics/program-derived-addresses/native/tests/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { start } from 'solana-bankrun';
66

77
describe('PDAs', async () => {
88
const PROGRAM_ID = PublicKey.unique();
9-
const context = await start([{ name: 'program_derived_addresses_program', programId: PROGRAM_ID }], []);
9+
const context = await start([{ name: 'program_derived_addresses_native_program', programId: PROGRAM_ID }], []);
1010
const client = context.banksClient;
1111
const payer = context.payer;
1212
const rent = await client.getRent();

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!("../../tests/fixtures/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)