miclockwork_network_program/state/
delegation.rs1use anchor_lang::{prelude::*, AnchorDeserialize};
2
3pub const SEED_DELEGATION: &[u8] = b"delegation";
4
5#[account]
7#[derive(Debug)]
8pub struct Delegation {
9 pub authority: Pubkey,
11
12 pub id: u64,
14
15 pub stake_amount: u64,
17
18 pub worker: Pubkey,
20
21 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
35pub 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}