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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::Config;
use crate::pda::PDA;
use anchor_lang::prelude::*;
use anchor_lang::AccountDeserialize;
use std::convert::TryFrom;
pub const SEED_HEALTH: &[u8] = b"health";
#[account]
#[derive(Debug)]
pub struct Health {
pub last_ping: i64,
pub target_ping: i64,
pub bump: u8,
}
impl Health {
pub fn pda() -> PDA {
Pubkey::find_program_address(&[SEED_HEALTH], &crate::ID)
}
}
impl TryFrom<Vec<u8>> for Health {
type Error = Error;
fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Health::try_deserialize(&mut data.as_slice())
}
}
pub trait HealthAccount {
fn init(&mut self, bump: u8) -> Result<()>;
fn ping(&mut self, clock: &Sysvar<Clock>, config: &Account<Config>) -> Result<()>;
fn reset(&mut self, clock: &Sysvar<Clock>) -> Result<()>;
}
impl HealthAccount for Account<'_, Health> {
fn init(&mut self, bump: u8) -> Result<()> {
self.last_ping = 0;
self.target_ping = 0;
self.bump = bump;
Ok(())
}
fn ping(&mut self, clock: &Sysvar<Clock>, config: &Account<Config>) -> Result<()> {
self.last_ping = clock.unix_timestamp;
self.target_ping = self.target_ping.checked_add(config.min_recurr).unwrap();
Ok(())
}
fn reset(&mut self, clock: &Sysvar<Clock>) -> Result<()> {
self.last_ping = clock.unix_timestamp;
self.target_ping = clock.unix_timestamp;
Ok(())
}
}