cnft-burn.ts 3.0 KB

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