hpsvm_token/
sync_native.rs1use 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#[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 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 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 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}