1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use {
anchor_lang::{prelude::*, AnchorDeserialize},
std::convert::TryFrom,
};
pub const SEED_PENALTY: &[u8] = b"penalty";
#[account]
#[derive(Debug)]
pub struct Penalty {
pub worker: Pubkey,
}
impl Penalty {
pub fn pubkey(worker: Pubkey) -> Pubkey {
Pubkey::find_program_address(&[SEED_PENALTY, worker.as_ref()], &crate::ID).0
}
}
impl TryFrom<Vec<u8>> for Penalty {
type Error = Error;
fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Penalty::try_deserialize(&mut data.as_slice())
}
}
pub trait PenaltyAccount {
fn pubkey(&self) -> Pubkey;
fn init(&mut self, worker: Pubkey) -> Result<()>;
}
impl PenaltyAccount for Account<'_, Penalty> {
fn pubkey(&self) -> Pubkey {
Penalty::pubkey(self.worker)
}
fn init(&mut self, worker: Pubkey) -> Result<()> {
self.worker = worker;
Ok(())
}
}