mpl_token_auth_rules/processor/
mod.rs

1//! The processors for the Rule Set program instructions.
2//!
3//! See state module for description of PDA memory layout.
4
5mod create_or_update;
6mod puff_rule_set;
7mod validate;
8mod write_to_buffer;
9
10use borsh::BorshDeserialize;
11use solana_program::{
12    account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
13    pubkey::Pubkey,
14};
15
16use crate::{
17    instruction::RuleSetInstruction,
18    processor::{
19        create_or_update::create_or_update, puff_rule_set::puff_rule_set, validate::validate,
20        write_to_buffer::write_to_buffer,
21    },
22};
23
24pub use crate::utils::cmp_pubkeys;
25
26/// The generic processor struct.
27pub struct Processor;
28impl Processor {
29    /// The main entrypoint for the Rule Set program that matches on the instruction type and args
30    pub fn process_instruction<'a>(
31        program_id: &Pubkey,
32        accounts: &'a [AccountInfo<'a>],
33        instruction_data: &[u8],
34    ) -> ProgramResult {
35        let instruction = RuleSetInstruction::try_from_slice(instruction_data)?;
36        match instruction {
37            RuleSetInstruction::CreateOrUpdate(args) => {
38                msg!("Instruction: CreateOrUpdate");
39                create_or_update(program_id, accounts, args)
40            }
41            RuleSetInstruction::Validate(args) => {
42                msg!("Instruction: Validate");
43                validate(program_id, accounts, args)
44            }
45            RuleSetInstruction::WriteToBuffer(args) => {
46                msg!("Instruction: WriteToBuffer");
47                write_to_buffer(program_id, accounts, args)
48            }
49            RuleSetInstruction::PuffRuleSet(args) => {
50                msg!("Instruction: PuffRuleSet");
51                puff_rule_set(program_id, accounts, args)
52            }
53        }
54    }
55}
56
57/// Convenience function for accessing the next item in an [`AccountInfo`]
58/// iterator and validating whether the account is present or not.
59///
60/// This relies on the client setting the `crate::id()` as the pubkey for
61/// accounts that are not set, which effectively allows us to use positional
62/// optional accounts.
63pub fn next_optional_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
64    iter: &mut I,
65) -> Result<Option<I::Item>, ProgramError> {
66    let account_info = iter.next().ok_or(ProgramError::NotEnoughAccountKeys)?;
67
68    Ok(if cmp_pubkeys(account_info.key, &crate::id()) {
69        None
70    } else {
71        Some(account_info)
72    })
73}
OSZAR »