Skip to content

Commit

Permalink
Update useDidJs and useActorManager hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Feb 16, 2024
1 parent 9db0f8f commit d16f65f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useActorManger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const useActorManager = <
...rest
} = useDidJs({
canisterId,
didjsId: didjsId,
didjsCanisterId: didjsId,
idlFactory: maybeIdlFactory,
})

Expand Down
41 changes: 15 additions & 26 deletions packages/react/src/hooks/useDidJs.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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<IDLFactoryState>(
{
...DEFAULT_STATE,
Expand All @@ -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) {
Expand All @@ -81,7 +70,7 @@ const useDidJs = ({ canisterId, didjsId, idlFactory }: UseIDLFactoryArgs) => {
fetching: false,
}))
}
}, [canisterId, didjsId, agent])
}, [canisterId, didjsCanisterId, agent])

useEffect(() => {
if (!fetching && !idlFactory) {
Expand Down
56 changes: 23 additions & 33 deletions packages/store/src/tools/candid.ts
Original file line number Diff line number Diff line change
@@ -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<Defenition> {
async getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition> {
try {
// First attempt: Try getting Candid definition from metadata
const fromMetadata = await this.getFromMetadata(canisterId)
Expand All @@ -55,15 +51,13 @@ export class CandidAdapter {

async getFromMetadata(
canisterId: CanisterId
): Promise<Defenition | undefined> {
): Promise<CandidDefenition | undefined> {
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"],
})
Expand All @@ -74,16 +68,14 @@ export class CandidAdapter {

async getFromTmpHack(
canisterId: CanisterId
): Promise<Defenition | undefined> {
): Promise<CandidDefenition | undefined> {
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,
})

Expand All @@ -93,16 +85,14 @@ export class CandidAdapter {
return data ? this.didTojs(data) : undefined
}

async didTojs(candidSource: string): Promise<Defenition> {
async didTojs(candidSource: string): Promise<CandidDefenition> {
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,
})

Expand Down
1 change: 1 addition & 0 deletions packages/store/src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./candid"
export * from "./types"
13 changes: 13 additions & 0 deletions packages/store/src/tools/types.ts
Original file line number Diff line number Diff line change
@@ -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[]
}

0 comments on commit d16f65f

Please sign in to comment.