test.ts 2.1 KB

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