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 → Token13 amountSpecified: 1 ether, // Positive = exact input4 sqrtPriceLimitX96: minPrice // Slippage protection5});67BalanceDelta 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:
- Direct calculation path: The output amount can be computed directly from the input amount using the AMM formula
- No iteration required: Unlike exact output swaps, there's no need to iterate backwards to find the required input
- 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-chain2uint256 expectedOutput = quoter.quoteExactInput(tokenIn, amountIn);34// Set minimum acceptable output (e.g., 0.5% slippage tolerance)5uint256 minOutput = expectedOutput * 9950 / 10000;67// Execute swap8BalanceDelta delta = poolManager.swap(key, params, hookData);910// Validate result11require(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 holdings2uint256 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 period2uint256 investAmount = 100e6; // USDC has 6 decimals3// 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 ETH2uint256 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:
- See your pending exact input swap in the mempool
- Buy the output token ahead of you (raising the price)
- Let your swap execute at the worse price
- 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 things2uint256 amount = 1;34// CORRECT: Account for token decimals5uint256 amount = 1 * 10**token.decimals();
Mishandling decimals can result in swapping 1 million times more or less than intended.
Comparison with exact output swaps
| Aspect | Exact Input | Exact Output |
|---|---|---|
| User specifies | Amount to spend | Amount to receive |
| amountSpecified | Positive | Negative |
| Gas cost | Lower | Higher |
| Use case | "Spend X tokens" | "Need exactly Y tokens" |
| Risk | Receive less than expected | Spend 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.
Articles Using This Term
Learn more about Exact Input Swap in these articles:
Related Terms
Exact Output Swap
A swap type where the user specifies exactly how much of the output token to receive, paying whatever amount of input token the market requires.
Slippage
The difference between the expected price of a trade and the actual execution price, caused by market movement or liquidity constraints.
BalanceDelta
A struct in Uniswap v4 that represents the net change in token balances resulting from pool operations like swaps or liquidity modifications.
Automated Market Maker (AMM)
A decentralized exchange protocol that uses mathematical formulas to price assets instead of order books.
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

