mint-nft.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import type NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet';
  4. import { ASSOCIATED_PROGRAM_ID } from '@coral-xyz/anchor/dist/cjs/utils/token';
  5. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
  6. import { Keypair, SystemProgram } from '@solana/web3.js';
  7. import type { MintNft } from '../target/types/mint_nft';
  8. describe('mint-nft', () => {
  9. // Configure the client to use the local cluster.
  10. const provider = anchor.AnchorProvider.env();
  11. anchor.setProvider(provider);
  12. const wallet = provider.wallet as NodeWallet;
  13. const program = anchor.workspace.MintNft as Program<MintNft>;
  14. const TOKEN_METADATA_PROGRAM_ID = new anchor.web3.PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
  15. const mintAuthority = anchor.web3.PublicKey.findProgramAddressSync([Buffer.from('authority')], program.programId)[0];
  16. const collectionKeypair = Keypair.generate();
  17. const collectionMint = collectionKeypair.publicKey;
  18. const mintKeypair = Keypair.generate();
  19. const mint = mintKeypair.publicKey;
  20. const getMetadata = async (mint: anchor.web3.PublicKey): Promise<anchor.web3.PublicKey> => {
  21. return anchor.web3.PublicKey.findProgramAddressSync(
  22. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer()],
  23. TOKEN_METADATA_PROGRAM_ID,
  24. )[0];
  25. };
  26. const getMasterEdition = async (mint: anchor.web3.PublicKey): Promise<anchor.web3.PublicKey> => {
  27. return anchor.web3.PublicKey.findProgramAddressSync(
  28. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer(), Buffer.from('edition')],
  29. TOKEN_METADATA_PROGRAM_ID,
  30. )[0];
  31. };
  32. it('Create Collection NFT', async () => {
  33. console.log('\nCollection Mint Key: ', collectionMint.toBase58());
  34. const metadata = await getMetadata(collectionMint);
  35. console.log('Collection Metadata Account: ', metadata.toBase58());
  36. const masterEdition = await getMasterEdition(collectionMint);
  37. console.log('Master Edition Account: ', masterEdition.toBase58());
  38. const destination = getAssociatedTokenAddressSync(collectionMint, wallet.publicKey);
  39. console.log('Destination ATA = ', destination.toBase58());
  40. const tx = await program.methods
  41. .createCollection()
  42. .accountsPartial({
  43. user: wallet.publicKey,
  44. mint: collectionMint,
  45. mintAuthority,
  46. metadata,
  47. masterEdition,
  48. destination,
  49. systemProgram: SystemProgram.programId,
  50. tokenProgram: TOKEN_PROGRAM_ID,
  51. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  52. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  53. })
  54. .signers([collectionKeypair])
  55. .rpc({
  56. skipPreflight: true,
  57. });
  58. console.log('\nCollection NFT minted: TxID - ', tx);
  59. });
  60. it('Mint NFT', async () => {
  61. console.log('\nMint', mint.toBase58());
  62. const metadata = await getMetadata(mint);
  63. console.log('Metadata', metadata.toBase58());
  64. const masterEdition = await getMasterEdition(mint);
  65. console.log('Master Edition', masterEdition.toBase58());
  66. const destination = getAssociatedTokenAddressSync(mint, wallet.publicKey);
  67. console.log('Destination', destination.toBase58());
  68. const tx = await program.methods
  69. .mintNft()
  70. .accountsPartial({
  71. owner: wallet.publicKey,
  72. destination,
  73. metadata,
  74. masterEdition,
  75. mint,
  76. mintAuthority,
  77. collectionMint,
  78. systemProgram: SystemProgram.programId,
  79. tokenProgram: TOKEN_PROGRAM_ID,
  80. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  81. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  82. })
  83. .signers([mintKeypair])
  84. .rpc({
  85. skipPreflight: true,
  86. });
  87. console.log('\nNFT Minted! Your transaction signature', tx);
  88. });
  89. it('Verify Collection', async () => {
  90. const mintMetadata = await getMetadata(mint);
  91. console.log('\nMint Metadata', mintMetadata.toBase58());
  92. const collectionMetadata = await getMetadata(collectionMint);
  93. console.log('Collection Metadata', collectionMetadata.toBase58());
  94. const collectionMasterEdition = await getMasterEdition(collectionMint);
  95. console.log('Collection Master Edition', collectionMasterEdition.toBase58());
  96. const tx = await program.methods
  97. .verifyCollection()
  98. .accountsPartial({
  99. authority: wallet.publicKey,
  100. metadata: mintMetadata,
  101. mint,
  102. mintAuthority,
  103. collectionMint,
  104. collectionMetadata,
  105. collectionMasterEdition,
  106. systemProgram: SystemProgram.programId,
  107. sysvarInstruction: anchor.web3.SYSVAR_INSTRUCTIONS_PUBKEY,
  108. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  109. })
  110. .rpc({
  111. skipPreflight: true,
  112. });
  113. console.log('\nCollection Verified! Your transaction signature', tx);
  114. });
  115. });