I am trying to use the ic_cdk
library to check a canister’s status. For example, consider a simple project, with one canister that has a single async update
method. This function uses ic_cdk::api::management_canister::main::canister_status
in order to check the canister’s status.
dfx.json
{
"canisters": {
"my_canister": {
"candid": "src/my_canister/my_canister.did",
"package": "my_canister",
"type": "rust"
}
}
}
src/my_canister/src/lib.rs
use ic_cdk::{
api::{
call::RejectionCode,
management_canister::{
main::{canister_status, CanisterStatusResponse},
provisional::CanisterIdRecord,
},
},
export_candid, update,
};
#[update]
async fn check_status() -> Result<CanisterStatusResponse, (RejectionCode, String)> {
let val = canister_status(CanisterIdRecord {
canister_id: ic_cdk::id(),
})
.await?;
Ok(val.0)
}
export_candid!();
After starting dfx and deploying the canister (dfx start --clean --background && dfx deploy
), the status can be checked in two ways:
- calling the management canister
- calling the canister that was just created
check_status_management:
dfx canister call aaaaa-aa canister_status \
'(record { canister_id = principal "$(shell dfx canister id my_canister)" })'
check_status_my_canister:
dfx canister call my_canister check_status
Calling make check_status_management
returns:
(
record {
79_599_772 = principal "ifxlm-aqaaa-multi-pleco-ntrol-lersa-h3ae";
100_394_802 = variant { 3_949_555_199 };
238_856_128 = 2_592_000 : nat;
596_483_356 = vec { record { blob "\00"; 3_091_753_184_950 : nat } };
1_054_895_615 = 1_995_104 : nat;
2_190_693_645 = 3_091_753_184_950 : nat;
2_336_062_691 = record {
79_599_772 = principal "ifxlm-aqaaa-multi-pleco-ntrol-lersa-h3ae";
238_856_128 = 2_592_000 : nat;
570_880_087 = vec {
principal "bnz7o-iuaaa-aaaaa-qaaaa-cai";
principal "oq4q5-pqabo-4pqwj-fdthp-ekg2g-tyh5i-hj54z-pudh7-brk35-koxey-rqe";
};
1_095_112_320 = 5_000_000_000_000 : nat;
3_844_961_758 = 0 : nat;
4_174_053_672 = 0 : nat;
};
2_733_945_392 = 1_568_338 : nat;
2_928_387_969 = opt blob "\92\a2p\be\ed:\aa\94\9c\1b\98S\e6\e1W\7fj\bd\82O\c6\cbQ\d6c\f5\c1+\b8\b6\f1\c6";
4_090_107_140 = 0 : nat;
},
)
Calling make check_status_my_canister
returns:
(
variant {
Err = record {
variant { CanisterError };
"failed to decode canister response as (ic_cdk::api::management_canister::main::types::CanisterStatusResponse,): Fail to decode argument 0";
}
},
)
As you see, the record returned from check_status_management
does in fact resemble CanisterStatusResponse
(the struct returned from canister_status
). However the field names are numbers and not strings. Could that be the issue?