Skip to main content

hpsvm_token/
sync_native.rs

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