cronos_scheduler/state/
fee.rs

1use {
2    crate::pda::PDA,
3    anchor_lang::{prelude::*, AnchorDeserialize},
4    std::convert::TryFrom,
5};
6
7pub const SEED_FEE: &[u8] = b"fee";
8
9/**
10 * Fee
11 */
12
13#[account]
14#[derive(Debug)]
15pub struct Fee {
16    pub balance: u64,
17    pub queue: Pubkey,
18}
19
20impl TryFrom<Vec<u8>> for Fee {
21    type Error = Error;
22    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
23        Fee::try_deserialize(&mut data.as_slice())
24    }
25}
26
27impl Fee {
28    pub fn pda(queue: Pubkey) -> PDA {
29        Pubkey::find_program_address(&[SEED_FEE, queue.as_ref()], &crate::ID)
30    }
31}
32
33/**
34 * FeeAccount
35 */
36
37pub trait FeeAccount {
38    fn new(&mut self, queue: Pubkey) -> Result<()>;
39
40    fn collect(&mut self, to: &mut Signer) -> Result<()>;
41}
42
43impl FeeAccount for Account<'_, Fee> {
44    fn new(&mut self, queue: Pubkey) -> Result<()> {
45        self.balance = 0;
46        self.queue = queue;
47        Ok(())
48    }
49
50    fn collect(&mut self, to: &mut Signer) -> Result<()> {
51        **self.to_account_info().try_borrow_mut_lamports()? = self
52            .to_account_info()
53            .lamports()
54            .checked_sub(self.balance)
55            .unwrap();
56        **to.to_account_info().try_borrow_mut_lamports()? = to
57            .to_account_info()
58            .lamports()
59            .checked_add(self.balance)
60            .unwrap();
61
62        self.balance = 0;
63
64        Ok(())
65    }
66}