withdraw.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. leafDelegate: 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();