Swap
A swap exchanges a specified amount of one asset for another under a quoted route, price rule, fee, and settlement condition.
Category: TradingOpposed arrows category cue
System record
Start with the economic purpose, participants, resources, and entitlements before studying implementation detail.
Why it exists
Swaps are the basic state transition that lets users rebalance, pay, acquire collateral, close positions, or move between financial building blocks.
Traditional-finance analogy
Spot foreign-exchange conversion is the closest comparison recorded for this concept.
Where the analogy stops
- An on-chain swap may execute atomically against a pool, order, solver, or route and is exposed to transaction ordering, gas, reverts, and token behavior.
- The displayed quote is not the settled result unless amount, price, deadline, route, and recipient constraints remain satisfied on-chain.
Main actors
- ActorTrader or authorized caller
- ActorCounterparty, liquidity pool, maker, or solver
- ActorRouter or settlement contract
- ActorValidator, sequencer, or block builder
Assets and claims
Assets — controlled or transformed resources
Assets are resources the mechanism moves, holds, values, or transforms.
- AssetInput token amount
- AssetOutput token amount
Claims — entitlements and corresponding dependencies
Claims are rights to value, repayment, redemption, control, or another party's performance; each depends on an obligation or system that must honor it.
- ClaimUnsettled order or refund entitlement when execution is not atomic
A swap gives one asset and receives another under explicit amount, price, route, fee, deadline, recipient, and settlement conditions.
Why it exists
Section titled “Why it exists”Swaps let users rebalance, make payments, acquire collateral, repay debt, close positions, or connect DeFi building blocks. The short interface can hide a multi-venue route, several token transfers, and important failure conditions.
Traditional-finance analogy
Section titled “Traditional-finance analogy”A spot foreign-exchange conversion is a useful analogy. On-chain execution may be atomic and public, but it adds gas, block ordering, approvals, token hooks, reverts, and chain finality. A screen quote is not settlement unless the transaction enforces compatible bounds.
Exact input and exact output
Section titled “Exact input and exact output”| Order form | Fixed value | User protection |
|---|---|---|
| Exact input | Maximum or exact amount sold | Minimum acceptable output |
| Exact output | Desired amount received | Maximum acceptable input |
A market order without a meaningful bound can settle at an unexpectedly bad price. A very tight bound can revert after ordinary market movement. The right bound depends on reference quality, liquidity, urgency, and adversarial ordering.
Actors, assets, and claims
Section titled “Actors, assets, and claims”The trader or authorized caller supplies the input. A pool, maker, order, or solver supplies the output. A router may split the route. The settlement contract checks constraints, while a validator, sequencer, or block builder decides transaction ordering.
An atomic swap normally creates no lasting claim: both asset changes succeed together or revert. An off-chain order, partial fill, escrow, or cross-domain route can leave an unsettled order or refund claim.
Step-by-step state transition
Section titled “Step-by-step state transition”- Normalize token identity, chain, decimals, and price direction.
- Obtain a quote and record its route, block or time, fees, and assumptions.
- Authorize only the required input and settlement contract.
- Submit exact-input or exact-output bounds, deadline, and recipient.
- Settlement checks live state and performs all authorized transfers.
- Verify actual input, output, fees, events, and any residual approval or claim.
| State | Input asset | Output asset | Order or claim |
|---|---|---|---|
| Before | Held by trader | Not yet received | Authorization may exist |
| Success | Decreases within bound | Increases within bound | Filled or consumed |
| Atomic failure | Remains with trader | No change | No fill; gas spent |
| Partial settlement | Decreases by filled amount | Increases by filled amount | Remainder persists or expires |
Capital flow is the input, output, fee, and gas movement. Information flow is the quote, route, pool or order state, and reference price. Claim flow is absent after a fully atomic spot swap, but a partial order, refund, escrow, or LP position can leave an entitlement. Return flow is absent from the exchange itself; later profit, fee income, or arbitrage has a separate payer and position. Risk flow reaches the trader through price impact, market movement, fees, routing, and ordering; it reaches a maker or LP through changing inventory and adverse selection.
Return source and loss allocation
Section titled “Return source and loss allocation”A swap is an exchange, not a return source by itself. Trader profit or loss comes from later valuation or a connected strategy; a marked gain has no direct payer until settlement or sale transfers value from a counterparty or market buyer. Fees are paid by the trader and allocated to makers, LPs, the protocol, routers, or solvers according to the venue. Gas goes to the chain’s execution and ordering system. The trader bears execution and subsequent valuation loss within the authorized trade, while a maker or LP bears the opposing inventory exposure; a treasury, reserve, or insurer absorbs loss only when an explicit funded rule says so.
Engineer or auditor lens
Section titled “Engineer or auditor lens”Test zero amounts, decimals, fee-on-transfer and callback tokens, exact-input and exact-output rounding, approval scope, reentrancy, route validation, recipient confusion, signature replay, nonces, deadlines, partial fills, refunds, and invariant enforcement. Confirm a failure cannot strand input in an intermediate contract outside the promised model.
Uniswap v2’s swapping documentation shows one pool-based implementation, and its pricing documentation explains exact-input, exact-output, and execution bounds. These pages do not define auction, order-book, RFQ, or solver settlement. Both references were reviewed 2026-08-10.
Common misunderstandings
Section titled “Common misunderstandings”- “The quote is the price.” A quote is conditional on route, state, time, fees, and settlement bounds.
- “Revert means no cost.” Asset changes revert, but gas and lost opportunity can remain.
- “Slippage tolerance is expected slippage.” It is an authorization boundary, not a forecast.
Learn to separate slippage from price impact.
Machine-readable model
Key equations
Canonical expressions come from the structured concept record. KaTeX renders the notation, while the plain-text expression and variable table keep its meaning and units inspectable without JavaScript. Read the narrative above for the model's domain, assumptions, and rounding rules.
This concept does not require one canonical equation. Its mechanism and state transitions remain the authoritative explanation; do not invent a formula merely to make the topic look quantitative.
Assurance contract
Security properties
These structured statements define desired behavior. Their stable IDs can bind tests, invariants, specifications, audit findings, or proof results without turning descriptive review advice into an assurance claim.
Desired · not evaluated: No test, audit, or proof result is implied until scoped evidence is linked to this property.
A successful exact-input swap cannot spend more than its authorized input
A successful exact-output swap cannot require more than its authorized maximum input
Minimum output, maximum input, deadline, route, and recipient constraints are enforced atomically when promised
Failed or expired execution preserves or returns the trader's unspent assets under the stated settlement model
Knowledge check
Quiz
Answer in your own words, then open the model answer.
What problem does Swap exist to address?
Model answer
Swaps are the basic state transition that lets users rebalance, pay, acquire collateral, close positions, or move between financial building blocks.