diff --git a/packages/react/src/hooks/useActorManger.ts b/packages/react/src/hooks/useActorManger.ts index 9b7f8769ff..82c8d7b77d 100644 --- a/packages/react/src/hooks/useActorManger.ts +++ b/packages/react/src/hooks/useActorManger.ts @@ -40,7 +40,7 @@ export const useActorManager = < ...rest } = useDidJs({ canisterId, - didjsId: didjsId, + didjsCanisterId: didjsId, idlFactory: maybeIdlFactory, }) diff --git a/packages/react/src/hooks/useDidJs.ts b/packages/react/src/hooks/useDidJs.ts index f2e2675a66..dc5ca347a8 100644 --- a/packages/react/src/hooks/useDidJs.ts +++ b/packages/react/src/hooks/useDidJs.ts @@ -1,5 +1,5 @@ import { useState, useCallback, useEffect } from "react" -import { CandidManager } from "@ic-reactor/store" +import { createCandidAdapter } from "@ic-reactor/store" import type { IDL } from "@dfinity/candid" import { useAgent } from "../context/agent" @@ -20,11 +20,15 @@ const DEFAULT_STATE: IDLFactoryState = { interface UseIDLFactoryArgs { canisterId: string - didjsId?: string + didjsCanisterId?: string idlFactory?: IDL.InterfaceFactory } -const useDidJs = ({ canisterId, didjsId, idlFactory }: UseIDLFactoryArgs) => { +const useDidJs = ({ + canisterId, + didjsCanisterId, + idlFactory, +}: UseIDLFactoryArgs) => { const [{ didJs, fetchError, fetching }, setDidJs] = useState( { ...DEFAULT_STATE, @@ -47,30 +51,15 @@ const useDidJs = ({ canisterId, didjsId, idlFactory }: UseIDLFactoryArgs) => { })) try { - const candidManager = new CandidManager(agent, didjsId) - let fetchedDidJs = await candidManager - .getFromMetadata(canisterId) - .catch(() => null) + const candidManager = createCandidAdapter({ agent, didjsCanisterId }) - if (!fetchedDidJs) { - fetchedDidJs = await candidManager - .getFromTmpHack(canisterId) - .catch(() => null) - } + let fetchedDidJs = await candidManager.getCandidDefinition(canisterId) - if (fetchedDidJs) { - setDidJs({ - didJs: fetchedDidJs, - fetching: false, - fetchError: null, - }) - } else { - setDidJs((prevState) => ({ - ...prevState, - fetchError: `Candid not found for canister ${canisterId}`, - fetching: false, - })) - } + setDidJs({ + didJs: fetchedDidJs, + fetching: false, + fetchError: null, + }) return fetchedDidJs } catch (err) { @@ -81,7 +70,7 @@ const useDidJs = ({ canisterId, didjsId, idlFactory }: UseIDLFactoryArgs) => { fetching: false, })) } - }, [canisterId, didjsId, agent]) + }, [canisterId, didjsCanisterId, agent]) useEffect(() => { if (!fetching && !idlFactory) { diff --git a/packages/store/src/tools/candid.ts b/packages/store/src/tools/candid.ts index 0afeb7f814..72987a3b42 100644 --- a/packages/store/src/tools/candid.ts +++ b/packages/store/src/tools/candid.ts @@ -1,40 +1,36 @@ -import { Actor, CanisterStatus } from "@dfinity/agent" +import { Actor, CanisterStatus, HttpAgent } from "@dfinity/agent" import { IDL } from "@dfinity/candid" import { Principal } from "@dfinity/principal" import { CanisterId } from "../actor" -import { AgentManager } from "../agent" - -export interface CandidAdapterOptions { - agentManager: AgentManager - didjsCanisterId?: string -} - -export interface Defenition { - idlFactory: IDL.InterfaceFactory - init: ({ IDL }: { IDL: any }) => never[] -} +import { CandidAdapterOptions, CandidDefenition } from "./types" export class CandidAdapter { - public agentManager: AgentManager + public agent: HttpAgent public didjsCanisterId: string - constructor({ agentManager, didjsCanisterId }: CandidAdapterOptions) { - this.agentManager = agentManager + constructor({ agentManager, agent, didjsCanisterId }: CandidAdapterOptions) { + if (agent) { + this.agent = agent + } else if (agentManager) { + this.agent = agentManager.getAgent() + agentManager.subscribeAgent((agent) => { + this.agent = agent + this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId() + }) + } else { + throw new Error("No agent or agentManager provided") + } this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId() - - this.agentManager.subscribeAgent(() => { - this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId() - }) } private getDefaultDidJsId() { - return this.agentManager.isLocalEnv + return this.agent.isLocal() ? "bd3sg-teaaa-aaaaa-qaaba-cai" : "a4gq6-oaaaa-aaaab-qaa4q-cai" } - async getCandidDefinition(canisterId: CanisterId): Promise { + async getCandidDefinition(canisterId: CanisterId): Promise { try { // First attempt: Try getting Candid definition from metadata const fromMetadata = await this.getFromMetadata(canisterId) @@ -55,15 +51,13 @@ export class CandidAdapter { async getFromMetadata( canisterId: CanisterId - ): Promise { + ): Promise { if (typeof canisterId === "string") { canisterId = Principal.fromText(canisterId) } - const agent = this.agentManager.getAgent() - const status = await CanisterStatus.request({ - agent, + agent: this.agent, canisterId, paths: ["candid"], }) @@ -74,16 +68,14 @@ export class CandidAdapter { async getFromTmpHack( canisterId: CanisterId - ): Promise { + ): Promise { const commonInterface: IDL.InterfaceFactory = ({ IDL }) => IDL.Service({ __get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]), }) - const agent = this.agentManager.getAgent() - const actor = Actor.createActor(commonInterface, { - agent, + agent: this.agent, canisterId, }) @@ -93,16 +85,14 @@ export class CandidAdapter { return data ? this.didTojs(data) : undefined } - async didTojs(candidSource: string): Promise { + async didTojs(candidSource: string): Promise { const didjsInterface: IDL.InterfaceFactory = ({ IDL }) => IDL.Service({ did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]), }) - const agent = this.agentManager.getAgent() - const didjs = Actor.createActor(didjsInterface, { - agent, + agent: this.agent, canisterId: this.didjsCanisterId, }) diff --git a/packages/store/src/tools/index.ts b/packages/store/src/tools/index.ts index 002d02cabf..891cb24ca7 100644 --- a/packages/store/src/tools/index.ts +++ b/packages/store/src/tools/index.ts @@ -1 +1,2 @@ export * from "./candid" +export * from "./types" diff --git a/packages/store/src/tools/types.ts b/packages/store/src/tools/types.ts new file mode 100644 index 0000000000..232c87f228 --- /dev/null +++ b/packages/store/src/tools/types.ts @@ -0,0 +1,13 @@ +import type { AgentManager } from "../agent" +import type { IDL, HttpAgent } from "../types" + +export interface CandidAdapterOptions { + agentManager?: AgentManager + agent?: HttpAgent + didjsCanisterId?: string +} + +export interface CandidDefenition { + idlFactory: IDL.InterfaceFactory + init: ({ IDL }: { IDL: any }) => never[] +}