Constant Product AMM Math | DeFi

Поділитися
Вставка
  • Опубліковано 15 лис 2024

КОМЕНТАРІ •

  • @smartcontractprogrammer
    @smartcontractprogrammer  2 роки тому +1

    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/

  • @guelster
    @guelster 7 місяців тому +1

    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!💪

  • @stephenkoessler8315
    @stephenkoessler8315 2 роки тому +2

    I think this would be even better to have some example, but this is a really good explanation, thank you!

  • @carlhopkinson
    @carlhopkinson 11 місяців тому

    Brilliant and clear derivations from the XY = K invariant.

  • @replikeit2204
    @replikeit2204 2 роки тому +3

    lets do curve, with their quadratic equalution and weight math!

  • @kishorevb8331
    @kishorevb8331 Рік тому

    such a great video, no one explained how adding liquidity works

  • @Ts2pc
    @Ts2pc 2 роки тому

    A very good math teacher!

  • @mister.solovev
    @mister.solovev Рік тому

    Thank you for your clear explanation!

  • @MrCoreyTexas
    @MrCoreyTexas 4 місяці тому

    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.

  • @kemalware4912
    @kemalware4912 2 роки тому

    So good to be true man, mind blowing content

  • @chain_l
    @chain_l 7 місяців тому

    Quintessential example of how an explanation should be. Is there a video explaining along with the fees that AMM's charge?

    • @smartcontractprogrammer
      @smartcontractprogrammer  7 місяців тому +1

      On Uniswap V2, it's 0.3% of token in

    • @chain_l
      @chain_l 7 місяців тому

      @@smartcontractprogrammer That's right, how the issuance of LP changes or how they are transferred after the platform fees is enabled.

  • @МарияЛисова-с2ц
    @МарияЛисова-с2ц 3 місяці тому +1

    What is T? 16:25
    Answer: the total amount of shares before an increment

  • @JayBInTech
    @JayBInTech Рік тому

    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

    • @user-zu1ix3yq2w
      @user-zu1ix3yq2w Рік тому

      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.

  • @captainp.3305
    @captainp.3305 2 роки тому +2

    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?

  • @codemuhammed
    @codemuhammed 2 роки тому +1

    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!

  • @salem232
    @salem232 2 роки тому

    Legend !! 😍 THANK YOU!

  • @РомаПетров-ж1н
    @РомаПетров-ж1н 2 роки тому +1

    Would be really interesting to get your video on Bancor curve! Would you create one? =)

  • @qiuhaoli
    @qiuhaoli Рік тому

    Thanks for the helpful video! May I ask when to use CPAMM and when to use CSAMM? Is there any references I can view?

  • @ebrahimkarimi1388
    @ebrahimkarimi1388 2 роки тому

    Thank you. Its video is very good.

  • @sattayakongkom8222
    @sattayakongkom8222 Рік тому

    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

  • @MrCoreyTexas
    @MrCoreyTexas 4 місяці тому

    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?

  • @santiagodevrel
    @santiagodevrel Рік тому

    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)

    • @smartcontractprogrammer
      @smartcontractprogrammer  Рік тому

      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)

  • @Ortons171
    @Ortons171 2 роки тому

    amazing math!

  • @vermabharat
    @vermabharat 2 роки тому

    Thanks for the video. Really appreciate it!

  • @ken14250
    @ken14250 2 роки тому +1

    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!

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 роки тому +3

      No one uses constant sum. It's good for education purpose.
      Curve uses a hybrid of constant sum and product

    • @vermabharat
      @vermabharat 2 роки тому

      Constant sum is not practically feasible

    • @paullawrence5841
      @paullawrence5841 2 роки тому +1

      Liquidity drains out in constant sum AMM much sooner as compared to constant product AMM.

    • @mayanknagwanshi
      @mayanknagwanshi Рік тому

      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

  • @marioarabaolaza5852
    @marioarabaolaza5852 2 роки тому

    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
      @smartcontractprogrammer  2 роки тому +1

      check other videos about constant product AMM, Uniswap V2 and V3

    • @marioarabaolaza5852
      @marioarabaolaza5852 2 роки тому

      @@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??

  • @calvinjoshua9615
    @calvinjoshua9615 2 роки тому +1

    24:05, wasn't it derived for liquidity ?

  • @aiduongphan285
    @aiduongphan285 2 роки тому

    I have a question: where is 'slippage' in those equation? Cause when we swap token A to B there is also slippage.

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 роки тому +3

      time between when you submit the transaction and time the the transaction is executed, price might be different

  • @chintancodewexy2933
    @chintancodewexy2933 2 роки тому +1

    (L1 - L0/L0) * T = S here how to calculate total share(T) ?

  • @wuchuannn
    @wuchuannn 2 роки тому

    What tool did you use for drawing this out?

  • @kishorevb8331
    @kishorevb8331 Рік тому

    can you please tell me what software are you using to write these notes

  • @MeredithGrey-13
    @MeredithGrey-13 5 місяців тому

    Yo kittens!
    MEREDITH ==> crypto's worst rapping cat! ᓚᘏᗢ