miclockwork_network_program/state/
delegation.rs

1use anchor_lang::{prelude::*, AnchorDeserialize};
2
3pub const SEED_DELEGATION: &[u8] = b"delegation";
4
5/// An account to manage a token holder's stake delegation with a particiular a worker.
6#[account]
7#[derive(Debug)]
8pub struct Delegation {
9    /// The authority of this delegation account.
10    pub authority: Pubkey,
11
12    /// The id of this delegation (auto-incrementing integer relative to worker)
13    pub id: u64,
14
15    /// The number of delegated tokens currently locked with the worker.
16    pub stake_amount: u64,
17
18    /// The worker to delegate stake to.
19    pub worker: Pubkey,
20
21    /// The number of lamports claimable as yield by the authority.
22    pub yield_balance: u64,
23}
24
25impl Delegation {
26    pub fn pubkey(worker: Pubkey, id: u64) -> Pubkey {
27        Pubkey::find_program_address(
28            &[SEED_DELEGATION, worker.as_ref(), id.to_be_bytes().as_ref()],
29            &crate::ID,
30        )
31        .0
32    }
33}
34
35/// DelegationAccount
36pub trait DelegationAccount {
37    fn pubkey(&self) -> Pubkey;
38
39    fn init(&mut self, authority: Pubkey, id: u64, worker: Pubkey) -> Result<()>;
40}
41
42impl DelegationAccount for Account<'_, Delegation> {
43    fn pubkey(&self) -> Pubkey {
44        Delegation::pubkey(self.worker, self.id)
45    }
46
47    fn init(&mut self, authority: Pubkey, id: u64, worker: Pubkey) -> Result<()> {
48        self.authority = authority;
49        self.id = id;
50        self.stake_amount = 0;
51        self.worker = worker;
52        self.yield_balance = 0;
53        Ok(())
54    }
55}