Skip to content

Instantly share code, notes, and snippets.

@amilz
Created May 16, 2024 15:11
Show Gist options
  • Select an option

  • Save amilz/d1131f512c65fc8cd99f2c0438f28846 to your computer and use it in GitHub Desktop.

Select an option

Save amilz/d1131f512c65fc8cd99f2c0438f28846 to your computer and use it in GitHub Desktop.
Script to Convert Anchor IDL (tested on v0.30) to Umi Client Library
// @ts-nocheck
import { AnchorIdl, rootNodeFromAnchor } from "@kinobi-so/nodes-from-anchor";
import { createFromRoot, renderJavaScriptVisitor } from "@metaplex-foundation/kinobi";
import { RootNode } from "@kinobi-so/node-types";
import { renderJavaScriptUmiVisitor } from "@kinobi-so/renderers";
import anchorIdl from "../target/idl/kinobi_test.json";
function appendStringToDefinedTypeNames(rootNode: RootNode, appendString: string): RootNode {
const rootNodeCopy = JSON.parse(JSON.stringify(rootNode));
rootNodeCopy.program.definedTypes.forEach(definedType => {
definedType.name += appendString;
});
return rootNodeCopy;
}
async function generateClients() {
const rootNode: RootNode = rootNodeFromAnchor(anchorIdl as AnchorIdl);
// Workaround for the Account/Type name conflict error
const cleanedRootNode = appendStringToDefinedTypeNames(rootNode, "Type");
const kinobi = createFromRoot(cleanedRootNode, false);
const clients = [
{ type: "JS", dir: "clients/js/src/generated", visitor: renderJavaScriptVisitor },
{ type: "Umi", dir: "clients/umi/src/generated", visitor: renderJavaScriptUmiVisitor }
];
for (const client of clients) {
try {
kinobi.accept(client.visitor(client.dir));
console.log(`✅ Successfully generated ${client.type} client for directory: ${client.dir}!`);
} catch (e) {
console.error(`Error in ${client.visitor.name}:`, e);
throw e;
}
}
}
generateClients();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment