ic_kit/interfaces/
management.rs

1use serde::Deserialize;
2
3use crate::candid::{CandidType, Nat};
4use crate::interfaces::Method;
5use crate::Principal;
6
7#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
8pub struct WithCanisterId {
9    pub canister_id: Principal,
10}
11
12#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
13pub struct CanisterSettings {
14    pub controllers: Option<Vec<Principal>>,
15    pub compute_allocation: Option<Nat>,
16    pub memory_allocation: Option<Nat>,
17    pub freezing_threshold: Option<Nat>,
18}
19
20#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
21pub struct DefiniteCanisterSettings {
22    pub controllers: Vec<Principal>,
23    pub compute_allocation: Nat,
24    pub memory_allocation: Nat,
25    pub freezing_threshold: Nat,
26}
27
28/// Create a canister on the current subnet.
29pub struct CreateCanister;
30
31#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
32pub struct CreateCanisterArgument {
33    pub settings: Option<CanisterSettings>,
34}
35
36impl Method for CreateCanister {
37    const NAME: &'static str = "create_canister";
38    type Arguments = (CreateCanisterArgument,);
39    type Response = (WithCanisterId,);
40}
41
42/// Update the settings of a canister.
43pub struct UpdateSettings;
44
45#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
46pub struct UpdateSettingsArgument {
47    pub canister_id: Principal,
48    pub settings: CanisterSettings,
49}
50
51impl Method for UpdateSettings {
52    const NAME: &'static str = "update_settings";
53    type Arguments = (UpdateSettingsArgument,);
54    type Response = ();
55}
56
57/// Installs the given WASM module on the canister.
58pub struct InstallCode;
59
60#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
61pub enum InstallMode {
62    #[serde(rename = "install")]
63    Install,
64    #[serde(rename = "reinstall")]
65    Reinstall,
66    #[serde(rename = "upgrade")]
67    Upgrade,
68}
69
70#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
71pub struct InstallCodeArgument {
72    pub mode: InstallMode,
73    pub canister_id: Principal,
74    #[serde(with = "serde_bytes")]
75    pub wasm_module: Vec<u8>,
76    pub arg: Vec<u8>,
77}
78
79impl Method for InstallCode {
80    const NAME: &'static str = "install_code";
81    type Arguments = (InstallCodeArgument,);
82    type Response = ();
83}
84
85/// Uninstall the code for the given canister.
86pub struct UninstallCode;
87
88impl Method for UninstallCode {
89    const NAME: &'static str = "uninstall_code";
90    type Arguments = (WithCanisterId,);
91    type Response = ();
92}
93
94/// Start the canister.
95pub struct StartCanister;
96
97impl Method for StartCanister {
98    const NAME: &'static str = "start_canister";
99    type Arguments = (WithCanisterId,);
100    type Response = ();
101}
102
103/// Stop the canister.
104pub struct StopCanister;
105
106impl Method for StopCanister {
107    const NAME: &'static str = "stop_canister";
108    type Arguments = (WithCanisterId,);
109    type Response = ();
110}
111
112/// Query the status of a canister.
113pub struct CanisterStatus;
114
115#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
116pub enum Status {
117    #[serde(rename = "running")]
118    Running,
119    #[serde(rename = "stopping")]
120    Stopping,
121    #[serde(rename = "stopped")]
122    Stopped,
123}
124
125#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
126pub struct CanisterStatusResponse {
127    pub status: Status,
128    pub settings: DefiniteCanisterSettings,
129    pub module_hash: Option<Vec<u8>>,
130    pub memory_size: Nat,
131    pub cycles: Nat,
132}
133
134impl Method for CanisterStatus {
135    const NAME: &'static str = "canister_status";
136    type Arguments = (WithCanisterId,);
137    type Response = (CanisterStatusResponse,);
138}
139
140/// Delete the canister.
141pub struct DeleteCanister;
142
143impl Method for DeleteCanister {
144    const NAME: &'static str = "delete_canister";
145    type Arguments = (WithCanisterId,);
146    type Response = ();
147}
148
149/// Deposit the cycles in the call to the given canister's balance.
150pub struct DepositCycles;
151
152impl Method for DepositCycles {
153    const NAME: &'static str = "deposit_cycles";
154    type Arguments = (WithCanisterId,);
155    type Response = ();
156}
157
158/// Return 32 bytes of random data.
159pub struct RawRand;
160
161impl Method for RawRand {
162    const NAME: &'static str = "raw_rand";
163    type Arguments = ();
164    type Response = (Vec<u8>,);
165}
166
167/// Create a canister on dev versions of the I.C.
168pub struct ProvisionalCreateCanisterWithCycles;
169
170#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
171pub struct ProvisionalCreateCanisterWithCyclesArgument {
172    pub amount: Option<Nat>,
173    pub settings: Option<CanisterSettings>,
174}
175
176impl Method for ProvisionalCreateCanisterWithCycles {
177    const NAME: &'static str = "provisional_create_canister_with_cycles";
178    type Arguments = (ProvisionalCreateCanisterWithCyclesArgument,);
179    type Response = (WithCanisterId,);
180}
181
182/// Top up a canister with the given amount of cycles on dev versions of I.C.
183pub struct ProvisionalTopUpCanister;
184
185#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)]
186pub struct ProvisionalTopUpCanisterArgument {
187    pub canister_id: Principal,
188    pub amount: Nat,
189}
190
191impl Method for ProvisionalTopUpCanister {
192    const NAME: &'static str = "provisional_top_up_canister";
193    type Arguments = (ProvisionalTopUpCanisterArgument,);
194    type Response = ();
195}