litesvm.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Program } from '@coral-xyz/anchor';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, PublicKey } from '@solana/web3.js';
  4. import { LiteSVMProvider, fromWorkspace } from 'anchor-litesvm';
  5. import { AnchorProgramExample } from '../target/types/anchor_program_example';
  6. const IDL = require('../target/idl/anchor_program_example.json');
  7. describe('PDAs', async () => {
  8. const client = fromWorkspace('');
  9. const provider = new LiteSVMProvider(client);
  10. const payer = provider.wallet.payer;
  11. const program = new anchor.Program<AnchorProgramExample>(IDL, provider);
  12. // PDA for the page visits account
  13. const [pageVisitPDA] = PublicKey.findProgramAddressSync([Buffer.from('page_visits'), payer.publicKey.toBuffer()], program.programId);
  14. it('Create the page visits tracking PDA', async () => {
  15. await program.methods
  16. .createPageVisits()
  17. .accounts({
  18. payer: payer.publicKey,
  19. })
  20. .rpc();
  21. });
  22. it('Visit the page!', async () => {
  23. await program.methods
  24. .incrementPageVisits()
  25. .accounts({
  26. user: payer.publicKey,
  27. })
  28. .rpc();
  29. });
  30. it('Visit the page!', async () => {
  31. await client.expireBlockhash();
  32. const recentBlockhash = await client.latestBlockhash();
  33. const tx = await program.methods.incrementPageVisits().accounts({ user: payer.publicKey }).transaction();
  34. tx.recentBlockhash = recentBlockhash;
  35. tx.feePayer = payer.publicKey;
  36. await provider.sendAndConfirm(tx, [payer]);
  37. });
  38. it('View page visits', async () => {
  39. const pageVisits = await program.account.pageVisits.fetch(pageVisitPDA);
  40. console.log(`Number of page visits: ${pageVisits.pageVisits}`);
  41. });
  42. });