spl-token-minter.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { Program } from '@coral-xyz/anchor';
  3. import { TOKEN_PROGRAM_ID, createMint, getAccount, getMint, getOrCreateAssociatedTokenAccount } from '@solana/spl-token';
  4. import { PublicKey } from '@solana/web3.js';
  5. import { assert } from 'chai';
  6. import { SplTokenMinter } from '../target/types/spl_token_minter';
  7. describe('spl_token_minter', () => {
  8. // Configure the client to use the local cluster.
  9. const provider = anchor.AnchorProvider.env();
  10. anchor.setProvider(provider);
  11. const program = anchor.workspace.SplTokenMinter as Program<SplTokenMinter>;
  12. const wallet = provider.wallet as anchor.Wallet;
  13. const payer = wallet.payer;
  14. let mint: PublicKey;
  15. it('Creates a new token mint', async () => {
  16. const decimals = 9;
  17. mint = await createMint(provider.connection, payer, payer.publicKey, payer.publicKey, decimals, undefined, undefined, TOKEN_PROGRAM_ID);
  18. await program.methods
  19. .createToken(decimals, wallet.publicKey)
  20. .accounts({
  21. payer: payer.publicKey,
  22. mint: mint,
  23. })
  24. .rpc();
  25. // Fetch the mint account and verify that it was created
  26. const mintAccount = await getMint(provider.connection, mint);
  27. assert.equal(mintAccount.decimals, decimals, 'Decimals should match the input');
  28. console.log('Mint Account', mintAccount);
  29. });
  30. it('Mints tokens to the associated token account', async () => {
  31. const amount = new anchor.BN(1000);
  32. // Create or get the associated token account for the user
  33. const userAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, mint, payer.publicKey);
  34. console.log('Associated Account', userAssociatedTokenAccount);
  35. await program.methods
  36. .mint(amount)
  37. .accounts({
  38. mintAccount: mint,
  39. signer: payer.publicKey,
  40. toAccount: userAssociatedTokenAccount.address,
  41. })
  42. .signers([payer])
  43. .rpc();
  44. // Fetch the token account to verify the balance
  45. const tokenAccountInfo = await getAccount(provider.connection, userAssociatedTokenAccount.address);
  46. console.log('Token Account Info', tokenAccountInfo);
  47. assert.equal(tokenAccountInfo.amount, BigInt(amount.toString()), 'Balance should be minted');
  48. });
  49. });