generate-clients.mjs.njk 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env zx
  2. import "zx/globals";
  3. import * as k from "kinobi";
  4. import { rootNodeFromAnchor } from "@kinobi-so/nodes-from-anchor";
  5. {% if jsClient %}
  6. import { renderVisitor as renderJavaScriptVisitor } from "@kinobi-so/renderers-js";
  7. {% endif %}
  8. {% if rustClient %}
  9. import { renderVisitor as renderRustVisitor } from "@kinobi-so/renderers-rust";
  10. {% endif %}
  11. import { getAllProgramIdls } from "./utils.mjs";
  12. // Instanciate Kinobi.
  13. const [idl, ...additionalIdls] = getAllProgramIdls().map(idl => rootNodeFromAnchor(require(idl)))
  14. const kinobi = k.createFromRoot(idl, additionalIdls);
  15. // Update programs.
  16. kinobi.update(
  17. k.updateProgramsVisitor({
  18. "{{ programCrateName | camelCase }}": { name: "{{ programName | camelCase }}" },
  19. })
  20. );
  21. // Update accounts.
  22. kinobi.update(
  23. k.updateAccountsVisitor({
  24. counter: {
  25. seeds: [
  26. k.constantPdaSeedNodeFromString("utf8", "counter"),
  27. k.variablePdaSeedNode(
  28. "authority",
  29. k.publicKeyTypeNode(),
  30. "The authority of the counter account"
  31. ),
  32. ],
  33. },
  34. })
  35. );
  36. // Update instructions.
  37. kinobi.update(
  38. k.updateInstructionsVisitor({
  39. create: {
  40. byteDeltas: [k.instructionByteDeltaNode(k.accountLinkNode("counter"))],
  41. accounts: {
  42. counter: { defaultValue: k.pdaValueNode("counter") },
  43. payer: { defaultValue: k.accountValueNode("authority") },
  44. },
  45. },
  46. increment: {
  47. accounts: {
  48. counter: { defaultValue: k.pdaValueNode("counter") },
  49. },
  50. arguments: {
  51. amount: { defaultValue: k.noneValueNode() },
  52. },
  53. },
  54. })
  55. );
  56. // Set account discriminators.
  57. const key = (name) => ({ field: "key", value: k.enumValueNode("Key", name) });
  58. kinobi.update(
  59. k.setAccountDiscriminatorFromFieldVisitor({
  60. counter: key("counter"),
  61. })
  62. );
  63. {% if jsClient %}
  64. // Render JavaScript.
  65. const jsClient = path.join(__dirname, "..", "clients", "js");
  66. kinobi.accept(
  67. renderJavaScriptVisitor(path.join(jsClient, "src", "generated"), {
  68. prettier: require(path.join(jsClient, ".prettierrc.json"))
  69. })
  70. );
  71. {% endif %}
  72. {% if rustClient %}
  73. // Render Rust.
  74. const rustClient = path.join(__dirname, "..", "clients", "rust");
  75. kinobi.accept(
  76. renderRustVisitor(path.join(rustClient, "src", "generated"), {
  77. formatCode: true,
  78. crateFolder: rustClient,
  79. })
  80. );
  81. {% endif %}