Flash Swap
A Uniswap V2 feature where the Pair contract sends output tokens to the recipient before checking the K-invariant, letting the recipient use them temporarily as long as they return sufficient tokens in the same transaction.
A flash swap is a capability built into the Uniswap V2 Pair contract that allows a caller to receive output tokens before paying for them, as long as they repay within the same transaction. It is how Uniswap enabled flash loan functionality as a native primitive of the AMM, not as a separate system.
How It Works Mechanically
When a user calls pair.swap(amount0Out, amount1Out, to, data):
- The Pair optimistically transfers the requested tokens to the
toaddress. - If
data.length > 0, the Pair invokesIUniswapV2Callee(to).uniswapV2Call(...)— a callback giving the recipient control with the tokens in hand. - Inside the callback, the recipient can do anything — arbitrage, liquidations, collateral swaps, rebalancing — as long as by the end of the callback the Pair's post-swap token balances satisfy the K-invariant check.
- The Pair then reads its token balances, computes
balanceAdjusted0 * balanceAdjusted1, and reverts if the K-invariant is violated.
The check at the end is what makes this safe. If the recipient does not return enough tokens (from the same pair or otherwise), the transaction reverts and all state changes unwind.
Why It Matters
Flash swaps were a quiet but significant innovation in Uniswap V2. Before flash swaps, a DeFi protocol that wanted to use Uniswap liquidity for arbitrage or liquidations had to pre-fund capital, pay gas to move it, and accept execution risk. Flash swaps made that capital unnecessary. You can borrow $1M of DAI from a Uniswap pool, use it, pay it back plus the swap fee, and never actually needed $1M in your account.
This primitive is also the attack surface for some of the most famous DeFi exploits. Any contract that composes with Uniswap V2 must be defensive against flash-swap-enabled attacks — especially oracle manipulation. A malicious actor can flash-swap a huge amount of tokens, distort the spot price, use the distorted price to manipulate a downstream protocol, and return the tokens. This is why TWAP oracles and other time-averaged price feeds exist.
Flash Swap vs Flash Loan
Flash loans (Aave, dYdX, Uniswap V3's "flash transactions") are a more general primitive. You borrow any asset, do anything with it, repay in the same transaction. Uniswap V2's flash swap is specifically a swap with a callback — the borrower must end up swapping one token for the other, not just borrowing a single token.
In practice, you can use a flash swap to borrow a single token (by setting amount1Out = 0) and pay back in the same token, but you pay the Uniswap swap fee (0.3%) on the way. For pure borrow-and-return use cases, a protocol-specific flash loan is usually cheaper. For arbitrage across pairs, flash swaps shine.
Implementation Trap
When rebuilding Uniswap V2 from scratch, the most common flash-swap bug is checking the K-invariant against the Pair's stored reserves instead of its current balances. Stored reserves are the pre-swap values; balances reflect what actually arrived. If you use reserves in the check, the flash-swap recipient can trivially withdraw tokens without returning them. The correct check uses balance, not reserve. The Zealynx Academy Uniswap V2 module forces you to get this right — several of the 207 automated tests are dedicated to exactly this distinction.
Articles Using This Term
Learn more about Flash Swap in these articles:
Related Terms
Automated Market Maker (AMM)
A decentralized exchange protocol that uses mathematical formulas to price assets instead of order books.
Uniswap V2
The second version of the Uniswap AMM protocol, released in 2020. A minimal constant-product automated market maker composed of a Factory, Pair contracts, a Router, and a Library. The most-forked DeFi contract ever shipped.
K-Invariant
The core invariant of Uniswap V2 and other constant-product AMMs: `reserve0 * reserve1 >= k` after every swap. Its enforcement is what prevents attackers from draining the pool by trading at off-curve prices.
Flash Loan
Uncollateralized loan borrowed and repaid within a single transaction, often used for arbitrage or attacks.
Need expert guidance on Flash 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

