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
use async_trait::async_trait;
use ic_cdk::api::call::{call, call_with_payment, CallResult};
use ic_cdk::export::candid::{CandidType, Deserialize, Principal};

#[async_trait]
pub trait IManagementCanisterClient {
    async fn create_canister(
        &self,
        req: CreateCanisterRequest,
        cycles: u64,
    ) -> CallResult<(CreateCanisterResponse,)>;
    async fn install_code(&self, req: InstallCodeRequest) -> CallResult<()>;
    async fn update_settings(&self, req: UpdateSettingsRequest) -> CallResult<()>;
}

#[async_trait]
impl IManagementCanisterClient for Principal {
    async fn create_canister(
        &self,
        req: CreateCanisterRequest,
        cycles: u64,
    ) -> CallResult<(CreateCanisterResponse,)> {
        call_with_payment(*self, "create_canister", (req,), cycles).await
    }

    async fn install_code(&self, req: InstallCodeRequest) -> CallResult<()> {
        call(*self, "install_code", (req,)).await
    }

    async fn update_settings(&self, req: UpdateSettingsRequest) -> CallResult<()> {
        call(*self, "update_settings", (req,)).await
    }
}

#[derive(Clone, CandidType, Deserialize)]
pub struct DeployCanisterSettings {
    pub controller: Option<Principal>,
    pub compute_allocation: Option<u64>,
    pub memory_allocation: Option<u64>,
    pub freezing_threshold: Option<u64>,
}

#[derive(Clone, CandidType, Deserialize)]
pub enum CanisterInstallMode {
    install,
    reinstall,
    upgrade,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct CreateCanisterRequest {
    pub settings: Option<DeployCanisterSettings>,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct CreateCanisterResponse {
    pub canister_id: Principal,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct InstallCodeRequest {
    pub canister_id: Principal,
    pub mode: CanisterInstallMode,
    pub wasm_module: Vec<u8>,
    pub arg: Vec<u8>,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct CanisterSettings {
    pub controllers: Vec<Principal>,
}

#[derive(Clone, CandidType, Deserialize)]
pub struct UpdateSettingsRequest {
    pub canister_id: Principal,
    pub settings: CanisterSettings,
}