withdrawTwo.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import { AccountMeta } from "@solana/web3.js";
  9. const connection = new anchor.web3.Connection("https://api.devnet.solana.com");
  10. const keypair = loadWalletKey("../AndYPfCmbSSHpe2yukLXDT9N29twa7kJDk3yrRMQW7SN.json");
  11. const wallet = new anchor.Wallet(keypair);
  12. const provider = new anchor.AnchorProvider(connection, wallet, {});
  13. const programID = new anchor.web3.PublicKey("CNftyK7T8udPwYRzZUMWzbh79rKrz9a5GwV2wv7iEHpk")
  14. const program = new anchor.Program<CnftVault>(IDL, programID, provider);
  15. async function main() {
  16. // TODO change all of these to your values
  17. const assetId1 = "DGWU3mHenDerCvjkeDsKYEbsvXbWvqdo1bVoXy3dkeTd";
  18. const assetId2 = "14JojSTdBZvP7f77rCxB3oQK78skTVD6DiXrXUL4objg";//"D2CoMLCRfsfv1EAiNbaBHfoU1Sqf1964KXLGxEfyUwWo";
  19. const tree1 = new anchor.web3.PublicKey("trezdkTFPKyj4gE9LAJYPpxn8AYVCvM7Mc4JkTb9X5B")
  20. const tree2 = new anchor.web3.PublicKey("Feywkti8LLBLfxhSGmYgzUBqpq89qehfB1SMTYV1zCu")
  21. const receiver1 = new anchor.web3.PublicKey("Andys9wuoMdUeRiZLgRS5aJwYNFv4Ut6qQi8PNDTAPEM")
  22. const receiver2 = new anchor.web3.PublicKey("Andys9wuoMdUeRiZLgRS5aJwYNFv4Ut6qQi8PNDTAPEM")
  23. // ---
  24. const [vaultPDA, _bump] = anchor.web3.PublicKey.findProgramAddressSync(
  25. [Buffer.from("cNFT-vault", "utf8")],
  26. programID,
  27. );
  28. const [treeAuthority1, _bump2] = anchor.web3.PublicKey.findProgramAddressSync(
  29. [tree1.toBuffer()],
  30. BUBBLEGUM_PROGRAM_ID,
  31. );
  32. const [treeAuthority2, _bump3] = anchor.web3.PublicKey.findProgramAddressSync(
  33. [tree2.toBuffer()],
  34. BUBBLEGUM_PROGRAM_ID,
  35. );
  36. const asset1 = await getAsset(assetId1);
  37. const asset2 = await getAsset(assetId2);
  38. const proof1 = await getAssetProof(assetId1);
  39. const proofPathAsAccounts1 = mapProof(proof1);
  40. const proof2 = await getAssetProof(assetId2);
  41. const proofPathAsAccounts2 = mapProof(proof2);
  42. const ixData1 = getInstructionData(asset1, proof1);
  43. const ixData2 = getInstructionData(asset2, proof2);
  44. const remainingAccounts: AccountMeta[] = [...proofPathAsAccounts1, ...proofPathAsAccounts2];
  45. const tx = await program.methods.withdrawTwoCnfts(...ixData1, ...ixData2)
  46. .accounts({
  47. leafOwner: vaultPDA,
  48. merkleTree1: tree1,
  49. newLeafOwner1: receiver1,
  50. treeAuthority1: treeAuthority1,
  51. merkleTree2: tree2,
  52. newLeafOwner2: receiver2,
  53. treeAuthority2: treeAuthority2,
  54. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  55. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  56. logWrapper: SPL_NOOP_PROGRAM_ID,
  57. systemProgram: anchor.web3.SystemProgram.programId
  58. })
  59. .remainingAccounts(remainingAccounts)
  60. .rpc();
  61. console.log(tx);
  62. };
  63. function getInstructionData(asset: any, proof: any):
  64. [number[], number[], number[], anchor.BN, number, number] {
  65. const root = decode(proof.root);
  66. const dataHash = decode(asset.compression.data_hash);
  67. const creatorHash = decode(asset.compression.creator_hash);
  68. const nonce = new anchor.BN(asset.compression.leaf_id);
  69. const index = asset.compression.leaf_id;
  70. const proofLength = proof.proof.length;
  71. return [root, dataHash, creatorHash, nonce, index, proofLength];
  72. }
  73. main();