Skip to content

Commit

Permalink
build: upgrade next and ic-management (#2805)
Browse files Browse the repository at this point in the history
# Motivation

Bump ic-js to next after release of ic-js v0.0.18.

Implement the usage of the `@dfinity/ic-management` library because the
definition in agent-js is not up-to-date anymore and ship a fix to use
this library locally.

# PRs

- [x] dfinity/ic-js#377
  • Loading branch information
peterpeterparker authored Jul 3, 2023
1 parent a2c4f4e commit 8697607
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 74 deletions.
96 changes: 48 additions & 48 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion frontend/src/lib/canisters/ic-management/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ const getCanisterStatus = (
};

export const toCanisterDetails = ({
response: { memory_size, status, cycles, settings, module_hash },
response: {
memory_size,
status,
cycles,
settings,
module_hash,
idle_cycles_burned_per_day,
},
canisterId,
}: {
response: CanisterStatusResponse;
Expand All @@ -46,4 +53,5 @@ export const toCanisterDetails = ({
module_hash.length > 0 && module_hash[0] !== undefined
? new Uint8Array(module_hash[0]).buffer
: undefined,
idleCyclesBurnedPerDay: idle_cycles_burned_per_day,
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface CanisterDetails {
cycles: bigint;
settings: CanisterSettings;
moduleHash?: ArrayBuffer;
idleCyclesBurnedPerDay: bigint;
}
16 changes: 8 additions & 8 deletions frontend/src/lib/worker-api/canisters.worker-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { mapError } from "$lib/canisters/ic-management/ic-management.errors";
import type { CanisterActorParams } from "$lib/types/worker";
import { mapCanisterId } from "$lib/utils/canisters.utils";
import { logWithTimestamp } from "$lib/utils/dev.utils";
import type { ManagementCanisterRecord } from "@dfinity/agent";
import { getManagementCanister, HttpAgent } from "@dfinity/agent";
import { HttpAgent } from "@dfinity/agent";
import type { CanisterStatusResponse } from "@dfinity/ic-management";
import { ICManagementCanister } from "@dfinity/ic-management";

export const queryCanisterDetails = async ({
identity,
Expand All @@ -16,15 +16,15 @@ export const queryCanisterDetails = async ({
}: { canisterId: string } & CanisterActorParams): Promise<CanisterDetails> => {
logWithTimestamp(`Getting canister ${canisterId} details call...`);
const {
icMgtService: { canister_status },
icMgtService: { canisterStatus },
} = await canisters({ identity, host, fetchRootKey });

try {
const canister_id = mapCanisterId(canisterId);

const rawResponse: CanisterStatusResponse = await canister_status({
canister_id,
});
const rawResponse: CanisterStatusResponse = await canisterStatus(
canister_id
);

const response = toCanisterDetails({
response: rawResponse,
Expand All @@ -44,7 +44,7 @@ const canisters = async ({
host,
fetchRootKey,
}: CanisterActorParams): Promise<{
icMgtService: ManagementCanisterRecord;
icMgtService: ICManagementCanister;
}> => {
const agent = new HttpAgent({
identity,
Expand All @@ -55,7 +55,7 @@ const canisters = async ({
await agent.fetchRootKey();
}

const icMgtService = getManagementCanister({ agent });
const icMgtService = ICManagementCanister.create({ agent });

return { icMgtService };
};
22 changes: 12 additions & 10 deletions frontend/src/tests/lib/canisters/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
mockCanisterId,
mockCanisterSettings,
} from "$tests/mocks/canisters.mock";
import type { ActorSubclass, ManagementCanisterRecord } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import type { CanisterStatusResponse } from "@dfinity/ic-management";
import type { _SERVICE as IcManagementService } from "@dfinity/ic-management/dist/candid/ic-management";
import { Principal } from "@dfinity/principal";
import { mock } from "jest-mock-extended";

describe("ICManagementCanister", () => {
const createICManagement = async (service: ManagementCanisterRecord) => {
const createICManagement = async (service: IcManagementService) => {
const defaultAgent = await createAgent({ identity: mockIdentity });

return ICManagementCanister.create({
agent: defaultAgent,
serviceOverride: service as ActorSubclass<ManagementCanisterRecord>,
serviceOverride: service as ActorSubclass<IcManagementService>,
});
};

Expand All @@ -41,8 +42,9 @@ describe("ICManagementCanister", () => {
cycles: BigInt(10_000),
settings,
module_hash: [],
idle_cycles_burned_per_day: BigInt(30_000),
};
const service = mock<ManagementCanisterRecord>();
const service = mock<IcManagementService>();
service.canister_status.mockResolvedValue(response);

const icManagement = await createICManagement(service);
Expand All @@ -56,7 +58,7 @@ describe("ICManagementCanister", () => {

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

const icManagement = await createICManagement(service);
Expand All @@ -69,7 +71,7 @@ describe("ICManagementCanister", () => {

it("throws Error", async () => {
const error = new Error("Test");
const service = mock<ManagementCanisterRecord>();
const service = mock<IcManagementService>();
service.canister_status.mockRejectedValue(error);

const icManagement = await createICManagement(service);
Expand All @@ -83,7 +85,7 @@ describe("ICManagementCanister", () => {

describe("updateSettings", () => {
it("calls update_settings with new settings", async () => {
const service = mock<ManagementCanisterRecord>();
const service = mock<IcManagementService>();
service.update_settings.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);
Expand All @@ -101,7 +103,7 @@ describe("ICManagementCanister", () => {
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe",
],
};
const service = mock<ManagementCanisterRecord>();
const service = mock<IcManagementService>();
service.update_settings.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);
Expand All @@ -115,7 +117,7 @@ describe("ICManagementCanister", () => {

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

const icManagement = await createICManagement(service);
Expand All @@ -130,7 +132,7 @@ describe("ICManagementCanister", () => {

it("throws Error", async () => {
const error = new Error("Test");
const service = mock<ManagementCanisterRecord>();
const service = mock<IcManagementService>();
service.update_settings.mockRejectedValue(error);

const icManagement = await createICManagement(service);
Expand Down
Loading

0 comments on commit 8697607

Please sign in to comment.