Docs / Application security

Field guide · Web3 foundations

Web3 foundations

What is a swap?

By Abhimanyu Gupta, Founder & Principal Operator

On this page

A swap is the on-chain version of changing money: put in some of token A, receive token B, in one transaction, against a contract rather than a custodial company. You never give up control of your funds.

Read with a purpose. A swap is atomic and permissionless, but the price moves as trades land, so always set a minimum output and a deadline or it can settle far worse than the quote.

At a glance

A swap is
Trading one token for another in a single on-chain transaction, without handing your funds to a company first
Where it happens
On a decentralised exchange, a smart contract you trade against directly, most often an automated market maker
It is atomic
The whole trade either completes or reverts. You never end up having paid without receiving
It is permissionless
No sign-up, no account, no approval from anyone. Your wallet connects and trades
The catch
The price can move between quote and execution, so a swap should always set a minimum acceptable output

Trading one token for another, on-chain

A swap is the on-chain version of changing money: you put in some of token A and receive token B in return. The difference from a traditional exchange is where the trade takes place. On a centralised exchange you first deposit your funds, the company holds them, and its internal database records the trade. In an on-chain swap you never give up custody. You send a transaction that hands token A to a contract and, in the same breath, receive token B back to your own wallet. The contract is the counterparty, and the ledger records the result directly.

Where a swap happens

The contract you swap against is a decentralised exchange, or DEX. Most modern DEXes are automated market makers, which hold pooled reserves of each token and price your trade with a formula rather than by matching you to another trader. You do not need to find someone who wants the opposite of your trade; you trade against the pool, and it is always available. How that pool sets its price is worth a guide of its own, on AMMs, which pairs naturally with this one.

Your wallet DEX contract(a pool) token A in token B out

Atomic and permissionless

Two properties make on-chain swaps feel different once you are used to them. They are atomic: the transaction that takes your token A and gives you token B is a single unit, so it either fully succeeds or fully reverts as though it never happened. There is no state where the contract has your money and you have nothing. And they are permissionless: there is no account to open and no one to approve you. Any wallet can call the contract, which is liberating and also means no safety net if you trade against a malicious or broken pool. One practical wrinkle: before a contract can pull an ERC-20 token from you, you grant it an allowance, which is the approval step discussed in the signature-phishing guide, so treat those approvals with care.

Price, slippage, and the catch

An AMM's price is not fixed; it shifts with every trade, including yours, and with any trade that lands just before yours. The quote your wallet shows is a prediction, not a promise. The gap between the expected price and the one you actually get is called slippage. To protect yourself, a swap sets a minimum output: the least amount of token B you will accept, below which the whole transaction reverts. Without it, a trade can execute at a far worse price than you saw, whether from ordinary market movement or from someone deliberately pushing the price around your trade, which is the sandwich attack covered in the front-running guide. Setting a sensible minimum output and a deadline is the single most important habit when swapping.

The short version. A swap trades one token for another in one on-chain transaction, against a decentralised exchange rather than a custodial company, so you keep control of your funds throughout. It is atomic, so you never pay without receiving, and permissionless, so anyone can trade with no safety net. The price moves as trades land, so always set a minimum output and a deadline, or a swap can settle far worse than the quote suggested.

References & further reading

  1. Ethereum community, Decentralised finance (DeFi). Where swaps sit in the wider picture.
  2. Ethereum community, ERC-20 tokens and approvals. The allowance a swap needs first.
  3. Uniswap, How swaps work. The mechanics on a leading DEX, including slippage.
All guides Web app penetration testing

Want this tested on you?

Reading about it is one thing. Seeing it proven on your own systems is another.