initializeMint.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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';
  18. import {
  19. createDefaultSolanaClient,
  20. createDefaultTransaction,
  21. generateKeyPairSignerWithSol,
  22. signAndSendTransaction,
  23. } from './_setup';
  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. });
  64. test('it creates a new mint account with a freeze authority', async (t) => {
  65. // Given an authority and a mint account.
  66. const client = createDefaultSolanaClient();
  67. const [payer, mintAuthority, freezeAuthority, mint] = await Promise.all([
  68. generateKeyPairSignerWithSol(client),
  69. generateKeyPairSigner(),
  70. generateKeyPairSigner(),
  71. generateKeyPairSigner(),
  72. ]);
  73. // When we create and initialize a mint account at this address.
  74. const space = BigInt(getMintSize());
  75. const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
  76. const instructions = [
  77. getCreateAccountInstruction({
  78. payer,
  79. newAccount: mint,
  80. lamports: rent,
  81. space,
  82. programAddress: TOKEN_PROGRAM_ADDRESS,
  83. }),
  84. getInitializeMintInstruction({
  85. mint: mint.address,
  86. decimals: 0,
  87. mintAuthority: mintAuthority.address,
  88. freezeAuthority: freezeAuthority.address,
  89. }),
  90. ];
  91. await pipe(
  92. await createDefaultTransaction(client, payer),
  93. (tx) => appendTransactionMessageInstructions(instructions, tx),
  94. (tx) => signAndSendTransaction(client, tx)
  95. );
  96. // Then we expect the mint account to exist and have the following data.
  97. const mintAccount = await fetchMint(client.rpc, mint.address);
  98. t.like(mintAccount, <Account<Mint>>{
  99. address: mint.address,
  100. data: {
  101. mintAuthority: some(mintAuthority.address),
  102. freezeAuthority: some(freezeAuthority.address),
  103. },
  104. });
  105. });