sushi 3.2.4 → 4.0.1

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 (527) hide show
  1. package/dist/_cjs/calculate/fee.js +2 -2
  2. package/dist/_cjs/calculate/fee.js.map +1 -1
  3. package/dist/_cjs/index.js +0 -3
  4. package/dist/_cjs/index.js.map +1 -1
  5. package/dist/_cjs/math/functions/get-big-int.js +15 -0
  6. package/dist/_cjs/math/functions/get-big-int.js.map +1 -0
  7. package/dist/_cjs/math/functions/index.js +3 -1
  8. package/dist/_cjs/math/functions/index.js.map +1 -1
  9. package/dist/_cjs/pool/sushiswap-v3/utils/computePoolAddress.js +14 -18
  10. package/dist/_cjs/pool/sushiswap-v3/utils/computePoolAddress.js.map +1 -1
  11. package/dist/_cjs/router/index.js +27 -11
  12. package/dist/_cjs/router/index.js.map +1 -1
  13. package/dist/_cjs/router/pool-type.js +14 -0
  14. package/dist/_cjs/router/pool-type.js.map +1 -0
  15. package/dist/_cjs/router/route-status.js +10 -0
  16. package/dist/_cjs/router/route-status.js.map +1 -0
  17. package/dist/_cjs/router/router-liquidity-source.js +9 -0
  18. package/dist/_cjs/router/router-liquidity-source.js.map +1 -0
  19. package/dist/_cjs/validate/index.js +1 -0
  20. package/dist/_cjs/validate/index.js.map +1 -1
  21. package/dist/_cjs/{serializer/AddressValidation.js → validate/isAddressFast.js} +1 -1
  22. package/dist/_cjs/validate/isAddressFast.js.map +1 -0
  23. package/dist/_esm/calculate/fee.js +1 -1
  24. package/dist/_esm/calculate/fee.js.map +1 -1
  25. package/dist/_esm/index.js +0 -3
  26. package/dist/_esm/index.js.map +1 -1
  27. package/dist/_esm/math/functions/get-big-int.js +12 -0
  28. package/dist/_esm/math/functions/get-big-int.js.map +1 -0
  29. package/dist/_esm/math/functions/index.js +1 -0
  30. package/dist/_esm/math/functions/index.js.map +1 -1
  31. package/dist/_esm/pool/sushiswap-v3/utils/computePoolAddress.js +15 -18
  32. package/dist/_esm/pool/sushiswap-v3/utils/computePoolAddress.js.map +1 -1
  33. package/dist/_esm/router/get-currency-combinations.js +0 -2
  34. package/dist/_esm/router/get-currency-combinations.js.map +1 -1
  35. package/dist/_esm/router/index.js +21 -11
  36. package/dist/_esm/router/index.js.map +1 -1
  37. package/dist/_esm/router/pool-type.js +11 -0
  38. package/dist/_esm/router/pool-type.js.map +1 -0
  39. package/dist/_esm/router/route-status.js +7 -0
  40. package/dist/_esm/router/route-status.js.map +1 -0
  41. package/dist/_esm/router/router-liquidity-source.js +6 -0
  42. package/dist/_esm/router/router-liquidity-source.js.map +1 -0
  43. package/dist/_esm/validate/index.js +1 -0
  44. package/dist/_esm/validate/index.js.map +1 -1
  45. package/dist/_esm/{serializer/AddressValidation.js → validate/isAddressFast.js} +1 -1
  46. package/dist/_esm/validate/isAddressFast.js.map +1 -0
  47. package/dist/_types/index.d.ts +0 -3
  48. package/dist/_types/index.d.ts.map +1 -1
  49. package/dist/_types/math/functions/get-big-int.d.ts +2 -0
  50. package/dist/_types/math/functions/get-big-int.d.ts.map +1 -0
  51. package/dist/_types/math/functions/index.d.ts +1 -0
  52. package/dist/_types/math/functions/index.d.ts.map +1 -1
  53. package/dist/_types/pool/sushiswap-v3/utils/computePoolAddress.d.ts.map +1 -1
  54. package/dist/_types/router/get-currency-combinations.d.ts +2 -2
  55. package/dist/_types/router/get-currency-combinations.d.ts.map +1 -1
  56. package/dist/_types/router/index.d.ts +24 -11
  57. package/dist/_types/router/index.d.ts.map +1 -1
  58. package/dist/_types/router/pool-type.d.ts +10 -0
  59. package/dist/_types/router/pool-type.d.ts.map +1 -0
  60. package/dist/_types/router/route-status.d.ts +6 -0
  61. package/dist/_types/router/route-status.d.ts.map +1 -0
  62. package/dist/_types/router/router-liquidity-source.d.ts +5 -0
  63. package/dist/_types/router/router-liquidity-source.d.ts.map +1 -0
  64. package/dist/_types/validate/index.d.ts +1 -0
  65. package/dist/_types/validate/index.d.ts.map +1 -1
  66. package/dist/_types/{serializer/AddressValidation.d.ts → validate/isAddressFast.d.ts} +1 -1
  67. package/dist/_types/validate/isAddressFast.d.ts.map +1 -0
  68. package/package.json +1 -25
  69. package/dist/_cjs/pricing/IncrementalPricer.js +0 -375
  70. package/dist/_cjs/pricing/IncrementalPricer.js.map +0 -1
  71. package/dist/_cjs/pricing/PoolTokenGraph.js +0 -58
  72. package/dist/_cjs/pricing/PoolTokenGraph.js.map +0 -1
  73. package/dist/_cjs/pricing/index.js +0 -18
  74. package/dist/_cjs/pricing/index.js.map +0 -1
  75. package/dist/_cjs/router/HEXer.js +0 -112
  76. package/dist/_cjs/router/HEXer.js.map +0 -1
  77. package/dist/_cjs/router/PoolBinarySerialization.js +0 -466
  78. package/dist/_cjs/router/PoolBinarySerialization.js.map +0 -1
  79. package/dist/_cjs/router/Sankey.AnyChart.js +0 -25
  80. package/dist/_cjs/router/Sankey.AnyChart.js.map +0 -1
  81. package/dist/_cjs/router/curve-sdk.js +0 -204
  82. package/dist/_cjs/router/curve-sdk.js.map +0 -1
  83. package/dist/_cjs/router/data-fetcher.js +0 -159
  84. package/dist/_cjs/router/data-fetcher.js.map +0 -1
  85. package/dist/_cjs/router/liquidity-providers/ApeSwap.js +0 -31
  86. package/dist/_cjs/router/liquidity-providers/ApeSwap.js.map +0 -1
  87. package/dist/_cjs/router/liquidity-providers/Biswap.js +0 -26
  88. package/dist/_cjs/router/liquidity-providers/Biswap.js.map +0 -1
  89. package/dist/_cjs/router/liquidity-providers/CurvePoolsWhitelist.js +0 -3983
  90. package/dist/_cjs/router/liquidity-providers/CurvePoolsWhitelist.js.map +0 -1
  91. package/dist/_cjs/router/liquidity-providers/CurveProvider.js +0 -398
  92. package/dist/_cjs/router/liquidity-providers/CurveProvider.js.map +0 -1
  93. package/dist/_cjs/router/liquidity-providers/CurveProviderWhitelist.js +0 -99
  94. package/dist/_cjs/router/liquidity-providers/CurveProviderWhitelist.js.map +0 -1
  95. package/dist/_cjs/router/liquidity-providers/Dfyn.js +0 -31
  96. package/dist/_cjs/router/liquidity-providers/Dfyn.js.map +0 -1
  97. package/dist/_cjs/router/liquidity-providers/DovishV3.js +0 -28
  98. package/dist/_cjs/router/liquidity-providers/DovishV3.js.map +0 -1
  99. package/dist/_cjs/router/liquidity-providers/Elk.js +0 -47
  100. package/dist/_cjs/router/liquidity-providers/Elk.js.map +0 -1
  101. package/dist/_cjs/router/liquidity-providers/HoneySwap.js +0 -25
  102. package/dist/_cjs/router/liquidity-providers/HoneySwap.js.map +0 -1
  103. package/dist/_cjs/router/liquidity-providers/JetSwap.js +0 -29
  104. package/dist/_cjs/router/liquidity-providers/JetSwap.js.map +0 -1
  105. package/dist/_cjs/router/liquidity-providers/LaserSwap.js +0 -26
  106. package/dist/_cjs/router/liquidity-providers/LaserSwap.js.map +0 -1
  107. package/dist/_cjs/router/liquidity-providers/LiquidityProvider.js +0 -100
  108. package/dist/_cjs/router/liquidity-providers/LiquidityProvider.js.map +0 -1
  109. package/dist/_cjs/router/liquidity-providers/NativeWrapProvider.js +0 -46
  110. package/dist/_cjs/router/liquidity-providers/NativeWrapProvider.js.map +0 -1
  111. package/dist/_cjs/router/liquidity-providers/NetSwap.js +0 -25
  112. package/dist/_cjs/router/liquidity-providers/NetSwap.js.map +0 -1
  113. package/dist/_cjs/router/liquidity-providers/PancakeSwap.js +0 -28
  114. package/dist/_cjs/router/liquidity-providers/PancakeSwap.js.map +0 -1
  115. package/dist/_cjs/router/liquidity-providers/QuickSwap.js +0 -25
  116. package/dist/_cjs/router/liquidity-providers/QuickSwap.js.map +0 -1
  117. package/dist/_cjs/router/liquidity-providers/SpookySwap.js +0 -25
  118. package/dist/_cjs/router/liquidity-providers/SpookySwap.js.map +0 -1
  119. package/dist/_cjs/router/liquidity-providers/SushiSwapV2.js +0 -20
  120. package/dist/_cjs/router/liquidity-providers/SushiSwapV2.js.map +0 -1
  121. package/dist/_cjs/router/liquidity-providers/SushiSwapV3.js +0 -106
  122. package/dist/_cjs/router/liquidity-providers/SushiSwapV3.js.map +0 -1
  123. package/dist/_cjs/router/liquidity-providers/TraderJoe.js +0 -25
  124. package/dist/_cjs/router/liquidity-providers/TraderJoe.js.map +0 -1
  125. package/dist/_cjs/router/liquidity-providers/UbeSwap.js +0 -25
  126. package/dist/_cjs/router/liquidity-providers/UbeSwap.js.map +0 -1
  127. package/dist/_cjs/router/liquidity-providers/UniswapV2.js +0 -25
  128. package/dist/_cjs/router/liquidity-providers/UniswapV2.js.map +0 -1
  129. package/dist/_cjs/router/liquidity-providers/UniswapV2Base.js +0 -112
  130. package/dist/_cjs/router/liquidity-providers/UniswapV2Base.js.map +0 -1
  131. package/dist/_cjs/router/liquidity-providers/UniswapV3.js +0 -43
  132. package/dist/_cjs/router/liquidity-providers/UniswapV3.js.map +0 -1
  133. package/dist/_cjs/router/liquidity-providers/UniswapV3Base.js +0 -281
  134. package/dist/_cjs/router/liquidity-providers/UniswapV3Base.js.map +0 -1
  135. package/dist/_cjs/router/liquidity-providers/index.js +0 -27
  136. package/dist/_cjs/router/liquidity-providers/index.js.map +0 -1
  137. package/dist/_cjs/router/pool-codes/BentoBridge.js +0 -82
  138. package/dist/_cjs/router/pool-codes/BentoBridge.js.map +0 -1
  139. package/dist/_cjs/router/pool-codes/BentoPool.js +0 -47
  140. package/dist/_cjs/router/pool-codes/BentoPool.js.map +0 -1
  141. package/dist/_cjs/router/pool-codes/Bridge.js +0 -7
  142. package/dist/_cjs/router/pool-codes/Bridge.js.map +0 -1
  143. package/dist/_cjs/router/pool-codes/ConstantProductPool.js +0 -42
  144. package/dist/_cjs/router/pool-codes/ConstantProductPool.js.map +0 -1
  145. package/dist/_cjs/router/pool-codes/CurvePool.js +0 -43
  146. package/dist/_cjs/router/pool-codes/CurvePool.js.map +0 -1
  147. package/dist/_cjs/router/pool-codes/NativeWrapBridge.js +0 -50
  148. package/dist/_cjs/router/pool-codes/NativeWrapBridge.js.map +0 -1
  149. package/dist/_cjs/router/pool-codes/PoolCode.js +0 -35
  150. package/dist/_cjs/router/pool-codes/PoolCode.js.map +0 -1
  151. package/dist/_cjs/router/pool-codes/TridentCLPool.js +0 -27
  152. package/dist/_cjs/router/pool-codes/TridentCLPool.js.map +0 -1
  153. package/dist/_cjs/router/pool-codes/UniV3Pool.js +0 -27
  154. package/dist/_cjs/router/pool-codes/UniV3Pool.js.map +0 -1
  155. package/dist/_cjs/router/pool-codes/UniV4Pool.js +0 -40
  156. package/dist/_cjs/router/pool-codes/UniV4Pool.js.map +0 -1
  157. package/dist/_cjs/router/pool-codes/index.js +0 -22
  158. package/dist/_cjs/router/pool-codes/index.js.map +0 -1
  159. package/dist/_cjs/router/router.js +0 -352
  160. package/dist/_cjs/router/router.js.map +0 -1
  161. package/dist/_cjs/router/routingBases.js +0 -55
  162. package/dist/_cjs/router/routingBases.js.map +0 -1
  163. package/dist/_cjs/router/tines-to-route-processor-2.js +0 -187
  164. package/dist/_cjs/router/tines-to-route-processor-2.js.map +0 -1
  165. package/dist/_cjs/router/tines-to-route-processor-4.js +0 -69
  166. package/dist/_cjs/router/tines-to-route-processor-4.js.map +0 -1
  167. package/dist/_cjs/router/tines-to-route-processor-6.js +0 -17
  168. package/dist/_cjs/router/tines-to-route-processor-6.js.map +0 -1
  169. package/dist/_cjs/router/tines-to-route-processor.js +0 -192
  170. package/dist/_cjs/router/tines-to-route-processor.js.map +0 -1
  171. package/dist/_cjs/serializer/AddressValidation.js.map +0 -1
  172. package/dist/_cjs/serializer/BinarySerialization.js +0 -256
  173. package/dist/_cjs/serializer/BinarySerialization.js.map +0 -1
  174. package/dist/_cjs/serializer/index.js +0 -18
  175. package/dist/_cjs/serializer/index.js.map +0 -1
  176. package/dist/_cjs/tines/BridgeBento.js +0 -116
  177. package/dist/_cjs/tines/BridgeBento.js.map +0 -1
  178. package/dist/_cjs/tines/BridgeBidirectionalUnlimited.js +0 -26
  179. package/dist/_cjs/tines/BridgeBidirectionalUnlimited.js.map +0 -1
  180. package/dist/_cjs/tines/BridgeStargateV04OneWay.js +0 -44
  181. package/dist/_cjs/tines/BridgeStargateV04OneWay.js.map +0 -1
  182. package/dist/_cjs/tines/CLPool.js +0 -248
  183. package/dist/_cjs/tines/CLPool.js.map +0 -1
  184. package/dist/_cjs/tines/CurveMultitokenPool.js +0 -238
  185. package/dist/_cjs/tines/CurveMultitokenPool.js.map +0 -1
  186. package/dist/_cjs/tines/CurveMultitokenPoolSingle.js +0 -195
  187. package/dist/_cjs/tines/CurveMultitokenPoolSingle.js.map +0 -1
  188. package/dist/_cjs/tines/CurvePool.js +0 -121
  189. package/dist/_cjs/tines/CurvePool.js.map +0 -1
  190. package/dist/_cjs/tines/Graph.js +0 -1187
  191. package/dist/_cjs/tines/Graph.js.map +0 -1
  192. package/dist/_cjs/tines/MultiRouter.js +0 -192
  193. package/dist/_cjs/tines/MultiRouter.js.map +0 -1
  194. package/dist/_cjs/tines/PrimaryPools.js +0 -147
  195. package/dist/_cjs/tines/PrimaryPools.js.map +0 -1
  196. package/dist/_cjs/tines/RPool.js +0 -77
  197. package/dist/_cjs/tines/RPool.js.map +0 -1
  198. package/dist/_cjs/tines/StableSwapPool.js +0 -222
  199. package/dist/_cjs/tines/StableSwapPool.js.map +0 -1
  200. package/dist/_cjs/tines/StarGateFeesV04.js +0 -113
  201. package/dist/_cjs/tines/StarGateFeesV04.js.map +0 -1
  202. package/dist/_cjs/tines/UniV3Pool.js +0 -291
  203. package/dist/_cjs/tines/UniV3Pool.js.map +0 -1
  204. package/dist/_cjs/tines/UniV4Pool.js +0 -28
  205. package/dist/_cjs/tines/UniV4Pool.js.map +0 -1
  206. package/dist/_cjs/tines/Utils.js +0 -113
  207. package/dist/_cjs/tines/Utils.js.map +0 -1
  208. package/dist/_cjs/tines/constants/hybrid.js +0 -5
  209. package/dist/_cjs/tines/constants/hybrid.js.map +0 -1
  210. package/dist/_cjs/tines/constants/index.js +0 -18
  211. package/dist/_cjs/tines/constants/index.js.map +0 -1
  212. package/dist/_cjs/tines/functions/computeHybridLiquidity.js +0 -25
  213. package/dist/_cjs/tines/functions/computeHybridLiquidity.js.map +0 -1
  214. package/dist/_cjs/tines/functions/convertTokenToBento.js +0 -13
  215. package/dist/_cjs/tines/functions/convertTokenToBento.js.map +0 -1
  216. package/dist/_cjs/tines/functions/getBentoChainId.js +0 -7
  217. package/dist/_cjs/tines/functions/getBentoChainId.js.map +0 -1
  218. package/dist/_cjs/tines/functions/index.js +0 -10
  219. package/dist/_cjs/tines/functions/index.js.map +0 -1
  220. package/dist/_cjs/tines/index.js +0 -34
  221. package/dist/_cjs/tines/index.js.map +0 -1
  222. package/dist/_esm/pricing/IncrementalPricer.js +0 -430
  223. package/dist/_esm/pricing/IncrementalPricer.js.map +0 -1
  224. package/dist/_esm/pricing/PoolTokenGraph.js +0 -56
  225. package/dist/_esm/pricing/PoolTokenGraph.js.map +0 -1
  226. package/dist/_esm/pricing/index.js +0 -2
  227. package/dist/_esm/pricing/index.js.map +0 -1
  228. package/dist/_esm/router/HEXer.js +0 -110
  229. package/dist/_esm/router/HEXer.js.map +0 -1
  230. package/dist/_esm/router/PoolBinarySerialization.js +0 -471
  231. package/dist/_esm/router/PoolBinarySerialization.js.map +0 -1
  232. package/dist/_esm/router/Sankey.AnyChart.js +0 -25
  233. package/dist/_esm/router/Sankey.AnyChart.js.map +0 -1
  234. package/dist/_esm/router/curve-sdk.js +0 -215
  235. package/dist/_esm/router/curve-sdk.js.map +0 -1
  236. package/dist/_esm/router/data-fetcher.js +0 -188
  237. package/dist/_esm/router/data-fetcher.js.map +0 -1
  238. package/dist/_esm/router/liquidity-providers/ApeSwap.js +0 -27
  239. package/dist/_esm/router/liquidity-providers/ApeSwap.js.map +0 -1
  240. package/dist/_esm/router/liquidity-providers/Biswap.js +0 -22
  241. package/dist/_esm/router/liquidity-providers/Biswap.js.map +0 -1
  242. package/dist/_esm/router/liquidity-providers/CurvePoolsWhitelist.js +0 -3980
  243. package/dist/_esm/router/liquidity-providers/CurvePoolsWhitelist.js.map +0 -1
  244. package/dist/_esm/router/liquidity-providers/CurveProvider.js +0 -473
  245. package/dist/_esm/router/liquidity-providers/CurveProvider.js.map +0 -1
  246. package/dist/_esm/router/liquidity-providers/CurveProviderWhitelist.js +0 -200
  247. package/dist/_esm/router/liquidity-providers/CurveProviderWhitelist.js.map +0 -1
  248. package/dist/_esm/router/liquidity-providers/Dfyn.js +0 -27
  249. package/dist/_esm/router/liquidity-providers/Dfyn.js.map +0 -1
  250. package/dist/_esm/router/liquidity-providers/DovishV3.js +0 -24
  251. package/dist/_esm/router/liquidity-providers/DovishV3.js.map +0 -1
  252. package/dist/_esm/router/liquidity-providers/Elk.js +0 -51
  253. package/dist/_esm/router/liquidity-providers/Elk.js.map +0 -1
  254. package/dist/_esm/router/liquidity-providers/HoneySwap.js +0 -21
  255. package/dist/_esm/router/liquidity-providers/HoneySwap.js.map +0 -1
  256. package/dist/_esm/router/liquidity-providers/JetSwap.js +0 -25
  257. package/dist/_esm/router/liquidity-providers/JetSwap.js.map +0 -1
  258. package/dist/_esm/router/liquidity-providers/LaserSwap.js +0 -22
  259. package/dist/_esm/router/liquidity-providers/LaserSwap.js.map +0 -1
  260. package/dist/_esm/router/liquidity-providers/LiquidityProvider.js +0 -106
  261. package/dist/_esm/router/liquidity-providers/LiquidityProvider.js.map +0 -1
  262. package/dist/_esm/router/liquidity-providers/NativeWrapProvider.js +0 -42
  263. package/dist/_esm/router/liquidity-providers/NativeWrapProvider.js.map +0 -1
  264. package/dist/_esm/router/liquidity-providers/NetSwap.js +0 -21
  265. package/dist/_esm/router/liquidity-providers/NetSwap.js.map +0 -1
  266. package/dist/_esm/router/liquidity-providers/PancakeSwap.js +0 -24
  267. package/dist/_esm/router/liquidity-providers/PancakeSwap.js.map +0 -1
  268. package/dist/_esm/router/liquidity-providers/QuickSwap.js +0 -21
  269. package/dist/_esm/router/liquidity-providers/QuickSwap.js.map +0 -1
  270. package/dist/_esm/router/liquidity-providers/SpookySwap.js +0 -21
  271. package/dist/_esm/router/liquidity-providers/SpookySwap.js.map +0 -1
  272. package/dist/_esm/router/liquidity-providers/SushiSwapV2.js +0 -17
  273. package/dist/_esm/router/liquidity-providers/SushiSwapV2.js.map +0 -1
  274. package/dist/_esm/router/liquidity-providers/SushiSwapV3.js +0 -102
  275. package/dist/_esm/router/liquidity-providers/SushiSwapV3.js.map +0 -1
  276. package/dist/_esm/router/liquidity-providers/TraderJoe.js +0 -21
  277. package/dist/_esm/router/liquidity-providers/TraderJoe.js.map +0 -1
  278. package/dist/_esm/router/liquidity-providers/UbeSwap.js +0 -21
  279. package/dist/_esm/router/liquidity-providers/UbeSwap.js.map +0 -1
  280. package/dist/_esm/router/liquidity-providers/UniswapV2.js +0 -21
  281. package/dist/_esm/router/liquidity-providers/UniswapV2.js.map +0 -1
  282. package/dist/_esm/router/liquidity-providers/UniswapV2Base.js +0 -112
  283. package/dist/_esm/router/liquidity-providers/UniswapV2Base.js.map +0 -1
  284. package/dist/_esm/router/liquidity-providers/UniswapV3.js +0 -39
  285. package/dist/_esm/router/liquidity-providers/UniswapV3.js.map +0 -1
  286. package/dist/_esm/router/liquidity-providers/UniswapV3Base.js +0 -286
  287. package/dist/_esm/router/liquidity-providers/UniswapV3Base.js.map +0 -1
  288. package/dist/_esm/router/liquidity-providers/index.js +0 -9
  289. package/dist/_esm/router/liquidity-providers/index.js.map +0 -1
  290. package/dist/_esm/router/pool-codes/BentoBridge.js +0 -86
  291. package/dist/_esm/router/pool-codes/BentoBridge.js.map +0 -1
  292. package/dist/_esm/router/pool-codes/BentoPool.js +0 -48
  293. package/dist/_esm/router/pool-codes/BentoPool.js.map +0 -1
  294. package/dist/_esm/router/pool-codes/Bridge.js +0 -4
  295. package/dist/_esm/router/pool-codes/Bridge.js.map +0 -1
  296. package/dist/_esm/router/pool-codes/ConstantProductPool.js +0 -41
  297. package/dist/_esm/router/pool-codes/ConstantProductPool.js.map +0 -1
  298. package/dist/_esm/router/pool-codes/CurvePool.js +0 -41
  299. package/dist/_esm/router/pool-codes/CurvePool.js.map +0 -1
  300. package/dist/_esm/router/pool-codes/NativeWrapBridge.js +0 -52
  301. package/dist/_esm/router/pool-codes/NativeWrapBridge.js.map +0 -1
  302. package/dist/_esm/router/pool-codes/PoolCode.js +0 -36
  303. package/dist/_esm/router/pool-codes/PoolCode.js.map +0 -1
  304. package/dist/_esm/router/pool-codes/TridentCLPool.js +0 -25
  305. package/dist/_esm/router/pool-codes/TridentCLPool.js.map +0 -1
  306. package/dist/_esm/router/pool-codes/UniV3Pool.js +0 -25
  307. package/dist/_esm/router/pool-codes/UniV3Pool.js.map +0 -1
  308. package/dist/_esm/router/pool-codes/UniV4Pool.js +0 -37
  309. package/dist/_esm/router/pool-codes/UniV4Pool.js.map +0 -1
  310. package/dist/_esm/router/pool-codes/index.js +0 -10
  311. package/dist/_esm/router/pool-codes/index.js.map +0 -1
  312. package/dist/_esm/router/router.js +0 -351
  313. package/dist/_esm/router/router.js.map +0 -1
  314. package/dist/_esm/router/routingBases.js +0 -50
  315. package/dist/_esm/router/routingBases.js.map +0 -1
  316. package/dist/_esm/router/tines-to-route-processor-2.js +0 -189
  317. package/dist/_esm/router/tines-to-route-processor-2.js.map +0 -1
  318. package/dist/_esm/router/tines-to-route-processor-4.js +0 -75
  319. package/dist/_esm/router/tines-to-route-processor-4.js.map +0 -1
  320. package/dist/_esm/router/tines-to-route-processor-6.js +0 -14
  321. package/dist/_esm/router/tines-to-route-processor-6.js.map +0 -1
  322. package/dist/_esm/router/tines-to-route-processor.js +0 -200
  323. package/dist/_esm/router/tines-to-route-processor.js.map +0 -1
  324. package/dist/_esm/serializer/AddressValidation.js.map +0 -1
  325. package/dist/_esm/serializer/BinarySerialization.js +0 -270
  326. package/dist/_esm/serializer/BinarySerialization.js.map +0 -1
  327. package/dist/_esm/serializer/index.js +0 -2
  328. package/dist/_esm/serializer/index.js.map +0 -1
  329. package/dist/_esm/tines/BridgeBento.js +0 -116
  330. package/dist/_esm/tines/BridgeBento.js.map +0 -1
  331. package/dist/_esm/tines/BridgeBidirectionalUnlimited.js +0 -22
  332. package/dist/_esm/tines/BridgeBidirectionalUnlimited.js.map +0 -1
  333. package/dist/_esm/tines/BridgeStargateV04OneWay.js +0 -43
  334. package/dist/_esm/tines/BridgeStargateV04OneWay.js.map +0 -1
  335. package/dist/_esm/tines/CLPool.js +0 -265
  336. package/dist/_esm/tines/CLPool.js.map +0 -1
  337. package/dist/_esm/tines/CurveMultitokenPool.js +0 -268
  338. package/dist/_esm/tines/CurveMultitokenPool.js.map +0 -1
  339. package/dist/_esm/tines/CurveMultitokenPoolSingle.js +0 -194
  340. package/dist/_esm/tines/CurveMultitokenPoolSingle.js.map +0 -1
  341. package/dist/_esm/tines/CurvePool.js +0 -120
  342. package/dist/_esm/tines/CurvePool.js.map +0 -1
  343. package/dist/_esm/tines/Graph.js +0 -1324
  344. package/dist/_esm/tines/Graph.js.map +0 -1
  345. package/dist/_esm/tines/MultiRouter.js +0 -191
  346. package/dist/_esm/tines/MultiRouter.js.map +0 -1
  347. package/dist/_esm/tines/PrimaryPools.js +0 -166
  348. package/dist/_esm/tines/PrimaryPools.js.map +0 -1
  349. package/dist/_esm/tines/RPool.js +0 -80
  350. package/dist/_esm/tines/RPool.js.map +0 -1
  351. package/dist/_esm/tines/StableSwapPool.js +0 -221
  352. package/dist/_esm/tines/StableSwapPool.js.map +0 -1
  353. package/dist/_esm/tines/StarGateFeesV04.js +0 -135
  354. package/dist/_esm/tines/StarGateFeesV04.js.map +0 -1
  355. package/dist/_esm/tines/UniV3Pool.js +0 -325
  356. package/dist/_esm/tines/UniV3Pool.js.map +0 -1
  357. package/dist/_esm/tines/UniV4Pool.js +0 -40
  358. package/dist/_esm/tines/UniV4Pool.js.map +0 -1
  359. package/dist/_esm/tines/Utils.js +0 -108
  360. package/dist/_esm/tines/Utils.js.map +0 -1
  361. package/dist/_esm/tines/constants/hybrid.js +0 -2
  362. package/dist/_esm/tines/constants/hybrid.js.map +0 -1
  363. package/dist/_esm/tines/constants/index.js +0 -2
  364. package/dist/_esm/tines/constants/index.js.map +0 -1
  365. package/dist/_esm/tines/functions/computeHybridLiquidity.js +0 -22
  366. package/dist/_esm/tines/functions/computeHybridLiquidity.js.map +0 -1
  367. package/dist/_esm/tines/functions/convertTokenToBento.js +0 -11
  368. package/dist/_esm/tines/functions/convertTokenToBento.js.map +0 -1
  369. package/dist/_esm/tines/functions/getBentoChainId.js +0 -4
  370. package/dist/_esm/tines/functions/getBentoChainId.js.map +0 -1
  371. package/dist/_esm/tines/functions/index.js +0 -4
  372. package/dist/_esm/tines/functions/index.js.map +0 -1
  373. package/dist/_esm/tines/index.js +0 -18
  374. package/dist/_esm/tines/index.js.map +0 -1
  375. package/dist/_types/pricing/IncrementalPricer.d.ts +0 -48
  376. package/dist/_types/pricing/IncrementalPricer.d.ts.map +0 -1
  377. package/dist/_types/pricing/PoolTokenGraph.d.ts +0 -22
  378. package/dist/_types/pricing/PoolTokenGraph.d.ts.map +0 -1
  379. package/dist/_types/pricing/index.d.ts +0 -2
  380. package/dist/_types/pricing/index.d.ts.map +0 -1
  381. package/dist/_types/router/HEXer.d.ts +0 -20
  382. package/dist/_types/router/HEXer.d.ts.map +0 -1
  383. package/dist/_types/router/PoolBinarySerialization.d.ts +0 -11
  384. package/dist/_types/router/PoolBinarySerialization.d.ts.map +0 -1
  385. package/dist/_types/router/Sankey.AnyChart.d.ts +0 -7
  386. package/dist/_types/router/Sankey.AnyChart.d.ts.map +0 -1
  387. package/dist/_types/router/curve-sdk.d.ts +0 -214
  388. package/dist/_types/router/curve-sdk.d.ts.map +0 -1
  389. package/dist/_types/router/data-fetcher.d.ts +0 -24
  390. package/dist/_types/router/data-fetcher.d.ts.map +0 -1
  391. package/dist/_types/router/liquidity-providers/ApeSwap.d.ts +0 -10
  392. package/dist/_types/router/liquidity-providers/ApeSwap.d.ts.map +0 -1
  393. package/dist/_types/router/liquidity-providers/Biswap.d.ts +0 -11
  394. package/dist/_types/router/liquidity-providers/Biswap.d.ts.map +0 -1
  395. package/dist/_types/router/liquidity-providers/CurvePoolsWhitelist.d.ts +0 -12
  396. package/dist/_types/router/liquidity-providers/CurvePoolsWhitelist.d.ts.map +0 -1
  397. package/dist/_types/router/liquidity-providers/CurveProvider.d.ts +0 -41
  398. package/dist/_types/router/liquidity-providers/CurveProvider.d.ts.map +0 -1
  399. package/dist/_types/router/liquidity-providers/CurveProviderWhitelist.d.ts +0 -40
  400. package/dist/_types/router/liquidity-providers/CurveProviderWhitelist.d.ts.map +0 -1
  401. package/dist/_types/router/liquidity-providers/Dfyn.d.ts +0 -10
  402. package/dist/_types/router/liquidity-providers/Dfyn.d.ts.map +0 -1
  403. package/dist/_types/router/liquidity-providers/DovishV3.d.ts +0 -10
  404. package/dist/_types/router/liquidity-providers/DovishV3.d.ts.map +0 -1
  405. package/dist/_types/router/liquidity-providers/Elk.d.ts +0 -10
  406. package/dist/_types/router/liquidity-providers/Elk.d.ts.map +0 -1
  407. package/dist/_types/router/liquidity-providers/HoneySwap.d.ts +0 -10
  408. package/dist/_types/router/liquidity-providers/HoneySwap.d.ts.map +0 -1
  409. package/dist/_types/router/liquidity-providers/JetSwap.d.ts +0 -10
  410. package/dist/_types/router/liquidity-providers/JetSwap.d.ts.map +0 -1
  411. package/dist/_types/router/liquidity-providers/LaserSwap.d.ts +0 -11
  412. package/dist/_types/router/liquidity-providers/LaserSwap.d.ts.map +0 -1
  413. package/dist/_types/router/liquidity-providers/LiquidityProvider.d.ts +0 -123
  414. package/dist/_types/router/liquidity-providers/LiquidityProvider.d.ts.map +0 -1
  415. package/dist/_types/router/liquidity-providers/NativeWrapProvider.d.ts +0 -15
  416. package/dist/_types/router/liquidity-providers/NativeWrapProvider.d.ts.map +0 -1
  417. package/dist/_types/router/liquidity-providers/NetSwap.d.ts +0 -10
  418. package/dist/_types/router/liquidity-providers/NetSwap.d.ts.map +0 -1
  419. package/dist/_types/router/liquidity-providers/PancakeSwap.d.ts +0 -11
  420. package/dist/_types/router/liquidity-providers/PancakeSwap.d.ts.map +0 -1
  421. package/dist/_types/router/liquidity-providers/QuickSwap.d.ts +0 -10
  422. package/dist/_types/router/liquidity-providers/QuickSwap.d.ts.map +0 -1
  423. package/dist/_types/router/liquidity-providers/SpookySwap.d.ts +0 -10
  424. package/dist/_types/router/liquidity-providers/SpookySwap.d.ts.map +0 -1
  425. package/dist/_types/router/liquidity-providers/SushiSwapV2.d.ts +0 -10
  426. package/dist/_types/router/liquidity-providers/SushiSwapV2.d.ts.map +0 -1
  427. package/dist/_types/router/liquidity-providers/SushiSwapV3.d.ts +0 -10
  428. package/dist/_types/router/liquidity-providers/SushiSwapV3.d.ts.map +0 -1
  429. package/dist/_types/router/liquidity-providers/TraderJoe.d.ts +0 -10
  430. package/dist/_types/router/liquidity-providers/TraderJoe.d.ts.map +0 -1
  431. package/dist/_types/router/liquidity-providers/UbeSwap.d.ts +0 -10
  432. package/dist/_types/router/liquidity-providers/UbeSwap.d.ts.map +0 -1
  433. package/dist/_types/router/liquidity-providers/UniswapV2.d.ts +0 -10
  434. package/dist/_types/router/liquidity-providers/UniswapV2.d.ts.map +0 -1
  435. package/dist/_types/router/liquidity-providers/UniswapV2Base.d.ts +0 -29
  436. package/dist/_types/router/liquidity-providers/UniswapV2Base.d.ts.map +0 -1
  437. package/dist/_types/router/liquidity-providers/UniswapV3.d.ts +0 -10
  438. package/dist/_types/router/liquidity-providers/UniswapV3.d.ts.map +0 -1
  439. package/dist/_types/router/liquidity-providers/UniswapV3Base.d.ts +0 -32
  440. package/dist/_types/router/liquidity-providers/UniswapV3Base.d.ts.map +0 -1
  441. package/dist/_types/router/liquidity-providers/index.d.ts +0 -9
  442. package/dist/_types/router/liquidity-providers/index.d.ts.map +0 -1
  443. package/dist/_types/router/pool-codes/BentoBridge.d.ts +0 -11
  444. package/dist/_types/router/pool-codes/BentoBridge.d.ts.map +0 -1
  445. package/dist/_types/router/pool-codes/BentoPool.d.ts +0 -9
  446. package/dist/_types/router/pool-codes/BentoPool.d.ts.map +0 -1
  447. package/dist/_types/router/pool-codes/Bridge.d.ts +0 -5
  448. package/dist/_types/router/pool-codes/Bridge.d.ts.map +0 -1
  449. package/dist/_types/router/pool-codes/ConstantProductPool.d.ts +0 -10
  450. package/dist/_types/router/pool-codes/ConstantProductPool.d.ts.map +0 -1
  451. package/dist/_types/router/pool-codes/CurvePool.d.ts +0 -13
  452. package/dist/_types/router/pool-codes/CurvePool.d.ts.map +0 -1
  453. package/dist/_types/router/pool-codes/NativeWrapBridge.d.ts +0 -10
  454. package/dist/_types/router/pool-codes/NativeWrapBridge.d.ts.map +0 -1
  455. package/dist/_types/router/pool-codes/PoolCode.d.ts +0 -24
  456. package/dist/_types/router/pool-codes/PoolCode.d.ts.map +0 -1
  457. package/dist/_types/router/pool-codes/TridentCLPool.d.ts +0 -10
  458. package/dist/_types/router/pool-codes/TridentCLPool.d.ts.map +0 -1
  459. package/dist/_types/router/pool-codes/UniV3Pool.d.ts +0 -10
  460. package/dist/_types/router/pool-codes/UniV3Pool.d.ts.map +0 -1
  461. package/dist/_types/router/pool-codes/UniV4Pool.d.ts +0 -13
  462. package/dist/_types/router/pool-codes/UniV4Pool.d.ts.map +0 -1
  463. package/dist/_types/router/pool-codes/index.d.ts +0 -10
  464. package/dist/_types/router/pool-codes/index.d.ts.map +0 -1
  465. package/dist/_types/router/router.d.ts +0 -68
  466. package/dist/_types/router/router.d.ts.map +0 -1
  467. package/dist/_types/router/routingBases.d.ts +0 -8
  468. package/dist/_types/router/routingBases.d.ts.map +0 -1
  469. package/dist/_types/router/tines-to-route-processor-2.d.ts +0 -41
  470. package/dist/_types/router/tines-to-route-processor-2.d.ts.map +0 -1
  471. package/dist/_types/router/tines-to-route-processor-4.d.ts +0 -12
  472. package/dist/_types/router/tines-to-route-processor-4.d.ts.map +0 -1
  473. package/dist/_types/router/tines-to-route-processor-6.d.ts +0 -5
  474. package/dist/_types/router/tines-to-route-processor-6.d.ts.map +0 -1
  475. package/dist/_types/router/tines-to-route-processor.d.ts +0 -21
  476. package/dist/_types/router/tines-to-route-processor.d.ts.map +0 -1
  477. package/dist/_types/serializer/AddressValidation.d.ts.map +0 -1
  478. package/dist/_types/serializer/BinarySerialization.d.ts +0 -43
  479. package/dist/_types/serializer/BinarySerialization.d.ts.map +0 -1
  480. package/dist/_types/serializer/index.d.ts +0 -2
  481. package/dist/_types/serializer/index.d.ts.map +0 -1
  482. package/dist/_types/tines/BridgeBento.d.ts +0 -21
  483. package/dist/_types/tines/BridgeBento.d.ts.map +0 -1
  484. package/dist/_types/tines/BridgeBidirectionalUnlimited.d.ts +0 -16
  485. package/dist/_types/tines/BridgeBidirectionalUnlimited.d.ts.map +0 -1
  486. package/dist/_types/tines/BridgeStargateV04OneWay.d.ts +0 -23
  487. package/dist/_types/tines/BridgeStargateV04OneWay.d.ts.map +0 -1
  488. package/dist/_types/tines/CLPool.d.ts +0 -27
  489. package/dist/_types/tines/CLPool.d.ts.map +0 -1
  490. package/dist/_types/tines/CurveMultitokenPool.d.ts +0 -58
  491. package/dist/_types/tines/CurveMultitokenPool.d.ts.map +0 -1
  492. package/dist/_types/tines/CurveMultitokenPoolSingle.d.ts +0 -54
  493. package/dist/_types/tines/CurveMultitokenPoolSingle.d.ts.map +0 -1
  494. package/dist/_types/tines/CurvePool.d.ts +0 -28
  495. package/dist/_types/tines/CurvePool.d.ts.map +0 -1
  496. package/dist/_types/tines/Graph.d.ts +0 -135
  497. package/dist/_types/tines/Graph.d.ts.map +0 -1
  498. package/dist/_types/tines/MultiRouter.d.ts +0 -11
  499. package/dist/_types/tines/MultiRouter.d.ts.map +0 -1
  500. package/dist/_types/tines/PrimaryPools.d.ts +0 -44
  501. package/dist/_types/tines/PrimaryPools.d.ts.map +0 -1
  502. package/dist/_types/tines/RPool.d.ts +0 -54
  503. package/dist/_types/tines/RPool.d.ts.map +0 -1
  504. package/dist/_types/tines/StableSwapPool.d.ts +0 -50
  505. package/dist/_types/tines/StableSwapPool.d.ts.map +0 -1
  506. package/dist/_types/tines/StarGateFeesV04.d.ts +0 -16
  507. package/dist/_types/tines/StarGateFeesV04.d.ts.map +0 -1
  508. package/dist/_types/tines/UniV3Pool.d.ts +0 -25
  509. package/dist/_types/tines/UniV3Pool.d.ts.map +0 -1
  510. package/dist/_types/tines/UniV4Pool.d.ts +0 -13
  511. package/dist/_types/tines/UniV4Pool.d.ts.map +0 -1
  512. package/dist/_types/tines/Utils.d.ts +0 -9
  513. package/dist/_types/tines/Utils.d.ts.map +0 -1
  514. package/dist/_types/tines/constants/hybrid.d.ts +0 -2
  515. package/dist/_types/tines/constants/hybrid.d.ts.map +0 -1
  516. package/dist/_types/tines/constants/index.d.ts +0 -2
  517. package/dist/_types/tines/constants/index.d.ts.map +0 -1
  518. package/dist/_types/tines/functions/computeHybridLiquidity.d.ts +0 -2
  519. package/dist/_types/tines/functions/computeHybridLiquidity.d.ts.map +0 -1
  520. package/dist/_types/tines/functions/convertTokenToBento.d.ts +0 -4
  521. package/dist/_types/tines/functions/convertTokenToBento.d.ts.map +0 -1
  522. package/dist/_types/tines/functions/getBentoChainId.d.ts +0 -2
  523. package/dist/_types/tines/functions/getBentoChainId.d.ts.map +0 -1
  524. package/dist/_types/tines/functions/index.d.ts +0 -4
  525. package/dist/_types/tines/functions/index.d.ts.map +0 -1
  526. package/dist/_types/tines/index.d.ts +0 -18
  527. package/dist/_types/tines/index.d.ts.map +0 -1
@@ -1,1324 +0,0 @@
1
- import { PoolType, RPool, setTokenId } from './RPool.js';
2
- import { StableSwapRPool } from './StableSwapPool.js';
3
- import { ASSERT, DEBUG, closeValues, fastArrayMerge, getBigInt, } from './Utils.js';
4
- const ROUTER_DISTRIBUTION_PORTION = 65535;
5
- export var RouteStatus;
6
- (function (RouteStatus) {
7
- RouteStatus["Success"] = "Success";
8
- RouteStatus["NoWay"] = "NoWay";
9
- RouteStatus["Partial"] = "Partial";
10
- })(RouteStatus || (RouteStatus = {}));
11
- export function NoWayMultiRoute(from, to) {
12
- return {
13
- status: RouteStatus.NoWay,
14
- fromToken: from,
15
- toToken: to,
16
- amountIn: 0,
17
- amountInBI: 0n,
18
- amountOut: 0,
19
- amountOutBI: 0n,
20
- legs: [],
21
- gasSpent: 0,
22
- totalAmountOut: 0,
23
- totalAmountOutBI: 0n,
24
- primaryPrice: undefined,
25
- swapPrice: undefined,
26
- priceImpact: undefined,
27
- };
28
- }
29
- export class Edge {
30
- constructor(p, v0, v1) {
31
- this.pool = p;
32
- this.vert0 = v0;
33
- this.vert1 = v1;
34
- this.amountInPrevious = 0;
35
- this.amountOutPrevious = 0;
36
- this.canBeUsed = true;
37
- this.direction = true;
38
- this.spentGas = 0;
39
- this.spentGasNew = 0;
40
- this.bestEdgeIncome = 0;
41
- }
42
- cleanTmpData() {
43
- this.amountInPrevious = 0;
44
- this.amountOutPrevious = 0;
45
- this.canBeUsed = true;
46
- this.direction = true;
47
- this.spentGas = 0;
48
- this.spentGasNew = 0;
49
- this.bestEdgeIncome = 0;
50
- this.pool.cleanTmpData();
51
- }
52
- reserve(v) {
53
- return v === this.vert0 ? this.pool.getReserve0() : this.pool.getReserve1();
54
- }
55
- calcOutput(v, amountIn) {
56
- let res;
57
- let gas;
58
- if (v === this.vert1) {
59
- if (this.direction) {
60
- if (amountIn < this.amountOutPrevious) {
61
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountOutPrevious - amountIn, true);
62
- res = this.amountInPrevious - inp;
63
- gas = gasSpent;
64
- }
65
- else {
66
- const { out, gasSpent } = this.pool.calcOutByIn(amountIn - this.amountOutPrevious, false);
67
- res = out + this.amountInPrevious;
68
- gas = gasSpent;
69
- }
70
- }
71
- else {
72
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountOutPrevious + amountIn, false);
73
- res = out - this.amountInPrevious;
74
- gas = gasSpent;
75
- }
76
- }
77
- else {
78
- if (this.direction) {
79
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountInPrevious + amountIn, true);
80
- res = out - this.amountOutPrevious;
81
- gas = gasSpent;
82
- }
83
- else {
84
- if (amountIn < this.amountInPrevious) {
85
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountInPrevious - amountIn, false);
86
- res = this.amountOutPrevious - inp;
87
- gas = gasSpent;
88
- }
89
- else {
90
- const { out, gasSpent } = this.pool.calcOutByIn(amountIn - this.amountInPrevious, true);
91
- res = out + this.amountOutPrevious;
92
- gas = gasSpent;
93
- }
94
- }
95
- }
96
- // this.testApply(v, amountIn, out);
97
- return { out: res, gasSpent: gas - this.spentGas };
98
- }
99
- calcInput(v, amountOut) {
100
- let res;
101
- let gas;
102
- if (v === this.vert1) {
103
- if (!this.direction) {
104
- if (amountOut < this.amountOutPrevious) {
105
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountOutPrevious - amountOut, false);
106
- res = this.amountInPrevious - out;
107
- gas = gasSpent;
108
- }
109
- else {
110
- const { inp, gasSpent } = this.pool.calcInByOut(amountOut - this.amountOutPrevious, true);
111
- res = inp + this.amountInPrevious;
112
- gas = gasSpent;
113
- }
114
- }
115
- else {
116
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountOutPrevious + amountOut, true);
117
- res = inp - this.amountInPrevious;
118
- gas = gasSpent;
119
- }
120
- }
121
- else {
122
- if (!this.direction) {
123
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountInPrevious + amountOut, false);
124
- res = inp - this.amountOutPrevious;
125
- gas = gasSpent;
126
- }
127
- else {
128
- if (amountOut < this.amountInPrevious) {
129
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountInPrevious - amountOut, true);
130
- res = this.amountOutPrevious - out;
131
- gas = gasSpent;
132
- }
133
- else {
134
- const { inp, gasSpent } = this.pool.calcInByOut(amountOut - this.amountInPrevious, false);
135
- res = inp + this.amountOutPrevious;
136
- gas = gasSpent;
137
- }
138
- }
139
- }
140
- // this.testApply(v, amountIn, out);
141
- return { inp: res, gasSpent: gas - this.spentGas };
142
- }
143
- checkMinimalLiquidityExceededAfterSwap(from, amountOut) {
144
- if (from === this.vert0) {
145
- const r1 = parseInt(this.pool.getReserve1().toString());
146
- if (this.direction) {
147
- return r1 - amountOut - this.amountOutPrevious < this.pool.minLiquidity;
148
- }
149
- else {
150
- return r1 - amountOut + this.amountOutPrevious < this.pool.minLiquidity;
151
- }
152
- }
153
- else {
154
- const r0 = parseInt(this.pool.getReserve0().toString());
155
- if (this.direction) {
156
- return r0 - amountOut + this.amountInPrevious < this.pool.minLiquidity;
157
- }
158
- else {
159
- return r0 - amountOut - this.amountInPrevious < this.pool.minLiquidity;
160
- }
161
- }
162
- }
163
- // doesn't used in production - just for testing
164
- testApply(from, amountIn, amountOut) {
165
- console.assert(this.amountInPrevious * this.amountOutPrevious >= 0);
166
- const inPrev = this.direction
167
- ? this.amountInPrevious
168
- : -this.amountInPrevious;
169
- const outPrev = this.direction
170
- ? this.amountOutPrevious
171
- : -this.amountOutPrevious;
172
- const to = from.getNeibour(this);
173
- let directionNew;
174
- let amountInNew = 0;
175
- let amountOutNew = 0;
176
- if (to) {
177
- const inInc = from === this.vert0 ? amountIn : -amountOut;
178
- const outInc = from === this.vert0 ? amountOut : -amountIn;
179
- const inNew = inPrev + inInc;
180
- const outNew = outPrev + outInc;
181
- console.assert(inNew * outNew >= 0);
182
- if (inNew >= 0) {
183
- directionNew = true;
184
- amountInNew = inNew;
185
- amountOutNew = outNew;
186
- }
187
- else {
188
- directionNew = false;
189
- amountInNew = -inNew;
190
- amountOutNew = -outNew;
191
- }
192
- }
193
- else
194
- console.error('Error 221');
195
- if (directionNew) {
196
- const calc = this.pool.calcOutByIn(amountInNew, true).out;
197
- const res = closeValues(amountOutNew, calc, 1e-6);
198
- if (!res)
199
- console.log('Err 225-1 !!', amountOutNew, calc, Math.abs(calc / amountOutNew - 1));
200
- return res;
201
- }
202
- else {
203
- const calc = this.pool.calcOutByIn(amountOutNew, false).out;
204
- const res = closeValues(amountInNew, calc, 1e-6);
205
- if (!res)
206
- console.log('Err 225-2!!', amountInNew, calc, Math.abs(calc / amountInNew - 1));
207
- return res;
208
- }
209
- }
210
- applySwap(from) {
211
- console.assert(this.amountInPrevious * this.amountOutPrevious >= 0);
212
- const inPrev = this.direction
213
- ? this.amountInPrevious
214
- : -this.amountInPrevious;
215
- const outPrev = this.direction
216
- ? this.amountOutPrevious
217
- : -this.amountOutPrevious;
218
- const to = from.getNeibour(this);
219
- const inInc = from === this.vert0 ? from.bestIncome : -to.bestIncome;
220
- const outInc = from === this.vert0 ? to.bestIncome : -from.bestIncome;
221
- const inNew = inPrev + inInc;
222
- const outNew = outPrev + outInc;
223
- console.assert(inNew * outNew >= 0);
224
- if (inNew >= 0) {
225
- this.direction = true;
226
- this.amountInPrevious = inNew;
227
- this.amountOutPrevious = outNew;
228
- }
229
- else {
230
- this.direction = false;
231
- this.amountInPrevious = -inNew;
232
- this.amountOutPrevious = -outNew;
233
- }
234
- this.pool.setCurrentFlow(inNew, -outNew, this.spentGasNew);
235
- this.spentGas = this.spentGasNew;
236
- ASSERT(() => {
237
- let precision = Math.max(1 / this.amountOutPrevious, 1 / this.amountInPrevious, 1e-9);
238
- if (precision === Infinity)
239
- precision = 1e-9;
240
- if (this.pool instanceof StableSwapRPool) {
241
- let price = this.pool.calcCurrentPriceWithoutFee(true);
242
- if (price < 1)
243
- price = 1 / price;
244
- if (price > 1e8) {
245
- // precision degradation for extreme conditions. Almost impossible situation in production
246
- precision = 2e-3;
247
- }
248
- }
249
- // if (this.pool instanceof CurveMultitokenPool)
250
- // console.log(this.amountInPrevious, this.amountOutPrevious, this.direction, this.pool.flow0, this.pool.flow1)
251
- if (this.direction) {
252
- const granularity = this.pool.granularity1();
253
- return closeValues(this.amountOutPrevious / granularity, this.pool.calcOutByIn(this.amountInPrevious, this.direction).out /
254
- granularity, precision);
255
- }
256
- else {
257
- const granularity = this.pool.granularity0();
258
- return closeValues(this.amountInPrevious / granularity, this.pool.calcOutByIn(this.amountOutPrevious, this.direction).out /
259
- granularity, precision);
260
- }
261
- }, 'Error 225');
262
- }
263
- }
264
- export class Vertice {
265
- constructor(t) {
266
- this.token = t;
267
- setTokenId(this.token);
268
- this.edges = [];
269
- this.price = 0;
270
- this.gasPrice = 0;
271
- this.bestIncome = 0;
272
- this.gasSpent = 0;
273
- this.bestTotal = 0;
274
- this.bestSource = undefined;
275
- this.checkLine = -1;
276
- }
277
- cleanTmpData() {
278
- this.bestIncome = 0;
279
- this.gasSpent = 0;
280
- this.bestTotal = 0;
281
- this.bestSource = undefined;
282
- this.checkLine = -1;
283
- }
284
- getNeibour(e) {
285
- if (!e)
286
- return undefined;
287
- return e.vert0 === this ? e.vert1 : e.vert0;
288
- }
289
- getOutputEdges() {
290
- return this.edges.filter((e) => {
291
- if (!e.canBeUsed)
292
- return false;
293
- if (e.amountInPrevious === 0)
294
- return false;
295
- if (e.direction !== (e.vert0 === this))
296
- return false;
297
- return true;
298
- });
299
- }
300
- getInputEdges() {
301
- return this.edges.filter((e) => {
302
- if (!e.canBeUsed)
303
- return false;
304
- if (e.amountInPrevious === 0)
305
- return false;
306
- if (e.direction === (e.vert0 === this))
307
- return false;
308
- return true;
309
- });
310
- }
311
- }
312
- export class Graph {
313
- // Single network usage: (pools, baseToken, gasPrice)
314
- // Multiple Network usage: (pools, networks)
315
- constructor(pools, start, baseTokenOrNetworks, gasPriceSingleNetwork, minPriceLiquidity = 0, trustedForPricingTokens, priceLogging = false) {
316
- const networks = Array.isArray(baseTokenOrNetworks)
317
- ? baseTokenOrNetworks
318
- : [
319
- {
320
- chainId: baseTokenOrNetworks.chainId,
321
- baseToken: baseTokenOrNetworks,
322
- //baseTokenPrice: 1,
323
- gasPrice: gasPriceSingleNetwork || 0,
324
- },
325
- ];
326
- setTokenId(...networks.map((n) => n.baseToken));
327
- this.vertices = [];
328
- this.edges = [];
329
- this.tokens = new Map();
330
- pools.forEach((p) => {
331
- const v0 = this.getOrCreateVertice(p.token0);
332
- const v1 = this.getOrCreateVertice(p.token1);
333
- const edge = new Edge(p, v0, v1);
334
- v0.edges.push(edge);
335
- v1.edges.push(edge);
336
- this.edges.push(edge);
337
- });
338
- // networks.forEach((n) => {
339
- // const baseVert = this.getVert(n.baseToken)
340
- // if (baseVert) {
341
- // this.setPricesStable(baseVert, n.baseTokenPrice, n.gasPrice, true)
342
- // }
343
- // })
344
- // Lets try to price from the first trusted token first
345
- const startToken = trustedForPricingTokens
346
- ? trustedForPricingTokens[0] ?? start
347
- : start;
348
- setTokenId(startToken);
349
- const startV = this.getVert(startToken);
350
- if (startV !== undefined)
351
- this.setPricesStable(startV, 1, networks, minPriceLiquidity, trustedForPricingTokens, priceLogging);
352
- }
353
- getVert(t) {
354
- return this.tokens.get(t.tokenId);
355
- }
356
- cleanTmpData() {
357
- this.edges.forEach((e) => e.cleanTmpData());
358
- this.vertices.forEach((v) => v.cleanTmpData());
359
- }
360
- // Set prices using greedy algorithm
361
- setPricesStable(from, price, networks, minLiquidity = 0, trustedTokens, logging = false) {
362
- const processedVert = new Set();
363
- let nextEdges = [];
364
- const trustedTokensSet = new Set(trustedTokens?.map((t) => t.address) ?? []);
365
- if (trustedTokens)
366
- trustedTokensSet.add(from.token.address);
367
- function addVertice(v, price) {
368
- v.price = price;
369
- processedVert.add(v);
370
- if (trustedTokens && !trustedTokensSet.has(v.token.address))
371
- return;
372
- const newEdges = v.edges
373
- .filter((e) => !processedVert.has(v.getNeibour(e)))
374
- .map((e) => {
375
- const liquidity = price * Number(e.reserve(v));
376
- return [liquidity, e];
377
- })
378
- .filter(([liquidity, e]) => e.pool.alwaysAppropriateForPricing() ||
379
- (liquidity >= minLiquidity && e.pool.isPoolAppropriateForPricing()));
380
- nextEdges = fastArrayMerge(nextEdges, newEdges);
381
- }
382
- if (logging)
383
- console.log(`Pricing: Initial token ${from.token.symbol} price=${price}`);
384
- addVertice(from, price);
385
- while (nextEdges.length > 0) {
386
- const [liquidity, bestEdge] = nextEdges.pop();
387
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
388
- ? [bestEdge.vert1, bestEdge.vert0]
389
- : [bestEdge.vert0, bestEdge.vert1];
390
- if (processedVert.has(vTo))
391
- continue;
392
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
393
- if (logging)
394
- console.log(`Pricing: + Token ${vTo.token.symbol} price=${vFrom.price * p}` +
395
- ` from ${vFrom.token.symbol} pool=${bestEdge.pool.address} liquidity=${liquidity}`);
396
- addVertice(vTo, vFrom.price * p);
397
- }
398
- const gasPrice = new Map();
399
- networks.forEach((n) => {
400
- const vPrice = this.getVert(n.baseToken)?.price || 0;
401
- gasPrice.set(n.chainId, n.gasPrice * vPrice);
402
- });
403
- processedVert.forEach((v) => {
404
- const gasPriceChainId = gasPrice.get(v.token.chainId);
405
- if (gasPriceChainId === undefined)
406
- console.error(`Error 427: token {${v.token.address} ${v.token.symbol}}` +
407
- ` has unknown chainId ${v.token.chainId} (${typeof v.token
408
- .chainId}).` +
409
- `Known chainIds: ${Array.from(gasPrice.keys()).map((k) => `"${k}"(${typeof k})`)}`);
410
- if (v.price === 0)
411
- console.error(`Error 428: token {${v.token.address} ${v.token.symbol} ${v.token.chainId}} was not priced`);
412
- v.gasPrice = gasPriceChainId / v.price;
413
- });
414
- }
415
- getPriceReasoning(from, price, token, minLiquidity = 0, trustedTokens) {
416
- const processedVert = new Set();
417
- const vertToPricingInfo = new Map();
418
- let nextEdges = [];
419
- const trustedTokensSet = new Set(trustedTokens?.map((t) => t.address) ?? []);
420
- if (trustedTokens)
421
- trustedTokensSet.add(from.address);
422
- function addVertice(parent, v, price, poolPrice, liquidity, edge) {
423
- processedVert.add(v);
424
- vertToPricingInfo.set(v, {
425
- parent: vertToPricingInfo.get(parent),
426
- vert: v,
427
- price,
428
- poolPrice,
429
- liquidity,
430
- edge,
431
- });
432
- if (trustedTokens && !trustedTokensSet.has(v.token.address))
433
- return;
434
- const newEdges = v.edges
435
- .filter((e) => !processedVert.has(v.getNeibour(e)))
436
- .map((e) => {
437
- const liquidity = price * Number(e.reserve(v));
438
- return [liquidity, e];
439
- })
440
- .filter(([liquidity, e]) => e.pool.alwaysAppropriateForPricing() ||
441
- (liquidity >= minLiquidity && e.pool.isPoolAppropriateForPricing()));
442
- nextEdges = fastArrayMerge(nextEdges, newEdges);
443
- }
444
- setTokenId(from);
445
- addVertice(undefined, this.tokens.get(from.tokenId), price / 10 ** from.decimals, 1, 0, undefined);
446
- while (nextEdges.length > 0) {
447
- const [liquidity, bestEdge] = nextEdges.pop();
448
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
449
- ? [bestEdge.vert1, bestEdge.vert0]
450
- : [bestEdge.vert0, bestEdge.vert1];
451
- if (processedVert.has(vTo))
452
- continue;
453
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
454
- const pPrice = vertToPricingInfo.get(vFrom)?.price;
455
- addVertice(vFrom, vTo, pPrice * p, p, liquidity, bestEdge);
456
- if (vTo.token.address === token) {
457
- const lines = [];
458
- let t = vertToPricingInfo.get(vTo);
459
- while (t !== undefined) {
460
- const dec = t.vert.token.decimals;
461
- const parentDecExp = t.parent?.vert.token.decimals ?? dec;
462
- lines.push(`Token ${t.vert.token.symbol} (${t.vert.token.address}) price is ${t.price * 10 ** dec}$`);
463
- if (t.edge !== undefined) {
464
- lines.push(`Pool ${t.edge.pool.address} liquidity ${Math.round(t.liquidity)}$ price ${(t.poolPrice / 10 ** parentDecExp) * 10 ** dec}`);
465
- }
466
- t = t.parent;
467
- }
468
- return lines.reverse();
469
- }
470
- }
471
- return ['Token is not priced'];
472
- }
473
- // Set prices using greedy algorithm
474
- setPricesStableInsideChain(from, price, gasPrice) {
475
- const processedVert = new Set();
476
- let nextEdges = [];
477
- const edgeValues = new Map();
478
- const value = (e) => edgeValues.get(e);
479
- function addVertice(v, price, gasPrice) {
480
- v.price = price;
481
- v.gasPrice = gasPrice;
482
- const newEdges = v.edges.filter((e) => {
483
- const newV = v.getNeibour(e);
484
- return (newV?.token.chainId === v.token.chainId &&
485
- !processedVert.has(v.getNeibour(e)));
486
- });
487
- newEdges.forEach((e) => edgeValues.set(e, price * parseInt(e.reserve(v).toString())));
488
- newEdges.sort((e1, e2) => value(e1) - value(e2));
489
- const res = [];
490
- while (nextEdges.length && newEdges.length) {
491
- if (value(nextEdges[0]) < value(newEdges[0]))
492
- res.push(nextEdges.shift());
493
- else
494
- res.push(newEdges.shift());
495
- }
496
- nextEdges = [...res, ...nextEdges, ...newEdges];
497
- processedVert.add(v);
498
- }
499
- addVertice(from, price, gasPrice);
500
- while (nextEdges.length > 0) {
501
- const bestEdge = nextEdges.pop();
502
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
503
- ? [bestEdge.vert1, bestEdge.vert0]
504
- : [bestEdge.vert0, bestEdge.vert1];
505
- if (processedVert.has(vTo))
506
- continue;
507
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
508
- addVertice(vTo, vFrom.price * p, vFrom.gasPrice / p);
509
- }
510
- }
511
- // Set prices by search in depth
512
- setPrices(from, price, gasPrice) {
513
- if (from.price !== 0)
514
- return;
515
- from.price = price;
516
- from.gasPrice = gasPrice;
517
- const edges = from.edges
518
- .map((e) => [e, parseInt(e.reserve(from).toString())])
519
- .sort(([, r1], [, r2]) => r2 - r1);
520
- edges.forEach(([e]) => {
521
- const v = e.vert0 === from ? e.vert1 : e.vert0;
522
- if (v.price !== 0)
523
- return;
524
- const p = e.pool.calcCurrentPriceWithoutFee(from === e.vert1);
525
- this.setPrices(v, price * p, gasPrice / p);
526
- });
527
- }
528
- getOrCreateVertice(token) {
529
- let vert = this.getVert(token);
530
- if (vert)
531
- return vert;
532
- vert = new Vertice(token);
533
- this.vertices.push(vert);
534
- this.tokens.set(token.tokenId, vert);
535
- return vert;
536
- }
537
- /*exportPath(from: RToken, to: RToken) {
538
-
539
- const fromVert = this.getVert(from) as Vertice
540
- const toVert = this.getVert(to) as Vertice
541
- const initValue = (fromVert.bestIncome * fromVert.price) / toVert.price
542
-
543
- const route = new Set<Edge>()
544
- for (let v = toVert; v !== fromVert; v = v.getNeibour(v.bestSource) as Vertice) {
545
- if (v.bestSource) route.add(v.bestSource)
546
- }
547
-
548
- function edgeStyle(e: Edge) {
549
- const finish = e.vert1.bestSource === e
550
- const start = e.vert0.bestSource === e
551
- let label
552
- if (e.bestEdgeIncome === -1) label = 'label: "low_liq"'
553
- if (e.bestEdgeIncome !== 0) label = `label: "${print((e.bestEdgeIncome / initValue - 1) * 100, 3)}%"`
554
- const edgeValue = route.has(e) ? 'value: 2' : undefined
555
- let arrow
556
- if (finish && start) arrow = 'arrows: "from,to"'
557
- if (finish) arrow = 'arrows: "to"'
558
- if (start) arrow = 'arrows: "from"'
559
- return ['', label, edgeValue, arrow].filter((a) => a !== undefined).join(', ')
560
- }
561
-
562
- function print(n: number, digits: number) {
563
- let out
564
- if (n === 0) out = '0'
565
- else {
566
- const n0 = n > 0 ? n : -n
567
- const shift = digits - Math.ceil(Math.log(n0) / Math.LN10)
568
- if (shift <= 0) out = `${Math.round(n0)}`
569
- else {
570
- const mult = Math.pow(10, shift)
571
- out = `${Math.round(n0 * mult) / mult}`
572
- }
573
- if (n < 0) out = -out
574
- }
575
- return out
576
- }
577
-
578
- function nodeLabel(v: Vertice) {
579
- const value = (v.bestIncome * v.price) / toVert.price
580
- const income = `${print(value, 3)}`
581
- const total = `${print(v.bestTotal, 3)}`
582
- // const income = `${print((value/initValue-1)*100, 3)}%`
583
- // const total = `${print((v.bestTotal/initValue-1)*100, 3)}%`
584
- const checkLine = v.checkLine === -1 ? undefined : `${v.checkLine}`
585
- return [checkLine, income, total].filter((a) => a !== undefined).join(':')
586
- }
587
-
588
- const nodes = `var nodes = new vis.DataSet([
589
- ${this.vertices.map((t) => `{ id: ${t.token.name}, label: "${nodeLabel(t)}"}`).join(',\n\t\t')}
590
- ]);\n`
591
- const edges = `var edges = new vis.DataSet([
592
- ${this.edges
593
- .map((p) => `{ from: ${p.vert0.token.name}, to: ${p.vert1.token.name}${edgeStyle(p)}}`)
594
- .join(',\n\t\t')}
595
- ]);\n`
596
- const data = `var data = {
597
- nodes: nodes,
598
- edges: edges,
599
- };\n`
600
-
601
- // TODO: This should be removed, this pacakge will not be installable on a client while this remains.
602
- const fs = require("fs");
603
- fs.writeFileSync(
604
- "D:/Info/Notes/GraphVisualization/data.js",
605
- nodes + edges + data
606
- );
607
- }*/
608
- findBestPathExactIn(from, to, amountIn) {
609
- const start = this.getVert(from);
610
- const finish = this.getVert(to);
611
- if (!start || !finish || Number.isNaN(finish.price))
612
- return undefined;
613
- this.edges.forEach((e) => {
614
- e.bestEdgeIncome = 0;
615
- e.spentGasNew = 0;
616
- });
617
- this.vertices.forEach((v) => {
618
- v.bestIncome = 0;
619
- v.gasSpent = 0;
620
- v.bestTotal = 0;
621
- v.bestSource = undefined;
622
- v.checkLine = -1;
623
- });
624
- start.bestIncome = amountIn;
625
- start.bestTotal = amountIn;
626
- const processedVert = new Set();
627
- const nextVertList = [start]; // TODO: Use sorted Set!
628
- let debug_info = '';
629
- let checkLine = 0;
630
- for (;;) {
631
- let closestVert;
632
- let closestTotal;
633
- let closestPosition = 0;
634
- nextVertList.forEach((v, i) => {
635
- if (closestTotal === undefined || v.bestTotal > closestTotal) {
636
- closestTotal = v.bestTotal;
637
- closestVert = v;
638
- closestPosition = i;
639
- }
640
- });
641
- if (!closestVert)
642
- return undefined;
643
- closestVert.checkLine = checkLine++;
644
- if (closestVert === finish) {
645
- const bestPath = [];
646
- for (let v = finish; v?.bestSource; v = v.getNeibour(v.bestSource)) {
647
- bestPath.unshift(v.bestSource);
648
- }
649
- DEBUG(() => console.log(debug_info));
650
- return {
651
- path: bestPath,
652
- output: finish.bestIncome,
653
- gasSpent: finish.gasSpent,
654
- totalOutput: finish.bestTotal,
655
- };
656
- }
657
- nextVertList.splice(closestPosition, 1);
658
- closestVert.edges.forEach((e) => {
659
- const v2 = closestVert === e.vert0 ? e.vert1 : e.vert0;
660
- if (processedVert.has(v2))
661
- return;
662
- // multitoken pool protection. Don't use two pools from one multipool in one path (but is possible in
663
- // different paths => in one route). It is not better then use one pool (For curve at least)
664
- // and it is calculated wrong (with no flow applying)
665
- if (e.pool.address === closestVert.bestSource?.pool.address)
666
- return;
667
- let newIncome;
668
- let gas;
669
- try {
670
- const { out, gasSpent } = e.calcOutput(closestVert, closestVert.bestIncome);
671
- if (!Number.isFinite(out) || !Number.isFinite(gasSpent))
672
- // Math errors protection
673
- return;
674
- newIncome = out;
675
- gas = gasSpent;
676
- }
677
- catch (_err) {
678
- // Any arithmetic error or out-of-liquidity
679
- e.bestEdgeIncome = -1;
680
- return;
681
- }
682
- // if (e.checkMinimalLiquidityExceededAfterSwap(closestVert as Vertice, newIncome)) {
683
- // e.bestEdgeIncome = -1
684
- // return
685
- // }
686
- const newGasSpent = closestVert.gasSpent + gas;
687
- const price = v2.price / finish.price;
688
- const gasPrice = v2.gasPrice * price;
689
- const newTotal = newIncome * price - newGasSpent * gasPrice;
690
- console.assert(e.bestEdgeIncome === 0, 'Error 373');
691
- e.bestEdgeIncome = newIncome * price;
692
- e.spentGasNew = e.spentGas + gas;
693
- if (!v2.bestSource)
694
- nextVertList.push(v2);
695
- if (!v2.bestSource || newTotal > v2.bestTotal) {
696
- DEBUG(() => {
697
- const st = closestVert?.token === from ? '*' : '';
698
- const fn = v2?.token === to ? '*' : '';
699
- debug_info += `${st}${closestVert?.token.name}->${v2.token.name}${fn} ${v2.bestIncome} -> ${newIncome}\n`;
700
- });
701
- v2.bestIncome = newIncome;
702
- v2.gasSpent = newGasSpent;
703
- v2.bestTotal = newTotal;
704
- v2.bestSource = e;
705
- }
706
- });
707
- processedVert.add(closestVert);
708
- }
709
- }
710
- findBestPathExactOut(from, to, amountOut) {
711
- const start = this.getVert(to);
712
- const finish = this.getVert(from);
713
- if (!start || !finish)
714
- return undefined;
715
- this.edges.forEach((e) => {
716
- e.bestEdgeIncome = 0;
717
- e.spentGasNew = 0;
718
- });
719
- this.vertices.forEach((v) => {
720
- v.bestIncome = 0;
721
- v.gasSpent = 0;
722
- v.bestTotal = 0;
723
- v.bestSource = undefined;
724
- v.checkLine = -1;
725
- });
726
- start.bestIncome = amountOut;
727
- start.bestTotal = amountOut;
728
- const processedVert = new Set();
729
- const nextVertList = [start]; // TODO: Use sorted Set!
730
- let debug_info = '';
731
- let checkLine = 0;
732
- for (;;) {
733
- let closestVert;
734
- let closestTotal;
735
- let closestPosition = 0;
736
- nextVertList.forEach((v, i) => {
737
- if (closestTotal === undefined || v.bestTotal < closestTotal) {
738
- closestTotal = v.bestTotal;
739
- closestVert = v;
740
- closestPosition = i;
741
- }
742
- });
743
- if (!closestVert)
744
- return undefined;
745
- closestVert.checkLine = checkLine++;
746
- if (closestVert === finish) {
747
- const bestPath = [];
748
- for (let v = finish; v?.bestSource; v = v.getNeibour(v.bestSource)) {
749
- bestPath.push(v.bestSource);
750
- }
751
- DEBUG(() => console.log(debug_info));
752
- return {
753
- path: bestPath,
754
- input: finish.bestIncome,
755
- gasSpent: finish.gasSpent,
756
- totalInput: finish.bestTotal,
757
- };
758
- }
759
- nextVertList.splice(closestPosition, 1);
760
- closestVert.edges.forEach((e) => {
761
- const v2 = closestVert === e.vert0 ? e.vert1 : e.vert0;
762
- if (processedVert.has(v2))
763
- return;
764
- let newIncome;
765
- let gas;
766
- try {
767
- const { inp, gasSpent } = e.calcInput(closestVert, closestVert.bestIncome);
768
- if (!Number.isFinite(inp) || !Number.isFinite(gasSpent))
769
- // Math errors protection
770
- return;
771
- if (inp < 0)
772
- return; // No enouph liquidity in the pool
773
- newIncome = inp;
774
- gas = gasSpent;
775
- }
776
- catch (_e) {
777
- // Any arithmetic error or out-of-liquidity
778
- return;
779
- }
780
- const newGasSpent = closestVert.gasSpent + gas;
781
- const price = v2.price / finish.price;
782
- const gasPrice = v2.gasPrice * price;
783
- const newTotal = newIncome * price + newGasSpent * gasPrice;
784
- console.assert(e.bestEdgeIncome === 0, 'Error 373');
785
- e.bestEdgeIncome = newIncome * price;
786
- e.spentGasNew = e.spentGas + gas;
787
- if (!v2.bestSource)
788
- nextVertList.push(v2);
789
- if (!v2.bestSource || newTotal < v2.bestTotal) {
790
- DEBUG(() => {
791
- const st = v2?.token === from ? '*' : '';
792
- const fn = closestVert?.token === to ? '*' : '';
793
- debug_info += `${st}${closestVert?.token.name}<-${v2.token.name}${fn} ${v2.bestIncome} -> ${newIncome}\n`;
794
- });
795
- v2.bestIncome = newIncome;
796
- v2.gasSpent = newGasSpent;
797
- v2.bestTotal = newTotal;
798
- v2.bestSource = e;
799
- }
800
- });
801
- processedVert.add(closestVert);
802
- }
803
- }
804
- addPath(from, to, path) {
805
- let _from = from;
806
- path.forEach((e) => {
807
- if (_from) {
808
- e.applySwap(_from);
809
- _from = _from.getNeibour(e);
810
- }
811
- else {
812
- console.error('Unexpected 315');
813
- }
814
- });
815
- ASSERT(() => {
816
- const res = this.vertices.every((v) => {
817
- let total = 0;
818
- let totalModule = 0;
819
- v.edges.forEach((e) => {
820
- if (e.vert0 === v) {
821
- if (e.direction) {
822
- total -= e.amountInPrevious;
823
- }
824
- else {
825
- total += e.amountInPrevious;
826
- }
827
- totalModule += e.amountInPrevious;
828
- }
829
- else {
830
- if (e.direction) {
831
- total += e.amountOutPrevious;
832
- }
833
- else {
834
- total -= e.amountOutPrevious;
835
- }
836
- totalModule += e.amountOutPrevious;
837
- }
838
- });
839
- if (v === from)
840
- return total <= 0;
841
- if (v === to)
842
- return total >= 0;
843
- if (totalModule === 0)
844
- return total === 0;
845
- return Math.abs(total / totalModule) < 1e10;
846
- });
847
- return res;
848
- }, 'Error 290');
849
- }
850
- getPrimaryPriceForPath(from, path) {
851
- let p = 1;
852
- let prevToken = from;
853
- path.forEach((edge) => {
854
- const direction = edge.vert0 === prevToken;
855
- const edgePrice = edge.pool.calcCurrentPriceWithoutFee(direction);
856
- p *= edgePrice;
857
- prevToken = prevToken.getNeibour(edge);
858
- });
859
- return p;
860
- }
861
- findBestRouteExactIn(from, to, amountIn, mode) {
862
- let amountInBI;
863
- if (typeof amountIn === 'bigint') {
864
- amountInBI = amountIn;
865
- amountIn = Number(amountIn);
866
- }
867
- else {
868
- amountInBI = getBigInt(amountIn);
869
- }
870
- let routeValues = [];
871
- if (Array.isArray(mode)) {
872
- const sum = mode.reduce((a, b) => a + b, 0);
873
- routeValues = mode.map((e) => e / sum);
874
- }
875
- else {
876
- for (let i = 0; i < mode; ++i)
877
- routeValues.push(1 / mode);
878
- }
879
- this.edges.forEach((e) => {
880
- e.amountInPrevious = 0;
881
- e.amountOutPrevious = 0;
882
- e.direction = true;
883
- e.pool.cleanTmpData();
884
- });
885
- let output = 0;
886
- let gasSpentInit = 0;
887
- let totalOutput = 0;
888
- let totalrouted = 0;
889
- let primaryPrice;
890
- let step;
891
- for (step = 0; step < routeValues.length; ++step) {
892
- const routeValue = routeValues[step];
893
- const p = this.findBestPathExactIn(from, to, amountIn * routeValue);
894
- if (!p) {
895
- break;
896
- }
897
- else {
898
- output += p.output;
899
- gasSpentInit += p.gasSpent;
900
- totalOutput += p.totalOutput;
901
- this.addPath(this.getVert(from), this.getVert(to), p.path);
902
- totalrouted += routeValue;
903
- }
904
- }
905
- if (step === 0 || output === 0)
906
- return NoWayMultiRoute(from, to);
907
- let status;
908
- if (step < routeValues.length)
909
- status = RouteStatus.Partial;
910
- else
911
- status = RouteStatus.Success;
912
- this.removeEdgesWithLowFlow(0.001);
913
- const fromVert = this.getVert(from);
914
- const toVert = this.getVert(to);
915
- const { legs, gasSpent } = this.getRouteLegs(fromVert, toVert);
916
- console.assert(gasSpent <= gasSpentInit, 'Internal Error 491');
917
- //if (topologyWasChanged || removedEdgesNumber > 0) {
918
- this.edges.forEach((e) => e.pool.cleanTmpData());
919
- // const initialOutput = output
920
- // const assumeZero = legs.some(l => l.assumedAmountOut < 1)
921
- // if (Math.abs(output - 17947093338.87304) < 1e-3) {
922
- // debugger
923
- // }
924
- output = this.updateLegsAmountOut(legs, amountIn * totalrouted);
925
- // const diff = (output - initialOutput)/initialOutput
926
- // if (Math.abs(diff) > 1e-4 && !assumeZero) console.log(` Output recalc: ${diff} ${initialOutput}=>${output}`)
927
- totalOutput = output - toVert.gasPrice * gasSpent;
928
- if (output === 0) {
929
- status = RouteStatus.NoWay;
930
- totalOutput = 0;
931
- }
932
- //}
933
- let swapPrice;
934
- let priceImpact;
935
- try {
936
- swapPrice = output / amountIn / totalrouted;
937
- const priceTo = this.getVert(to)?.price;
938
- const priceFrom = this.getVert(from)?.price;
939
- primaryPrice = priceTo && priceFrom ? priceFrom / priceTo : undefined;
940
- priceImpact =
941
- primaryPrice !== undefined ? 1 - swapPrice / primaryPrice : undefined;
942
- }
943
- catch (_e) {
944
- /* skip division by 0 errors*/
945
- }
946
- return {
947
- status,
948
- fromToken: from,
949
- toToken: to,
950
- primaryPrice,
951
- swapPrice,
952
- priceImpact,
953
- amountIn: amountIn * totalrouted,
954
- amountInBI: status === RouteStatus.Success
955
- ? amountInBI
956
- : getBigInt(amountIn * totalrouted),
957
- amountOut: output,
958
- amountOutBI: getBigInt(output),
959
- legs,
960
- gasSpent,
961
- totalAmountOut: totalOutput,
962
- totalAmountOutBI: getBigInt(totalOutput),
963
- };
964
- }
965
- findBestRouteExactOut(from, to, amountOut, mode) {
966
- let routeValues = [];
967
- if (Array.isArray(mode)) {
968
- const sum = mode.reduce((a, b) => a + b, 0);
969
- routeValues = mode.map((e) => e / sum);
970
- }
971
- else {
972
- for (let i = 0; i < mode; ++i)
973
- routeValues.push(1 / mode);
974
- }
975
- this.edges.forEach((e) => {
976
- e.amountInPrevious = 0;
977
- e.amountOutPrevious = 0;
978
- e.direction = true;
979
- });
980
- let input = 0;
981
- let gasSpentInit = 0;
982
- //let totalInput = 0
983
- let totalrouted = 0;
984
- let primaryPrice;
985
- let step;
986
- for (step = 0; step < routeValues.length; ++step) {
987
- const routeValue = routeValues[step];
988
- const p = this.findBestPathExactOut(from, to, amountOut * routeValue);
989
- if (!p) {
990
- break;
991
- }
992
- else {
993
- input += p.input;
994
- gasSpentInit += p.gasSpent;
995
- //totalInput += p.totalInput
996
- this.addPath(this.getVert(from), this.getVert(to), p.path);
997
- totalrouted += routeValue;
998
- // if (step === 0) {
999
- // primaryPrice = this.getPrimaryPriceForPath(this.getVert(from) as Vertice, p.path)
1000
- // }
1001
- }
1002
- }
1003
- if (step === 0)
1004
- return NoWayMultiRoute(from, to);
1005
- let status;
1006
- if (step < routeValues.length)
1007
- status = RouteStatus.Partial;
1008
- else
1009
- status = RouteStatus.Success;
1010
- const removedEdgesNumber = this.removeEdgesWithLowFlow(0.001);
1011
- const fromVert = this.getVert(from);
1012
- const toVert = this.getVert(to);
1013
- const { legs, gasSpent, topologyWasChanged } = this.getRouteLegs(fromVert, toVert);
1014
- console.assert(gasSpent <= gasSpentInit, 'Internal Error 491');
1015
- if (topologyWasChanged || removedEdgesNumber > 0) {
1016
- input = this.calcLegsAmountIn(legs, amountOut);
1017
- }
1018
- let swapPrice;
1019
- let priceImpact;
1020
- try {
1021
- swapPrice = amountOut / input;
1022
- const priceTo = this.getVert(to)?.price;
1023
- const priceFrom = this.getVert(from)?.price;
1024
- primaryPrice = priceTo && priceFrom ? priceFrom / priceTo : undefined;
1025
- priceImpact =
1026
- primaryPrice !== undefined ? 1 - swapPrice / primaryPrice : undefined;
1027
- }
1028
- catch (_e) {
1029
- /* skip division by 0 errors*/
1030
- }
1031
- return {
1032
- status,
1033
- fromToken: from,
1034
- toToken: to,
1035
- primaryPrice,
1036
- swapPrice,
1037
- priceImpact,
1038
- amountIn: input,
1039
- amountInBI: getBigInt(input),
1040
- amountOut: amountOut * totalrouted,
1041
- amountOutBI: getBigInt(amountOut * totalrouted),
1042
- legs,
1043
- gasSpent,
1044
- totalAmountOut: amountOut - gasSpent * toVert.gasPrice, // TODO: should be totalAmountIn instead !!!!
1045
- totalAmountOutBI: getBigInt(amountOut - gasSpent * toVert.gasPrice), // TODO: should be totalAmountInBI instead !!!!
1046
- };
1047
- }
1048
- getRouteLegs(from, to) {
1049
- const { vertices, topologyWasChanged } = this.cleanTopology(from, to);
1050
- const legs = [];
1051
- let gasSpent = 0;
1052
- vertices.forEach((n) => {
1053
- const outEdges = n.getOutputEdges().map((e) => {
1054
- const from = this.edgeFrom(e);
1055
- return from ? [e, from.vert, from.amount] : [e];
1056
- });
1057
- let outAmount = outEdges.reduce((a, b) => a + b[2], 0);
1058
- if (outAmount <= 0)
1059
- return;
1060
- const total = outAmount;
1061
- // IMPORTANT: Casting to Number to avoid compiler bug which for some reason
1062
- // keeps reference to outAmount which is mutated later
1063
- // const total = Number(outAmount)
1064
- // const totalTest = outAmount
1065
- outEdges.forEach((e, i) => {
1066
- const p = e[2];
1067
- const quantity = i + 1 === outEdges.length ? 1 : p / outAmount;
1068
- const edge = e[0];
1069
- legs.push({
1070
- poolAddress: edge.pool.address,
1071
- uniqueId: edge.pool.uniqueID(),
1072
- poolType: edge.pool.poolType(),
1073
- poolFee: edge.pool.fee,
1074
- tokenFrom: n.token,
1075
- tokenTo: n.getNeibour(edge).token,
1076
- assumedAmountIn: edge.direction
1077
- ? edge.amountInPrevious
1078
- : edge.amountOutPrevious,
1079
- assumedAmountOut: edge.direction
1080
- ? edge.amountOutPrevious
1081
- : edge.amountInPrevious,
1082
- swapPortion: quantity,
1083
- absolutePortion: p / total,
1084
- });
1085
- gasSpent += edge.pool.calcOutByIn(edge.direction ? edge.amountInPrevious : edge.amountOutPrevious, edge.direction).gasSpent;
1086
- // console.debug('before amountOut mutation', { total, outAmount })
1087
- outAmount -= p;
1088
- // console.debug('after amountOut mutation', { total, outAmount })
1089
- });
1090
- // console.debug('AFTER', { outAmount, total, totalTest }, outAmount / total)
1091
- console.assert(outAmount / total < 1e-12, 'Error 281');
1092
- });
1093
- return { legs, gasSpent, topologyWasChanged };
1094
- }
1095
- edgeFrom(e) {
1096
- if (e.amountInPrevious === 0)
1097
- return undefined;
1098
- return e.direction
1099
- ? { vert: e.vert0, amount: e.amountInPrevious }
1100
- : { vert: e.vert1, amount: e.amountOutPrevious };
1101
- }
1102
- // Removes all edges that have lesser than minFraction portion of vertex output liquidity
1103
- // Such edges can appear as an accumulation of forward and backward streams
1104
- // They spend more gas than provide slippage reduction
1105
- // They confuse users
1106
- // Also, route processor can fail trying to process them
1107
- removeEdgesWithLowFlow(minFraction) {
1108
- const weakEdgeList = [];
1109
- this.vertices.forEach((v) => {
1110
- const outEdges = v.getOutputEdges();
1111
- if (outEdges.length <= 1)
1112
- return;
1113
- const amounts = outEdges.map((e) => {
1114
- const data = this.edgeFrom(e);
1115
- if (data !== undefined)
1116
- return data.amount;
1117
- console.error('Tines: Internal Error 1123');
1118
- return undefined;
1119
- });
1120
- const totalOut = amounts.reduce((a, b) => {
1121
- a += b;
1122
- return a;
1123
- }, 0);
1124
- outEdges.forEach((e, i) => {
1125
- if (amounts[i] / totalOut < minFraction)
1126
- weakEdgeList.push(e);
1127
- });
1128
- });
1129
- weakEdgeList.forEach((e) => {
1130
- e.canBeUsed = false;
1131
- });
1132
- return weakEdgeList.length;
1133
- }
1134
- // returns route output
1135
- updateLegsAmountOut(legs, amountIn) {
1136
- if (legs.length === 0)
1137
- return 0;
1138
- const amounts = new Map();
1139
- amounts.set(legs[0].tokenFrom.tokenId, amountIn);
1140
- legs.forEach((l) => {
1141
- const vert = this.getVert(l.tokenFrom);
1142
- console.assert(vert !== undefined, 'Internal Error 570');
1143
- const edge = vert.edges.find((e) => e.pool.uniqueID() === l.uniqueId);
1144
- console.assert(edge !== undefined, 'Internel Error 569');
1145
- const pool = edge.pool;
1146
- const direction = vert === edge.vert0;
1147
- const inputTotal = amounts.get(l.tokenFrom.tokenId);
1148
- console.assert(inputTotal !== undefined, 'Internal Error 564');
1149
- const routerPortion = Math.round(l.swapPortion * ROUTER_DISTRIBUTION_PORTION) /
1150
- ROUTER_DISTRIBUTION_PORTION;
1151
- const input = Math.floor(inputTotal * routerPortion);
1152
- amounts.set(l.tokenFrom.tokenId, inputTotal - input);
1153
- const output = pool.calcOutByInReal(input, direction);
1154
- const vertNext = vert.getNeibour(edge);
1155
- const prevAmount = amounts.get(vertNext.token.tokenId);
1156
- amounts.set(vertNext.token.tokenId, (prevAmount || 0) + output);
1157
- l.assumedAmountIn = input;
1158
- l.assumedAmountOut = output;
1159
- });
1160
- return (amounts.get(legs[legs.length - 1].tokenTo.tokenId) || 0);
1161
- }
1162
- // TODO: make full test coverage!
1163
- calcLegsAmountIn(legs, amountOut) {
1164
- const totalOutputAssumed = new Map();
1165
- legs.forEach((l) => {
1166
- const prevValue = totalOutputAssumed.get(l.tokenFrom.tokenId) || 0;
1167
- totalOutputAssumed.set(l.tokenFrom.tokenId, prevValue + l.assumedAmountOut);
1168
- });
1169
- const amounts = new Map();
1170
- amounts.set(legs[legs.length - 1].tokenTo.tokenId, amountOut);
1171
- for (let i = legs.length - 1; i >= 0; --i) {
1172
- const l = legs[i];
1173
- const vert = this.getVert(l.tokenTo);
1174
- console.assert(vert !== undefined, 'Internal Error 884');
1175
- const edge = vert.edges.find((e) => e.pool.uniqueID() === l.uniqueId);
1176
- console.assert(edge !== undefined, 'Internel Error 888');
1177
- const pool = edge.pool;
1178
- const direction = vert === edge.vert1;
1179
- const outputTotal = amounts.get(l.tokenTo.tokenId);
1180
- console.assert(outputTotal !== undefined, 'Internal Error 893');
1181
- const totalAssumed = totalOutputAssumed.get(l.tokenFrom.tokenId);
1182
- console.assert(totalAssumed !== undefined, 'Internal Error 903');
1183
- const output = (outputTotal * l.assumedAmountOut) /
1184
- totalAssumed;
1185
- const input = pool.calcInByOut(output, direction).inp;
1186
- const vertNext = vert.getNeibour(edge);
1187
- const prevAmount = amounts.get(vertNext.token.tokenId);
1188
- amounts.set(vertNext.token.tokenId, (prevAmount || 0) + input);
1189
- }
1190
- return amounts.get(legs[0].tokenFrom.tokenId) || 0;
1191
- }
1192
- // removes all cycles if there are any, then removes all dead end could appear after cycle removing
1193
- // Returns clean result topologically sorted
1194
- cleanTopology(from, to) {
1195
- let topologyWasChanged = false;
1196
- let result = this.topologySort(from, to);
1197
- if (result.status !== 2) {
1198
- topologyWasChanged = true;
1199
- // it can be 3, because we make this.removeEdgesWithLowFlow(0.005) before
1200
- // console.assert(result.status === 0, 'Internal Error 554')
1201
- while (result.status === 0) {
1202
- this.removeWeakestEdge(result.vertices);
1203
- result = this.topologySort(from, to);
1204
- }
1205
- if (result.status === 3) {
1206
- this.removeDeadEnds(result.vertices);
1207
- result = this.topologySort(from, to);
1208
- }
1209
- console.assert(result.status === 2, 'Internal Error 563');
1210
- if (result.status !== 2)
1211
- return { vertices: [], topologyWasChanged };
1212
- }
1213
- return { vertices: result.vertices, topologyWasChanged };
1214
- }
1215
- removeDeadEnds(verts) {
1216
- verts.forEach((v) => {
1217
- v.getInputEdges().forEach((e) => {
1218
- e.canBeUsed = false;
1219
- });
1220
- });
1221
- }
1222
- removeWeakestEdge(verts) {
1223
- let minVert = undefined;
1224
- let minVertNext;
1225
- let minOutput = Number.MAX_VALUE;
1226
- verts.forEach((v1, i) => {
1227
- const v2 = i === 0 ? verts[verts.length - 1] : verts[i - 1];
1228
- let out = 0;
1229
- v1.getOutputEdges().forEach((e) => {
1230
- if (v1.getNeibour(e) !== v2)
1231
- return;
1232
- out += e.direction ? e.amountOutPrevious : e.amountInPrevious;
1233
- });
1234
- if (out < minOutput) {
1235
- minVert = v1;
1236
- minVertNext = v2;
1237
- minOutput = out;
1238
- }
1239
- });
1240
- if (minVert !== undefined)
1241
- minVert.getOutputEdges().forEach((e) => {
1242
- if (minVert.getNeibour(e) !== minVertNext)
1243
- return;
1244
- e.canBeUsed = false;
1245
- });
1246
- }
1247
- // topological sort
1248
- // if there is a cycle - returns [0, <List of envolved vertices in the cycle>]
1249
- // if there are no cycles but deadends- returns [3, <List of all envolved deadend vertices>]
1250
- // if there are no cycles or deadends- returns [2, <List of all envolved vertices topologically sorted>]
1251
- topologySort(from, to) {
1252
- // undefined or 0 - not processed, 1 - in process, 2 - finished, 3 - dedend
1253
- const vertState = new Map();
1254
- const vertsFinished = [];
1255
- const foundCycle = [];
1256
- const foundDeadEndVerts = [];
1257
- // 0 - cycle was found and created, return
1258
- // 1 - during cycle creating
1259
- // 2 - vertex is processed ok
1260
- // 3 - dead end vertex
1261
- function topSortRecursive(current) {
1262
- const state = vertState.get(current);
1263
- if (state === 2 || state === 3)
1264
- return state;
1265
- if (state === 1) {
1266
- console.assert(foundCycle.length === 0, 'Internal Error 566');
1267
- foundCycle.push(current);
1268
- return 1;
1269
- }
1270
- vertState.set(current, 1);
1271
- let successors2Exist = false;
1272
- const outEdges = current.getOutputEdges();
1273
- for (let i = 0; i < outEdges.length; ++i) {
1274
- const e = outEdges[i];
1275
- const res = topSortRecursive(current.getNeibour(e));
1276
- if (res === 0)
1277
- return 0;
1278
- if (res === 1) {
1279
- if (foundCycle[0] === current)
1280
- return 0;
1281
- else {
1282
- foundCycle.push(current);
1283
- return 1;
1284
- }
1285
- }
1286
- if (res === 2)
1287
- successors2Exist = true; // Ok successors
1288
- }
1289
- if (successors2Exist) {
1290
- console.assert(current !== to, 'Internal Error 589');
1291
- vertsFinished.push(current);
1292
- vertState.set(current, 2);
1293
- return 2;
1294
- }
1295
- else {
1296
- if (current !== to) {
1297
- foundDeadEndVerts.push(current);
1298
- vertState.set(current, 3);
1299
- return 3;
1300
- }
1301
- vertsFinished.push(current);
1302
- vertState.set(current, 2);
1303
- return 2;
1304
- }
1305
- }
1306
- const res = topSortRecursive(from);
1307
- if (res === 0)
1308
- return { status: 0, vertices: foundCycle };
1309
- if (foundDeadEndVerts.length)
1310
- return { status: 3, vertices: foundDeadEndVerts };
1311
- ASSERT(() => {
1312
- if (vertsFinished[0] !== to)
1313
- return false;
1314
- if (vertsFinished[vertsFinished.length - 1] !== from)
1315
- return false;
1316
- return true;
1317
- }, 'Internal Error 614');
1318
- if (res === 2)
1319
- return { status: 2, vertices: vertsFinished.reverse() };
1320
- console.assert(true, 'Internal Error 612');
1321
- return { status: 1, vertices: [] };
1322
- }
1323
- }
1324
- //# sourceMappingURL=Graph.js.map