clockwork_http/state/
config.rs

1use {
2    anchor_lang::{prelude::*, AnchorDeserialize},
3    std::convert::TryFrom,
4};
5
6pub const SEED_CONFIG: &[u8] = b"config";
7
8/**
9 * Defaults
10 */
11
12static DEFAULT_REQUEST_FEE: u64 = 1_000_000; // 0.001 SOL
13static DEFAULT_TIMEOUT_THRESHOLD: u64 = 100; // 100 slots
14
15/**
16 * Config
17 */
18
19#[account]
20#[derive(Debug)]
21pub struct Config {
22    pub admin: Pubkey,
23    pub request_fee: u64, // Amount to charge per request and payout to workers
24    pub timeout_threshold: u64, // Duration (slots) to wait before a requests is considered "timed out"
25}
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/**
41 * ConfigSettings
42 */
43
44#[derive(AnchorSerialize, AnchorDeserialize)]
45pub struct ConfigSettings {
46    pub admin: Pubkey,
47    pub request_fee: u64,
48    pub timeout_threshold: u64,
49}
50
51/**
52 * ConfigAccount
53 */
54
55pub 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}