Skip to content
Reading depth

Each view includes the earlier layers; the complete engineer or auditor page is shown by default.

A swap exchanges a specified amount of one asset for another under a quoted route, price rule, fee, and settlement condition.

Opposed arrows category cue

A swap gives one asset and receives another under explicit amount, price, route, fee, deadline, recipient, and settlement conditions.

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.

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.

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.

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.

  1. Normalize token identity, chain, decimals, and price direction.
  2. Obtain a quote and record its route, block or time, fees, and assumptions.
  3. Authorize only the required input and settlement contract.
  4. Submit exact-input or exact-output bounds, deadline, and recipient.
  5. Settlement checks live state and performs all authorized transfers.
  6. Verify actual input, output, fees, events, and any residual approval or claim.

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.

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.

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.

  • “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.

  1. A successful exact-input swap cannot spend more than its authorized input

  2. A successful exact-output swap cannot require more than its authorized maximum input

  3. Minimum output, maximum input, deadline, route, and recipient constraints are enforced atomically when promised

  4. 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.