DEX Liquidity Algorithm

Hi All,

I have a quick question on DEX liquidity algorithms. For example, let’s say I create an ASA called EXP with 100 total tokens. I add the 100 EXP to a DEX liquidity pool with 100 USDC. This gives 1 EXP a price of 1 USDC and EXP has a total market cap of ~$100.00.

If Bob wants to buy 1 EXP, it should cost him about 1 USDC. This would be probably be about right with a relatively small transaction in the pool. But, let’s say Bob wants to buy 90 EXP, which would be a much larger transaction in the pool. My intuition is if Bob goes to swap 90 USDC to the EXP/USDC pool, he will not get 90 EXP in return. It will probably be less than that. Either way, the resulting EXP/USDC pool will have 190 USDC and X EXP. But, I am not sure how the exact calculation is made or how to solve for X.

What is the general algorithm that determines the swap ratio for assets in DEX liquidity pools? Are there any examples of this in open source code for Algorand DEXs?

Thanks!

Brian

Uniswap v2 exchanges (so regular AMMs) are based on the fact that x*y=constant, so if you start out with 100 USDC and 100 EXP you have k=100*100=10000. If you sell 100 USDC you will solve the equation (100+100)(100-output)=10000 means that the output will be 50 EXP. You can also verify that the constant is constat. The contract now has 200 USDC and 50 EXP and the constant is, well constant, at 10000.

For stableswaps you have a similar concept, but it’s instead a combination of x+y=constant and the above in such a way that you have less price impact around a defined point.

2 Likes

That makes sense. Thank you.

Do you know if the Algorand AMMs are similar or if they are regular AMMs, as you call them?

Tinyman, humbleswap and pact all have constant product AMMs (uniswap v2 model, xy=k). Pact also has stableswaps. I think humbleswap is currently working on creating stableswaps as well.

1 Like

Thank you for the information!