nft-minter.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { NftMinter } from "../target/types/nft_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. getAssociatedTokenAddressSync,
  9. TOKEN_PROGRAM_ID,
  10. } from "@solana/spl-token";
  11. describe("nft-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;
  20. const connection = provider.connection;
  21. const program = anchor.workspace.NftMinter as Program<NftMinter>;
  22. // Metadata for the NFT
  23. const nftTitle = "Homer NFT";
  24. const nftSymbol = "HOMR";
  25. const nftUri =
  26. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.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 NFT!", 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. const tx = await program.methods
  44. .createTokenMint(
  45. wallet.publicKey, // freeze authority
  46. 0, // 0 decimals for NFT
  47. nftTitle, // NFT name
  48. nftSymbol, // NFT symbol
  49. nftUri // NFT URI
  50. )
  51. .accounts({
  52. payer: wallet.publicKey,
  53. mint: mintKeypair.publicKey,
  54. metadata: metadataAddress,
  55. mintAuthority: wallet.publicKey,
  56. metadataProgramId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  57. rentAddress: SYSVAR_RENT_PUBKEY,
  58. })
  59. .signers([mintKeypair])
  60. .rpc({ skipPreflight: true });
  61. console.log("Your transaction signature", tx);
  62. });
  63. it("Mint the NFT to your wallet!", async () => {
  64. // Derive wallet's associated token account address for mint
  65. const tokenAccount = getAssociatedTokenAddressSync(
  66. mintKeypair.publicKey,
  67. wallet.publicKey
  68. );
  69. const tx = await program.methods
  70. .mintTo()
  71. .accounts({
  72. payer: wallet.publicKey, // payer
  73. tokenAccount: tokenAccount, // associated token account address
  74. mint: mintKeypair.publicKey, // mint
  75. owner: wallet.publicKey, // owner of token account
  76. })
  77. .rpc({ skipPreflight: true });
  78. console.log("Your transaction signature", tx);
  79. });
  80. });