withdraw.ts 1.8 KB

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