Skip to content

Commit

Permalink
Update management canister example
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Oct 2, 2023
1 parent 0256f49 commit 1001791
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/management_canister/src/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions examples/management_canister/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
update
} from 'azle';
import {
CanisterInfoArgs,
CanisterInfoResult,
CanisterStatusArgs,
CanisterStatusResult,
CreateCanisterResult,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion examples/management_canister/test/tests.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 1001791

Please sign in to comment.