bankrun.test.ts 5.3 KB

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