sablier_network_program/state/
worker.rs

1use anchor_lang::{prelude::*, AnchorDeserialize};
2
3use crate::{constants::SEED_WORKER, errors::*};
4
5/// Worker
6#[account]
7#[derive(Debug, InitSpace)]
8pub struct Worker {
9    /// The worker's authority (owner).
10    pub authority: Pubkey,
11    /// The number of lamports claimable by the authority as commission for running the worker.
12    pub commission_balance: u64,
13    /// Integer between 0 and 100 determining the percentage of fees worker will keep as commission.
14    pub commission_rate: u64,
15    /// The worker's id.
16    pub id: u64,
17    /// The worker's signatory address (used to sign txs).
18    pub signatory: Pubkey,
19    /// The number delegations allocated to this worker.
20    pub total_delegations: u64,
21    pub bump: u8,
22}
23
24impl Worker {
25    pub fn pubkey(id: u64) -> Pubkey {
26        Pubkey::find_program_address(&[SEED_WORKER, id.to_be_bytes().as_ref()], &crate::ID).0
27    }
28}
29
30/// WorkerSettings
31#[derive(AnchorSerialize, AnchorDeserialize)]
32pub struct WorkerSettings {
33    pub commission_rate: u64,
34    pub signatory: Pubkey,
35}
36
37/// WorkerAccount
38pub trait WorkerAccount {
39    fn pubkey(&self) -> Pubkey;
40
41    fn init(&mut self, authority: &mut Signer, id: u64, signatory: &Signer, bump: u8)
42        -> Result<()>;
43
44    fn update(&mut self, settings: WorkerSettings) -> Result<()>;
45}
46
47impl WorkerAccount for Account<'_, Worker> {
48    fn pubkey(&self) -> Pubkey {
49        Worker::pubkey(self.id)
50    }
51
52    fn init(
53        &mut self,
54        authority: &mut Signer,
55        id: u64,
56        signatory: &Signer,
57        bump: u8,
58    ) -> Result<()> {
59        self.authority = authority.key();
60        self.commission_balance = 0;
61        self.commission_rate = 0;
62        self.id = id;
63        self.signatory = signatory.key();
64        self.total_delegations = 0;
65        self.bump = bump;
66        Ok(())
67    }
68
69    fn update(&mut self, settings: WorkerSettings) -> Result<()> {
70        require!(
71            settings.commission_rate > 0 && settings.commission_rate <= 100,
72            SablierError::InvalidCommissionRate
73        );
74        self.commission_rate = settings.commission_rate;
75
76        require!(
77            settings.signatory.ne(&self.authority),
78            SablierError::InvalidSignatory
79        );
80        self.signatory = settings.signatory;
81        Ok(())
82    }
83}