-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathallocate.rs
47 lines (40 loc) · 1.26 KB
/
allocate.rs
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
use {
pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
ProgramResult,
},
pinocchio_cpi::invoke_signed,
};
/// Allocate space in a (possibly new) account without funding.
///
/// ### Accounts:
/// 0. `[WRITE, SIGNER]` New account
pub struct Allocate<'a> {
/// Account to be assigned.
pub account: &'a AccountInfo,
/// Number of bytes of memory to allocate.
pub space: u64,
}
impl Allocate<'_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 1] = [AccountMeta::writable_signer(self.account.key())];
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..12]: space
let mut instruction_data = [0; 12];
instruction_data[0] = 8;
instruction_data[4..12].copy_from_slice(&self.space.to_le_bytes());
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data,
};
invoke_signed(&instruction, &[self.account], signers)
}
}