clockwork_http/state/
fee.rs

1use super::Request;
2
3use {
4    anchor_lang::{prelude::*, AnchorDeserialize},
5    std::convert::TryFrom,
6};
7
8pub const SEED_FEE: &[u8] = b"fee";
9
10/**
11 * Fee
12 */
13
14#[account]
15#[derive(Debug)]
16pub struct Fee {
17    pub authority: Pubkey,
18    pub admin_balance: u64,
19    pub worker_balance: u64,
20}
21
22impl Fee {
23    pub fn pubkey(authority: Pubkey) -> Pubkey {
24        Pubkey::find_program_address(&[SEED_FEE, authority.as_ref()], &crate::ID).0
25    }
26}
27
28impl TryFrom<Vec<u8>> for Fee {
29    type Error = Error;
30    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
31        Fee::try_deserialize(&mut data.as_slice())
32    }
33}
34
35/**
36 * FeeAccount
37 */
38
39pub trait FeeAccount {
40    fn new(&mut self, authority: Pubkey) -> Result<()>;
41
42    fn claim_admin_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;
43
44    fn claim_worker_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;
45
46    fn pay_to_admin(&mut self, request: &mut Account<Request>) -> Result<()>;
47
48    fn pay_to_worker(&mut self, request: &mut Account<Request>) -> Result<()>;
49}
50
51impl FeeAccount for Account<'_, Fee> {
52    fn new(&mut self, authority: Pubkey) -> Result<()> {
53        self.authority = authority;
54        self.admin_balance = 0;
55        self.worker_balance = 0;
56        Ok(())
57    }
58
59    fn claim_admin_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
60        // Withdraw from the admin balance
61        self.admin_balance = self.admin_balance.checked_sub(amount).unwrap();
62
63        // Transfer lamports to the pay_to acccount
64        **self.to_account_info().try_borrow_mut_lamports()? = self
65            .to_account_info()
66            .lamports()
67            .checked_sub(amount)
68            .unwrap();
69        **pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
70            .to_account_info()
71            .lamports()
72            .checked_add(amount)
73            .unwrap();
74
75        Ok(())
76    }
77
78    fn claim_worker_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
79        // Withdraw from the worker amount
80        self.worker_balance = self.worker_balance.checked_sub(amount).unwrap();
81
82        // Transfer lamports to the pay_to acccount
83        **self.to_account_info().try_borrow_mut_lamports()? = self
84            .to_account_info()
85            .lamports()
86            .checked_sub(amount)
87            .unwrap();
88        **pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
89            .to_account_info()
90            .lamports()
91            .checked_add(amount)
92            .unwrap();
93
94        Ok(())
95    }
96
97    fn pay_to_admin(&mut self, request: &mut Account<Request>) -> Result<()> {
98        // Increment the claimable amount
99        self.admin_balance = self.admin_balance.checked_add(request.fee_amount).unwrap();
100
101        // Transfer lamports
102        **request.to_account_info().try_borrow_mut_lamports()? = request
103            .to_account_info()
104            .lamports()
105            .checked_sub(request.fee_amount)
106            .unwrap();
107        **self.to_account_info().try_borrow_mut_lamports()? = self
108            .to_account_info()
109            .lamports()
110            .checked_add(request.fee_amount)
111            .unwrap();
112
113        Ok(())
114    }
115
116    fn pay_to_worker(&mut self, request: &mut Account<Request>) -> Result<()> {
117        // Increment the claimable amount
118        self.worker_balance = self.worker_balance.checked_add(request.fee_amount).unwrap();
119
120        // Transfer lamports
121        **request.to_account_info().try_borrow_mut_lamports()? = request
122            .to_account_info()
123            .lamports()
124            .checked_sub(request.fee_amount)
125            .unwrap();
126        **self.to_account_info().try_borrow_mut_lamports()? = self
127            .to_account_info()
128            .lamports()
129            .checked_add(request.fee_amount)
130            .unwrap();
131
132        Ok(())
133    }
134}