pda-mint-authority.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as anchor from "@coral-xyz/anchor"
  2. import { Program } from "@coral-xyz/anchor"
  3. import { PdaMintAuthority } from "../target/types/pda_mint_authority"
  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("pda-mint-authority", () => {
  12. // Configure the client to use the local cluster.
  13. const provider = anchor.AnchorProvider.env()
  14. anchor.setProvider(provider)
  15. const mintKeypair = anchor.web3.Keypair.generate()
  16. const wallet = provider.wallet
  17. const connection = provider.connection
  18. const program = anchor.workspace.PdaMintAuthority as Program<PdaMintAuthority>
  19. // Derive the PDA that will be used to initialize the dataAccount.
  20. const [dataAccountPDA, bump] = PublicKey.findProgramAddressSync(
  21. [Buffer.from("mint_authority")],
  22. program.programId
  23. )
  24. const nftTitle = "Homer NFT"
  25. const nftSymbol = "HOMR"
  26. const nftUri =
  27. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json"
  28. it("Is initialized!", async () => {
  29. // Add your test here.
  30. const tx = await program.methods
  31. .new(wallet.publicKey, [bump])
  32. .accounts({ dataAccount: dataAccountPDA })
  33. .rpc()
  34. console.log("Your transaction signature", tx)
  35. })
  36. it("Create an NFT!", async () => {
  37. const metaplex = Metaplex.make(connection)
  38. const metadataAddress = await metaplex
  39. .nfts()
  40. .pdas()
  41. .metadata({ mint: mintKeypair.publicKey })
  42. // Add your test here.
  43. const tx = await program.methods
  44. .createTokenMint(
  45. wallet.publicKey, // payer
  46. mintKeypair.publicKey, // mint
  47. dataAccountPDA, // mint authority
  48. dataAccountPDA, // 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: dataAccountPDA })
  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"),
  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: dataAccountPDA })
  90. .remainingAccounts([
  91. {
  92. pubkey: wallet.publicKey,
  93. isWritable: true,
  94. isSigner: true,
  95. },
  96. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  97. { pubkey: tokenAccount, isWritable: true, isSigner: false },
  98. { pubkey: dataAccountPDA, isWritable: true, isSigner: false },
  99. {
  100. pubkey: SystemProgram.programId,
  101. isWritable: false,
  102. isSigner: false,
  103. },
  104. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  105. {
  106. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  107. isWritable: false,
  108. isSigner: false,
  109. },
  110. ])
  111. .rpc({ skipPreflight: true })
  112. console.log("Your transaction signature", tx)
  113. })
  114. })