"use strict";
/**
 * GrimSwap ZK SDK - High-Level Private Swap
 *
 * Single-function interface for executing a complete private swap.
 * Handles: tree building, proof generation, formatting, relayer submission.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.executePrivateSwap = executePrivateSwap;
const merkle_1 = require("./merkle");
const proof_1 = require("./proof");
const relayer_1 = require("./relayer");
const deposits_1 = require("./deposits");
const constants_1 = require("./constants");
// Uniswap v4 sqrt price limits
const MIN_SQRT_PRICE_X96 = BigInt("4295128740"); // MIN + 1
const MAX_SQRT_PRICE_X96 = BigInt("1461446703485210103287273052203988822378723970341"); // MAX - 1
/**
 * Execute a complete private swap in one call
 *
 * This is the main entry point for partners. It handles:
 * 1. Fetching all deposits from GrimPool (or uses provided commitments)
 * 2. Building the Merkle tree
 * 3. Generating the ZK proof
 * 4. Formatting and submitting to the relayer
 *
 * @returns RelayerResponse with txHash on success
 *
 * @example
 * ```typescript
 * import {
 *   createDepositNote,
 *   executePrivateSwap,
 *   UNICHAIN_SEPOLIA_ADDRESSES,
 * } from "@grimswap/circuits";
 *
 * // After depositing on-chain...
 * const result = await executePrivateSwap({
 *   note,
 *   recipient: stealthAddress,
 *   poolKey: {
 *     currency0: "0x0000000000000000000000000000000000000000",
 *     currency1: tokenAddress,
 *     fee: 3000,
 *     tickSpacing: 60,
 *     hooks: UNICHAIN_SEPOLIA_ADDRESSES.grimSwapZK,
 *   },
 *   zeroForOne: true,
 *   amountSpecified: -note.amount, // exact input
 *   wasmBuffer,
 *   zkeyBuffer,
 * });
 *
 * console.log(result.txHash);
 * ```
 */
async function executePrivateSwap(params) {
    const { note, recipient, poolKey, zeroForOne, amountSpecified, wasmBuffer, zkeyBuffer, relayerUrl, rpcUrl, commitments: preloadedCommitments, } = params;
    const sqrtPriceLimitX96 = params.sqrtPriceLimitX96 ??
        (zeroForOne ? MIN_SQRT_PRICE_X96 : MAX_SQRT_PRICE_X96);
    const url = relayerUrl || constants_1.RELAYER_DEFAULT_URL;
    // 1. Get relayer info for fee + address
    const relayerInfo = await (0, relayer_1.getRelayerInfo)(url);
    // 2. Fetch deposits and build Merkle tree
    const commitments = preloadedCommitments || await (0, deposits_1.fetchDeposits)(rpcUrl);
    const tree = await (0, merkle_1.buildMerkleTree)(commitments);
    if (note.leafIndex === undefined) {
        throw new Error("note.leafIndex is required. Set it after deposit confirmation " +
            "(matches the leafIndex from the Deposit event).");
    }
    const merkleProof = tree.getProof(note.leafIndex);
    // 3. Generate ZK proof
    const { proof, publicSignals } = await (0, proof_1.generateProofFromBuffers)(note, merkleProof, {
        recipient,
        relayer: relayerInfo.address,
        relayerFee: relayerInfo.fee,
        expectedAmountOut: note.amount,
    }, wasmBuffer, zkeyBuffer);
    // 4. Format and submit to relayer
    const formatted = (0, proof_1.formatProofForContract)(proof, publicSignals);
    return (0, relayer_1.submitToRelayer)(url, {
        a: formatted.pA,
        b: formatted.pB,
        c: formatted.pC,
    }, formatted.pubSignals, {
        poolKey,
        zeroForOne,
        amountSpecified: amountSpecified.toString(),
        sqrtPriceLimitX96: sqrtPriceLimitX96.toString(),
    });
}
//# sourceMappingURL=privateSwap.js.map