withdraw.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as anchor from "@project-serum/anchor";
  2. import { CnftVault } from "../../target/types/cnft_vault";
  3. import { loadWalletKey, decode, mapProof } from "../utils";
  4. import { IDL } from "../../target/types/cnft_vault"
  5. import { PROGRAM_ID as BUBBLEGUM_PROGRAM_ID } from "@metaplex-foundation/mpl-bubblegum";
  6. import { SPL_ACCOUNT_COMPRESSION_PROGRAM_ID, SPL_NOOP_PROGRAM_ID } from "@solana/spl-account-compression";
  7. import { getAsset, getAssetProof } from "../readAPI";
  8. const connection = new anchor.web3.Connection("https://api.devnet.solana.com");
  9. const keypair = loadWalletKey("../AndYPfCmbSSHpe2yukLXDT9N29twa7kJDk3yrRMQW7SN.json");
  10. const wallet = new anchor.Wallet(keypair);
  11. const provider = new anchor.AnchorProvider(connection, wallet, {});
  12. const programID = new anchor.web3.PublicKey("CNftyK7T8udPwYRzZUMWzbh79rKrz9a5GwV2wv7iEHpk")
  13. const program = new anchor.Program<CnftVault>(IDL, programID, provider);
  14. async function main() {
  15. const [vaultPDA, _bump] = anchor.web3.PublicKey.findProgramAddressSync(
  16. [Buffer.from("cNFT-vault", "utf8")],
  17. programID,
  18. );
  19. const tree = new anchor.web3.PublicKey("trezdkTFPKyj4gE9LAJYPpxn8AYVCvM7Mc4JkTb9X5B")
  20. const receiver = new anchor.web3.PublicKey("Andys9wuoMdUeRiZLgRS5aJwYNFv4Ut6qQi8PNDTAPEM")
  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.withdrawCnft(root, dataHash, creatorHash, nonce, index)
  36. .accounts({
  37. leafOwner: vaultPDA,
  38. merkleTree: tree,
  39. newLeafOwner: receiver,
  40. treeAuthority: treeAuthority,
  41. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  42. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  43. logWrapper: SPL_NOOP_PROGRAM_ID,
  44. systemProgram: anchor.web3.SystemProgram.programId
  45. })
  46. .remainingAccounts(proofPathAsAccounts)
  47. .rpc();
  48. console.log(tx);
  49. };
  50. main();