test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
  3. } from '@metaplex-foundation/mpl-token-metadata';
  4. import * as anchor from "@project-serum/anchor";
  5. import { ASSOCIATED_PROGRAM_ID } from '@project-serum/anchor/dist/cjs/utils/token';
  6. import { NftMinter } from "../target/types/nft_minter";
  7. describe("NFT Minter", () => {
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const payer = provider.wallet as anchor.Wallet;
  11. const program = anchor.workspace.NftMinter as anchor.Program<NftMinter>;
  12. const mintAuthorityPublicKey = anchor.web3.PublicKey.findProgramAddressSync(
  13. [Buffer.from("mint_authority")],
  14. program.programId,
  15. )[0];
  16. const nftTitle = "Homer NFT";
  17. const nftSymbol = "HOMR";
  18. const nftUri = "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json";
  19. const mintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
  20. it("Init Mint Authority PDA", async () => {
  21. const sx = await program.methods.init()
  22. .accounts({
  23. mintAuthority: mintAuthorityPublicKey,
  24. payer: payer.publicKey,
  25. })
  26. .signers([payer.payer])
  27. .rpc();
  28. console.log("Success!");
  29. console.log(` Mint Authority: ${mintKeypair.publicKey}`);
  30. console.log(` Tx Signature : ${sx}`);
  31. });
  32. it("Create an NFT!", async () => {
  33. const metadataAddress = (anchor.web3.PublicKey.findProgramAddressSync(
  34. [
  35. Buffer.from("metadata"),
  36. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  37. mintKeypair.publicKey.toBuffer(),
  38. ],
  39. TOKEN_METADATA_PROGRAM_ID
  40. ))[0];
  41. const sx = await program.methods.createToken(
  42. nftTitle, nftSymbol, nftUri
  43. )
  44. .accounts({
  45. metadataAccount: metadataAddress,
  46. mintAccount: mintKeypair.publicKey,
  47. mintAuthority: mintAuthorityPublicKey,
  48. payer: payer.publicKey,
  49. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  50. systemProgram: anchor.web3.SystemProgram.programId,
  51. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  52. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  53. })
  54. .signers([mintKeypair, payer.payer])
  55. .rpc({skipPreflight: true});
  56. console.log("Success!");
  57. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  58. console.log(` Tx Signature: ${sx}`);
  59. });
  60. it("Mint the NFT to your wallet!", async () => {
  61. const metadataAddress = (anchor.web3.PublicKey.findProgramAddressSync(
  62. [
  63. Buffer.from("metadata"),
  64. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  65. mintKeypair.publicKey.toBuffer(),
  66. ],
  67. TOKEN_METADATA_PROGRAM_ID
  68. ))[0];
  69. const editionAddress = (anchor.web3.PublicKey.findProgramAddressSync(
  70. [
  71. Buffer.from("metadata"),
  72. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  73. mintKeypair.publicKey.toBuffer(),
  74. Buffer.from("edition"),
  75. ],
  76. TOKEN_METADATA_PROGRAM_ID
  77. ))[0];
  78. const associatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  79. mint: mintKeypair.publicKey,
  80. owner: payer.publicKey,
  81. });
  82. const sx = await program.methods.mintTo()
  83. .accounts({
  84. associatedTokenAccount: associatedTokenAccountAddress,
  85. editionAccount: editionAddress,
  86. metadataAccount: metadataAddress,
  87. mintAccount: mintKeypair.publicKey,
  88. mintAuthority: mintAuthorityPublicKey,
  89. payer: payer.publicKey,
  90. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  91. systemProgram: anchor.web3.SystemProgram.programId,
  92. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  93. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  94. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  95. })
  96. .signers([payer.payer])
  97. .rpc();
  98. console.log("Success!");
  99. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  100. console.log(` Tx Signature: ${sx}`);
  101. });
  102. });