litesvm.test.ts 2.7 KB

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