Skip to content

Commit

Permalink
feat: CMC get default subnets
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@dfinity.org>
  • Loading branch information
peterpeterparker committed Oct 13, 2024
1 parent 1db2d72 commit b7b71b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
46 changes: 29 additions & 17 deletions packages/cmc/src/cmc.canister.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
import { Actor } from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import { Canister, createServices, type QueryParams } from "@dfinity/utils";
import type {
_SERVICE as CMCCanisterService,
Cycles,
NotifyCreateCanisterArg,
NotifyTopUpArg,
_SERVICE,
} from "../candid/cmc";
import { idlFactory as certifiedIdlFactory } from "../candid/cmc.certified.idl";
import { idlFactory } from "../candid/cmc.idl";
import { throwNotifyError } from "./cmc.errors";
import type { CMCCanisterOptions } from "./cmc.options";

export class CMCCanister {
private constructor(private readonly service: _SERVICE) {
this.service = service;
}

public static create(options: CMCCanisterOptions) {
const agent = options.agent;
const canisterId = options.canisterId;

const service =
options.serviceOverride ??
Actor.createActor<_SERVICE>(idlFactory, {
agent,
canisterId,
export class CMCCanister extends Canister<CMCCanisterService> {
static create(options: CMCCanisterOptions): CMCCanister {
const { service, certifiedService, canisterId } =
createServices<CMCCanisterService>({
options,
idlFactory,
certifiedIdlFactory,
});

return new CMCCanister(service);
return new CMCCanister(canisterId, service, certifiedService);
}

/**
Expand Down Expand Up @@ -92,4 +86,22 @@ export class CMCCanister {
`Unsupported response type in notifyTopUp ${JSON.stringify(response)}`,
);
};

/**
* This function calls the `get_default_subnets` method of the CMC canister, which returns a list of
* default subnets as `Principal` objects. It can be called as query or update.
*
* @param {Object} [params] - The query parameters for the call.
* @param {boolean} [params.certified] - Determines whether the response should be certified
* (default: non-certified if not specified).
*
* @returns {Promise<Principal[]>} - A promise that resolves to an array of `Principal` objects
* representing the default subnets.
*/
public getDefaultSubnets = async ({ certified }: QueryParams = {}): Promise<
Principal[]
> => {
const { get_default_subnets } = this.caller({ certified });
return get_default_subnets();
};
}
2 changes: 1 addition & 1 deletion packages/cmc/src/cmc.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import type { _SERVICE as CanisterService } from "../candid/cmc";

export interface CMCCanisterOptions
extends Omit<CanisterOptions<CanisterService>, "canisterId"> {
canisterId: string | Principal;
canisterId: Principal;
}

0 comments on commit b7b71b7

Please sign in to comment.