Automated Market Making of Trading Pool
We introduce a new invariant function associated with generalised mean that underpins the ALEX AMM. ALEX builds DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by Stacks. With a suitable parameterisation, the invariant function supports both risky pairs (i.e.
), stable pairs (i.e.
) and any linear combination in-between (i.e. Curve). We also show that our invariant function maps
to the liquidity distribution of Uniswap V3.
At ALEX, we build DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by Stacks. As such, we focus on trading, lending and borrowing of crypto assets with Bitcoin as the settlement layer and Stacks as the smart contract layer. At the core of this focus is the automated market making ("AMM") protocol, which allows users to exchange one crypto asset with another trustlessly. This paper focuses on technical aspects of AMM.
ALEX AMM is built on three beliefs: (i) it is mathematically neat and reflect economic demand and supply and (ii) it is a type of mean, like other AMMs.
We will firstly review some desirable features of AMM that ALEX hopes to exhibit.
AMM protocol, which provides liquidity algorithmically, is the core engine of DeFi. In the liquidity pool, two or more assets are deposited and subsequently swapped resulting in both reserve and price movement. The protocol follows an invariant function
is constant. When
, which is the common practise by a range of protocols, AMM
can be expressed as
. Although it is not always true,
tends to be twice differentiable and satisfies the following
- monotonically decreasing, i.e.. This is because price is often defined as. A decreasing function ensures price to be positive.
- convex, i.e.. This is equivalent to say thatis a non-increasing function of. It is within the expectation of economic theory of demand and supply, as more reserve ofmeans declining price.
can usually be interpreted as a form of mean, for example, mStable relates to arithmetic mean, where
(constant sum formula); one of the most popular platforms Uniswap relates to geometric mean, where
(constant product formula); Balancer, which our collateral rebalancing pool employs, applies weighted geometric mean. Its AMM is
are fixed weights. ALEX AMM extends these to create a generalised mean.
After extensive research, we consider it possible for ALEX AMM to be connected to generalised mean defined as
. The expression might remind readers of
. It is however not true when
as triangle inequality doesn't hold.
is fixed, the core component of generalised mean is assumed constant as below.
This equation is regarded reasonable as AMM, because (i) function
is monotonically decreasing and convex; and (ii) The boundary value of
corresponds to constant sum and constant product formula respectively. When
decreases from 1 to 0, price
gradually converges to 1, i.e. the curve converges from constant product to constant sum (see Appendix 1 for the relevant proofs).
Market transaction, which involves exchange of one crypto asset and another, satisfies the invariant function. Please note the formulae do not account for the fee re-investment, which results in a slight increase of
after each transaction, like Uniswap V2.
In order to purchase
amount of token Y from the pool, the buyer needs to deposit
amount of token X.
satisfy the following
After each transaction, balance is updated as below:
. Rearranging the formula results in
When transaction cost exists, the actual deposit to the pool is less than
is the actual amount and
is the fee, above can now be expressed as
constant, the updated balance is:
This is the opposite case to above. We are deriving
Sometimes, trader would like to adjust the price, perhaps due to deviation of AMM price to the market value. Define
the AMM price after rebalancing the token X and token Y in the pool
Then, the added amount of
can be calculated from the formula below
ALEX's invariant function is
It can be rearranged as
should both be positive meaning the liquidity pool contains both tokens.
is monotonically decreasing and convex.
This is equivalent to prove
The last inequality holds because each component is positive.
= 1, it is straightforward to see that the invariant function is constant sum. To show that the invariant function converges to constant product when
= 0, we will show and prove an established result in a generalised
. Applying L'Hospital rule to the exponent, which is 0 in both denominator and nominator when
, we have
When d = 2,
Proof of the corollary is trivial, as it is a direct application of the theorem. It shows that generalised mean AMM implies constant product AMM when
As Uniswap v3 is able to simulate liquidity curve of any AMM, we are interested in exploring the connection between ALEX's AMM and that of Uniswap's. Interesting questions include: what is the shape of the liquidity distribution? Which point(s) has the highest liquidity? We acknowledge that the section is more of a theoretical study for now.
Uniswap V3 AMM can be expressed as a function of invariant constant
with respect to price
. For us, the difference in the invariant function means we can write price as
) and we have
Based on the previous sections, we can then express
Figure 1 plots
(which is proportional to
) regarding various levels of
is symmetric around 0% at which the maximum reaches . This is because
- 1.is symmetric around= 0% with minimum at 0% and the minimum value 1;
- 2.is a decreasing function ofwhenis positive and poweris negative. In our case, we have. Therefore, it is the maximum rather than minimum thatachieves at 0.
Furthermore, the higher the
, the flatter the liquidity distribution is. When
approaches 1, i.e. AMM converges to the constant product formula, the liquidity distribution is close to a flat line. When
approaches 0, the distribution concentrates around 0%.