cnft-burn.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { PROGRAM_ID as BUBBLEGUM_PROGRAM_ID } from '@metaplex-foundation/mpl-bubblegum';
  4. import { SPL_ACCOUNT_COMPRESSION_PROGRAM_ID, SPL_NOOP_PROGRAM_ID } from '@solana/spl-account-compression';
  5. import type { CnftBurn } from '../target/types/cnft_burn';
  6. import { createAndMint } from './createAndMint';
  7. import { getcNFTsFromCollection } from './fetchNFTsByCollection';
  8. import { getAsset, getAssetProof } from './readApi';
  9. import { decode, mapProof } from './utils';
  10. // Replace this with your custom RPC endpoint that supports cNFT indexing
  11. const RPC_PATH = 'https://api.devnet.solana.com';
  12. describe('cnft-burn', () => {
  13. // Configure the client to use the local cluster.
  14. anchor.setProvider(anchor.AnchorProvider.env());
  15. const program = anchor.workspace.CnftBurn as Program<CnftBurn>;
  16. const provider = anchor.AnchorProvider.env();
  17. const payerWallet = provider.wallet as anchor.Wallet;
  18. let treeAddress: anchor.web3.PublicKey | undefined = undefined;
  19. const MPL_BUBBLEGUM_PROGRAM_ID_KEY = new anchor.web3.PublicKey(BUBBLEGUM_PROGRAM_ID);
  20. // this is the assetId of the cNft you want to burn
  21. let assetId = '';
  22. it('Should create the tree and mint a cnft', async () => {
  23. const { tree, collection } = await createAndMint();
  24. if (!tree.treeAddress) {
  25. throw new Error('Tree address not found');
  26. }
  27. treeAddress = tree.treeAddress;
  28. const fetchcNFTs = await getcNFTsFromCollection(collection.mint, payerWallet.publicKey.toString());
  29. console.log('fetchcNFTs', fetchcNFTs);
  30. assetId = fetchcNFTs[0];
  31. });
  32. it('Burn cNft!', async () => {
  33. const asset = await getAsset(assetId);
  34. const proof = await getAssetProof(assetId);
  35. const proofPathAsAccounts = mapProof(proof);
  36. const root = decode(proof.root);
  37. const dataHash = decode(asset.compression.data_hash);
  38. const creatorHash = decode(asset.compression.creator_hash);
  39. const nonce = new anchor.BN(asset.compression.leaf_id);
  40. const index = asset.compression.leaf_id;
  41. const [treeAuthority, _bump2] = anchor.web3.PublicKey.findProgramAddressSync([treeAddress.toBuffer()], MPL_BUBBLEGUM_PROGRAM_ID_KEY);
  42. const tx = await program.methods
  43. .burnCnft(root, dataHash, creatorHash, nonce, index)
  44. .accounts({
  45. merkleTree: treeAddress,
  46. leafOwner: payerWallet.publicKey,
  47. treeAuthority: treeAuthority,
  48. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  49. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  50. logWrapper: SPL_NOOP_PROGRAM_ID,
  51. systemProgram: anchor.web3.SystemProgram.programId,
  52. })
  53. .remainingAccounts(proofPathAsAccounts)
  54. .rpc({
  55. skipPreflight: true,
  56. });
  57. console.log('Your transaction signature', tx);
  58. // here is a sample transaction signature on devnet
  59. // https://explorer.solana.com/tx/2MpeHi64pbWNY7BKBuhAp4yND5HdfQqNqkd8pu6F6meoSNUYRvxQgV5TC4w8BM8hUihB8G8TwBAaPRqS7pnN8Nu1?cluster=devnet
  60. });
  61. });