use std::mem::size_of;
use anchor_lang::prelude::*;
#[account]
#[derive(Debug, Hash, PartialEq, Default)]
pub struct ProgramState {
pub authority: Pubkey,
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)]
pub struct Middleware {
pub name: String,
pub program_id: Pubkey,
pub accounts_count: u8,
}
impl Middleware {
pub const SIZE: usize = size_of::<Middleware>();
}