Skip to content

The Formulas

This page shows the formulas used in mimo protocol.

Overview

In each trade, traders trade certain amount of a particular token for certain amount of another token with the price defined by a formula. There is no orderbook and waiting for fulfillment.

The formula that mimo uses is the famous xy=kx \cdot y = k that has been widely adopted by AMM based DEX, such as Uniswap.

The formula xy=kx \cdot y = k

Assume that XX is the source token, and YY is the destination token. In mimo, XX, YY could be either IOTX or XRC20 tokens. Let xx, yy be X-token, Y-token in current liquidity pool, respectively.

Based on the famous AMM equation:

xy=kx \cdot y = k

where kk is a constant.

The product of xx and yy remains the same before and after trading. For details, please refer to Vitalik Buterin's post.

Pricing based on the inputs

Let's further define dxdx, dydy are how many X-tokens you want to pay, and how many Y-tokens you will get, respectively.

getInputPrice

If getInputPrice denotes how many Y-Tokens (i.e. dydy) can be bought by selling a given dxdx:

getInputPrice(x,y,dx)=y997dx1000x+997dxgetInputPrice(x, y, dx) = \frac{y \cdot 997 \cdot dx}{1000 \cdot x + 997 \cdot dx}

getOutputPrice

If getOutputPrice denotes how many X-tokens is needed to buy dydy Y-tokens:

getOutputPrice(x,y,dy)=1000xdy(ydy)997+1getOutputPrice(x, y, dy) = \frac{1000 \cdot x \cdot dy}{(y - dy) \cdot 997} + 1

where / in above equations denotes divToInteger, which means divide with rounding to floor of the results.

Price Impact

In AMM, the price would change after each trade. The price impact is what traders want to know before the trade.

There are two ways of calculating price impact. It can be based on x,y,dxx, y, dx, or x,y,dyx, y, dy. One is based on input, one is based on output.

Based on input (dxdx)

PriceImpact(x,y,dx)=(1000x)2(1000x+997dx)21PriceImpact(x, y, dx) = \frac{(1000 \cdot x)^2}{(1000 \cdot x + 997 \cdot dx)^2} - 1

Based on output (dydy)

PriceImpact(x,y,dy)=(ydy)2y21PriceImpact(x, y, dy) = \frac{(y - dy)^2}{y^2} - 1

Note

The price impact is always between -1 and 0.

Cross-Trading Price Impact

If there are no direct trading pairs between two tokens, like in V1 where we only support IOTX/token pairs, traders need to use one token, such as IOTX, as a bridge to trade among two tokens.

In this case, the price impact would be:

PriceImpactcross=PI1PI2+PI1+PI2PriceImpact_{cross} = PI_1 \cdot PI_2 + PI_1 + PI_2

where:

  • PI1PI_1 is the price impact of first trading pair, such as xx to IOTX
  • PI2PI_2 is the price impact of second trading pair, such as IOTX to yy

Released under the MIT License.