clockwork_http/state/
config.rs1use {
2 anchor_lang::{prelude::*, AnchorDeserialize},
3 std::convert::TryFrom,
4};
5
6pub const SEED_CONFIG: &[u8] = b"config";
7
8static DEFAULT_REQUEST_FEE: u64 = 1_000_000; static DEFAULT_TIMEOUT_THRESHOLD: u64 = 100; #[account]
20#[derive(Debug)]
21pub struct Config {
22 pub admin: Pubkey,
23 pub request_fee: u64, pub timeout_threshold: u64, }
26
27impl Config {
28 pub fn pubkey() -> Pubkey {
29 Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID).0
30 }
31}
32
33impl TryFrom<Vec<u8>> for Config {
34 type Error = Error;
35 fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
36 Config::try_deserialize(&mut data.as_slice())
37 }
38}
39
40#[derive(AnchorSerialize, AnchorDeserialize)]
45pub struct ConfigSettings {
46 pub admin: Pubkey,
47 pub request_fee: u64,
48 pub timeout_threshold: u64,
49}
50
51pub trait ConfigAccount {
56 fn new(&mut self, admin: Pubkey) -> Result<()>;
57
58 fn update(&mut self, settings: ConfigSettings) -> Result<()>;
59}
60
61impl ConfigAccount for Account<'_, Config> {
62 fn new(&mut self, admin: Pubkey) -> Result<()> {
63 self.admin = admin;
64 self.request_fee = DEFAULT_REQUEST_FEE;
65 self.timeout_threshold = DEFAULT_TIMEOUT_THRESHOLD;
66 Ok(())
67 }
68
69 fn update(&mut self, settings: ConfigSettings) -> Result<()> {
70 self.admin = settings.admin;
71 self.request_fee = settings.request_fee;
72 self.timeout_threshold = settings.timeout_threshold;
73 Ok(())
74 }
75}