diff --git a/README.md b/README.md index cac2a21..01938c6 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,3 @@ # What is ALEX? -## ALEX is an open-source DeFi protocol built on Bitcoin via [Stacks](https://www.stacks.co), modelled on, yet extending, the world’s existing financial markets. - -Upon launch, at ALEX you can: - -* Lend your BTC and earn fixed or variable interest -* Borrow BTC without risk of liquidation -* Mitigate risk with BTC short positions -* Maximize upside with BTC leveraged long positions - -Our initial focus (v1) is to: - -* Build DeFi primitives targeting developers looking to build ecosystem on Stacks, and -* Simplify the user experience going from(to) CeFi to(from) DeFi to attract non-developers, broadening the Stacks user base. +ALEX builds DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by [Stacks](https://www.stacks.co/). diff --git a/SUMMARY.md b/SUMMARY.md index 784706e..ec24ead 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -4,7 +4,8 @@ ## Whitepaper -* [Automated Market Making of Yield Token Pool](whitepaper/automated-market-making-of-alex.md) +* [ALEX AMM Protocol](whitepaper/automated-market-making-of-alex/README.md) + * [Copy of Automated Market Making of Yield Token Pool](whitepaper/automated-market-making-of-alex/automated-market-making-of-alex.md) * [Automated Market Making of Collateral Rebalancing Pool](whitepaper/automated-market-making-of-collateral-rebalancing-pool.md) * [Dive Into Collateral Rebalancing Pool!](whitepaper/dive-into-collateral-rebalancing-pool.md) diff --git a/whitepaper/automated-market-making-of-alex/README.md b/whitepaper/automated-market-making-of-alex/README.md new file mode 100644 index 0000000..2159b1a --- /dev/null +++ b/whitepaper/automated-market-making-of-alex/README.md @@ -0,0 +1,202 @@ +# ALEX AMM Protocol + +## Abstract + +ALEX aims to provide a fixed rate borrowing and lending service with pre-determined maturity in the world of decentralised finance (DeFi). We include forward contracts in our trading pool, with Automated Market Making (AMM) engine in association with generalised mean. While we formalise the trading practise swapping forward contracts with underlying asset, we incorporate the latest innovation in the industry - concentrated liquidity. Consequently, liquidity provider of ALEX can save decent amount of capital by making markets on a selected range of interest rate. + +## Introduction + +At ALEX, we build DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by [Stacks](https://www.stacks.co/). 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. + +## AMM and Invariant Function + +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. + +### Properties of AMM + +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 $$f(X)=L$$, where $$X=\left(x_1,x_2,\dots,x_d\right)$$ is $$d$$ dimension representing $$d$$ assets and $$L$$ is constant. When $$d=2$$, which is the common practise by a range of protocols, AMM $$f(x_1,x_2)=L$$ can be expressed as $$x_2=g(x_1)$$. Although it is not always true, $$g$$ tends to be twice differentiable and satisfies the following + +* monotonically decreasing, i.e. $$\frac{dg(x_1)}{dx_1}<0$$. This is because price is often defined as $$-\frac{dg(x_1)}{dx_1}$$. A decreasing function ensures price to be positive. +* convex, i.e. $$\frac{d^2g(x_1)}{dx_1^2} \geq 0$$. This is equivalent to say that $$-\frac{dg(x_1)}{dx_1}$$ is a non-increasing function of $$x_1$$. It is within the expectation of economic theory of demand and supply, as more reserve of $$x_1$$ means declining price. + +Meanwhile, $$f$$ can usually be interpreted as a form of mean, for example, [mStable](https://docs.mstable.org) relates to arithmetic mean, where $$x_1+x_2=L$$ (constant sum formula); one of the most popular platforms [Uniswap](https://uniswap.org/whitepaper-v3.pdf) relates to geometric mean, where $$x_1 x_2=L$$ (constant product formula); [Balancer](https://balancer.fi/whitepaper.pdf), which our [collateral rebalancing pool](../automated-market-making-of-collateral-rebalancing-pool.md) employs, applies weighted geometric mean. Its AMM is $$x_1^{w_1} x_2^{w_2}=L$$ where $$w_1$$ and $$w_2$$ are fixed weights. ALEX AMM extends these to create a generalised mean. + +### ALEX AMM + +After extensive research, we consider it possible for ALEX AMM to be connected to generalised mean defined as + +$$ +\left( \frac{1}{d} \sum _{i=1}^{d} x_i^p \right)^{\frac{1}{p}} +$$ + +where $$0 \leq p \leq 1$$. The expression might remind readers of $$p$$-norm when $$x_i \geq 0$$. It is however not true when $$p<1$$ as triangle inequality doesn't hold. + +When $$d=2$$ and $$p\neq 0$$ is fixed, the core component of generalised mean is assumed constant as below. + +$$ +\begin{split}x_1^p+x_2^p&=L\\ +-\frac{dx_2}{dx_1}&=\left(\frac{x_2}{x_1} \right)^{1-p}\end{split} +$$ + + + + + +This equation is regarded reasonable as AMM, because (i) function $$g$$ where $$x_2=g(x_1)$$ is monotonically decreasing and convex; and (ii) The boundary value of $$p=1$$ and $$p=0$$ corresponds to constant sum and constant product formula respectively. When $$p$$ increases from 0 to 1, price $$-\frac{dg(x_1)}{x_1}$$ gradually converges to 1, i.e. the curve converges from constant product to constant sum (see [Appendix 1](./#appendix-1-generalised-mean-when-d-2) for the relevant proofs). + +Though purely theoretical at this stage, [Appendix 2](./#appendix-2-liquidity-mapping-to-uniswap-v3) maps $$L$$ to the liquidity distribution of [Uniswap V3](https://uniswap.org/whitepaper-v3.pdf). This is motivated by an independent research from [Paradigm](https://www.paradigm.xyz/2021/06/uniswap-v3-the-universal-amm/). + +## Trading Formulae + +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 $$L$$ after each transaction, like _Uniswap V2_. + +### Out-Given-In + +In order to purchase $$\Delta y$$ amount of token Y from the pool, the buyer needs to deposit $$\Delta x$$ amount of token X. $$\Delta x$$ and $$\Delta y$$ satisfy the following + +$$ +(x+\Delta x)^{p}+(y-\Delta y)^{p}=x^{p}+y^{p} +$$ + +After each transaction, balance is updated as below: $$x\rightarrow x+\Delta x$$ and $$y\rightarrow y-\Delta y$$. Rearranging the formula results in + +$$ +\Delta y=y-\left[x^{p}+y^{p}-(x+\Delta x)^{p}\right]^{\frac{1}{p}} +$$ + +When transaction cost exists, the actual deposit to the pool is less than $$\Delta x$$. Assuming $$\lambda\Delta x$$ is the actual amount and $$(1-\lambda)\Delta x$$ is the fee, above can now be expressed as + +$$ +\begin{split} &(x+\lambda\Delta x)^{p}+(y-\Delta y)^{p}=x^{p}+y^{p}\\ &\Delta y=y-\left[x^{p}+y^{p}-(x+\lambda\Delta x)^{p}\right]^{\frac{1}{p}} \end{split} +$$ + +To keep $$L$$ constant, the updated balance is: $$x\rightarrow x+\lambda\Delta x$$ and $$y\rightarrow y-\Delta y$$. + +### In-Given-Out + +This is the opposite case to above. We are deriving $$\Delta x$$ from $$\Delta y$$. + +$$ +\Delta x=\frac{1}{\lambda}{\left[x^{p}+y^{p}-(y-\Delta y)^{p}\right]^{\frac{1}{p}}-x} +$$ + +### In-Given-Price + +Sometimes, trader would like to adjust the price, perhaps due to deviation of AMM price to the market value. Define $$p'$$ the AMM price after rebalancing the token X and token Y in the pool + +$$ +p'=\left(\frac{y-\Delta y}{x+\lambda\Delta x}\right)^{1-p} +$$ + +Then, the added amount of $$\Delta x$$ can be calculated from the formula below + +$$ +\begin{split} &(x+\lambda\Delta x)^{p}+(y-\Delta y)^{p}=x^{p}+y^{p}\\ &1+\left(\frac{y}{x}\right)^{p}=\left(1+\lambda\frac{\Delta x}{x}\right)^{p}+(\frac{y-\Delta y}{x})^{p}\\ &1+p^{\frac{p}{t}}=\left(1+\lambda\frac{\Delta x}{x}\right)^{p}+p'^{\frac{p}{t}}\left(1+\lambda\frac{\Delta x}{x}\right)^{p}\\ &\Delta x=\frac{x}{\lambda}\left[\left(\frac{1+p^{\frac{p}{t}}}{1+p'^{\frac{p}{t}}}\right)^{\frac{1}{p}}-1\right]\\ \end{split} +$$ + +## Appendix 1: Generalised Mean when d=2 + +ALEX's invariant function is $$f(x_{1},x_{2};p)=x{_1}^{p}+x_{2}^{p}=L.$$ It can be rearranged as $$x{2}=g(x_{1})=(L-x_{1}^{p})^{\frac{1}{p}}$$. $$x_{1}$$ and $$x_{2}$$ should both be positive meaning the liquidity pool contains both tokens. + +#### Theorem + +When $$0) + +Figure 3 plots $$L_{\text{Uniswap}}$$ against interest rate $$r$$ regarding various levels of $$t$$. When $$0) +![Figure 2](<../../.gitbook/assets/cectable2 (1).png>) In Figure 2, assume lower bound is 0%, whereas upper bound is 50%. We also set $$t$$= 0.5 and $$L$$= 20. If interest rate is 0%, $$L$$= 20 means holding equal amount of Token and ayToken of 100 each $$\left(100^{0.5}+100^{0.5}=20\right)$$. The figure compares actual holding of Token and ayToken with and without cap and floor. @@ -325,7 +325,7 @@ $$ \begin{split} &\frac{dy}{dr}=L^{\frac{1}{1-t}}\frac{e^{-(1-t)r}}{(1+e^{-(1-t)r})^{\frac{2-t}{1-t}}}\\ &L_{\text{Uniswap}}=\frac{2}{t}L^{\frac{1}{1-t}}\left(e^{\frac{r(1-t)}{2}}+e^{\frac{-r(1-t)}{2}}\right)^{\frac{-2+t}{1-t}}\\ &=\frac{2}{t}L^{\frac{1}{1-t}}\big\{2\cosh\left[\frac{r(1-t)}{2}\right]\big\}^{\frac{-2+t}{1-t}} \end{split} $$ -![Figure 3](<../.gitbook/assets/liquidity (2) (2) (2) (2) (2) (2) (2) (1).png>) +![Figure 3](<../../.gitbook/assets/liquidity (2) (2) (2) (2) (2) (2) (2) (1).png>) Figure 3 plots $$L_{\text{Uniswap}}$$ against interest rate $$r$$ regarding various levels of $$t$$. When $$0