test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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('CrQZpSbUnkXxwf1FnepmefoZ7VsbYE6HXmG1TjChH6y'),
  7. provider: anchor.Provider.local(),
  8. };
  9. async function test() {
  10. console.log('Starting test.');
  11. // Configure the local cluster.
  12. anchor.setProvider(WORKSPACE.provider);
  13. // Generate the program from IDL.
  14. const program = new anchor.Program(WORKSPACE.idl, WORKSPACE.programId);
  15. // New account to create.
  16. const root = new anchor.web3.Account();
  17. // Execute the RPC (instruction) against the cluster, passing in the arguments
  18. // exactly as defined by the Solana program.
  19. //
  20. // The last parameter defines context for the transaction. Consisting of
  21. //
  22. // 1) Any additional instructions one wishes to execute *before* executing
  23. // the program.
  24. // 2) Any signers (in addition to the provider).
  25. // 3) Accounts for the program's instruction. Ordering does *not* matter,
  26. // only that they names are as specified in the IDL.
  27. await program.rpc.createRoot(WORKSPACE.provider.wallet.publicKey, new anchor.BN(1234), {
  28. accounts: {
  29. root: root.publicKey,
  30. },
  31. signers: [root],
  32. instructions: [
  33. anchor.web3.SystemProgram.createAccount({
  34. fromPubkey: WORKSPACE.provider.wallet.publicKey,
  35. newAccountPubkey: root.publicKey,
  36. space: 41,
  37. lamports: await WORKSPACE.provider.connection.getMinimumBalanceForRentExemption(41),
  38. programId: WORKSPACE.programId,
  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. // Create and initialize a leaf account.
  58. const leaf = new anchor.web3.Account();
  59. let customType = { myData: new anchor.BN(4), key: WORKSPACE.programId };
  60. await program.rpc.createLeaf(new anchor.BN(2), customType, {
  61. accounts: {
  62. root: root.publicKey,
  63. leaf: leaf.publicKey,
  64. },
  65. signers: [leaf],
  66. instructions: [
  67. anchor.web3.SystemProgram.createAccount({
  68. fromPubkey: WORKSPACE.provider.wallet.publicKey,
  69. newAccountPubkey: leaf.publicKey,
  70. space: 100,
  71. lamports: await WORKSPACE.provider.connection.getMinimumBalanceForRentExemption(100),
  72. programId: WORKSPACE.programId,
  73. }),
  74. ],
  75. });
  76. // Check the account was initialized.
  77. account = await program.account.leaf(leaf.publicKey);
  78. assert.ok(account.initialized);
  79. assert.ok(account.root.equals(root.publicKey));
  80. assert.ok(account.data.eq(new anchor.BN(2)));
  81. assert.ok(account.custom.myData.eq(new anchor.BN(4)));
  82. assert.ok(account.custom.key.equals(WORKSPACE.programId));
  83. // Update the account.
  84. await program.rpc.updateLeaf(new anchor.BN(5), null, {
  85. accounts: {
  86. authority: WORKSPACE.provider.wallet.publicKey,
  87. root: root.publicKey,
  88. leaf: leaf.publicKey,
  89. },
  90. });
  91. // Check it was updated.
  92. account = await program.account.leaf(leaf.publicKey);
  93. assert.ok(account.data.eq(new anchor.BN(5)));
  94. // Now update with the option.
  95. customType = { myData: new anchor.BN(7), key: WORKSPACE.programId };
  96. await program.rpc.updateLeaf(new anchor.BN(6), customType, {
  97. accounts: {
  98. authority: WORKSPACE.provider.wallet.publicKey,
  99. root: root.publicKey,
  100. leaf: leaf.publicKey,
  101. },
  102. });
  103. // Check it was updated.
  104. account = await program.account.leaf(leaf.publicKey);
  105. assert.ok(account.data.eq(new anchor.BN(6)));
  106. assert.ok(account.custom.myData.eq(new anchor.BN(7)));
  107. assert.ok(account.custom.key.equals(WORKSPACE.programId));
  108. console.log('Test complete.');
  109. }
  110. test();