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, $a$ stands for the amount of funds of currency $X$ given into the pool, $b$ stands for the amount of funds received in currency $Y$. $p$ is the price paid for the transaction.

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

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$):

$k_1 = X * T_1$
$k_2 = T_2 * Y$
$t = T_1 - k_1 / (X + a)$
$b = Y - k_2 / (T_2 + t)$
$p = b/a$

My question is: How do I solve that formula for $a$? Meaning, how much do I have to give in order to reach a specific price for a

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

  • Mathe Mathe
    0

    What are the known values in these equations? Is it only a that is unknown?

  • Maxbry Maxbry
    0

    a and b are the only unknown

    • Mathe Mathe
      +1

      And you want to solve for a in the first set of equations of for the second set (with T1 and T2)

  • Maxbry Maxbry
    0

    For the second set with T1 and T2

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

3 Attachments

Mathe Mathe
3.6K
  • Maxbry Maxbry
    0

    Okay there may have been a misunderstanding: t is not known

  • Maxbry Maxbry
    0

    t is defined in relation to a

  • Maxbry Maxbry
    0

    I tried with these numbers: $X = 100$ $T1 = 100$ $T2 = 1000$ $Y = 1000$ $a = 10$ This gives me $b = 9.0081...$, which gives $p = 1.11011001...$. Using this value for $p$ and your solution gives me $a = -9.017125...$, which doesn't seem correct. Your solution's nominator is $(YT_1) - (pXT_2))$, entering my value means the second part larger than the first one, resulting in a negative solution.

    • Mathe Mathe
      0

      You said that $a$ is an unknown. $a$ cannot take any possible value. It's going to be bounded by these equations. You can give a value to $p$ and find what the corresponding value for $a$ is. As you said before, the "Known variables are X, Y, T1, T2 and p".

    • Maxbry Maxbry
      0

      Yes, but I tested: If I give I set $a$ as known, I will get $p$ and $b$. So the reverse should be possible: If I use the value that I got for $p$, I should get the same $a$.

    • Mathe Mathe
      0

      Well, I double checked my math and I think it's correct. This leads me to think that, to get meaningful solutions, X, Y, T1 , T2 can't take on arbritrary values. For example, for $a$ to be positive, it must be the case that $YT_1 - pXT_2$ is positive.

    • Maxbry Maxbry
      0

      Yes, I will double check myself. And yes, $a$ has to be positive

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.