@marko00/routing-finder-mare 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/.idea/copilot.data.migration.agent.xml +6 -0
  2. package/.idea/copilot.data.migration.ask.xml +6 -0
  3. package/.idea/copilot.data.migration.ask2agent.xml +6 -0
  4. package/.idea/copilot.data.migration.edit.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/php.xml +19 -0
  7. package/.idea/ratex-sdk.iml +8 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/LICENSE-MIT +21 -0
  10. package/README.md +209 -0
  11. package/contracts/abi/BalancerHelperAbi.ts +1 -0
  12. package/contracts/abi/CamelotHelperAbi.ts +1 -0
  13. package/contracts/abi/RateXAbi.ts +482 -0
  14. package/contracts/abi/SushiSwapHelperAbi.ts +1 -0
  15. package/contracts/abi/UniswapHelperAbi.ts +1 -0
  16. package/contracts/abi/UniswapV2HelperAbi.ts +1 -0
  17. package/contracts/addresses-arbitrum.ts +8 -0
  18. package/contracts/addresses-mainnet.ts +7 -0
  19. package/contracts/addresses-polkadot.ts +9 -0
  20. package/contracts/addresses-sei.ts +9 -0
  21. package/contracts/rateX/BalancerHelper.ts +13 -0
  22. package/contracts/rateX/CamelotHelper.ts +13 -0
  23. package/contracts/rateX/SushiSwapHelper.ts +13 -0
  24. package/contracts/rateX/UniswapHelper.ts +19 -0
  25. package/contracts/rateX/UniswapV2Helper.ts +19 -0
  26. package/dexes/dexIdsList.ts +9 -0
  27. package/dexes/graph_queries/BalancerV2.ts +150 -0
  28. package/dexes/graph_queries/CamelotV2.ts +202 -0
  29. package/dexes/graph_queries/SushiSwapV2.ts +283 -0
  30. package/dexes/graph_queries/UniswapV2.ts +252 -0
  31. package/dexes/graph_queries/UniswapV3.ts +229 -0
  32. package/dexes/graph_queries/graphQueryFilters.ts +41 -0
  33. package/dexes/graph_queries/x_template.ts +67 -0
  34. package/dexes/pools/Balancer/BalancerState.ts +34 -0
  35. package/dexes/pools/Balancer/BalancerWeightedPool.ts +96 -0
  36. package/dexes/pools/Camelot.ts +164 -0
  37. package/dexes/pools/SushiSwapV2.ts +35 -0
  38. package/dexes/pools/UniswapV2.ts +36 -0
  39. package/dexes/pools/uniswap/UniswapV3.ts +40 -0
  40. package/dexes/pools/uniswap/testUniswapOffchainQuoter.ts +169 -0
  41. package/dexes/pools/uniswap/types.ts +174 -0
  42. package/dexes/pools/uniswap/uniswapOffchainQuoter.ts +173 -0
  43. package/dexes/pools/uniswap/uniswapState.ts +56 -0
  44. package/dexes/pools/uniswap/utils.ts +71 -0
  45. package/dist/contracts/abi/BalancerHelperAbi.d.ts +25 -0
  46. package/dist/contracts/abi/BalancerHelperAbi.js +4 -0
  47. package/dist/contracts/abi/CamelotHelperAbi.d.ts +45 -0
  48. package/dist/contracts/abi/CamelotHelperAbi.js +4 -0
  49. package/dist/contracts/abi/RateXAbi.d.ts +71 -0
  50. package/dist/contracts/abi/RateXAbi.js +485 -0
  51. package/dist/contracts/abi/SushiSwapHelperAbi.d.ts +45 -0
  52. package/dist/contracts/abi/SushiSwapHelperAbi.js +4 -0
  53. package/dist/contracts/abi/UniswapHelperAbi.d.ts +39 -0
  54. package/dist/contracts/abi/UniswapHelperAbi.js +4 -0
  55. package/dist/contracts/abi/UniswapV2HelperAbi.d.ts +45 -0
  56. package/dist/contracts/abi/UniswapV2HelperAbi.js +4 -0
  57. package/dist/contracts/addresses-arbitrum.d.ts +6 -0
  58. package/dist/contracts/addresses-arbitrum.js +10 -0
  59. package/dist/contracts/addresses-mainnet.d.ts +6 -0
  60. package/dist/contracts/addresses-mainnet.js +10 -0
  61. package/dist/contracts/addresses-polkadot.d.ts +6 -0
  62. package/dist/contracts/addresses-polkadot.js +10 -0
  63. package/dist/contracts/addresses-sei.d.ts +6 -0
  64. package/dist/contracts/addresses-sei.js +10 -0
  65. package/dist/contracts/rateX/BalancerHelper.d.ts +26 -0
  66. package/dist/contracts/rateX/BalancerHelper.js +14 -0
  67. package/dist/contracts/rateX/CamelotHelper.d.ts +46 -0
  68. package/dist/contracts/rateX/CamelotHelper.js +14 -0
  69. package/dist/contracts/rateX/SushiSwapHelper.d.ts +46 -0
  70. package/dist/contracts/rateX/SushiSwapHelper.js +14 -0
  71. package/dist/contracts/rateX/UniswapHelper.d.ts +40 -0
  72. package/dist/contracts/rateX/UniswapHelper.js +22 -0
  73. package/dist/contracts/rateX/UniswapV2Helper.d.ts +46 -0
  74. package/dist/contracts/rateX/UniswapV2Helper.js +22 -0
  75. package/dist/dexes/dexIdsList.d.ts +9 -0
  76. package/dist/dexes/dexIdsList.js +12 -0
  77. package/dist/dexes/graph_queries/BalancerV2.d.ts +14 -0
  78. package/dist/dexes/graph_queries/BalancerV2.js +141 -0
  79. package/dist/dexes/graph_queries/CamelotV2.d.ts +14 -0
  80. package/dist/dexes/graph_queries/CamelotV2.js +183 -0
  81. package/dist/dexes/graph_queries/SushiSwapV2.d.ts +14 -0
  82. package/dist/dexes/graph_queries/SushiSwapV2.js +263 -0
  83. package/dist/dexes/graph_queries/UniswapV2.d.ts +14 -0
  84. package/dist/dexes/graph_queries/UniswapV2.js +217 -0
  85. package/dist/dexes/graph_queries/UniswapV3.d.ts +14 -0
  86. package/dist/dexes/graph_queries/UniswapV3.js +198 -0
  87. package/dist/dexes/graph_queries/graphQueryFilters.d.ts +19 -0
  88. package/dist/dexes/graph_queries/graphQueryFilters.js +40 -0
  89. package/dist/dexes/graph_queries/x_template.d.ts +12 -0
  90. package/dist/dexes/graph_queries/x_template.js +57 -0
  91. package/dist/dexes/pools/Balancer/BalancerState.d.ts +6 -0
  92. package/dist/dexes/pools/Balancer/BalancerState.js +32 -0
  93. package/dist/dexes/pools/Balancer/BalancerWeightedPool.d.ts +12 -0
  94. package/dist/dexes/pools/Balancer/BalancerWeightedPool.js +109 -0
  95. package/dist/dexes/pools/Camelot.d.ts +12 -0
  96. package/dist/dexes/pools/Camelot.js +135 -0
  97. package/dist/dexes/pools/SushiSwapV2.d.ts +9 -0
  98. package/dist/dexes/pools/SushiSwapV2.js +34 -0
  99. package/dist/dexes/pools/UniswapV2.d.ts +9 -0
  100. package/dist/dexes/pools/UniswapV2.js +34 -0
  101. package/dist/dexes/pools/uniswap/UniswapV3.d.ts +7 -0
  102. package/dist/dexes/pools/uniswap/UniswapV3.js +36 -0
  103. package/dist/dexes/pools/uniswap/types.d.ts +76 -0
  104. package/dist/dexes/pools/uniswap/types.js +111 -0
  105. package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.d.ts +13 -0
  106. package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.js +121 -0
  107. package/dist/dexes/pools/uniswap/uniswapState.d.ts +14 -0
  108. package/dist/dexes/pools/uniswap/uniswapState.js +51 -0
  109. package/dist/dexes/pools/uniswap/utils.d.ts +3 -0
  110. package/dist/dexes/pools/uniswap/utils.js +41 -0
  111. package/dist/index.d.ts +26 -0
  112. package/dist/index.js +41 -0
  113. package/dist/routes.d.ts +1 -0
  114. package/dist/routes.js +20 -0
  115. package/dist/routing/iterative_spliting/main.d.ts +3 -0
  116. package/dist/routing/iterative_spliting/main.js +104 -0
  117. package/dist/routing/iterative_spliting/multiHopSwap.d.ts +4 -0
  118. package/dist/routing/iterative_spliting/multiHopSwap.js +83 -0
  119. package/dist/routing/main.d.ts +2 -0
  120. package/dist/routing/main.js +22 -0
  121. package/dist/routing/uni_like_algo/algo_config.d.ts +2 -0
  122. package/dist/routing/uni_like_algo/algo_config.js +8 -0
  123. package/dist/routing/uni_like_algo/amount_distribution.d.ts +2 -0
  124. package/dist/routing/uni_like_algo/amount_distribution.js +17 -0
  125. package/dist/routing/uni_like_algo/compute_routes_backtrack.d.ts +3 -0
  126. package/dist/routing/uni_like_algo/compute_routes_backtrack.js +44 -0
  127. package/dist/routing/uni_like_algo/main.d.ts +2 -0
  128. package/dist/routing/uni_like_algo/main.js +49 -0
  129. package/dist/routing/uni_like_algo/routes_quoter.d.ts +21 -0
  130. package/dist/routing/uni_like_algo/routes_quoter.js +53 -0
  131. package/dist/routing/uni_like_algo/swap_finder.d.ts +25 -0
  132. package/dist/routing/uni_like_algo/swap_finder.js +154 -0
  133. package/dist/routing/uni_like_algo/types.d.ts +40 -0
  134. package/dist/routing/uni_like_algo/types.js +12 -0
  135. package/dist/swap/graph_communication.d.ts +5 -0
  136. package/dist/swap/graph_communication.js +187 -0
  137. package/dist/swap/my_local_storage.d.ts +8 -0
  138. package/dist/swap/my_local_storage.js +16 -0
  139. package/dist/utils/addresses.d.ts +24 -0
  140. package/dist/utils/addresses.js +60 -0
  141. package/dist/utils/math/fixed-points.d.ts +14 -0
  142. package/dist/utils/math/fixed-points.js +123 -0
  143. package/dist/utils/math/log-exp.d.ts +5 -0
  144. package/dist/utils/math/log-exp.js +385 -0
  145. package/dist/utils/math/math.d.ts +12 -0
  146. package/dist/utils/math/math.js +50 -0
  147. package/dist/utils/types/types.d.ts +51 -0
  148. package/dist/utils/types/types.js +25 -0
  149. package/dist/utils/utils.d.ts +20 -0
  150. package/dist/utils/utils.js +72 -0
  151. package/images/decenter_logo.png +0 -0
  152. package/index.ts +50 -0
  153. package/package.json +39 -0
  154. package/routes.ts +27 -0
  155. package/routing/iterative_spliting/main.ts +131 -0
  156. package/routing/iterative_spliting/multiHopSwap.ts +98 -0
  157. package/routing/main.ts +22 -0
  158. package/routing/uni_like_algo/algo_config.ts +7 -0
  159. package/routing/uni_like_algo/amount_distribution.ts +16 -0
  160. package/routing/uni_like_algo/compute_routes_backtrack.ts +81 -0
  161. package/routing/uni_like_algo/main.ts +65 -0
  162. package/routing/uni_like_algo/routes_quoter.ts +63 -0
  163. package/routing/uni_like_algo/swap_finder.ts +185 -0
  164. package/routing/uni_like_algo/types.ts +54 -0
  165. package/swap/graph_communication.ts +212 -0
  166. package/swap/my_local_storage.ts +27 -0
  167. package/tsconfig.json +26 -0
  168. package/utils/addresses.ts +64 -0
  169. package/utils/math/fixed-points.ts +88 -0
  170. package/utils/math/log-exp.ts +469 -0
  171. package/utils/math/math.ts +46 -0
  172. package/utils/types/types.ts +100 -0
  173. package/utils/utils.ts +125 -0
@@ -0,0 +1,469 @@
1
+ // Ported from Solidity:
2
+ // https://github.com/balancer-labs/balancer-core-v2/blob/70843e6a61ad11208c1cfabf5cfe15be216ca8d3/pkg/solidity-utils/contracts/math/LogExpMath.sol
3
+
4
+ import BigNumber from "bignumber.js"
5
+
6
+ // All fixed point multiplications and divisions are inlined
7
+ // This means we need to divide by ONE when multiplying two numbers, and multiply by ONE when dividing them
8
+
9
+ // All arguments and return values are 18 decimal fixed point numbers
10
+ const ONE_18 = new BigNumber("1000000000000000000"); // 1e18
11
+
12
+ // Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the case of ln36, 36 decimals
13
+ const ONE_20 = new BigNumber("100000000000000000000"); // 1e20
14
+ const ONE_36 = new BigNumber("1000000000000000000000000000000000000"); // 1e36
15
+
16
+ // The domain of natural exponentiation is bound by the word size and number of decimals used
17
+ // Because internally the result will be stored using 20 decimals, the largest possible result is
18
+ // (2^255 - 1) / 10^20, which makes the largest exponent ln((2^255 - 1) / 10^20) = 130.700829182905140221
19
+ // The smallest possible result is 10^(-18), which makes largest negative argument
20
+ // ln(10^(-18)) = -41.446531673892822312.
21
+ // We use 130.0 and -41.0 to have some safety margin
22
+ const MAX_NATURAL_EXPONENT = new BigNumber("130000000000000000000"); // 130e18
23
+ const MIN_NATURAL_EXPONENT = new BigNumber("-41000000000000000000"); // (-41)e18
24
+
25
+ // Bounds for ln_36's argument
26
+ // Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point 256 bit integer
27
+ const LN_36_LOWER_BOUND = ONE_18.minus(new BigNumber("100000000000000000")); // 1e18 - 1e17
28
+ const LN_36_UPPER_BOUND = ONE_18.plus(new BigNumber("100000000000000000")); // 1e18 + 1e17
29
+
30
+ const MILD_EXPONENT_BOUND = new BigNumber(2).pow(254).idiv(ONE_20);
31
+
32
+ // 18 decimal constants
33
+ const x0 = new BigNumber("128000000000000000000"); // 2ˆ7
34
+ const a0 = new BigNumber("38877084059945950922200000000000000000000000000000000000"); // eˆ(x0) (no decimals)
35
+ const x1 = new BigNumber("64000000000000000000"); // 2ˆ6
36
+ const a1 = new BigNumber("6235149080811616882910000000"); // eˆ(x1) (no decimals)
37
+
38
+ // 20 decimal constants
39
+ const x2 = new BigNumber("3200000000000000000000"); // 2ˆ5
40
+ const a2 = new BigNumber("7896296018268069516100000000000000"); // eˆ(x2)
41
+ const x3 = new BigNumber("1600000000000000000000"); // 2ˆ4
42
+ const a3 = new BigNumber("888611052050787263676000000"); // eˆ(x3)
43
+ const x4 = new BigNumber("800000000000000000000"); // 2ˆ3
44
+ const a4 = new BigNumber("298095798704172827474000"); // eˆ(x4)
45
+ const x5 = new BigNumber("400000000000000000000"); // 2ˆ2
46
+ const a5 = new BigNumber("5459815003314423907810"); // eˆ(x5)
47
+ const x6 = new BigNumber("200000000000000000000"); // 2ˆ1
48
+ const a6 = new BigNumber("738905609893065022723"); // eˆ(x6)
49
+ const x7 = new BigNumber("100000000000000000000"); // 2ˆ0
50
+ const a7 = new BigNumber("271828182845904523536"); // eˆ(x7)
51
+ const x8 = new BigNumber("50000000000000000000"); // 2ˆ(-1)
52
+ const a8 = new BigNumber("164872127070012814685"); // eˆ(x8)
53
+ const x9 = new BigNumber("25000000000000000000"); // 2ˆ(-2)
54
+ const a9 = new BigNumber("128402541668774148407"); // eˆ(x9)
55
+ const x10 = new BigNumber("12500000000000000000"); // 2ˆ(-3)
56
+ const a10 = new BigNumber("113314845306682631683"); // eˆ(x10)
57
+ const x11 = new BigNumber("6250000000000000000"); // 2ˆ(-4)
58
+ const a11 = new BigNumber("106449445891785942956"); // eˆ(x11)
59
+
60
+ export const pow = (x: BigNumber, y: BigNumber): BigNumber => {
61
+ if (y.isZero()) {
62
+ // We solve the 0^0 indetermination by making it equal one.
63
+ return ONE_18;
64
+ }
65
+
66
+ if (x.isZero()) {
67
+ return new BigNumber(0);
68
+ }
69
+
70
+ // Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
71
+ // arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
72
+ // x^y = exp(y * ln(x)).
73
+
74
+ // The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
75
+ if (x.gte(new BigNumber(2).pow(255))) {
76
+ throw new Error("X_OUT_OF_BOUNDS");
77
+ }
78
+
79
+ // We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
80
+ // both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
81
+
82
+ // This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
83
+ if (y.gte(MILD_EXPONENT_BOUND)) {
84
+ throw new Error("Y_OUT_OF_BOUNDS");
85
+ }
86
+
87
+ let logx_times_y: BigNumber;
88
+ if (LN_36_LOWER_BOUND.lt(x) && x.lt(LN_36_UPPER_BOUND)) {
89
+ let ln_36_x = _ln_36(x);
90
+
91
+ // ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
92
+ // bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
93
+ // multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
94
+ // (downscaled) last 18 decimals.
95
+ logx_times_y = ln_36_x
96
+ .idiv(ONE_18)
97
+ .times(y)
98
+ .plus(ln_36_x.mod(ONE_18).times(y).idiv(ONE_18));
99
+ } else {
100
+ logx_times_y = _ln(x).times(y);
101
+ }
102
+ logx_times_y = logx_times_y.idiv(ONE_18);
103
+
104
+ // Finally, we compute exp(y * ln(x)) to arrive at x^y
105
+ if (
106
+ logx_times_y.lt(MIN_NATURAL_EXPONENT) ||
107
+ logx_times_y.gt(MAX_NATURAL_EXPONENT)
108
+ ) {
109
+ throw new Error("PRODUCT_OUT_OF_BOUNDS");
110
+ }
111
+
112
+ return exp(logx_times_y);
113
+ };
114
+
115
+ export const exp = (x: BigNumber): BigNumber => {
116
+ if (x.lt(MIN_NATURAL_EXPONENT) || x.gt(MAX_NATURAL_EXPONENT)) {
117
+ throw new Error("INVALID_EXPONENT");
118
+ }
119
+
120
+ if (x.lt(0)) {
121
+ // We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
122
+ // fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
123
+ // Fixed point division requires multiplying by ONE_18.
124
+ return ONE_18.times(ONE_18).idiv(exp(x.negated()));
125
+ }
126
+
127
+ // First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
128
+ // where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
129
+ // because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
130
+ // decomposition.
131
+ // At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
132
+ // decomposition, which will be lower than the smallest x_n.
133
+ // exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
134
+ // We mutate x by subtracting x_n, making it the remainder of the decomposition.
135
+
136
+ // The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
137
+ // intermediate overflows. Instead we store them as plain integers, with 0 decimals.
138
+ // Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
139
+ // decomposition.
140
+
141
+ // For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
142
+ // it and compute the accumulated product.
143
+
144
+ let firstAN: BigNumber;
145
+ if (x.gte(x0)) {
146
+ x = x.minus(x0);
147
+ firstAN = a0;
148
+ } else if (x.gte(x1)) {
149
+ x = x.minus(x1);
150
+ firstAN = a1;
151
+ } else {
152
+ firstAN = new BigNumber(1); // One with no decimal places
153
+ }
154
+
155
+ // We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
156
+ // smaller terms.
157
+ x = x.times(100);
158
+
159
+ // `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
160
+ // one. Recall that fixed point multiplication requires dividing by ONE_20.
161
+ let product = ONE_20;
162
+
163
+ if (x.gte(x2)) {
164
+ x = x.minus(x2);
165
+ product = product.times(a2).idiv(ONE_20);
166
+ }
167
+ if (x.gte(x3)) {
168
+ x = x.minus(x3);
169
+ product = product.times(a3).idiv(ONE_20);
170
+ }
171
+ if (x.gte(x4)) {
172
+ x = x.minus(x4);
173
+ product = product.times(a4).idiv(ONE_20);
174
+ }
175
+ if (x.gte(x5)) {
176
+ x = x.minus(x5);
177
+ product = product.times(a5).idiv(ONE_20);
178
+ }
179
+ if (x.gte(x6)) {
180
+ x = x.minus(x6);
181
+ product = product.times(a6).idiv(ONE_20);
182
+ }
183
+ if (x.gte(x7)) {
184
+ x = x.minus(x7);
185
+ product = product.times(a7).idiv(ONE_20);
186
+ }
187
+ if (x.gte(x8)) {
188
+ x = x.minus(x8);
189
+ product = product.times(a8).idiv(ONE_20);
190
+ }
191
+ if (x.gte(x9)) {
192
+ x = x.minus(x9);
193
+ product = product.times(a9).idiv(ONE_20);
194
+ }
195
+
196
+ // x10 and x11 are unnecessary here since we have high enough precision already.
197
+
198
+ // Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
199
+ // expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
200
+
201
+ let seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places.
202
+ let term: BigNumber; // Each term in the sum, where the nth term is (x^n / n!).
203
+
204
+ // The first term is simply x.
205
+ term = x;
206
+ seriesSum = seriesSum.plus(term);
207
+
208
+ // Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
209
+ // multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
210
+
211
+ term = term.times(x).idiv(ONE_20).idiv(2);
212
+ seriesSum = seriesSum.plus(term);
213
+
214
+ term = term.times(x).idiv(ONE_20).idiv(3);
215
+ seriesSum = seriesSum.plus(term);
216
+
217
+ term = term.times(x).idiv(ONE_20).idiv(4);
218
+ seriesSum = seriesSum.plus(term);
219
+
220
+ term = term.times(x).idiv(ONE_20).idiv(5);
221
+ seriesSum = seriesSum.plus(term);
222
+
223
+ term = term.times(x).idiv(ONE_20).idiv(6);
224
+ seriesSum = seriesSum.plus(term);
225
+
226
+ term = term.times(x).idiv(ONE_20).idiv(7);
227
+ seriesSum = seriesSum.plus(term);
228
+
229
+ term = term.times(x).idiv(ONE_20).idiv(8);
230
+ seriesSum = seriesSum.plus(term);
231
+
232
+ term = term.times(x).idiv(ONE_20).idiv(9);
233
+ seriesSum = seriesSum.plus(term);
234
+
235
+ term = term.times(x).idiv(ONE_20).idiv(10);
236
+ seriesSum = seriesSum.plus(term);
237
+
238
+ term = term.times(x).idiv(ONE_20).idiv(11);
239
+ seriesSum = seriesSum.plus(term);
240
+
241
+ term = term.times(x).idiv(ONE_20).idiv(12);
242
+ seriesSum = seriesSum.plus(term);
243
+
244
+ // 12 Taylor terms are sufficient for 18 decimal precision.
245
+
246
+ // We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
247
+ // approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
248
+ // all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
249
+ // and then drop two digits to return an 18 decimal value.
250
+
251
+ return product.times(seriesSum).idiv(ONE_20).times(firstAN).idiv(100);
252
+ };
253
+
254
+ export const log = (arg: BigNumber, base: BigNumber): BigNumber => {
255
+ // This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
256
+
257
+ // Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
258
+ // upscaling.
259
+
260
+ let logBase: BigNumber;
261
+ if (LN_36_LOWER_BOUND.lt(base) && base.lt(LN_36_UPPER_BOUND)) {
262
+ logBase = _ln_36(base);
263
+ } else {
264
+ logBase = _ln(base).times(ONE_18);
265
+ }
266
+
267
+ let logArg: BigNumber;
268
+ if (LN_36_LOWER_BOUND.lt(arg) && arg.lt(LN_36_UPPER_BOUND)) {
269
+ logArg = _ln_36(arg);
270
+ } else {
271
+ logArg = _ln(arg).times(ONE_18);
272
+ }
273
+
274
+ // When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
275
+ return logArg.times(ONE_18).idiv(logBase);
276
+ };
277
+
278
+ export const ln = (a: BigNumber): BigNumber => {
279
+ // The real natural logarithm is not defined for negative numbers or zero.
280
+ if (a.lte(0)) {
281
+ throw new Error("OUT_OF_BOUNDS");
282
+ }
283
+ if (LN_36_LOWER_BOUND.lt(a) && a.lt(LN_36_UPPER_BOUND)) {
284
+ return _ln_36(a).idiv(ONE_18);
285
+ } else {
286
+ return _ln(a);
287
+ }
288
+ };
289
+
290
+ const _ln = (a: BigNumber): BigNumber => {
291
+ if (a.lt(ONE_18)) {
292
+ // Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a))
293
+ // If a is less than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call
294
+ // Fixed point division requires multiplying by ONE_18
295
+ return _ln(ONE_18.times(ONE_18).idiv(a)).negated();
296
+ }
297
+
298
+ // First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
299
+ // we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
300
+ // ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
301
+ // be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
302
+ // At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
303
+ // decomposition, which will be lower than the smallest a_n.
304
+ // ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1
305
+ // We mutate a by subtracting a_n, making it the remainder of the decomposition
306
+
307
+ // For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
308
+ // numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
309
+ // ONE_18 to convert them to fixed point.
310
+ // For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
311
+ // by it and compute the accumulated sum.
312
+
313
+ let sum = new BigNumber(0)
314
+ if (a.gte(a0.times(ONE_18))) {
315
+ a = a.idiv(a0); // Integer, not fixed point division
316
+ sum = sum.plus(x0);
317
+ }
318
+
319
+ if (a.gte(a1.times(ONE_18))) {
320
+ a = a.idiv(a1); // Integer, not fixed point division
321
+ sum = sum.plus(x1);
322
+ }
323
+
324
+ // All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
325
+ sum = sum.times(100);
326
+ a = a.times(100);
327
+
328
+ // Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
329
+
330
+ if (a.gte(a2)) {
331
+ a = a.times(ONE_20).idiv(a2);
332
+ sum = sum.plus(x2);
333
+ }
334
+
335
+ if (a.gte(a3)) {
336
+ a = a.times(ONE_20).idiv(a3);
337
+ sum = sum.plus(x3);
338
+ }
339
+
340
+ if (a.gte(a4)) {
341
+ a = a.times(ONE_20).idiv(a4);
342
+ sum = sum.plus(x4);
343
+ }
344
+
345
+ if (a.gte(a5)) {
346
+ a = a.times(ONE_20).idiv(a5);
347
+ sum = sum.plus(x5);
348
+ }
349
+
350
+ if (a.gte(a6)) {
351
+ a = a.times(ONE_20).idiv(a6);
352
+ sum = sum.plus(x6);
353
+ }
354
+
355
+ if (a.gte(a7)) {
356
+ a = a.times(ONE_20).idiv(a7);
357
+ sum = sum.plus(x7);
358
+ }
359
+
360
+ if (a.gte(a8)) {
361
+ a = a.times(ONE_20).idiv(a8);
362
+ sum = sum.plus(x8);
363
+ }
364
+
365
+ if (a.gte(a9)) {
366
+ a = a.times(ONE_20).idiv(a9);
367
+ sum = sum.plus(x9);
368
+ }
369
+
370
+ if (a.gte(a10)) {
371
+ a = a.times(ONE_20).idiv(a10);
372
+ sum = sum.plus(x10);
373
+ }
374
+
375
+ if (a.gte(a11)) {
376
+ a = a.times(ONE_20).idiv(a11);
377
+ sum = sum.plus(x11);
378
+ }
379
+
380
+ // a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
381
+ // that converges rapidly for values of `a` close to one - the same one used in ln_36.
382
+ // Let z = (a - 1) / (a + 1).
383
+ // ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
384
+
385
+ // Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
386
+ // division by ONE_20.
387
+ const z = a.minus(ONE_20).times(ONE_20).idiv(a.plus(ONE_20));
388
+ const z_squared = z.times(z).idiv(ONE_20);
389
+
390
+ // num is the numerator of the series: the z^(2 * n + 1) term
391
+ let num = z;
392
+
393
+ // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
394
+ let seriesSum = num;
395
+
396
+ // In each step, the numerator is multiplied by z^2
397
+ num = num.times(z_squared).idiv(ONE_20);
398
+ seriesSum = seriesSum.plus(num.idiv(3));
399
+
400
+ num = num.times(z_squared).idiv(ONE_20);
401
+ seriesSum = seriesSum.plus(num.idiv(5));
402
+
403
+ num = num.times(z_squared).idiv(ONE_20);
404
+ seriesSum = seriesSum.plus(num.idiv(7));
405
+
406
+ num = num.times(z_squared).idiv(ONE_20);
407
+ seriesSum = seriesSum.plus(num.idiv(9));
408
+
409
+ num = num.times(z_squared).idiv(ONE_20);
410
+ seriesSum = seriesSum.plus(num.idiv(11));
411
+
412
+ // 6 Taylor terms are sufficient for 36 decimal precision.
413
+
414
+ // Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
415
+ seriesSum = seriesSum.times(2);
416
+
417
+ // We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
418
+ // with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
419
+ // value.
420
+
421
+ return sum.plus(seriesSum).idiv(100);
422
+ };
423
+
424
+ const _ln_36 = (x: BigNumber): BigNumber => {
425
+ // Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits worthwhile
426
+
427
+ // First, we transform x to a 36 digit fixed point value
428
+ x = x.times(ONE_18);
429
+
430
+ // We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1)
431
+ // ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
432
+
433
+ // Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires division by ONE_36
434
+ const z = x.minus(ONE_36).times(ONE_36).idiv(x.plus(ONE_36));
435
+ const z_squared = z.times(z).idiv(ONE_36);
436
+
437
+ // num is the numerator of the series: the z^(2 * n + 1) term
438
+ let num = z;
439
+
440
+ // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
441
+ let seriesSum = num;
442
+
443
+ // In each step, the numerator is multiplied by z^2
444
+ num = num.times(z_squared).idiv(ONE_36);
445
+ seriesSum = seriesSum.plus(num.idiv(3));
446
+
447
+ num = num.times(z_squared).idiv(ONE_36);
448
+ seriesSum = seriesSum.plus(num.idiv(5));
449
+
450
+ num = num.times(z_squared).idiv(ONE_36);
451
+ seriesSum = seriesSum.plus(num.idiv(7));
452
+
453
+ num = num.times(z_squared).idiv(ONE_36);
454
+ seriesSum = seriesSum.plus(num.idiv(9));
455
+
456
+ num = num.times(z_squared).idiv(ONE_36);
457
+ seriesSum = seriesSum.plus(num.idiv(11));
458
+
459
+ num = num.times(z_squared).idiv(ONE_36);
460
+ seriesSum = seriesSum.plus(num.idiv(13));
461
+
462
+ num = num.times(z_squared).idiv(ONE_36);
463
+ seriesSum = seriesSum.plus(num.idiv(15));
464
+
465
+ // 8 Taylor terms are sufficient for 36 decimal precision
466
+
467
+ // All that remains is multiplying by 2 (non fixed point)
468
+ return seriesSum.times(2);
469
+ };
@@ -0,0 +1,46 @@
1
+ // Ported from Solidity:
2
+ // https://github.com/balancer-labs/balancer-v2-monorepo/blob/ce70f7663e0ac94b25ed60cb86faaa8199fd9e13/pkg/solidity-utils/contracts/math/Math.sol
3
+
4
+ import BigNumber from "bignumber.js"
5
+
6
+ export const ZERO = new BigNumber(0)
7
+ export const ONE = new BigNumber(1)
8
+ export const TWO = new BigNumber(2)
9
+
10
+ export const add = (a: BigNumber, b: BigNumber): BigNumber => {
11
+ return a.plus(b)
12
+ }
13
+
14
+ export const sub = (a: BigNumber, b: BigNumber): BigNumber => {
15
+ if (b.gt(a))
16
+ throw new Error("SUB_OVERFLOW")
17
+ return a.minus(b)
18
+ }
19
+
20
+ export const max = (a: BigNumber, b: BigNumber): BigNumber => {
21
+ return a.gte(b) ? a : b
22
+ }
23
+
24
+ export const min = (a: BigNumber, b: BigNumber): BigNumber => {
25
+ return a.lt(b) ? a : b
26
+ }
27
+
28
+ export const mul = (a: BigNumber, b: BigNumber): BigNumber => {
29
+ return a.times(b)
30
+ }
31
+
32
+ export const div = (a: BigNumber, b: BigNumber, roundUp: boolean): BigNumber => {
33
+ return roundUp ? divUp(a, b) : divDown(a, b)
34
+ }
35
+
36
+ export const divDown = (a: BigNumber, b: BigNumber): BigNumber => {
37
+ if (b.isZero())
38
+ throw new Error("ZERO_DIVISION")
39
+ return a.idiv(b)
40
+ }
41
+
42
+ export const divUp = (a: BigNumber, b: BigNumber): BigNumber => {
43
+ if (b.isZero())
44
+ throw new Error("ZERO_DIVISION")
45
+ return a.isZero() ? ZERO : ONE.plus(a.minus(ONE).idiv(b))
46
+ }
@@ -0,0 +1,100 @@
1
+ import Web3 from "web3";
2
+
3
+ export interface ResponseType {
4
+ isSuccess: boolean;
5
+ txHash: string;
6
+ errorMessage: string;
7
+ }
8
+
9
+ export interface DEXGraphFunctionality {
10
+ setEndpoint: (chainId: number, graphApiKey: string) => void;
11
+ getTopPools: (numPools: number) => Promise<PoolInfo[]>;
12
+ getPoolsWithTokenPair: (
13
+ tokenA: string,
14
+ tokenB: string,
15
+ first: number
16
+ ) => Promise<PoolInfo[]>;
17
+ getPoolsWithToken: (token: string, first: number) => Promise<PoolInfo[]>;
18
+
19
+ // calls to Solidity for additional data
20
+ getAdditionalPoolDataFromSolidity: (
21
+ poolInfos: PoolInfo[],
22
+ rpcProvider: Web3
23
+ ) => Promise<Pool[]>;
24
+ }
25
+
26
+ export type Token = {
27
+ _address: string;
28
+ decimals: number;
29
+ name?: string;
30
+ };
31
+
32
+ // we get from Graph QL
33
+ export type PoolInfo = {
34
+ poolId: string;
35
+ dexId: string;
36
+ tokens: Token[]; // list of addresses
37
+ };
38
+
39
+ // we get from Solidity (extra info)
40
+ export abstract class Pool {
41
+ poolId: string;
42
+ dexId: string;
43
+ tokens: Token[]; // list of addresses
44
+
45
+ protected constructor(poolId: string, dexId: string, tokens: Token[]) {
46
+ this.poolId = poolId;
47
+ this.dexId = dexId;
48
+ this.tokens = tokens.map((token) => ({
49
+ _address: token._address.toLowerCase(),
50
+ decimals: token.decimals,
51
+ name: token.name,
52
+ }));
53
+ }
54
+
55
+ abstract calculateExpectedOutputAmount(
56
+ tokenIn: string,
57
+ tokenOut: string,
58
+ amountIn: bigint
59
+ ): bigint;
60
+ abstract reset(): void;
61
+ abstract update(
62
+ tokenIn: string,
63
+ tokenOut: string,
64
+ amountIn: bigint,
65
+ amountOut: bigint
66
+ ): void;
67
+
68
+ containsToken(token: string): boolean {
69
+ return this.tokens.some(
70
+ (t) => t._address.toLowerCase() === token.toLowerCase()
71
+ );
72
+ }
73
+
74
+ getToken0(): Token {
75
+ return this.tokens[0];
76
+ }
77
+
78
+ getToken1(): Token {
79
+ return this.tokens[1];
80
+ }
81
+ }
82
+
83
+ export type SwapStep = {
84
+ poolId: string;
85
+ dexId: string;
86
+ tokenIn: string;
87
+ tokenOut: string;
88
+ };
89
+
90
+ export type Route = {
91
+ swaps: SwapStep[];
92
+ amountIn: bigint;
93
+ percentage: number;
94
+ quote: bigint;
95
+ };
96
+
97
+ export type Quote = {
98
+ routes: Route[];
99
+ quote: bigint;
100
+ };