# Swaps

![](https://269125728-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3TvwQN2Bz9GIqAUzIGHO%2Fuploads%2FekElQd0qNBkraHqmOGmD%2Fimage.png?alt=media\&token=3475f53a-5cda-4511-9ba3-3f570d8b3353)

## Introduction <a href="#introduction" id="introduction"></a>

Token swaps in Uniswap are a simple way to trade one ERC-20 token for another.

For end-users, swapping is intuitive: a user picks an input token and an output token. They specify an input amount, and the protocol calculates how much of the output token they’ll receive. They then execute the swap with one click, receiving the output token in their wallet immediately.

In this guide, we’ll look at what happens during a swap at the protocol level in order to gain a deeper understanding of how Uniswap works.

Swaps in Uniswap are different from trades on traditional platforms. Uniswap does not use an order book to represent liquidity or determine prices. Uniswap uses an automated market maker mechanism to provide instant feedback on rates and slippage.

As we learned in [Protocol Overview](https://moonsdust.gitbook.io/rcpswap/protocol-overview), each pair on Uniswap is actually underpinned by a liquidity pool. Liquidity pools are smart contracts that hold balances of two unique tokens and enforces rules around depositing and withdrawing them.

This rule is the [constant product formula](https://moonsdust.gitbook.io/rcpswap/protocol-overview/glossary#constant-product-formula). When a token is deposited (sold), a proportional amount must be withdrawn to maintain the constant. Contrariwise, if a token is withdrawn (purchased), a proportional amount must instead be also deposited.

### Anatomy of a swap <a href="#anatomy-of-a-swap" id="anatomy-of-a-swap"></a>

At the most basic level, all swaps in Uniswap V2 happen within a single function, aptly named `swap`:

```
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data);Copy
```

## Receiving tokens <a href="#receiving-tokens" id="receiving-tokens"></a>

As is probably clear from the function signature, Uniswap requires `swap` callers to *specify how many output tokens they would like to receive* via the `amount{0,1}Out` parameters, which correspond to the desired amount of `token{0,1}`.

## Sending Tokens <a href="#sending-tokens" id="sending-tokens"></a>

What’s not as clear is how Uniswap *receives* tokens as payment for the swap. Typically, smart contracts which need tokens to perform some functionality require callers to first make an approval on the token contract, then call a function that in turn calls transferFrom on the token contract. This is *not* how V2 pairs accept tokens. Instead, pairs check their token balances at the *end* of every interaction. Then, at the beginning of the *next* interaction, current balances are differenced against the stored values to determine the amount of tokens that were sent by the current interactor. See the [whitepaper](https://uniswap.org/whitepaper.pdf) for a justification of why this is the case.

The takeaway is that **tokens must be transferred to pairs before swap is called** (the one exception to this rule is [Flash Swaps](https://moonsdust.gitbook.io/rcpswap/core-concepts/flash-swaps)). This means that to safely use the `swap` function, it must be called from *another smart contract*. The alternative (transferring tokens to the pair and then calling `swap`) is not safe to do non-atomically because the sent tokens would be vulnerable to arbitrage.

## Developer resources <a href="#developer-resources" id="developer-resources"></a>

* To see how to execute a swap from an interface read Trading (SDK)
