Expression 29: "d" Subscript, "r" "a" "t" "i" "o" "a" "v" "a" "i" "l" "a" "b" "l" "e" "t" "o" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "p" Subscript, "n" "e" "x" "t" "t" "i" "c" "k" , Baseline , right parenthesisdratioavailabletodepositpnexttick
29
Expression 30: "d" Subscript, "r" "a" "t" "i" "o" "v" "a" "l" "i" "d" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "p" Subscript, "n" "e" "x" "t" "t" "i" "c" "k" , Baseline , right parenthesisdratiovaliddepositpnexttick
30
Since the ratio of assets available is still greater than the ratio that can be deposited, that means we need to trade to the next tick, let the liquidity change, and then repeat the first step. We do that over and over again until we find that d_ratioavailabletodeposit at the next tick is less than d_ratiovaliddeposit for it, which will mean that the target price is within the current tick.
31
Once we've reached the right tick, we then want to find the exact right p_target where trading to p_target will allow us to deposit all of our tokens into our desired range.
32
We have a formula below that computes the right value for p_target, but we can peek at the answer here (and its square root):
33
Expression 34: "x" equals "p" Subscript, "t" "a" "r" "g" "e" "t" , Baselinex=ptarget
34
Expression 35: "x" equals StartRoot, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , EndRootx=ptarget
35
We can use the same formulas to compute the ratio for valid deposits and the ratio available to deposit for p_target:
36
Expression 37: "d" Subscript, "r" "a" "t" "i" "o" "v" "a" "l" "i" "d" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , right parenthesisdratiovaliddepositptarget
37
Expression 38: "d" Subscript, "r" "a" "t" "i" "o" "a" "v" "a" "i" "l" "a" "b" "l" "e" "t" "o" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , right parenthesisdratioavailabletodepositptarget
38
To find the right value of p_target, we find the price where these curves meet—where the ratio of assets that the user has available to deposit to be equal to the ratio of assets that the user CAN deposit. (The "+0" is so Desmos doesn't treat this as a definition.)
39
Expression 40: "d" Subscript, "r" "a" "t" "i" "o" "v" "a" "l" "i" "d" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "x" , right parenthesis plus 0 equals "d" Subscript, "r" "a" "t" "i" "o" "a" "v" "a" "i" "l" "a" "b" "l" "e" "t" "o" "d" "e" "p" "o" "s" "i" "t" , Baseline left parenthesis, "x" , right parenthesisdratiovaliddepositx+0=dratioavailabletodepositx
40
Combining all of the above formulas gives us this equation:
41
Expression 42: StartFraction, StartRoot, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , EndRoot minus StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot Over StartNestedFraction, 1 NestedOver StartRoot, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , EndRoot , EndNestedFraction minus StartNestedFraction, 1 NestedOver StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndNestedFraction , EndFraction equals StartFraction, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus "g" times "l" left parenthesis, StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot minus StartRoot, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , EndRoot , right parenthesis Over "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus "l" left parenthesis, StartNestedFraction, 1 NestedOver StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndNestedFraction minus StartNestedFraction, 1 NestedOver StartRoot, "p" Subscript, "t" "a" "r" "g" "e" "t" , Baseline , EndRoot , EndNestedFraction , right parenthesis , EndFractionptarget−plower1ptarget−1pupper=ydeposit+g·lpstart−ptargetxdeposit+l1pstart−1ptarget
42
Let's replace sqrt(p_target) with x (so note that we are solving now for the square root of price rather than price), and expand:
43
Expression 44: StartFraction, "x" minus StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot Over StartNestedFraction, 1 NestedOver "x" , EndNestedFraction minus StartNestedFraction, 1 NestedOver StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndNestedFraction , EndFraction equals StartFraction, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot minus "g" "l" "x" Over "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus StartNestedFraction, "l" NestedOver StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndNestedFraction minus StartNestedFraction, "l" NestedOver "x" , EndNestedFraction , EndFractionx−plower1x−1pupper=ydeposit+glpstart−glxxdeposit+lpstart−lx
44
Expression 45: left parenthesis, "x" minus StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot , right parenthesis left parenthesis, "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus StartFraction, "l" Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction minus StartFraction, "l" Over "x" , EndFraction , right parenthesis equals left parenthesis, StartFraction, 1 Over "x" , EndFraction minus StartFraction, 1 Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction , right parenthesis left parenthesis, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline minus "g" "l" "x" plus "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , right parenthesisx−plowerxdeposit+lpstart−lx=1x−1pupperydeposit−glx+glpstart
45
Expression 46: "x" times "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus StartFraction, "x" "l" Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction minus "l" minus StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline minus StartFraction, "l" StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction plus StartFraction, "l" StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot Over "x" , EndFraction equals StartFraction, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline Over "x" , EndFraction minus "g" "l" plus StartFraction, "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot Over "x" , EndFraction minus StartFraction, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction plus StartFraction, "g" "l" "x" Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction minus StartFraction, "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFractionx·xdeposit+xlpstart−l−plowerxdeposit−lplowerpstart+lplowerx=ydepositx−gl+glpstartx−ydepositpupper+glxpupper−glpstartpupper
46
Expression 47: "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline times "x" squared plus StartFraction, "l" Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction "x" squared minus "l" "x" minus StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline "x" minus StartFraction, "l" StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction "x" plus "l" StartRoot, "p" Subscript, "l" "o" "w" "e" "r" , Baseline , EndRoot equals "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline minus "g" "l" "x" plus "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot minus StartFraction, "y" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction "x" plus StartFraction, "g" "l" Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction "x" squared minus StartFraction, "g" "l" StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFraction "x"xdeposit·x2+lpstartx2−lx−plowerxdepositx−lplowerpstartx+lplower=ydeposit−glx+glpstart−ydepositpupperx+glpupperx2−glpstartpupperx
47
Then rearrange to make it a quadratic equation, and use the quadratic formula to solve for x.
48
Expression 49: "a" equals "x" Subscript, "d" "e" "p" "o" "s" "i" "t" , Baseline plus StartFraction, "l" Over StartRoot, "p" Subscript, "s" "t" "a" "r" "t" , Baseline , EndRoot , EndFraction minus StartFraction, "g" "l" Over StartRoot, "p" Subscript, "u" "p" "p" "e" "r" , Baseline , EndRoot , EndFractiona=xdeposit+lpstart−glpupper
Therefore, in order to compute the target price, first compute a, b, and c as per the formulas above, plug it into the quadratic formula, and then square it.