CenturionDEX
Launch App

Swaps

Last modified:

How swaps work

A swap trades one ERC-20 token for another. The user specifies an input token, an output token, and an amount. The protocol calculates the output using the constant product formula and transfers the result to the user's wallet.

CenturionDEX does not use an order book. Instead, each pair holds pooled reserves of two tokens and prices trades algorithmically. When either token is withdrawn, a proportional amount of the other must be deposited to preserve the invariant.

Anatomy of a swap

All V2 swaps execute through a single function on the pair contract:

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

Receiving tokens

The caller specifies how many output tokens to receive via amount{0,1}Out, corresponding to the desired amount of token{0,1}.

Sending tokens

Pairs do not use the standard approve / transferFrom pattern. Instead, they check their token balances at the end of every interaction and diff against stored values at the start of the next one. This means tokens must be transferred to the pair before calling swap (the exception is flash swaps).

Because the transfer and swap must be atomic, swap should be called from a smart contract — not sent as a separate transaction. The Router handles this for most integrators.

See the whitepaper for a detailed justification of this design.

Developer resources