clockwork_scheduler/state/
config.rs1use {
2 anchor_lang::{prelude::*, AnchorDeserialize},
3 std::convert::TryFrom,
4};
5
6pub const SEED_CONFIG: &[u8] = b"config";
7
8#[account]
13#[derive(Debug)]
14pub struct Config {
15 pub admin: Pubkey,
16 pub grace_period: i64,
17 pub spam_penalty: u64,
18 pub worker_fee: u64,
19}
20
21impl Config {
22 pub fn pubkey() -> Pubkey {
23 Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID).0
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#[derive(AnchorSerialize, AnchorDeserialize)]
39pub struct ConfigSettings {
40 pub admin: Pubkey,
41 pub grace_period: i64,
42 pub spam_penalty: u64,
43 pub worker_fee: u64,
44}
45pub trait ConfigAccount {
50 fn new(&mut self, admin: Pubkey) -> Result<()>;
51
52 fn update(&mut self, settings: ConfigSettings) -> Result<()>;
53}
54
55impl ConfigAccount for Account<'_, Config> {
56 fn new(&mut self, admin: Pubkey) -> Result<()> {
57 self.admin = admin;
58 self.grace_period = 0;
59 self.spam_penalty = 0;
60 self.worker_fee = 0;
61 Ok(())
62 }
63
64 fn update(&mut self, settings: ConfigSettings) -> Result<()> {
65 self.admin = settings.admin;
66 self.grace_period = settings.grace_period;
67 self.spam_penalty = settings.spam_penalty;
68 self.worker_fee = settings.worker_fee;
69 Ok(())
70 }
71}