Solving constant product for price

The constant product is used in decentralized finance. It is used to calculate how much someone gets for currency $Y$ when giving funds of currency $X$. $X$ and $Y$ indicate the size of funds in the corresponding currencies, $b$ stands for the amount of funds of currency $X$ taken from the pool, $a$ stands for the amount of funds one has to give in currency $Y$ in return. $p$ is the price paid for the transaction.

$k = X Y$
$a = (XY / (X-b)) - X$
$p = a/b$

For a two-step trade covering intermediate currency $T$ (The pool sizes can differ when $T$ is paired with different currencies, which is why there are $T_1$ and $T_2$), when buying amount $b$:

$k_2 = T_2Y$
$k_1 = XT_1$
$t = (T_2Y / (T_2 - b)) - T_2 $
$a = (X T_1 / (X - t)) - X $
$p = a/b$

My question is: How do I solve that second set of formulas, those using $T_1$ and $T_2$, for $b$? Meaning, how much do I need to take to get a specific price $p$?

Known variables are $X$, $T_1$, $T_2$, $Y$ and $p$.

  • Mathe Mathe
    0

    If p is known, why do you want to get a specific price p?

    • Maxbry Maxbry
      0

      I want to solve for b

    • Mathe Mathe
      0

      Notice that these are different equations compared to the last problem. Do you want me to try to answer it?

    • Maxbry Maxbry
      0

      It's like: The more I buy, the higher the price gets. And what I want to know is how much I can buy until the price reaches a specific value.

    • Maxbry Maxbry
      0

      Yes the equations are slightly different. The previous question was about how to much one can sell, this one is about how much one can buy. The previous one was from a to b, this is one from b to a, so it's backwards.

    • Mathe Mathe
      0

      These equations are slightly more difficult. Would you consider increasing the bounty?

    • Maxbry Maxbry
      0

      Yes, done.

Answer

Answers can only be viewed under the following conditions:
  1. The questioner was satisfied with and accepted the answer, or
  2. The answer was evaluated as being 100% correct by the judge.
View the answer

1 Attachment

Mathe Mathe
3.6K
The answer is accepted.
Join Matchmaticians Affiliate Marketing Program to earn up to a 50% commission on every question that your affiliated users ask or answer.