Repository navigation
Expand file tree
/
Copy paththread.rs
More file actions
162 lines (152 loc) · 5.73 KB
/
Copy paththread.rs
File metadata and controls
162 lines (152 loc) · 5.73 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use anchor_lang::{
solana_program::{instruction::Instruction, system_program},
InstructionData, AccountDeserialize, ToAccountMetas
};
use clockwork_thread_program::state::{SerializableInstruction, Thread, ThreadSettings, Trigger, VersionedThread};
use clockwork_utils::CrateInfo;
use solana_sdk::pubkey::Pubkey;
use crate::{client::Client, errors::CliError};
pub fn crate_info(client: &Client) -> Result<(), CliError> {
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::GetCrateInfo {
system_program: system_program::ID,
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::GetCrateInfo {}.data(),
};
let crate_info: CrateInfo = client.get_return_data(ix).unwrap();
println!("{:#?}", crate_info);
Ok(())
}
pub fn create(
client: &Client,
id: String,
instructions: Vec<SerializableInstruction>,
trigger: Trigger,
) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.clone().into_bytes());
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadCreate {
authority: client.payer_pubkey(),
payer: client.payer_pubkey(),
system_program: system_program::ID,
thread: thread_pubkey
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadCreate {
amount: 0,
id: id.into_bytes(),
instructions,
trigger,
}
.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
get(client, thread_pubkey)?;
Ok(())
}
pub fn delete(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadDelete {
authority: client.payer_pubkey(),
close_to: client.payer_pubkey(),
thread: thread_pubkey,
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadDelete {}.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
Ok(())
}
pub fn get(client: &Client, address: Pubkey) -> Result<(), CliError> {
let data = client.get_account_data(&address).unwrap();
let thread = VersionedThread::try_deserialize(&mut data.as_slice()).unwrap();
println!("Address: {}\n{:#?}", address, thread);
Ok(())
}
pub fn pause(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadPause {
authority: client.payer_pubkey(),
thread: thread_pubkey,
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadPause {}.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
get(client, thread_pubkey)?;
Ok(())
}
pub fn resume(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadResume {
authority: client.payer_pubkey(),
thread: thread_pubkey
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadResume {}.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
get(client, thread_pubkey)?;
Ok(())
}
pub fn reset(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadReset {
authority: client.payer_pubkey(),
thread: thread_pubkey
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadReset {}.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
get(client, thread_pubkey)?;
Ok(())
}
pub fn update(
client: &Client,
id: String,
rate_limit: Option<u64>,
schedule: Option<String>,
) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let trigger = if let Some(schedule) = schedule {
Some(Trigger::Cron {
schedule,
skippable: true,
})
} else {
None
};
let settings = ThreadSettings {
fee: None,
instructions: None,
name: None,
rate_limit,
trigger,
};
let ix = Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadUpdate {
authority: client.payer_pubkey(),
system_program: system_program::ID,
thread: thread_pubkey
}.to_account_metas(Some(false)),
data: clockwork_thread_program::instruction::ThreadUpdate { settings }.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
get(client, thread_pubkey)?;
Ok(())
}
pub fn parse_pubkey_from_id_or_address(
authority: Pubkey,
id: Option<String>,
address: Option<Pubkey>,
) -> Result<Pubkey, CliError> {
let address_from_id = id.map(|str| Thread::pubkey(authority, str.into()));
address.or(address_from_id).ok_or(CliError::InvalidAddress)
}