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

> **For AI agents:** the complete documentation index is at [llms.txt](/llms.txt). Append `.md` to any page URL for its markdown version.

Binary-searches [quoteBuyExactReserveIn](/developers/math/quote-buy-exact-reserve-in) or [quoteSellExactAvmIn](/developers/math/quote-sell-exact-avm-in) until `priceImpact.totalPriceImpactWad` is ≤ `maxTotalPriceImpactWad`.

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

## Import

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

## Usage

```ts
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`

```ts
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](/developers/math/trade-price-impact) `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.

```ts
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).
