basic-1.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const assert = require("assert");
  2. const anchor = require("@project-serum/anchor");
  3. const { SystemProgram } = anchor.web3;
  4. describe("basic-1", () => {
  5. // Use a local provider.
  6. const provider = anchor.AnchorProvider.local();
  7. // Configure the client to use the local cluster.
  8. anchor.setProvider(provider);
  9. it("Creates and initializes an account in a single atomic transaction (simplified)", async () => {
  10. // #region code-simplified
  11. // The program to execute.
  12. const program = anchor.workspace.Basic1;
  13. // The Account to create.
  14. const myAccount = anchor.web3.Keypair.generate();
  15. // Create the new account and initialize it with the program.
  16. // #region code-simplified
  17. await program.rpc.initialize(new anchor.BN(1234), {
  18. accounts: {
  19. myAccount: myAccount.publicKey,
  20. user: provider.wallet.publicKey,
  21. systemProgram: SystemProgram.programId,
  22. },
  23. signers: [myAccount],
  24. });
  25. // #endregion code-simplified
  26. // Fetch the newly created account from the cluster.
  27. const account = await program.account.myAccount.fetch(myAccount.publicKey);
  28. // Check it's state was initialized.
  29. assert.ok(account.data.eq(new anchor.BN(1234)));
  30. // Store the account for the next test.
  31. _myAccount = myAccount;
  32. });
  33. it("Updates a previously created account", async () => {
  34. const myAccount = _myAccount;
  35. // #region update-test
  36. // The program to execute.
  37. const program = anchor.workspace.Basic1;
  38. // Invoke the update rpc.
  39. await program.rpc.update(new anchor.BN(4321), {
  40. accounts: {
  41. myAccount: myAccount.publicKey,
  42. },
  43. });
  44. // Fetch the newly updated account.
  45. const account = await program.account.myAccount.fetch(myAccount.publicKey);
  46. // Check it's state was mutated.
  47. assert.ok(account.data.eq(new anchor.BN(4321)));
  48. // #endregion update-test
  49. });
  50. });