1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::errors::ErrorCode;
use crate::pda::PDA;

use anchor_lang::prelude::*;
use anchor_lang::AccountDeserialize;
use std::convert::TryFrom;

pub const SEED_CONFIG: &[u8] = b"config";

/**
 * Config
 */

#[account]
#[derive(Debug)]
pub struct Config {
    pub admin: Pubkey,
    pub min_recurr: i64,
    pub program_fee: u64,
    pub worker_fee: u64,
    pub bump: u8,
}

impl Config {
    pub fn pda() -> PDA {
        Pubkey::find_program_address(&[SEED_CONFIG], &crate::ID)
    }
}

impl TryFrom<Vec<u8>> for Config {
    type Error = ProgramError;
    fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
        Config::try_deserialize(&mut data.as_slice())
    }
}

/**
 * ConfigAccount
 */

pub trait ConfigAccount {
    fn init(&mut self, admin: Pubkey, bump: u8) -> ProgramResult;
    fn update_admin(&mut self, admin: &Signer, new_admin: Pubkey) -> ProgramResult;
    fn update_min_recurr(&mut self, admin: &Signer, new_min_recurr: i64) -> ProgramResult;
    fn update_program_fee(&mut self, admin: &Signer, new_program_fee: u64) -> ProgramResult;
    fn update_worker_fee(&mut self, admin: &Signer, new_worker_fee: u64) -> ProgramResult;
}

impl ConfigAccount for Account<'_, Config> {
    fn init(&mut self, admin: Pubkey, bump: u8) -> ProgramResult {
        self.admin = admin;
        self.min_recurr = 5; // Minimum supported recurrence interval
        self.program_fee = 0; // Lamports to pay to program for each task execution
        self.worker_fee = 0; // Lamports to pay to worker for each task execution
        self.bump = bump;
        Ok(())
    }

    fn update_admin(&mut self, admin: &Signer, new_admin: Pubkey) -> ProgramResult {
        require!(self.admin == admin.key(), ErrorCode::NotAuthorizedAdmin);
        self.admin = new_admin;
        Ok(())
    }

    fn update_min_recurr(&mut self, admin: &Signer, new_min_recurr: i64) -> ProgramResult {
        require!(self.admin == admin.key(), ErrorCode::NotAuthorizedAdmin);
        require!(new_min_recurr >= 0, ErrorCode::InvalidRecurrNegative);
        self.min_recurr = new_min_recurr;
        Ok(())
    }

    fn update_program_fee(&mut self, admin: &Signer, new_program_fee: u64) -> ProgramResult {
        require!(self.admin == admin.key(), ErrorCode::NotAuthorizedAdmin);
        self.program_fee = new_program_fee;
        Ok(())
    }

    fn update_worker_fee(&mut self, admin: &Signer, new_worker_fee: u64) -> ProgramResult {
        require!(self.admin == admin.key(), ErrorCode::NotAuthorizedAdmin);
        self.worker_fee = new_worker_fee;
        Ok(())
    }
}