litesvm.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { Program } from '@coral-xyz/anchor';
  2. import { Keypair, PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
  3. import { LiteSVMProvider, fromWorkspace } from 'anchor-litesvm';
  4. import { AnchorProgramExample } from '../target/types/anchor_program_example';
  5. const IDL = require('../target/idl/anchor_program_example.json');
  6. describe('anchor', () => {
  7. let client: any;
  8. let provider: LiteSVMProvider;
  9. let program: Program<AnchorProgramExample>;
  10. let payer: Keypair;
  11. let accountToChange: Keypair;
  12. let accountToCreate: Keypair;
  13. before(async () => {
  14. client = fromWorkspace('');
  15. provider = new LiteSVMProvider(client);
  16. payer = provider.wallet.payer;
  17. program = new Program<AnchorProgramExample>(IDL, provider);
  18. // We'll create this ahead of time.
  19. // Our program will try to modify it.
  20. accountToChange = new Keypair();
  21. // Our program will create this.
  22. accountToCreate = new Keypair();
  23. });
  24. it('Create an account owned by our program', async () => {
  25. const instruction = SystemProgram.createAccount({
  26. fromPubkey: provider.wallet.publicKey,
  27. newAccountPubkey: accountToChange.publicKey,
  28. lamports: await provider.connection.getMinimumBalanceForRentExemption(0),
  29. space: 0,
  30. programId: program.programId, // Our program
  31. });
  32. const transaction = new Transaction();
  33. const blockhash = provider.client.latestBlockhash();
  34. transaction.recentBlockhash = blockhash;
  35. transaction.add(instruction).sign(payer, accountToChange);
  36. await provider.sendAndConfirm(transaction);
  37. });
  38. it('Check accounts', async () => {
  39. await program.methods
  40. .checkAccounts()
  41. .accounts({
  42. payer: payer.publicKey,
  43. accountToCreate: accountToCreate.publicKey,
  44. accountToChange: accountToChange.publicKey,
  45. })
  46. .rpc();
  47. });
  48. });