A deep dive into the need and usages of AMMs

What do you mean by an AMM and why do we need it in DeFi ?

Devdatt N
Timeswap Chronicles
9 min readFeb 27, 2022

--

In traditional finance, we mostly see two ways of exchanging assets — direct swapping between traders and order books. Direct swapping obviously means there should be someone who wants your asset and is willing to give their asset for it. Well, it doesn’t sound too exciting does it ! Order books, on the other hand, provide an optimized way of direct swapping — a common marketplace where everyone quotes their buying and selling price and products get swapped !

However, in both cases if looked at closely, we see a common problem — the lack of liquidity. Order books enhanced liquidity provisions but in scenarios where perhaps, everyone wants to sell or everyone wants to buy something or maybe even if everyone decided to stay away from crypto for a day and there is just one guy who wanted to buy some BTC, we have a lack of liquidity.

So how do we solve this ?

Three words. Automated Market Makers.

Automated market makers or AMMs are perhaps the best way of providing liquidity we have now. An AMM is basically a tool or protocol which helps traders to swap between assets without an actual person at the other end ! It does this by the help of liquidity pools which are basically asset pools of an AMM and contain assets in a specified ratio which reflects the price of assets.

For example, if BTC costs 60,000 USDT, a BTC-USDT liquidity pool would have a ratio of BTC : USDT :: 1 : 60,000 .

So now that we have an idea of what are liquidity pools, let’s dive deeper into the working of an AMM.

What’s inside an AMM ?

Simply said, Mathematical formulas.

In the AMM world we find a plethora of different ways liquidity can be managed in AMMs — there are a ton of different formulas, one more optimized than the other !

However, we will be looking into a very simple and perhaps the first AMM formula :

X * Y = K

Here, X is the amount of Asset 1, Y is the amount of Asset 2, and K is the constant product obtained from multiplying the assets when the liquidity pool was made. Let’s look at an example of how this works :

Let’s say a liquidity pool of assets A and B were made, and number of A was worth 10,000 times B when the pool was made.

Let’s say 10 A and 100.000 B were added. Therefore, substituting in the formula,

10*100000=1000000

1000000 is the constant product K we have here. Now, imagine someone who wants to buy 1 asset. So how much B does he have to pay ? Let’s assume he has to pay x number of B assets and substitute in the formula :

(10–2)*(100000+x)=1000000

We get x = 25.000. Therefore, the person has to pay 25.000 B for procuring 2A even though originally 1 A was worth 10.000 B . This happens because the formula x*y=k tends to minimize the loss in value for either asset. We can graph either formulae and check the price change slopes, but we will leave that to you !

Delving deeper into advanced AMMs

Even though the concept of liquidity pools reduced the liquidity availability issue, the simple constant product formula wasn’t suited for all kinds of money markets. Of course, the blockchain industry is one of the most researched right now and it wasn’t much time before more optimized liquidity pools were formed.

Here, we will learn about Balancer and Curve AMMs which have been quite innovative.

Case Study : Balancer

Traditional AMMs usually allow only token addition in liquidity pools in a ratio of 1:1. However, Balancer offers weighted pools where you can add tokens in a ratio you wish to, for instance 80:20.

Liquidity Bootstrapping Pools or LBPs are perhaps the highlight of Balancer — they make use of weighted pools to bootstrap liquidity. For example, a newly launching project can launch with very low funds and a 99 : 1 liquidity pool thereby facilitating fair launches. This has a connection to Timeswap which we will discuss soon !

Another worthy mention is the managed pools on Balancer which allow upto 50 tokens and is useful for portfolio management — this provides incredible flexibility with its time-based weight shifting mechanisms and precautions set in place to eradicate tokens which are compromised !

Balancer also offers Stableswap based on Stablemath which we will explain more about in the Curve AMM case study !

Case Study : Curve Stableswap

The Curve Stableswap AMM is perhaps one of the more modern and widely used AMMs right now in DeFi. Let’s try to get a very basic understanding of how the Stableswap AMM works !

First, we will look at two possibilities of how tokens could be exchanged, or more clearly, two formulas according to which AMMs could work :

Linear invariant :

Let’s say we have two tokens USDC and USDT, and their values are the same.

Essentially, price(USDC) = price(USDT).

We add 100 tokens of each in the liquidity pool. So the market price of USDC and USDT are the same now !

Now, if I wanted to buy 20 USDT at the current market price, I would have to pay 20 USDC since their prices are equal. After my transaction,

(100+20) = 120 USDC remain in the pool

(100–20) = 80 USDT remain in the pool

Now 1 USDT = 1.5 USDC in the liquidity pool.

However, you can see that if I keep buying USDT at market price, eventually all the token liquidity will run out which did not happen for the formula we looked at earlier !

If we write the formula of the above AMM in a mathematical way, we would have this :

X + Y = K

Let’s plot this on a graph to see where the token liquidity runs out !

Let’s say we have 5 X tokens and 5 Y tokens in liquidity pool.

Therefore, X + Y = 5 + 5 = 10.

We get this graph :

Notice how the red line eventually touches the X and Y axis !

At the X axis, the token count of Y would have run to 0 and vice versa !

Therefore, it’s pretty easy to understand that a linear invariant, i.e , keeping a formula based on constant sum will drain out the liquidity, essentially heading us back to the original liquidity problem.

At the same time, what can be observed is that the person swapping can actually get the token at the exact market price ! We will use this observation soon.

Constant Product Formula

We have already visited the infamous Uniswap constant formula,

X*Y = K

Let’s say we have two tokens USDC and USDT, and their values are the same.

Essentially, price(USDC) = price(USDT).

We add 100 tokens of each in the liquidity pool. So the market price of USDC and USDT are the same now !

Now, I want to buy 20 USDT with some USDC that I have. Let’s plug this into the constant product formula and see how much I have to pay :

Initially, 100 USDC * 100 USDT = 10000

After 20 USDT has been bought,

(100+X) * (100–20) = 10000

Solving for X,

X = 25

I have to pay 25 USDC for 20 USDT !

We can observe that the user ended up paying even more than the market price, meaning they had to keep a slippage so that the constant product formula could be satisfied !

However, a very interesting fact about the constant product formula is that liquidity never runs out ! In fact, we can graph the above example and check for ourselves !

As you can see, the graph does not intersect the X or Y axis at any noticeable distance !

However, as we noticed earlier, slippage tends to be very high in the constant product formula. How can we solve this ?

Remember how in the linear invariant formula, we could buy at market price and no slippage was required ? What if we could combine the constant product formula with the awesome liquidity management and the linear invariant formula with the low slippage and bring out the best of both worlds ?

Stableswap does exactly this !

Introducing DeFi 2.0 with the TimeSwap AMM

Timeswap uses the constant product formula in a very different way than it was thought possible — in lending and borrowing assets !

DeFi 1.0 products had their own disadvantages — many were prone to flash loan attacks and many only offered over-collateralized loans. Even amidst all the hype, these are a deal-breaker for many. This is where TimeSwap comes in.

First let’s look at how the TimeSwap AMM works, then move on to how it will benefit us because there are a lot of ways !

Inside the TimeSwap AMM is the three variable constant product formula,

X*Y*Z = K

Here,

X → Principal Pool

Y → Interest Rate Pool

Z → Collateral Factor Pool

You might be wondering what interest rates are doing inside a liquidity pool. Keep the suspense up, we are explaining it later in this section !

Here, it is important to understand that X, Y and Z are virtual pools. Knowing this, let’s look at how the formula works :

Pool X : The Principal Pool is a virtual pool which consists of the sum of assets that have been deposited by lenders.

Pool Y : The interest rate pool is a virtual pool which determines the interest to be paid. This is in such a way that Y/X gives the interest per second to be paid until the pool matures (yes, we will talk about pool maturity soon xD) !

Pool Z — The collateral factor pool is also a virtual pool which calculates the collateral to be locked by borrowers such that X/Z is the average collateral factor.

Of course, in reality there are just two pools which are the Collateral pool which consists of tokens given by borrowers in collateral and the asset pools which consists of lent tokens and repaid loans !

Now let’s talk about the real deal, how TimeSwap intends to revolutionize the DeFi scene :

Fixed Maturity Deposits and Loans

Every pool on TimeSwap has a maturity date, following which the lenders can claim their tokens and the interest, while borrowers will have their loans liquidated. The good part about this is that loans won’t be liquidated as per market price, but as per a time specified earlier ! No more worrying about if the value of your collateral decreased !

Oracle-less AMM design

All TimeSwap pools run independently, not relying on any price oracle !

Since TimeSwap doesn’t need an oracle, flash-loan attacks are not possible at all ! TimeSwap doesn’t have any price-based liquidator either ! This is perhaps one of the most important advantages of TimeSwap — no hacks.

Completely Permissionless Pools

Anyone can create a pool on TimeSwap for any kind and number of tokens they want ! This completely permissionless system ensures that no middle-person is present for any transaction !

Isolated Markets

Each lending and borrowing market made on TimeSwap has its own liquidity pools ! This means that even if one market collapses due to defaulting, this does not affect the other markets !

Flexible Interest Rates

According to the market and their own preference, users can change the interest rates they wish to get on their lent assets and the rates they wish to pay for borrowing assets ! This will, of course, change their respective insurance and collateral accordingly !

These are not where it ends, TimeSwap provides many alternative use cases and we will go through a couple of them !

Alternative Use Cases of TimeSwap

Initial Debt Financing Offerings

Debt financing is a very important part of any financial market. However, the decentralized debt financing occurring at the moment, is unfortunately not really decentralized. Most debt financing from communities is done through IDOs and ICOs, which tend to be very centralized.

TimeSwap markets can be used for Initial Debt Financing Offerings or IDFOs ! This will ensure fair launches really are fair. This can be done in such a mechanism that when the pools mature, the tokens are distributed too !

Social Tokens and Debt financing for creators

The newly emerging social tokens fit perfectly into the TimeSwap ecosystem ! Now, communities can set up markets with social tokens on TimeSwap ! This will ensure that communities can get yield from the social tokens they hold.

TimeSwap enables Creator Debt Financing, wherein a creator can issue their debt to the community in exchange of social tokens !

Conclusion

TimeSwap is one of the foremost pioneers of DeFi 2.0 enabling not only permissionless lending and borrowing with oracle-less fixed maturity pools, but also other possibilities such as Debt Financing and Social Token markets.

--

--