SV3 logo

maxTradeForPriceImpact

Largest exact-in buy or sell whose Uniswap-style impact is at most a cap.

Binary-searches quoteBuyExactReserveIn or quoteSellExactAvmIn until priceImpact.totalPriceImpactWad is ≤ maxTotalPriceImpactWad.

This is a UI helper (impact-limited sliders). It is not an on-chain view.

Import

import { maxTradeForPriceImpact } from "@repo/contract-client/math";

Usage

import { asWad } from "@repo/contract-client/types";
import { maxTradeForPriceImpact } from "@repo/contract-client/math";

const { amountIn, quote } = maxTradeForPriceImpact(snapshot, {
  side: "buy",
  maxTotalPriceImpactWad: asWad(10n ** 16n), // 1%
});

amountIn; // USDC raw on a buy, AVM atoms on a sell
quote.priceImpact.totalPriceImpactWad;

Return Value

{ amountIn: bigint, quote: TradeQuote }

The largest input that still fits. quote is the matching exact-in quote.

Parameters

snapshot

  • Type: MarketSnapshot
maxTradeForPriceImpact(snapshot, { side: "buy", maxTotalPriceImpactWad });

params.side

  • Type: 'buy' | 'sell'

Buy searches USDC in. Sell searches AVM in.

params.maxTotalPriceImpactWad

  • Type: Wad

Cap on tradePriceImpact totalPriceImpactWad. 10n ** 16n is 1%.

params.cap (optional)

  • Type: bigint

Search upper bound. Defaults to 1_000_000_000_000n USDC raw on buy, snapshot.curveSupply on sell.

maxTradeForPriceImpact(snapshot, {
  side: "sell",
  maxTotalPriceImpactWad: asWad(5n * 10n ** 16n),
  cap: snapshot.curveSupply / 2n,
});

Error

InvalidAmountError if cap <= 0 or no trade fits the impact budget (including amounts below min settlement).