-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull_lever.rs
More file actions
46 lines (40 loc) · 1.64 KB
/
Copy pathpull_lever.rs
File metadata and controls
46 lines (40 loc) · 1.64 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
use quasar_lang::prelude::*;
/// Accounts for the hand program's pull_lever instruction.
/// The lever_program uses `Program<LeverProgram>` with a custom marker type
/// that implements `Id` — this lets Quasar verify the program address and
/// the executable flag during account parsing.
#[derive(Accounts)]
pub struct PullLever {
#[account(mut)]
pub power: UncheckedAccount,
pub lever_program: Program<crate::LeverProgram>,
}
#[inline(always)]
pub fn handle_pull_lever(accounts: &PullLever, name: &str) -> Result<(), ProgramError> {
log("Hand is pulling the lever!");
// Build the switch_power instruction data for the lever program.
//
// Wire format: [discriminator = 1] [name: u8 length prefix + bytes].
//
// The lever's switch_power instruction takes `String<50>`, which Quasar
// serialises with a single-byte length prefix (matching every other
// Quasar program: account-data, close-account, rent, realloc,
// repository-layout). An earlier version of this builder used a u32
// length prefix, which sent a malformed payload on every CPI call.
//
// 128 bytes is enough for any reasonable name (max 50 + 1 + 1 = 52).
let mut data = [0u8; 128];
let name_bytes = name.as_bytes();
let data_len = 1 + 1 + name_bytes.len();
data[0] = 1;
data[1] = name_bytes.len() as u8;
let mut i = 0;
while i < name_bytes.len() {
data[2 + i] = name_bytes[i];
i += 1;
}
let mut cpi = DynCpiCall::<1, 128>::new(accounts.lever_program.address());
cpi.push_account(accounts.power.to_account_view(), false, true)?;
cpi.set_data(&data[..data_len])?;
cpi.invoke()
}