cronos_pool/state/
config.rs

1use {
2    crate::{errors::CronosError, pda::PDA},
3    anchor_lang::{prelude::*, AnchorDeserialize},
4    std::convert::TryFrom,
5};
6
7pub const SEED_CONFIG: &[u8] = b"config";
8
9/**
10 * Config
11 */
12
13#[account]
14#[derive(Debug)]
15pub struct Config {
16    pub admin: Pubkey,
17    pub rotator: Pubkey,
18    pub pool_size: usize,
19}
20
21impl Config {
22    pub fn pda() -> PDA {
23        Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID)
24    }
25}
26
27impl TryFrom<Vec<u8>> for Config {
28    type Error = Error;
29    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
30        Config::try_deserialize(&mut data.as_slice())
31    }
32}
33
34/**
35 * ConfigSettings
36 */
37
38#[derive(AnchorSerialize, AnchorDeserialize)]
39pub struct ConfigSettings {
40    pub admin: Pubkey,
41    pub rotator: Pubkey,
42    pub pool_size: usize,
43}
44
45/**
46 * ConfigAccount
47 */
48
49pub trait ConfigAccount {
50    fn new(&mut self, admin: Pubkey, rotator: Pubkey) -> Result<()>;
51
52    fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()>;
53}
54
55impl ConfigAccount for Account<'_, Config> {
56    fn new(&mut self, admin: Pubkey, rotator: Pubkey) -> Result<()> {
57        self.admin = admin;
58        self.rotator = rotator;
59        self.pool_size = 1;
60        Ok(())
61    }
62
63    fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()> {
64        require!(self.admin == admin.key(), CronosError::NotAuthorizedAdmin);
65        self.admin = settings.admin;
66        self.rotator = settings.rotator;
67        self.pool_size = settings.pool_size;
68        Ok(())
69    }
70}