createAccount.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. appendTransactionMessageInstruction,
  3. fetchEncodedAccount,
  4. generateKeyPairSigner,
  5. pipe,
  6. } from '@solana/web3.js';
  7. import test from 'ava';
  8. import {
  9. SYSTEM_PROGRAM_ADDRESS,
  10. getCreateAccountInstruction,
  11. } from '../src/index.js';
  12. import {
  13. createDefaultSolanaClient,
  14. createDefaultTransaction,
  15. generateKeyPairSignerWithSol,
  16. signAndSendTransaction,
  17. } from './_setup.js';
  18. test('it creates a new empty account', async (t) => {
  19. // Given we have a newly generated account keypair to create with 42 bytes of space.
  20. const client = createDefaultSolanaClient();
  21. const space = 42n;
  22. const [payer, newAccount, lamports] = await Promise.all([
  23. generateKeyPairSignerWithSol(client),
  24. generateKeyPairSigner(),
  25. client.rpc.getMinimumBalanceForRentExemption(space).send(),
  26. ]);
  27. // When we call createAccount in a transaction.
  28. const createAccount = getCreateAccountInstruction({
  29. payer,
  30. newAccount,
  31. space,
  32. lamports,
  33. programAddress: SYSTEM_PROGRAM_ADDRESS,
  34. });
  35. await pipe(
  36. await createDefaultTransaction(client, payer),
  37. (tx) => appendTransactionMessageInstruction(createAccount, tx),
  38. (tx) => signAndSendTransaction(client, tx)
  39. );
  40. // Then we expect the following account data.
  41. const fetchedAccount = await fetchEncodedAccount(
  42. client.rpc,
  43. newAccount.address
  44. );
  45. t.deepEqual(fetchedAccount, {
  46. executable: false,
  47. lamports,
  48. programAddress: SYSTEM_PROGRAM_ADDRESS,
  49. address: newAccount.address,
  50. data: new Uint8Array(Array.from({ length: 42 }, () => 0)),
  51. exists: true,
  52. });
  53. });