test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.ts';
  5. describe('transfer-sol', async () => {
  6. const PROGRAM_ID = new PublicKey('FNDnd3ZJptKromzx7h71o67AcR1emryyJPb9LjS8WPVw');
  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, true);
  17. const tx = new Transaction();
  18. const res = await client.getLatestBlockhash();
  19. if (res) {
  20. const [blockhash, _] = res;
  21. tx.recentBlockhash = blockhash;
  22. tx.add(ix).sign(payer);
  23. await client.processTransaction(tx);
  24. await getBalances(payer.publicKey, test1Recipient.publicKey, 'Resulting');
  25. }
  26. });
  27. test('Create two accounts for the following test', async () => {
  28. const ix = (pubkey: PublicKey) => {
  29. return SystemProgram.createAccount({
  30. fromPubkey: payer.publicKey,
  31. newAccountPubkey: pubkey,
  32. space: 0,
  33. lamports: 2 * LAMPORTS_PER_SOL,
  34. programId: PROGRAM_ID,
  35. });
  36. };
  37. const tx = new Transaction();
  38. const res = await client.getLatestBlockhash();
  39. if (res) {
  40. const [blockhash, _] = res;
  41. tx.recentBlockhash = blockhash;
  42. tx.add(ix(test2Recipient1.publicKey)).add(ix(test2Recipient2.publicKey)).sign(payer, test2Recipient1, test2Recipient2);
  43. await client.processTransaction(tx);
  44. }
  45. });
  46. test('Transfer between accounts using our program', async () => {
  47. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, 'Beginning');
  48. const ix = createTransferInstruction(
  49. test2Recipient1.publicKey,
  50. test2Recipient2.publicKey,
  51. PROGRAM_ID,
  52. InstructionType.ProgramTransfer,
  53. transferAmount,
  54. );
  55. const tx = new Transaction();
  56. const res = await client.getLatestBlockhash();
  57. if (res) {
  58. const [blockhash, _] = res;
  59. tx.recentBlockhash = blockhash;
  60. tx.add(ix).sign(payer, test2Recipient1);
  61. await client.processTransaction(tx);
  62. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, 'Resulting');
  63. }
  64. });
  65. async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) {
  66. const payerBalance = await client.getBalance(payerPubkey);
  67. const recipientBalance = await client.getBalance(recipientPubkey);
  68. console.log(`${timeframe} balances:`);
  69. console.log(` Payer: ${payerBalance}`);
  70. console.log(` Recipient: ${recipientBalance}`);
  71. }
  72. });