The Constant Product AMM (Automated Market Maker) model, used by Uniswap and other decentralized exchanges, maintains a constant product between two assets in a liquidity pool (X × Y = K), meaning whenever traders buy or sell tokens, the quantities of both assets must adjust mathematically to preserve this constant value, ensuring fair price discovery without centralized order books.
Constant Product AMM Explained: Uniswap's Core Mechanism Explained
Added:hey everybody in today's video we will talk about constant product amm in the last video we talked about what is an amm and the different types of amm in today's video we will talk about in depth about the constant product amm model because once we will understand how this constant product amm model works it will be pretty easy to understand how all these different amm model works okay now if you talk about constant product amm model it is one of the earliest and the most popular AM models and it is based on a very simple concept and the concept is that it need to maintain a constant product between the two assets that are in liquidity pool so let's say this is our two tokens X token and Y token and this is our liquidity pool and what is a liquidity pool we will talk about this in depth in the next video but for now you can think of liquidity pool as a pool where we will have different number of X and Y tokens so let's say in this liquidity pool we have different X and Y tokens okay let's say the number of token X is equal to X and the number of token Y is equal to y means let's say the number of token X can be 100 200,000 2,000 or even 3,000 whatever number it can be in the same way number of Y token can be anything now if you talk about constant product amm model it makes sure that the number of tokens between the number of token inside the liquidity pool should always be constant means X into y should always be equal to a constant let us understand this with the help of an example let's say we have X number of 100 number of X tokens and we have 200 y tokens and let's say the liquidity pool fee is zero so actually whenever we have a liquidity pool there is a liquidity pool fee we will talk about this again in the next video don't worry about it but for now we are assuming that the liquidity pool fee is equal to zero and if you will take the product of these two numbers because constant product amm works on product of the two assets right so it will be equal to X into y that is 20,000 now now in this this 20,000 is the constant and amm needs to make sure that this number this 20,000 should always be 20,000 means the product of these two tokens that is X token and Y token should always be 20,000 now let's say because we are talking about a decentralized exchange right which is actually using the amm constant product amm model so let's say there is a Trader Okay who wants to buy let's say 60x tokens okay so currently in the liquidity pool we have have 100 x tokens and this Trader wants to buy 60 x tokens now once he will actually trade means once he will tries to buy this 60x token what will happen the number of X token will get reduced means currently we have 100 and after the trader will take out 60 tokens the number of X tokens will be 40 right now once it will become 40 and if we will multiply this 40 with this 200 right because with the number of Y tokens you will see that the constant number is decreased right means now the value that we have is around 8,000 and 8,000 is not equal to 20,000 and this is also true right because he this person this Trader is actually taking 60x tokens from the liquidity pool but actually when he is doing that the constant is getting changed and we do not want the constant to be changed because this is what constant product amm is all about that we need to make sure that this number should remain always and always constant so how this Trader will make sure that the constant does not get Disturbed so in order to make sure that this number does not get Disturbed the trader will deposit some amount of Y tokens so that we can make sure that X into Y is always 20,000 so now if you will calculate this like how much y token this Trader need to deposit into the liquidity pool so this is our liquidity pool and how much y token the trader need to deposit in it after withdrawing 60x tokens how we can calculate it we can simply use this formula that if there are 40 tokens 40 x tokens in the liquidity pool and let's say we take this as y then how much y tokens should we have in the liquidity pool so we can simply calculate y from this particular formula then and you will find out that Y is equal to 500 tokens so we are simply using mathematics here so what I have done is I have simply taking the number of Y tokens to be y okay and after the trader will take out 60 x tokens the number of X tokens in the liquidity pool will be 40 and now I'm multiplying this 40 with Y so that I can equate it with the constant value that is 20,000 and if we will calculate this y like we will calculate y value from this equation you will find out that in the liquidity pool we must have 500 y tokens in order to have this constant always so now if I will calculate let's say if I will calculate this 500 into 40 that is this is our Y and this is our X you will see that this value will give us 20,000 so you can see by depositing some 300 more tokens because currently we have 200 tokens right initially we have 200 tokens and once the trader will find out okay in order to maintain this constant value I need to deposit some more 300 tokens because after 300 tokens will be added it will become 500 tokens so this 500 into 40 will be equal to 20,000 so this is what Trader will need to deposit in order to maintain the constant product amm so after the deposit the new X into y equation will be 40 x token because 60 tokens has already been taken out by the trader and then 500 y tokens because 300 more tokens will be added by the trader so that we can maintain this 20,000 value and this is how constant product amm model Works let's take one more example in order to better understand it so let's say in this particular case as you can see the constant will be 500 into 100 right 500 into 100 that will be equivalent to 50,000 now this time let's say the trader wants 100 x tokens from the liquidity pool so if the trader will withdraw X tokens from the liquidity pool how much X tokens will be left inside the liquidity pool 500 minus 100 that is 400 tokens will be left inside the liquidity pool and if you will calculate and this is the X tokens right X tokens and if we will calculate 400 into 100 that will be equivalent to 40,000 and you can like this is this is not equal to 50,000 right that is for sure so since this is not equal to 40,000 so if the trader will withdraw 100x tokens from the liquidity pool Trader needs to deposit some y tokens right and how much y tokens uh Trader need to deposit into the liquidity pool it's very simple right because currently we have uh 400 x tokens and we do not know anything about y tokens so 400 into Y which will be equal to the constant and if we will solve this equation this will be equivalent to I think 50,000 divide by 400 again so it will be 20 okay sorry for my mathematics it should not be 20 right because it is four I am confused I was confusing it with some other digit so it should be uh 125 right it should be 125 because there are 500 500 is in the numerator and we have four in the denominator and if you will calculate it it will be 125 so let us cross check this so after withd drawing 100x tokens there will be 400 y tokens so 400 into Y and if you will calculate y will be 125 so if you will see in the liquidity pool already we have 100 tokens right so Trader needs to add 25 plus more token so that the constant is 50,000 only right if you will calculate the number of Y tokens it will be 125 minus because after you will add 25 plus 100 it will be 125 right so the new equation will be X into y 400 into 125 and if you will calculate this it will be equal to 50,000 so as you can see again we are maintaining this constant value that is 50,000 we have reduced x value by 100 and we have increased the yv value by 25 and by this we are able to maintain this constant product am and this is the graph of constant product amm model so you might have seen this graph so many times on Unis Swap and you are like oh my god well it's this all about but trust me this is very easy this graph is all about X into Y is equal to constant so this is your balance of X this is your balance of Y let's say the balance of X is 100 and balance of Y is 200 and if you will calculate it will lie somewhere here on this uh on this line on this Parabola looking kind of looking graph okay in the same way if let's say the value of x is reduced by 60 right so now it is 40 and now if you will calculate the Y value it will be around 500 and again the value will lie something somewhere here so earlier into 20000 was around 20,000 and when once we will reduce the value of x that is 40 we need to increase the value of y which is 500 and once you will again calculate this 500 into 40 again you will get the same number that is 20,000 and if you are confused like how we have got this particular graph let me tell you how we have got this so let us see how we can find this graph that we have seen in the slide that is this graph so we have to have X right and then we need to multiply it by Y and then when you will multiply this x into y we will have some constant value right so let's say the constant value is one so you can see once we will do that and ignore this particular part for now you will find a graph which looks something like this right and this is the same graph that you are seeing here and that's why your constant product am model graph looks something like this is because when you will plot X into y with some constant value when you will equate it with some constant value you will get this kind of uh graph so if you will increase this value let's say two you will see the graph will start shifting okay so if I will make this three if I'll make this four so you can see the graph is Shifting as soon as I'm increasing the value and if I will decrease the value definitely it will go sideways but this will not happen eventually in the liquidity pool because there will be no value like there can be no asset in negative value right so it will be mostly positive in nature and this is how it will look and that's why you see graph which looks something like this right for the constant product amm so with this we will end the video for today I hope you enjoyed today's video meet you soon in the next video
Up Next

Setting Token Prices in Liquidity Pools: Uniswap & PancakeSwap
@EatTheBlocks
130.1K views•2021-07-12

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Full-Stack Dapp Development: Solidity, Ether.js, Hardhat, React
@codeeaterweb3971
50.8K views•2023-03-28

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto







































