basic-1.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(8+8),
  21. programId: program.programId,
  22. }),
  23. );
  24. // Execute the transaction against the cluster.
  25. await provider.send(tx, [myAccount]);
  26. // Execute the RPC.
  27. // #region code-separated
  28. await program.rpc.initialize(new anchor.BN(1234), {
  29. accounts: {
  30. myAccount: myAccount.publicKey,
  31. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  32. },
  33. });
  34. // #endregion code-separated
  35. // Fetch the newly created account from the cluster.
  36. const account = await program.account.myAccount(myAccount.publicKey);
  37. // Check it's state was initialized.
  38. assert.ok(account.data.eq(new anchor.BN(1234)));
  39. });
  40. // Reference to an account to use between multiple tests.
  41. let _myAccount = undefined;
  42. it('Creates and initializes an account in a single atomic transaction', async () => {
  43. // The program to execute.
  44. const program = anchor.workspace.Basic1;
  45. // #region code
  46. // The Account to create.
  47. const myAccount = new anchor.web3.Account();
  48. // Atomically create the new account and initialize it with the program.
  49. await program.rpc.initialize(new anchor.BN(1234), {
  50. accounts: {
  51. myAccount: myAccount.publicKey,
  52. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  53. },
  54. signers: [myAccount],
  55. instructions: [
  56. anchor.web3.SystemProgram.createAccount({
  57. fromPubkey: provider.wallet.publicKey,
  58. newAccountPubkey: myAccount.publicKey,
  59. space: 8+8, // Add 8 for the account discriminator.
  60. lamports: await provider.connection.getMinimumBalanceForRentExemption(8+8),
  61. programId: program.programId,
  62. }),
  63. ],
  64. });
  65. // Fetch the newly created account from the cluster.
  66. const account = await program.account.myAccount(myAccount.publicKey);
  67. // Check it's state was initialized.
  68. assert.ok(account.data.eq(new anchor.BN(1234)));
  69. // #endregion code
  70. // Store the account for the next test.
  71. _myAccount = myAccount;
  72. });
  73. it('Updates a previously created account', async () => {
  74. const myAccount = _myAccount;
  75. // #region update-test
  76. // The program to execute.
  77. const program = anchor.workspace.Basic1;
  78. // Invoke the update rpc.
  79. await program.rpc.update(new anchor.BN(4321), {
  80. accounts: {
  81. myAccount: myAccount.publicKey,
  82. },
  83. });
  84. // Fetch the newly updated account.
  85. const account = await program.account.myAccount(myAccount.publicKey);
  86. // Check it's state was mutated.
  87. assert.ok(account.data.eq(new anchor.BN(4321)));
  88. // #endregion update-test
  89. });
  90. });