Skip to content

Commit

Permalink
fix: usage of ic-management locally
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Jul 3, 2023
1 parent a35f65b commit f550ae5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 20 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 { 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,28 @@ 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[],
_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
9 changes: 7 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 @@ -8,14 +8,17 @@ import { defaultAgent } from "./agent.utils";
type RequiredCanisterOptions<T> = Required<
Pick<CanisterOptions<T>, "canisterId">
> &
Omit<CanisterOptions<T>, "canisterId">;
Omit<CanisterOptions<T>, "canisterId"> &
Pick<ActorConfig, "queryTransform" | "callTransform">;

export const createServices = <T>({
options: {
canisterId,
serviceOverride,
certifiedServiceOverride,
agent: agentOption,
callTransform,
queryTransform,
},
idlFactory,
certifiedIdlFactory,
Expand All @@ -36,6 +39,8 @@ export const createServices = <T>({
Actor.createActor<T>(idlFactory, {
agent,
canisterId,
callTransform,
queryTransform,
});

const certifiedService: ActorSubclass<T> =
Expand Down

0 comments on commit f550ae5

Please sign in to comment.