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,1187 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Graph = exports.Vertice = exports.Edge = exports.RouteStatus = void 0;
4
- exports.NoWayMultiRoute = NoWayMultiRoute;
5
- const RPool_js_1 = require("./RPool.js");
6
- const StableSwapPool_js_1 = require("./StableSwapPool.js");
7
- const Utils_js_1 = require("./Utils.js");
8
- const ROUTER_DISTRIBUTION_PORTION = 65535;
9
- var RouteStatus;
10
- (function (RouteStatus) {
11
- RouteStatus["Success"] = "Success";
12
- RouteStatus["NoWay"] = "NoWay";
13
- RouteStatus["Partial"] = "Partial";
14
- })(RouteStatus || (exports.RouteStatus = RouteStatus = {}));
15
- function NoWayMultiRoute(from, to) {
16
- return {
17
- status: RouteStatus.NoWay,
18
- fromToken: from,
19
- toToken: to,
20
- amountIn: 0,
21
- amountInBI: 0n,
22
- amountOut: 0,
23
- amountOutBI: 0n,
24
- legs: [],
25
- gasSpent: 0,
26
- totalAmountOut: 0,
27
- totalAmountOutBI: 0n,
28
- primaryPrice: undefined,
29
- swapPrice: undefined,
30
- priceImpact: undefined,
31
- };
32
- }
33
- class Edge {
34
- constructor(p, v0, v1) {
35
- this.pool = p;
36
- this.vert0 = v0;
37
- this.vert1 = v1;
38
- this.amountInPrevious = 0;
39
- this.amountOutPrevious = 0;
40
- this.canBeUsed = true;
41
- this.direction = true;
42
- this.spentGas = 0;
43
- this.spentGasNew = 0;
44
- this.bestEdgeIncome = 0;
45
- }
46
- cleanTmpData() {
47
- this.amountInPrevious = 0;
48
- this.amountOutPrevious = 0;
49
- this.canBeUsed = true;
50
- this.direction = true;
51
- this.spentGas = 0;
52
- this.spentGasNew = 0;
53
- this.bestEdgeIncome = 0;
54
- this.pool.cleanTmpData();
55
- }
56
- reserve(v) {
57
- return v === this.vert0 ? this.pool.getReserve0() : this.pool.getReserve1();
58
- }
59
- calcOutput(v, amountIn) {
60
- let res;
61
- let gas;
62
- if (v === this.vert1) {
63
- if (this.direction) {
64
- if (amountIn < this.amountOutPrevious) {
65
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountOutPrevious - amountIn, true);
66
- res = this.amountInPrevious - inp;
67
- gas = gasSpent;
68
- }
69
- else {
70
- const { out, gasSpent } = this.pool.calcOutByIn(amountIn - this.amountOutPrevious, false);
71
- res = out + this.amountInPrevious;
72
- gas = gasSpent;
73
- }
74
- }
75
- else {
76
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountOutPrevious + amountIn, false);
77
- res = out - this.amountInPrevious;
78
- gas = gasSpent;
79
- }
80
- }
81
- else {
82
- if (this.direction) {
83
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountInPrevious + amountIn, true);
84
- res = out - this.amountOutPrevious;
85
- gas = gasSpent;
86
- }
87
- else {
88
- if (amountIn < this.amountInPrevious) {
89
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountInPrevious - amountIn, false);
90
- res = this.amountOutPrevious - inp;
91
- gas = gasSpent;
92
- }
93
- else {
94
- const { out, gasSpent } = this.pool.calcOutByIn(amountIn - this.amountInPrevious, true);
95
- res = out + this.amountOutPrevious;
96
- gas = gasSpent;
97
- }
98
- }
99
- }
100
- return { out: res, gasSpent: gas - this.spentGas };
101
- }
102
- calcInput(v, amountOut) {
103
- let res;
104
- let gas;
105
- if (v === this.vert1) {
106
- if (!this.direction) {
107
- if (amountOut < this.amountOutPrevious) {
108
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountOutPrevious - amountOut, false);
109
- res = this.amountInPrevious - out;
110
- gas = gasSpent;
111
- }
112
- else {
113
- const { inp, gasSpent } = this.pool.calcInByOut(amountOut - this.amountOutPrevious, true);
114
- res = inp + this.amountInPrevious;
115
- gas = gasSpent;
116
- }
117
- }
118
- else {
119
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountOutPrevious + amountOut, true);
120
- res = inp - this.amountInPrevious;
121
- gas = gasSpent;
122
- }
123
- }
124
- else {
125
- if (!this.direction) {
126
- const { inp, gasSpent } = this.pool.calcInByOut(this.amountInPrevious + amountOut, false);
127
- res = inp - this.amountOutPrevious;
128
- gas = gasSpent;
129
- }
130
- else {
131
- if (amountOut < this.amountInPrevious) {
132
- const { out, gasSpent } = this.pool.calcOutByIn(this.amountInPrevious - amountOut, true);
133
- res = this.amountOutPrevious - out;
134
- gas = gasSpent;
135
- }
136
- else {
137
- const { inp, gasSpent } = this.pool.calcInByOut(amountOut - this.amountInPrevious, false);
138
- res = inp + this.amountOutPrevious;
139
- gas = gasSpent;
140
- }
141
- }
142
- }
143
- return { inp: res, gasSpent: gas - this.spentGas };
144
- }
145
- checkMinimalLiquidityExceededAfterSwap(from, amountOut) {
146
- if (from === this.vert0) {
147
- const r1 = parseInt(this.pool.getReserve1().toString());
148
- if (this.direction) {
149
- return r1 - amountOut - this.amountOutPrevious < this.pool.minLiquidity;
150
- }
151
- else {
152
- return r1 - amountOut + this.amountOutPrevious < this.pool.minLiquidity;
153
- }
154
- }
155
- else {
156
- const r0 = parseInt(this.pool.getReserve0().toString());
157
- if (this.direction) {
158
- return r0 - amountOut + this.amountInPrevious < this.pool.minLiquidity;
159
- }
160
- else {
161
- return r0 - amountOut - this.amountInPrevious < this.pool.minLiquidity;
162
- }
163
- }
164
- }
165
- testApply(from, amountIn, amountOut) {
166
- console.assert(this.amountInPrevious * this.amountOutPrevious >= 0);
167
- const inPrev = this.direction
168
- ? this.amountInPrevious
169
- : -this.amountInPrevious;
170
- const outPrev = this.direction
171
- ? this.amountOutPrevious
172
- : -this.amountOutPrevious;
173
- const to = from.getNeibour(this);
174
- let directionNew;
175
- let amountInNew = 0;
176
- let amountOutNew = 0;
177
- if (to) {
178
- const inInc = from === this.vert0 ? amountIn : -amountOut;
179
- const outInc = from === this.vert0 ? amountOut : -amountIn;
180
- const inNew = inPrev + inInc;
181
- const outNew = outPrev + outInc;
182
- console.assert(inNew * outNew >= 0);
183
- if (inNew >= 0) {
184
- directionNew = true;
185
- amountInNew = inNew;
186
- amountOutNew = outNew;
187
- }
188
- else {
189
- directionNew = false;
190
- amountInNew = -inNew;
191
- amountOutNew = -outNew;
192
- }
193
- }
194
- else
195
- console.error('Error 221');
196
- if (directionNew) {
197
- const calc = this.pool.calcOutByIn(amountInNew, true).out;
198
- const res = (0, Utils_js_1.closeValues)(amountOutNew, calc, 1e-6);
199
- if (!res)
200
- console.log('Err 225-1 !!', amountOutNew, calc, Math.abs(calc / amountOutNew - 1));
201
- return res;
202
- }
203
- else {
204
- const calc = this.pool.calcOutByIn(amountOutNew, false).out;
205
- const res = (0, Utils_js_1.closeValues)(amountInNew, calc, 1e-6);
206
- if (!res)
207
- console.log('Err 225-2!!', amountInNew, calc, Math.abs(calc / amountInNew - 1));
208
- return res;
209
- }
210
- }
211
- applySwap(from) {
212
- console.assert(this.amountInPrevious * this.amountOutPrevious >= 0);
213
- const inPrev = this.direction
214
- ? this.amountInPrevious
215
- : -this.amountInPrevious;
216
- const outPrev = this.direction
217
- ? this.amountOutPrevious
218
- : -this.amountOutPrevious;
219
- const to = from.getNeibour(this);
220
- const inInc = from === this.vert0 ? from.bestIncome : -to.bestIncome;
221
- const outInc = from === this.vert0 ? to.bestIncome : -from.bestIncome;
222
- const inNew = inPrev + inInc;
223
- const outNew = outPrev + outInc;
224
- console.assert(inNew * outNew >= 0);
225
- if (inNew >= 0) {
226
- this.direction = true;
227
- this.amountInPrevious = inNew;
228
- this.amountOutPrevious = outNew;
229
- }
230
- else {
231
- this.direction = false;
232
- this.amountInPrevious = -inNew;
233
- this.amountOutPrevious = -outNew;
234
- }
235
- this.pool.setCurrentFlow(inNew, -outNew, this.spentGasNew);
236
- this.spentGas = this.spentGasNew;
237
- (0, Utils_js_1.ASSERT)(() => {
238
- let precision = Math.max(1 / this.amountOutPrevious, 1 / this.amountInPrevious, 1e-9);
239
- if (precision === Infinity)
240
- precision = 1e-9;
241
- if (this.pool instanceof StableSwapPool_js_1.StableSwapRPool) {
242
- let price = this.pool.calcCurrentPriceWithoutFee(true);
243
- if (price < 1)
244
- price = 1 / price;
245
- if (price > 1e8) {
246
- precision = 2e-3;
247
- }
248
- }
249
- if (this.direction) {
250
- const granularity = this.pool.granularity1();
251
- return (0, Utils_js_1.closeValues)(this.amountOutPrevious / granularity, this.pool.calcOutByIn(this.amountInPrevious, this.direction).out /
252
- granularity, precision);
253
- }
254
- else {
255
- const granularity = this.pool.granularity0();
256
- return (0, Utils_js_1.closeValues)(this.amountInPrevious / granularity, this.pool.calcOutByIn(this.amountOutPrevious, this.direction).out /
257
- granularity, precision);
258
- }
259
- }, 'Error 225');
260
- }
261
- }
262
- exports.Edge = Edge;
263
- class Vertice {
264
- constructor(t) {
265
- this.token = t;
266
- (0, RPool_js_1.setTokenId)(this.token);
267
- this.edges = [];
268
- this.price = 0;
269
- this.gasPrice = 0;
270
- this.bestIncome = 0;
271
- this.gasSpent = 0;
272
- this.bestTotal = 0;
273
- this.bestSource = undefined;
274
- this.checkLine = -1;
275
- }
276
- cleanTmpData() {
277
- this.bestIncome = 0;
278
- this.gasSpent = 0;
279
- this.bestTotal = 0;
280
- this.bestSource = undefined;
281
- this.checkLine = -1;
282
- }
283
- getNeibour(e) {
284
- if (!e)
285
- return undefined;
286
- return e.vert0 === this ? e.vert1 : e.vert0;
287
- }
288
- getOutputEdges() {
289
- return this.edges.filter((e) => {
290
- if (!e.canBeUsed)
291
- return false;
292
- if (e.amountInPrevious === 0)
293
- return false;
294
- if (e.direction !== (e.vert0 === this))
295
- return false;
296
- return true;
297
- });
298
- }
299
- getInputEdges() {
300
- return this.edges.filter((e) => {
301
- if (!e.canBeUsed)
302
- return false;
303
- if (e.amountInPrevious === 0)
304
- return false;
305
- if (e.direction === (e.vert0 === this))
306
- return false;
307
- return true;
308
- });
309
- }
310
- }
311
- exports.Vertice = Vertice;
312
- class Graph {
313
- constructor(pools, start, baseTokenOrNetworks, gasPriceSingleNetwork, minPriceLiquidity = 0, trustedForPricingTokens, priceLogging = false) {
314
- const networks = Array.isArray(baseTokenOrNetworks)
315
- ? baseTokenOrNetworks
316
- : [
317
- {
318
- chainId: baseTokenOrNetworks.chainId,
319
- baseToken: baseTokenOrNetworks,
320
- gasPrice: gasPriceSingleNetwork || 0,
321
- },
322
- ];
323
- (0, RPool_js_1.setTokenId)(...networks.map((n) => n.baseToken));
324
- this.vertices = [];
325
- this.edges = [];
326
- this.tokens = new Map();
327
- pools.forEach((p) => {
328
- const v0 = this.getOrCreateVertice(p.token0);
329
- const v1 = this.getOrCreateVertice(p.token1);
330
- const edge = new Edge(p, v0, v1);
331
- v0.edges.push(edge);
332
- v1.edges.push(edge);
333
- this.edges.push(edge);
334
- });
335
- const startToken = trustedForPricingTokens
336
- ? trustedForPricingTokens[0] ?? start
337
- : start;
338
- (0, RPool_js_1.setTokenId)(startToken);
339
- const startV = this.getVert(startToken);
340
- if (startV !== undefined)
341
- this.setPricesStable(startV, 1, networks, minPriceLiquidity, trustedForPricingTokens, priceLogging);
342
- }
343
- getVert(t) {
344
- return this.tokens.get(t.tokenId);
345
- }
346
- cleanTmpData() {
347
- this.edges.forEach((e) => e.cleanTmpData());
348
- this.vertices.forEach((v) => v.cleanTmpData());
349
- }
350
- setPricesStable(from, price, networks, minLiquidity = 0, trustedTokens, logging = false) {
351
- const processedVert = new Set();
352
- let nextEdges = [];
353
- const trustedTokensSet = new Set(trustedTokens?.map((t) => t.address) ?? []);
354
- if (trustedTokens)
355
- trustedTokensSet.add(from.token.address);
356
- function addVertice(v, price) {
357
- v.price = price;
358
- processedVert.add(v);
359
- if (trustedTokens && !trustedTokensSet.has(v.token.address))
360
- return;
361
- const newEdges = v.edges
362
- .filter((e) => !processedVert.has(v.getNeibour(e)))
363
- .map((e) => {
364
- const liquidity = price * Number(e.reserve(v));
365
- return [liquidity, e];
366
- })
367
- .filter(([liquidity, e]) => e.pool.alwaysAppropriateForPricing() ||
368
- (liquidity >= minLiquidity && e.pool.isPoolAppropriateForPricing()));
369
- nextEdges = (0, Utils_js_1.fastArrayMerge)(nextEdges, newEdges);
370
- }
371
- if (logging)
372
- console.log(`Pricing: Initial token ${from.token.symbol} price=${price}`);
373
- addVertice(from, price);
374
- while (nextEdges.length > 0) {
375
- const [liquidity, bestEdge] = nextEdges.pop();
376
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
377
- ? [bestEdge.vert1, bestEdge.vert0]
378
- : [bestEdge.vert0, bestEdge.vert1];
379
- if (processedVert.has(vTo))
380
- continue;
381
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
382
- if (logging)
383
- console.log(`Pricing: + Token ${vTo.token.symbol} price=${vFrom.price * p}` +
384
- ` from ${vFrom.token.symbol} pool=${bestEdge.pool.address} liquidity=${liquidity}`);
385
- addVertice(vTo, vFrom.price * p);
386
- }
387
- const gasPrice = new Map();
388
- networks.forEach((n) => {
389
- const vPrice = this.getVert(n.baseToken)?.price || 0;
390
- gasPrice.set(n.chainId, n.gasPrice * vPrice);
391
- });
392
- processedVert.forEach((v) => {
393
- const gasPriceChainId = gasPrice.get(v.token.chainId);
394
- if (gasPriceChainId === undefined)
395
- console.error(`Error 427: token {${v.token.address} ${v.token.symbol}}` +
396
- ` has unknown chainId ${v.token.chainId} (${typeof v.token
397
- .chainId}).` +
398
- `Known chainIds: ${Array.from(gasPrice.keys()).map((k) => `"${k}"(${typeof k})`)}`);
399
- if (v.price === 0)
400
- console.error(`Error 428: token {${v.token.address} ${v.token.symbol} ${v.token.chainId}} was not priced`);
401
- v.gasPrice = gasPriceChainId / v.price;
402
- });
403
- }
404
- getPriceReasoning(from, price, token, minLiquidity = 0, trustedTokens) {
405
- const processedVert = new Set();
406
- const vertToPricingInfo = new Map();
407
- let nextEdges = [];
408
- const trustedTokensSet = new Set(trustedTokens?.map((t) => t.address) ?? []);
409
- if (trustedTokens)
410
- trustedTokensSet.add(from.address);
411
- function addVertice(parent, v, price, poolPrice, liquidity, edge) {
412
- processedVert.add(v);
413
- vertToPricingInfo.set(v, {
414
- parent: vertToPricingInfo.get(parent),
415
- vert: v,
416
- price,
417
- poolPrice,
418
- liquidity,
419
- edge,
420
- });
421
- if (trustedTokens && !trustedTokensSet.has(v.token.address))
422
- return;
423
- const newEdges = v.edges
424
- .filter((e) => !processedVert.has(v.getNeibour(e)))
425
- .map((e) => {
426
- const liquidity = price * Number(e.reserve(v));
427
- return [liquidity, e];
428
- })
429
- .filter(([liquidity, e]) => e.pool.alwaysAppropriateForPricing() ||
430
- (liquidity >= minLiquidity && e.pool.isPoolAppropriateForPricing()));
431
- nextEdges = (0, Utils_js_1.fastArrayMerge)(nextEdges, newEdges);
432
- }
433
- (0, RPool_js_1.setTokenId)(from);
434
- addVertice(undefined, this.tokens.get(from.tokenId), price / 10 ** from.decimals, 1, 0, undefined);
435
- while (nextEdges.length > 0) {
436
- const [liquidity, bestEdge] = nextEdges.pop();
437
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
438
- ? [bestEdge.vert1, bestEdge.vert0]
439
- : [bestEdge.vert0, bestEdge.vert1];
440
- if (processedVert.has(vTo))
441
- continue;
442
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
443
- const pPrice = vertToPricingInfo.get(vFrom)?.price;
444
- addVertice(vFrom, vTo, pPrice * p, p, liquidity, bestEdge);
445
- if (vTo.token.address === token) {
446
- const lines = [];
447
- let t = vertToPricingInfo.get(vTo);
448
- while (t !== undefined) {
449
- const dec = t.vert.token.decimals;
450
- const parentDecExp = t.parent?.vert.token.decimals ?? dec;
451
- lines.push(`Token ${t.vert.token.symbol} (${t.vert.token.address}) price is ${t.price * 10 ** dec}$`);
452
- if (t.edge !== undefined) {
453
- lines.push(`Pool ${t.edge.pool.address} liquidity ${Math.round(t.liquidity)}$ price ${(t.poolPrice / 10 ** parentDecExp) * 10 ** dec}`);
454
- }
455
- t = t.parent;
456
- }
457
- return lines.reverse();
458
- }
459
- }
460
- return ['Token is not priced'];
461
- }
462
- setPricesStableInsideChain(from, price, gasPrice) {
463
- const processedVert = new Set();
464
- let nextEdges = [];
465
- const edgeValues = new Map();
466
- const value = (e) => edgeValues.get(e);
467
- function addVertice(v, price, gasPrice) {
468
- v.price = price;
469
- v.gasPrice = gasPrice;
470
- const newEdges = v.edges.filter((e) => {
471
- const newV = v.getNeibour(e);
472
- return (newV?.token.chainId === v.token.chainId &&
473
- !processedVert.has(v.getNeibour(e)));
474
- });
475
- newEdges.forEach((e) => edgeValues.set(e, price * parseInt(e.reserve(v).toString())));
476
- newEdges.sort((e1, e2) => value(e1) - value(e2));
477
- const res = [];
478
- while (nextEdges.length && newEdges.length) {
479
- if (value(nextEdges[0]) < value(newEdges[0]))
480
- res.push(nextEdges.shift());
481
- else
482
- res.push(newEdges.shift());
483
- }
484
- nextEdges = [...res, ...nextEdges, ...newEdges];
485
- processedVert.add(v);
486
- }
487
- addVertice(from, price, gasPrice);
488
- while (nextEdges.length > 0) {
489
- const bestEdge = nextEdges.pop();
490
- const [vFrom, vTo] = processedVert.has(bestEdge.vert1)
491
- ? [bestEdge.vert1, bestEdge.vert0]
492
- : [bestEdge.vert0, bestEdge.vert1];
493
- if (processedVert.has(vTo))
494
- continue;
495
- const p = bestEdge.pool.calcCurrentPriceWithoutFee(vFrom === bestEdge.vert1);
496
- addVertice(vTo, vFrom.price * p, vFrom.gasPrice / p);
497
- }
498
- }
499
- setPrices(from, price, gasPrice) {
500
- if (from.price !== 0)
501
- return;
502
- from.price = price;
503
- from.gasPrice = gasPrice;
504
- const edges = from.edges
505
- .map((e) => [e, parseInt(e.reserve(from).toString())])
506
- .sort(([, r1], [, r2]) => r2 - r1);
507
- edges.forEach(([e]) => {
508
- const v = e.vert0 === from ? e.vert1 : e.vert0;
509
- if (v.price !== 0)
510
- return;
511
- const p = e.pool.calcCurrentPriceWithoutFee(from === e.vert1);
512
- this.setPrices(v, price * p, gasPrice / p);
513
- });
514
- }
515
- getOrCreateVertice(token) {
516
- let vert = this.getVert(token);
517
- if (vert)
518
- return vert;
519
- vert = new Vertice(token);
520
- this.vertices.push(vert);
521
- this.tokens.set(token.tokenId, vert);
522
- return vert;
523
- }
524
- findBestPathExactIn(from, to, amountIn) {
525
- const start = this.getVert(from);
526
- const finish = this.getVert(to);
527
- if (!start || !finish || Number.isNaN(finish.price))
528
- return undefined;
529
- this.edges.forEach((e) => {
530
- e.bestEdgeIncome = 0;
531
- e.spentGasNew = 0;
532
- });
533
- this.vertices.forEach((v) => {
534
- v.bestIncome = 0;
535
- v.gasSpent = 0;
536
- v.bestTotal = 0;
537
- v.bestSource = undefined;
538
- v.checkLine = -1;
539
- });
540
- start.bestIncome = amountIn;
541
- start.bestTotal = amountIn;
542
- const processedVert = new Set();
543
- const nextVertList = [start];
544
- let debug_info = '';
545
- let checkLine = 0;
546
- for (;;) {
547
- let closestVert;
548
- let closestTotal;
549
- let closestPosition = 0;
550
- nextVertList.forEach((v, i) => {
551
- if (closestTotal === undefined || v.bestTotal > closestTotal) {
552
- closestTotal = v.bestTotal;
553
- closestVert = v;
554
- closestPosition = i;
555
- }
556
- });
557
- if (!closestVert)
558
- return undefined;
559
- closestVert.checkLine = checkLine++;
560
- if (closestVert === finish) {
561
- const bestPath = [];
562
- for (let v = finish; v?.bestSource; v = v.getNeibour(v.bestSource)) {
563
- bestPath.unshift(v.bestSource);
564
- }
565
- (0, Utils_js_1.DEBUG)(() => console.log(debug_info));
566
- return {
567
- path: bestPath,
568
- output: finish.bestIncome,
569
- gasSpent: finish.gasSpent,
570
- totalOutput: finish.bestTotal,
571
- };
572
- }
573
- nextVertList.splice(closestPosition, 1);
574
- closestVert.edges.forEach((e) => {
575
- const v2 = closestVert === e.vert0 ? e.vert1 : e.vert0;
576
- if (processedVert.has(v2))
577
- return;
578
- if (e.pool.address === closestVert.bestSource?.pool.address)
579
- return;
580
- let newIncome;
581
- let gas;
582
- try {
583
- const { out, gasSpent } = e.calcOutput(closestVert, closestVert.bestIncome);
584
- if (!Number.isFinite(out) || !Number.isFinite(gasSpent))
585
- return;
586
- newIncome = out;
587
- gas = gasSpent;
588
- }
589
- catch (_err) {
590
- e.bestEdgeIncome = -1;
591
- return;
592
- }
593
- const newGasSpent = closestVert.gasSpent + gas;
594
- const price = v2.price / finish.price;
595
- const gasPrice = v2.gasPrice * price;
596
- const newTotal = newIncome * price - newGasSpent * gasPrice;
597
- console.assert(e.bestEdgeIncome === 0, 'Error 373');
598
- e.bestEdgeIncome = newIncome * price;
599
- e.spentGasNew = e.spentGas + gas;
600
- if (!v2.bestSource)
601
- nextVertList.push(v2);
602
- if (!v2.bestSource || newTotal > v2.bestTotal) {
603
- (0, Utils_js_1.DEBUG)(() => {
604
- const st = closestVert?.token === from ? '*' : '';
605
- const fn = v2?.token === to ? '*' : '';
606
- debug_info += `${st}${closestVert?.token.name}->${v2.token.name}${fn} ${v2.bestIncome} -> ${newIncome}\n`;
607
- });
608
- v2.bestIncome = newIncome;
609
- v2.gasSpent = newGasSpent;
610
- v2.bestTotal = newTotal;
611
- v2.bestSource = e;
612
- }
613
- });
614
- processedVert.add(closestVert);
615
- }
616
- }
617
- findBestPathExactOut(from, to, amountOut) {
618
- const start = this.getVert(to);
619
- const finish = this.getVert(from);
620
- if (!start || !finish)
621
- return undefined;
622
- this.edges.forEach((e) => {
623
- e.bestEdgeIncome = 0;
624
- e.spentGasNew = 0;
625
- });
626
- this.vertices.forEach((v) => {
627
- v.bestIncome = 0;
628
- v.gasSpent = 0;
629
- v.bestTotal = 0;
630
- v.bestSource = undefined;
631
- v.checkLine = -1;
632
- });
633
- start.bestIncome = amountOut;
634
- start.bestTotal = amountOut;
635
- const processedVert = new Set();
636
- const nextVertList = [start];
637
- let debug_info = '';
638
- let checkLine = 0;
639
- for (;;) {
640
- let closestVert;
641
- let closestTotal;
642
- let closestPosition = 0;
643
- nextVertList.forEach((v, i) => {
644
- if (closestTotal === undefined || v.bestTotal < closestTotal) {
645
- closestTotal = v.bestTotal;
646
- closestVert = v;
647
- closestPosition = i;
648
- }
649
- });
650
- if (!closestVert)
651
- return undefined;
652
- closestVert.checkLine = checkLine++;
653
- if (closestVert === finish) {
654
- const bestPath = [];
655
- for (let v = finish; v?.bestSource; v = v.getNeibour(v.bestSource)) {
656
- bestPath.push(v.bestSource);
657
- }
658
- (0, Utils_js_1.DEBUG)(() => console.log(debug_info));
659
- return {
660
- path: bestPath,
661
- input: finish.bestIncome,
662
- gasSpent: finish.gasSpent,
663
- totalInput: finish.bestTotal,
664
- };
665
- }
666
- nextVertList.splice(closestPosition, 1);
667
- closestVert.edges.forEach((e) => {
668
- const v2 = closestVert === e.vert0 ? e.vert1 : e.vert0;
669
- if (processedVert.has(v2))
670
- return;
671
- let newIncome;
672
- let gas;
673
- try {
674
- const { inp, gasSpent } = e.calcInput(closestVert, closestVert.bestIncome);
675
- if (!Number.isFinite(inp) || !Number.isFinite(gasSpent))
676
- return;
677
- if (inp < 0)
678
- return;
679
- newIncome = inp;
680
- gas = gasSpent;
681
- }
682
- catch (_e) {
683
- return;
684
- }
685
- const newGasSpent = closestVert.gasSpent + gas;
686
- const price = v2.price / finish.price;
687
- const gasPrice = v2.gasPrice * price;
688
- const newTotal = newIncome * price + newGasSpent * gasPrice;
689
- console.assert(e.bestEdgeIncome === 0, 'Error 373');
690
- e.bestEdgeIncome = newIncome * price;
691
- e.spentGasNew = e.spentGas + gas;
692
- if (!v2.bestSource)
693
- nextVertList.push(v2);
694
- if (!v2.bestSource || newTotal < v2.bestTotal) {
695
- (0, Utils_js_1.DEBUG)(() => {
696
- const st = v2?.token === from ? '*' : '';
697
- const fn = closestVert?.token === to ? '*' : '';
698
- debug_info += `${st}${closestVert?.token.name}<-${v2.token.name}${fn} ${v2.bestIncome} -> ${newIncome}\n`;
699
- });
700
- v2.bestIncome = newIncome;
701
- v2.gasSpent = newGasSpent;
702
- v2.bestTotal = newTotal;
703
- v2.bestSource = e;
704
- }
705
- });
706
- processedVert.add(closestVert);
707
- }
708
- }
709
- addPath(from, to, path) {
710
- let _from = from;
711
- path.forEach((e) => {
712
- if (_from) {
713
- e.applySwap(_from);
714
- _from = _from.getNeibour(e);
715
- }
716
- else {
717
- console.error('Unexpected 315');
718
- }
719
- });
720
- (0, Utils_js_1.ASSERT)(() => {
721
- const res = this.vertices.every((v) => {
722
- let total = 0;
723
- let totalModule = 0;
724
- v.edges.forEach((e) => {
725
- if (e.vert0 === v) {
726
- if (e.direction) {
727
- total -= e.amountInPrevious;
728
- }
729
- else {
730
- total += e.amountInPrevious;
731
- }
732
- totalModule += e.amountInPrevious;
733
- }
734
- else {
735
- if (e.direction) {
736
- total += e.amountOutPrevious;
737
- }
738
- else {
739
- total -= e.amountOutPrevious;
740
- }
741
- totalModule += e.amountOutPrevious;
742
- }
743
- });
744
- if (v === from)
745
- return total <= 0;
746
- if (v === to)
747
- return total >= 0;
748
- if (totalModule === 0)
749
- return total === 0;
750
- return Math.abs(total / totalModule) < 1e10;
751
- });
752
- return res;
753
- }, 'Error 290');
754
- }
755
- getPrimaryPriceForPath(from, path) {
756
- let p = 1;
757
- let prevToken = from;
758
- path.forEach((edge) => {
759
- const direction = edge.vert0 === prevToken;
760
- const edgePrice = edge.pool.calcCurrentPriceWithoutFee(direction);
761
- p *= edgePrice;
762
- prevToken = prevToken.getNeibour(edge);
763
- });
764
- return p;
765
- }
766
- findBestRouteExactIn(from, to, amountIn, mode) {
767
- let amountInBI;
768
- if (typeof amountIn === 'bigint') {
769
- amountInBI = amountIn;
770
- amountIn = Number(amountIn);
771
- }
772
- else {
773
- amountInBI = (0, Utils_js_1.getBigInt)(amountIn);
774
- }
775
- let routeValues = [];
776
- if (Array.isArray(mode)) {
777
- const sum = mode.reduce((a, b) => a + b, 0);
778
- routeValues = mode.map((e) => e / sum);
779
- }
780
- else {
781
- for (let i = 0; i < mode; ++i)
782
- routeValues.push(1 / mode);
783
- }
784
- this.edges.forEach((e) => {
785
- e.amountInPrevious = 0;
786
- e.amountOutPrevious = 0;
787
- e.direction = true;
788
- e.pool.cleanTmpData();
789
- });
790
- let output = 0;
791
- let gasSpentInit = 0;
792
- let totalOutput = 0;
793
- let totalrouted = 0;
794
- let primaryPrice;
795
- let step;
796
- for (step = 0; step < routeValues.length; ++step) {
797
- const routeValue = routeValues[step];
798
- const p = this.findBestPathExactIn(from, to, amountIn * routeValue);
799
- if (!p) {
800
- break;
801
- }
802
- else {
803
- output += p.output;
804
- gasSpentInit += p.gasSpent;
805
- totalOutput += p.totalOutput;
806
- this.addPath(this.getVert(from), this.getVert(to), p.path);
807
- totalrouted += routeValue;
808
- }
809
- }
810
- if (step === 0 || output === 0)
811
- return NoWayMultiRoute(from, to);
812
- let status;
813
- if (step < routeValues.length)
814
- status = RouteStatus.Partial;
815
- else
816
- status = RouteStatus.Success;
817
- this.removeEdgesWithLowFlow(0.001);
818
- const fromVert = this.getVert(from);
819
- const toVert = this.getVert(to);
820
- const { legs, gasSpent } = this.getRouteLegs(fromVert, toVert);
821
- console.assert(gasSpent <= gasSpentInit, 'Internal Error 491');
822
- this.edges.forEach((e) => e.pool.cleanTmpData());
823
- output = this.updateLegsAmountOut(legs, amountIn * totalrouted);
824
- totalOutput = output - toVert.gasPrice * gasSpent;
825
- if (output === 0) {
826
- status = RouteStatus.NoWay;
827
- totalOutput = 0;
828
- }
829
- let swapPrice;
830
- let priceImpact;
831
- try {
832
- swapPrice = output / amountIn / totalrouted;
833
- const priceTo = this.getVert(to)?.price;
834
- const priceFrom = this.getVert(from)?.price;
835
- primaryPrice = priceTo && priceFrom ? priceFrom / priceTo : undefined;
836
- priceImpact =
837
- primaryPrice !== undefined ? 1 - swapPrice / primaryPrice : undefined;
838
- }
839
- catch (_e) {
840
- }
841
- return {
842
- status,
843
- fromToken: from,
844
- toToken: to,
845
- primaryPrice,
846
- swapPrice,
847
- priceImpact,
848
- amountIn: amountIn * totalrouted,
849
- amountInBI: status === RouteStatus.Success
850
- ? amountInBI
851
- : (0, Utils_js_1.getBigInt)(amountIn * totalrouted),
852
- amountOut: output,
853
- amountOutBI: (0, Utils_js_1.getBigInt)(output),
854
- legs,
855
- gasSpent,
856
- totalAmountOut: totalOutput,
857
- totalAmountOutBI: (0, Utils_js_1.getBigInt)(totalOutput),
858
- };
859
- }
860
- findBestRouteExactOut(from, to, amountOut, mode) {
861
- let routeValues = [];
862
- if (Array.isArray(mode)) {
863
- const sum = mode.reduce((a, b) => a + b, 0);
864
- routeValues = mode.map((e) => e / sum);
865
- }
866
- else {
867
- for (let i = 0; i < mode; ++i)
868
- routeValues.push(1 / mode);
869
- }
870
- this.edges.forEach((e) => {
871
- e.amountInPrevious = 0;
872
- e.amountOutPrevious = 0;
873
- e.direction = true;
874
- });
875
- let input = 0;
876
- let gasSpentInit = 0;
877
- let totalrouted = 0;
878
- let primaryPrice;
879
- let step;
880
- for (step = 0; step < routeValues.length; ++step) {
881
- const routeValue = routeValues[step];
882
- const p = this.findBestPathExactOut(from, to, amountOut * routeValue);
883
- if (!p) {
884
- break;
885
- }
886
- else {
887
- input += p.input;
888
- gasSpentInit += p.gasSpent;
889
- this.addPath(this.getVert(from), this.getVert(to), p.path);
890
- totalrouted += routeValue;
891
- }
892
- }
893
- if (step === 0)
894
- return NoWayMultiRoute(from, to);
895
- let status;
896
- if (step < routeValues.length)
897
- status = RouteStatus.Partial;
898
- else
899
- status = RouteStatus.Success;
900
- const removedEdgesNumber = this.removeEdgesWithLowFlow(0.001);
901
- const fromVert = this.getVert(from);
902
- const toVert = this.getVert(to);
903
- const { legs, gasSpent, topologyWasChanged } = this.getRouteLegs(fromVert, toVert);
904
- console.assert(gasSpent <= gasSpentInit, 'Internal Error 491');
905
- if (topologyWasChanged || removedEdgesNumber > 0) {
906
- input = this.calcLegsAmountIn(legs, amountOut);
907
- }
908
- let swapPrice;
909
- let priceImpact;
910
- try {
911
- swapPrice = amountOut / input;
912
- const priceTo = this.getVert(to)?.price;
913
- const priceFrom = this.getVert(from)?.price;
914
- primaryPrice = priceTo && priceFrom ? priceFrom / priceTo : undefined;
915
- priceImpact =
916
- primaryPrice !== undefined ? 1 - swapPrice / primaryPrice : undefined;
917
- }
918
- catch (_e) {
919
- }
920
- return {
921
- status,
922
- fromToken: from,
923
- toToken: to,
924
- primaryPrice,
925
- swapPrice,
926
- priceImpact,
927
- amountIn: input,
928
- amountInBI: (0, Utils_js_1.getBigInt)(input),
929
- amountOut: amountOut * totalrouted,
930
- amountOutBI: (0, Utils_js_1.getBigInt)(amountOut * totalrouted),
931
- legs,
932
- gasSpent,
933
- totalAmountOut: amountOut - gasSpent * toVert.gasPrice,
934
- totalAmountOutBI: (0, Utils_js_1.getBigInt)(amountOut - gasSpent * toVert.gasPrice),
935
- };
936
- }
937
- getRouteLegs(from, to) {
938
- const { vertices, topologyWasChanged } = this.cleanTopology(from, to);
939
- const legs = [];
940
- let gasSpent = 0;
941
- vertices.forEach((n) => {
942
- const outEdges = n.getOutputEdges().map((e) => {
943
- const from = this.edgeFrom(e);
944
- return from ? [e, from.vert, from.amount] : [e];
945
- });
946
- let outAmount = outEdges.reduce((a, b) => a + b[2], 0);
947
- if (outAmount <= 0)
948
- return;
949
- const total = outAmount;
950
- outEdges.forEach((e, i) => {
951
- const p = e[2];
952
- const quantity = i + 1 === outEdges.length ? 1 : p / outAmount;
953
- const edge = e[0];
954
- legs.push({
955
- poolAddress: edge.pool.address,
956
- uniqueId: edge.pool.uniqueID(),
957
- poolType: edge.pool.poolType(),
958
- poolFee: edge.pool.fee,
959
- tokenFrom: n.token,
960
- tokenTo: n.getNeibour(edge).token,
961
- assumedAmountIn: edge.direction
962
- ? edge.amountInPrevious
963
- : edge.amountOutPrevious,
964
- assumedAmountOut: edge.direction
965
- ? edge.amountOutPrevious
966
- : edge.amountInPrevious,
967
- swapPortion: quantity,
968
- absolutePortion: p / total,
969
- });
970
- gasSpent += edge.pool.calcOutByIn(edge.direction ? edge.amountInPrevious : edge.amountOutPrevious, edge.direction).gasSpent;
971
- outAmount -= p;
972
- });
973
- console.assert(outAmount / total < 1e-12, 'Error 281');
974
- });
975
- return { legs, gasSpent, topologyWasChanged };
976
- }
977
- edgeFrom(e) {
978
- if (e.amountInPrevious === 0)
979
- return undefined;
980
- return e.direction
981
- ? { vert: e.vert0, amount: e.amountInPrevious }
982
- : { vert: e.vert1, amount: e.amountOutPrevious };
983
- }
984
- removeEdgesWithLowFlow(minFraction) {
985
- const weakEdgeList = [];
986
- this.vertices.forEach((v) => {
987
- const outEdges = v.getOutputEdges();
988
- if (outEdges.length <= 1)
989
- return;
990
- const amounts = outEdges.map((e) => {
991
- const data = this.edgeFrom(e);
992
- if (data !== undefined)
993
- return data.amount;
994
- console.error('Tines: Internal Error 1123');
995
- return undefined;
996
- });
997
- const totalOut = amounts.reduce((a, b) => {
998
- a += b;
999
- return a;
1000
- }, 0);
1001
- outEdges.forEach((e, i) => {
1002
- if (amounts[i] / totalOut < minFraction)
1003
- weakEdgeList.push(e);
1004
- });
1005
- });
1006
- weakEdgeList.forEach((e) => {
1007
- e.canBeUsed = false;
1008
- });
1009
- return weakEdgeList.length;
1010
- }
1011
- updateLegsAmountOut(legs, amountIn) {
1012
- if (legs.length === 0)
1013
- return 0;
1014
- const amounts = new Map();
1015
- amounts.set(legs[0].tokenFrom.tokenId, amountIn);
1016
- legs.forEach((l) => {
1017
- const vert = this.getVert(l.tokenFrom);
1018
- console.assert(vert !== undefined, 'Internal Error 570');
1019
- const edge = vert.edges.find((e) => e.pool.uniqueID() === l.uniqueId);
1020
- console.assert(edge !== undefined, 'Internel Error 569');
1021
- const pool = edge.pool;
1022
- const direction = vert === edge.vert0;
1023
- const inputTotal = amounts.get(l.tokenFrom.tokenId);
1024
- console.assert(inputTotal !== undefined, 'Internal Error 564');
1025
- const routerPortion = Math.round(l.swapPortion * ROUTER_DISTRIBUTION_PORTION) /
1026
- ROUTER_DISTRIBUTION_PORTION;
1027
- const input = Math.floor(inputTotal * routerPortion);
1028
- amounts.set(l.tokenFrom.tokenId, inputTotal - input);
1029
- const output = pool.calcOutByInReal(input, direction);
1030
- const vertNext = vert.getNeibour(edge);
1031
- const prevAmount = amounts.get(vertNext.token.tokenId);
1032
- amounts.set(vertNext.token.tokenId, (prevAmount || 0) + output);
1033
- l.assumedAmountIn = input;
1034
- l.assumedAmountOut = output;
1035
- });
1036
- return (amounts.get(legs[legs.length - 1].tokenTo.tokenId) || 0);
1037
- }
1038
- calcLegsAmountIn(legs, amountOut) {
1039
- const totalOutputAssumed = new Map();
1040
- legs.forEach((l) => {
1041
- const prevValue = totalOutputAssumed.get(l.tokenFrom.tokenId) || 0;
1042
- totalOutputAssumed.set(l.tokenFrom.tokenId, prevValue + l.assumedAmountOut);
1043
- });
1044
- const amounts = new Map();
1045
- amounts.set(legs[legs.length - 1].tokenTo.tokenId, amountOut);
1046
- for (let i = legs.length - 1; i >= 0; --i) {
1047
- const l = legs[i];
1048
- const vert = this.getVert(l.tokenTo);
1049
- console.assert(vert !== undefined, 'Internal Error 884');
1050
- const edge = vert.edges.find((e) => e.pool.uniqueID() === l.uniqueId);
1051
- console.assert(edge !== undefined, 'Internel Error 888');
1052
- const pool = edge.pool;
1053
- const direction = vert === edge.vert1;
1054
- const outputTotal = amounts.get(l.tokenTo.tokenId);
1055
- console.assert(outputTotal !== undefined, 'Internal Error 893');
1056
- const totalAssumed = totalOutputAssumed.get(l.tokenFrom.tokenId);
1057
- console.assert(totalAssumed !== undefined, 'Internal Error 903');
1058
- const output = (outputTotal * l.assumedAmountOut) /
1059
- totalAssumed;
1060
- const input = pool.calcInByOut(output, direction).inp;
1061
- const vertNext = vert.getNeibour(edge);
1062
- const prevAmount = amounts.get(vertNext.token.tokenId);
1063
- amounts.set(vertNext.token.tokenId, (prevAmount || 0) + input);
1064
- }
1065
- return amounts.get(legs[0].tokenFrom.tokenId) || 0;
1066
- }
1067
- cleanTopology(from, to) {
1068
- let topologyWasChanged = false;
1069
- let result = this.topologySort(from, to);
1070
- if (result.status !== 2) {
1071
- topologyWasChanged = true;
1072
- while (result.status === 0) {
1073
- this.removeWeakestEdge(result.vertices);
1074
- result = this.topologySort(from, to);
1075
- }
1076
- if (result.status === 3) {
1077
- this.removeDeadEnds(result.vertices);
1078
- result = this.topologySort(from, to);
1079
- }
1080
- console.assert(result.status === 2, 'Internal Error 563');
1081
- if (result.status !== 2)
1082
- return { vertices: [], topologyWasChanged };
1083
- }
1084
- return { vertices: result.vertices, topologyWasChanged };
1085
- }
1086
- removeDeadEnds(verts) {
1087
- verts.forEach((v) => {
1088
- v.getInputEdges().forEach((e) => {
1089
- e.canBeUsed = false;
1090
- });
1091
- });
1092
- }
1093
- removeWeakestEdge(verts) {
1094
- let minVert = undefined;
1095
- let minVertNext;
1096
- let minOutput = Number.MAX_VALUE;
1097
- verts.forEach((v1, i) => {
1098
- const v2 = i === 0 ? verts[verts.length - 1] : verts[i - 1];
1099
- let out = 0;
1100
- v1.getOutputEdges().forEach((e) => {
1101
- if (v1.getNeibour(e) !== v2)
1102
- return;
1103
- out += e.direction ? e.amountOutPrevious : e.amountInPrevious;
1104
- });
1105
- if (out < minOutput) {
1106
- minVert = v1;
1107
- minVertNext = v2;
1108
- minOutput = out;
1109
- }
1110
- });
1111
- if (minVert !== undefined)
1112
- minVert.getOutputEdges().forEach((e) => {
1113
- if (minVert.getNeibour(e) !== minVertNext)
1114
- return;
1115
- e.canBeUsed = false;
1116
- });
1117
- }
1118
- topologySort(from, to) {
1119
- const vertState = new Map();
1120
- const vertsFinished = [];
1121
- const foundCycle = [];
1122
- const foundDeadEndVerts = [];
1123
- function topSortRecursive(current) {
1124
- const state = vertState.get(current);
1125
- if (state === 2 || state === 3)
1126
- return state;
1127
- if (state === 1) {
1128
- console.assert(foundCycle.length === 0, 'Internal Error 566');
1129
- foundCycle.push(current);
1130
- return 1;
1131
- }
1132
- vertState.set(current, 1);
1133
- let successors2Exist = false;
1134
- const outEdges = current.getOutputEdges();
1135
- for (let i = 0; i < outEdges.length; ++i) {
1136
- const e = outEdges[i];
1137
- const res = topSortRecursive(current.getNeibour(e));
1138
- if (res === 0)
1139
- return 0;
1140
- if (res === 1) {
1141
- if (foundCycle[0] === current)
1142
- return 0;
1143
- else {
1144
- foundCycle.push(current);
1145
- return 1;
1146
- }
1147
- }
1148
- if (res === 2)
1149
- successors2Exist = true;
1150
- }
1151
- if (successors2Exist) {
1152
- console.assert(current !== to, 'Internal Error 589');
1153
- vertsFinished.push(current);
1154
- vertState.set(current, 2);
1155
- return 2;
1156
- }
1157
- else {
1158
- if (current !== to) {
1159
- foundDeadEndVerts.push(current);
1160
- vertState.set(current, 3);
1161
- return 3;
1162
- }
1163
- vertsFinished.push(current);
1164
- vertState.set(current, 2);
1165
- return 2;
1166
- }
1167
- }
1168
- const res = topSortRecursive(from);
1169
- if (res === 0)
1170
- return { status: 0, vertices: foundCycle };
1171
- if (foundDeadEndVerts.length)
1172
- return { status: 3, vertices: foundDeadEndVerts };
1173
- (0, Utils_js_1.ASSERT)(() => {
1174
- if (vertsFinished[0] !== to)
1175
- return false;
1176
- if (vertsFinished[vertsFinished.length - 1] !== from)
1177
- return false;
1178
- return true;
1179
- }, 'Internal Error 614');
1180
- if (res === 2)
1181
- return { status: 2, vertices: vertsFinished.reverse() };
1182
- console.assert(true, 'Internal Error 612');
1183
- return { status: 1, vertices: [] };
1184
- }
1185
- }
1186
- exports.Graph = Graph;
1187
- //# sourceMappingURL=Graph.js.map