triggr-program 0.1.1

Created with Anchor
Documentation
use std::mem::size_of;

use anchor_lang::prelude::*;

#[account]
#[derive(Debug, Hash, PartialEq, Default)]
pub struct ProgramState {
    /// The program's authority.
    pub authority: Pubkey,

    /// The list of valid middlewares.
    pub middleware: Vec<Middleware>,
}

impl ProgramState {
    pub const SIZE: usize = 8 + 32 + 4 + Middleware::SIZE;

    pub fn new(authority: Pubkey) -> Self {
        Self {
            authority,
            middleware: vec![],
        }
    }

    pub fn add_middleware(&mut self, name: String, program_id: Pubkey, accounts_count: u8) {
        self.middleware.push(Middleware {
            name,
            program_id,
            accounts_count,
        });
    }

    pub fn update_middleware_by_index(
        &mut self,
        index: usize,
        program_id: Pubkey,
        accounts_count: u8,
    ) {
        if let Some(middleware) = self.middleware.get_mut(index) {
            middleware.program_id = program_id;
            middleware.accounts_count = accounts_count;
        }
    }
}

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug, Hash, PartialEq, Default)]
/// Represents a middleware used in the program state.
pub struct Middleware {
    /// The name of the middleware. (mostly for display purposes)
    pub name: String,

    /// The program ID associated with the middleware.
    pub program_id: Pubkey,

    /// The number of accounts associated with the middleware. (to slice remaining accounts passed to the program)
    pub accounts_count: u8,
}

impl Middleware {
    pub const SIZE: usize = size_of::<Middleware>();
}