Skip to content

Commit 7f00fbe

Browse files
committed
add rust test for cpi example
1 parent f3e8e8d commit 7f00fbe

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

basics/cross-program-invocation/native/programs/hand/Cargo.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ edition = "2021"
66
[features]
77
no-entrypoint = []
88
cpi = ["no-entrypoint"]
9+
custom-heap = []
10+
custom-panic = []
911

1012
[dependencies]
1113
borsh = "1.5.7"
1214
borsh-derive = "1.5.7"
1315
solana-program = "3.0"
1416
cross-program-invocatio-native-lever = { path = "../lever", features = ["cpi"] }
1517

16-
1718
[lib]
1819
crate-type = ["cdylib", "lib"]
20+
21+
[dev-dependencies]
22+
litesvm = "0.8.1"
23+
solana-instruction = "3.0.0"
24+
solana-keypair = "3.0.1"
25+
solana-native-token = "3.0.0"
26+
solana-pubkey = "3.0.0"
27+
solana-transaction = "3.0.1"
28+
solana-system-interface = {version = "2.0.0", features = ["bincode"]}
29+
30+
[lints.rust]
31+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use cross_program_invocatio_native_lever::{PowerStatus, SetPowerStatus};
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+
#[test]
10+
fn test_cpi() {
11+
let hand_program_id = Pubkey::new_unique();
12+
let lever_program_id = Pubkey::new_unique();
13+
let hand_program_bytes =
14+
include_bytes!("../../../target/deploy/cross_program_invocatio_native_hand.so");
15+
let lever_program_bytes =
16+
include_bytes!("../../../target/deploy/cross_program_invocatio_native_lever.so");
17+
18+
let payer = Keypair::new();
19+
let power_account = Keypair::new();
20+
21+
let mut svm = LiteSVM::new();
22+
23+
svm.add_program(hand_program_id, hand_program_bytes)
24+
.unwrap();
25+
svm.add_program(lever_program_id, lever_program_bytes)
26+
.unwrap();
27+
28+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
29+
30+
let data = borsh::to_vec(&PowerStatus { is_on: true }).unwrap();
31+
32+
let initiate_lever_ix = Instruction {
33+
program_id: lever_program_id,
34+
accounts: vec![
35+
AccountMeta::new(power_account.pubkey(), true),
36+
AccountMeta::new(payer.pubkey(), true),
37+
AccountMeta::new(solana_system_interface::program::ID, false),
38+
],
39+
data,
40+
};
41+
42+
let tx = Transaction::new_signed_with_payer(
43+
&[initiate_lever_ix],
44+
Some(&payer.pubkey()),
45+
&[&payer, &power_account],
46+
svm.latest_blockhash(),
47+
);
48+
49+
let _ = svm.send_transaction(tx).is_ok();
50+
51+
let data = borsh::to_vec(&SetPowerStatus {
52+
name: "Chris".to_string(),
53+
})
54+
.unwrap();
55+
56+
let pull_lever_ix = Instruction {
57+
program_id: hand_program_id,
58+
accounts: vec![
59+
AccountMeta::new(power_account.pubkey(), false),
60+
AccountMeta::new(lever_program_id, false),
61+
],
62+
data,
63+
};
64+
65+
let tx = Transaction::new_signed_with_payer(
66+
&[pull_lever_ix],
67+
Some(&payer.pubkey()),
68+
&[&payer],
69+
svm.latest_blockhash(),
70+
);
71+
72+
let _ = svm.send_transaction(tx).is_ok();
73+
}

basics/cross-program-invocation/native/programs/lever/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edition = "2021"
66
[features]
77
no-entrypoint = []
88
cpi = ["no-entrypoint"]
9+
custom-heap = []
10+
custom-panic = []
911

1012
[dependencies]
1113
borsh = "1.5.7"
@@ -15,3 +17,6 @@ solana-system-interface = {version = "2.0.0", features = ["bincode"]}
1517

1618
[lib]
1719
crate-type = ["cdylib", "lib"]
20+
21+
[lints.rust]
22+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }

0 commit comments

Comments
 (0)