Skip to main content

sealevel_tools/cpi/token_program/
sync_native.rs

1use crate::{
2    cpi::{CpiAuthority, CpiInstruction},
3    entrypoint::NoStdAccountInfo,
4    pubkey::Pubkey,
5};
6
7/// Arguments for the sync native instruction on the specified Token program, which synchronizes the
8/// amount (balance) on the token account with the number of excess lamports on the token account.
9/// Performing this call is effectively "wrapping" SOL as the mint representation of SOL.
10#[derive(Clone, PartialEq, Eq)]
11pub struct SyncNative<'a, 'b: 'a> {
12    pub token_program_id: &'a Pubkey,
13    pub source: &'b NoStdAccountInfo,
14    pub authority: CpiAuthority<'a, 'b>,
15}
16
17impl<'a, 'b: 'a> SyncNative<'a, 'b> {
18    /// Consume arguments to perform CPI call.
19    #[inline(always)]
20    pub fn into_invoke(self) {
21        let Self {
22            token_program_id,
23            source,
24            authority,
25        } = self;
26
27        CpiInstruction {
28            program_id: token_program_id,
29            accounts: &[source.to_meta_c(), authority.to_meta_c_signer()],
30            // Sync native selector == 17.
31            data: &[17],
32        }
33        .invoke_possibly_signed(
34            &[source.to_info_c(), authority.to_info_c()],
35            &[authority.signer_seeds],
36        );
37    }
38}