mintTo.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {
  2. appendTransactionMessageInstruction,
  3. generateKeyPairSigner,
  4. pipe,
  5. } from '@solana/web3.js';
  6. import test from 'ava';
  7. import {
  8. Mint,
  9. Token,
  10. fetchMint,
  11. fetchToken,
  12. getMintToInstruction,
  13. } from '../src/index.js';
  14. import {
  15. createDefaultSolanaClient,
  16. createDefaultTransaction,
  17. createMint,
  18. createToken,
  19. generateKeyPairSignerWithSol,
  20. signAndSendTransaction,
  21. } from './_setup.js';
  22. test('it mints tokens to a token account', async (t) => {
  23. // Given a mint account and a token account.
  24. const client = createDefaultSolanaClient();
  25. const [payer, mintAuthority, owner] = await Promise.all([
  26. generateKeyPairSignerWithSol(client),
  27. generateKeyPairSigner(),
  28. generateKeyPairSigner(),
  29. ]);
  30. const mint = await createMint(client, payer, mintAuthority.address);
  31. const token = await createToken(client, payer, mint, owner.address);
  32. await new Promise((resolve) => setTimeout(resolve, 2000));
  33. // When the mint authority mints tokens to the token account.
  34. const mintTo = getMintToInstruction({
  35. mint,
  36. token,
  37. mintAuthority,
  38. amount: 100n,
  39. });
  40. await pipe(
  41. await createDefaultTransaction(client, payer),
  42. (tx) => appendTransactionMessageInstruction(mintTo, tx),
  43. (tx) => signAndSendTransaction(client, tx)
  44. );
  45. // Then we expect the mint and token accounts to have the following updated data.
  46. const [{ data: mintData }, { data: tokenData }] = await Promise.all([
  47. fetchMint(client.rpc, mint),
  48. fetchToken(client.rpc, token),
  49. ]);
  50. t.like(mintData, <Mint>{ supply: 100n });
  51. t.like(tokenData, <Token>{ amount: 100n });
  52. });