import { Actor, HttpAgent } from "@dfinity/agent"; import type { IDL } from "@dfinity/candid"; import { toCanisterResponseError } from "./error"; export abstract class CandidService { protected createServiceClient( factory: IDL.InterfaceFactory, canisterId: string, host: string, agent: HttpAgent, ): T { const isMainnet = host.includes("icp-api.io"); if (!isMainnet) { agent.fetchRootKey(); } return Actor.createActor(factory, { agent, canisterId, }); } protected handleResponse( service: Promise, mapper: (from: From) => To, args?: unknown, ): Promise { return service.then(mapper).catch((err) => { console.log(err, args); throw toCanisterResponseError(err as Error); }); } constructor() {} }