Skip to content

Commit

Permalink
feat: Use full IC Management IDL (#406)
Browse files Browse the repository at this point in the history
* feat: Use full IC Management IDL

* Assert fn prov...with_cycles has type ActorMethod

Co-authored-by: Kyle Peacock <kylpeacock@gmail.com>
  • Loading branch information
megrogan and krpeacock committed Mar 24, 2022
1 parent 58acece commit 9b663c6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 51 deletions.
13 changes: 7 additions & 6 deletions e2e/node/basic/basic.test.ts
@@ -1,7 +1,7 @@
/**
* @jest-environment node
*/
import { Certificate, getManagementCanister } from '@dfinity/agent';
import { ActorMethod, Certificate, getManagementCanister } from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import agent from '../utils/agent';
Expand Down Expand Up @@ -37,23 +37,24 @@ test('createCanister', async () => {
// Make sure this doesn't fail.
await getManagementCanister({
agent: await agent,
}).provisional_create_canister_with_cycles({ amount: [1e12], settings: [] });
}).provisional_create_canister_with_cycles({ amount: [BigInt(1e12)], settings: [] });
});

test('withOptions', async () => {
// Make sure this fails.
await expect(
(async () => {
await getManagementCanister({
const canisterActor = await getManagementCanister({
agent: await agent,
}).provisional_create_canister_with_cycles.withOptions({
});
await (canisterActor.provisional_create_canister_with_cycles as ActorMethod).withOptions({
canisterId: 'abcde-gghhi',
})({ amount: [1e12], settings: [] });
})({ amount: [BigInt(1e12)], settings: [] });
})(),
).rejects.toThrow();

// Make sure this doesn't fail.
await getManagementCanister({
agent: await agent,
}).provisional_create_canister_with_cycles({ amount: [1e12], settings: [] });
}).provisional_create_canister_with_cycles({ amount: [BigInt(1e12)], settings: [] });
});
39 changes: 5 additions & 34 deletions packages/agent/src/canisters/management.ts
@@ -1,45 +1,16 @@
import { Actor, ActorMethod, ActorSubclass, CallConfig } from '../actor';
import { Actor, ActorSubclass, CallConfig } from '../actor';
import { Principal } from '@dfinity/principal';
import managementCanisterIdl from './management_idl';
import _SERVICE from './management_service';

export interface CanisterSettings {
controller: [] | [Principal];
compute_allocation: [] | [bigint];
memory_allocation: [] | [bigint];
freezing_threshold: [] | [bigint];
}

/* tslint:disable */
export interface ManagementCanisterRecord {
provisional_create_canister_with_cycles: ActorMethod<
[
{
amount: [] | [number];
settings: [] | [CanisterSettings];
},
],
{ canister_id: Principal }
>;
install_code: ActorMethod<
[
{
mode: { install: null } | { reinstall: null } | { upgrade: null };
canister_id: Principal;
wasm_module: number[];
arg: number[];
},
],
void
>;
}
/* tslint:enable */
export type ManagementCanisterRecord = _SERVICE;

/**
* Create a management canister actor.
* Create a management canister actor
* @param config
*/
export function getManagementCanister(config: CallConfig): ActorSubclass<ManagementCanisterRecord> {
function transform(methodName: string, args: unknown[], callConfig: CallConfig) {
function transform(_methodName: string, args: unknown[], _callConfig: CallConfig) {
const first = args[0] as any;
let effectiveCanisterId = Principal.fromHex('');
if (first && typeof first === 'object' && first.canister_id) {
Expand Down
76 changes: 65 additions & 11 deletions packages/agent/src/canisters/management_idl.ts
Expand Up @@ -5,32 +5,86 @@
// @ts-ignore
export default ({ IDL }) => {
const canister_id = IDL.Principal;
const wasm_module = IDL.Vec(IDL.Nat8);
const CanisterSettings = IDL.Record({
compute_allocation: IDL.Opt(IDL.Nat),
const definite_canister_settings = IDL.Record({
controllers: IDL.Vec(IDL.Principal),
freezing_threshold: IDL.Nat,
memory_allocation: IDL.Nat,
compute_allocation: IDL.Nat,
});
const canister_settings = IDL.Record({
controllers: IDL.Opt(IDL.Vec(IDL.Principal)),
freezing_threshold: IDL.Opt(IDL.Nat),
memory_allocation: IDL.Opt(IDL.Nat),
compute_allocation: IDL.Opt(IDL.Nat),
});
const wasm_module = IDL.Vec(IDL.Nat8);
return IDL.Service({
provisional_create_canister_with_cycles: IDL.Func(
[IDL.Record({ amount: IDL.Opt(IDL.Nat), settings: IDL.Opt(CanisterSettings) })],
canister_status: IDL.Func(
[IDL.Record({ canister_id: canister_id })],
[
IDL.Record({
status: IDL.Variant({
stopped: IDL.Null,
stopping: IDL.Null,
running: IDL.Null,
}),
memory_size: IDL.Nat,
cycles: IDL.Nat,
settings: definite_canister_settings,
module_hash: IDL.Opt(IDL.Vec(IDL.Nat8)),
}),
],
[],
),
create_canister: IDL.Func(
[IDL.Record({ settings: IDL.Opt(canister_settings) })],
[IDL.Record({ canister_id: canister_id })],
[],
),
create_canister: IDL.Func([], [IDL.Record({ canister_id: canister_id })], []),
delete_canister: IDL.Func([IDL.Record({ canister_id: canister_id })], [], []),
deposit_cycles: IDL.Func([IDL.Record({ canister_id: canister_id })], [], []),
install_code: IDL.Func(
[
IDL.Record({
mode: IDL.Variant({ install: IDL.Null, reinstall: IDL.Null, upgrade: IDL.Null }),
canister_id: canister_id,
wasm_module: wasm_module,
arg: IDL.Vec(IDL.Nat8),
wasm_module: wasm_module,
mode: IDL.Variant({
reinstall: IDL.Null,
upgrade: IDL.Null,
install: IDL.Null,
}),
canister_id: canister_id,
}),
],
[],
[],
),
provisional_create_canister_with_cycles: IDL.Func(
[
IDL.Record({
settings: IDL.Opt(canister_settings),
amount: IDL.Opt(IDL.Nat),
}),
],
[IDL.Record({ canister_id: canister_id })],
[],
),
provisional_top_up_canister: IDL.Func(
[IDL.Record({ canister_id: canister_id, amount: IDL.Nat })],
[],
[],
),
set_controller: IDL.Func(
[IDL.Record({ canister_id: canister_id, new_controller: IDL.Principal })],
raw_rand: IDL.Func([], [IDL.Vec(IDL.Nat8)], []),
start_canister: IDL.Func([IDL.Record({ canister_id: canister_id })], [], []),
stop_canister: IDL.Func([IDL.Record({ canister_id: canister_id })], [], []),
uninstall_code: IDL.Func([IDL.Record({ canister_id: canister_id })], [], []),
update_settings: IDL.Func(
[
IDL.Record({
canister_id: IDL.Principal,
settings: canister_settings,
}),
],
[],
[],
),
Expand Down
57 changes: 57 additions & 0 deletions packages/agent/src/canisters/management_service.ts
@@ -0,0 +1,57 @@
/**
* This file is generated from the candid for asset management.
*/
/* tslint:disable */
// @ts-ignore
import type { Principal } from '@dfinity/principal';
export type canister_id = Principal;
export interface canister_settings {
controllers: [] | [Array<Principal>];
freezing_threshold: [] | [bigint];
memory_allocation: [] | [bigint];
compute_allocation: [] | [bigint];
}
export interface definite_canister_settings {
controllers: Array<Principal>;
freezing_threshold: bigint;
memory_allocation: bigint;
compute_allocation: bigint;
}
export type user_id = Principal;
export type wasm_module = Array<number>;
export default interface _SERVICE {
canister_status: (arg_0: { canister_id: canister_id }) => Promise<{
status: { stopped: null } | { stopping: null } | { running: null };
memory_size: bigint;
cycles: bigint;
settings: definite_canister_settings;
module_hash: [] | [Array<number>];
}>;
create_canister: (arg_0: {
settings: [] | [canister_settings];
}) => Promise<{ canister_id: canister_id }>;
delete_canister: (arg_0: { canister_id: canister_id }) => Promise<undefined>;
deposit_cycles: (arg_0: { canister_id: canister_id }) => Promise<undefined>;
install_code: (arg_0: {
arg: Array<number>;
wasm_module: wasm_module;
mode: { reinstall: null } | { upgrade: null } | { install: null };
canister_id: canister_id;
}) => Promise<undefined>;
provisional_create_canister_with_cycles: (arg_0: {
settings: [] | [canister_settings];
amount: [] | [bigint];
}) => Promise<{ canister_id: canister_id }>;
provisional_top_up_canister: (arg_0: {
canister_id: canister_id;
amount: bigint;
}) => Promise<undefined>;
raw_rand: () => Promise<Array<number>>;
start_canister: (arg_0: { canister_id: canister_id }) => Promise<undefined>;
stop_canister: (arg_0: { canister_id: canister_id }) => Promise<undefined>;
uninstall_code: (arg_0: { canister_id: canister_id }) => Promise<undefined>;
update_settings: (arg_0: {
canister_id: Principal;
settings: canister_settings;
}) => Promise<undefined>;
}

0 comments on commit 9b663c6

Please sign in to comment.