declare-program.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as anchor from "@coral-xyz/anchor";
  2. import assert from "assert";
  3. import type { DeclareProgram } from "../target/types/declare_program";
  4. import type { External } from "../target/types/external";
  5. describe("declare-program", () => {
  6. anchor.setProvider(anchor.AnchorProvider.env());
  7. const program: anchor.Program<DeclareProgram> =
  8. anchor.workspace.declareProgram;
  9. const externalProgram: anchor.Program<External> = anchor.workspace.external;
  10. // TODO: Add a utility type that does this?
  11. let pubkeys: Awaited<
  12. ReturnType<
  13. ReturnType<typeof externalProgram["methods"]["init"]>["rpcAndKeys"]
  14. >
  15. >["pubkeys"];
  16. before(async () => {
  17. pubkeys = (await externalProgram.methods.init().rpcAndKeys()).pubkeys;
  18. });
  19. it("Can CPI", async () => {
  20. const value = 5;
  21. await program.methods
  22. .cpi(value)
  23. .accounts({ cpiMyAccount: pubkeys.myAccount })
  24. .rpc();
  25. const myAccount = await externalProgram.account.myAccount.fetch(
  26. pubkeys.myAccount
  27. );
  28. assert.strictEqual(myAccount.field, value);
  29. });
  30. it("Can CPI composite", async () => {
  31. const value = 3;
  32. await program.methods
  33. .cpiComposite(value)
  34. .accounts({ cpiMyAccount: pubkeys.myAccount })
  35. .rpc();
  36. const myAccount = await externalProgram.account.myAccount.fetch(
  37. pubkeys.myAccount
  38. );
  39. assert.strictEqual(myAccount.field, value);
  40. });
  41. it("Can use account utils", async () => {
  42. await program.methods.accountUtils().rpc();
  43. });
  44. it("Can use event utils", async () => {
  45. await program.methods.eventUtils().rpc();
  46. });
  47. });