idl.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { IdlCommandsOne } from "../target/types/idl_commands_one";
  4. import { IdlCommandsTwo } from "../target/types/idl_commands_two";
  5. import { assert } from "chai";
  6. import { execSync } from "child_process";
  7. import * as fs from "fs";
  8. describe("Test CLI IDL commands", () => {
  9. // Configure the client to use the local cluster.
  10. const provider = anchor.AnchorProvider.env();
  11. anchor.setProvider(provider);
  12. const programOne = anchor.workspace.IdlCommandsOne as Program<IdlCommandsOne>;
  13. const programTwo = anchor.workspace.IdlCommandsTwo as Program<IdlCommandsTwo>;
  14. it("Can initialize IDL account", async () => {
  15. execSync(
  16. `anchor idl init --filepath target/idl/idl_commands_one.json ${programOne.programId}`,
  17. { stdio: "inherit" }
  18. );
  19. });
  20. it("Can fetch an IDL using the TypeScript client", async () => {
  21. const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
  22. assert.deepEqual(idl, programOne.idl);
  23. });
  24. it("Can fetch an IDL via the CLI", async () => {
  25. const idl = execSync(`anchor idl fetch ${programOne.programId}`).toString();
  26. assert.deepEqual(JSON.parse(idl), programOne.idl);
  27. });
  28. it("Can write a new IDL using the upgrade command", async () => {
  29. // Upgrade the IDL of program one to the IDL of program two to test upgrade
  30. execSync(
  31. `anchor idl upgrade --filepath target/idl/idl_commands_two.json ${programOne.programId}`,
  32. { stdio: "inherit" }
  33. );
  34. const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
  35. assert.deepEqual(idl, programTwo.idl);
  36. });
  37. it("Can write a new IDL using write-buffer and set-buffer", async () => {
  38. // "Upgrade" back to program one via write-buffer set-buffer
  39. let buffer = execSync(
  40. `anchor idl write-buffer --filepath target/idl/idl_commands_one.json ${programOne.programId}`
  41. ).toString();
  42. buffer = buffer.replace("Idl buffer created: ", "").trim();
  43. execSync(
  44. `anchor idl set-buffer --buffer ${buffer} ${programOne.programId}`,
  45. { stdio: "inherit" }
  46. );
  47. const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
  48. assert.deepEqual(idl, programOne.idl);
  49. });
  50. it("Can fetch an IDL authority via the CLI", async () => {
  51. const authority = execSync(`anchor idl authority ${programOne.programId}`)
  52. .toString()
  53. .trim();
  54. assert.equal(authority, provider.wallet.publicKey.toString());
  55. });
  56. it("Can close IDL account", async () => {
  57. execSync(`anchor idl close ${programOne.programId}`, { stdio: "inherit" });
  58. const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
  59. assert.isNull(idl);
  60. });
  61. it("Can initialize super massive IDL account", async () => {
  62. execSync(
  63. `anchor idl init --filepath testLargeIdl.json ${programOne.programId}`,
  64. { stdio: "inherit" }
  65. );
  66. const idlActual = await anchor.Program.fetchIdl(
  67. programOne.programId,
  68. provider
  69. );
  70. const idlExpected = JSON.parse(
  71. fs.readFileSync("testLargeIdl.json", "utf8")
  72. );
  73. assert.deepEqual(idlActual, idlExpected);
  74. });
  75. });