basic-1.js 1.9 KB

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