import { type BuildQueryFn } from "../../utils/buildQuery.js"; import type { IDeploymentService } from "../deploymentService/index.js"; import type { GetDepositQuoteArgs, GetRepayQuoteArgs, GetWalletSwapQuoteArgs, SwapProvidersApiResponse, SwapQuote, SwapQuoteRequest, SwapsApiResponse } from "./swapServiceTypes.js"; export interface SwapServiceConfig { swapApiUrl: string; defaultDeadline?: number; } export interface ISwapService { /** Fetches raw swap quotes from the API. Prefer fetchRepayQuotes or fetchDepositQuote for repay/collateral-swap flows. */ fetchSwapQuotes(args: SwapQuoteRequest): Promise; /** Fetches swap quotes for repaying debt by swapping collateral (withdraw → swap → repay). */ fetchRepayQuotes(args: GetRepayQuoteArgs): Promise; /** Fetches swap quotes for swapping collateral between vaults (withdraw → swap → deposit). */ fetchDepositQuote(args: GetDepositQuoteArgs): Promise; /** Fetches swap quotes for swapping wallet input to wallet output (transferFromSender → swap → transferOutputToReceiver). */ fetchWalletSwapQuote(args: GetWalletSwapQuoteArgs): Promise; /** Fetches available swap providers for a given chain. */ fetchProviders(chainId: number): Promise; } export declare class SwapService implements ISwapService { private readonly config; private readonly deploymentService; constructor(config: SwapServiceConfig, deploymentService: IDeploymentService, buildQuery?: BuildQueryFn); querySwapQuotes: (url: string) => Promise; setQuerySwapQuotes(fn: typeof this.querySwapQuotes): void; querySwapProviders: (url: string) => Promise; setQuerySwapProviders(fn: typeof this.querySwapProviders): void; /** * Fetches swap quotes from the swap API for a given token pair and amount. * Validates verifier data for each quote. Use fetchRepayQuotes or fetchDepositQuote for repay/collateral-swap flows. * * @param request - Swap quote request * @param request.chainId - Chain ID * @param request.tokenIn - Token to sell (input) * @param request.tokenOut - Token to buy (output); must differ from tokenIn * @param request.accountIn - Sub-account providing the input (e.g. withdrawing from vaultIn) * @param request.accountOut - Sub-account receiving the output (e.g. repay target or collateral receiver) * @param request.amount - Exact-in: amount to sell; exact-out: amount to buy; exact-out repay: estimated amount to buy * @param request.vaultIn - Vault to withdraw from (for returning unused input) * @param request.receiver - Vault that receives the swap output (e.g. liability vault for repay, destination vault for deposit) * @param request.origin - EOA sending the transaction (required, cannot be zero address) * @param request.slippage - Slippage in percent (e.g. 1 = 1%); must be between 0 and 50 * @param request.swapperMode - EXACT_IN (0), EXACT_OUT (1), or TARGET_DEBT (2) for repay * @param request.isRepay - If true, quote is for repaying debt (verify type debtMax) * @param request.targetDebt - Target debt after repay (used when swapperMode is TARGET_DEBT) * @param request.currentDebt - Current debt of the account (required when isRepay is true) * @param request.deadline - Quote deadline timestamp in seconds (defaults to config defaultDeadline from now) * @param request.dustAccount - Account receiving dust from over-swap repays (defaults to origin) * @param request.provider - Optional preselected provider (see fetchProviders) * @returns Promise of array of swap quotes (amounts, swap calldata, verifier calldata). Throws if tokenIn === tokenOut, origin is zero, or API/verifier validation fails. */ fetchSwapQuotes(request: SwapQuoteRequest): Promise; /** * Fetches available swap providers for a given chain. * The result is static per chain and can be cached for a long time. * * @param chainId - Chain ID * @returns Promise of array of provider name strings */ fetchProviders(chainId: number): Promise; /** * Builds request parameters for the swap API */ private buildRequestParams; /** * Validates that the verifier data matches what we expect * This is a security measure to ensure the swap payload hasn't been tampered with */ private validateVerifierData; private validateQuoteMatchesRequest; private validateCowSwapQuoteMatchesRequest; private validateSupportedCowSwapRequest; private getExpectedCowSwapSellAmount; private getExpectedCowSwapBuyAmount; private parseCowSwapProviderAmount; private normalizeCowSwapQuoteId; private shouldTreatAsCowSwapQuote; private isCowSwapRouteLabel; private getExpectedVerifierAmount; private assertAddressField; private assertBigIntField; private assertBigIntValue; /** * Fetches swap quotes for repaying debt by swapping collateral (e.g. withdraw collateral → swap → repay). * Delegates to fetchSwapQuotes with isRepay true. fromAsset and liabilityAsset must differ. * * @param args - Repay quote arguments * @param args.chainId - Chain ID * @param args.fromVault - Vault to withdraw collateral from (source of swap input) * @param args.fromAsset - Underlying asset of fromVault (tokenIn for the swap) * @param args.fromAccount - Sub-account that holds the collateral in fromVault * @param args.liabilityVault - Vault to repay debt to (receiver of swap output) * @param args.liabilityAsset - Underlying asset of liabilityVault (tokenOut for the swap) * @param args.currentDebt - Current debt of the account being repaid (must be > 0) * @param args.toAccount - Sub-account whose debt is repaid (accountOut) * @param args.origin - EOA sending the transaction * @param args.swapperMode - EXACT_IN (sell fixed collateral amount) or TARGET_DEBT (repay toward target debt) * @param args.slippage - Slippage in percent (0–50) * @param args.collateralAmount - In EXACT_IN mode: amount of collateral to sell; required in EXACT_IN * @param args.liabilityAmount - In TARGET_DEBT mode: amount of debt to repay; set to currentDebt for full repay * @param args.deadline - Quote deadline timestamp in seconds (optional) * @returns Promise of array of swap quotes for repay (verify type debtMax). Throws if currentDebt <= 0, fromAsset === liabilityAsset, or no quotes. */ fetchRepayQuotes(args: GetRepayQuoteArgs): Promise; /** * Fetches swap quotes for swapping one asset to another and depositing into a destination vault. * Delegates to fetchSwapQuotes with isRepay false and EXACT_IN mode. * * The swapped output tokens are always deposited into `toVault` for `toAccount` (verify type skimMin). * Use `fetchSwapQuotes` directly with `transferOutputToReceiver` if you need to transfer output * tokens to an address instead of depositing into a vault. * * `unusedInputReceiver` can redirect leftover input tokens to a wallet address instead of * depositing them back into `fromVault` for `fromAccount`. When set, `fromVault` and * `fromAccount` should be zero address. * * `skipSweepDepositOut` leaves the output tokens in the Swapper contract instead of depositing. * Useful when the Swapper is the receiver and further processing is needed. * * @param args - Deposit/collateral-swap quote arguments * @param args.chainId - Chain ID * @param args.fromVault - Vault to withdraw collateral from (source). Use zero address when `unusedInputReceiver` is set. * @param args.toVault - Vault to deposit swapped tokens into (destination, receiver) * @param args.fromAccount - Sub-account that holds the collateral in fromVault. Use zero address when `unusedInputReceiver` is set. * @param args.toAccount - Sub-account that will hold the new collateral in toVault * @param args.fromAsset - Underlying asset of fromVault (tokenIn) * @param args.toAsset - Underlying asset of toVault (tokenOut) * @param args.amount - Amount of fromAsset to swap (exact-in) * @param args.origin - EOA sending the transaction * @param args.slippage - Slippage in percent (0–50) * @param args.deadline - Quote deadline timestamp in seconds (optional) * @param args.unusedInputReceiver - Address to receive unused input tokens instead of depositing back to fromVault/fromAccount (optional) * @param args.skipSweepDepositOut - If true, output tokens are left in the Swapper (no deposit of output). (optional) * @returns Promise of array of swap quotes (verify type skimMin). Throws if slippage invalid or no quotes. */ fetchDepositQuote(args: GetDepositQuoteArgs): Promise; /** * Fetches swap quotes for swapping a wallet token into another wallet token. * Delegates to fetchSwapQuotes with zero-address vault/account placeholders, * `unusedInputReceiver` set to origin, `skipSweepDepositOut` enabled, and * `transferOutputToReceiver` enabled so the output is transferred to `receiver`. * * This helper is designed to pair with executionService.planSwapFromWallet(), * which pulls the input token from the sender wallet via SwapVerifier.transferFromSender. * * @param args - Wallet-to-wallet swap quote arguments * @param args.chainId - Chain ID * @param args.fromAsset - Wallet token to sell (tokenIn) * @param args.toAsset - Wallet token to buy (tokenOut) * @param args.amount - Amount of fromAsset to swap (exact-in) * @param args.receiver - Address that receives the output token * @param args.origin - EOA sending the transaction and later authorizing transferFromSender * @param args.slippage - Slippage in percent (0–50) * @param args.deadline - Quote deadline timestamp in seconds (optional) * @returns Promise of array of swap quotes (verify type transferMin). Throws if slippage invalid or no quotes. */ fetchWalletSwapQuote(args: GetWalletSwapQuoteArgs): Promise; private validateSlippage; private buildDepositCowSwapProviderExtraData; private buildRepayCowSwapProviderExtraData; private shouldBuildCowProviderExtraData; private getCowSwapDeadline; private getSlippageBips; private getRequiredCowSwapChainConfig; private isCowSwapProviderName; } //# sourceMappingURL=swapService.d.ts.map