Skip to content

Commit

Permalink
feat: remove ic-management converters (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Jun 20, 2023
1 parent a25fbfa commit 0ae7ea8
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 234 deletions.
4 changes: 2 additions & 2 deletions packages/ic-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ const agent = await createAgent({
host: HOST,
});

const { getCanisterDetails } = ICMgmtCanister.create({
const { canisterStatus } = ICMgmtCanister.create({
agent,
});

const details = await getCanisterDetails(YOUR_CANISTER_ID);
const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);
```

## Features
Expand Down
26 changes: 0 additions & 26 deletions packages/ic-management/src/errors/ic-management.errors.spec.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/ic-management/src/errors/ic-management.errors.ts

This file was deleted.

50 changes: 7 additions & 43 deletions packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import type { ManagementCanisterRecord } from "@dfinity/agent";
import { ActorSubclass, HttpAgent } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { mock } from "jest-mock-extended";
import { UserNotTheControllerError } from "./errors/ic-management.errors";
import { ICManagementCanister } from "./ic-management.canister";
import {
mockCanisterDetails,
mockCanisterId,
mockCanisterSettings,
} from "./ic-management.mock";
import { CanisterStatusDidResponse } from "./types/ic-management.did";
import { toCanisterDetails } from "./utils/ic-management.converters";
import { mockCanisterId, mockCanisterSettings } from "./ic-management.mock";
import { CanisterStatusResponse } from "./types/ic-management.responses";

describe("ICManagementCanister", () => {
const mockAgent: HttpAgent = mock<HttpAgent>();
Expand All @@ -23,7 +17,7 @@ describe("ICManagementCanister", () => {
};

describe("ICManagementCanister.getCanisterDetails", () => {
it("returns account identifier when success", async () => {
it("returns canister status when success", async () => {
const settings = {
freezing_threshold: BigInt(2),
controllers: [
Expand All @@ -34,7 +28,7 @@ describe("ICManagementCanister", () => {
memory_allocation: BigInt(4),
compute_allocation: BigInt(10),
};
const response: CanisterStatusDidResponse = {
const response: CanisterStatusResponse = {
status: { running: null },
memory_size: BigInt(1000),
cycles: BigInt(10_000),
Expand All @@ -46,24 +40,9 @@ describe("ICManagementCanister", () => {

const icManagement = await createICManagement(service);

const res = await icManagement.getCanisterDetails(mockCanisterDetails.id);
const res = await icManagement.canisterStatus(mockCanisterId);

expect(res).toEqual(
toCanisterDetails({ response, canisterId: mockCanisterDetails.id })
);
});

it("throws UserNotTheControllerError", async () => {
const error = new Error("code: 403");
const service = mock<ManagementCanisterRecord>();
service.canister_status.mockRejectedValue(error);

const icManagement = await createICManagement(service);

const call = () =>
icManagement.getCanisterDetails(Principal.fromText("aaaaa-aa"));

expect(call).rejects.toThrowError(UserNotTheControllerError);
expect(res).toEqual(response);
});

it("throws Error", async () => {
Expand All @@ -74,7 +53,7 @@ describe("ICManagementCanister", () => {
const icManagement = await createICManagement(service);

const call = () =>
icManagement.getCanisterDetails(Principal.fromText("aaaaa-aa"));
icManagement.canisterStatus(Principal.fromText("aaaaa-aa"));

expect(call).rejects.toThrowError(Error);
});
Expand Down Expand Up @@ -112,21 +91,6 @@ describe("ICManagementCanister", () => {
expect(service.update_settings).toBeCalled();
});

it("throws UserNotTheControllerError", async () => {
const error = new Error("code: 403");
const service = mock<ManagementCanisterRecord>();
service.update_settings.mockRejectedValue(error);

const icManagement = await createICManagement(service);

const call = () =>
icManagement.updateSettings({
canisterId: mockCanisterId,
settings: mockCanisterSettings,
});
expect(call).rejects.toThrowError(UserNotTheControllerError);
});

it("throws Error", async () => {
const error = new Error("Test");
const service = mock<ManagementCanisterRecord>();
Expand Down
67 changes: 18 additions & 49 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import {
} from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { toNullable } from "@dfinity/utils";
import { mapError } from "./errors/ic-management.errors";
import type { ICManagementCanisterOptions } from "./types/canister.options";
import type { CanisterStatusDidResponse } from "./types/ic-management.did";
import type {
CanisterDetails,
CanisterSettings,
} from "./types/ic-management.response";
import { toCanisterDetails } from "./utils/ic-management.converters";
import type { UpdateSettingsParams } from "./types/ic-management.params";
import type { CanisterStatusResponse } from "./types/ic-management.responses";

export class ICManagementCanister {
private constructor(private readonly service: ManagementCanisterRecord) {
Expand All @@ -30,62 +25,36 @@ export class ICManagementCanister {
* Returns canister details (memory size, status, etc.)
*
* @param {Principal} canisterId
* @returns Promise<CanisterDetails>
* @throws UserNotTheController, Error
* @returns Promise<CanisterStatusResponse>
*/
public getCanisterDetails = async (
canisterId: Principal
): Promise<CanisterDetails> => {
try {
const rawResponse: CanisterStatusDidResponse =
await this.service.canister_status({
canister_id: canisterId,
});
return toCanisterDetails({
response: rawResponse,
canisterId,
});
} catch (error: unknown) {
throw mapError(error);
}
};
canisterStatus = (canisterId: Principal): Promise<CanisterStatusResponse> =>
this.service.canister_status({
canister_id: canisterId,
});

/**
* Update canister settings
*
* @param {Object} params
* @param {Principal} params.canisterId
* @param {CanisterSettings} params.settings
* @returns Promise<void>
* @throws UserNotTheController, Error
*/
public updateSettings = async ({
updateSettings = async ({
canisterId,
settings: {
controllers,
freezingThreshold,
memoryAllocation,
computeAllocation,
},
}: {
canisterId: Principal;
settings: Partial<CanisterSettings>;
}): Promise<void> => {
try {
// Empty array does not change the value in the settings.
await this.service.update_settings({
canister_id: canisterId,
settings: {
controllers: toNullable(
controllers?.map((c) => Principal.fromText(c))
),
freezing_threshold: toNullable(freezingThreshold),
memory_allocation: toNullable(memoryAllocation),
compute_allocation: toNullable(computeAllocation),
},
});
} catch (error: unknown) {
throw mapError(error);
}
};
}: UpdateSettingsParams): Promise<void> =>
this.service.update_settings({
canister_id: canisterId,
settings: {
controllers: toNullable(controllers?.map((c) => Principal.fromText(c))),
freezing_threshold: toNullable(freezingThreshold),
memory_allocation: toNullable(memoryAllocation),
compute_allocation: toNullable(computeAllocation),
},
});
}
17 changes: 0 additions & 17 deletions packages/ic-management/src/ic-management.mock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Identity } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import {
CanisterDetails,
CanisterStatus,
} from "./types/ic-management.response";

export const mockPrincipalText =
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe";
Expand All @@ -16,19 +12,6 @@ export const mockIdentity = {

export const mockCanisterId = Principal.fromText("ryjl3-tyaaa-aaaaa-aaaba-cai");

export const mockCanisterDetails: CanisterDetails = {
id: mockCanisterId,
status: CanisterStatus.Running,
memorySize: BigInt(10),
cycles: BigInt(30),
settings: {
controllers: [mockIdentity.getPrincipal().toText()],
freezingThreshold: BigInt(30),
memoryAllocation: BigInt(1000),
computeAllocation: BigInt(2000),
},
};

export const mockCanisterSettings = {
freezing_threshold: BigInt(2),
controllers: [
Expand Down
3 changes: 2 additions & 1 deletion packages/ic-management/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ICManagementCanister } from "./ic-management.canister";
export * from "./types/ic-management.response";
export * from "./types/ic-management.params";
export * from "./types/ic-management.responses";
14 changes: 14 additions & 0 deletions packages/ic-management/src/types/ic-management.params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Principal } from "@dfinity/principal";
import type { QueryParams } from "@dfinity/utils/src";

export interface CanisterSettings {
controllers?: string[];
freezingThreshold?: bigint;
memoryAllocation?: bigint;
computeAllocation?: bigint;
}

export type UpdateSettingsParams = {
canisterId: Principal;
settings: CanisterSettings;
} & Omit<QueryParams, "certified">;
23 changes: 0 additions & 23 deletions packages/ic-management/src/types/ic-management.response.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { definite_canister_settings } from "../../candid/ic-management";

export type CanisterStatusDid =
| { stopped: null }
| { stopping: null }
| { running: null };

export type CanisterStatusDidResponse = {
status: CanisterStatusDid;
export interface CanisterStatusResponse {
status:
| {
stopped: null;
}
| {
stopping: null;
}
| {
running: null;
};
memory_size: bigint;
cycles: bigint;
settings: definite_canister_settings;
module_hash: [] | [Array<number>];
};
}
42 changes: 0 additions & 42 deletions packages/ic-management/src/utils/ic-management.converters.ts

This file was deleted.

0 comments on commit 0ae7ea8

Please sign in to comment.