clockwork_crank/state/
config.rs1use {
2 anchor_lang::{prelude::*, AnchorDeserialize},
3 std::convert::TryFrom,
4};
5
6pub const SEED_CONFIG: &[u8] = b"config";
7
8static DEFAULT_CRANK_FEE: u64 = 1_000;
9
10#[account]
15#[derive(Debug)]
16pub struct Config {
17 pub admin: Pubkey,
18 pub crank_fee: u64,
19 pub worker_pool: Pubkey,
20}
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 crank_fee: u64,
43 pub worker_pool: Pubkey,
44}
45
46pub trait ConfigAccount {
51 fn init(&mut self, admin: Pubkey, worker_pool: 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, worker_pool: Pubkey) -> Result<()> {
58 self.admin = admin;
59 self.crank_fee = DEFAULT_CRANK_FEE;
60 self.worker_pool = worker_pool;
61 Ok(())
62 }
63
64 fn update(&mut self, settings: ConfigSettings) -> Result<()> {
65 self.admin = settings.admin;
66 self.crank_fee = settings.crank_fee;
67 self.worker_pool;
68 Ok(())
69 }
70}