withdraw.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { decode, mapProof } from "../utils";
  3. import { PROGRAM_ID as BUBBLEGUM_PROGRAM_ID } from "@metaplex-foundation/mpl-bubblegum";
  4. import {
  5. SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  6. SPL_NOOP_PROGRAM_ID,
  7. } from "@solana/spl-account-compression";
  8. import { getAsset, getAssetProof } from "../readAPI";
  9. import { program, programID } from "./constants";
  10. async function main() {
  11. const [vaultPDA, _bump] = anchor.web3.PublicKey.findProgramAddressSync(
  12. [Buffer.from("cNFT-vault", "utf8")],
  13. programID
  14. );
  15. const tree = new anchor.web3.PublicKey(
  16. "trezdkTFPKyj4gE9LAJYPpxn8AYVCvM7Mc4JkTb9X5B"
  17. );
  18. const receiver = new anchor.web3.PublicKey(
  19. "Andys9wuoMdUeRiZLgRS5aJwYNFv4Ut6qQi8PNDTAPEM"
  20. );
  21. const [treeAuthority, _bump2] = anchor.web3.PublicKey.findProgramAddressSync(
  22. [tree.toBuffer()],
  23. BUBBLEGUM_PROGRAM_ID
  24. );
  25. const assetId = "DGWU3mHenDerCvjkeDsKYEbsvXbWvqdo1bVoXy3dkeTd";
  26. const asset = await getAsset(assetId);
  27. // console.log(res)
  28. const proof = await getAssetProof(assetId);
  29. const proofPathAsAccounts = mapProof(proof);
  30. const root = decode(proof.root);
  31. const dataHash = decode(asset.compression.data_hash);
  32. const creatorHash = decode(asset.compression.creator_hash);
  33. const nonce = new anchor.BN(asset.compression.leaf_id);
  34. const index = asset.compression.leaf_id;
  35. const tx = await program.methods
  36. .withdrawCnft(root, dataHash, creatorHash, nonce, index)
  37. .accounts({
  38. leafOwner: vaultPDA,
  39. merkleTree: tree,
  40. newLeafOwner: receiver,
  41. treeAuthority: treeAuthority,
  42. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  43. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  44. logWrapper: SPL_NOOP_PROGRAM_ID,
  45. systemProgram: anchor.web3.SystemProgram.programId,
  46. })
  47. .remainingAccounts(proofPathAsAccounts)
  48. .rpc();
  49. console.log(tx);
  50. }
  51. main();