mintToATA.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Account, generateKeyPairSigner, none } from '@solana/kit';
  2. import test from 'ava';
  3. import {
  4. AccountState,
  5. TOKEN_PROGRAM_ADDRESS,
  6. Token,
  7. getMintToATAInstructionPlan,
  8. getMintToATAInstructionPlanAsync,
  9. fetchToken,
  10. findAssociatedTokenPda,
  11. } from '../src';
  12. import {
  13. createDefaultSolanaClient,
  14. createDefaultTransactionPlanner,
  15. createMint,
  16. generateKeyPairSignerWithSol,
  17. } from './_setup';
  18. test('it creates a new associated token account with an initial balance', async (t) => {
  19. // Given a mint account, its mint authority, a token owner and the ATA.
  20. const client = createDefaultSolanaClient();
  21. const [payer, mintAuthority, owner] = await Promise.all([
  22. generateKeyPairSignerWithSol(client),
  23. generateKeyPairSigner(),
  24. generateKeyPairSigner(),
  25. ]);
  26. const decimals = 2;
  27. const mint = await createMint(client, payer, mintAuthority.address, decimals);
  28. const [ata] = await findAssociatedTokenPda({
  29. mint,
  30. owner: owner.address,
  31. tokenProgram: TOKEN_PROGRAM_ADDRESS,
  32. });
  33. // When we mint to a token account at this address.
  34. const instructionPlan = getMintToATAInstructionPlan({
  35. payer,
  36. ata,
  37. mint,
  38. owner: owner.address,
  39. mintAuthority,
  40. amount: 1_000n,
  41. decimals,
  42. });
  43. const transactionPlanner = createDefaultTransactionPlanner(client, payer);
  44. const transactionPlan = await transactionPlanner(instructionPlan);
  45. await client.sendTransactionPlan(transactionPlan);
  46. // Then we expect the token account to exist and have the following data.
  47. t.like(await fetchToken(client.rpc, ata), <Account<Token>>{
  48. address: ata,
  49. data: {
  50. mint,
  51. owner: owner.address,
  52. amount: 1000n,
  53. delegate: none(),
  54. state: AccountState.Initialized,
  55. isNative: none(),
  56. delegatedAmount: 0n,
  57. closeAuthority: none(),
  58. },
  59. });
  60. });
  61. test('it derives a new associated token account with an initial balance', async (t) => {
  62. // Given a mint account, its mint authority, a token owner and the ATA.
  63. const client = createDefaultSolanaClient();
  64. const [payer, mintAuthority, owner] = await Promise.all([
  65. generateKeyPairSignerWithSol(client),
  66. generateKeyPairSigner(),
  67. generateKeyPairSigner(),
  68. ]);
  69. const decimals = 2;
  70. const mint = await createMint(client, payer, mintAuthority.address, decimals);
  71. // When we mint to a token account for the mint.
  72. const instructionPlan = await getMintToATAInstructionPlanAsync({
  73. payer,
  74. mint,
  75. owner: owner.address,
  76. mintAuthority,
  77. amount: 1_000n,
  78. decimals,
  79. });
  80. const transactionPlanner = createDefaultTransactionPlanner(client, payer);
  81. const transactionPlan = await transactionPlanner(instructionPlan);
  82. await client.sendTransactionPlan(transactionPlan);
  83. // Then we expect the token account to exist and have the following data.
  84. const [ata] = await findAssociatedTokenPda({
  85. mint,
  86. owner: owner.address,
  87. tokenProgram: TOKEN_PROGRAM_ADDRESS,
  88. });
  89. t.like(await fetchToken(client.rpc, ata), <Account<Token>>{
  90. address: ata,
  91. data: {
  92. mint,
  93. owner: owner.address,
  94. amount: 1000n,
  95. delegate: none(),
  96. state: AccountState.Initialized,
  97. isNative: none(),
  98. delegatedAmount: 0n,
  99. closeAuthority: none(),
  100. },
  101. });
  102. });
  103. test('it also mints to an existing associated token account', async (t) => {
  104. // Given a mint account, its mint authority, a token owner and the ATA.
  105. const client = createDefaultSolanaClient();
  106. const [payer, mintAuthority, owner] = await Promise.all([
  107. generateKeyPairSignerWithSol(client),
  108. generateKeyPairSigner(),
  109. generateKeyPairSigner(),
  110. ]);
  111. const decimals = 2;
  112. const mint = await createMint(client, payer, mintAuthority.address, decimals);
  113. const [ata] = await findAssociatedTokenPda({
  114. mint,
  115. owner: owner.address,
  116. tokenProgram: TOKEN_PROGRAM_ADDRESS,
  117. });
  118. // When we create and initialize a token account at this address.
  119. const instructionPlan = getMintToATAInstructionPlan({
  120. payer,
  121. ata,
  122. mint,
  123. owner: owner.address,
  124. mintAuthority,
  125. amount: 1_000n,
  126. decimals,
  127. });
  128. const transactionPlanner = createDefaultTransactionPlanner(client, payer);
  129. const transactionPlan = await transactionPlanner(instructionPlan);
  130. await client.sendTransactionPlan(transactionPlan);
  131. // And then we mint additional tokens to the same account.
  132. const instructionPlan2 = getMintToATAInstructionPlan({
  133. payer,
  134. ata,
  135. mint,
  136. owner: owner.address,
  137. mintAuthority,
  138. amount: 1_000n,
  139. decimals,
  140. });
  141. const transactionPlan2 = await transactionPlanner(instructionPlan2);
  142. await client.sendTransactionPlan(transactionPlan2);
  143. // Then we expect the token account to exist and have the following data.
  144. t.like(await fetchToken(client.rpc, ata), <Account<Token>>{
  145. address: ata,
  146. data: {
  147. mint,
  148. owner: owner.address,
  149. amount: 2000n,
  150. delegate: none(),
  151. state: AccountState.Initialized,
  152. isNative: none(),
  153. delegatedAmount: 0n,
  154. closeAuthority: none(),
  155. },
  156. });
  157. });