pda-mint-authority.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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
  19. .PdaMintAuthority as Program<PdaMintAuthority>;
  20. // Derive the PDA that will be used to initialize the dataAccount.
  21. const [dataAccountPDA, bump] = PublicKey.findProgramAddressSync(
  22. [Buffer.from("mint_authority")],
  23. program.programId
  24. );
  25. const nftTitle = "Homer NFT";
  26. const nftSymbol = "HOMR";
  27. const nftUri =
  28. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json";
  29. it("Is initialized!", async () => {
  30. // Add your test here.
  31. const tx = await program.methods
  32. .new([bump])
  33. .accounts({ dataAccount: dataAccountPDA })
  34. .rpc();
  35. console.log("Your transaction signature", tx);
  36. });
  37. it("Create an NFT!", async () => {
  38. const metaplex = Metaplex.make(connection);
  39. const metadataAddress = await metaplex
  40. .nfts()
  41. .pdas()
  42. .metadata({ mint: mintKeypair.publicKey });
  43. // Add your test here.
  44. const tx = await program.methods
  45. .createTokenMint(
  46. wallet.publicKey, // payer
  47. mintKeypair.publicKey, // mint
  48. dataAccountPDA, // mint authority
  49. dataAccountPDA, // freeze authority
  50. metadataAddress, // metadata address
  51. 0, // 0 decimals for NFT
  52. nftTitle, // NFT name
  53. nftSymbol, // NFT symbol
  54. nftUri // NFT URI
  55. )
  56. .accounts({ dataAccount: dataAccountPDA })
  57. .remainingAccounts([
  58. {
  59. pubkey: wallet.publicKey,
  60. isWritable: true,
  61. isSigner: true,
  62. },
  63. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: true },
  64. {
  65. pubkey: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  66. isWritable: false,
  67. isSigner: false,
  68. },
  69. { pubkey: metadataAddress, isWritable: true, isSigner: false },
  70. { pubkey: SystemProgram.programId, isWritable: false, isSigner: false },
  71. { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false },
  72. ])
  73. .signers([mintKeypair])
  74. .rpc({ skipPreflight: true });
  75. console.log("Your transaction signature", tx);
  76. });
  77. it("Mint the NFT to your wallet!", async () => {
  78. // Derive wallet's associated token account address for mint
  79. const tokenAccount = getAssociatedTokenAddressSync(
  80. mintKeypair.publicKey,
  81. wallet.publicKey
  82. );
  83. const tx = await program.methods
  84. .mintTo(
  85. wallet.publicKey, // payer
  86. tokenAccount, // associated token account address
  87. mintKeypair.publicKey, // mint
  88. wallet.publicKey // owner of token account
  89. )
  90. .accounts({ dataAccount: dataAccountPDA })
  91. .remainingAccounts([
  92. {
  93. pubkey: wallet.publicKey,
  94. isWritable: true,
  95. isSigner: true,
  96. },
  97. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  98. { pubkey: tokenAccount, isWritable: true, isSigner: false },
  99. { pubkey: dataAccountPDA, isWritable: true, isSigner: false },
  100. {
  101. pubkey: SystemProgram.programId,
  102. isWritable: false,
  103. isSigner: false,
  104. },
  105. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  106. {
  107. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  108. isWritable: false,
  109. isSigner: false,
  110. },
  111. ])
  112. .rpc({ skipPreflight: true });
  113. console.log("Your transaction signature", tx);
  114. });
  115. });