createAccountWithSeed.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. appendTransactionMessageInstruction,
  3. createAddressWithSeed,
  4. fetchEncodedAccount,
  5. generateKeyPairSigner,
  6. pipe,
  7. } from '@solana/kit';
  8. import test from 'ava';
  9. import { getCreateAccountWithSeedInstruction } from '../src';
  10. import {
  11. createDefaultSolanaClient,
  12. createDefaultTransaction,
  13. generateKeyPairSignerWithSol,
  14. signAndSendTransaction,
  15. } from './_setup';
  16. test('it creates a new empty account when base is not payer', async (t) => {
  17. const client = createDefaultSolanaClient();
  18. const space = 42n;
  19. const [payer, program, lamports] = await Promise.all([
  20. generateKeyPairSignerWithSol(client),
  21. generateKeyPairSigner(),
  22. client.rpc.getMinimumBalanceForRentExemption(space).send(),
  23. ]);
  24. const baseAccount = await generateKeyPairSigner();
  25. const programAddress = program.address;
  26. const SEED = '123456789';
  27. const newAccount = await createAddressWithSeed({
  28. baseAddress: baseAccount.address,
  29. programAddress,
  30. seed: SEED,
  31. });
  32. // When we call createAccountWithSeed in a transaction.
  33. const createAccount = getCreateAccountWithSeedInstruction({
  34. payer,
  35. newAccount,
  36. baseAccount,
  37. base: baseAccount.address,
  38. seed: SEED,
  39. space,
  40. amount: lamports,
  41. programAddress,
  42. });
  43. await pipe(
  44. await createDefaultTransaction(client, payer),
  45. (tx) => appendTransactionMessageInstruction(createAccount, tx),
  46. (tx) => signAndSendTransaction(client, tx)
  47. );
  48. // Then we expect the following account data.
  49. const fetchedAccount = await fetchEncodedAccount(client.rpc, newAccount);
  50. t.deepEqual(fetchedAccount, {
  51. executable: false,
  52. lamports,
  53. programAddress,
  54. address: newAccount,
  55. data: new Uint8Array(Array.from({ length: 42 }, () => 0)),
  56. exists: true,
  57. space: 42n,
  58. });
  59. });
  60. test('it creates a new empty account when base is payer', async (t) => {
  61. const client = createDefaultSolanaClient();
  62. const space = 42n;
  63. const [payer, program, lamports] = await Promise.all([
  64. generateKeyPairSignerWithSol(client),
  65. generateKeyPairSigner(),
  66. client.rpc.getMinimumBalanceForRentExemption(space).send(),
  67. ]);
  68. const baseAddress = payer.address;
  69. const programAddress = program.address;
  70. const SEED = '123456789';
  71. const newAccount = await createAddressWithSeed({
  72. baseAddress,
  73. programAddress,
  74. seed: SEED,
  75. });
  76. // When we call createAccountWithSeed in a transaction.
  77. const createAccount = getCreateAccountWithSeedInstruction({
  78. payer,
  79. newAccount,
  80. base: baseAddress,
  81. seed: SEED,
  82. space,
  83. amount: lamports,
  84. programAddress,
  85. });
  86. await pipe(
  87. await createDefaultTransaction(client, payer),
  88. (tx) => appendTransactionMessageInstruction(createAccount, tx),
  89. (tx) => signAndSendTransaction(client, tx)
  90. );
  91. // Then we expect the following account data.
  92. const fetchedAccount = await fetchEncodedAccount(client.rpc, newAccount);
  93. t.deepEqual(fetchedAccount, {
  94. executable: false,
  95. lamports,
  96. programAddress,
  97. address: newAccount,
  98. data: new Uint8Array(Array.from({ length: 42 }, () => 0)),
  99. exists: true,
  100. space: 42n,
  101. });
  102. });