basic-1.js 3.3 KB

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