cronos_scheduler/state/
config.rs

1use {
2    crate::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 delegate_fee: u64,
18    pub delegate_holdout_period: i64,
19    pub delegate_spam_penalty: u64,
20    pub program_fee: u64,
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 delegate_fee: u64,
44    pub delegate_holdout_period: i64,
45    pub delegate_spam_penalty: u64,
46    pub program_fee: u64,
47}
48/**
49 * ConfigAccount
50 */
51
52pub trait ConfigAccount {
53    fn new(&mut self, admin: Pubkey) -> Result<()>;
54
55    fn update(&mut self, settings: ConfigSettings) -> Result<()>;
56}
57
58impl ConfigAccount for Account<'_, Config> {
59    fn new(&mut self, admin: Pubkey) -> Result<()> {
60        self.admin = admin;
61        self.delegate_fee = 0;
62        self.delegate_holdout_period = 0;
63        self.delegate_spam_penalty = 0;
64        self.program_fee = 0;
65        Ok(())
66    }
67
68    fn update(&mut self, settings: ConfigSettings) -> Result<()> {
69        self.admin = settings.admin;
70        self.delegate_fee = settings.delegate_fee;
71        self.delegate_holdout_period = settings.delegate_holdout_period;
72        self.delegate_spam_penalty = settings.delegate_spam_penalty;
73        self.program_fee = settings.program_fee;
74        Ok(())
75    }
76}