Skip to main content

hpsvm_token/
sync_native.rs

1use hpsvm::{HPSVM, types::FailedTransactionMetadata};
2use solana_address::Address;
3use solana_keypair::Keypair;
4
5use super::{TOKEN_ID, spl_token::instruction::sync_native};
6
7/// ### Description
8/// Builder for the [`sync_native`] instruction.
9///
10/// ### Optional fields
11/// - `token_program_id`: [`TOKEN_ID`] by default.
12#[derive(Debug)]
13pub struct SyncNative<'a> {
14    svm: &'a mut HPSVM,
15    payer: &'a Keypair,
16    account: &'a Address,
17    token_program_id: Option<&'a Address>,
18}
19
20impl<'a> SyncNative<'a> {
21    /// Creates a new instance of [`sync_native`] instruction.
22    pub fn new(svm: &'a mut HPSVM, payer: &'a Keypair, account: &'a Address) -> Self {
23        SyncNative { svm, payer, account, token_program_id: None }
24    }
25
26    /// Sets the token program id for the instruction.
27    pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
28        self.token_program_id = Some(program_id);
29        self
30    }
31
32    /// Sends the transaction.
33    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
34        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
35
36        let ix = sync_native(token_program_id, self.account)?;
37
38        super::sign_and_send(self.svm, self.payer, &[], ix)
39    }
40}