yellowstone_shield_parser/generated/
instructions_parser.rs

1//! This code was AUTOGENERATED using the codama library.
2//! Please DO NOT EDIT THIS FILE, instead use visitors
3//! to add features, then rerun codama to update it.
4//!
5//! <https://github.com/codama-idl/codama>
6//!
7
8use borsh_0_10::BorshDeserialize;
9use yellowstone_shield_client::instructions::{
10    AddIdentity as AddIdentityIxAccounts, AddIdentityInstructionArgs as AddIdentityIxData,
11    CreatePolicy as CreatePolicyIxAccounts, CreatePolicyInstructionArgs as CreatePolicyIxData,
12    RemoveIdentity as RemoveIdentityIxAccounts,
13    RemoveIdentityInstructionArgs as RemoveIdentityIxData,
14};
15use yellowstone_shield_client::ID;
16
17/// Shield Instructions
18#[derive(Debug)]
19#[allow(dead_code)]
20pub enum ShieldProgramIx {
21    CreatePolicy(CreatePolicyIxAccounts, CreatePolicyIxData),
22    AddIdentity(AddIdentityIxAccounts, AddIdentityIxData),
23    RemoveIdentity(RemoveIdentityIxAccounts, RemoveIdentityIxData),
24}
25
26#[derive(Debug, Copy, Clone)]
27pub struct InstructionParser;
28
29impl yellowstone_vixen_core::Parser for InstructionParser {
30    type Input = yellowstone_vixen_core::instruction::InstructionUpdate;
31    type Output = ShieldProgramIx;
32
33    fn id(&self) -> std::borrow::Cow<str> {
34        "Shield::InstructionParser".into()
35    }
36
37    fn prefilter(&self) -> yellowstone_vixen_core::Prefilter {
38        yellowstone_vixen_core::Prefilter::builder()
39            .transaction_accounts([ID])
40            .build()
41            .unwrap()
42    }
43
44    async fn parse(
45        &self,
46        ix_update: &yellowstone_vixen_core::instruction::InstructionUpdate,
47    ) -> yellowstone_vixen_core::ParseResult<Self::Output> {
48        if ix_update.program.equals_ref(ID) {
49            InstructionParser::parse_impl(ix_update)
50        } else {
51            Err(yellowstone_vixen_core::ParseError::Filtered)
52        }
53    }
54}
55
56impl yellowstone_vixen_core::ProgramParser for InstructionParser {
57    #[inline]
58    fn program_id(&self) -> yellowstone_vixen_core::Pubkey {
59        ID.to_bytes().into()
60    }
61}
62
63impl InstructionParser {
64    pub(crate) fn parse_impl(
65        ix: &yellowstone_vixen_core::instruction::InstructionUpdate,
66    ) -> yellowstone_vixen_core::ParseResult<ShieldProgramIx> {
67        let accounts_len = ix.accounts.len();
68        let ix_discriminator: [u8; 1] = ix.data[0..1].try_into()?;
69        let mut ix_data = &ix.data[1..];
70        match ix_discriminator {
71            [0] => {
72                check_min_accounts_req(accounts_len, 6)?;
73                let ix_accounts = CreatePolicyIxAccounts {
74                    mint: ix.accounts[0].0.into(),
75                    token_account: ix.accounts[1].0.into(),
76                    policy: ix.accounts[2].0.into(),
77                    payer: ix.accounts[3].0.into(),
78                    owner: ix.accounts[4].0.into(),
79                    system_program: ix.accounts[5].0.into(),
80                };
81                let de_ix_data: CreatePolicyIxData = BorshDeserialize::deserialize(&mut ix_data)?;
82                Ok(ShieldProgramIx::CreatePolicy(ix_accounts, de_ix_data))
83            }
84            [1] => {
85                check_min_accounts_req(accounts_len, 5)?;
86                let ix_accounts = AddIdentityIxAccounts {
87                    mint: ix.accounts[0].0.into(),
88                    token_account: ix.accounts[1].0.into(),
89                    policy: ix.accounts[2].0.into(),
90                    payer: ix.accounts[3].0.into(),
91                    owner: ix.accounts[4].0.into(),
92                    system_program: ix.accounts[5].0.into(),
93                };
94                let de_ix_data: AddIdentityIxData = BorshDeserialize::deserialize(&mut ix_data)?;
95                Ok(ShieldProgramIx::AddIdentity(ix_accounts, de_ix_data))
96            }
97            [2] => {
98                check_min_accounts_req(accounts_len, 5)?;
99                let ix_accounts = RemoveIdentityIxAccounts {
100                    mint: ix.accounts[0].0.into(),
101                    token_account: ix.accounts[1].0.into(),
102                    policy: ix.accounts[2].0.into(),
103                    owner: ix.accounts[4].0.into(),
104                };
105                let de_ix_data: RemoveIdentityIxData = BorshDeserialize::deserialize(&mut ix_data)?;
106                Ok(ShieldProgramIx::RemoveIdentity(ix_accounts, de_ix_data))
107            }
108            _ => Err(yellowstone_vixen_core::ParseError::from(
109                "Invalid Instruction discriminator".to_owned(),
110            )),
111        }
112    }
113}
114
115pub fn check_min_accounts_req(
116    actual: usize,
117    expected: usize,
118) -> yellowstone_vixen_core::ParseResult<()> {
119    if actual < expected {
120        Err(yellowstone_vixen_core::ParseError::from(format!(
121            "Too few accounts provided: expected {expected}, got {actual}"
122        )))
123    } else {
124        Ok(())
125    }
126}