program-derived-addresses.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { Program } from '@coral-xyz/anchor';
  3. import { Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
  4. import { assert } from 'chai';
  5. import { ProgramDerivedAddresses } from '../target/types/program_derived_addresses';
  6. describe('program-derived-addresses', () => {
  7. // Configure the client to use the local cluster
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. // Generate new user keypairs for testing
  11. const user = Keypair.generate();
  12. const program = anchor.workspace.ProgramDerivedAddresses as Program<ProgramDerivedAddresses>;
  13. // Variables for storing Program Derived Address (PDA) and bump
  14. let pageVisitsPDA: PublicKey; // PDA for page visits account
  15. let pageVisitsBump: number;
  16. // Define a randomized seed that'll be used for the account
  17. const seed = new anchor.BN(Math.floor(Math.random() * 10000));
  18. // Before all tests, airdrop SOL to user for transaction fees and derive the PDA for the page visits account
  19. before(async () => {
  20. const latestBlockHash = await provider.connection.getLatestBlockhash();
  21. // Airdrop 1 SOL to the first user
  22. const airdropUser = await provider.connection.requestAirdrop(user.publicKey, 1 * LAMPORTS_PER_SOL);
  23. await provider.connection.confirmTransaction({
  24. blockhash: latestBlockHash.blockhash,
  25. lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
  26. signature: airdropUser,
  27. });
  28. // Derive PDA for the first user account
  29. [pageVisitsPDA, pageVisitsBump] = PublicKey.findProgramAddressSync(
  30. [seed.toArrayLike(Buffer, 'le', 8), user.publicKey.toBuffer()],
  31. program.programId,
  32. );
  33. });
  34. it('It creates page visits tracking account!', async () => {
  35. // Invoke the Create Page Visits instruction from the program
  36. await program.methods
  37. .createPageVisits(seed)
  38. .accountsPartial({
  39. payer: user.publicKey,
  40. pageVisits: pageVisitsPDA,
  41. })
  42. .signers([user])
  43. .rpc();
  44. });
  45. it('Visit the page!', async () => {
  46. // Invoke the Increment Page Visits instruction from the program
  47. await program.methods
  48. .incrementPageVisits()
  49. .accountsPartial({
  50. payer: user.publicKey,
  51. pageVisits: pageVisitsPDA,
  52. })
  53. .signers([user])
  54. .rpc();
  55. // Fetch the page visits account information
  56. const pageVisitsInfo = await program.account.pageVisits.fetch(pageVisitsPDA);
  57. // Assert that the page visits count is 1
  58. assert.equal(pageVisitsInfo.pageVisits, 1, 'This is supposed to be the first visit, so value should be 1');
  59. console.log('\nNumber of page visits: ', pageVisitsInfo.pageVisits);
  60. });
  61. it('Visit the page, again!', async () => {
  62. await program.methods
  63. .incrementPageVisits()
  64. .accountsPartial({
  65. payer: user.publicKey,
  66. pageVisits: pageVisitsPDA,
  67. })
  68. .signers([user])
  69. .rpc();
  70. // Fetch the page visits account information
  71. const pageVisitsInfo = await program.account.pageVisits.fetch(pageVisitsPDA);
  72. // Assert that the page visits count is 2
  73. assert.equal(pageVisitsInfo.pageVisits, 2, 'This is supposed to be the second visit, so value should be 2');
  74. console.log('\nNumber of page visits: ', pageVisitsInfo.pageVisits);
  75. });
  76. });