Xfai V0 Protocol Swaps


Token swaps on the Xfai decentralized exchange (DEX) provide a convenient way to trade one ERC20 token for another. To initiate a swap, users simply select an input and output token, and specify either the desired input or output amount. The Xfai protocol will then calculate the other amount automatically. Unlike traditional order-book exchanges, trades on Xfai do not have to wait for matching buy and sell orders. This is possible because of Xfai's automated market maker (AMM) design, also known as a constant product market maker (CPMM) model. However, Xfai's CPMM model is unique in that it utilizes a weighted design, which eliminates the need for fragmenting liquidity across multiple token pairs and allows for more efficient trades with lower slippage.

Entangled Swaps

One of the key features that sets the Xfai DEX apart is its ability to concentrate liquidity into unique pools. In other words, instead of having to fragment liquidity across many token pairs, Xfai concentrates liquidity into weighted pools. The deep token pools enable end-users to perform swaps with drastically reduced slippage. The nature of Xfai pools is also dynamic. That is, a pool's weight, which determines its token's exchange value, can dynamically change throughout time. For more information on the weight mechanism of Xfai's liquidity pools, see the Theory subsection of this page.

Anatomy of a swap

In the background, every swap in the Xfai protocol ends up calling a single function named swap:

function swap(
        address _token0, 
        address _token1, 
        address _to

Theswap caller has to specify both the input token (_token0), as well as the output token (_token1). In practice, the parameter values for swap are handled automatically by the Periphery contract in the background. Direct interactions with the swap function of the Core contract are not advised, as it does not perform important safety checks.

Usually end-users have to make a token approval for smart contracts to perform some functions (e.g. swap) using their tokens. This is not the case for Xfai pools. Instead, tokens must be sent to a pool before swap is called.


The exchange value for a trade (aka swap) is determined using a deterministic "exchange function". In the case of Xfai's CFMM implementation, a constant product market maker (CPMM) model is used:

This simple mechanism enables the Xfai model to dynamically increase and decrease exchange values in a self-organizing manner, depending on a pool's market demand. The design solves the fragmented liquidity problem found in many other DEXs, while enabling at the same time better trades.

Developer resources

To see how to implement token swaps in a smart contract read the Implement a Swap subsection of the developers documentation.

Last updated