bankrun.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { startAnchor } from 'solana-bankrun';
  6. import type { TransferSol } from '../target/types/transfer_sol';
  7. const IDL = require('../target/idl/transfer_sol.json');
  8. const PROGRAM_ID = new PublicKey(IDL.address);
  9. describe('Bankrun example', async () => {
  10. const context = await startAnchor('', [{ name: 'transfer_sol', programId: PROGRAM_ID }], []);
  11. const provider = new BankrunProvider(context);
  12. const payer = provider.wallet as anchor.Wallet;
  13. const program = new anchor.Program<TransferSol>(IDL, provider);
  14. // 1 SOL
  15. const transferAmount = 1 * LAMPORTS_PER_SOL;
  16. // Generate a new keypair for the recipient
  17. const recipient = new Keypair();
  18. // Generate a new keypair to create an account owned by our program
  19. const programOwnedAccount = new Keypair();
  20. it('Transfer SOL with CPI', async () => {
  21. await getBalances(payer.publicKey, recipient.publicKey, 'Beginning');
  22. await program.methods
  23. .transferSolWithCpi(new anchor.BN(transferAmount))
  24. .accounts({
  25. payer: payer.publicKey,
  26. recipient: recipient.publicKey,
  27. })
  28. .rpc();
  29. await getBalances(payer.publicKey, recipient.publicKey, 'Resulting');
  30. });
  31. it('Create and fund account owned by our program', async () => {
  32. const instruction = SystemProgram.createAccount({
  33. fromPubkey: payer.publicKey,
  34. newAccountPubkey: programOwnedAccount.publicKey,
  35. space: 0,
  36. lamports: 1 * LAMPORTS_PER_SOL, // 1 SOL
  37. programId: program.programId, // Program Owner, our program's address
  38. });
  39. const transaction = new Transaction().add(instruction);
  40. await sendAndConfirmTransaction(provider.connection, transaction, [payer.payer, programOwnedAccount]);
  41. });
  42. it('Transfer SOL with Program', async () => {
  43. await getBalances(programOwnedAccount.publicKey, payer.publicKey, 'Beginning');
  44. await program.methods
  45. .transferSolWithProgram(new anchor.BN(transferAmount))
  46. .accounts({
  47. payer: programOwnedAccount.publicKey,
  48. recipient: payer.publicKey,
  49. })
  50. .rpc();
  51. await getBalances(programOwnedAccount.publicKey, payer.publicKey, 'Resulting');
  52. });
  53. async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) {
  54. const payerBalance = await provider.connection.getBalance(payerPubkey);
  55. const recipientBalance = await provider.connection.getBalance(recipientPubkey);
  56. console.log(`${timeframe} balances:`);
  57. console.log(` Payer: ${payerBalance / LAMPORTS_PER_SOL}`);
  58. console.log(` Recipient: ${recipientBalance / LAMPORTS_PER_SOL}`);
  59. }
  60. });