createAccount.test.ts 1.5 KB

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