basic-3.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const assert = require("assert");
  2. const anchor = require("@project-serum/anchor");
  3. describe("basic-3", () => {
  4. const provider = anchor.Provider.local();
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(provider);
  7. it("Performs CPI from puppet master to puppet", async () => {
  8. const puppetMaster = anchor.workspace.PuppetMaster;
  9. const puppet = anchor.workspace.Puppet;
  10. // Initialize a new puppet account.
  11. const newPuppetAccount = new anchor.web3.Account();
  12. const tx = await puppet.rpc.initialize({
  13. accounts: {
  14. puppet: newPuppetAccount.publicKey,
  15. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  16. },
  17. signers: [newPuppetAccount],
  18. instructions: [
  19. anchor.web3.SystemProgram.createAccount({
  20. fromPubkey: provider.wallet.publicKey,
  21. newAccountPubkey: newPuppetAccount.publicKey,
  22. space: 8 + 8, // Add 8 for the account discriminator.
  23. lamports: await provider.connection.getMinimumBalanceForRentExemption(
  24. 8 + 8
  25. ),
  26. programId: puppet.programId,
  27. }),
  28. ],
  29. });
  30. // Invoke the puppet master to perform a CPI to the puppet.
  31. await puppetMaster.rpc.pullStrings(new anchor.BN(111), {
  32. accounts: {
  33. puppet: newPuppetAccount.publicKey,
  34. puppetProgram: puppet.programId,
  35. },
  36. });
  37. // Check the state updated.
  38. puppetAccount = await puppet.account.puppet(newPuppetAccount.publicKey);
  39. assert.ok(puppetAccount.data.eq(new anchor.BN(111)));
  40. });
  41. });