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                    token_program: ix.accounts[6].0.into(),
81                };
82                let de_ix_data: CreatePolicyIxData = BorshDeserialize::deserialize(&mut ix_data)?;
83                Ok(ShieldProgramIx::CreatePolicy(ix_accounts, de_ix_data))
84            }
85            [1] => {
86                check_min_accounts_req(accounts_len, 5)?;
87                let ix_accounts = AddIdentityIxAccounts {
88                    mint: ix.accounts[0].0.into(),
89                    token_account: ix.accounts[1].0.into(),
90                    policy: ix.accounts[2].0.into(),
91                    payer: ix.accounts[3].0.into(),
92                    owner: ix.accounts[4].0.into(),
93                    system_program: ix.accounts[5].0.into(),
94                };
95                let de_ix_data: AddIdentityIxData = BorshDeserialize::deserialize(&mut ix_data)?;
96                Ok(ShieldProgramIx::AddIdentity(ix_accounts, de_ix_data))
97            }
98            [2] => {
99                check_min_accounts_req(accounts_len, 5)?;
100                let ix_accounts = RemoveIdentityIxAccounts {
101                    mint: ix.accounts[0].0.into(),
102                    token_account: ix.accounts[1].0.into(),
103                    policy: ix.accounts[2].0.into(),
104                    payer: ix.accounts[3].0.into(),
105                    owner: ix.accounts[4].0.into(),
106                    system_program: ix.accounts[5].0.into(),
107                };
108                let de_ix_data: RemoveIdentityIxData = BorshDeserialize::deserialize(&mut ix_data)?;
109                Ok(ShieldProgramIx::RemoveIdentity(ix_accounts, de_ix_data))
110            }
111            _ => Err(yellowstone_vixen_core::ParseError::from(
112                "Invalid Instruction discriminator".to_owned(),
113            )),
114        }
115    }
116}
117
118pub fn check_min_accounts_req(
119    actual: usize,
120    expected: usize,
121) -> yellowstone_vixen_core::ParseResult<()> {
122    if actual < expected {
123        Err(yellowstone_vixen_core::ParseError::from(format!(
124            "Too few accounts provided: expected {expected}, got {actual}"
125        )))
126    } else {
127        Ok(())
128    }
129}