From 982c92eedf4e1e530e1818ef158badc43219d037 Mon Sep 17 00:00:00 2001 From: megrogan Date: Thu, 6 May 2021 14:14:13 +0100 Subject: [PATCH 1/5] feat: Use full IC Management IDL --- packages/agent/src/canisters/management.ts | 32 +---- .../agent/src/canisters/management_idl.ts | 131 ++++++++++++++---- .../agent/src/canisters/management_service.ts | 76 ++++++++++ 3 files changed, 181 insertions(+), 58 deletions(-) create mode 100644 packages/agent/src/canisters/management_service.ts diff --git a/packages/agent/src/canisters/management.ts b/packages/agent/src/canisters/management.ts index 6a0b5baf2..6c415044e 100644 --- a/packages/agent/src/canisters/management.ts +++ b/packages/agent/src/canisters/management.ts @@ -1,40 +1,16 @@ -import { Actor, ActorMethod, ActorSubclass, CallConfig } from '../actor'; +import { Actor, ActorSubclass, CallConfig } from '../actor'; import { Principal } from '../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. * @param config */ export function getManagementCanister(config: CallConfig): ActorSubclass { - 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) { diff --git a/packages/agent/src/canisters/management_idl.ts b/packages/agent/src/canisters/management_idl.ts index 13deeba79..439b7f0e4 100644 --- a/packages/agent/src/canisters/management_idl.ts +++ b/packages/agent/src/canisters/management_idl.ts @@ -5,37 +5,108 @@ // @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), - memory_allocation: IDL.Opt(IDL.Nat), + const definite_canister_settings = IDL.Record({ + 'controller' : IDL.Principal, + 'freezing_threshold' : IDL.Nat, + 'memory_allocation' : IDL.Nat, + 'compute_allocation' : IDL.Nat, + }); + const canister_settings = IDL.Record({ + 'controller' : IDL.Opt(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) }), - ], [ - IDL.Record({ canister_id: canister_id }), - ], - [], - ), - create_canister: 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), - }), - ], - [], - [], - ), - set_controller: IDL.Func( - [IDL.Record({ canister_id: canister_id, new_controller: IDL.Principal })], - [], - [], - ), + '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 })], + [], + ), + '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({ + '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 })], + [], + [], + ), + '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, + }), + ], + [], + [], + ), }); }; diff --git a/packages/agent/src/canisters/management_service.ts b/packages/agent/src/canisters/management_service.ts new file mode 100644 index 000000000..8c43f8fd0 --- /dev/null +++ b/packages/agent/src/canisters/management_service.ts @@ -0,0 +1,76 @@ +/** + * This file is generated from the candid for asset management. + */ +/* tslint:disable */ +// @ts-ignore +import type { Principal } from '@dfinity/agent'; +export type canister_id = Principal; +export interface canister_settings { + 'controller' : [] | [Principal], + 'freezing_threshold' : [] | [bigint], + 'memory_allocation' : [] | [bigint], + 'compute_allocation' : [] | [bigint], +}; +export interface definite_canister_settings { + 'controller' : Principal, + 'freezing_threshold' : bigint, + 'memory_allocation' : bigint, + 'compute_allocation' : bigint, +}; +export type unit = Array; +export type user_id = Principal; +export type wasm_module = Array; +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], + } + >, + '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, + 'wasm_module' : wasm_module, + 'mode' : { 'reinstall' : null } | + { 'upgrade' : null } | + { 'install' : null }, + 'canister_id' : canister_id, + }, + ) => Promise, + '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, + 'raw_rand' : () => Promise>, + '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, +}; From 455a5a4455773028e0f106d0434e25fcefc09d33 Mon Sep 17 00:00:00 2001 From: megrogan Date: Wed, 3 Nov 2021 09:38:20 +0000 Subject: [PATCH 2/5] Use latest management candid --- .../agent/src/canisters/management_idl.ts | 170 ++++++++---------- .../agent/src/canisters/management_service.ts | 111 +++++------- 2 files changed, 121 insertions(+), 160 deletions(-) diff --git a/packages/agent/src/canisters/management_idl.ts b/packages/agent/src/canisters/management_idl.ts index 439b7f0e4..5780608cd 100644 --- a/packages/agent/src/canisters/management_idl.ts +++ b/packages/agent/src/canisters/management_idl.ts @@ -6,107 +6,87 @@ export default ({ IDL }) => { const canister_id = IDL.Principal; const definite_canister_settings = IDL.Record({ - 'controller' : IDL.Principal, - 'freezing_threshold' : IDL.Nat, - 'memory_allocation' : IDL.Nat, - 'compute_allocation' : IDL.Nat, + controllers: IDL.Vec(IDL.Principal), + freezing_threshold: IDL.Nat, + memory_allocation: IDL.Nat, + compute_allocation: IDL.Nat, }); const canister_settings = IDL.Record({ - 'controller' : IDL.Opt(IDL.Principal), - 'freezing_threshold' : IDL.Opt(IDL.Nat), - 'memory_allocation' : IDL.Opt(IDL.Nat), - 'compute_allocation' : IDL.Opt(IDL.Nat), + 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({ - '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)), + canister_status: IDL.Func( + [IDL.Record({ canister_id: canister_id })], + [ + IDL.Record({ + status: IDL.Variant({ + stopped: IDL.Null, + stopping: IDL.Null, + running: IDL.Null, }), - ], - [], - ), - 'create_canister' : IDL.Func( - [IDL.Record({ 'settings' : IDL.Opt(canister_settings) })], - [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({ - '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, + 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 })], + [], + ), + 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({ + arg: IDL.Vec(IDL.Nat8), + wasm_module: wasm_module, + mode: IDL.Variant({ + reinstall: IDL.Null, + upgrade: IDL.Null, + install: IDL.Null, }), - ], - [], - [], - ), - '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 })], - [], - [], - ), - '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, - }), - ], - [], - [], - ), + 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 })], + [], + [], + ), + 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, + }), + ], + [], + [], + ), }); }; diff --git a/packages/agent/src/canisters/management_service.ts b/packages/agent/src/canisters/management_service.ts index 8c43f8fd0..1c295dff3 100644 --- a/packages/agent/src/canisters/management_service.ts +++ b/packages/agent/src/canisters/management_service.ts @@ -3,74 +3,55 @@ */ /* tslint:disable */ // @ts-ignore -import type { Principal } from '@dfinity/agent'; +import type { Principal } from '@dfinity/principal'; export type canister_id = Principal; export interface canister_settings { - 'controller' : [] | [Principal], - 'freezing_threshold' : [] | [bigint], - 'memory_allocation' : [] | [bigint], - 'compute_allocation' : [] | [bigint], -}; + controllers: [] | [Array]; + freezing_threshold: [] | [bigint]; + memory_allocation: [] | [bigint]; + compute_allocation: [] | [bigint]; +} export interface definite_canister_settings { - 'controller' : Principal, - 'freezing_threshold' : bigint, - 'memory_allocation' : bigint, - 'compute_allocation' : bigint, -}; -export type unit = Array; + controllers: Array; + freezing_threshold: bigint; + memory_allocation: bigint; + compute_allocation: bigint; +} export type user_id = Principal; export type wasm_module = Array; 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], - } - >, - '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, - 'wasm_module' : wasm_module, - 'mode' : { 'reinstall' : null } | - { 'upgrade' : null } | - { 'install' : null }, - 'canister_id' : canister_id, - }, - ) => Promise, - '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, - 'raw_rand' : () => Promise>, - '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, -}; + 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]; + }>; + create_canister: (arg_0: { + settings: [] | [canister_settings]; + }) => Promise<{ canister_id: canister_id }>; + delete_canister: (arg_0: { canister_id: canister_id }) => Promise; + deposit_cycles: (arg_0: { canister_id: canister_id }) => Promise; + install_code: (arg_0: { + arg: Array; + wasm_module: wasm_module; + mode: { reinstall: null } | { upgrade: null } | { install: null }; + canister_id: canister_id; + }) => Promise; + 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; + raw_rand: () => Promise>; + start_canister: (arg_0: { canister_id: canister_id }) => Promise; + stop_canister: (arg_0: { canister_id: canister_id }) => Promise; + uninstall_code: (arg_0: { canister_id: canister_id }) => Promise; + update_settings: (arg_0: { + canister_id: Principal; + settings: canister_settings; + }) => Promise; +} From e1b34c812b407d31e92e407a56e63432beece127 Mon Sep 17 00:00:00 2001 From: megrogan Date: Wed, 10 Nov 2021 15:59:16 +0000 Subject: [PATCH 3/5] dummy commit --- packages/agent/src/canisters/management.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/agent/src/canisters/management.ts b/packages/agent/src/canisters/management.ts index 93485bf31..9124e1950 100644 --- a/packages/agent/src/canisters/management.ts +++ b/packages/agent/src/canisters/management.ts @@ -6,7 +6,7 @@ import _SERVICE from './management_service'; export type ManagementCanisterRecord = _SERVICE; /** - * Create a management canister actor. + * Create a management canister actor * @param config */ export function getManagementCanister(config: CallConfig): ActorSubclass { From b79cb91ab62b83139721f3ebe66c549ce61f9566 Mon Sep 17 00:00:00 2001 From: megrogan Date: Fri, 19 Nov 2021 09:38:01 +0000 Subject: [PATCH 4/5] Fix tests to work with bigint rather than number --- e2e/node/basic/basic.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/node/basic/basic.test.ts b/e2e/node/basic/basic.test.ts index 0f48adae1..e0d91d9b2 100644 --- a/e2e/node/basic/basic.test.ts +++ b/e2e/node/basic/basic.test.ts @@ -37,7 +37,7 @@ 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 () => { @@ -48,12 +48,12 @@ test('withOptions', async () => { agent: await agent, }).provisional_create_canister_with_cycles.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: [] }); }); From 29239a4964eb8b138da7d25adcbd4fabfdb530ab Mon Sep 17 00:00:00 2001 From: megrogan Date: Thu, 24 Mar 2022 09:02:46 +0000 Subject: [PATCH 5/5] Assert fn prov...with_cycles has type ActorMethod --- e2e/node/basic/basic.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/e2e/node/basic/basic.test.ts b/e2e/node/basic/basic.test.ts index e0d91d9b2..4b21ed6ab 100644 --- a/e2e/node/basic/basic.test.ts +++ b/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'; @@ -44,9 +44,10 @@ 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: [BigInt(1e12)], settings: [] }); })(),