use anchor_lang::{prelude::*, AnchorDeserialize};
pub const SEED_DELEGATION: &[u8] = b"delegation";
#[account]
#[derive(Debug)]
pub struct Delegation {
pub authority: Pubkey,
pub id: u64,
pub stake_amount: u64,
pub worker: Pubkey,
pub yield_balance: u64,
}
impl Delegation {
pub fn pubkey(worker: Pubkey, id: u64) -> Pubkey {
Pubkey::find_program_address(
&[SEED_DELEGATION, worker.as_ref(), id.to_be_bytes().as_ref()],
&crate::ID,
)
.0
}
}
pub trait DelegationAccount {
fn pubkey(&self) -> Pubkey;
fn init(&mut self, authority: Pubkey, id: u64, worker: Pubkey) -> Result<()>;
}
impl DelegationAccount for Account<'_, Delegation> {
fn pubkey(&self) -> Pubkey {
Delegation::pubkey(self.worker, self.id)
}
fn init(&mut self, authority: Pubkey, id: u64, worker: Pubkey) -> Result<()> {
self.authority = authority;
self.id = id;
self.stake_amount = 0;
self.worker = worker;
self.yield_balance = 0;
Ok(())
}
}