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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use {
crate::state::*,
anchor_lang::{prelude::*, solana_program::{system_program, instruction::Instruction, sysvar}},
std::mem::size_of,
};
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct AdminScheduleHealthCheck<'info> {
#[account(mut, address = config.admin)]
pub admin: Signer<'info>,
#[account(
seeds = [SEED_AUTHORITY],
bump = authority.bump,
owner = crate::ID
)]
pub authority: Account<'info, Authority>,
#[account(address = sysvar::clock::ID)]
pub clock: Sysvar<'info, Clock>,
#[account(
seeds = [SEED_CONFIG],
bump = config.bump,
owner = crate::ID,
)]
pub config: Account<'info, Config>,
#[account(
mut,
seeds = [
SEED_DAEMON,
daemon.owner.as_ref()
],
bump = daemon.bump,
constraint = daemon.owner == authority.key(),
owner = crate::ID,
)]
pub daemon: Account<'info, Daemon>,
#[account(
mut,
seeds = [SEED_HEALTH],
bump = health.bump,
owner = crate::ID,
)]
pub health: Account<'info, Health>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
#[account(
init,
seeds = [
SEED_TASK,
daemon.key().as_ref(),
daemon.task_count.to_be_bytes().as_ref(),
],
bump = bump,
payer = admin,
space = 8 + size_of::<Task>() + 80,
)]
pub task: Account<'info, Task>,
}
pub fn handler(ctx: Context<AdminScheduleHealthCheck>, bump: u8) -> ProgramResult {
let authority = &ctx.accounts.authority;
let clock = &ctx.accounts.clock;
let daemon = &mut ctx.accounts.daemon;
let health = &mut ctx.accounts.health;
let task = &mut ctx.accounts.task;
let now = clock.unix_timestamp as u64;
let execute_at = now.checked_add(1).unwrap();
health.real_time = now;
health.target_time = execute_at;
let health_check_ix = InstructionData::from(
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new_readonly(clock.key(), false),
AccountMeta::new_readonly(authority.key(), false),
AccountMeta::new(daemon.key(), true),
AccountMeta::new(health.key(), false),
],
data: vec![],
}
);
task.daemon = daemon.key();
task.id = daemon.task_count;
task.instruction_data = health_check_ix;
task.status = TaskStatus::Pending;
task.execute_at = execute_at;
task.repeat_every = 1;
task.repeat_until = u64::MAX;
task.bump = bump;
daemon.task_count = daemon.task_count.checked_add(1).unwrap();
Ok(())
}