Created
April 12, 2023 17:59
-
-
Save krpeacock/698646d80a5b16afcac401839f8f869e to your computer and use it in GitHub Desktop.
Script to convert a candid file to JS IDL using a canister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IDL } from "@dfinity/candid"; | |
import { Actor, HttpAgent } from "@dfinity/agent"; | |
export const candidToJS = async (candid_source: string) => { | |
// call didjs canister | |
const didjs_interface: IDL.InterfaceFactory = ({ IDL }) => | |
IDL.Service({ | |
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]), | |
}); | |
const candidCanister = `a4gq6-oaaaa-aaaab-qaa4q-cai`; | |
const agent = new HttpAgent({ host: "https://icp-api.io" }); | |
const didjs = Actor.createActor(didjs_interface, { | |
agent, | |
canisterId: candidCanister, | |
}); | |
const js: any = await didjs.did_to_js(candid_source); | |
if (Array.isArray(js) && js.length === 0) { | |
return undefined; | |
} | |
return js[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment