test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const assert = require('assert');
  2. const anchor = require('.');
  3. // Global workspace settings.
  4. const WORKSPACE = {
  5. idl: JSON.parse(require('fs').readFileSync('../examples/basic/idl.json', 'utf8')),
  6. programId: new anchor.web3.PublicKey('3bSz7zXCXFdEBw8AKEWJAa53YswM5aCoNNt5xSR42JDp'),
  7. provider: anchor.Provider.local(),
  8. };
  9. async function test() {
  10. // Configure the local cluster.
  11. anchor.setProvider(WORKSPACE.provider);
  12. // Generate the program from IDL.
  13. const program = new anchor.Program(WORKSPACE.idl, WORKSPACE.programId);
  14. // New account to create.
  15. const root = new anchor.web3.Account();
  16. // Execute the RPC (instruction) against the cluster, passing in the arguments
  17. // exactly as defined by the Solana program.
  18. //
  19. // The last parameter defines context for the transaction. Consisting of
  20. //
  21. // 1) Any additional instructions one wishes to execute *before* executing
  22. // the program.
  23. // 2) Any signers (in addition to the provider).
  24. // 3) Accounts for the program's instruction. Ordering does *not* matter,
  25. // only that they names are as specified in the IDL.
  26. await program.rpc.createRoot(WORKSPACE.provider.wallet.publicKey, new anchor.BN(1234), {
  27. accounts: {
  28. root: root.publicKey,
  29. },
  30. signers: [root],
  31. instructions: [
  32. anchor.web3.SystemProgram.createAccount({
  33. fromPubkey: WORKSPACE.provider.wallet.publicKey,
  34. newAccountPubkey: root.publicKey,
  35. space: 41,
  36. lamports: await WORKSPACE.provider.connection.getMinimumBalanceForRentExemption(41),
  37. programId: WORKSPACE.programId,
  38. }),
  39. ],
  40. }
  41. );
  42. // Read the newly created account data.
  43. let account = await program.account.root(root.publicKey);
  44. assert.ok(account.initialized);
  45. assert.ok(account.data.eq(new anchor.BN(1234)));
  46. assert.ok(account.authority.equals(WORKSPACE.provider.wallet.publicKey));
  47. // Execute another RPC to update the data.
  48. await program.rpc.updateRoot(new anchor.BN(999), {
  49. accounts: {
  50. root: root.publicKey,
  51. authority: WORKSPACE.provider.wallet.publicKey,
  52. },
  53. });
  54. // Check the update actually persisted.
  55. account = await program.account.root(root.publicKey);
  56. assert.ok(account.data.eq(new anchor.BN(999)));
  57. }
  58. test();