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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::pda::PDA;
use anchor_lang::prelude::*;
use anchor_lang::AccountDeserialize;
use solana_program::instruction::Instruction;
use std::convert::TryFrom;
pub const SEED_TASK: &[u8] = b"task";
#[account]
#[derive(Debug)]
pub struct Task {
pub daemon: Pubkey,
pub int: u128,
pub ix: InstructionData,
pub schedule: TaskSchedule,
pub status: TaskStatus,
pub bump: u8,
}
impl Task {
pub fn pda(daemon: Pubkey, id: u128) -> PDA {
Pubkey::find_program_address(
&[SEED_TASK, daemon.as_ref(), id.to_be_bytes().as_ref()],
&crate::ID,
)
}
}
#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug, PartialEq)]
pub struct InstructionData {
pub program_id: Pubkey,
pub accounts: Vec<AccountMetaData>,
pub data: Vec<u8>,
}
impl std::fmt::Display for InstructionData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{
program_id: {},
accounts: {:?},
data: {:?}
}}",
self.program_id, self.accounts, self.data
)
}
}
#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug, PartialEq)]
pub struct AccountMetaData {
pub pubkey: Pubkey,
pub is_signer: bool,
pub is_writable: bool,
}
impl TryFrom<Vec<u8>> for Task {
type Error = ProgramError;
fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
Task::try_deserialize(&mut data.as_slice())
}
}
impl From<Instruction> for InstructionData {
fn from(instruction: Instruction) -> Self {
InstructionData {
program_id: instruction.program_id,
accounts: instruction
.accounts
.iter()
.map(|a| AccountMetaData {
pubkey: a.pubkey,
is_signer: a.is_signer,
is_writable: a.is_writable,
})
.collect(),
data: instruction.data,
}
}
}
impl From<&InstructionData> for Instruction {
fn from(instruction: &InstructionData) -> Self {
Instruction {
program_id: instruction.program_id,
accounts: instruction
.accounts
.iter()
.map(|a| AccountMeta {
pubkey: a.pubkey,
is_signer: a.is_signer,
is_writable: a.is_writable,
})
.collect(),
data: instruction.data.clone(),
}
}
}
#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy, Debug)]
pub struct TaskSchedule {
pub exec_at: i64,
pub stop_at: i64,
pub recurr: i64,
}
impl std::fmt::Display for TaskSchedule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{ exec_at: {}, stop_at: {}, recurr: {} }}",
self.exec_at, self.stop_at, self.recurr
)
}
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, PartialEq)]
pub enum TaskStatus {
Cancelled,
Done,
Queued,
}
impl std::fmt::Display for TaskStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TaskStatus::Cancelled => write!(f, "cancelled"),
TaskStatus::Done => write!(f, "done"),
TaskStatus::Queued => write!(f, "queued"),
}
}
}