Skip to content

Instantly share code, notes, and snippets.

@krpeacock
Created April 12, 2023 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krpeacock/698646d80a5b16afcac401839f8f869e to your computer and use it in GitHub Desktop.
Save krpeacock/698646d80a5b16afcac401839f8f869e to your computer and use it in GitHub Desktop.
Script to convert a candid file to JS IDL using a canister
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