1
1

initializeMint.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { getCreateAccountInstruction } from '@solana-program/system';
  2. import {
  3. Account,
  4. appendTransactionMessageInstructions,
  5. generateKeyPairSigner,
  6. none,
  7. pipe,
  8. some,
  9. } from '@solana/web3.js';
  10. import test from 'ava';
  11. import {
  12. Mint,
  13. TOKEN_PROGRAM_ADDRESS,
  14. fetchMint,
  15. getInitializeMintInstruction,
  16. getMintSize,
  17. } from '../src/index.js';
  18. import {
  19. createDefaultSolanaClient,
  20. createDefaultTransaction,
  21. generateKeyPairSignerWithSol,
  22. signAndSendTransaction,
  23. } from './_setup.js';
  24. test('it creates and initializes a new mint account', async (t) => {
  25. // Given an authority and a mint account.
  26. const client = createDefaultSolanaClient();
  27. const authority = await generateKeyPairSignerWithSol(client);
  28. const mint = await generateKeyPairSigner();
  29. // When we create and initialize a mint account at this address.
  30. const space = BigInt(getMintSize());
  31. const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
  32. const instructions = [
  33. getCreateAccountInstruction({
  34. payer: authority,
  35. newAccount: mint,
  36. lamports: rent,
  37. space,
  38. programAddress: TOKEN_PROGRAM_ADDRESS,
  39. }),
  40. getInitializeMintInstruction({
  41. mint: mint.address,
  42. decimals: 2,
  43. mintAuthority: authority.address,
  44. }),
  45. ];
  46. await pipe(
  47. await createDefaultTransaction(client, authority),
  48. (tx) => appendTransactionMessageInstructions(instructions, tx),
  49. (tx) => signAndSendTransaction(client, tx)
  50. );
  51. // Then we expect the mint account to exist and have the following data.
  52. const mintAccount = await fetchMint(client.rpc, mint.address);
  53. t.like(mintAccount, <Account<Mint>>{
  54. address: mint.address,
  55. data: {
  56. mintAuthority: some(authority.address),
  57. supply: 0n,
  58. decimals: 2,
  59. isInitialized: true,
  60. freezeAuthority: none(),
  61. },
  62. });
  63. });