transferSol.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {
  2. AccountRole,
  3. Address,
  4. appendTransactionMessageInstruction,
  5. generateKeyPairSigner,
  6. lamports,
  7. pipe,
  8. } from '@solana/web3.js';
  9. import test from 'ava';
  10. import {
  11. getTransferSolInstruction,
  12. parseTransferSolInstruction,
  13. } from '../src/index.js';
  14. import {
  15. createDefaultSolanaClient,
  16. createDefaultTransaction,
  17. generateKeyPairSignerWithSol,
  18. getBalance,
  19. signAndSendTransaction,
  20. } from './_setup.js';
  21. test('it transfers SOL from one account to another', async (t) => {
  22. // Given a source account with 3 SOL and a destination account with no SOL.
  23. const client = createDefaultSolanaClient();
  24. const source = await generateKeyPairSignerWithSol(client, 3_000_000_000n);
  25. const destination = (await generateKeyPairSigner()).address;
  26. // When the source account transfers 1 SOL to the destination account.
  27. const transferSol = getTransferSolInstruction({
  28. source,
  29. destination,
  30. amount: 1_000_000_000,
  31. });
  32. await pipe(
  33. await createDefaultTransaction(client, source),
  34. (tx) => appendTransactionMessageInstruction(transferSol, tx),
  35. (tx) => signAndSendTransaction(client, tx)
  36. );
  37. // Then the source account now has roughly 2 SOL (minus the transaction fee).
  38. const sourceBalance = await getBalance(client, source.address);
  39. t.true(sourceBalance < 2_000_000_000n);
  40. t.true(sourceBalance > 1_999_000_000n);
  41. // And the destination account has exactly 1 SOL.
  42. t.is(await getBalance(client, destination), lamports(1_000_000_000n));
  43. });
  44. test('it parses the accounts and the data of an existing transfer SOL instruction', async (t) => {
  45. // Given a transfer SOL instruction with the following accounts and data.
  46. const source = await generateKeyPairSigner();
  47. const destination = (await generateKeyPairSigner()).address;
  48. const transferSol = getTransferSolInstruction({
  49. source,
  50. destination,
  51. amount: 1_000_000_000,
  52. });
  53. // When we parse this instruction.
  54. const parsedTransferSol = parseTransferSolInstruction(transferSol);
  55. // Then we expect the following accounts and data.
  56. t.is(parsedTransferSol.accounts.source.address, source.address);
  57. t.is(parsedTransferSol.accounts.source.role, AccountRole.WRITABLE_SIGNER);
  58. t.is(parsedTransferSol.accounts.source.signer, source);
  59. t.is(parsedTransferSol.accounts.destination.address, destination);
  60. t.is(parsedTransferSol.accounts.destination.role, AccountRole.WRITABLE);
  61. t.is(parsedTransferSol.data.amount, 1_000_000_000n);
  62. t.is(
  63. parsedTransferSol.programAddress,
  64. '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>
  65. );
  66. });