Constant Product AMM Math | DeFi

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 มิ.ย. 2024
  • Math for constant product AMM (automated market maker).
    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
    #Solidity #SmartContract #Ethereum #スマートコントラクト
    Take a course
    www.smartcontract.engineer/
    Follow
    / programmersmart
    / discord
    t.me/smartcontractprogrammer
    smartcontractprogrammer.com
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 47

  • @smartcontractprogrammer
    @smartcontractprogrammer  ปีที่แล้ว +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 หลายเดือนก่อน +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

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

  • @mister.solovev
    @mister.solovev ปีที่แล้ว

    Thank you for your clear explanation!

  • @carlhopkinson
    @carlhopkinson 6 หลายเดือนก่อน

    Brilliant and clear derivations from the XY = K invariant.

  • @kemalware4912
    @kemalware4912 ปีที่แล้ว

    So good to be true man, mind blowing content

  • @kishorevb8331
    @kishorevb8331 ปีที่แล้ว

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

  • @replikeit2204
    @replikeit2204 ปีที่แล้ว +3

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

  • @user-qf2fm4js1z
    @user-qf2fm4js1z ปีที่แล้ว

    A very good math teacher!

  • @ebrahimkarimi1388
    @ebrahimkarimi1388 ปีที่แล้ว

    Thank you. Its video is very good.

  • @salem232
    @salem232 ปีที่แล้ว

    Legend !! 😍 THANK YOU!

  • @vermabharat
    @vermabharat ปีที่แล้ว

    Thanks for the video. Really appreciate it!

  • @Ortons171
    @Ortons171 ปีที่แล้ว

    amazing math!

  • @user-sz3rd5qk9l
    @user-sz3rd5qk9l ปีที่แล้ว +1

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

  • @chain_l
    @chain_l 2 หลายเดือนก่อน

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

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 หลายเดือนก่อน +1

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

    • @chain_l
      @chain_l 2 หลายเดือนก่อน

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

  • @calvinjoshua9615
    @calvinjoshua9615 ปีที่แล้ว +1

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

  • @captainp.3305
    @captainp.3305 ปีที่แล้ว +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?

  • @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)

  • @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 9 หลายเดือนก่อน

      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.

  • @wuchuannn
    @wuchuannn ปีที่แล้ว

    What tool did you use for drawing this out?

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

  • @aiduongphan285
    @aiduongphan285 ปีที่แล้ว

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

    • @smartcontractprogrammer
      @smartcontractprogrammer  ปีที่แล้ว +3

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

  • @marioarabaolaza5852
    @marioarabaolaza5852 ปีที่แล้ว

    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  ปีที่แล้ว +1

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

    • @marioarabaolaza5852
      @marioarabaolaza5852 ปีที่แล้ว

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

  • @ken14250
    @ken14250 ปีที่แล้ว +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  ปีที่แล้ว +3

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

    • @vermabharat
      @vermabharat ปีที่แล้ว

      Constant sum is not practically feasible

    • @paullawrence5841
      @paullawrence5841 ปีที่แล้ว +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

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

  • @chintancodewexy2933
    @chintancodewexy2933 ปีที่แล้ว +1

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

  • @kishorevb8331
    @kishorevb8331 ปีที่แล้ว

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

  • @codemuhammed
    @codemuhammed ปีที่แล้ว +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!

  • @MeredithGray-13
    @MeredithGray-13 13 วันที่ผ่านมา

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