# Untitled

a guest
Apr 9th, 2021
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class ModuleOne:
2.
3.     """
4.    Python model of  pool math.
5.    """
6.
7.     def __init__(self, A, D, n, p=None, tokens=None):
8.         """
9.        A: Amplification coefficient
10.        D: Total deposit size
11.        n: number of currencies
12.        p: target prices
13.        """
14.         self.A = A  # actually A * n ** (n - 1) because it's an invariant
15.         self.n = n
16.         self.fee = 10 ** 7
17.         if p:
18.             self.p = p
19.         else:
20.             self.p = [10 ** 18] * n
21.         if isinstance(D, list):
22.             self.x = D
23.         else:
24.             self.x = [D // n * 10 ** 18 // _p for _p in self.p]
25.         self.tokens = tokens
26.
27.     def xp(self):
28.         return [x * p // 10 ** 18 for x, p in zip(self.x, self.p)]
29.
30.     def D(self):
31.         """
32.        D invariant calculation in non-overflowing integer operations
33.        iteratively
34.
35.        A * sum(x_i) * n**n + D = A * D * n**n + D**(n+1) / (n**n * prod(x_i))
36.
37.        Converging solution:
38.        D[j+1] = (A * n**n * sum(x_i) - D[j]**(n+1) / (n**n prod(x_i))) / (A * n**n - 1)
39.        """
40.         Dprev = 0
41.         xp = self.xp()
42.         S = sum(xp)
43.         D = S
44.         Ann = self.A * self.n
45.         while abs(D - Dprev) > 1:
46.             D_P = D
47.             for x in xp:
48.                 D_P = D_P * D // (self.n * x)
49.             Dprev = D
50.             D = (Ann * S + D_P * self.n) * D // ((Ann - 1) * D + (self.n + 1) * D_P)
51.
52.         return D
53.
54.     def y(self, i, j, x):
55.         """
56.        Calculate x[j] if one makes x[i] = x
57.
58.        Done by solving quadratic equation iteratively.
59.        x_1**2 + x1 * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n+1)/(n ** (2 * n) * prod' * A)
60.        x_1**2 + b*x_1 = c
61.
62.        x_1 = (x_1**2 + c) / (2*x_1 + b)
63.        """
64.         D = self.D()
65.         xx = self.xp()
66.         xx[i] = x  # x is quantity of underlying asset brought to 1e18 precision
67.         xx = [xx[k] for k in range(self.n) if k != j]
68.         Ann = self.A * self.n
69.         c = D
70.         for y in xx:
71.             c = c * D // (y * self.n)
72.         c = c * D // (self.n * Ann)
73.         b = sum(xx) + D // Ann - D
74.         y_prev = 0
75.         y = D
76.         while abs(y - y_prev) > 1:
77.             y_prev = y
78.             y = (y ** 2 + c) // (2 * y + b)
79.         return y  # the result is in underlying units too
80.
81.     def y_D(self, i, _D):
82.         """
83.        Calculate x[j] if one makes x[i] = x
84.
85.        Done by solving quadratic equation iteratively.
86.        x_1**2 + x1 * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n+1)/(n ** (2 * n) * prod' * A)
87.        x_1**2 + b*x_1 = c
88.
89.        x_1 = (x_1**2 + c) / (2*x_1 + b)
90.        """
91.         xx = self.xp()
92.         xx = [xx[k] for k in range(self.n) if k != i]
93.         S = sum(xx)
94.         Ann = self.A * self.n
95.         c = _D
96.         for y in xx:
97.             c = c * _D // (y * self.n)
98.         c = c * _D // (self.n * Ann)
99.         b = S + _D // Ann
100.         y_prev = 0
101.         y = _D
102.         while abs(y - y_prev) > 1:
103.             y_prev = y
104.             y = (y ** 2 + c) // (2 * y + b - _D)
105.         return y  # the result is in underlying units too
106.
107.     def dy(self, i, j, dx):
108.         # dx and dy are in underlying units
109.         xp = self.xp()
110.         return xp[j] - self.y(i, j, xp[i] + dx)
111.
112.     def exchange(self, i, j, dx):
113.         xp = self.xp()
114.         x = xp[i] + dx
115.         y = self.y(i, j, x)
116.         dy = xp[j] - y
117.         fee = dy * self.fee // 10 ** 10
118.         assert dy > 0
119.         self.x[i] = x * 10 ** 18 // self.p[i]
120.         self.x[j] = (y + fee) * 10 ** 18 // self.p[j]
121.         return dy
122.
123.