Skip to content

Commit

Permalink
Refactor getCandidDefinition method in CandidAdapter class
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Apr 27, 2024
1 parent 641e935 commit f9ea7d3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/classes/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
initialState: T,
Expand All @@ -23,7 +23,9 @@ export function createStoreWithOptionalDevtools<T>(
}
}

export const importCandidDefinition = (candidDef: string) => {
export const importCandidDefinition = (
candidDef: string
): Promise<CandidDefenition> => {
try {
const dataUri =
"data:text/javascript;charset=utf-8," + encodeURIComponent(candidDef)
Expand Down
48 changes: 31 additions & 17 deletions packages/core/test/candid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
})
})
12 changes: 9 additions & 3 deletions packages/parser/tests/candid.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
})
})

0 comments on commit f9ea7d3

Please sign in to comment.