basic.spec.ts 4.4 KB

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