The Formulas
This page shows the formulas used in mimo protocol.
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
that has been widely adopted by AMM based DEX, such as uniswap.
Assume that
is the source token, and
is the destination token. In mimo,
,
could be either IOTX or XRC20 tokens. Let
,
be X-token, Y-token in current liquidity pool, respectively.
Based on the famous AMM equation
where
is a constant.
The product of
and
remains the same before and after trading. For details, please refer to vbuterin's post.
Let's further define
,
are how many X-tokens you want to pay, and how many Y-tokens you will get, respectively.
We'd like to know, the price based on
or
. If
getInputPrice
denotes how many Y-Tokens (i.e. ) can be bought by selling a given
,
or in code,
getInputPrice(x, y, dx) = (y * 997 * dx) / (1000 x + 997 dx)
If
getOutputPrice
denotes how many X-tokens is needed to buy Y-tokens,
or in code,
getOutputPrice(x, y, dy) = 1000 x * dy / ((y - dy) * 997) + 1
where
/
in above equations denotes divToInteger
, which means divide with rounding to floor of the results.In AMM, the price would change after each trade,
,
and
the liquidity in the pool. 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
, or
. One is based on input , one is based on output.
or
in code,
price impact(x, y, dx) = (1000*x)^2 / (1000*x + 997*dx)^2 - 1
price impact(x, y, dy) = (y - dy)^2 / y^2 - 1
Note that the price impact is always between
-1
and 0
.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
or in code,
price impact = PI1 * PI2 + PI1 + PI2
where
PI1 is the price impact of first trading pair, such as x to IOTX
PI2 is the price impact of second trading pair, such as IOTX to y
Last modified 3yr ago