|
1 | | -use pinocchio::{ |
2 | | - cpi::{Seed, Signer}, |
3 | | - error::ProgramError, |
4 | | - AccountView, ProgramResult, |
5 | | -}; |
6 | | -use pinocchio_system::instructions::{Allocate, Assign}; |
| 1 | +use pinocchio::{cpi::Signer, error::ProgramError, instruction::seeds, AccountView, ProgramResult}; |
| 2 | +use pinocchio_system::instructions::{Assign, CreateAccountAllowPrefund}; |
7 | 3 |
|
8 | 4 | use crate::{ |
9 | 5 | error::ProgramMetadataError, |
@@ -90,28 +86,38 @@ pub fn allocate(accounts: &mut [AccountView], instruction_data: &[u8]) -> Progra |
90 | 86 |
|
91 | 87 | match (is_pda, canonical) { |
92 | 88 | // canonical |
93 | | - (true, true) => allocate_and_assign( |
94 | | - buffer, |
| 89 | + (true, true) => CreateAccountAllowPrefund { |
| 90 | + to: buffer, |
95 | 91 | space, |
96 | | - &[ |
97 | | - Seed::from(program.address().as_array()), |
98 | | - Seed::from(instruction_data), |
99 | | - Seed::from(&[bump]), |
100 | | - ], |
101 | | - )?, |
| 92 | + owner: &crate::ID, |
| 93 | + funding: None, |
| 94 | + } |
| 95 | + .invoke_signed(&[Signer::from(&seeds!( |
| 96 | + program.address().as_ref(), |
| 97 | + instruction_data, |
| 98 | + &[bump] |
| 99 | + ))])?, |
102 | 100 | // non-canonical |
103 | | - (true, false) => allocate_and_assign( |
104 | | - buffer, |
| 101 | + (true, false) => CreateAccountAllowPrefund { |
| 102 | + to: buffer, |
105 | 103 | space, |
106 | | - &[ |
107 | | - Seed::from(program.address().as_array()), |
108 | | - Seed::from(authority.address().as_array()), |
109 | | - Seed::from(instruction_data), |
110 | | - Seed::from(&[bump]), |
111 | | - ], |
112 | | - )?, |
| 104 | + owner: &crate::ID, |
| 105 | + funding: None, |
| 106 | + } |
| 107 | + .invoke_signed(&[Signer::from(&seeds!( |
| 108 | + program.address().as_ref(), |
| 109 | + authority.address().as_ref(), |
| 110 | + instruction_data, |
| 111 | + &[bump] |
| 112 | + ))])?, |
113 | 113 | // keypair |
114 | | - (false, _) => allocate_and_assign(buffer, space, &[])?, |
| 114 | + (false, _) => CreateAccountAllowPrefund { |
| 115 | + to: buffer, |
| 116 | + space, |
| 117 | + owner: &crate::ID, |
| 118 | + funding: None, |
| 119 | + } |
| 120 | + .invoke()?, |
115 | 121 | } |
116 | 122 | } |
117 | 123 | n if n >= Buffer::LEN => { |
@@ -163,23 +169,3 @@ pub fn allocate(accounts: &mut [AccountView], instruction_data: &[u8]) -> Progra |
163 | 169 |
|
164 | 170 | Ok(()) |
165 | 171 | } |
166 | | - |
167 | | -/// Allocates the space for the account and assigns it to the program. |
168 | | -/// |
169 | | -/// When the `account` is a PDA, the `seeds` are used to create the signer. |
170 | | -#[inline(always)] |
171 | | -fn allocate_and_assign(account: &AccountView, space: u64, seeds: &[Seed]) -> ProgramResult { |
172 | | - let signer: &[Signer] = if seeds.is_empty() { |
173 | | - &[] |
174 | | - } else { |
175 | | - &[Signer::from(seeds)] |
176 | | - }; |
177 | | - |
178 | | - Allocate { account, space }.invoke_signed(signer)?; |
179 | | - |
180 | | - Assign { |
181 | | - account, |
182 | | - owner: &crate::ID, |
183 | | - } |
184 | | - .invoke_signed(signer) |
185 | | -} |
0 commit comments