Skip to main content

dlp_api/compact/
instruction.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{args::MaybeEncryptedInstruction, compact, compact::ClearText};
4
5#[derive(Clone, Debug, Serialize, Deserialize)]
6pub struct Instruction {
7    pub program_id: u8,
8    pub accounts: Vec<compact::AccountMeta>,
9    pub data: Vec<u8>,
10}
11
12impl Instruction {
13    pub fn from_instruction(
14        ix: solana_program::instruction::Instruction,
15        index_of: &mut impl FnMut(
16            /*account_key*/ solana_program::pubkey::Pubkey,
17            /*signer*/ bool,
18        ) -> u8,
19    ) -> Instruction {
20        Instruction {
21            program_id: index_of(ix.program_id, false),
22
23            accounts: ix
24                .accounts
25                .iter()
26                .map(|meta| {
27                    compact::AccountMeta::try_new(
28                        index_of(meta.pubkey, meta.is_signer),
29                        meta.is_signer,
30                        meta.is_writable,
31                    )
32                    .expect("compact account index must fit in 6 bits")
33                })
34                .collect(),
35
36            data: ix.data,
37        }
38    }
39}
40
41impl ClearText for Instruction {
42    type Output = MaybeEncryptedInstruction;
43
44    fn cleartext(self) -> Self::Output {
45        MaybeEncryptedInstruction {
46            program_id: self.program_id,
47            accounts: self
48                .accounts
49                .into_iter()
50                .map(|meta| meta.cleartext())
51                .collect(),
52            data: self.data.cleartext(),
53        }
54    }
55}