test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { Lever } from "../target/types/lever";
  4. import { Hand } from "../target/types/hand";
  5. describe("cross-program-invocation", () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. // Generate a new keypair for the data accounts for each program
  10. const dataAccountLever = anchor.web3.Keypair.generate();
  11. const dataAccountHand = anchor.web3.Keypair.generate();
  12. const wallet = provider.wallet;
  13. // The lever program and hand program
  14. const leverProgram = anchor.workspace.Lever as Program<Lever>;
  15. const handProgram = anchor.workspace.Hand as Program<Hand>;
  16. it("Initialize the lever!", async () => {
  17. // Initialize data account for the lever program
  18. const tx = await leverProgram.methods
  19. .new()
  20. .accounts({ dataAccount: dataAccountLever.publicKey })
  21. .signers([dataAccountLever])
  22. .rpc();
  23. console.log("Your transaction signature", tx);
  24. // Fetch the state of the data account
  25. const val = await leverProgram.methods
  26. .get()
  27. .accounts({ dataAccount: dataAccountLever.publicKey })
  28. .view();
  29. console.log("State:", val);
  30. });
  31. it("Pull the lever!", async () => {
  32. // Initialize data account for the hand program
  33. // This is required by Solang, but the account is not used
  34. const tx = await handProgram.methods
  35. .new()
  36. .accounts({ dataAccount: dataAccountHand.publicKey })
  37. .signers([dataAccountHand])
  38. .rpc();
  39. console.log("Your transaction signature", tx);
  40. // Call the pullLever instruction on the hand program, which invokes the lever program via CPI
  41. const tx2 = await handProgram.methods
  42. .pullLever("Chris")
  43. .accounts({
  44. leverData: dataAccountLever.publicKey, // The lever program's data account, which stores the state
  45. leverInterface_programId: leverProgram.programId // The lever program's program ID
  46. })
  47. .rpc({ skipPreflight: true });
  48. console.log("Your transaction signature", tx2);
  49. // Fetch the state of the data account
  50. const val = await leverProgram.methods
  51. .get()
  52. .accounts({ dataAccount: dataAccountLever.publicKey })
  53. .view();
  54. console.log("State:", val);
  55. });
  56. it("Pull it again!", async () => {
  57. // Call the pullLever instruction on the hand program, which invokes the lever program via CPI
  58. const tx = await handProgram.methods
  59. .pullLever("Ashley")
  60. .accounts({
  61. leverData: dataAccountLever.publicKey, // The lever program's data account, which stores the state
  62. leverInterface_programId: leverProgram.programId, // The lever program's program ID
  63. })
  64. .rpc({ skipPreflight: true });
  65. console.log("Your transaction signature", tx);
  66. // Fetch the state of the data account
  67. const val = await leverProgram.methods
  68. .get()
  69. .accounts({ dataAccount: dataAccountLever.publicKey })
  70. .view();
  71. console.log("State:", val);
  72. });
  73. });