withdraw.ts 1.8 KB

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