bankrun.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { PublicKey, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { startAnchor } from 'solana-bankrun';
  6. import type { Carnival } from '../target/types/carnival';
  7. const IDL = require('../target/idl/carnival.json');
  8. const PROGRAM_ID = new PublicKey(IDL.address);
  9. describe('Bankrun example', async () => {
  10. const context = await startAnchor('', [{ name: 'carnival', programId: PROGRAM_ID }], []);
  11. const provider = new BankrunProvider(context);
  12. const wallet = provider.wallet as anchor.Wallet;
  13. const program = new anchor.Program<Carnival>(IDL, provider);
  14. async function sendCarnivalInstructions(instructionsList: anchor.web3.TransactionInstruction[]) {
  15. const tx = new Transaction();
  16. for (const ix of instructionsList) {
  17. tx.add(ix);
  18. }
  19. await provider.sendAndConfirm(tx, [wallet.payer]);
  20. }
  21. it('Go on some rides!', async () => {
  22. await sendCarnivalInstructions([
  23. await program.methods.goOnRide('Jimmy', 36, 15, 'Scrambler').instruction(),
  24. await program.methods.goOnRide('Mary', 52, 1, 'Ferris Wheel').instruction(),
  25. await program.methods.goOnRide('Alice', 56, 15, 'Scrambler').instruction(),
  26. await program.methods.goOnRide('Bob', 49, 6, 'Tilt-a-Whirl').instruction(),
  27. ]);
  28. });
  29. it('Play some games!', async () => {
  30. await sendCarnivalInstructions([
  31. await program.methods.playGame('Jimmy', 15, 'I Got It!').instruction(),
  32. await program.methods.playGame('Mary', 1, 'Ring Toss').instruction(),
  33. await program.methods.playGame('Alice', 15, 'Ladder Climb').instruction(),
  34. await program.methods.playGame('Bob', 6, 'Ring Toss').instruction(),
  35. ]);
  36. });
  37. it('Eat some food!', async () => {
  38. await sendCarnivalInstructions([
  39. await program.methods.eatFood('Jimmy', 15, 'Taco Shack').instruction(),
  40. await program.methods.eatFood('Mary', 1, "Larry's Pizza").instruction(),
  41. await program.methods.eatFood('Alice', 15, "Dough Boy's").instruction(),
  42. await program.methods.eatFood('Bob', 6, "Dough Boy's").instruction(),
  43. ]);
  44. });
  45. });