test.ts 3.0 KB

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