pda-mint-authority.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. dataAccountPDA, // freeze authority
  47. 0, // 0 decimals for NFT
  48. nftTitle, // NFT name
  49. nftSymbol, // NFT symbol
  50. nftUri // NFT URI
  51. )
  52. .accounts({
  53. payer: wallet.publicKey,
  54. mint: mintKeypair.publicKey,
  55. metadata: metadataAddress,
  56. mintAuthority: dataAccountPDA,
  57. rentAddress: SYSVAR_RENT_PUBKEY,
  58. metaplexId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  59. })
  60. .signers([mintKeypair])
  61. .rpc({ skipPreflight: true });
  62. console.log("Your transaction signature", tx);
  63. });
  64. it("Mint the NFT to your wallet!", async () => {
  65. // Derive wallet's associated token account address for mint
  66. const tokenAccount = getAssociatedTokenAddressSync(
  67. mintKeypair.publicKey,
  68. wallet.publicKey
  69. );
  70. const tx = await program.methods
  71. .mintTo()
  72. .accounts({
  73. pdaAccount: dataAccountPDA,
  74. payer: wallet.publicKey,
  75. tokenAccount: tokenAccount,
  76. owner: wallet.publicKey,
  77. mint: mintKeypair.publicKey
  78. })
  79. .rpc({ skipPreflight: true });
  80. console.log("Your transaction signature", tx);
  81. });
  82. });