forked from solana-developers/program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
73 lines (60 loc) · 2.13 KB
/
test.rs
File metadata and controls
73 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use cross_program_invocatio_native_lever::{PowerStatus, SetPowerStatus};
use litesvm::LiteSVM;
use solana_instruction::{AccountMeta, Instruction};
use solana_keypair::{Keypair, Signer};
use solana_native_token::LAMPORTS_PER_SOL;
use solana_pubkey::Pubkey;
use solana_transaction::Transaction;
#[test]
fn test_cpi() {
let hand_program_id = Pubkey::new_unique();
let lever_program_id = Pubkey::new_unique();
let hand_program_bytes =
include_bytes!("../../../target/deploy/cross_program_invocatio_native_hand.so");
let lever_program_bytes =
include_bytes!("../../../target/deploy/cross_program_invocatio_native_lever.so");
let payer = Keypair::new();
let power_account = Keypair::new();
let mut svm = LiteSVM::new();
svm.add_program(hand_program_id, hand_program_bytes)
.unwrap();
svm.add_program(lever_program_id, lever_program_bytes)
.unwrap();
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
let data = borsh::to_vec(&PowerStatus { is_on: true }).unwrap();
let initiate_lever_ix = Instruction {
program_id: lever_program_id,
accounts: vec![
AccountMeta::new(power_account.pubkey(), true),
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(solana_system_interface::program::ID, false),
],
data,
};
let tx = Transaction::new_signed_with_payer(
&[initiate_lever_ix],
Some(&payer.pubkey()),
&[&payer, &power_account],
svm.latest_blockhash(),
);
let _ = svm.send_transaction(tx).is_ok();
let data = borsh::to_vec(&SetPowerStatus {
name: "Chris".to_string(),
})
.unwrap();
let pull_lever_ix = Instruction {
program_id: hand_program_id,
accounts: vec![
AccountMeta::new(power_account.pubkey(), false),
AccountMeta::new(lever_program_id, false),
],
data,
};
let tx = Transaction::new_signed_with_payer(
&[pull_lever_ix],
Some(&payer.pubkey()),
&[&payer],
svm.latest_blockhash(),
);
let _ = svm.send_transaction(tx).is_ok();
}