DeFi Bascis #4 | Stableswap :

Naveen Kumar
5 min readOct 29, 2023

--

I am writing a series of posts on DeFi basics from the past several months(sorry for my inconsistency). In this post, I will be delving deep into the famous stableswap mechanism and it’s background. Check the previous article here.

Background:

AMM:

An AMM is a decentralized exchange where the token price is determined by a mathematical equation within the contract, as opposed to a centralized exchange where the price is determined by traders. The three main functionalities of an AMM are adding liquidity, swapping tokens, and removing liquidity.

When adding liquidity, users provide tokens to the AMM and in return receive shares that represent partial ownership. Swapping tokens involves trading one token for another based on the predetermined equation. Removing liquidity allows liquidity providers to withdraw their tokens in exchange for their shares.

Traditional Automated Market Makers (AMMs) such as Uniswap are smart contracts that allow users to exchange between assets. For example, I can exchange token X for token Y using an AMM pair that stores the token pair.

Constant Product AMM:

In Constant product AMM when people make trades, the price varies depends on how many tokens are left in a combined pool.

constant product AMM

This can be represented using the mathematical model X*Y = K. In AMM, the xy=k equation and depth of the pool determine the slippage. Finding the slippage means comparing the price before and after the trade. However this suits for those tokens whose prices are variable in nature as their quantity moves along the curve and remain volatile.

However for stable assets this isn’t great, since the prices of stable assets remain stable(stablecoins pegged at $1 USD for example). This is where constant sum AMM is used. In the constant sum AMM I can always get one token for exchange of other.

Stableswap AMM:

Stablecoins, by design, are pegged to stable assets (e.g., the US dollar) and are intended to maintain a relatively consistent value. However, in practice, there can be minor discrepancies in the price of different stablecoins due to:

  • Varying mechanisms for maintaining the peg.
  • Differences in market trust and adoption.
  • Minor deviations based on supply and demand dynamics.

Stableswap is a mathematical mechanism designed to set the price. How many Y tokens should I get for one X token. Stableswap is designed to set exchange assets with similar value. It combines the two AMM mechanisms which are Constant product AMM and Constant sum AMM.

The math behind a constant sum AMM is simple. The value of tokens coming in and going out is just the sum of all the tokens. The constant sum AMM equation is x + y = k, where x represents the amount of token A and y represents the amount of token B. This equation creates a straight line on a graph, ensuring that the constant k remains the same despite changes in token amounts.

With a constant sum formula, the price will always remain the same regardless of the balances in the pools. There is zero slippage with the linear invariant. Zero slippage is also called infinite leverage.

Linear invariants(constant sum) that have no slippage and are not ideal because the pool can run out of tokens or make the pool very unbalanced.

Therefore for StableSwap, there was a middle-ground invariant found.

The StableSwap AMM algorithm combines

X + Y = D & X*Y = K

The StableSwap compromise is that the pool can slide up or down the X+Y = K curve only when pools are pretty balanced and the price is stable around $1 value.

When pools become unbalanced then the invariant becomes a product invariant instead of the sum invariant, therefore swapping then becomes expensive like an X*Y = K exchange.

StableSwap AMM pools have multiple stablecoins together, such as USDC, DAI, USDT, so the formula looks more like XY*Z = K & X+Y+Z = D.

The below content is math heavy and you can skip this if you aren only interested in basics and not delving deep into the math part of it.*

The famous curve’s stableswap equation goes like this:

K * (X+Y) * D + XY = KD² + (D/2)²

Let’s understand the math behind this equation.

We can merge constant sum with constant product AMM to ensure assets always trade at the same price while ensuring enough liquidity.

Constant sum: X + Y = D ;

Constant product: X*Y = K ;

Merging both for stable swap, we get X + Y + XY = K + D Let us look at how does K parameter related to D parameter. When the amount of tokens of X becomes equal to amount of tokens in Y, we will have D/2 amount of tokens in constant sum.

Replacing this in the equation of constant product,

we have

X*Y = (D/2) * (D/2)

Adding it in the above stableswap function it becomes

X + Y + XY = D + (D/2)²

In the above equation, multiply the constant sum part(red dotted line) i.e (X+Y = D) with D, since the constant product(purple dotted line) D² is more stronger than the sum part.

(X+Y)*D + X*Y = D² + (D/2)²

The above equation now becomes a curve that lies somewhere between a constant sum and a constant product(blue line).

we can make the line even more flattish by multiplying it with a parameter ‘A’

A*(X+Y)*D + XY = A*D² + (D/2)²

at A = 0; the above equation becomes constant product

and higher the A value the curve becomes more flattish and becomes the constant sum

However when the value of A becomes higher positive, the curve(notice the red dotted line) might enter into negative region. We want to avoid that, and it is better to have a multiplication additive that helps in staying it positive. Here that is constant product line(check the purple dotted line which is in positive quadrant.)

Let’s multiply both sides of constant sum equation with XY

X*Y*A(X+Y)*D + X*Y = X*Y*A*D² + (D/2)²

Let’s divide both sides of equation by (D/2)² to amplify the flattishness of constant sum curve

A*[XY/(D/2)²] * (X+Y)D + XY = A * [XY/(D/2)²] D² + (D/2)²

A* (XY/D²) * (X+Y)*D + XY = A * (XY/ D²) * D² * + (D/2)²

In the above equation A * (XY/ D² = K constant, therefore

K * (X+Y) * D + XY = KD² + (D/2)²

uff finally arrived at the equation.

To understand this better using a visualisation, I recommend to check this video by #smartcontractprogrammer

https://youtu.be/oUqFeJPnq3s

Resources and citations :

--

--

Naveen Kumar

Optimistic nihilist, Another Atom, in the universe of atoms :)