spl-token-minter.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { SplTokenMinter } from "../target/types/spl_token_minter";
  4. import { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
  5. import { Metaplex } from "@metaplex-foundation/js";
  6. import {
  7. ASSOCIATED_TOKEN_PROGRAM_ID,
  8. getOrCreateAssociatedTokenAccount,
  9. TOKEN_PROGRAM_ID,
  10. } from "@solana/spl-token";
  11. describe("spl-token-minter", () => {
  12. // Configure the client to use the local cluster.
  13. const provider = anchor.AnchorProvider.env();
  14. anchor.setProvider(provider);
  15. // Generate a new keypair for the data account for the program
  16. const dataAccount = anchor.web3.Keypair.generate();
  17. // Generate a mint keypair
  18. const mintKeypair = anchor.web3.Keypair.generate();
  19. const wallet = provider.wallet as anchor.Wallet;
  20. const connection = provider.connection;
  21. const program = anchor.workspace.SplTokenMinter as Program<SplTokenMinter>;
  22. // Metadata for the Token
  23. const tokenTitle = "Solana Gold";
  24. const tokenSymbol = "GOLDSOL";
  25. const tokenUri =
  26. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json";
  27. it("Is initialized!", async () => {
  28. // Initialize data account for the program, which is required by Solang
  29. const tx = await program.methods
  30. .new()
  31. .accounts({ dataAccount: dataAccount.publicKey })
  32. .signers([dataAccount])
  33. .rpc();
  34. console.log("Your transaction signature", tx);
  35. });
  36. it("Create an SPL Token!", async () => {
  37. // Get the metadata address for the mint
  38. const metaplex = Metaplex.make(connection);
  39. const metadataAddress = await metaplex
  40. .nfts()
  41. .pdas()
  42. .metadata({ mint: mintKeypair.publicKey });
  43. // Create the token mint
  44. const tx = await program.methods
  45. .createTokenMint(
  46. wallet.publicKey, // freeze authority
  47. 9, // decimals
  48. tokenTitle, // token name
  49. tokenSymbol, // token symbol
  50. tokenUri // token uri
  51. )
  52. .accounts({
  53. payer: wallet.publicKey,
  54. mint: mintKeypair.publicKey,
  55. metadata: metadataAddress,
  56. mintAuthority: wallet.publicKey,
  57. rentAddress: SYSVAR_RENT_PUBKEY,
  58. metadataProgramId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s")
  59. })
  60. .signers([mintKeypair])
  61. .rpc({ skipPreflight: true });
  62. console.log("Your transaction signature", tx);
  63. });
  64. it("Mint some tokens to your wallet!", async () => {
  65. // Wallet's associated token account address for mint
  66. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  67. connection,
  68. wallet.payer, // payer
  69. mintKeypair.publicKey, // mint
  70. wallet.publicKey // owner
  71. );
  72. const tx = await program.methods
  73. .mintTo(
  74. new anchor.BN(150) // amount to mint
  75. )
  76. .accounts({
  77. mintAuthority: wallet.publicKey,
  78. tokenAccount: tokenAccount.address,
  79. mint: mintKeypair.publicKey,
  80. })
  81. .rpc({ skipPreflight: true });
  82. console.log("Your transaction signature", tx);
  83. });
  84. });