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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use anchor_lang::{prelude::*, AnchorDeserialize};

use super::Request;

pub const SEED_FEE: &[u8] = b"fee";

/**
 * Fee
 */

#[account]
#[derive(Debug)]
pub struct Fee {
    pub authority: Pubkey,
    pub admin_balance: u64,
    pub worker_balance: u64,
}

impl Fee {
    pub fn pubkey(authority: Pubkey) -> Pubkey {
        Pubkey::find_program_address(&[SEED_FEE, authority.as_ref()], &crate::ID).0
    }
}

/**
 * FeeAccount
 */

pub trait FeeAccount {
    fn pubkey(&self) -> Pubkey;

    fn init(&mut self, authority: Pubkey) -> Result<()>;

    fn claim_admin_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;

    fn claim_worker_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;

    fn pay_to_admin(&mut self, request: &mut Account<Request>) -> Result<()>;

    fn pay_to_worker(&mut self, request: &mut Account<Request>) -> Result<()>;
}

impl FeeAccount for Account<'_, Fee> {
    fn pubkey(&self) -> Pubkey {
        Fee::pubkey(self.authority)
    }

    fn init(&mut self, authority: Pubkey) -> Result<()> {
        self.authority = authority;
        self.admin_balance = 0;
        self.worker_balance = 0;
        Ok(())
    }

    fn claim_admin_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
        // Withdraw from the admin balance
        self.admin_balance = self.admin_balance.checked_sub(amount).unwrap();

        // Transfer lamports to the pay_to acccount
        **self.to_account_info().try_borrow_mut_lamports()? = self
            .to_account_info()
            .lamports()
            .checked_sub(amount)
            .unwrap();
        **pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
            .to_account_info()
            .lamports()
            .checked_add(amount)
            .unwrap();

        Ok(())
    }

    fn claim_worker_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
        // Withdraw from the worker amount
        self.worker_balance = self.worker_balance.checked_sub(amount).unwrap();

        // Transfer lamports to the pay_to acccount
        **self.to_account_info().try_borrow_mut_lamports()? = self
            .to_account_info()
            .lamports()
            .checked_sub(amount)
            .unwrap();
        **pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
            .to_account_info()
            .lamports()
            .checked_add(amount)
            .unwrap();

        Ok(())
    }

    fn pay_to_admin(&mut self, request: &mut Account<Request>) -> Result<()> {
        // Increment the claimable amount
        self.admin_balance = self.admin_balance.checked_add(request.fee_amount).unwrap();

        // Transfer lamports
        **request.to_account_info().try_borrow_mut_lamports()? = request
            .to_account_info()
            .lamports()
            .checked_sub(request.fee_amount)
            .unwrap();
        **self.to_account_info().try_borrow_mut_lamports()? = self
            .to_account_info()
            .lamports()
            .checked_add(request.fee_amount)
            .unwrap();

        Ok(())
    }

    fn pay_to_worker(&mut self, request: &mut Account<Request>) -> Result<()> {
        // Increment the claimable amount
        self.worker_balance = self.worker_balance.checked_add(request.fee_amount).unwrap();

        // Transfer lamports
        **request.to_account_info().try_borrow_mut_lamports()? = request
            .to_account_info()
            .lamports()
            .checked_sub(request.fee_amount)
            .unwrap();
        **self.to_account_info().try_borrow_mut_lamports()? = self
            .to_account_info()
            .lamports()
            .checked_add(request.fee_amount)
            .unwrap();

        Ok(())
    }
}