Go up to the main HW page (md)
Let’s define some variables, all of which are known quantities.
- g is the gas fees in ETH
- pe is the price of ETH in USD
- pt is the price of TC in USD
- qe is how much ETH you have in your holdings (as a float: so 10.0 ETH, not 10000000000000000000 wei)
- qt is how much TC you have in your holdings (as a float: so 100.0 TC, not 1000000000000)
- xd, yd, and kd are the values from the DEX (respectively: ether liquidity, token liqudiity, and k); we’ll assume that these values are the floating-point values without all the extra decimals (so 1.5 for yt instead of 15000000000). Likewise, we’ll assume that kd = xd * yd, meaning that it too is scaled down without all those decimals.
- f is the percentage (out of 1.0) obtained after the DEX fees are removed – if fn is the fee numerator (say, 3) and fd is the fee denominator (say, 1000), then f = 1 − fn/fd. In this example, with fn = 3 and fd = 1000, f = 0.997. Note that this fee applies to both ETH and TC transactions.
The variable subscripts here all follow these rules:
- A subscript of t means it represents a value related to the TC (price, quantity, etc.)
- A subscript of e means it represents a value related to the ETH (price, quantity, etc.)
- A subscript of d means it represents a value of the DEX (x, y, k, etc.)
Your current holdings in USD are: hbefore = qe * pe + qt * pt
You have two options as to how to interact with a DEX: you could trade in either ETH or TC. We’ll define those amounts as δe and δt, depending on which we are trading in.
If you are trading δt TC to the DEX (note that δt must be positive):
- qt is decreased by δt; thus the amount of TC after, which we will represent by ωt, is:
- The value of the DEX’s xd after the transaction we’ll represent by x′d = kd/(yd+δt)
- The amount of ETH received: re = f * (xd−x′d)
- This amount is added to qe
- As TC was traded to the DEX, re will be positive, indicating a receipt of ETH, as expected
- The amount of ETH after (ωe) is thus:
- ωe = qe + re
- ωe = qe + f * (xd−x′d)
- ωe = qe + f * xd − f * kd/(yd+δt)
- However, we have to deduct the gas fees of g, so the amount of ETH after is then:
- ωe = qe + f * xd − f * kd/(yd+δt) − g
- We can then figure out our holdings after the transaction:
- hafter = ωe * pe + ωt * pt
- hafter = (qe+f*xd−f*kd/(yd+δt)−g) * pe + (qt−δt) * pt
- We’ll separate out the gas fees to make it simpler a bit later on:
- hafter = (qe+f*xd−f*kd/(yd+δt)) * pe + (qt−δt) * pt − g * pe
- We want to maximize this formula. The only variable that we can change is how much TC we are trading in (δt)
- To do this, we are going to have to take the derivative of that function
- Before we do, let’s put it into a reduced form: y = a + b/(c+x) + d * x = a + b * (x+c)−1 + d * x
- If we massage the above formula into the y = a + b/(c+x) + d * x form, we can derive what x, a, b, c, and d equal:
- x, the variable we are solving for, to x = δt
- a = qe * pe − g * pe + qt * pt + pe * f * xd
- b = − f * kd * pe
- c = yd
- d = − pt
- We take the derivative of that:
- To find the maxima, we set y′ = 0 and solve for x:
- y′ = 0 = − b * (x+c)−2 + d
- 0 = − b * (x+c)−2 + d
- − d = − b * (x+c)−2
- d = b * (x+c)−2
- d = b/(x+c)2
- d * (x+c)2 = b
- (x+c)2 = b/d
- x + c= √ (b/d)
- x = − c± √ (b/d)
- Recall that:
- x = δt
- b = − f * kd * pe
- c = yd
- d = − pt
- So we can rewrite that formula as:
- δt = − yd± √ (f*kd*pe/pt)
- That formula does not render well in HTML, but the entire parenthetical is what we are taking the square root of
- So once we know the DEX values (xd, yd, kd, and the fees f) and the current prices (pe and pt), we can compute the points for the maxima and minima
- Note that these are not guaranteed to make a profit! But if they do make a profit, one of them will make the most and/or least profit.
- If we are trading in ETH, the formula is similar: δe = − xd± √ (f*kd*pt/pe)
- Because the variables in the parenthetical can never be negative, the square root will always return real values
- We can simplify these equations somewhat. Although there is a ± (plus-minus), subtracting that value would make the overall δt or δe value negative, which doesn’t make sense in this case – what we derived is how much we are trading in to the DEX, not how much we are getting out of the DEX. So we will just replace the ± with a regular plus symbol.
- If we are trading in TC: δt = − yd+ √ (f*kd*pe/pt)
- If we are trading in ETH: δe = − xd+ √ (f*kd*pt/pe)
- As above, those formulas do not render well in HTML, but the entire parenthetical is what we are taking the square root of