This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Silkswap: An asymmetric automated market maker model for stablecoins

Nicola Cantarutti111Corresponding author email: nicolacantarutti@gmail.com , Alex Harker, Carter Woetzel
shadeprotocol.io
Abstract

Silkswap is an automated market maker model designed for efficient stablecoin trading with minimal price impact. The original purpose of Silkswap is to facilitate the trading of fiat pegged stablecoins with the stablecoin Silk, but it can be applied to any pair of stablecoins. The Silkswap invariant is a hybrid function that generates an asymmetric price impact curve. We present the derivation of the Silkswap model and its mathematical properties. We also compare different numerical methods used to solve the invariant equation. Finally, we compare our model with the well known Curve Finance model.

Keywords: AMM, DEX, stablecoin, hybrid function model, low price impact.

1 Introduction

Decentralized EXchanges (DEXs) are currently the most popular application of Decentralized Finance (DeFi). Unlike centralized exchanges, DEXs are not based on a single centralized entity acting as custodians or intermediaries. Instead, on DEXs traders retain full control of their funds and private keys, and smart contracts execute trades for users in a neutral and automated fashion. Most DEXs use Automated Market Maker (AMM) models to define the rules of trading, rather than relying on the order book model. Liquidity providers can deposit their tokens into liquidity pools in exchange for rewards coming from swap fees and token farming. The AMM algorithmically computes the price of the tokens inside a liquidity pool only based on the token balance. A technical introduction on the mechanics of AMMs can be found in [Angeris and Chitra, 2020] and [Mohan, 2022].

One of the first AMMs to appear in DeFi is Uniswap [Adams, 2018], that together with its upgraded version, Uniswap v2 [Adams et al., 2020], is based on the Constant Product Market Maker (CPMM) model. This model assumes that the product of the quantities of tokens in a liquidity pool is constant, which guarantees infinite liquidity inside the pool. CPMM works quite well for volatile tokens, as it promptly adapts the price in response to new trades. Nevertheless, it is not very suitable when trading low volatility or stable assets.

Another important AMM model is the Constant Sum Market Maker (CSMM), that assumes that the sum of the quantities of tokens inside the pool is constant. The advantage of this model is that any trade has zero price impact, however it also has the inconvenience that it permits the complete drain of the entire liquidity inside the pool. For this reason, it is not used in practical applications.

Low price impact is a good property when swapping between fiat pegged stablecoins. Curve Finance [Egorov, 2019], formerly Stableswap, and the new version Curve v2 [Egorov, 2021] implement a Hybrid Function Market Maker (HFMM) model with the exact purpose of facilitating swaps between stablecoins. This model combines CPMM and CSMM together in order to take advantage of both the infinite liquidity property of CPMM and the zero price impact property of CSMM.

The main goal of the Silkswap model is to facilitate the trades of Silk with other fiat pegged stablecoins. However this model can be used to trade any pair of stablecoins. In the current version, it is designed only for two-asset liquidity pools. Silk [Duniya, 2021] is a privacy-preserving overcollateralized stablecoin developed by Shade Protocol and native to the Secret Network blockchain [Woetzel, 2020]. The main feature of Silk is that it is a stablecoin pegged to a basket of global currencies and commodities using decentralized price feeds, creating a digital currency that serves as a hedge against single currency volatility. We developed Silkswap as an HFMM model inspired by the Curve Finance v2 model. Additionally we introduced more flexibility in the shape of the invariant function, allowing for an asymmetric price impact curve. In this way, it is possible to discourage possible imbalances within the liquidity pool.
In the next sections we derive the Silkswap invariant and show its mathematical properties. We present numerical results for three different zero-finder algorithms and finally we compare our model with the Curve finance model.

2 Silkswap model

Let us consider a liquidity pool containing only two stablecoins. The two tokens in focus are token XX, that represents any fiat pegged stablecoin, and token YY that represents Silk. We use lower-case letters xx and yy to indicate the quantities of XX and YY. Without loss of generality, let us quote the price of XX and YY in US dollars, although any other fiat currency would work. Let us introduce the conversion factors pXp_{X} and pYp_{Y}, such that the values in dollars of xx and yy are simply pX×xp_{X}\times x and pY×yp_{Y}\times y respectively. The conversion factor pXp_{X} has units [USD]/[X] and pYp_{Y} has units [USD]/[Y]. We also introduce p:=pYpXp:=\frac{p_{Y}}{p_{X}}, the conversion factor between YY and XX, which has units [X]/[Y].
The conversion factors pXp_{X} and pYp_{Y} represent the market prices of one unit of XX and YY respectively, while pp is the price of one unit of YY in terms of XX. These values must be provided by an oracle, which is an external source of information that is fed into the AMM smart contract with a certain frequency.

Example:
Let us consider a liquidity pool containing USDC (token XX) and Silk (token YY). If the oracle price of Silk is 1.05$ then pY=1.05USD/SILKp_{Y}=1.05\,\text{USD/SILK}, while for USDC the oracle price is exactly 1$, resulting in a perfect peg, then we have pX=1USD/USDCp_{X}=1\,\text{USD/USDC}. The direct conversion between Silk and USDC is therefore p=pYpX=1.05USDC/SILKp=\frac{p_{Y}}{p_{X}}=1.05\,\text{USDC/SILK}. Later we will often identify XX with USDC and YY with SILK to make the discussion clearer.

Let us define the equilibrium point in a liquidity pool as the point (x,y)(x,y) where the dollar value of xx equals the dollar value of yy. The equilibrium point satisfies the equilibrium equation

pYy=pXx.p_{Y}\,y\,=\,p_{X}\,x. (1)

From now on, we will assume that XX is our numeraire, and we will express the value of YY in terms of XX. The equilibrium equation becomes

py=x.p\,y=x. (2)

This choice is due to the fact that it is more natural for the user to evaluate an asset in terms of a fiat pegged stablecoin. Because Silk is pegged to a basket, its value expressed in terms of any fiat currency is not stable, but it fluctuates with a very low volatility.

2.1 Silkswap invariant

Refer to caption
Refer to caption
Figure 1: Graph of the CPMM and CSMM models with D=2000D=2000. On the left p=1p=1. On the right p=2p=2.

Under the CPMM model, the quantities xx and yy must satisfy the following equation:

xpy=Kx\,p\,y=K (3)

with K>0K>0 constant. The equilibrium point, satisfying both (2) and (3), is (x,y)=(K,Kp)(x,y)=(\sqrt{K},\frac{\sqrt{K}}{p}). Under the CSMM model instead, the following equation holds:

x+py=Dx\,+\,py=D (4)

with D>0D>0 constant. The equilibrium point, satisfying (2) and (4), is (x,y)=(D2,D2p)(x,y)=(\frac{D}{2},\frac{D}{2p}). The equilibrium point is a fundamental point where the ratio of the quantities of tokens in the pool is equal to pp, and any invariant curve should contain this point. Since this point is unique, it follows that K=D24K=\frac{D^{2}}{4}. In figure 1 we can see the graph of these two models, with different values of pp.

Refer to caption
Refer to caption
Figure 2: Graph of the Silkswap invariant. On the A=10A=10. On the right A=400A=400. We can see that the parameter AA indicates the closeness of the Silkswap curve to the CSMM line.
Refer to caption
Refer to caption
Figure 3: Graph of the Silkswap invariant. We inverted the values of γ1\gamma_{1} and γ2\gamma_{2} to show how these parameters can control the asymmetry of the curve.

Now let us consider the following linear combination of the CSMM and CPMM models. Let us multiply Eq. (4) by χAD\chi AD, and sum it with eq. (3):

(χAD)(x+py)+xpy=(χAD)D+D24(\chi AD)\,(x+py)+xpy\,=\,(\chi AD)D+\frac{D^{2}}{4} (5)

where χ\chi is a function of xx and yy, defined as

χ:=(4xpyD2)γ\chi:=\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma} (6)

and

γ:={γ1,if xpyγ2,if x>py\gamma:=\begin{cases}\gamma_{1},&\mbox{if }x\leq py\\ \gamma_{2},&\mbox{if }x>py\end{cases} (7)

and the other parameters are constants satisfying A>0A>0, γ10\gamma_{1}\geq 0, γ20\gamma_{2}\geq 0. Let us remark that χ\chi is an adimensional quantity and DD has the same dimension of xx.
We call the equation (5), together with (6) and (7), the Silkswap invariant.
The graph of the Silkswap invariant is shown in figures 3 and 3 under different set of parameters. We can see that the CPMM hyperbola is always greater than the CSMM line, and they touch each other at the equilibrium point. The Silkswap invariant graph lies in between them. In the Appendix we prove these facts in the theorems (A.1) and (A.3). The parameter AA tells us how close the Silkswap graph is to the CPMM or to the CSMM. From Eq. (5) we can easily see that for A0A\to 0 the invariant converges to the CPMM curve, while for AA\to\infty it converges to the CSMM curve.222The parameter AA has the same meaning of the parameter AA in the Stableswap model [Egorov, 2019].
The two dimensional function χ\chi in (6) is a discontinuous function. We show the surface plot in Fig. (4). In the Appendix we prove that, although the Silkswap invariant contains a discontinuous function, the points of our interest are regular points where the invariant is continuously differentiable.

Refer to caption
Figure 4: Function χ\chi calculated with D=2000D=2000, p=1p=1, γ1=2\gamma_{1}=2, γ2=8\gamma_{2}=8. The equilibrium point is at (x,y)=(1000,1000)(x,y)=(1000,1000) and χ(x,y)=1\chi(x,y)=1. The function χ\chi is continuous at this point, but not differentiable.

2.2 Pricing with the Silkswap invariant

In order to compute the price of a token, it is convenient to express the invariant in the explicit form y=f(x)y=f(x), where f:>0>0f:\mathbb{R}_{>0}\to\mathbb{R}_{>0} is continuously differentiable, decreasing and convex 333The set >0\mathbb{R}_{>0} is the set {x|x>0}\{x\in\mathbb{R}|x>0\}.. For the CPMM the explicit form is the hyperbola f(x)=Kpxf(x)=\frac{K}{px}, while for the CSMM the explicit form is the straight line f(x)=1p(x+D)f(x)=\frac{1}{p}(-x+D), defined for 0xD0\leq x\leq D.

If we trade an infinitesimal quantity dydy, using the first order Taylor approximation dydf(x)dxdxdy\approx\frac{df(x)}{dx}dx, it results equal to trade the quantity df(x)dxdx\frac{df(x)}{dx}dx. Let us recall that by definition df(x)dx<0\frac{df(x)}{dx}<0, so we need to use the absolute value to define a positive price. We define the current price of the token XX as function of xx

PX(x):=|df(x)dx|=|dydx|.P_{X}(x)\,:=\,\biggl{|}\frac{df(x)}{dx}\biggr{|}\,=\,\biggl{|}\frac{dy}{dx}\biggr{|}. (8)

The price of the token YY is defined as:

PY(x):=|dxdy|=1PX.P_{Y}(x)\,:=\,\biggl{|}\frac{dx}{dy}\biggr{|}=\frac{1}{P_{X}}. (9)

Under the CPMM model, the current price is PY(x)=px2KP_{Y}(x)=\frac{px^{2}}{K}, and at equilibrium PY(K)=pP_{Y}(\sqrt{K})=p. Under the CSMM model, the current price is a constant value, PY(x)=pP_{Y}(x)=p, for any 0<x<D0<x<D.

Refer to caption
Refer to caption
Figure 5: Graph of the price of the token YY as function of xx, obtained from the Silkswap invariant. Same parameters used in Fig (3). The vertical lines at 500 and 1500 only serve to better understand the asymmetry in the graph.

Unfortunately the Silkswap invariant cannot be written in an explicit form. We can define the function F:>02>0F:\mathbb{R}_{>0}^{2}\to\mathbb{R}_{>0} as

F(x,y):=AD(4xpyD2)γ(x+pyD)+xpyD24F(x,y)\,:=\,AD\,\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma}\,(x+py-D)+xpy-\frac{D^{2}}{4} (10)

with γ\gamma as in (7). The Silkswap invariant (5) is the set of points that satisfy

F(x,y)=0.F(x,y)=0. (11)

The partial derivatives are:

Fx=\displaystyle\frac{\partial F}{\partial x}= AD(4xpyD2)γ[1+γx+pyDx]+py\displaystyle\;AD\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma}\biggl{[}1+\gamma\,\frac{x+py-D}{x}\biggr{]}+py (12)
Fy=\displaystyle\frac{\partial F}{\partial y}= AD(4xpyD2)γ[p+γx+pyDy]+px.\displaystyle\;AD\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma}\biggl{[}p+\gamma\,\frac{x+py-D}{y}\biggr{]}+px. (13)

Thanks to the Theorem (A.6), we know the expression of the slope of the Silkswap invariant, which can be used to compute the prices of the tokens by (8) and (9). The slope has the following expression:

dydx=FxFy.\frac{dy}{dx}=-\frac{\frac{\partial F}{\partial x}}{\frac{\partial F}{\partial y}}\,. (14)

In Figure 5 we present two examples of price curves obtained from this expression.

2.3 Scaled invariant

Refer to caption
Figure 6: Scaled function F~(x~,z~)\tilde{F}(\tilde{x},\tilde{z}), with, γ1=2\gamma_{1}=2, γ2=8\gamma_{2}=8. The equilibrium point is at (x~,z~)=(0.5,0.5)(\tilde{x},\tilde{z})=(0.5,0.5) and the function is continuously differentiable in this point. The solid line represents the zero level of the invariant.

If we consider the function (10) depending also on DD, for any constant c>0c>0 we have

F(x,y,D)=0F(cx,cy,cD)=0.F(x,y,D)=0\quad\Longrightarrow\quad F(cx,cy,cD)=0. (15)

This means that the Silkswap invariant is also invariant by scaling.
This property is very useful in practice because it reduces the convergence time of some numerical methods and prevents possible overflows when the variables have very high magnitudes.
Let us introduce the variable z:=pyz:=py. We can consider F(cx,cy,cD)F(cx,cy,cD) with c=1Dc=\frac{1}{D}, equal to F(xD,yD,1)F(\frac{x}{D},\frac{y}{D},1), and define the corresponding scaled function:

F~(x~,z~):=A(4x~z~)γ(x~+z~1)+x~z~14.\tilde{F}(\tilde{x},\tilde{z})\,:=\,A\,\bigl{(}4\tilde{x}\tilde{z}\bigr{)}^{\gamma}\,(\tilde{x}+\tilde{z}-1)+\tilde{x}\tilde{z}-\frac{1}{4}. (16)

where x~=xD\tilde{x}=\frac{x}{D}, and z~=zD\tilde{z}=\frac{z}{D}. We show in Fig. 6 the graph of this function. The scaled Silkswap invariant is given by

F~(x~,z~)= 0.\tilde{F}(\tilde{x},\tilde{z})\,=\,0. (17)

The function (16) is related with (10) by

F~(x~,z~)=1D2F(x,y).\tilde{F}(\tilde{x},\tilde{z})=\frac{1}{D^{2}}F(x,y). (18)

Using the chain rule

F~(x~,z~)x~=F~(x~,z~)xdxdx~=1DF(x,y)x\displaystyle\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial\tilde{x}}\,=\,\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial x}\,\frac{dx}{d\tilde{x}}\,=\,\frac{1}{D}\frac{\partial F(x,y)}{\partial x} (19)
F~(x~,z~)z~=F~(x~,z~)ydydz~=1pDF(x,y)y,\displaystyle\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial\tilde{z}}\,=\,\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial y}\,\frac{dy}{d\tilde{z}}\,=\,\frac{1}{pD}\frac{\partial F(x,y)}{\partial y}, (20)

we can rewrite the price equation (14) as

dydx=1pF~(x~,z~)x~F~(x~,z~)z~.\frac{dy}{dx}=-\frac{1}{p}\;\frac{\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial\tilde{x}}}{\frac{\partial\tilde{F}(\tilde{x},\tilde{z})}{\partial\tilde{z}}}\,. (21)

In the numerical calculation of the swap amount, we will make use of the scaled function (16), rather than (10), because it reduces a lot the number of operations, and consequently the run time and gas fees.

3 Numerical implementation

3.1 Calculation of D

Refer to caption
Refer to caption
Figure 7: LEFT: Invariant as function of DD. We used x=1900x=1900, y=100y=100, p=2p=2, A=30A=30, γ1=8\gamma_{1}=8, γ2=2\gamma_{2}=2.
RIGHT: Scaled invariant as function of x~\tilde{x}. We used y=600y=600, D=2000D=2000, p=1p=1, A=5A=5, γ1=2\gamma_{1}=2, γ2=3\gamma_{2}=3. The function has only one zero.
Refer to caption
Refer to caption
Figure 8: LEFT: Scaled invariant as function of x~\tilde{x}. We used y=600y=600, D=2000D=2000, p=1p=1, A=5A=5, γ1=3\gamma_{1}=3, γ2=2\gamma_{2}=2. The graph shows two zeros. The Halley and Newton methods are highly dependent on the initial guess x~0\tilde{x}_{0}.
RIGHT: Scaled invariant as function of x~\tilde{x}. We used y=200y=200, D=2000D=2000, p=1p=1, A=5A=5, γ1=3\gamma_{1}=3, γ2=4\gamma_{2}=4. The function is increasing for x~>0\tilde{x}>0, therefore the Halley and Newton methods converge to the right solution.

Since this model is defined by an implicit equation, we need numerical methods to compute the variables of interest. If the amounts of tokens in the pool satisfy the equilibrium equation 2, then the parameter DD can be quickly computed from Eq. (4). However, it is quite rare that a liquidity pool is in perfect equilibrium and the equation 2 is almost never satisfied. In this cases we need to compute DD from Eq. (5) using a numerical method. Let us define F(D)F(D) as the function (10) when we consider DD variable and xx, yy fixed. With an abuse of notation we can call it ”invariant function dependent of DD”, but let us recall that the name invariant can be used only when F(D)=0F(D)=0. In Fig 8, we can see that F(D)F(D) is a smooth decreasing function. We compare three different numerical methods: the Newton method, Halley method and the bisection method, see Table 1.

Pool with 2000 USDC and 1000 SILK
Starting point Method Iterations Time
2AM Newton 4 312.8µs
2GM Newton 8 770.4µs
2AM Halley 2 390.5µs
2GM Halley 5 820.4µs
/ Bisection 61 2.37ms
Pool with 200000 USDC and 100000 SILK
Starting point Newton Halley Bisection
2AM Newton 4 290.0µs
2GM Newton 7 790.3µs
2AM Halley 3 399.5µs
2GM Halley 5 914.2µs
/ Bisection 67 2.6ms
Table 1: Performance tables for the calculation of the parameter DD. Newton and Halley methods are computed using two different initial guesses: 2AM2\,AM and 2GM2\,GM. The bisection method is computed inside the interval [[2GM, 2AM]], see Theorem A.4. The parameters of the AMM are p=1p=1, A=100A=100, γ1=γ2=8\gamma_{1}=\gamma_{2}=8. We set a very small tolerance ϵD=1016\epsilon_{D}=10^{-16}.

The idea of using two times arithmetic mean (AM) and geometric mean (GM) as starting points for Newton and Halley methods comes from Theorem A.4. We can see that 2AM is a better choice. Although Newton method has more loop iterations than Halley, it performs better in terms of time. The reason is that the calculation of the second derivative is more expensive, in terms of operations, than the extra iterations. The expression of the derivatives are in Appendix B. Bisection method, as expected from theoretical results, is the slowest.

Pool with 10310^{3} USDC and 10310^{3} SILK
Swap size Method Iterations Time
0 Bisect 0 153µs
0.10.1 Bisect 52 1.32ms
11 Bisect 52 1.31ms
1010 Bisect 52 1.30ms
10210^{2} Bisect 52 1.37ms
10310^{3} Bisect 52 1.31ms
10410^{4} Bisect 52 1.34ms
10510^{5} Bisect 52 1.47ms
10610^{6} Bisect 52 1.46ms
0 Halley 0 190µs
0.10.1 Halley 2 311µs
11 Halley 2 303µs
1010 Halley 3 366µs
10210^{2} Halley 4 436µs
10310^{3} Halley 9 751µs
10410^{4} Halley 17 1.30ms
10510^{5} Halley 26 1.95ms
10610^{6} Halley 37 2.88ms
0 Newton 0 159µs
0.10.1 Newton 3 262µs
11 Newton 3 246µs
1010 Newton 4 285µs
10210^{2} Newton 6 358µs
10310^{3} Newton 16 674µs
10410^{4} Newton 30 1.20ms
10510^{5} Newton 49 2.01ms
10610^{6} Newton 68 2.80ms
Pool with 10610^{6} USDC and 10610^{6} SILK
Swap size Method Iterations Time
0 Bisect 0 141µs
0.10.1 Bisect 52 1.51ms
11 Bisect 52 1.45ms
1010 Bisect 52 1.62ms
10210^{2} Bisect 52 1.46ms
10310^{3} Bisect 52 1.48ms
10410^{4} Bisect 52 1.46ms
10510^{5} Bisect 52 1.57ms
10610^{6} Bisect 52 1.47ms
0 Halley 0 150µs
0.10.1 Halley 1 270µs
11 Halley 1 269µs
1010 Halley 2 346µs
10210^{2} Halley 2 341µs
10310^{3} Halley 2 341µs
10410^{4} Halley 3 422µs
10510^{5} Halley 4 488µs
10610^{6} Halley 9 885µs
0 Newton 0 155µs
0.10.1 Newton 2 236µs
11 Newton 2 239µs
1010 Newton 2 247µs
10210^{2} Newton 3 297µs
10310^{3} Newton 3 284µs
10410^{4} Newton 4 316µs
10510^{5} Newton 6 394µs
10610^{6} Newton 16 1.04ms
Table 2: Performance tables. We compare swap times where a trader swaps SILK for USDC. We consider different sizes and different numerical methods. The initial guess for the Halley and Newton methods is x~0\tilde{x}_{0} i.e. the scaled amount of USDC before the swap. The parameters of the AMM are p=1p=1, A=100A=100, γ1=γ2=8\gamma_{1}=\gamma_{2}=8. We set a very small tolerance ϵx=1016\epsilon_{x}=10^{-16}.

3.2 Calculation of the swap amount

We will also make use of numerical methods to calculate the amount of tokens that are returned during a swap. When a quantity Δy\Delta y is introduced into the pool, we need to compute the quantity Δx\Delta x that is extracted from the pool, and such that the point (xΔx,y+Δy)(x-\Delta x,y+\Delta y) belongs to the Silkswap invariant. The parameter DD is fixed, and it is irrelevant for this calculation, therefore in order to simplify the problem, we can use the scaled function (16). Let us define F~(x~|y~)\tilde{F}(\tilde{x}|\tilde{y}) and F~(y~|x~)\tilde{F}(\tilde{y}|\tilde{x}) the function with y~\tilde{y} or x~\tilde{x} fixed respectively. Again, with an abuse of notation we can call these functions ”invariant functions depending on x~\tilde{x} or y~\tilde{y}”. In the following we consider the case when Δx\Delta x is extracted and therefore we need to find the zero of F~(x~|y~)\tilde{F}(\tilde{x}|\tilde{y}), but the same analysis works for F~(y~|x~)\tilde{F}(\tilde{y}|\tilde{x}). In the Figures 8 and 8, we can see that the function is discontinuous and can have different shapes depending on the choice of the parameters. So the numerical convergence to the right solution is not always guaranteed.
By definition, the parameter γ\gamma can be any non-negative real number. In practice we restrict γ\gamma to be a positive integer value in order to simplify the model. The value of γ1\gamma_{1} is particularly important because it controls the behavior of the left tail:

limx~F~(x~|y~) for even γ1\displaystyle\lim_{\tilde{x}\to-\infty}\tilde{F}(\tilde{x}|\tilde{y})\to-\infty\quad\text{ for even }\gamma_{1}
limx~F~(x~|y~)+ for odd γ1,\displaystyle\lim_{\tilde{x}\to-\infty}\tilde{F}(\tilde{x}|\tilde{y})\to+\infty\quad\text{ for odd }\gamma_{1},

as we can see if we compare the plot of F~(x~|y~)\tilde{F}(\tilde{x}|\tilde{y}) in Fig. 8 with the plots in Fig. 8. When γ1\gamma_{1} is odd, F~(x~|y~)\tilde{F}(\tilde{x}|\tilde{y}) has two zeros. Since x~>0\tilde{x}>0 by definition, we know that the correct solution must be positive. However, numerical methods such as Newton and Halley can converge to the wrong solution if initialized with an unlucky starting point, see Fig. 8 (LEFT). Under some set of parameters, it is possible that the function F~(x~|y~)\tilde{F}(\tilde{x}|\tilde{y}) is increasing for x~>0\tilde{x}>0, and the Newton method works fine, see Fig. 8 (RIGHT). However, to avoid this uncertainty, in these cases it is better to use numerical methods that always guarantee the convergence to the right solution, such as the bisection algorithm. If we call (x~0,y~0)(\tilde{x}_{0},\tilde{y}_{0}) the amounts in the pool before the swap, and (x~1,y~1)(\tilde{x}_{1},\tilde{y}_{1}) the amounts after the swap such that F~(x~1|y~1)=0\tilde{F}(\tilde{x}_{1}|\tilde{y}_{1})=0, then we know that x~1[0,x~0]\tilde{x}_{1}\in[0,\tilde{x}_{0}]. The swap amount is therefore Δx=D(x~0x~1)\Delta x=D(\tilde{x}_{0}-\tilde{x}_{1}).
In practical applications we want to take advantage of the speed of the Newton method, and therefore we choose to use only odd values for γ1\gamma_{1}. For comparisons between the three numerical methods under consideration, see Table 2. Let us comment a few points:

  1. 1.

    The number of iterations of the bisection method does not depend on the size of the pool. The reason is that we are searching in the interval [0,x~0][0,\tilde{x}_{0}], and x~0=xD\tilde{x}_{0}=\frac{x}{D}. In the example, since we are at equilibrium D=2xD=2x and therefore x~0=12\tilde{x}_{0}=\frac{1}{2}.

  2. 2.

    The number of iterations of the bisection method does not depend on the size of the trade either.

  3. 3.

    As for the calculation of DD, Newton is slightly faster than Halley. The computation of second order derivatives (see Appendix B) is expensive.

  4. 4.

    The number of iterations of the Newton method increases when the size of the trade is big with respect to the size of the pool. In this case the Newton method is the one that performs worse while the bisection method is the one that performs best. However, in practice it is very unlikely to see transactions bigger than the size of the pool.

  5. 5.

    We used x~0\tilde{x}_{0} i.e. the value xD\frac{x}{D} before the swap, as an initial guess. We tried also with 2AMD\frac{\text{2AM}}{D} and 2GMD\frac{\text{2GM}}{D} as initial guesses, but the performances are worse.

When considering an even γ1\gamma_{1}, it turns out that the Newton method is superior for both the calculations of DD and the swap size. In production, we decided to use Newton as main solver, and bisection as fallback in case of failure.

3.3 Model implementation

We tested the model with values of AA ranging from 1 to 10510^{5} and values of gamma from 11 to 7575, under a variety of conditions including pool sizes from 1 to 101110^{11} USDC total value, with a similar range of trade sizes. Generally, the Rust implementation can handle trade sizes several orders of magnitude above the size of the pool without overflowing, unless the values for γ1\gamma_{1} or γ2\gamma_{2} are excessively high. Since γ1\gamma_{1} and γ2\gamma_{2} are exponents, large values quickly create unmanageable numbers.

We implemented the Silkswap algorithm inside Rust smart contracts on the Secret Network blockchain. Smart contract do not allow floating point calculations, and this required the use of a few workarounds. Since smart contracts only support integer arithmetics, we stored most numbers as 101810^{18} larger than their actual value. This effectively allowed us to store 18 decimal places. Variables are stored as uint256 i.e. unsigned 256 bits integers. Additionally, we paired each variable with a boolean representing its sign, allowing us to calculate both positive and negative numbers. The square root in 2GM is computed by the Babilonian method.

Refer to caption
Refer to caption
Figure 9: Graph of the price of the token YY (SILK) as function of the fraction of toke XX (USDC). Two lines corresponding to two different values of γ2\gamma_{2} are compared in order to show how this parameter can control the shape of the curve.

3.4 Parameter selection

In the previous sections we have seen how the parameters AA, γ1\gamma_{1} and γ2\gamma_{2} modify the shape of the Silkswap invariant. Specifically, parameter AA controls the flatness of the curve in the area around the equilibrium point (see Fig 3), while the parameters γ1\gamma_{1} and γ2\gamma_{2} are used to control the curvature and asymmetry of the invariant (see Fig 3). The shape of the invariant is directly reflected in the price curve (see Fig. 5). Thus, the parameters of the model must be chosen in such a way as to make the price curve attractive for DEX users. Therefore, they must maintain a low price impact and protect against possible imbalances in the liquidity pool.

In this section we present a practical method for deciding the value of parameters. Since it is not very practical to consider a curve that depends on the quantity of tokens in the liquidity pool, we define the fraction of token XX inside the pool as xx+py\frac{x}{x+py} and similarly we define the fraction of token YY as pyx+py\frac{py}{x+py}. As usual we multiply yy by pp to have the same units as xx. It is very convenient to express the price curve as a function of the fraction, because the fraction is a percentage and does not depend on the size of the pool. Now we can choose a level of price impact that suits us, and choose the parameters in such a way that the price is impacted by this price impact at a certain level of fraction of the tokens.
In Fig. 9, we show examples of price curves for different parameter values. We choose a 5% level of price impact for YY. We are considering the case x>pyx>py such that we are able to calculate γ2\gamma_{2}. The same procedure should also be done for x<pyx<py and considering a price impact for XX in order to find γ1\gamma_{1}.
In this example, the price of the token YY (SILK) at equilibrium is p=1p=1. Its value after the 5% price impact increase is 1.051.05 and it is represented by the points on the price curves in the figure. We can see how by changing AA and γ2\gamma_{2} it is possible to move the point to the right or to the left as desired.
A look at the curvature can also be helpful in choosing these parameters.

4 Comparison with the Curve model

Curve finance is currently the most popular DEX for trading stablecoins. The Curve v2 model [Egorov, 2021] is an HFMM model, conceptually not very different from Silkswap. They both take a linear combination of the CSMM and the CPMM models. Since in the development of Silkswap we took inspiration from Curve, we decided to use a similar notation. The parameter AA in Curve has the same meaning as in Silkswap. The main difference between these models is the definition of the function χ\chi:

Silkswap : χ=(4xyD2)γγ={γ1,if xyγ2,if x>y\displaystyle\text{Silkswap : }\quad\chi=\biggl{(}\frac{4xy}{D^{2}}\biggr{)}^{\gamma}\quad\quad\quad\gamma=\begin{cases}\gamma_{1},&\mbox{if }x\leq y\\ \gamma_{2},&\mbox{if }x>y\end{cases}
Curve v2 : χ=4xyD2(γγ+14xyD2)2.\displaystyle\text{Curve v2 : }\quad\chi=\frac{4xy}{D^{2}}\biggl{(}\frac{\gamma}{\gamma+1-\frac{4xy}{D^{2}}}\biggr{)}^{2}.

For simplicity we didn’t include the conversion factor pp. In both models 0χ10\leq\chi\leq 1. If for a moment we do not consider the fact that our gamma can assume two values, the gammas in the two models have different meanings. For any point not at equilibrium, in Silkswap: limγ0χ=1\lim_{\gamma\to 0}\chi=1 and limγχ=0\lim_{\gamma\to\infty}\chi=0, while for Curve v2 we have the opposite: limγ0χ=0\lim_{\gamma\to 0}\chi=0 and limγχ=1\lim_{\gamma\to\infty}\chi=1.

Refer to caption
Refer to caption
Figure 10: LEFT: Comparison of the Silkswap invariant with the Curve v2 invariant curves. RIGHT: Comparison of price curves of SILK as function of USDC in the pool. We used D=2000D=2000, p=1p=1, and A=400A=400 for both models. In Silkswap we use γ1=γ2=10\gamma_{1}=\gamma_{2}=10. In Curve v2 we use γ=0.05\gamma=0.05

In Fig. 10 we can see the invariant and price curves produced by these two models. The values of gammas are chosen to make the two curves as close as possible when using same value of AA. We notice that the Silkswap model produces a higher curvature, although we did not prove it formally.

Another difference between the models is that currently Silkswap is designed to work with liquidity pools containing only two tokens, while Curve v2 can have pools with any number of coins.

Unlike Curve, we have decided to apply transaction fees to the token that is inserted into the pool, following the same approach used by Uniswap and Osmosis.

5 Conclusions

The Silkswap invariant is an AMM model that allows users to trade stablecoins with minimal price impact when the state of the pool is close to equilibrium. Away from equilibrium, the price impact tends to that of the CPMM model. The main innovation of this model is the asymmetry of the invariant, which helps to regulate the liquidity in the pool and discourage strong imbalances in the quantity of tokens asymmetrically. For new stablecoins like Silk that are in the process of building up liquidity, the asymmetric curve provides attack protection, discouraging the sale when the amount of Silk in the pool exceeds a certain threshold (dependent on the parameters of the model). This is done by making the price impact grow faster in that direction and slower in the opposite. Figure 5 shows this behavior very well.

Acknowledgements

This research was fully funded by Shade Protocol.

Appendix A Properties of the Silkswap invariant

In Figure 1 we can see that the CPMM graph is greater than the CSMM graph, and they intersect each other at the equilibrium point. Let us formally prove this fact.

Theorem A.1.

For x>0x>0, the condition

1p(x+D)D24px\frac{1}{p}(-x+D)\;\leq\;\frac{D^{2}}{4px} (A.1)

is always satisfied.

Proof.

The condition (A.1) can be written as

x2Dx+D24=(xD2)2 0,x^{2}-Dx+\frac{D^{2}}{4}=\biggl{(}x-\frac{D}{2}\biggr{)}^{2}\;\geq\,0,

which is always verified. ∎

Theorem A.2.

The value of χ\chi, defined in (6) always satisfy

0<χ1.0<\chi\leq 1. (A.2)
Proof.

Since χ\chi is a function of only positive variables, it follows that χ\chi must be positive.
Let us consider the Silkswap invariant (5):

(χAD)>0(x+pyD)0+xpyD240= 0\underbrace{(\chi AD)}_{>0}\,\underbrace{(x+py-D)}_{\geq_{0}}\,+\,\underbrace{xpy-\frac{D^{2}}{4}}_{\leq 0}\,=\,0 (A.3)

Since the sum of two terms is zero, it means that or both terms are zero, or the two terms have opposite sign. The case of both terms equal zero happens only at the equilibrium point.
Let us consider the case of both terms different than zero. We want to prove by contradiction that the first term must be positive.
If the first term is negative, then (x+pyD)<0(x+py-D)<0, and we have that

y<1p(x+D)D24px,y\,<\,\frac{1}{p}(-x+D)\,\leq\,\frac{D^{2}}{4px},

by Theorem A.1. This implies that xpyD24<0xpy-\frac{D^{2}}{4}<0, but this is a contradiction because the terms must have opposite sign. The second term is always negative or zero, and we can conclude the proof.

xpyD24 04xpyD2 1χ1.\displaystyle xpy-\frac{D^{2}}{4}\,\leq\,0\quad\Longrightarrow\quad\frac{4xpy}{D^{2}}\;\leq\;1\quad\Longrightarrow\quad\chi\leq 1.

In Figure 3 we can see that the Silkswap invariant always lies between the graphs of the CSMM and CPMM models.

Theorem A.3.

The graph of the Silkswap invariant always lies between the graphs of the CSMM and CPMM models.

Proof.

First we prove that the Silkswap invariant graph is not greater than the CPMM graph. This is a direct consequence of Theorem A.2:

4xpyD21yD24xp.\frac{4xpy}{D^{2}}\leq 1\quad\Longrightarrow\quad y\leq\frac{D^{2}}{4xp}. (A.4)

Now we prove that the Silkswap invariant graph is not smaller than the CSMM graph. Let us consider the invariant (5) and divide it by the positive quantity ADχAD\chi. We get

0\displaystyle 0 =(x+pyD)+xpyD24ADχ\displaystyle=(x+py-D)+\frac{xpy-\frac{D^{2}}{4}}{AD\chi}
x+pyD.\displaystyle\leq x+py-D.

where we used xpyD240xpy-\frac{D^{2}}{4}\leq 0. It follows that

yx+Dp.y\;\geq\;\frac{-x+D}{p}. (A.5)

Theorem A.4.

The parameter DD in the Silkswap invariant satisfies

2GMD 2AM,2\text{GM}\,\leq\,D\,\leq\,2\text{AM}, (A.6)

where GM is the geometric mean of xx and pypy, and AM is the arithmetic mean.

Proof.

This is an immediate consequence of Theorem A.3. From the two inequalities (A.4) and (A.5) we obtain

2xpyDx+py.2\sqrt{xpy}\,\leq\,D\,\leq\,x+py. (A.7)

Theorem A.5.

The partial derivatives (12), (13) of the function F(x,y)F(x,y) defined in (10) are always positive on {(x,y):F(x,y)=0}\bigl{\{}(x,y)\,:\,F(x,y)=0\bigr{\}}.

Proof.

We present a proof for Fy\frac{\partial F}{\partial y} only, since the same arguments can be used for Fx\frac{\partial F}{\partial x}. Let us consider the expression:

Fy=ADχ[p(γ+1)+γ(xyDy)]+px.\frac{\partial F}{\partial y}=\;AD\chi\,\biggl{[}p(\gamma+1)+\gamma\biggl{(}\frac{x}{y}-\frac{D}{y}\biggr{)}\biggr{]}+px.

Since all variables are positive, when (xyDy)0\bigl{(}\frac{x}{y}-\frac{D}{y}\bigr{)}\geq 0 then Fy>0\frac{\partial F}{\partial y}>0. This happens for xDx\geq D.
For 0<x<D0<x<D, let us rearrange the terms inside the square brackets and use (A.5):

[γp+pγp(1yx+Dp)1]p> 0.\displaystyle\biggl{[}\gamma p+p-\gamma p\underbrace{\biggl{(}\frac{1}{y}\,\frac{-x+D}{p}\biggr{)}}_{\leq 1}\biggr{]}\;\geq p\;>\;0.

This last inequality proves the theorem. ∎

Unfortunately, the function F(x,y)F(x,y) defined in (10) is not continuous in the entire >02\mathbb{R}_{>0}^{2}, and we need to be careful around the points of discontinuity. The following theorem guarantees the validity of (14).

Theorem A.6.

The derivative of the Silkswap invariant can be written as

dydx=FxFy.\frac{dy}{dx}=-\frac{\frac{\partial F}{\partial x}}{\frac{\partial F}{\partial y}}\,.
Proof.

The function F(x,y)F(x,y) is continuously differentiable everywhere except on the line x=pyx=py, where it is discontinuous. The intersection between this line and the Silkswap invariant,

{x=pyF(x,y)=0,\begin{cases}x=py\\ F(x,y)=0,\end{cases}

corresponds to the equilibrium point (D2,D2p)(\frac{D}{2},\frac{D}{2p}). At the equilibrium point we have

lim(x,y)(D2,D2p)F(x,y)= 0\lim_{(x,y)\to\bigl{(}\frac{D}{2},\frac{D}{2p}\bigr{)}}F(x,y)\;=\;0

from any directions, and therefore F(x,y)F(x,y) is continuous in this point. Also

lim(x,y)(D2,D2p)Fx=(A+12)D\displaystyle\lim_{(x,y)\to\bigl{(}\frac{D}{2},\frac{D}{2p}\bigr{)}}\frac{\partial F}{\partial x}\;=\;\bigl{(}A+\frac{1}{2}\bigr{)}D
lim(x,y)(D2,D2p)Fy=p(A+12)D\displaystyle\lim_{(x,y)\to\bigl{(}\frac{D}{2},\frac{D}{2p}\bigr{)}}\frac{\partial F}{\partial y}\;=\;p\bigl{(}A+\frac{1}{2}\bigr{)}D

with limits from any directions. Therefore the partial derivatives are continuous and F(x,y)F(x,y) is continuously differentiable at (D2,D2p)\bigl{(}\frac{D}{2},\frac{D}{2p}\bigr{)}.
Let us differentiate F(x,y)F(x,y) along the direction of the Silkswap invariant

0=dF(x,y)=Fxdx+Fydy.0\;=\;dF(x,y)=\frac{\partial F}{\partial x}dx+\frac{\partial F}{\partial y}dy.

In the Theorem (A.5) we prove that Fy>0\frac{\partial F}{\partial y}>0. We can rearrange the last expression to conclude the proof. ∎

Appendix B Expression of the derivatives

Derivative expressions used for the calculation of DD by Newton and Halley methods:

dF(D|x,y)dD=A(4xpyD2)γ[(2γ+1)(x+pyD)D]D2,\frac{dF(D\,|\,x,y)}{dD}=A\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma}\biggl{[}(-2\gamma+1)\,(x+py-D)-D\biggr{]}-\frac{D}{2}, (B.1)
d2F(D|x,y)dD2=A(4xpyD2)γ[4γ2+2γ(2γ1)(xD+pyD1)]12.\frac{d^{2}F(D|x,y)}{dD^{2}}=A\biggl{(}\frac{4xpy}{D^{2}}\biggr{)}^{\gamma}\biggl{[}4\gamma-2+2\gamma(2\gamma-1)\biggl{(}\frac{x}{D}+\frac{py}{D}-1\biggr{)}\biggr{]}-\frac{1}{2}. (B.2)

First derivative expressions for computing x~\tilde{x} and z~\tilde{z} by Newton and Halley methods:

dF~(x~|z~)dx~=A(4x~z~)γ[γx~+z~1x~+1]+z~,\frac{d\tilde{F}(\tilde{x}\,|\,\tilde{z})}{d\tilde{x}}=A\bigl{(}4\tilde{x}\tilde{z}\bigr{)}^{\gamma}\biggl{[}\gamma\frac{\tilde{x}+\tilde{z}-1}{\tilde{x}}+1\biggr{]}+\tilde{z}, (B.3)
dF~(z~|x~)dz~=A(4x~z~)γ[γx~+z~1z~+1]+x~.\frac{d\tilde{F}(\tilde{z}\,|\,\tilde{x})}{d\tilde{z}}=A\bigl{(}4\tilde{x}\tilde{z}\bigr{)}^{\gamma}\biggl{[}\gamma\frac{\tilde{x}+\tilde{z}-1}{\tilde{z}}+1\biggr{]}+\tilde{x}. (B.4)

for

γ:={γ1,if x~z~γ2,if x~>z~.\gamma:=\begin{cases}\gamma_{1},&\mbox{if }\tilde{x}\leq\tilde{z}\\ \gamma_{2},&\mbox{if }\tilde{x}>\tilde{z}.\end{cases} (B.5)

Second derivatives:

d2F~(x~|z~)dx~2=4z~γA(4x~z~)γ1[2+(γ1)x~+z~1x~],\frac{d^{2}\tilde{F}(\tilde{x}\,|\,\tilde{z})}{d\tilde{x}^{2}}=4\tilde{z}\gamma A\bigl{(}4\tilde{x}\tilde{z}\bigr{)}^{\gamma-1}\biggl{[}2+(\gamma-1)\frac{\tilde{x}+\tilde{z}-1}{\tilde{x}}\biggr{]}, (B.6)
d2F~(z~|x~)dz~2=4x~γA(4x~z~)γ1[2+(γ1)x~+z~1z~].\frac{d^{2}\tilde{F}(\tilde{z}\,|\,\tilde{x})}{d\tilde{z}^{2}}=4\tilde{x}\gamma A\bigl{(}4\tilde{x}\tilde{z}\bigr{)}^{\gamma-1}\biggl{[}2+(\gamma-1)\frac{\tilde{x}+\tilde{z}-1}{\tilde{z}}\biggr{]}. (B.7)

References