DEV Community

Merissa Stemler
Merissa Stemler

Posted on

4 Steps to Price an Exact-Output Swap From Your Wallet

If you are used to placing a buy order on a centralised exchange, an exact-output swap lets you set how many tokens you want to receive and puts a ceiling on how much you will spend. The pool’s current reserves determine the input quote, which can change before your wallet transaction executes.

1. Set the amount you want to receive

An exact-output trade fixes the quantity of the token you buy; the required input is calculated from the pool. That reverses the more familiar exact-input swap, where you choose what to spend and accept the resulting output.

For example, you might ask a pool to deliver exactly 2,000 USDC. Your wallet or trading interface then estimates the required input and asks you to allow up to a maximum. If a pending wallet action leaves you unsure whether to retry, check the network and transaction nonce first; how to recover a stuck SpookySwap transaction explains how to decide whether to wait, replace, or resubmit.

2. The pool’s reserves set the required input

In a constant-product pool, the reserve balances shape the price: the larger your requested output is relative to the available output tokens, the more input the pool requires. The calculation uses the reserves at execution time and includes the pool’s trading fee.

For a V2-style pool, let x be the input-token reserve, y the output-token reserve, and z your desired output. If the fee is f, the required input is approximately x × z ÷ ((y − z) × (1 − f)). Contracts round the result up to the smallest token unit so the pool receives enough after the fee. The Uniswap V2 Core whitepaper describes the constant-product mechanism behind this calculation.

Say a pool holds 100 ETH and 200,000 USDC, and you request 2,000 USDC with an illustrative 0.3% fee. The quote is about 1.013 ETH, before network gas. At the pool’s starting ratio, 2,000 USDC would correspond to 1 ETH; the extra input reflects both the fee and the price impact of removing USDC from the pool.

3. Your maximum input limits what the wallet can spend

The maximum input is the most the swap contract may take from your wallet to deliver the exact output. Set it above the current quote by your chosen slippage tolerance—the allowance for the quote to worsen before execution. If reserves move enough that the required input exceeds this ceiling, the swap reverts instead of taking more.

Check the pool fee and estimated network gas separately: the fee is part of the trade calculation, while gas is paid to process the transaction. On SpookySwap, fees depend on the selected pool; its V3 documentation lists fee levels from 0.01% to 1%. A quoted amount can also change while the transaction waits to be included in a block.

4. Check the pool type before you submit

For a V2-style pool, compare the requested output and maximum input with the displayed quote, then confirm that the wallet shows the intended network and tokens. A route through multiple pools is calculated hop by hop, so each pool’s reserves and fee affect the final input.

The reserve formula above does not describe every pool. In a concentrated-liquidity pool, such as V3, the calculation follows liquidity available across the active price range rather than treating all deposited tokens as one reserve pair. If the quote requires more input than you accept, reduce the output or choose a route with better available liquidity; a larger order can make a thin pool expensive even when its displayed starting price looks attractive.

Does exact output guarantee the price?

No. It guarantees the requested output only if the swap executes within your maximum-input limit. If the pool changes before execution and needs more input than that limit, the transaction reverts; your wallet may still charge gas for the failed attempt.

Why is the input quote higher than the starting pool price?

The starting ratio reflects only the reserves before your trade. Buying output tokens reduces their reserve, moving the pool price as the swap progresses. The fee adds another cost, so the total input can exceed what the starting ratio alone suggests.

When should I choose a smaller exact output?

Choose less when the quote or maximum input is beyond your budget, especially in a shallow pool where a large request moves the price sharply. For a concentrated-liquidity pool, the relevant factor is liquidity across the price range your trade crosses. If that range is thin, reducing the trade can improve its quoted cost.

Top comments (0)