test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(dataAccountLever.publicKey, "Chris")
  43. .accounts({ dataAccount: dataAccountHand.publicKey })
  44. .remainingAccounts([
  45. {
  46. pubkey: dataAccountLever.publicKey, // The lever program's data account, which stores the state
  47. isWritable: true,
  48. isSigner: false,
  49. },
  50. {
  51. pubkey: leverProgram.programId, // The lever program's program ID
  52. isWritable: false,
  53. isSigner: false,
  54. },
  55. ])
  56. .rpc({ skipPreflight: true });
  57. console.log("Your transaction signature", tx2);
  58. // Fetch the state of the data account
  59. const val = await leverProgram.methods
  60. .get()
  61. .accounts({ dataAccount: dataAccountLever.publicKey })
  62. .view();
  63. console.log("State:", val);
  64. });
  65. it("Pull it again!", async () => {
  66. // Call the pullLever instruction on the hand program, which invokes the lever program via CPI
  67. const tx = await handProgram.methods
  68. .pullLever(dataAccountLever.publicKey, "Ashley")
  69. .accounts({ dataAccount: dataAccountHand.publicKey })
  70. .remainingAccounts([
  71. {
  72. pubkey: dataAccountLever.publicKey, // The lever program's data account, which stores the state
  73. isWritable: true,
  74. isSigner: false,
  75. },
  76. {
  77. pubkey: leverProgram.programId, // The lever program's program ID
  78. isWritable: false,
  79. isSigner: false,
  80. },
  81. ])
  82. .rpc({ skipPreflight: true });
  83. console.log("Your transaction signature", tx);
  84. // Fetch the state of the data account
  85. const val = await leverProgram.methods
  86. .get()
  87. .accounts({ dataAccount: dataAccountLever.publicKey })
  88. .view();
  89. console.log("State:", val);
  90. });
  91. });