Comment on page


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.
Xfai's entangled swap mechanism


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:
(ri+γΔi)(wiΔw)(rjΔj)(wj+Δw)=k\left(r_i + \gamma \Delta_i \right) \left(w_i - \Delta_w\right) \left(r_j - \Delta_j \right) \left(w_j + \Delta_w\right) = k
are the smart contract's reserves of token
and token
within pool
and pool
are the exchange value weights of pool
and pool
is the amount of tokens $i$ that are sent to pool
is the amount of tokens
that get removed from pool
and sent to a recipient, and
is the amount of weights that need to be added to
and subtracted from
In Xfai, as in any CPMM model, the trade between a pair of assets has to happen in a way that keeps the product of the two asset reserves unchanged after the trade. In other words, the constant
has to remain equal before and after a swap. In practice, because
is usually set to a none-zero value, each trade slightly increases
. If we assume that a trade has no fees, we can rewrite the previous equation as:
(ri+Δi)(wiΔw)(rjΔj)(wj+Δw)=riwirjwj\left(r_i + \Delta_i \right) \left(w_i - \Delta_w\right) \left(r_j - \Delta_j \right) \left(w_j + \Delta_w\right) = r_i w_i r_j w_j
To know therefore how many tokens
one receives for inserting a given amount of
tokens, one would rewrite the previous equation as:
Δj=(Δiwiri+Δirjwj+Δiwiri+Δi)\Delta_j = \left(\frac{\frac{\Delta_i w_i}{r_i + \Delta_i} r_j}{w_j + \frac{\Delta_i w_i}{r_i + \Delta_i}}\right)
It is worth noting that the Xfai CFMM model design does not use ERC20 token pairs, that is, it does not rely on a sparse matrix of exchange values, but instead uses unique ERC20 token pools. In other words, it relies on a vector of exchange values. That is, Xfai uses an ETH weighted pool system instead. Each pool is made of an ERC20 token reserve
and a dynamic ETH denominated weight
to determine the exchange values of the token. One key advantage of Xfai's weighted pool model, is that i removes the need for liquidity fragmentation. This allows the Xfai CPMM model to form deep liquidity pools that can perform swaps with an overall lower slippage than in token-pair-based CFMM models.
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.