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 async_trait::async_trait;
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::pubkey::Pubkey;
use crate::account_readers::dvl_readable::{DvlClientParams, DvlIndexParam};
use crate::accounts::client::client_account::client_account::ClientAccount;
use crate::accounts::client::client_account::signer_account_params::SignerAccountParams;
use crate::accounts::instruments::instruments_account::InstrumentsAccount;
use crate::accounts::root::root_account::RootAccount;
use crate::accounts::worker::pools_trace::pools_trace_account::PoolsTraceAccount;
use crate::accounts::worker::worker_account::WorkerAccount;
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::payoff::InstructionPayoff;

pub struct PayoffTransactionParams {
    pub worker_id: u32,
    pub client_key: Pubkey,
}

#[async_trait]
impl AsTransactionInstruction for InstructionPayoff {
    type DvlTransactionInstructionParams = PayoffTransactionParams;

    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 client_acc_key = transaction_params.client_key;
        let devol_sign_flag = true;
        let signer_account_params = SignerAccountParams {
            signer: &client_acc_key,
            devol_sign: devol_sign_flag,
        };
        let signer_account_params_option: Option<&SignerAccountParams> = Some(&signer_account_params);
        let client_acc = client.get_account::<ClientAccount>(DvlClientParams { client_address: &transaction_params.client_key, signer_account_params: signer_account_params_option }).await?;
        let instruments_acc_key = client.account_public_key::<InstrumentsAccount>(()).await?;
        let worker_acc_key = client.account_public_key::<WorkerAccount>(DvlIndexParam { id: transaction_params.worker_id }).await?;
        let pools_trace_key = client.account_public_key::<PoolsTraceAccount>(DvlIndexParam { id: transaction_params.worker_id }).await?;

        let account_metas = Vec::from([
            AccountMeta {
                pubkey: *signer,
                is_signer: true,
                is_writable: false,
            },
            AccountMeta {
                pubkey: *root_acc_key,
                is_signer: false,
                is_writable: false,
            },
            AccountMeta {
                pubkey: client_acc_key,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: client_acc.payoff_log,
                is_signer: false,
                is_writable: true,
            },
            AccountMeta {
                pubkey: *instruments_acc_key,
                is_signer: false,
                is_writable: false,
            },
            AccountMeta {
                pubkey: *worker_acc_key,
                is_signer: false,
                is_writable: false,
            },
            AccountMeta {
                pubkey: *pools_trace_key,
                is_signer: false,
                is_writable: true,
            },
        ]);
        Ok(Box::from(Instruction::new_with_bytes(
            client.program_id,
            &*data,
            account_metas,
        )))
    }
}