diff --git a/examples/management_canister/src/index.did b/examples/management_canister/src/index.did index 1c11a82a54..ba311bf24e 100644 --- a/examples/management_canister/src/index.did +++ b/examples/management_canister/src/index.did @@ -7,6 +7,7 @@ service: () -> { executeStopCanister: (principal) -> (bool); executeUninstallCode: (principal) -> (bool); executeUpdateSettings: (principal) -> (bool); + getCanisterInfo: (record {canister_id:principal; num_requested_changes:opt nat64}) -> (record {controllers:vec principal; module_hash:opt vec nat8; recent_changes:vec record {timestamp_nanos:nat64; canister_version:nat64; origin:variant {from_user:record {user_id:principal}; from_canister:record {canister_version:opt nat64; canister_id:principal}}; details:variant {creation:record {controllers:vec principal}; code_deployment:record {mode:variant {reinstall; upgrade; install}; module_hash:vec nat8}; controllers_change:record {controllers:vec principal}; code_uninstall}}; total_num_changes:nat64}); getCanisterStatus: (record {canister_id:principal}) -> (record {status:variant {stopped; stopping; running}; memory_size:nat; cycles:nat; settings:record {freezing_threshold:nat; controllers:vec principal; memory_allocation:nat; compute_allocation:nat}; module_hash:opt vec nat8}); getCreatedCanisterId: () -> (principal) query; getRawRand: () -> (vec nat8); diff --git a/examples/management_canister/src/index.ts b/examples/management_canister/src/index.ts index 4e7027998c..9f534bb565 100644 --- a/examples/management_canister/src/index.ts +++ b/examples/management_canister/src/index.ts @@ -13,6 +13,8 @@ import { update } from 'azle'; import { + CanisterInfoArgs, + CanisterInfoResult, CanisterStatusArgs, CanisterStatusResult, CreateCanisterResult, @@ -113,6 +115,21 @@ export default Canister({ return true; }), + getCanisterInfo: update( + [CanisterInfoArgs], + CanisterInfoResult, + async (args) => { + const result = await ic.call(managementCanister.canister_info, { + args: [ + { + canister_id: args.canister_id, + num_requested_changes: args.num_requested_changes + } + ] + }); + return result; + } + ), getCanisterStatus: update( [CanisterStatusArgs], CanisterStatusResult, diff --git a/examples/management_canister/test/tests.ts b/examples/management_canister/test/tests.ts index 33a1c3b044..ad06213c6e 100644 --- a/examples/management_canister/test/tests.ts +++ b/examples/management_canister/test/tests.ts @@ -1,5 +1,5 @@ import { ActorSubclass } from '@dfinity/agent'; -import { ok, Test } from 'azle/test'; +import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/management_canister/management_canister.did'; import { readFileSync } from 'fs'; @@ -192,6 +192,26 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { }; } }, + { + name: 'getCanisterInfo', + test: async () => { + const canisterId = + await managementCanister.getCreatedCanisterId(); + + const canisterInfo = await managementCanister.getCanisterInfo({ + canister_id: canisterId, + num_requested_changes: [50n] + }); + + return { + Ok: + canisterInfo.total_num_changes === 3n && + canisterInfo.recent_changes.length === 3 && + canisterInfo.module_hash.length === 0 && + canisterInfo.controllers.length === 1 + }; + } + }, { name: 'executeDeleteCanister', test: async () => {