cronos_network/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
9static DEFAULT_SLOTS_PER_ROTATION: u64 = 10;
10
11/**
12 * Config
13 */
14
15#[account]
16#[derive(Debug)]
17pub struct Config {
18    pub admin: Pubkey,
19    pub mint: Pubkey,
20    pub slots_per_rotation: u64, // Target number of slots between each rotation
21}
22
23impl Config {
24    pub fn pda() -> PDA {
25        Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID)
26    }
27}
28
29impl TryFrom<Vec<u8>> for Config {
30    type Error = Error;
31    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
32        Config::try_deserialize(&mut data.as_slice())
33    }
34}
35
36/**
37 * ConfigSettings
38 */
39
40#[derive(AnchorSerialize, AnchorDeserialize)]
41pub struct ConfigSettings {
42    pub admin: Pubkey,
43    pub mint: Pubkey,
44    pub slots_per_rotation: u64,
45}
46
47/**
48 * ConfigAccount
49 */
50
51pub trait ConfigAccount {
52    fn new(&mut self, admin: Pubkey, mint: Pubkey) -> Result<()>;
53
54    fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()>;
55}
56
57impl ConfigAccount for Account<'_, Config> {
58    fn new(&mut self, admin: Pubkey, mint: Pubkey) -> Result<()> {
59        self.admin = admin;
60        self.mint = mint;
61        self.slots_per_rotation = DEFAULT_SLOTS_PER_ROTATION;
62        Ok(())
63    }
64
65    fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()> {
66        require!(
67            self.admin == admin.key(),
68            CronosError::AdminAuthorityInvalid
69        );
70        self.admin = settings.admin;
71        Ok(())
72    }
73}