solana_native_programs/system_program/generated/instructions/
create_account.rs

1//! This code was AUTOGENERATED using the kinobi library.
2//! Please DO NOT EDIT THIS FILE, instead use visitors
3//! to add features, then rerun kinobi to update it.
4//!
5//! [https://github.com/metaplex-foundation/kinobi]
6//!
7
8use borsh::BorshDeserialize;
9use borsh::BorshSerialize;
10use solana_program::pubkey::Pubkey;
11
12/// Accounts.
13pub struct CreateAccount {
14    /// The payer for the new account
15    pub funding_account: solana_program::pubkey::Pubkey,
16    /// The account to create
17    pub new_account: solana_program::pubkey::Pubkey,
18}
19
20impl CreateAccount {
21    pub fn instruction(
22        &self,
23        args: CreateAccountInstructionArgs,
24    ) -> solana_program::instruction::Instruction {
25        self.instruction_with_remaining_accounts(args, &[])
26    }
27    #[allow(clippy::vec_init_then_push)]
28    pub fn instruction_with_remaining_accounts(
29        &self,
30        args: CreateAccountInstructionArgs,
31        remaining_accounts: &[solana_program::instruction::AccountMeta],
32    ) -> solana_program::instruction::Instruction {
33        let mut accounts = Vec::with_capacity(2 + remaining_accounts.len());
34        accounts.push(solana_program::instruction::AccountMeta::new(
35            self.funding_account,
36            true,
37        ));
38        accounts.push(solana_program::instruction::AccountMeta::new(
39            self.new_account,
40            true,
41        ));
42        accounts.extend_from_slice(remaining_accounts);
43        let mut data = CreateAccountInstructionData::new().try_to_vec().unwrap();
44        let mut args = args.try_to_vec().unwrap();
45        data.append(&mut args);
46
47        solana_program::instruction::Instruction {
48            program_id: crate::SYSTEM_PROGRAM_ID,
49            accounts,
50            data,
51        }
52    }
53}
54
55#[derive(BorshDeserialize, BorshSerialize)]
56struct CreateAccountInstructionData {
57    discriminator: u32,
58}
59
60impl CreateAccountInstructionData {
61    fn new() -> Self {
62        Self { discriminator: 0 }
63    }
64}
65
66#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub struct CreateAccountInstructionArgs {
69    pub lamports: u64,
70    pub space: u64,
71    pub owner: Pubkey,
72}
73
74/// Instruction builder for `CreateAccount`.
75///
76/// ### Accounts:
77///
78///   0. `[writable, signer]` funding_account
79///   1. `[writable, signer]` new_account
80#[derive(Default)]
81pub struct CreateAccountBuilder {
82    funding_account: Option<solana_program::pubkey::Pubkey>,
83    new_account: Option<solana_program::pubkey::Pubkey>,
84    lamports: Option<u64>,
85    space: Option<u64>,
86    owner: Option<Pubkey>,
87    __remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
88}
89
90impl CreateAccountBuilder {
91    pub fn new() -> Self {
92        Self::default()
93    }
94    /// The payer for the new account
95    #[inline(always)]
96    pub fn funding_account(
97        &mut self,
98        funding_account: solana_program::pubkey::Pubkey,
99    ) -> &mut Self {
100        self.funding_account = Some(funding_account);
101        self
102    }
103    /// The account to create
104    #[inline(always)]
105    pub fn new_account(&mut self, new_account: solana_program::pubkey::Pubkey) -> &mut Self {
106        self.new_account = Some(new_account);
107        self
108    }
109    #[inline(always)]
110    pub fn lamports(&mut self, lamports: u64) -> &mut Self {
111        self.lamports = Some(lamports);
112        self
113    }
114    #[inline(always)]
115    pub fn space(&mut self, space: u64) -> &mut Self {
116        self.space = Some(space);
117        self
118    }
119    #[inline(always)]
120    pub fn owner(&mut self, owner: Pubkey) -> &mut Self {
121        self.owner = Some(owner);
122        self
123    }
124    /// Add an aditional account to the instruction.
125    #[inline(always)]
126    pub fn add_remaining_account(
127        &mut self,
128        account: solana_program::instruction::AccountMeta,
129    ) -> &mut Self {
130        self.__remaining_accounts.push(account);
131        self
132    }
133    /// Add additional accounts to the instruction.
134    #[inline(always)]
135    pub fn add_remaining_accounts(
136        &mut self,
137        accounts: &[solana_program::instruction::AccountMeta],
138    ) -> &mut Self {
139        self.__remaining_accounts.extend_from_slice(accounts);
140        self
141    }
142    #[allow(clippy::clone_on_copy)]
143    pub fn instruction(&self) -> solana_program::instruction::Instruction {
144        let accounts = CreateAccount {
145            funding_account: self.funding_account.expect("funding_account is not set"),
146            new_account: self.new_account.expect("new_account is not set"),
147        };
148        let args = CreateAccountInstructionArgs {
149            lamports: self.lamports.clone().expect("lamports is not set"),
150            space: self.space.clone().expect("space is not set"),
151            owner: self.owner.clone().expect("owner is not set"),
152        };
153
154        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
155    }
156}
157
158/// `create_account` CPI accounts.
159pub struct CreateAccountCpiAccounts<'a, 'b> {
160    /// The payer for the new account
161    pub funding_account: &'b solana_program::account_info::AccountInfo<'a>,
162    /// The account to create
163    pub new_account: &'b solana_program::account_info::AccountInfo<'a>,
164}
165
166/// `create_account` CPI instruction.
167pub struct CreateAccountCpi<'a, 'b> {
168    /// The program to invoke.
169    pub __program: &'b solana_program::account_info::AccountInfo<'a>,
170    /// The payer for the new account
171    pub funding_account: &'b solana_program::account_info::AccountInfo<'a>,
172    /// The account to create
173    pub new_account: &'b solana_program::account_info::AccountInfo<'a>,
174    /// The arguments for the instruction.
175    pub __args: CreateAccountInstructionArgs,
176}
177
178impl<'a, 'b> CreateAccountCpi<'a, 'b> {
179    pub fn new(
180        program: &'b solana_program::account_info::AccountInfo<'a>,
181        accounts: CreateAccountCpiAccounts<'a, 'b>,
182        args: CreateAccountInstructionArgs,
183    ) -> Self {
184        Self {
185            __program: program,
186            funding_account: accounts.funding_account,
187            new_account: accounts.new_account,
188            __args: args,
189        }
190    }
191    #[inline(always)]
192    pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
193        self.invoke_signed_with_remaining_accounts(&[], &[])
194    }
195    #[inline(always)]
196    pub fn invoke_with_remaining_accounts(
197        &self,
198        remaining_accounts: &[(
199            &'b solana_program::account_info::AccountInfo<'a>,
200            bool,
201            bool,
202        )],
203    ) -> solana_program::entrypoint::ProgramResult {
204        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
205    }
206    #[inline(always)]
207    pub fn invoke_signed(
208        &self,
209        signers_seeds: &[&[&[u8]]],
210    ) -> solana_program::entrypoint::ProgramResult {
211        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
212    }
213    #[allow(clippy::clone_on_copy)]
214    #[allow(clippy::vec_init_then_push)]
215    pub fn invoke_signed_with_remaining_accounts(
216        &self,
217        signers_seeds: &[&[&[u8]]],
218        remaining_accounts: &[(
219            &'b solana_program::account_info::AccountInfo<'a>,
220            bool,
221            bool,
222        )],
223    ) -> solana_program::entrypoint::ProgramResult {
224        let mut accounts = Vec::with_capacity(2 + remaining_accounts.len());
225        accounts.push(solana_program::instruction::AccountMeta::new(
226            *self.funding_account.key,
227            true,
228        ));
229        accounts.push(solana_program::instruction::AccountMeta::new(
230            *self.new_account.key,
231            true,
232        ));
233        remaining_accounts.iter().for_each(|remaining_account| {
234            accounts.push(solana_program::instruction::AccountMeta {
235                pubkey: *remaining_account.0.key,
236                is_signer: remaining_account.1,
237                is_writable: remaining_account.2,
238            })
239        });
240        let mut data = CreateAccountInstructionData::new().try_to_vec().unwrap();
241        let mut args = self.__args.try_to_vec().unwrap();
242        data.append(&mut args);
243
244        let instruction = solana_program::instruction::Instruction {
245            program_id: crate::SYSTEM_PROGRAM_ID,
246            accounts,
247            data,
248        };
249        let mut account_infos = Vec::with_capacity(2 + 1 + remaining_accounts.len());
250        account_infos.push(self.__program.clone());
251        account_infos.push(self.funding_account.clone());
252        account_infos.push(self.new_account.clone());
253        remaining_accounts
254            .iter()
255            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
256
257        if signers_seeds.is_empty() {
258            solana_program::program::invoke(&instruction, &account_infos)
259        } else {
260            solana_program::program::invoke_signed(&instruction, &account_infos, signers_seeds)
261        }
262    }
263}
264
265/// Instruction builder for `CreateAccount` via CPI.
266///
267/// ### Accounts:
268///
269///   0. `[writable, signer]` funding_account
270///   1. `[writable, signer]` new_account
271pub struct CreateAccountCpiBuilder<'a, 'b> {
272    instruction: Box<CreateAccountCpiBuilderInstruction<'a, 'b>>,
273}
274
275impl<'a, 'b> CreateAccountCpiBuilder<'a, 'b> {
276    pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
277        let instruction = Box::new(CreateAccountCpiBuilderInstruction {
278            __program: program,
279            funding_account: None,
280            new_account: None,
281            lamports: None,
282            space: None,
283            owner: None,
284            __remaining_accounts: Vec::new(),
285        });
286        Self { instruction }
287    }
288    /// The payer for the new account
289    #[inline(always)]
290    pub fn funding_account(
291        &mut self,
292        funding_account: &'b solana_program::account_info::AccountInfo<'a>,
293    ) -> &mut Self {
294        self.instruction.funding_account = Some(funding_account);
295        self
296    }
297    /// The account to create
298    #[inline(always)]
299    pub fn new_account(
300        &mut self,
301        new_account: &'b solana_program::account_info::AccountInfo<'a>,
302    ) -> &mut Self {
303        self.instruction.new_account = Some(new_account);
304        self
305    }
306    #[inline(always)]
307    pub fn lamports(&mut self, lamports: u64) -> &mut Self {
308        self.instruction.lamports = Some(lamports);
309        self
310    }
311    #[inline(always)]
312    pub fn space(&mut self, space: u64) -> &mut Self {
313        self.instruction.space = Some(space);
314        self
315    }
316    #[inline(always)]
317    pub fn owner(&mut self, owner: Pubkey) -> &mut Self {
318        self.instruction.owner = Some(owner);
319        self
320    }
321    /// Add an additional account to the instruction.
322    #[inline(always)]
323    pub fn add_remaining_account(
324        &mut self,
325        account: &'b solana_program::account_info::AccountInfo<'a>,
326        is_writable: bool,
327        is_signer: bool,
328    ) -> &mut Self {
329        self.instruction
330            .__remaining_accounts
331            .push((account, is_writable, is_signer));
332        self
333    }
334    /// Add additional accounts to the instruction.
335    ///
336    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
337    /// and a `bool` indicating whether the account is a signer or not.
338    #[inline(always)]
339    pub fn add_remaining_accounts(
340        &mut self,
341        accounts: &[(
342            &'b solana_program::account_info::AccountInfo<'a>,
343            bool,
344            bool,
345        )],
346    ) -> &mut Self {
347        self.instruction
348            .__remaining_accounts
349            .extend_from_slice(accounts);
350        self
351    }
352    #[inline(always)]
353    pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
354        self.invoke_signed(&[])
355    }
356    #[allow(clippy::clone_on_copy)]
357    #[allow(clippy::vec_init_then_push)]
358    pub fn invoke_signed(
359        &self,
360        signers_seeds: &[&[&[u8]]],
361    ) -> solana_program::entrypoint::ProgramResult {
362        let args = CreateAccountInstructionArgs {
363            lamports: self
364                .instruction
365                .lamports
366                .clone()
367                .expect("lamports is not set"),
368            space: self.instruction.space.clone().expect("space is not set"),
369            owner: self.instruction.owner.clone().expect("owner is not set"),
370        };
371        let instruction = CreateAccountCpi {
372            __program: self.instruction.__program,
373
374            funding_account: self
375                .instruction
376                .funding_account
377                .expect("funding_account is not set"),
378
379            new_account: self
380                .instruction
381                .new_account
382                .expect("new_account is not set"),
383            __args: args,
384        };
385        instruction.invoke_signed_with_remaining_accounts(
386            signers_seeds,
387            &self.instruction.__remaining_accounts,
388        )
389    }
390}
391
392struct CreateAccountCpiBuilderInstruction<'a, 'b> {
393    __program: &'b solana_program::account_info::AccountInfo<'a>,
394    funding_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
395    new_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
396    lamports: Option<u64>,
397    space: Option<u64>,
398    owner: Option<Pubkey>,
399    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
400    __remaining_accounts: Vec<(
401        &'b solana_program::account_info::AccountInfo<'a>,
402        bool,
403        bool,
404    )>,
405}