test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata";
  2. import * as anchor from "@coral-xyz/anchor";
  3. import { NftMinter } from "../target/types/nft_minter";
  4. import {
  5. Keypair,
  6. PublicKey,
  7. SYSVAR_RENT_PUBKEY,
  8. SystemProgram,
  9. } from "@solana/web3.js";
  10. import {
  11. getAssociatedTokenAddressSync,
  12. ASSOCIATED_TOKEN_PROGRAM_ID,
  13. TOKEN_PROGRAM_ID,
  14. } from "@solana/spl-token";
  15. describe("NFT Minter", () => {
  16. const provider = anchor.AnchorProvider.env();
  17. anchor.setProvider(provider);
  18. const payer = provider.wallet as anchor.Wallet;
  19. const program = anchor.workspace.NftMinter as anchor.Program<NftMinter>;
  20. // The metadata for our NFT
  21. const metadata = {
  22. name: "Homer NFT",
  23. symbol: "HOMR",
  24. uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
  25. };
  26. it("Create an NFT!", async () => {
  27. // Generate a keypair to use as the address of our mint account
  28. const mintKeypair = new Keypair();
  29. // Derive the PDA of the metadata account for the mint.
  30. const [metadataAddress] = PublicKey.findProgramAddressSync(
  31. [
  32. Buffer.from("metadata"),
  33. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  34. mintKeypair.publicKey.toBuffer(),
  35. ],
  36. TOKEN_METADATA_PROGRAM_ID
  37. );
  38. // Derive the PDA of the master edition account for the mint.
  39. const [editionAddress] = PublicKey.findProgramAddressSync(
  40. [
  41. Buffer.from("metadata"),
  42. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  43. mintKeypair.publicKey.toBuffer(),
  44. Buffer.from("edition"),
  45. ],
  46. TOKEN_METADATA_PROGRAM_ID
  47. );
  48. // Derive the associated token address account for the mint and payer.
  49. const associatedTokenAccountAddress = getAssociatedTokenAddressSync(
  50. mintKeypair.publicKey,
  51. payer.publicKey
  52. );
  53. const transactionSignature = await program.methods
  54. .mintNft(metadata.name, metadata.symbol, metadata.uri)
  55. .accounts({
  56. payer: payer.publicKey,
  57. metadataAccount: metadataAddress,
  58. editionAccount: editionAddress,
  59. mintAccount: mintKeypair.publicKey,
  60. associatedTokenAccount: associatedTokenAccountAddress,
  61. tokenProgram: TOKEN_PROGRAM_ID,
  62. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  63. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  64. systemProgram: SystemProgram.programId,
  65. rent: SYSVAR_RENT_PUBKEY,
  66. })
  67. .signers([mintKeypair])
  68. .rpc({ skipPreflight: true });
  69. console.log("Success!");
  70. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  71. console.log(` Transaction Signature: ${transactionSignature}`);
  72. });
  73. });