Skip to content

Commit 6ef1bda

Browse files
committed
Use create allow prefund
1 parent 770bfbc commit 6ef1bda

2 files changed

Lines changed: 34 additions & 52 deletions

File tree

program/src/processor/allocate.rs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
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};
73

84
use crate::{
95
error::ProgramMetadataError,
@@ -90,28 +86,38 @@ pub fn allocate(accounts: &mut [AccountView], instruction_data: &[u8]) -> Progra
9086

9187
match (is_pda, canonical) {
9288
// canonical
93-
(true, true) => allocate_and_assign(
94-
buffer,
89+
(true, true) => CreateAccountAllowPrefund {
90+
to: buffer,
9591
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+
))])?,
102100
// non-canonical
103-
(true, false) => allocate_and_assign(
104-
buffer,
101+
(true, false) => CreateAccountAllowPrefund {
102+
to: buffer,
105103
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+
))])?,
113113
// 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()?,
115121
}
116122
}
117123
n if n >= Buffer::LEN => {
@@ -163,23 +169,3 @@ pub fn allocate(accounts: &mut [AccountView], instruction_data: &[u8]) -> Progra
163169

164170
Ok(())
165171
}
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-
}

program/src/processor/initialize.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use pinocchio::{
66
instruction::seeds,
77
AccountView, Address, ProgramResult,
88
};
9-
use pinocchio_system::instructions::{Allocate, Assign};
9+
use pinocchio_system::instructions::CreateAccountAllowPrefund;
1010

1111
use crate::{
1212
processor::derive_program_address,
@@ -139,15 +139,11 @@ pub fn initialize(accounts: &mut [AccountView], instruction_data: &[u8]) -> Prog
139139
// Instruction data is limited to ~1232 bytes.
140140
let space = Header::LEN + remaining_data.len();
141141

142-
Allocate {
143-
account: metadata,
142+
CreateAccountAllowPrefund {
143+
to: metadata,
144144
space: space as u64,
145-
}
146-
.invoke_signed(signer)?;
147-
148-
Assign {
149-
account: metadata,
150145
owner: &crate::ID,
146+
funding: None,
151147
}
152148
.invoke_signed(signer)?;
153149

0 commit comments

Comments
 (0)