-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathcreate_account_with_seed.rs
87 lines (74 loc) · 3.01 KB
/
create_account_with_seed.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use pinocchio::{
account_view::AccountView,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
Address, ProgramResult,
};
/// Create a new account at an address derived from a base address and a seed.
///
/// ### Accounts:
/// 0. `[WRITE, SIGNER]` Funding account
/// 1. `[WRITE]` Created account
/// 2. `[SIGNER]` (optional) Base account; the account matching the base address below must be
/// provided as a signer, but may be the same as the funding account
pub struct CreateAccountWithSeed<'a, 'b, 'c> {
/// Funding account.
pub from: &'a AccountView,
/// New account.
pub to: &'a AccountView,
/// Base account.
///
/// The account matching the base [`Address`] below must be provided as
/// a signer, but may be the same as the funding account and provided
/// as account 0.
pub base: Option<&'a AccountView>,
/// String of ASCII chars, no longer than [MAX_SEED_LEN](https://docs.rs/solana-address/latest/solana_address/constant.MAX_SEED_LEN.html).
pub seed: &'b str,
/// Number of lamports to transfer to the new account.
pub lamports: u64,
/// Number of bytes of memory to allocate.
pub space: u64,
/// Address of program that will own the new account.
pub owner: &'c Address,
}
impl CreateAccountWithSeed<'_, '_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 3] = [
AccountMeta::writable_signer(self.from.key()),
AccountMeta::writable(self.to.key()),
AccountMeta::readonly_signer(self.base.unwrap_or(self.from).key()),
];
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36 ]: base address
// - [36..44]: seed length
// - [44.. ]: seed (max 32)
// - [.. +8]: lamports
// - [.. +8]: account space
// - [.. +32]: owner address
let mut instruction_data = [0; 120];
instruction_data[0] = 3;
instruction_data[4..36].copy_from_slice(self.base.unwrap_or(self.from).key());
instruction_data[36..44].copy_from_slice(&u64::to_le_bytes(self.seed.len() as u64));
let offset = 44 + self.seed.len();
instruction_data[44..offset].copy_from_slice(self.seed.as_bytes());
instruction_data[offset..offset + 8].copy_from_slice(&self.lamports.to_le_bytes());
instruction_data[offset + 8..offset + 16].copy_from_slice(&self.space.to_le_bytes());
instruction_data[offset + 16..offset + 48].copy_from_slice(self.owner.as_ref());
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data[..offset + 48],
};
invoke_signed(
&instruction,
&[self.from, self.to, self.base.unwrap_or(self.from)],
signers,
)
}
}