bankrun.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { PublicKey } from '@solana/web3.js';
  5. import { BankrunProvider } from 'anchor-bankrun';
  6. import { startAnchor } from 'solana-bankrun';
  7. import type { AnchorProgramExample } from '../target/types/anchor_program_example';
  8. const IDL = require('../target/idl/anchor_program_example.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('Bankrun example', async () => {
  11. const context = await startAnchor('', [{ name: 'anchor_program_example', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const wallet = provider.wallet as anchor.Wallet;
  14. const program = new anchor.Program<AnchorProgramExample>(IDL, provider);
  15. const client = context.banksClient;
  16. // We'll create this ahead of time.
  17. // Our program will try to modify it.
  18. const accountToChange = new Keypair();
  19. // Our program will create this.
  20. const accountToCreate = new Keypair();
  21. it('Create an account owned by our program', async () => {
  22. const instruction = SystemProgram.createAccount({
  23. fromPubkey: provider.wallet.publicKey,
  24. newAccountPubkey: accountToChange.publicKey,
  25. lamports: await provider.connection.getMinimumBalanceForRentExemption(0),
  26. space: 0,
  27. programId: program.programId, // Our program
  28. });
  29. const transaction = new Transaction();
  30. const blockhash = context.lastBlockhash;
  31. transaction.recentBlockhash = blockhash;
  32. transaction.add(instruction).sign(wallet.payer, accountToChange);
  33. await client.processTransaction(transaction);
  34. });
  35. it('Check accounts', async () => {
  36. await program.methods
  37. .checkAccounts()
  38. .accounts({
  39. payer: wallet.publicKey,
  40. accountToCreate: accountToCreate.publicKey,
  41. accountToChange: accountToChange.publicKey,
  42. })
  43. .rpc();
  44. });
  45. });