bankrun.test.ts 1.9 KB

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