Skip to main content

Decentralized exchange overview

Beginner
Concept

A decentralized exchange, or DEX, is a peer-to-peer marketplace where transactions are executed directly between parties. There is no central intermediary that executes the transaction, such as a bank, broker, or other institution. Each transaction is recorded directly onchain. DEXs are primarily used to trade or swap cryptocurrencies.

Centralized exchanges versus decentralized exchanges

A centralized exchange allows for fiat currencies and cryptocurrencies to be exchanged for one another. On a centralized exchange such as Coinbase, you can trade ICP for its worth in USD, then transfer that USD directly to your bank.

A decentralized exchange only allows for cryptocurrencies to be traded for other cryptocurrencies through crypto pairs. They may also support more advanced actions like limit orders and margin trades. Each transaction is handled by the decentralized exchange via an order book, which uses the current buy and sell orders for that token to establish the price. Decentralized exchanges use a set of smart contracts to establish the price of different cryptocurrencies against each other. They also use liquidity pools, where investors can facilitate trades by locking funds within an exchange in return for rewards.

ICP decentralized exchanges

ICP enables decentralized exchanges and order books to run onchain, utilizing ICP's cheap storage and computation. The DEX's internal balance and transactions can be stored in a canister, alongside user balances and data.

Some examples of DEXs running on ICP include:

Interacting with a DEX

To use a decentralized exchange (DEX) on ICP, you can interact with the web application for the exchange, use tooling such as an SDK or API for more technical interactions, or create your own decentralized exchange application. DEXs are used to interact with the token mechanics of ICP, the native token of the Internet Computer, and ICRC-1 compliant tokens.

Tooling

To use a decentralized exchange, you can interact with its web application directly. For developers, several DEXes on ICP have also created utilities, such as SDKs and APIs, that allow for more technical interactions, such as:

Moreover, the following DEXes have open-sourced their entire codebase for you to reference:

Examples

The following code samples demonstrate implementing a decentralized exchange on ICP:

  • DEX using DIP-20 tokens on ICP in Motoko and Rust.

Please note that DIP-20 has been replaced by ICRC-2 and is not recommended for the deployment of new tokens.

  • Swap and transfer ICRC-1/ICRC-2 tokens in Rust and Motoko.

  • Transfer ICRC-1 token on behalf of another account in Rust and Motoko.