basic-1.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const assert = require("assert");
  2. const anchor = require("@project-serum/anchor");
  3. describe("basic-1", () => {
  4. // Use a local provider.
  5. const provider = anchor.Provider.local();
  6. // Configure the client to use the local cluster.
  7. anchor.setProvider(provider);
  8. it("Creates and initializes an account in two different transactions", async () => {
  9. // The program owning the account to create.
  10. const program = anchor.workspace.Basic1;
  11. // The Account to create.
  12. const myAccount = new anchor.web3.Account();
  13. // Create account transaction.
  14. const tx = new anchor.web3.Transaction();
  15. tx.add(
  16. anchor.web3.SystemProgram.createAccount({
  17. fromPubkey: provider.wallet.publicKey,
  18. newAccountPubkey: myAccount.publicKey,
  19. space: 8 + 8,
  20. lamports: await provider.connection.getMinimumBalanceForRentExemption(
  21. 8 + 8
  22. ),
  23. programId: program.programId,
  24. })
  25. );
  26. // Execute the transaction against the cluster.
  27. await provider.send(tx, [myAccount]);
  28. // Execute the RPC.
  29. // #region code-separated
  30. await program.rpc.initialize(new anchor.BN(1234), {
  31. accounts: {
  32. myAccount: myAccount.publicKey,
  33. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  34. },
  35. });
  36. // #endregion code-separated
  37. // Fetch the newly created account from the cluster.
  38. const account = await program.account.myAccount(myAccount.publicKey);
  39. // Check it's state was initialized.
  40. assert.ok(account.data.eq(new anchor.BN(1234)));
  41. });
  42. // Reference to an account to use between multiple tests.
  43. let _myAccount = undefined;
  44. it("Creates and initializes an account in a single atomic transaction", async () => {
  45. // The program to execute.
  46. const program = anchor.workspace.Basic1;
  47. // #region code
  48. // The Account to create.
  49. const myAccount = new anchor.web3.Account();
  50. // Atomically create the new account and initialize it with the program.
  51. await program.rpc.initialize(new anchor.BN(1234), {
  52. accounts: {
  53. myAccount: myAccount.publicKey,
  54. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  55. },
  56. signers: [myAccount],
  57. instructions: [
  58. anchor.web3.SystemProgram.createAccount({
  59. fromPubkey: provider.wallet.publicKey,
  60. newAccountPubkey: myAccount.publicKey,
  61. space: 8 + 8, // Add 8 for the account discriminator.
  62. lamports: await provider.connection.getMinimumBalanceForRentExemption(
  63. 8 + 8
  64. ),
  65. programId: program.programId,
  66. }),
  67. ],
  68. });
  69. // Fetch the newly created account from the cluster.
  70. const account = await program.account.myAccount(myAccount.publicKey);
  71. // Check it's state was initialized.
  72. assert.ok(account.data.eq(new anchor.BN(1234)));
  73. // #endregion code
  74. });
  75. it("Creates and initializes an account in a single atomic transaction (simplified)", async () => {
  76. // The program to execute.
  77. const program = anchor.workspace.Basic1;
  78. // The Account to create.
  79. const myAccount = new anchor.web3.Account();
  80. // Atomically create the new account and initialize it with the program.
  81. // #region code-simplified
  82. await program.rpc.initialize(new anchor.BN(1234), {
  83. accounts: {
  84. myAccount: myAccount.publicKey,
  85. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  86. },
  87. signers: [myAccount],
  88. instructions: [await program.account.myAccount.createInstruction(myAccount)],
  89. });
  90. // #endregion code-simplified
  91. // Fetch the newly created account from the cluster.
  92. const account = await program.account.myAccount(myAccount.publicKey);
  93. // Check it's state was initialized.
  94. assert.ok(account.data.eq(new anchor.BN(1234)));
  95. // Store the account for the next test.
  96. _myAccount = myAccount;
  97. });
  98. it("Updates a previously created account", async () => {
  99. const myAccount = _myAccount;
  100. // #region update-test
  101. // The program to execute.
  102. const program = anchor.workspace.Basic1;
  103. // Invoke the update rpc.
  104. await program.rpc.update(new anchor.BN(4321), {
  105. accounts: {
  106. myAccount: myAccount.publicKey,
  107. },
  108. });
  109. // Fetch the newly updated account.
  110. const account = await program.account.myAccount(myAccount.publicKey);
  111. // Check it's state was mutated.
  112. assert.ok(account.data.eq(new anchor.BN(4321)));
  113. // #endregion update-test
  114. });
  115. });