test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { describe, test } from 'node:test';
  2. import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
  3. import { start } from 'solana-bankrun';
  4. import { InstructionType, createTransferInstruction } from './instruction';
  5. describe('transfer-sol', async () => {
  6. const PROGRAM_ID = PublicKey.unique();
  7. const context = await start([{ name: 'transfer_sol_program', programId: PROGRAM_ID }], []);
  8. const client = context.banksClient;
  9. const payer = context.payer;
  10. const transferAmount = 1 * LAMPORTS_PER_SOL;
  11. const test1Recipient = Keypair.generate();
  12. const test2Recipient1 = Keypair.generate();
  13. const test2Recipient2 = Keypair.generate();
  14. test('Transfer between accounts using the system program', async () => {
  15. await getBalances(payer.publicKey, test1Recipient.publicKey, 'Beginning');
  16. const ix = createTransferInstruction(payer.publicKey, test1Recipient.publicKey, PROGRAM_ID, InstructionType.CpiTransfer, transferAmount);
  17. const tx = new Transaction();
  18. const [blockhash, _] = await client.getLatestBlockhash();
  19. tx.recentBlockhash = blockhash;
  20. tx.add(ix).sign(payer);
  21. await client.processTransaction(tx);
  22. await getBalances(payer.publicKey, test1Recipient.publicKey, 'Resulting');
  23. });
  24. test('Create two accounts for the following test', async () => {
  25. const ix = (pubkey: PublicKey) => {
  26. return SystemProgram.createAccount({
  27. fromPubkey: payer.publicKey,
  28. newAccountPubkey: pubkey,
  29. space: 0,
  30. lamports: 2 * LAMPORTS_PER_SOL,
  31. programId: PROGRAM_ID,
  32. });
  33. };
  34. const tx = new Transaction();
  35. const [blockhash, _] = await client.getLatestBlockhash();
  36. tx.recentBlockhash = blockhash;
  37. tx.add(ix(test2Recipient1.publicKey)).add(ix(test2Recipient2.publicKey)).sign(payer, test2Recipient1, test2Recipient2);
  38. await client.processTransaction(tx);
  39. });
  40. test('Transfer between accounts using our program', async () => {
  41. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, 'Beginning');
  42. const ix = createTransferInstruction(
  43. test2Recipient1.publicKey,
  44. test2Recipient2.publicKey,
  45. PROGRAM_ID,
  46. InstructionType.ProgramTransfer,
  47. transferAmount,
  48. );
  49. const tx = new Transaction();
  50. const [blockhash, _] = await client.getLatestBlockhash();
  51. tx.recentBlockhash = blockhash;
  52. tx.add(ix).sign(payer, test2Recipient1);
  53. await client.processTransaction(tx);
  54. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, 'Resulting');
  55. });
  56. async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) {
  57. const payerBalance = await client.getBalance(payerPubkey);
  58. const recipientBalance = await client.getBalance(recipientPubkey);
  59. console.log(`${timeframe} balances:`);
  60. console.log(` Payer: ${payerBalance}`);
  61. console.log(` Recipient: ${recipientBalance}`);
  62. }
  63. });