TR kizaki Tech Memo

Understanding how pricing on bonding curve by UniSwap

If I were to trade tokens on Uniswap, e.g. from DIE to USDC

The amount of tokens that I would get in return is calculated by a simple equation times, *e.g. $y = K$. x

*I will write DAI as $x$ and USDC as $y$.

$K$ is invariant. (which in classical invariant theory was usually assumed to be the complex numbers).

However, if I were to do the same trade using curve then the equation is more complex.

I’m going to explain how the last equation is derived and in the process, you will have a better understanding of how pricing on curve is determine.


We’ll start with simple ideas and build our way up to this equation.

Imagine, we have an exchange where one DIE can be trade for one USDC. This can be expressed using $x + y =C$.

This is the graph of $x + y = C$. ($C$= constant)

$x$ tokens of assets. $y$ tokens of assets.

Let’s say we start out here with $50$ DIE and $50$ USDC. $ In this case, $x$ will equal $50$, $y$ will equal 50. So, our $C$ will equal $100$.

Now if I were to sell $20$ DIE for USDC since one DIE is trading for one USDC.

I put in $20$ DIE, then I get $20$ USDC and you can see that happening in this graph, we started out with $50$ and we added $20$ DIE, so the amount of USDC that needs to be inside here is $30$.

$50 - 30 = 20$, so we get back $20$ USDC.

Likewise, Let’s say that we start out $x$ $50$ (DIE) $y$ $50$ (USDC), and I sell $30$ on my USDC then the amount of DIE that needs to be inside the pool is $20$. $(50 - 20 = 30)$

So I get back $30$ DIE notice that in all cases the total amount of tokens remain the same $100$.

Initially we have $x$ $50$ $y$ $50$ , which is $100$. After our trade, we have $x$ $70$ $y$ $30$ which is $100$.

and after another trade, we have $x$ $80$ $y$ $20$ , which is $100$.

Image

That is what is expressed here in this equation it says that the total amount of token $x$ and token $y$ must always equal a constant.


However, in the real world, the price of DIE is not exactly equal to the price of USDC.

For example, one DIE might equal $1.01$ dollar, whereas one USDC might equal $99$ cent.

1 DIE = 1.01USD, 1 USDC = 0.99USD

In this case, what ends up happening is that everyone sells their USDC for DIE, because you can buy something that is worth $1.01$ dollar by paying only $99$ cent and you make a two cent profit.

We also want to use the equation that says the price of DIE is not exactly equal to the price of USDC. and we can do that by using the equation $(xy = k)$.

The graph will look something like this, Basically this graph says that the less token there is the more expensive it gets.

Image

Image

Now curve finance combines these two graphs the constant sum curve $x + y = C$ and the constant product curve $xy = K$, and you will get a curve that is flat in the middle and behaves more like UniSwap on the extreme ends.


Let’s try creating this curve. we will start with the condition that says x plus y equals a constant $D$ $(x + y = D)$ and we also use the constant product formula

$xy = \left( \dfrac{D}{2} \right)^2$.

Combining the two equations above we get the equation

$x + y + xy = D + \left( \dfrac{D}{2} \right)^2$ .

However, plotting this equation we get a graph that looks like this. It still looks like the curve of a Uniswap.

Image and not something like this where it is flat in the middle.

Image

We can accomplish $x+y=D$ by amplifying this part of the equation.

So, we multiply this part of the equation by a variable, we will name it $\chi$ (call it "$chi$" is variable, “sum invariant”).

we get this equation $$\chi (x+y) + xy = \chi D + (\dfrac{D}{2})^2$$

images

When the variable $\chi = 0$ , these two parts of the equation cancel out,

and we’re left with $xy =$ $\left( \dfrac{D}{2} \right)^2$.

This equation becomes a constant product curve.


On the other hand, when $\chi$ is a really big number, these two part of the equation becomes relatively small to $\chi(x + y)$ and $\chi D$ part of the equation. So, we are left with an equation that looks like a constant product curve.

images

you can see here how increasing $\chi$ makes the curve more and more flat.

When $\chi$ is big the curve for this equation flattens out. But this is also true when $(x + y)$ is big.

So, we want to express that when $\chi$ is big the curve is flat , and when $\chi$ is small it looks like the constant product curve. Regardless of how big or small $(x + y)$ is. and we can do that by taking these two part of the equation. $$\chi (x+y) = \chi D$$

and we normalize it by dividing $\dfrac{x + y}{D}$.

we can rewrite this equation $$D \chi (x+y) = \chi D^2 $$ and then we will update the equation $$\chi (x+y) + xy = \chi {D} + \left( \dfrac{D}{2} \right)^2$$

images

So, now our equation looks like this ↓

$$\Large D\chi(x+y)+xy =\chi D^2+\left(\dfrac{D}{2} \right)^2 $$

$$\Large \chi D^{n-1} \sum x_i+\Pi x_i = \chi D^n+ \left(\dfrac{D}{n} \right)^n $$

This is exactly the same equation that is mentioned in the curve white paper. Furthermore, In the curved whitepaper we take this equation, set $\chi$ equal to this equation.

*$A$ is a fixed constants.

$$\Large \chi=\dfrac{A \Pi x_i}{(D/n)^2} $$

and then, Finally we arrive at this equation. Multi token Curve Pools,

$$\Large An\sum x_i + D = ADn^n+\dfrac{D^{n+1}}{n^n \Pi x_i} $$


Doing the same things to the equation, that we derived $$\Large \chi = \dfrac{Axy}{(D/2)^2} $$ we set $\chi$ equal to this variable.

and the equation above this, $$\Large D \dfrac{Axy}{(D/2)^2}(x + y)+ xy = \dfrac{Axy}{(D/2)^2}D^2 + \left( \dfrac{D}{2} \right)^2 $$

this equation can be reduced, $$\Large \left( D\dfrac{Axy}{(D/2)^2}(x + y)+ xy \right) = \dfrac{D}{xy} \left( \dfrac{Axy}{(D/2)^2}D^2+ \left(\dfrac{D}{2} \right)^2 \right) $$

and it becomes this, $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$


Comparing the two equations the one that we derived, and the one mentioned in the curved white paper.

you can see that these two equations are exactly same.

$$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

$$\Large An\sum x_i + D = ADn^n+\dfrac{D^{n+1}}{n^n \Pi x_i} $$

Now let’s summarize how we got here, $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

we start off with some constrains, $x + y = D$,

and, $xy = \left( \dfrac{D}{2} \right)^2$

and then we said we wanted to amplify the effect of $x + y = D$.

So, we multiply by $\chi$, and we also wanted to normalize $x + y$ by $D$, we multiply $D$ on both sides of the equation.

As mentioned in the curved white paper, we set $\chi$ equal to this variable.

$$\Large \chi = \dfrac{Axy}{(D/2)^2} $$

and we get this equation. $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

Graphing this equation, you will get the curve used in curve finance.

images10

when $A = 0$,

you will get the constant product curve $xy = K$,

and when $A = ∞$,

you will get constants sum curve $x + y = C$.


appendix:

Add Token → Calculate $D$ by Vyper

X is deposited, apply fees (Uniswap is 0.04%)


@private
@constant
def get_D(xp: uint256[N_COINS]) -> uint256:
    S: uint256 = 0
    for _x in xp:
        S += _x
    if S == 0:
        return 0

    Dprev: uint256 = 0
    D: uint256 = S
    Ann: uint256 = self.A * N_COINS
    for _i in range(255):
        D_P: uint256 = D
        for _x in xp:
            D_P = D_P * D / (_x * N_COINS + 1)  # +1 is to prevent /0
        Dprev = D
        D = (Ann * S + D_P * N_COINS) * D / ((Ann - 1) * D + (N_COINS + 1) * D_P)
        # Equality with the precision of 1
        if D > Dprev:
            if D - Dprev <= 1:
                break
        else:
            if Dprev - D <= 1:
                break
    return D

UniSwapによるボンディングカーブの価格設定の理解

日付: "2022-10-16T22:12:03.284Z"

説明: "DeFiは破滅しない。"

もし私がUniswapでトークンを交換する場合、例えばDIEからUSDCへと

返ってくるトークンの量は、単純な方程式によって計算されます。例えば、$y = K$。 $x$

DIEを$x$とし、USDCを$y$と書きます。

$K$は不変です。(古典的な不変理論では、通常複素数が仮定されます)。

しかし、もし同じ取引をCurveを使用して行う場合、方程式はもっと複雑になります。

この最後の方程式がどのように導出されるかを説明し、そのプロセスを通じて、Curve上での価格設定がどのように決定されるかをより良く理解することができます。

単純なアイデアから始めて、この方程式に至るまでを構築していきます。

交換所があり、1DIEを1USDCと交換できると想像してみてください。これは$x + y = C$を使用して表現できます。

これが$x + y = C$のグラフです。($C$= 定数)

$x$ トークンの資産。$y$ トークンの資産。

ここで、$50$ DIE と $50$ USDCで始めるとしましょう。 $ この場合、$x$は$50$に等しく、$y$は50に等しくなります。したがって、私たちの$C$は$100$になります。

今、もし$20$ DIEをUSDCで売る場合、1DIEは1USDCと交換されているので、

$20$ DIEを入れると、$20$ USDCを取得し、このグラフでそれが起こっているのを見ることができます。$50$から始まり、$20$ DIEを追加したので、ここに必要なUSDCの量は$30$です。

$50 - 30 = 20$なので、$20$ USDCを取り戻します。

最初に、私たちは $x$ 50 (DIE) と $y$ 50 (USDC) を持っています。これは合計で $100$ です。私が USDC を $30$ 売った場合、プール内に必要な DIE の量は $20$ になります。$(50 - 20 = 30)$

その結果、$30$ DIE を取り戻します。全ての場合でトークンの合計数は同じ $100$ を保ちます。

最初に $x$ 50、$y$ 50 で、合計 $100$ です。取引後、$x$ 70、$y$ 30 で、合計 $100$ です。

さらに別の取引後、$x$ 80、$y$ 20 で、これは $100$ です。

ここで表されているこの方程式は、トークン$x$とトークン$y$の総量が常に一定でなければならないことを表しています。

しかし、現実世界では、DIEの価格はUSDCの価格と等しくありません。

例えば、1 DIE が $1.01 の価値がある一方で、1 USDC が $0.99 の価値があるかもしれません。

1 DIE = 1.01USD, 1 USDC = 0.99USD

この場合、何が起こるかというと、全員が USDC を DIE に売り払うことになります。なぜなら、$0.99 で $1.01 の価値があるものを購入でき、2セントの利益を得ることができるからです。

DIE の価格が USDC の価格と正確に等しくないという方程式を使用したいと考えています。それは $(xy = k)$ を使用して行うことができます。

グラフはこのような形になります。基本的にこのグラフは、トークンが少なくなるほど価格が高くなることを示しています。

このカーブを作成してみましょう。 $x + y = D$ という条件から始め、定数積の公式

$xy = \left( \dfrac{D}{2} \right)^2$ を使用します。

上記の2つの方程式を組み合わせると、

$x + y + xy = D + \left( \dfrac{D}{2} \right)^2$ という方程式が得られます。

しかし、この方程式をプロットすると、次のようなグラフが得られます。これはまだ Uniswap のカーブに似ています。

そして、中央が平坦ではないものになります。

この部分の方程式を増幅させることで $x+y=D$ を実現できます。

したがって、方程式のこの部分に変数を乗じることにします。 この変数を $\chi$("chi" と呼びます、"和不変量")と名付けることにします。

この方程式を得るには $$\chi (x+y) + xy = \chi D + (\dfrac{D}{2})^2$$

変数 $\chi = 0$ のとき、方程式のこれら2部分は打ち消し合い、

$xy = \left( \dfrac{D}{2} \right)^2$ の定数積カーブになります。 一方、$\chi$ が非常に大きな数値の場合、方程式のこれらの部分は $\chi(x + y)$ および $\chi D$ の部分に比べて相対的に小さくなります。 したがって、定数和カーブのような方程式になります。

$\chi$ を増やすと、カーブがどんどん平坦になるのが見て取れます。

$\chi$ が大きいとき、この方程式のカーブは平坦になります。しかし、$(x + y)$ が大きいときも同様です。

したがって、$\chi$ が大きいときはカーブが平坦であり、$\chi$ が小さいときは定数積カーブのように見えることを表現したいです。$(x + y)$ の大きさに関係なく、これを達成するには、方程式のこれら2部分を $$\chi (x+y) = \chi D$$

で正規化し、$\dfrac{x + y}{D}$ で割ります。

この方程式を書き直すと $$D \chi (x+y) = \chi D^2 $$ そして、方程式を更新します。

$$\chi (x+y) + xy = \chi {D} + \left( \dfrac{D}{2} \right)^2$$

このようにして、私たちの方程式は次のようになります。 $$\Large D\chi(x+y)+xy =\chi D^2+\left(\dfrac{D}{2} \right)^2 $$

$$\Large \chi D^{n-1} \sum x_i+\Pi x_i = \chi D^n+ \left(\dfrac{D}{n} \right)^n $$

これはCurveのホワイトペーパーに記載されている方程式と全く同じです。 さらに、Curveのホワイトペーパーでは、この方程式を取り、$\chi$ をこの方程式に等しいと設定します。

*$A$ は固定定数です。 $$\Large \chi=\dfrac{A \Pi x_i}{(D/n)^2} $$

そして、最終的にこの方程式に到達します。 マルチトークンCurveプール $$\Large An\sum x_i + D = ADn^n+\dfrac{D^{n+1}}{n^n \Pi x_i} $$


方程式を導出したのと同じことを行うと、 $$\Large \chi = \dfrac{Axy}{(D/2)^2} $$ $\chi$ をこの変数に等しいと設定します。

そして、上記の方程式、 $$\Large D \dfrac{Axy}{(D/2)^2}(x + y)+ xy = \dfrac{Axy}{(D/2)^2}D^2 + \left( \dfrac{D}{2} \right)^2 $$

この方程式は簡素化でき、 $$\Large \left( D\dfrac{Axy}{(D/2)^2}(x + y)+ xy \right) = \dfrac{D}{xy} \left( \dfrac{Axy}{(D/2)^2}D^2+ \left(\dfrac{D}{2} \right)^2 \right) $$

そして、このようにになります。 $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

導出した方程式とCurveのホワイトペーパーに記載されている方程式を比較すると、

これら2つの方程式が全く同じであることがわかります。

$$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

$$\Large An\sum x_i + D = ADn^n+\dfrac{D^{n+1}}{n^n \Pi x_i} $$

ここまでの要約です。 $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$

いくつかの制約から始め、$x + y = D$、

そして、$xy = \left( \dfrac{D}{2} \right)^2$ そして、$x + y = D$の効果を増幅しました。

したがって、$\chi$ を乗じ、$x + y$ を $D$ で正規化したいと考え、方程式の両側に $D$ を乗じます。

Curveのホワイトペーパーに記載されているように、$\chi$ をこの変数に等しいと設定しました。

$$\Large \chi = \dfrac{Axy}{(D/2)^2} $$

そして、この方程式を得ました。 $$\Large A2^2(x + y)+ D = AD2^2+ \left(\dfrac{D}{2} \right)^2 \dfrac{D}{xy} $$ この方程式をグラフにすると、Curve Financeで使用されるカーブが得られます。

$A = 0$ のとき、

定数積カーブ $xy = K$ を得ます。

そして、$A = ∞$ のとき、

定数和カーブ $x + y = C$ を得ます。