basic-1.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.methods.initialize(new anchor.BN(1234)).
  18. accounts({
  19. myAccount: myAccount.publicKey,
  20. user: provider.wallet.publicKey,
  21. systemProgram: SystemProgram.programId,
  22. }).
  23. signers([myAccount]).rpc();
  24. // #endregion code-simplified
  25. // Fetch the newly created account from the cluster.
  26. const account = await program.account.myAccount.fetch(myAccount.publicKey);
  27. // Check it's state was initialized.
  28. assert.ok(account.data.eq(new anchor.BN(1234)));
  29. // Store the account for the next test.
  30. _myAccount = myAccount;
  31. });
  32. it("Updates a previously created account", async () => {
  33. const myAccount = _myAccount;
  34. // #region update-test
  35. // The program to execute.
  36. const program = anchor.workspace.Basic1;
  37. // Invoke the update rpc.
  38. await program.methods.update(new anchor.BN(4321)).accounts({
  39. myAccount: myAccount.publicKey,
  40. }).rpc();
  41. // Fetch the newly updated account.
  42. const account = await program.account.myAccount.fetch(myAccount.publicKey);
  43. // Check it's state was mutated.
  44. assert.ok(account.data.eq(new anchor.BN(4321)));
  45. // #endregion update-test
  46. });
  47. });