generate-clients.mjs.njk 1.9 KB

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