Exact Input Swap

A swap type where the user specifies exactly how much of the input token to spend, receiving whatever amount of output token the market provides.

Exact Input Swap (also called "exactInputForOutput") is a swap type in automated market makers where the trader specifies precisely how much of the input token they want to spend, accepting whatever amount of the output token the market provides at execution time. This contrasts with exact output swaps where the output amount is fixed and the input amount varies.

Conceptual understanding

An exact input swap is analogous to visiting a currency exchange with a fixed amount of money: "I have exactly $100—how many euros can I get for this?" The exchange rate determines what you receive, but you know precisely what you're spending.

In DeFi context:

  • You control: The exact amount of tokens leaving your wallet
  • Market determines: How many tokens you receive in return
  • Risk exposure: Receiving less than expected due to slippage

Implementation in Uniswap v4

In Uniswap v4, exact input swaps are indicated by a positive amountSpecified value in the SwapParams struct:

1SwapParams memory params = SwapParams({
2 zeroForOne: true, // Direction: Token0 → Token1
3 amountSpecified: 1 ether, // Positive = exact input
4 sqrtPriceLimitX96: minPrice // Slippage protection
5});
6
7BalanceDelta delta = poolManager.swap(key, params, hookData);

The positive sign tells the Pool Manager that 1 ether represents the input amount to consume, and the output should be calculated based on available liquidity.

Gas efficiency advantages

Exact input swaps are generally more gas-efficient than exact output swaps for several reasons:

  1. Direct calculation path: The output amount can be computed directly from the input amount using the AMM formula
  2. No iteration required: Unlike exact output swaps, there's no need to iterate backwards to find the required input
  3. Simpler tick traversal: The swap can process liquidity linearly through price ticks

For protocols executing many swaps or operating on gas-sensitive chains, preferring exact input swaps can provide meaningful cost savings.

Slippage protection

Since the output amount is market-determined, traders must implement slippage protection to avoid receiving significantly less than expected:

1// Calculate expected output off-chain
2uint256 expectedOutput = quoter.quoteExactInput(tokenIn, amountIn);
3
4// Set minimum acceptable output (e.g., 0.5% slippage tolerance)
5uint256 minOutput = expectedOutput * 9950 / 10000;
6
7// Execute swap
8BalanceDelta delta = poolManager.swap(key, params, hookData);
9
10// Validate result
11require(delta.amount1() >= int256(minOutput), "Slippage exceeded");

The sqrtPriceLimitX96 parameter provides additional protection by reverting the swap if price moves beyond acceptable bounds during execution.

Common use cases

Portfolio rebalancing

When rebalancing a portfolio, exact input swaps ensure you sell exactly the intended amount of an overweight asset:

1// Sell exactly 10% of ETH holdings
2uint256 sellAmount = ethBalance * 10 / 100;
3// Swap exact amount, accept market output

Dollar-cost averaging (DCA)

DCA strategies typically use exact input swaps to invest a fixed dollar amount periodically:

1// Invest exactly 100 USDC per period
2uint256 investAmount = 100e6; // USDC has 6 decimals
3// Output varies based on market conditions

Fee payments

When a protocol needs to convert tokens to pay fees in a specific denomination, exact input ensures the source amount is known:

1// Convert exactly the fee amount from protocol token to ETH
2uint256 feeInTokens = calculateProtocolFee();
3// Swap exact amount to cover gas costs

Security considerations

Front-running vulnerability

Exact input swaps broadcast the intended trade size, making them predictable targets for front-running and sandwich attacks. Attackers can:

  1. See your pending exact input swap in the mempool
  2. Buy the output token ahead of you (raising the price)
  3. Let your swap execute at the worse price
  4. Sell immediately after for profit

Mitigations include using private mempools, setting tight slippage bounds, or implementing MEV protection mechanisms.

Decimal handling

When specifying exact input amounts, ensure proper decimal handling:

1// WRONG: 1 "unit" means different things
2uint256 amount = 1;
3
4// CORRECT: Account for token decimals
5uint256 amount = 1 * 10**token.decimals();

Mishandling decimals can result in swapping 1 million times more or less than intended.

Comparison with exact output swaps

AspectExact InputExact Output
User specifiesAmount to spendAmount to receive
amountSpecifiedPositiveNegative
Gas costLowerHigher
Use case"Spend X tokens""Need exactly Y tokens"
RiskReceive less than expectedSpend more than expected

Choose exact input when you care more about controlling what you spend; choose exact output when you need a precise amount of the output token.

Need expert guidance on Exact Input Swap?

Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.

Get a Quote

oog
zealynx

Smart Contract Security Digest

Monthly exploit breakdowns, audit checklists, and DeFi security research — straight to your inbox

© 2026 Zealynx