WhiteSwap
  • About WhiteSwap
  • Protocol Overview
    • How WhiteSwap Works
    • Eсosystem Participants
    • Smart Contracts
    • Glossary
  • Core Concepts
    • Swaps
    • Pools
    • Flash Swaps
    • Oracles
    • Farming
    • WSD Staking
  • Advanced Topics
    • Fees
    • Pricing
    • Understanding Returns
    • Security
    • Research
  • Governance
    • Overview
    • Process
    • Glossary
  • Governance Token
    • Introducing WSD
    • Allocation & Vesting
    • Community Treasury
  • Developer Guides
    • Token Listing
    • Javascript SDK
      • SDK Quick Start
      • Fetching Data
      • Pricing
      • Trading
      • Pair Addresses
  • Reference
    • API
      • API Overview
      • Entities
      • Queries
    • SDK
      • Getting Started
      • Token
      • Pair
      • Route
      • Trade
      • Fractions
      • Fetcher
      • Other Exports
    • Smart Contracts
Powered by GitBook
On this page
  • Introduction
  • Anatomy of a swap
  • Receiving tokens
  • Sending Tokens
  • Developer resource

Was this helpful?

  1. Core Concepts

Swaps

PreviousCore ConceptsNextPools

Last updated 4 years ago

Was this helpful?

Introduction

Token swaps in WhiteSwap are a simple way to trade one ERC20 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 WhiteSwap works.

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

Anatomy of a swap

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

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

Receiving tokens

As is probably clear from the function signature, WhiteSwap 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

What’s not as clear is how WhiteSwap 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 WhiteSwap 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.

Developer resource

As we learned in , each pair on WhiteSwap is actually underpinned by a liquidity pool. Liquidity pools are smart contracts that hold balances of two unique tokens and enforce rules around depositing and withdrawing them.

This rule is the . When either token is withdrawn (purchased), a proportional amount of the other must be deposited (sold), in order to maintain the constant.

The takeaway is that tokens must be transferred to pairs before swap is called (the one exception to this rule is ). 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.

To see how to execute a swap from an interface read

Protocol Overview
Flash Swaps
Trading (SDK)
constant product formula