test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { getAssociatedTokenAddressSync } from '@solana/spl-token';
  3. import { Keypair } from '@solana/web3.js';
  4. import type { SplTokenMinter } from '../target/types/spl_token_minter';
  5. describe('SPL Token Minter', () => {
  6. const provider = anchor.AnchorProvider.env();
  7. anchor.setProvider(provider);
  8. const payer = provider.wallet as anchor.Wallet;
  9. const program = anchor.workspace.SplTokenMinter as anchor.Program<SplTokenMinter>;
  10. const metadata = {
  11. name: 'Solana Gold',
  12. symbol: 'GOLDSOL',
  13. uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json',
  14. };
  15. // Generate new keypair to use as address for mint account.
  16. const mintKeypair = new Keypair();
  17. it('Create an SPL Token!', async () => {
  18. const transactionSignature = await program.methods
  19. .createToken(metadata.name, metadata.symbol, metadata.uri)
  20. .accounts({
  21. payer: payer.publicKey,
  22. mintAccount: mintKeypair.publicKey,
  23. })
  24. .signers([mintKeypair])
  25. .rpc();
  26. console.log('Success!');
  27. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  28. console.log(` Transaction Signature: ${transactionSignature}`);
  29. });
  30. it('Mint some tokens to your wallet!', async () => {
  31. // Derive the associated token address account for the mint and payer.
  32. const associatedTokenAccountAddress = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  33. // Amount of tokens to mint.
  34. const amount = new anchor.BN(100);
  35. // Mint the tokens to the associated token account.
  36. const transactionSignature = await program.methods
  37. .mintToken(amount)
  38. .accounts({
  39. mintAuthority: payer.publicKey,
  40. recipient: payer.publicKey,
  41. mintAccount: mintKeypair.publicKey,
  42. associatedTokenAccount: associatedTokenAccountAddress,
  43. })
  44. .rpc();
  45. console.log('Success!');
  46. console.log(` Associated Token Account Address: ${associatedTokenAccountAddress}`);
  47. console.log(` Transaction Signature: ${transactionSignature}`);
  48. });
  49. });