1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::error::Error;
use std::str::FromStr;
use async_trait::async_trait;
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::pubkey::Pubkey;
use crate::account_readers::dvl_readable::DvlIndexParam;
use crate::accounts::mints::mint_log::mint_log_account::MintLogAccount;
use crate::accounts::mints::mints_account::MintsAccount;
use crate::accounts::root::root_account::RootAccount;
use crate::constants::TOKEN_PROGRAM_ID;
use crate::dvl_client::dvl_client::DvlClient;
use crate::instructions_data::as_transaction_instruction::as_transaction_instruction::AsTransactionInstruction;
use crate::instructions_data::dvl_instruction_data::DvlInstructionData;
use crate::instructions_data::transfer_token::InstructionTransferToken;

pub struct TransferTokenTransactionParams {
    pub mint_id: u32,
    pub client_key: Pubkey,
}

#[async_trait]
impl AsTransactionInstruction for InstructionTransferToken {
    type DvlTransactionInstructionParams = TransferTokenTransactionParams;

    async fn as_transaction_instruction(
        &self,
        client: &DvlClient,
        signer: &Pubkey,
        transaction_params: Self::DvlTransactionInstructionParams,
    ) -> Result<Box<Instruction>, Box<dyn Error>> {
        let data = self.to_vec_le();
        let root_acc_key = client.account_public_key::<RootAccount>(()).await?;
        let mint_acc_key = client.account_public_key::<MintsAccount>(()).await?;
        let mints_acc = client.get_account::<MintsAccount>(()).await?;
        let mints_program_acc_key = mints_acc.data[transaction_params.mint_id as usize].program_address;
        let mints_address_acc_key = mints_acc.data[transaction_params.mint_id as usize].address;
        let client_acc_key = transaction_params.client_key;
        let log_acc_key = client.account_public_key::<MintLogAccount>(DvlIndexParam { id: transaction_params.mint_id }).await?;
        let token_program_id_key = Pubkey::from_str(TOKEN_PROGRAM_ID)?;

        let account_metas = Vec::from([
            AccountMeta {
                pubkey: *signer,
                is_signer: true,
                is_writable: false,
            },
            AccountMeta {
                pubkey: *root_acc_key,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: mints_program_acc_key,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: *mint_acc_key,
                is_signer: false,
                is_writable: false,
            },
            AccountMeta {
                pubkey: token_program_id_key,
                is_signer: false,
                is_writable: false,
            },
            AccountMeta {
                pubkey: client_acc_key,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: *log_acc_key,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: mints_address_acc_key,
                is_signer: false,
                is_writable: true,
            },
        ]);
        Ok(Box::from(Instruction::new_with_bytes(
            client.program_id,
            &*data,
            account_metas,
        )))
    }
}