ic-utils 0.47.1

Collection of utilities for Rust, on top of ic-agent, to communicate with the Internet Computer, following the Public Specification.
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! The canister interface for the IC management canister. See the [specification][spec] for full documentation of the interface.
//!
//! [spec]: https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-management-canister

use crate::{
    call::{AsyncCall, SyncCall},
    Canister,
};
use ic_agent::{export::Principal, Agent};
use ic_management_canister_types::{
    CanisterIdRecord, DeleteCanisterSnapshotArgs, LoadCanisterSnapshotArgs,
    ProvisionalTopUpCanisterArgs, ReadCanisterSnapshotDataArgs, ReadCanisterSnapshotMetadataArgs,
    TakeCanisterSnapshotArgs, UploadCanisterSnapshotDataArgs, UploadCanisterSnapshotMetadataArgs,
    UploadChunkArgs,
};
// Re-export the types that are used be defined in this file.
pub use ic_management_canister_types::{
    CanisterLogFilter, CanisterLogRecord, CanisterMetadataArgs, CanisterMetadataResult,
    CanisterStatusResult, CanisterStatusType, CanisterTimer, ChunkHash, DefiniteCanisterSettings,
    FetchCanisterLogsArgs, FetchCanisterLogsResult, LogVisibility, MemoryMetrics,
    OnLowWasmMemoryHookStatus, QueryStats, ReadCanisterSnapshotDataResult,
    ReadCanisterSnapshotMetadataResult, RenameCanisterRecord, RenameToRecord, Snapshot,
    SnapshotDataKind, SnapshotDataOffset, SnapshotMetadataGlobal, SnapshotSource,
    StoredChunksResult, UploadCanisterSnapshotMetadataResult, UploadChunkResult,
};
use std::{convert::AsRef, ops::Deref};
use strum_macros::{AsRefStr, Display, EnumString};

pub mod attributes;
pub mod builders;

#[doc(inline)]
pub use builders::{
    CreateCanisterBuilder, InstallBuilder, InstallChunkedCodeBuilder, InstallCodeBuilder,
    UpdateSettingsBuilder,
};

/// The IC management canister.
#[derive(Debug, Clone)]
pub struct ManagementCanister<'agent>(Canister<'agent>);

impl<'agent> Deref for ManagementCanister<'agent> {
    type Target = Canister<'agent>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// All the known methods of the management canister.
#[derive(AsRefStr, Debug, EnumString, Display)]
#[strum(serialize_all = "snake_case")]
pub enum MgmtMethod {
    /// See [`ManagementCanister::create_canister`].
    CreateCanister,
    /// See [`ManagementCanister::install_code`].
    InstallCode,
    /// See [`ManagementCanister::start_canister`].
    StartCanister,
    /// See [`ManagementCanister::stop_canister`].
    StopCanister,
    /// See [`ManagementCanister::canister_status`].
    CanisterStatus,
    /// See [`ManagementCanister::delete_canister`].
    DeleteCanister,
    /// See [`ManagementCanister::deposit_cycles`].
    DepositCycles,
    /// See [`ManagementCanister::raw_rand`].
    RawRand,
    /// See [`CreateCanisterBuilder::as_provisional_create_with_amount`].
    ProvisionalCreateCanisterWithCycles,
    /// See [`ManagementCanister::provisional_top_up_canister`].
    ProvisionalTopUpCanister,
    /// See [`ManagementCanister::uninstall_code`].
    UninstallCode,
    /// See [`ManagementCanister::update_settings`].
    UpdateSettings,
    /// See [`ManagementCanister::upload_chunk`].
    UploadChunk,
    /// See [`ManagementCanister::clear_chunk_store`].
    ClearChunkStore,
    /// See [`ManagementCanister::stored_chunks`].
    StoredChunks,
    /// See [`ManagementCanister::install_chunked_code`].
    InstallChunkedCode,
    /// See [`ManagementCanister::fetch_canister_logs`].
    FetchCanisterLogs,
    /// See [`ManagementCanister::take_canister_snapshot`].
    TakeCanisterSnapshot,
    /// See [`ManagementCanister::load_canister_snapshot`].
    LoadCanisterSnapshot,
    /// See [`ManagementCanister::list_canister_snapshots`].
    ListCanisterSnapshots,
    /// See [`ManagementCanister::delete_canister_snapshot`].
    DeleteCanisterSnapshot,
    /// See [`ManagementCanister::read_canister_snapshot_metadata`].
    ReadCanisterSnapshotMetadata,
    /// See [`ManagementCanister::read_canister_snapshot_data`].
    ReadCanisterSnapshotData,
    /// See [`ManagementCanister::upload_canister_snapshot_metadata`].
    UploadCanisterSnapshotMetadata,
    /// See [`ManagementCanister::upload_canister_snapshot_data`].
    UploadCanisterSnapshotData,
    /// There is no corresponding agent function as only canisters can call it.
    EcdsaPublicKey,
    /// There is no corresponding agent function as only canisters can call it.
    SignWithEcdsa,
    /// There is no corresponding agent function as only canisters can call it. Use [`BitcoinCanister`](super::BitcoinCanister) instead.
    BitcoinGetBalance,
    /// There is no corresponding agent function as only canisters can call it. Use [`BitcoinCanister`](super::BitcoinCanister) instead.
    BitcoinGetUtxos,
    /// There is no corresponding agent function as only canisters can call it. Use [`BitcoinCanister`](super::BitcoinCanister) instead.
    BitcoinSendTransaction,
    /// There is no corresponding agent function as only canisters can call it. Use [`BitcoinCanister`](super::BitcoinCanister) instead.
    BitcoinGetCurrentFeePercentiles,
    /// There is no corresponding agent function as only canisters can call it. Use [`BitcoinCanister`](super::BitcoinCanister) instead.
    BitcoinGetBlockHeaders,
    /// There is no corresponding agent function as only canisters can call it.
    NodeMetricsHistory,
    /// There is no corresponding agent function as only canisters can call it.
    CanisterInfo,
    /// See [`ManagementCanister::canister_metadata`].
    CanisterMetadata,
}

impl<'agent> ManagementCanister<'agent> {
    /// Create an instance of a `ManagementCanister` interface pointing to the specified Canister ID.
    pub fn create(agent: &'agent Agent) -> Self {
        Self(
            Canister::builder()
                .with_agent(agent)
                .with_canister_id(Principal::management_canister())
                .build()
                .unwrap(),
        )
    }

    /// Create a `ManagementCanister` interface from an existing canister object.
    pub fn from_canister(canister: Canister<'agent>) -> Self {
        Self(canister)
    }
}

#[doc(hidden)]
#[deprecated(since = "0.42.0", note = "Please use CanisterStatusResult instead")]
pub type StatusCallResult = CanisterStatusResult;

#[doc(hidden)]
#[deprecated(since = "0.42.0", note = "Please use CanisterStatusType instead")]
pub type CanisterStatus = CanisterStatusType;

#[doc(hidden)]
#[deprecated(since = "0.42.0", note = "Please use FetchCanisterLogsResult instead")]
pub type FetchCanisterLogsResponse = FetchCanisterLogsResult;

#[doc(hidden)]
#[deprecated(since = "0.42.0", note = "Please use StoredChunksResult instead")]
pub type StoreChunksResult = StoredChunksResult;

#[doc(hidden)]
#[deprecated(
    since = "0.42.0",
    note = "Please use ReadCanisterSnapshotMetadataResult instead"
)]
pub type SnapshotMetadata = ReadCanisterSnapshotMetadataResult;

#[doc(hidden)]
#[deprecated(
    since = "0.42.0",
    note = "Please use ReadCanisterSnapshotDataResult instead"
)]
pub type SnapshotDataResult = ReadCanisterSnapshotDataResult;

#[doc(hidden)]
#[deprecated(
    since = "0.42.0",
    note = "Please use UploadCanisterSnapshotMetadataResult instead"
)]
pub type CanisterSnapshotId = UploadCanisterSnapshotMetadataResult;

impl<'agent> ManagementCanister<'agent> {
    /// Get the status of a canister.
    pub fn canister_status(
        &self,
        canister_id: &Principal,
    ) -> impl 'agent + AsyncCall<Value = (CanisterStatusResult,)> {
        self.update(MgmtMethod::CanisterStatus.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
            .map(|result: (CanisterStatusResult,)| (result.0,))
    }

    /// Create a canister.
    pub fn create_canister<'canister>(&'canister self) -> CreateCanisterBuilder<'agent, 'canister> {
        CreateCanisterBuilder::builder(self)
    }

    /// This method deposits the cycles included in this call into the specified canister.
    /// Only the controller of the canister can deposit cycles.
    pub fn deposit_cycles(&self, canister_id: &Principal) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::DepositCycles.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// Deletes a canister.
    pub fn delete_canister(&self, canister_id: &Principal) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::DeleteCanister.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// Until developers can convert real ICP tokens to a top up an existing canister,
    /// the system provides the `provisional_top_up_canister` method.
    /// It adds amount cycles to the balance of canister identified by amount
    /// (implicitly capping it at `MAX_CANISTER_BALANCE`).
    pub fn provisional_top_up_canister(
        &self,
        canister_id: &Principal,
        top_up_args: &ProvisionalTopUpCanisterArgs,
    ) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::ProvisionalTopUpCanister.as_ref())
            .with_arg(top_up_args)
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// This method takes no input and returns 32 pseudo-random bytes to the caller.
    /// The return value is unknown to any part of the IC at time of the submission of this call.
    /// A new return value is generated for each call to this method.
    pub fn raw_rand(&self) -> impl 'agent + AsyncCall<Value = (Vec<u8>,)> {
        self.update(MgmtMethod::RawRand.as_ref())
            .build()
            .map(|result: (Vec<u8>,)| (result.0,))
    }

    /// Starts a canister.
    pub fn start_canister(&self, canister_id: &Principal) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::StartCanister.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// Stop a canister.
    pub fn stop_canister(&self, canister_id: &Principal) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::StopCanister.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// This method removes a canister’s code and state, making the canister empty again.
    /// Only the controller of the canister can uninstall code.
    /// Uninstalling a canister’s code will reject all calls that the canister has not yet responded to,
    /// and drop the canister’s code and state.
    /// Outstanding responses to the canister will not be processed, even if they arrive after code has been installed again.
    /// The canister is now empty. In particular, any incoming or queued calls will be rejected.
    //// A canister after uninstalling retains its cycles balance, controller, status, and allocations.
    pub fn uninstall_code(&self, canister_id: &Principal) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::UninstallCode.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(canister_id.to_owned())
            .build()
    }

    /// Install a canister, with all the arguments necessary for creating the canister.
    pub fn install_code<'canister>(
        &'canister self,
        canister_id: &Principal,
        wasm: &'canister [u8],
    ) -> InstallCodeBuilder<'agent, 'canister> {
        InstallCodeBuilder::builder(self, canister_id, wasm)
    }

    /// Update one or more of a canisters settings (i.e its controller, compute allocation, or memory allocation.)
    pub fn update_settings<'canister>(
        &'canister self,
        canister_id: &Principal,
    ) -> UpdateSettingsBuilder<'agent, 'canister> {
        UpdateSettingsBuilder::builder(self, canister_id)
    }

    /// Upload a chunk of a WASM module to a canister's chunked WASM storage.
    pub fn upload_chunk(
        &self,
        canister_id: &Principal,
        upload_chunk_args: &UploadChunkArgs,
    ) -> impl 'agent + AsyncCall<Value = (UploadChunkResult,)> {
        self.update(MgmtMethod::UploadChunk.as_ref())
            .with_arg(upload_chunk_args)
            .with_effective_canister_id(*canister_id)
            .build()
    }

    /// Clear a canister's chunked WASM storage.
    pub fn clear_chunk_store(
        &self,
        canister_id: &Principal,
    ) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::ClearChunkStore.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(*canister_id)
            .build()
    }

    /// Get a list of the hashes of a canister's stored WASM chunks
    pub fn stored_chunks(
        &self,
        canister_id: &Principal,
    ) -> impl 'agent + AsyncCall<Value = (StoredChunksResult,)> {
        self.update(MgmtMethod::StoredChunks.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(*canister_id)
            .build()
    }

    /// Install a canister module previously uploaded in chunks via [`upload_chunk`](Self::upload_chunk).
    pub fn install_chunked_code<'canister>(
        &'canister self,
        canister_id: &Principal,
        wasm_module_hash: &[u8],
    ) -> InstallChunkedCodeBuilder<'agent, 'canister> {
        InstallChunkedCodeBuilder::builder(self, *canister_id, wasm_module_hash)
    }

    /// Install a canister module, automatically selecting one-shot installation or chunked installation depending on module size.
    ///
    /// # Warnings
    ///
    /// This will clear chunked code storage if chunked installation is used. Do not use with canisters that you are manually uploading chunked code to.
    pub fn install<'canister: 'builder, 'builder>(
        &'canister self,
        canister_id: &Principal,
        wasm: &'builder [u8],
    ) -> InstallBuilder<'agent, 'canister, 'builder> {
        InstallBuilder::builder(self, canister_id, wasm)
    }

    /// Fetch (query) the logs of a canister.
    pub fn fetch_canister_logs(
        &self,
        args: &FetchCanisterLogsArgs,
    ) -> impl 'agent + SyncCall<Value = (FetchCanisterLogsResult,)> {
        self.query(MgmtMethod::FetchCanisterLogs.as_ref())
            .with_arg(args)
            .with_effective_canister_id(args.canister_id)
            .build()
    }

    /// Creates a canister snapshot, optionally replacing an existing snapshot.
    ///  
    /// <div class="warning">Canisters should be stopped before running this method!</div>
    pub fn take_canister_snapshot(
        &self,
        take_args: &TakeCanisterSnapshotArgs,
    ) -> impl 'agent + AsyncCall<Value = (Snapshot,)> {
        let args = TakeCanisterSnapshotArgs {
            sender_canister_version: None,
            ..take_args.clone()
        };
        self.update(MgmtMethod::TakeCanisterSnapshot.as_ref())
            .with_arg(args)
            .with_effective_canister_id(take_args.canister_id)
            .build()
    }

    /// Loads a canister snapshot by ID, replacing the canister's state with its state at the time the snapshot was taken.
    ///
    /// <div class="warning">Canisters should be stopped before running this method!</div>
    pub fn load_canister_snapshot(
        &self,
        load_args: &LoadCanisterSnapshotArgs,
    ) -> impl 'agent + AsyncCall<Value = ()> {
        let args = LoadCanisterSnapshotArgs {
            sender_canister_version: None,
            ..load_args.clone()
        };
        self.update(MgmtMethod::LoadCanisterSnapshot.as_ref())
            .with_arg(args)
            .with_effective_canister_id(load_args.canister_id)
            .build()
    }

    /// List a canister's recorded snapshots.
    pub fn list_canister_snapshots(
        &self,
        canister_id: &Principal,
    ) -> impl 'agent + AsyncCall<Value = (Vec<Snapshot>,)> {
        self.update(MgmtMethod::ListCanisterSnapshots.as_ref())
            .with_arg(CanisterIdRecord {
                canister_id: *canister_id,
            })
            .with_effective_canister_id(*canister_id)
            .build()
    }

    /// Deletes a recorded canister snapshot by ID.
    pub fn delete_canister_snapshot(
        &self,
        delete_args: &DeleteCanisterSnapshotArgs,
    ) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::DeleteCanisterSnapshot.as_ref())
            .with_arg(delete_args)
            .with_effective_canister_id(delete_args.canister_id)
            .build()
    }

    /// Reads the metadata of a recorded canister snapshot by canister ID and snapshot ID.
    pub fn read_canister_snapshot_metadata(
        &self,
        metadata_args: &ReadCanisterSnapshotMetadataArgs,
    ) -> impl 'agent + AsyncCall<Value = (ReadCanisterSnapshotMetadataResult,)> {
        self.update(MgmtMethod::ReadCanisterSnapshotMetadata.as_ref())
            .with_arg(metadata_args)
            .with_effective_canister_id(metadata_args.canister_id)
            .build()
    }

    /// Reads the data of a recorded canister snapshot by canister ID and snapshot ID.
    pub fn read_canister_snapshot_data(
        &self,
        data_args: &ReadCanisterSnapshotDataArgs,
    ) -> impl 'agent + AsyncCall<Value = (ReadCanisterSnapshotDataResult,)> {
        self.update(MgmtMethod::ReadCanisterSnapshotData.as_ref())
            .with_arg(data_args)
            .with_effective_canister_id(data_args.canister_id)
            .build()
    }

    /// Uploads the metadata of a canister snapshot by canister ID.
    pub fn upload_canister_snapshot_metadata(
        &self,
        metadata_args: &UploadCanisterSnapshotMetadataArgs,
    ) -> impl 'agent + AsyncCall<Value = (UploadCanisterSnapshotMetadataResult,)> {
        self.update(MgmtMethod::UploadCanisterSnapshotMetadata.as_ref())
            .with_arg(metadata_args)
            .with_effective_canister_id(metadata_args.canister_id)
            .build()
    }

    /// Uploads the data of a canister snapshot by canister ID and snapshot ID..
    pub fn upload_canister_snapshot_data(
        &self,
        data_args: &UploadCanisterSnapshotDataArgs,
    ) -> impl 'agent + AsyncCall<Value = ()> {
        self.update(MgmtMethod::UploadCanisterSnapshotData.as_ref())
            .with_arg(data_args)
            .with_effective_canister_id(data_args.canister_id)
            .build()
    }

    /// Fetch a named metadata section of a canister's Wasm module.
    pub fn canister_metadata(
        &self,
        args: &CanisterMetadataArgs,
    ) -> impl 'agent + SyncCall<Value = (CanisterMetadataResult,)> {
        self.query(MgmtMethod::CanisterMetadata.as_ref())
            .with_arg(args)
            .with_effective_canister_id(args.canister_id)
            .build()
    }
}