nft-minter.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(wallet.publicKey)
  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, // payer
  46. mintKeypair.publicKey, // mint
  47. wallet.publicKey, // mint authority
  48. wallet.publicKey, // freeze authority
  49. metadataAddress, // metadata address
  50. 0, // 0 decimals for NFT
  51. nftTitle, // NFT name
  52. nftSymbol, // NFT symbol
  53. nftUri // NFT URI
  54. )
  55. .accounts({ dataAccount: dataAccount.publicKey })
  56. .remainingAccounts([
  57. {
  58. pubkey: wallet.publicKey,
  59. isWritable: true,
  60. isSigner: true,
  61. },
  62. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: true },
  63. {
  64. pubkey: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), // Metadata program id
  65. isWritable: false,
  66. isSigner: false,
  67. },
  68. { pubkey: metadataAddress, isWritable: true, isSigner: false },
  69. { pubkey: SystemProgram.programId, isWritable: false, isSigner: false },
  70. { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false },
  71. ])
  72. .signers([mintKeypair])
  73. .rpc({ skipPreflight: true })
  74. console.log("Your transaction signature", tx)
  75. })
  76. it("Mint the NFT to your wallet!", async () => {
  77. // Derive wallet's associated token account address for mint
  78. const tokenAccount = getAssociatedTokenAddressSync(
  79. mintKeypair.publicKey,
  80. wallet.publicKey
  81. )
  82. const tx = await program.methods
  83. .mintTo(
  84. wallet.publicKey, // payer
  85. tokenAccount, // associated token account address
  86. mintKeypair.publicKey, // mint
  87. wallet.publicKey // owner of token account
  88. )
  89. .accounts({ dataAccount: dataAccount.publicKey })
  90. .remainingAccounts([
  91. {
  92. pubkey: wallet.publicKey,
  93. isWritable: true,
  94. isSigner: true,
  95. },
  96. { pubkey: tokenAccount, isWritable: true, isSigner: false },
  97. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  98. {
  99. pubkey: SystemProgram.programId,
  100. isWritable: false,
  101. isSigner: false,
  102. },
  103. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  104. {
  105. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  106. isWritable: false,
  107. isSigner: false,
  108. },
  109. ])
  110. .rpc({ skipPreflight: true })
  111. console.log("Your transaction signature", tx)
  112. })
  113. })