Skip to content

Commit

Permalink
fix: usage of ic-management locally (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Jul 3, 2023
1 parent a35f65b commit 93f718c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
24 changes: 12 additions & 12 deletions packages/ic-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);

### :factory: ICManagementCanister

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L24)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L25)

#### Methods

Expand All @@ -76,7 +76,7 @@ const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);
| -------- | ---------------------------------------------------------------- |
| `create` | `(options: ICManagementCanisterOptions) => ICManagementCanister` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L29)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L30)

##### :gear: createCanister

Expand All @@ -86,7 +86,7 @@ Create a new canister
| ---------------- | ------------------------------------------------------------------------------------ |
| `createCanister` | `({ settings, senderCanisterVerion, }?: CreateCanisterParams) => Promise<Principal>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L50)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L70)

##### :gear: updateSettings

Expand All @@ -96,7 +96,7 @@ Update canister settings
| ---------------- | ------------------------------------------------------------------------------------------ |
| `updateSettings` | `({ canisterId, senderCanisterVerion, settings, }: UpdateSettingsParams) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L71)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L91)

##### :gear: installCode

Expand All @@ -106,7 +106,7 @@ Install code to a canister
| ------------- | ---------------------------------------------------------------------------------------------------- |
| `installCode` | `({ mode, canisterId, wasmModule, arg, senderCanisterVerion, }: InstallCodeParams) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L93)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L113)

##### :gear: uninstallCode

Expand All @@ -116,7 +116,7 @@ Uninstall code from a canister
| --------------- | ------------------------------------------------------------------------------- |
| `uninstallCode` | `({ canisterId, senderCanisterVerion, }: UninstallCodeParams) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L116)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L136)

##### :gear: startCanister

Expand All @@ -126,7 +126,7 @@ Start a canister
| --------------- | ------------------------------------------ |
| `startCanister` | `(canisterId: Principal) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L131)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L151)

##### :gear: stopCanister

Expand All @@ -136,7 +136,7 @@ Stop a canister
| -------------- | ------------------------------------------ |
| `stopCanister` | `(canisterId: Principal) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L140)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L160)

##### :gear: canisterStatus

Expand All @@ -146,7 +146,7 @@ Get canister details (memory size, status, etc.)
| ---------------- | --------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
| `canisterStatus` | `(canisterId: Principal) => Promise<{ status: { stopped: null; } or { stopping: null; } | { running: null; }; memory_size: bigint; cycles: bigint; settings: definite_canister_settings; idle_cycles_burned_per_day: bigint; module_hash: [] | [...]; }>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L149)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L169)

##### :gear: canisterInfo

Expand All @@ -156,7 +156,7 @@ Get canister info (controllers, module hash, changes, etc.)
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `canisterInfo` | `({ canisterId, numRequestChanges, }: CanisterInfoParams) => Promise<{ controllers: Principal[]; module_hash: [] or [Uint8Array]; recent_changes: change[]; total_num_changes: bigint; }>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L162)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L182)

##### :gear: deleteCanister

Expand All @@ -166,7 +166,7 @@ Deletes a canister
| ---------------- | ------------------------------------------ |
| `deleteCanister` | `(canisterId: Principal) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L177)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L197)

##### :gear: provisionalCreateCanisterWithCycles

Expand All @@ -176,7 +176,7 @@ Creates a canister. Only available on development instances.
| ------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| `provisionalCreateCanisterWithCycles` | `({ settings, amount, canisterId, }?: ProvisionalCreateCanisterWithCyclesParams) => Promise<Principal>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L189)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L209)

<!-- TSDOC_END -->

Expand Down
22 changes: 21 additions & 1 deletion packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CallConfig } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { createServices, toNullable } from "@dfinity/utils";
import type { _SERVICE as IcManagementService } from "../candid/ic-management";
Expand Down Expand Up @@ -27,10 +28,29 @@ export class ICManagementCanister {
}

public static create(options: ICManagementCanisterOptions) {
// Source getManagementCanister in agent-js.
// Allow usage of the ICManagementCanister wrapper locally.
const transform = (
_methodName: string,
args: unknown[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_callConfig: CallConfig
) => {
const first = args[0] as { canister_id: string };
let effectiveCanisterId = Principal.fromHex("");
if (first && typeof first === "object" && first.canister_id) {
effectiveCanisterId = Principal.from(first.canister_id as unknown);
}
return { effectiveCanisterId };
};

const { service } = createServices<IcManagementService>({
options: {
...options,
canisterId: Principal.fromText("aaaaa-aa"),
// Resolve to "aaaaa-aa" on mainnet
canisterId: Principal.fromHex(""),
callTransform: transform,
queryTransform: transform,
},
idlFactory,
certifiedIdlFactory,
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ Parameters:

#### :gear: createServices

| Function | Type |
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `createServices` | `<T>({ options: { canisterId, serviceOverride, certifiedServiceOverride, agent: agentOption, }, idlFactory, certifiedIdlFactory, }: { options: RequiredCanisterOptions<T>; idlFactory: InterfaceFactory; certifiedIdlFactory: InterfaceFactory; }) => { ...; }` |
| Function | Type |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `createServices` | `<T>({ options: { canisterId, serviceOverride, certifiedServiceOverride, agent: agentOption, callTransform, queryTransform, }, idlFactory, certifiedIdlFactory, }: { options: Required<Pick<CanisterOptions<T>, "canisterId">> and Omit<CanisterOptions<T>, "canisterId"> & Pick<...>; idlFactory: InterfaceFactory; certifiedId...` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/actor.utils.ts#L13)

Expand Down
11 changes: 9 additions & 2 deletions packages/utils/src/utils/actor.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ActorSubclass, Agent } from "@dfinity/agent";
import type { ActorConfig, ActorSubclass, Agent } from "@dfinity/agent";
import { Actor } from "@dfinity/agent";
import type { IDL } from "@dfinity/candid";
import type { Principal } from "@dfinity/principal";
Expand All @@ -16,11 +16,14 @@ export const createServices = <T>({
serviceOverride,
certifiedServiceOverride,
agent: agentOption,
callTransform,
queryTransform,
},
idlFactory,
certifiedIdlFactory,
}: {
options: RequiredCanisterOptions<T>;
options: RequiredCanisterOptions<T> &
Pick<ActorConfig, "queryTransform" | "callTransform">;
idlFactory: IDL.InterfaceFactory;
certifiedIdlFactory: IDL.InterfaceFactory;
}): {
Expand All @@ -36,13 +39,17 @@ export const createServices = <T>({
Actor.createActor<T>(idlFactory, {
agent,
canisterId,
callTransform,
queryTransform,
});

const certifiedService: ActorSubclass<T> =
certifiedServiceOverride ??
Actor.createActor<T>(certifiedIdlFactory, {
agent,
canisterId,
callTransform,
queryTransform,
});

return { service, certifiedService, agent, canisterId };
Expand Down

0 comments on commit 93f718c

Please sign in to comment.