Skip to main content

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