StableSwap - efficient mechanism for Stablecoinliquidity
Michael Egorov
November 10, 2019
Abstract
StableSwap provides a mechanism to create cross-markets for stablecoins in a way which could be called “Uniswap with leverage”. It is a fullyautonomous market-maker for stablecoins with very minimal price slippage, as well as an efficient “fiat savings account” for liquidity providers
on the other side.
This is a brief version which doesn’t show all the details (most notably,
the StableSwap invariant).

Introduction
Stablecoins become very popular recently: custodial USDC, USDT, BUSD,PAX, TrueUSD, as well as decentralized DAI. They however (especially decentralized ones) have a problem of price stability and liquidity. This is especiallypainful for DeFi arbitrage. For example, when MakerDAO decreased its stability fee to 5.5%, many users of Compound (which had the interest rate of 11%
at the time) preferred to stay there because they’ve taken the loan in DAI, andconverting between DAI and USDC is an expensive task.
At the same time, many DeFi users are willing to load their stablecoins upfor lending in order to earn 5% APR, as it is much more than what traditionalbanking offers. They, however, would be uncomfortable giving same money totrading firms who “promise profits”.
In this work, I introduce StableSwap - automated liquidity provider for stablecoins. On the demand side, it offers a Uniswap-like automated exchangewith very low price slippage (typically 100 times smaller). On the supply side,
it offers a multi-stablecoin “savings account” which, according to simulation, canbring 300% APR, according to simulations assuming that traders will arbitrage
between the smart contract and existing exchanges, taking into account theirtrading volumes and prices for stablecoins for the past half a year. This happens
with no middleman being responsible for the trading, e.g. no exchange owners,
no orderbooks, no human market makers.

1

How it works
First of all, imagine a liquidity provider which has constant price. If you have
two coins X and Y , for example, selling dx of coin X will lead to buying −dy =dx of coin Y . This can be generalized for any number of coins Xi having a“linear” invariant:
Xxi = const.
The price is determined as −dxi /dxj which is, in this case, always exactly
1. This doesn’t work in a fluctuating market unless the price is adjusted all
the time. It can be done with price oracles, but it has risks and not very
decentralized. It’s possible to do better.
Uniswap, Bancor and Kyber work with inherently volatile and price-unstable
markets, so they do it differently. They adjust prices in such a way that the“portfolio” (which is usually just two coins) is rebalances (so that value of coin
X and Y in the liquidity pool, when expressed in the same currency, is thesame). It appears, that this is given automatically when you keep product ofquantities of coins in the liquidity pool constant:xy = const.
Moreover, it is possible to generalize this invariant to any number of coinswith any rations, as was brilliantly done by Balancer:
Yixw
i = const.
While this is suitable for assets like ETH and tokens, it’s not very well
working for something which is meant to be stable. The problem is that theprice slippage is enormous, and one should provide enormous funds to keep ameaningful liquidity. On the flip side, if one for example loads DAI and USDC
into Uniswap’s liquidity pool, the returns will be tiny (perhaps, several percent
per year).
For StableSwap, there was a middle-ground invariant found (Fig. 1). As
expected, the price (equal to derivative) only slightly deviates from 1 when
number of coins is closed to balance.

2

20.0

Uniswap invariant (xy = const)
Constant price = 1.0 (x + y = const)
Stableswap invariant

17.5
15.0y ($)

12.5
10.0
7.5
5.0
2.5
0.005101520x ($)

2530

Figure 1: Comparison of StableSwap invariant with Uniswap (constant-product)and constant price invariants. The portfolio consists of coins X and Y whichhave the “ideal” price of 1.0. There are x = 5 and y = 5 coins loaded up initially.
As x decreases, y increases, and the price is the derivative dy/dx.
The price slippage (Fig. 2) is much smaller, if compared to constant-productinvariant.
The StableSwap invariant has an “amplification coefficient” parameter: thelower it is, the closer the invariant is to the constant product. When calculating
slippage, we use a practical value of A = 100. This is somewhat comparable tousing Uniswap with 100x leverage.
1.0

Stableswap
Uniswap

0.9

price

0.8
0.7
0.6
0.5
0.4
0.3

024dx ($)

6810Figure 2: Price slippage: Uniswap invariant (dashed line) vs Stableswap (solidline)
If the price appears to be shifted from equilibrium point (1.0), the invariant
starts operating in a suboptimal point, still however providing some liquidity (inmost cases larger than constant-product invariant, if optimal A was correctly
3found). At any price, this invariant, just like a constant-product one, would
provide some liquidity (unlike the constant-sum invariant).

Constructing the StableSwap invariant
As depicted in Fig. 1, the constant-price invariant forms a straight line (ora hypersurface if having more than two coins). A constant-product invariant
forms a hyperbola.
The price is a slope of the line on the graph. We are looking for some invariant which is relatively flat near balance (price changes slowly, the graph is very
close to the straight line, likely a “zoomed in” hyperbola), however shifting towards the constant-product invariant as the portfolio becomes more imbalanced(e.g. closer to the axes).
Here are constant-sum (constant-price) and constant-product invariants generalized for n coins, enumerated by i:X
xi = D;Yxi =Dnn.

The constant D has a meaning of total amount of coins when they have an equalprice.
Let’s imagine what would an “amplified” invariant be. It should have a small
curvature to have a low price slippage. A “zero slippage” invariant would correspond to infinite leverage. However, the zero-slippage invariant is a constantprice, or constant-sum one! Hence, assuming that constant-product has a “zeroleverage”, and constant sum has an “infinite leverage”, let’s construct something
in between. Let’s denote the leverage χ. If we multiply the constant-sum invariant by χ and add it to the constant-product one, we will have an invariant whichis constant-product when χ = 0, and constant-sum when χ = ∞: the property
we are looking for. However, χ should ideally be a dimensionless parameter, notdepending on numbers of coins we have.
Therefore, let’s multiply the constant-sum invariant by χDn−1 and add tothe second invariant: nXYD
χDn−1xi +xi = χDn +.
nIf this equation holds at all times, we will have trades with a leverage χ. However, it wouldn’t support prices going far from the ideal price 1.0. The invariant
should support any prices (so that we have some liquidity at all times).
In order to do so, we make χ dynamic. When the portfolio is in a perfectbalance, it’s equal to a constant A, however falls off to 0 when going out ofbalance:
4χ=QA xin.
(D/n)

Substituting this to the “leveraged” invariant above, we come to the StableSwap
invariant:AnnXxi + D = ADnn +Dn+1
Q .
nn xi

When a portfolio of coins {xi } is loaded up, we need to calculate D, and weneed to hold this equation true when we perform trades (e.g. swap xi into xj ).
That is done by finding an iterative, converging solution either for D, or for xj
when all other variables are known.

Simulations and performance
The performance of the algorithm was evaluated and optimized assuming providing liquidity for 3 stablecoins (DAI, USDC and USTD) taking price feeds from
Coinbase Pro (DAI/USDC), Binance (USDC/USDT) and HitBtc (USDT/DAI)
over the period of 6 months (May - October 2019). The simulations assumed
the total liquidity in the contract of $30000. Trades were only done if there wasenough volume in the price change. The results were the following:• Optimial “amplification coefficient” (“leverage”): A = 85;• Optimal fee: 0.06% per trade;• Liquidity provider profit at optimal parameters: 312% APR.

Implementation
Multi-stablecoin contract was implemented in Vyper. Solutions of the equations
which use the stableswap invariant were obtained iteratiely inside the smart contract itself, using only integer arithmetics. Browser UI (Fig. 3) was implemented
in pure client-side Javascript.

Other applications
Apart from liquidity for stablecoins, the same method can be applied for providing liquidity to interest-bearing assets (cDAI) and tokenized stake for stakeable
cryptocurrencies. In my opinion, the method is an important part of future
DeFi infrastructure.
Applying this method to stablecoins can get it battle-tested, and to increaseusability of decentralized (non-custodial) stablecoins.

5Figure 3: Stableswap UI

6