miclockwork_network_program/state/
fee.rs

1use anchor_lang::{prelude::*, AnchorDeserialize};
2
3pub const SEED_FEE: &[u8] = b"fee";
4
5/// Escrows the lamport balance owed to a particular worker.
6#[account]
7#[derive(Debug)]
8pub struct Fee {
9    /// The number of lamports that are distributable for this epoch period.
10    pub distributable_balance: u64,
11    /// The worker who received the fees.
12    pub worker: Pubkey,
13}
14
15impl Fee {
16    /// Derive the pubkey of a fee account.
17    pub fn pubkey(worker: Pubkey) -> Pubkey {
18        Pubkey::find_program_address(&[SEED_FEE, worker.as_ref()], &crate::ID).0
19    }
20}
21
22/// Trait for reading and writing to a fee account.
23pub trait FeeAccount {
24    /// Get the pubkey of the fee account.
25    fn pubkey(&self) -> Pubkey;
26
27    /// Initialize the account to hold fee object.
28    fn init(&mut self, worker: Pubkey) -> Result<()>;
29}
30
31impl FeeAccount for Account<'_, Fee> {
32    fn pubkey(&self) -> Pubkey {
33        Fee::pubkey(self.worker)
34    }
35
36    fn init(&mut self, worker: Pubkey) -> Result<()> {
37        self.distributable_balance = 0;
38        self.worker = worker;
39        Ok(())
40    }
41}