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