0:00 - Intro 2:38 - Swap - How many tokens to return in a trade? 5:53 - Add liquidity - How many shares to mint? - Answer 6:20 - Add liquidity - Constraints 11:20 - How many shares to mint? 12:54 - How to measure liquidity? 16:04 - Simplify equation for shares to mint 22:05 - Remove liquidity - How many tokens to withdraw? Notes github.com/t4sk/notes Take a course www.smartcontract.engineer/
Thanks for the great tutorial. It is really helpful, but I have two questions to ask. 1. 10:51 How do you summarize dy = ydx / x and dx = xdy / y to dx/dy = x/y? 2. 12:24 This may be simple math but I don't know how you simplify L1/L0 * T - T to L1-L0/LO * T
2. it works so that's all that matters. What rules are involved? I want to know, too. I'll figure it out later. You can't just remove "T - T" and simplify it to L1/L0. That's wrong.
In summary: CPAMM (Constant product automated market making) One such rule is the constant product formula X * Y = k, where X and Y are the reserves of two tokens, A and B. To withdraw some token A, one must deposit a proportional amount of token B to maintain the constant k before fees. How do we get initial LP shares, T, after adding initial X and Y reserve? f(X,Y) => sqrt(X*Y) = T To add liquidity, the price impact must be zero X/Y = (X+dx)/(Y+dy) X/Y = dx/dy How many LP shares to mint after adding liquidity? s = (dx/X)T = (dy/Y)T, where T = current LP shares How many tokens dx and dy to withdraw after removing liquidity and burning s? dx = (s/T)X and dy = (s/T)Y and T' = T-s How many tokens to return in a trade? When you want to buy dx from X tokens reserve, you pay by adding dy to Y tokens reserve, in other words, tokens are priced along an iso-liquidity price curve for non-zero token reserves. (X - dx) * (Y + dy) = k, where k = X * Y Thanks for this awesome video!
How did you come up with "the square root of x * y is no greater than 2 times the max of x and y? I'm just plugging numbers into the formula, I've never thought how do you relate square root of a product with maximum? You're coming up with a ceiling / floor somehow?
Thanks for this amazing video!! Your content is gold. Just a quick doubt: Maybe for the shares to mint formula, isn't easy to do it like this? SharesToMint = sqrt(dx*dy) in this way we can get the shares to mint in 2 operations. (of course using a library for square root)
Great video! But there is one question that has been bothering me for so long...What is the dif between constant sum and constant product AMM? And what's the advantage of each over the other? Thank you!
Awesome video, like really awesome. I have a question: why the function for liquidity is √xy?? Is there an explanation for that apart the one from the video??
@@smartcontractprogrammer thanks!! Also how could we interpretate the equation: dx = (x *dy)/y + dy ?? For example if we separate dy/y + dy is giving this a ratio or something??
0:00 - Intro
2:38 - Swap - How many tokens to return in a trade?
5:53 - Add liquidity - How many shares to mint? - Answer
6:20 - Add liquidity - Constraints
11:20 - How many shares to mint?
12:54 - How to measure liquidity?
16:04 - Simplify equation for shares to mint
22:05 - Remove liquidity - How many tokens to withdraw?
Notes
github.com/t4sk/notes
Take a course
www.smartcontract.engineer/
I absolutely love all the videos you make! They are technical enough without being too bogged down in the fine details! Keep up the good work!💪
I think this would be even better to have some example, but this is a really good explanation, thank you!
Brilliant and clear derivations from the XY = K invariant.
lets do curve, with their quadratic equalution and weight math!
such a great video, no one explained how adding liquidity works
A very good math teacher!
Thank you for your clear explanation!
At 21:00, I wanted to point out you can also get from dx/x = dy/y directly from dx/dy = x/y without having to go through some complex algebra.
So good to be true man, mind blowing content
Quintessential example of how an explanation should be. Is there a video explaining along with the fees that AMM's charge?
On Uniswap V2, it's 0.3% of token in
@@smartcontractprogrammer That's right, how the issuance of LP changes or how they are transferred after the platform fees is enabled.
What is T? 16:25
Answer: the total amount of shares before an increment
Thanks for the great tutorial. It is really helpful, but I have two questions to ask.
1. 10:51 How do you summarize dy = ydx / x and dx = xdy / y to dx/dy = x/y?
2. 12:24 This may be simple math but I don't know how you simplify L1/L0 * T - T to L1-L0/LO * T
2. it works so that's all that matters. What rules are involved? I want to know, too. I'll figure it out later.
You can't just remove "T - T" and simplify it to L1/L0. That's wrong.
21:20 What if we just created a pool and total shares (T) is equal to zero? I mean in this situation shares to mint (s) will be equal to zero too?
shares to mint = change in liquidity = sqrt(xy)
In summary:
CPAMM (Constant product automated market making)
One such rule is the constant product formula X * Y = k, where X and Y are the reserves of two tokens, A and B. To withdraw some token A, one must deposit a proportional amount of token B to maintain the constant k before fees.
How do we get initial LP shares, T, after adding initial X and Y reserve?
f(X,Y) => sqrt(X*Y) = T
To add liquidity, the price impact must be zero
X/Y = (X+dx)/(Y+dy)
X/Y = dx/dy
How many LP shares to mint after adding liquidity?
s = (dx/X)T = (dy/Y)T, where T = current LP shares
How many tokens dx and dy to withdraw after removing liquidity
and burning s?
dx = (s/T)X and dy = (s/T)Y and T' = T-s
How many tokens to return in a trade?
When you want to buy dx from X tokens reserve, you pay by adding dy to Y tokens reserve,
in other words, tokens are priced along an iso-liquidity price curve for non-zero token reserves.
(X - dx) * (Y + dy) = k, where k = X * Y
Thanks for this awesome video!
Legend !! 😍 THANK YOU!
Would be really interesting to get your video on Bancor curve! Would you create one? =)
Thanks for the helpful video! May I ask when to use CPAMM and when to use CSAMM? Is there any references I can view?
CPAMM - Uniswap V2
CPAMM + CSAMM hybrid = curve V1
Thank you. Its video is very good.
1.before Swap and After Swap , K not change ?
2.How to calculate with fees if r is 0.1% of fees (X - dx) * (Y + (r)(dy)) = k
How did you come up with "the square root of x * y is no greater than 2 times the max of x and y? I'm just plugging numbers into the formula, I've never thought how do you relate square root of a product with maximum? You're coming up with a ceiling / floor somehow?
sqrt(xy)
Thanks for this amazing video!!
Your content is gold.
Just a quick doubt:
Maybe for the shares to mint formula, isn't easy to do it like this?
SharesToMint = sqrt(dx*dy)
in this way we can get the shares to mint in 2 operations. (of course using a library for square root)
I think that will give incorrect or inconsistent numbers.
Liquidity is measured as sqrt(xy) so change in liquidity is sqrt((x+dx)(y+dy)) - sqrt(xy)
amazing math!
Thanks for the video. Really appreciate it!
Great video! But there is one question that has been bothering me for so long...What is the dif between constant sum and constant product AMM? And what's the advantage of each over the other? Thank you!
No one uses constant sum. It's good for education purpose.
Curve uses a hybrid of constant sum and product
Constant sum is not practically feasible
Liquidity drains out in constant sum AMM much sooner as compared to constant product AMM.
I am not sure if this is correct but in x*y=k, you can never have x=0 or y=0, however in x+y=k, there can be x=0 or y=0, if x=k or y=k
Awesome video, like really awesome. I have a question: why the function for liquidity is √xy?? Is there an explanation for that apart the one from the video??
check other videos about constant product AMM, Uniswap V2 and V3
@@smartcontractprogrammer thanks!! Also how could we interpretate the equation: dx = (x *dy)/y + dy ?? For example if we separate dy/y + dy is giving this a ratio or something??
24:05, wasn't it derived for liquidity ?
I have a question: where is 'slippage' in those equation? Cause when we swap token A to B there is also slippage.
time between when you submit the transaction and time the the transaction is executed, price might be different
(L1 - L0/L0) * T = S here how to calculate total share(T) ?
totalSupply
@@smartcontractprogrammer thank you
What tool did you use for drawing this out?
Excalidraw
@@cleverblocks6422 thank you ser
can you please tell me what software are you using to write these notes
excalidraw.com/
Yo kittens!
MEREDITH ==> crypto's worst rapping cat! ᓚᘏᗢ