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