diff --git a/packages/core/src/classes/adapter/index.ts b/packages/core/src/classes/adapter/index.ts index 5e3d19a94b..9ad7479671 100644 --- a/packages/core/src/classes/adapter/index.ts +++ b/packages/core/src/classes/adapter/index.ts @@ -141,7 +141,7 @@ export class CandidAdapter { throw new Error("Cannot compile Candid to JavaScript") } - return importCandidDefinition(candidDef) + return await importCandidDefinition(candidDef) } catch (error) { throw new Error(`Error evaluating Candid definition: ${error}`) } diff --git a/packages/core/src/utils/helper.ts b/packages/core/src/utils/helper.ts index 5e4822ded3..35c5d16450 100644 --- a/packages/core/src/utils/helper.ts +++ b/packages/core/src/utils/helper.ts @@ -2,7 +2,7 @@ import { hash } from "@dfinity/agent" import { DevtoolsOptions, devtools } from "zustand/middleware" import { createStore } from "zustand/vanilla" -import type { BaseActor, IDL } from "../types" +import type { BaseActor, CandidDefenition, IDL } from "../types" export function createStoreWithOptionalDevtools( initialState: T, @@ -23,7 +23,9 @@ export function createStoreWithOptionalDevtools( } } -export const importCandidDefinition = (candidDef: string) => { +export const importCandidDefinition = ( + candidDef: string +): Promise => { try { const dataUri = "data:text/javascript;charset=utf-8," + encodeURIComponent(candidDef) diff --git a/packages/core/test/candid.test.ts b/packages/core/test/candid.test.ts index fd90e5adf4..00d5e2c437 100644 --- a/packages/core/test/candid.test.ts +++ b/packages/core/test/candid.test.ts @@ -2,7 +2,10 @@ import { createActorManager, createCandidAdapter, createAgentManager, + createReactorStore, } from "../src" +import * as parser from "../../parser/dist/nodejs" +import { importCandidDefinition } from "../src/utils" describe("createReactorStore", () => { const agentManager = createAgentManager() @@ -26,27 +29,38 @@ describe("createReactorStore", () => { expect(name).toEqual("Internet Computer") }) - // it("should return candid idlFactory", async () => { - // const candid = await candidAdapter.getCandidDefinition( - // "a4gq6-oaaaa-aaaab-qaa4q-cai" - // ) + const canisterId = "ryjl3-tyaaa-aaaaa-aaaba-cai" - // expect(candid.idlFactory).toBeDefined() - // }) + it("should return fetch candid definition and callMethod", async () => { + const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId) - // const canisterId = "ryjl3-tyaaa-aaaaa-aaaba-cai" + const { callMethod } = createReactorStore({ + canisterId, + idlFactory, + agentManager, + }) - // it("should return fetch candid definition and callMethod", async () => { - // const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId) + const name = await callMethod("name") - // const { callMethod } = createReactorStore({ - // canisterId, - // idlFactory, - // agentManager, - // }) + expect(name).toEqual({ name: "Internet Computer" }) + }) - // const name = await callMethod("name") + it("should return fetch candid definition and callMethod using parser", async () => { + await candidAdapter.initializeParser(parser) + const candid = candidAdapter.parseDidToJs( + `service:{icrc1_name:()->(text) query;}` + ) + + const candidDef = await importCandidDefinition(candid) - // expect(name).toEqual({ name: "Internet Computer" }) - // }) + const { callMethod } = createReactorStore({ + canisterId, + idlFactory: candidDef.idlFactory, + agentManager, + }) + + const name = await callMethod("icrc1_name") + + expect(name).toEqual("Internet Computer") + }) }) diff --git a/packages/parser/tests/candid.test.ts b/packages/parser/tests/candid.test.ts index a89304fb49..a3542423ad 100644 --- a/packages/parser/tests/candid.test.ts +++ b/packages/parser/tests/candid.test.ts @@ -1,9 +1,9 @@ -import { createCandidAdapter } from "@ic-reactor/core" -import { createAgentManager } from "@ic-reactor/core" +import { createAgentManager, createCandidAdapter } from "@ic-reactor/core" import * as fetcher from "whatwg-fetch" -import * as parser from "../dist/node" +import * as parser from "../dist/nodejs" import fs from "fs" import path from "path" +import { importCandidDefinition } from "@ic-reactor/core/dist/utils" // Mocking the fetch function // @ts-ignore @@ -59,5 +59,11 @@ describe("createReactorStore", () => { ) expect(candid).toEqual(EXPECTED_JS) + + const candidDef = await importCandidDefinition(candid) + console.log("🚀 ~ it ~ candidDef:", candidDef) + + expect(candidDef.idlFactory).toBeInstanceOf(Function) + expect(candidDef.init).toBeInstanceOf(Function) }) })