Optimizing blockchain token purchase. Finding maximum value of a function.

210 Views Asked by At

I am working on smart contract on ethereum blockchain. The idea is that using some specified amount of money I have to buy maximum number of tokens from two different token smart contracts. Those two smart contracts calculate number of tokens to be sold using two different functions.

Contract 1 - C-ORG by Fairmint.

$$F1(x) = \sqrt{\frac{2x}{b} + a^2} - a$$

where b and a are constant values.

Contract 2 - Uniswap Token Traiding Contract.

$$F2(x) = \frac{x*d*997}{g*1000 + x * 997}$$

where d and g are constant.

What I need to do is split my aount of money I have between those two contracts so in total I can get maximum number of tokens possible from those two contracts combined.

So my understanding is that I have to find a maximum value of function $$F3(x) = F1(z) + F(x-z)$$ where z is amount of money spend on contract 1 and $z <= x$ and $z>0$

So I can say that $$F3(x) = \sqrt{\frac{2z}{b} + a^2} - a + \frac{(x-z)*d*997}{g*1000 + (x-z) * 997} $$

What I need to do is I have to find value of z, knowing value of x, for which this function will give maximum result.

I am not good in calculus so I used Integral-calculator and it came back with equation like this: $$\dfrac{1000dg\ln\left(\left|997\left(x-z\right)-1000g\right|\right)}{997}+\dfrac{\left(2x+a^2b\right)^\frac{3}{2}}{3\sqrt{b}}+dx-ax$$

And now I understand that I have to find value of z where this function will be equal zero. Right ? $$\dfrac{1000dg\ln\left(\left|997\left(x-z\right)-1000g\right|\right)}{997}+\dfrac{\left(2x+a^2b\right)^\frac{3}{2}}{3\sqrt{b}}+dx-ax = 0$$

I have following questions:

  1. Is my thinking correct. Should I do it this way ?
  2. Is result of calculus correct ?
  3. How I can be sure I found maximum and not minimum ?
  4. How I can simplify last function so it is easier to calculate. I am working in coding language ( solidity ) which only support basic mathematical functions (add,sub,mul,div and binary operations). I don't have anything for $\sqrt a$ or $\ln$

Any help will be more than appreciated. Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

To determine the maximums and/or minimums of a function, you should take the derivative of it, not the integral. As you basically stated, your function is

$$F3(z; x) = \sqrt{\frac{2z}{b} + a^2} - a + \frac{(x-z)*d*997}{g*1000 + (x-z) * 997} \tag{1}\label{eq1}$$

In this case, $x$ is some given amount and $z$ is your variable. Thus, you want to take the derivative of \eqref{eq1} with respect to $z$. Doing this gives

$$\begin{equation}\begin{aligned} \frac{d\left(F3(z; x)\right)}{dz} & = \frac{1}{b\sqrt{\frac{2z}{b} + a^2}} - \frac{997d}{1000g + 997(x-z)} + \frac{997^2d(x - z)}{\left(1000g + 997(x-z)\right)^2} \\ & = \frac{1}{b\sqrt{\frac{2z}{b} + a^2}} + \frac{-997d\left(1000g + 997(x-z)\right) + 997^2d(x - z)}{\left(1000g + 997(x-z)\right)^2} \\ & = \frac{1}{b\sqrt{\frac{2z}{b} + a^2}} - \frac{997d(1000g)}{\left(1000g + 997(x-z)\right)^2} \end{aligned}\end{equation}\tag{2}\label{eq2}$$

The maximum and minimum values will occur at the end-points, i.e., where $z = 0$ or $z = x$, or where the expression in \eqref{eq2} is $0$. As for determining the latter, you can equate both terms to each other, cross-multiply, square and collect the terms in $z$ together to get a quartic polynomial. In particular, first let $c = 997d(1000g)$ to make the algebra a bit simpler. You then get

$$\begin{equation}\begin{aligned} \frac{1}{b\sqrt{\frac{2z}{b} + a^2}} & = \frac{c}{\left(1000g + 997(x-z)\right)^2} \\ \left(1000g + 997(x-z)\right)^2 & = cb\sqrt{\frac{2z}{b} + a^2} \\ \left(1000g + 997(x-z)\right)^4 & = \left(cb\right)^2\left(\frac{2z}{b} + a^2\right) \end{aligned}\end{equation}\tag{3}\label{eq3}$$

I'll leave it to you to do the multiplications and collection of terms if you wish. As for finding its roots, there's actually an analytic solution, such as described in Wikipedia's Quartic function article, but it's somewhat long & messy. As an easier alternative, there are many root finding algorithms, such as described in Wikipedia's Root-finding algorithm article. I don't believe you'll need anything fancy, so even on those basic numerical methods will likely work well enough for you.

Regarding whether you have a maximum or minimum at some point, say $z_1$, you would normally check the second derivative at $z_1$, with it being negative meaning $f$ is a maximum at $z_1$, and it being positive meaning $f$ is a minimum at $z_1$. In your case, as there won't likely be an issue of these points being too close together, you may wish to just check the $f$ values at $z_1$, just a bit less than $z_1$, and just a bit more than $z_1$, to compare them & determine if it's a maximum or minimum.

As for handling the expression on a computer, there's apparently just the complication of determining the square root. Fortunately, there are relatively simple, but quite efficient, square root determination algorithms. For example, Wikipedia's Methods of computing square roots has some excellent suggestions, with likely even any fairly basic one they mention being sufficient for your needs.