clockwork_network/state/
config.rs1use {
2 anchor_lang::{prelude::*, AnchorDeserialize},
3 std::convert::TryFrom,
4};
5
6pub const SEED_CONFIG: &[u8] = b"config";
7
8static DEFAULT_SLOTS_PER_ROTATION: u64 = 10;
9
10#[account]
15#[derive(Debug)]
16pub struct Config {
17 pub admin: Pubkey,
18 pub mint: Pubkey,
19 pub slots_per_rotation: u64, }
21
22impl Config {
23 pub fn pubkey() -> Pubkey {
24 Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID).0
25 }
26}
27
28impl TryFrom<Vec<u8>> for Config {
29 type Error = Error;
30 fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
31 Config::try_deserialize(&mut data.as_slice())
32 }
33}
34
35#[derive(AnchorSerialize, AnchorDeserialize)]
40pub struct ConfigSettings {
41 pub admin: Pubkey,
42 pub mint: Pubkey,
43 pub slots_per_rotation: u64,
44}
45
46pub trait ConfigAccount {
51 fn init(&mut self, admin: Pubkey, mint: Pubkey) -> Result<()>;
52
53 fn update(&mut self, settings: ConfigSettings) -> Result<()>;
54}
55
56impl ConfigAccount for Account<'_, Config> {
57 fn init(&mut self, admin: Pubkey, mint: Pubkey) -> Result<()> {
58 self.admin = admin;
59 self.mint = mint;
60 self.slots_per_rotation = DEFAULT_SLOTS_PER_ROTATION;
61 Ok(())
62 }
63
64 fn update(&mut self, settings: ConfigSettings) -> Result<()> {
65 self.admin = settings.admin;
66 self.mint = settings.mint;
67 self.slots_per_rotation = settings.slots_per_rotation;
68 Ok(())
69 }
70}