basic.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as assert from 'assert';
  2. import * as anchor from '../../';
  3. import { readFileSync } from 'fs';
  4. // Global workspace settings.
  5. const WORKSPACE = {
  6. idl: JSON.parse(readFileSync('../examples/basic/idl.json', 'utf8')),
  7. programId: new anchor.web3.PublicKey('CrQZpSbUnkXxwf1FnepmefoZ7VsbYE6HXmG1TjChH6y'),
  8. provider: anchor.Provider.local(),
  9. };
  10. describe('Constraints program tests', () => {
  11. it('Parses a workspace', async () => {
  12. });
  13. it('Runs against a localnetwork', async () => {
  14. // Configure the local cluster.
  15. anchor.setProvider(WORKSPACE.provider);
  16. // Generate the program from IDL.
  17. const program = new anchor.Program(WORKSPACE.idl, WORKSPACE.programId);
  18. // New account to create.
  19. const root = new anchor.web3.Account();
  20. // Execute the RPC (instruction) against the cluster, passing in the arguments
  21. // exactly as defined by the Solana program.
  22. //
  23. // The last parameter defines context for the transaction. Consisting of
  24. //
  25. // 1) Any additional instructions one wishes to execute *before* executing
  26. // the program.
  27. // 2) Any signers (in addition to the provider).
  28. // 3) Accounts for the program's instruction. Ordering does *not* matter,
  29. // only that they names are as specified in the IDL.
  30. await program.rpc.createRoot(WORKSPACE.provider.wallet.publicKey, new anchor.BN(1234), {
  31. accounts: {
  32. root: root.publicKey,
  33. },
  34. signers: [root],
  35. instructions: [
  36. anchor.web3.SystemProgram.createAccount({
  37. fromPubkey: WORKSPACE.provider.wallet.publicKey,
  38. newAccountPubkey: root.publicKey,
  39. space: 41,
  40. lamports: await WORKSPACE.provider.connection.getMinimumBalanceForRentExemption(41),
  41. programId: WORKSPACE.programId,
  42. }),
  43. ],
  44. });
  45. // Read the newly created account data.
  46. let account = await program.account.root(root.publicKey);
  47. assert.ok(account.initialized);
  48. assert.ok(account.data.eq(new anchor.BN(1234)));
  49. assert.ok(account.authority.equals(WORKSPACE.provider.wallet.publicKey));
  50. // Execute another RPC to update the data.
  51. await program.rpc.updateRoot(new anchor.BN(999), {
  52. accounts: {
  53. root: root.publicKey,
  54. authority: WORKSPACE.provider.wallet.publicKey,
  55. },
  56. });
  57. // Check the update actually persisted.
  58. account = await program.account.root(root.publicKey);
  59. assert.ok(account.data.eq(new anchor.BN(999)));
  60. // Create and initialize a leaf account.
  61. const leaf = new anchor.web3.Account();
  62. let customType = { myData: new anchor.BN(4), key: WORKSPACE.programId };
  63. await program.rpc.createLeaf(new anchor.BN(2), customType, {
  64. accounts: {
  65. root: root.publicKey,
  66. leaf: leaf.publicKey,
  67. },
  68. signers: [leaf],
  69. instructions: [
  70. anchor.web3.SystemProgram.createAccount({
  71. fromPubkey: WORKSPACE.provider.wallet.publicKey,
  72. newAccountPubkey: leaf.publicKey,
  73. space: 100,
  74. lamports: await WORKSPACE.provider.connection.getMinimumBalanceForRentExemption(100),
  75. programId: WORKSPACE.programId,
  76. }),
  77. ],
  78. });
  79. // Check the account was initialized.
  80. account = await program.account.leaf(leaf.publicKey);
  81. assert.ok(account.initialized);
  82. assert.ok(account.root.equals(root.publicKey));
  83. assert.ok(account.data.eq(new anchor.BN(2)));
  84. assert.ok(account.custom.myData.eq(new anchor.BN(4)));
  85. assert.ok(account.custom.key.equals(WORKSPACE.programId));
  86. // Update the account.
  87. await program.rpc.updateLeaf(new anchor.BN(5), null, {
  88. accounts: {
  89. authority: WORKSPACE.provider.wallet.publicKey,
  90. root: root.publicKey,
  91. leaf: leaf.publicKey,
  92. },
  93. });
  94. // Check it was updated.
  95. account = await program.account.leaf(leaf.publicKey);
  96. assert.ok(account.data.eq(new anchor.BN(5)));
  97. // Now update with the option.
  98. customType = { myData: new anchor.BN(7), key: WORKSPACE.programId };
  99. await program.rpc.updateLeaf(new anchor.BN(6), customType, {
  100. accounts: {
  101. authority: WORKSPACE.provider.wallet.publicKey,
  102. root: root.publicKey,
  103. leaf: leaf.publicKey,
  104. },
  105. });
  106. // Check it was updated.
  107. account = await program.account.leaf(leaf.publicKey);
  108. assert.ok(account.data.eq(new anchor.BN(6)));
  109. assert.ok(account.custom.myData.eq(new anchor.BN(7)));
  110. assert.ok(account.custom.key.equals(WORKSPACE.programId));
  111. });
  112. });