mintTo.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // When the mint authority mints tokens to the token account.
  33. const mintTo = getMintToInstruction({
  34. mint,
  35. token,
  36. mintAuthority,
  37. amount: 100n,
  38. });
  39. await pipe(
  40. await createDefaultTransaction(client, payer),
  41. (tx) => appendTransactionMessageInstruction(mintTo, tx),
  42. (tx) => signAndSendTransaction(client, tx)
  43. );
  44. // Then we expect the mint and token accounts to have the following updated data.
  45. const [{ data: mintData }, { data: tokenData }] = await Promise.all([
  46. fetchMint(client.rpc, mint),
  47. fetchToken(client.rpc, token),
  48. ]);
  49. t.like(mintData, <Mint>{ supply: 100n });
  50. t.like(tokenData, <Token>{ amount: 100n });
  51. });