@centurion_project/v3-sdk 3.30.0-ctn.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/cjs/src/constants.d.ts +23 -0
  4. package/dist/cjs/src/constants.js +44 -0
  5. package/dist/cjs/src/constants.js.map +1 -0
  6. package/dist/cjs/src/entities/index.d.ts +7 -0
  7. package/dist/cjs/src/entities/index.js +11 -0
  8. package/dist/cjs/src/entities/index.js.map +1 -0
  9. package/dist/cjs/src/entities/pool.d.ts +81 -0
  10. package/dist/cjs/src/entities/pool.js +142 -0
  11. package/dist/cjs/src/entities/pool.js.map +1 -0
  12. package/dist/cjs/src/entities/position.d.ts +131 -0
  13. package/dist/cjs/src/entities/position.js +248 -0
  14. package/dist/cjs/src/entities/position.js.map +1 -0
  15. package/dist/cjs/src/entities/route.d.ts +26 -0
  16. package/dist/cjs/src/entities/route.js +75 -0
  17. package/dist/cjs/src/entities/route.js.map +1 -0
  18. package/dist/cjs/src/entities/tick.d.ts +13 -0
  19. package/dist/cjs/src/entities/tick.js +17 -0
  20. package/dist/cjs/src/entities/tick.js.map +1 -0
  21. package/dist/cjs/src/entities/tickDataProvider.d.ts +31 -0
  22. package/dist/cjs/src/entities/tickDataProvider.js +18 -0
  23. package/dist/cjs/src/entities/tickDataProvider.js.map +1 -0
  24. package/dist/cjs/src/entities/tickListDataProvider.d.ts +15 -0
  25. package/dist/cjs/src/entities/tickListDataProvider.js +23 -0
  26. package/dist/cjs/src/entities/tickListDataProvider.js.map +1 -0
  27. package/dist/cjs/src/entities/trade.d.ts +220 -0
  28. package/dist/cjs/src/entities/trade.js +440 -0
  29. package/dist/cjs/src/entities/trade.js.map +1 -0
  30. package/dist/cjs/src/index.d.ts +10 -0
  31. package/dist/cjs/src/index.js +14 -0
  32. package/dist/cjs/src/index.js.map +1 -0
  33. package/dist/cjs/src/internalConstants.d.ts +6 -0
  34. package/dist/cjs/src/internalConstants.js +13 -0
  35. package/dist/cjs/src/internalConstants.js.map +1 -0
  36. package/dist/cjs/src/multicall.d.ts +10 -0
  37. package/dist/cjs/src/multicall.js +24 -0
  38. package/dist/cjs/src/multicall.js.map +1 -0
  39. package/dist/cjs/src/nonfungiblePositionManager.d.ts +159 -0
  40. package/dist/cjs/src/nonfungiblePositionManager.js +250 -0
  41. package/dist/cjs/src/nonfungiblePositionManager.js.map +1 -0
  42. package/dist/cjs/src/payments.d.ts +24 -0
  43. package/dist/cjs/src/payments.js +56 -0
  44. package/dist/cjs/src/payments.js.map +1 -0
  45. package/dist/cjs/src/quoter.d.ts +37 -0
  46. package/dist/cjs/src/quoter.js +69 -0
  47. package/dist/cjs/src/quoter.js.map +1 -0
  48. package/dist/cjs/src/selfPermit.d.ts +25 -0
  49. package/dist/cjs/src/selfPermit.js +38 -0
  50. package/dist/cjs/src/selfPermit.js.map +1 -0
  51. package/dist/cjs/src/staker.d.ts +101 -0
  52. package/dist/cjs/src/staker.js +129 -0
  53. package/dist/cjs/src/staker.js.map +1 -0
  54. package/dist/cjs/src/swapRouter.d.ts +51 -0
  55. package/dist/cjs/src/swapRouter.js +145 -0
  56. package/dist/cjs/src/swapRouter.js.map +1 -0
  57. package/dist/cjs/src/utils/calldata.d.ts +20 -0
  58. package/dist/cjs/src/utils/calldata.js +20 -0
  59. package/dist/cjs/src/utils/calldata.js.map +1 -0
  60. package/dist/cjs/src/utils/computePoolAddress.d.ts +20 -0
  61. package/dist/cjs/src/utils/computePoolAddress.js +33 -0
  62. package/dist/cjs/src/utils/computePoolAddress.js.map +1 -0
  63. package/dist/cjs/src/utils/encodeRouteToPath.d.ts +8 -0
  64. package/dist/cjs/src/utils/encodeRouteToPath.js +32 -0
  65. package/dist/cjs/src/utils/encodeRouteToPath.js.map +1 -0
  66. package/dist/cjs/src/utils/encodeSqrtRatioX96.d.ts +9 -0
  67. package/dist/cjs/src/utils/encodeSqrtRatioX96.js +20 -0
  68. package/dist/cjs/src/utils/encodeSqrtRatioX96.js.map +1 -0
  69. package/dist/cjs/src/utils/fullMath.d.ts +8 -0
  70. package/dist/cjs/src/utils/fullMath.js +21 -0
  71. package/dist/cjs/src/utils/fullMath.js.map +1 -0
  72. package/dist/cjs/src/utils/index.d.ts +18 -0
  73. package/dist/cjs/src/utils/index.js +22 -0
  74. package/dist/cjs/src/utils/index.js.map +1 -0
  75. package/dist/cjs/src/utils/isSorted.d.ts +7 -0
  76. package/dist/cjs/src/utils/isSorted.js +19 -0
  77. package/dist/cjs/src/utils/isSorted.js.map +1 -0
  78. package/dist/cjs/src/utils/liquidityMath.d.ts +8 -0
  79. package/dist/cjs/src/utils/liquidityMath.js +22 -0
  80. package/dist/cjs/src/utils/liquidityMath.js.map +1 -0
  81. package/dist/cjs/src/utils/maxLiquidityForAmounts.d.ts +14 -0
  82. package/dist/cjs/src/utils/maxLiquidityForAmounts.js +87 -0
  83. package/dist/cjs/src/utils/maxLiquidityForAmounts.js.map +1 -0
  84. package/dist/cjs/src/utils/mostSignificantBit.d.ts +2 -0
  85. package/dist/cjs/src/utils/mostSignificantBit.js +27 -0
  86. package/dist/cjs/src/utils/mostSignificantBit.js.map +1 -0
  87. package/dist/cjs/src/utils/nearestUsableTick.d.ts +6 -0
  88. package/dist/cjs/src/utils/nearestUsableTick.js +25 -0
  89. package/dist/cjs/src/utils/nearestUsableTick.js.map +1 -0
  90. package/dist/cjs/src/utils/position.d.ts +8 -0
  91. package/dist/cjs/src/utils/position.js +21 -0
  92. package/dist/cjs/src/utils/position.js.map +1 -0
  93. package/dist/cjs/src/utils/priceTickConversions.d.ts +15 -0
  94. package/dist/cjs/src/utils/priceTickConversions.js +50 -0
  95. package/dist/cjs/src/utils/priceTickConversions.js.map +1 -0
  96. package/dist/cjs/src/utils/sqrtPriceMath.d.ts +13 -0
  97. package/dist/cjs/src/utils/sqrtPriceMath.js +95 -0
  98. package/dist/cjs/src/utils/sqrtPriceMath.js.map +1 -0
  99. package/dist/cjs/src/utils/swapMath.d.ts +9 -0
  100. package/dist/cjs/src/utils/swapMath.js +78 -0
  101. package/dist/cjs/src/utils/swapMath.js.map +1 -0
  102. package/dist/cjs/src/utils/tickLibrary.d.ts +14 -0
  103. package/dist/cjs/src/utils/tickLibrary.js +51 -0
  104. package/dist/cjs/src/utils/tickLibrary.js.map +1 -0
  105. package/dist/cjs/src/utils/tickList.d.ts +23 -0
  106. package/dist/cjs/src/utils/tickList.js +108 -0
  107. package/dist/cjs/src/utils/tickList.js.map +1 -0
  108. package/dist/cjs/src/utils/tickMath.d.ts +34 -0
  109. package/dist/cjs/src/utils/tickMath.js +125 -0
  110. package/dist/cjs/src/utils/tickMath.js.map +1 -0
  111. package/dist/cjs/src/utils/v3swap.d.ts +8 -0
  112. package/dist/cjs/src/utils/v3swap.js +85 -0
  113. package/dist/cjs/src/utils/v3swap.js.map +1 -0
  114. package/dist/esm/src/constants.d.ts +23 -0
  115. package/dist/esm/src/constants.js +40 -0
  116. package/dist/esm/src/constants.js.map +1 -0
  117. package/dist/esm/src/entities/index.d.ts +7 -0
  118. package/dist/esm/src/entities/index.js +8 -0
  119. package/dist/esm/src/entities/index.js.map +1 -0
  120. package/dist/esm/src/entities/pool.d.ts +81 -0
  121. package/dist/esm/src/entities/pool.js +137 -0
  122. package/dist/esm/src/entities/pool.js.map +1 -0
  123. package/dist/esm/src/entities/position.d.ts +131 -0
  124. package/dist/esm/src/entities/position.js +243 -0
  125. package/dist/esm/src/entities/position.js.map +1 -0
  126. package/dist/esm/src/entities/route.d.ts +26 -0
  127. package/dist/esm/src/entities/route.js +70 -0
  128. package/dist/esm/src/entities/route.js.map +1 -0
  129. package/dist/esm/src/entities/tick.d.ts +13 -0
  130. package/dist/esm/src/entities/tick.js +12 -0
  131. package/dist/esm/src/entities/tick.js.map +1 -0
  132. package/dist/esm/src/entities/tickDataProvider.d.ts +31 -0
  133. package/dist/esm/src/entities/tickDataProvider.js +14 -0
  134. package/dist/esm/src/entities/tickDataProvider.js.map +1 -0
  135. package/dist/esm/src/entities/tickListDataProvider.d.ts +15 -0
  136. package/dist/esm/src/entities/tickListDataProvider.js +19 -0
  137. package/dist/esm/src/entities/tickListDataProvider.js.map +1 -0
  138. package/dist/esm/src/entities/trade.d.ts +220 -0
  139. package/dist/esm/src/entities/trade.js +434 -0
  140. package/dist/esm/src/entities/trade.js.map +1 -0
  141. package/dist/esm/src/index.d.ts +10 -0
  142. package/dist/esm/src/index.js +11 -0
  143. package/dist/esm/src/index.js.map +1 -0
  144. package/dist/esm/src/internalConstants.d.ts +6 -0
  145. package/dist/esm/src/internalConstants.js +9 -0
  146. package/dist/esm/src/internalConstants.js.map +1 -0
  147. package/dist/esm/src/multicall.d.ts +10 -0
  148. package/dist/esm/src/multicall.js +19 -0
  149. package/dist/esm/src/multicall.js.map +1 -0
  150. package/dist/esm/src/nonfungiblePositionManager.d.ts +159 -0
  151. package/dist/esm/src/nonfungiblePositionManager.js +245 -0
  152. package/dist/esm/src/nonfungiblePositionManager.js.map +1 -0
  153. package/dist/esm/src/payments.d.ts +24 -0
  154. package/dist/esm/src/payments.js +51 -0
  155. package/dist/esm/src/payments.js.map +1 -0
  156. package/dist/esm/src/quoter.d.ts +37 -0
  157. package/dist/esm/src/quoter.js +64 -0
  158. package/dist/esm/src/quoter.js.map +1 -0
  159. package/dist/esm/src/selfPermit.d.ts +25 -0
  160. package/dist/esm/src/selfPermit.js +33 -0
  161. package/dist/esm/src/selfPermit.js.map +1 -0
  162. package/dist/esm/src/staker.d.ts +101 -0
  163. package/dist/esm/src/staker.js +124 -0
  164. package/dist/esm/src/staker.js.map +1 -0
  165. package/dist/esm/src/swapRouter.d.ts +51 -0
  166. package/dist/esm/src/swapRouter.js +140 -0
  167. package/dist/esm/src/swapRouter.js.map +1 -0
  168. package/dist/esm/src/utils/calldata.d.ts +20 -0
  169. package/dist/esm/src/utils/calldata.js +15 -0
  170. package/dist/esm/src/utils/calldata.js.map +1 -0
  171. package/dist/esm/src/utils/computePoolAddress.d.ts +20 -0
  172. package/dist/esm/src/utils/computePoolAddress.js +29 -0
  173. package/dist/esm/src/utils/computePoolAddress.js.map +1 -0
  174. package/dist/esm/src/utils/encodeRouteToPath.d.ts +8 -0
  175. package/dist/esm/src/utils/encodeRouteToPath.js +28 -0
  176. package/dist/esm/src/utils/encodeRouteToPath.js.map +1 -0
  177. package/dist/esm/src/utils/encodeSqrtRatioX96.d.ts +9 -0
  178. package/dist/esm/src/utils/encodeSqrtRatioX96.js +15 -0
  179. package/dist/esm/src/utils/encodeSqrtRatioX96.js.map +1 -0
  180. package/dist/esm/src/utils/fullMath.d.ts +8 -0
  181. package/dist/esm/src/utils/fullMath.js +16 -0
  182. package/dist/esm/src/utils/fullMath.js.map +1 -0
  183. package/dist/esm/src/utils/index.d.ts +18 -0
  184. package/dist/esm/src/utils/index.js +19 -0
  185. package/dist/esm/src/utils/index.js.map +1 -0
  186. package/dist/esm/src/utils/isSorted.d.ts +7 -0
  187. package/dist/esm/src/utils/isSorted.js +15 -0
  188. package/dist/esm/src/utils/isSorted.js.map +1 -0
  189. package/dist/esm/src/utils/liquidityMath.d.ts +8 -0
  190. package/dist/esm/src/utils/liquidityMath.js +17 -0
  191. package/dist/esm/src/utils/liquidityMath.js.map +1 -0
  192. package/dist/esm/src/utils/maxLiquidityForAmounts.d.ts +14 -0
  193. package/dist/esm/src/utils/maxLiquidityForAmounts.js +82 -0
  194. package/dist/esm/src/utils/maxLiquidityForAmounts.js.map +1 -0
  195. package/dist/esm/src/utils/mostSignificantBit.d.ts +2 -0
  196. package/dist/esm/src/utils/mostSignificantBit.js +22 -0
  197. package/dist/esm/src/utils/mostSignificantBit.js.map +1 -0
  198. package/dist/esm/src/utils/nearestUsableTick.d.ts +6 -0
  199. package/dist/esm/src/utils/nearestUsableTick.js +20 -0
  200. package/dist/esm/src/utils/nearestUsableTick.js.map +1 -0
  201. package/dist/esm/src/utils/position.d.ts +8 -0
  202. package/dist/esm/src/utils/position.js +16 -0
  203. package/dist/esm/src/utils/position.js.map +1 -0
  204. package/dist/esm/src/utils/priceTickConversions.d.ts +15 -0
  205. package/dist/esm/src/utils/priceTickConversions.js +44 -0
  206. package/dist/esm/src/utils/priceTickConversions.js.map +1 -0
  207. package/dist/esm/src/utils/sqrtPriceMath.d.ts +13 -0
  208. package/dist/esm/src/utils/sqrtPriceMath.js +90 -0
  209. package/dist/esm/src/utils/sqrtPriceMath.js.map +1 -0
  210. package/dist/esm/src/utils/swapMath.d.ts +9 -0
  211. package/dist/esm/src/utils/swapMath.js +73 -0
  212. package/dist/esm/src/utils/swapMath.js.map +1 -0
  213. package/dist/esm/src/utils/tickLibrary.d.ts +14 -0
  214. package/dist/esm/src/utils/tickLibrary.js +45 -0
  215. package/dist/esm/src/utils/tickLibrary.js.map +1 -0
  216. package/dist/esm/src/utils/tickList.d.ts +23 -0
  217. package/dist/esm/src/utils/tickList.js +103 -0
  218. package/dist/esm/src/utils/tickList.js.map +1 -0
  219. package/dist/esm/src/utils/tickMath.d.ts +34 -0
  220. package/dist/esm/src/utils/tickMath.js +120 -0
  221. package/dist/esm/src/utils/tickMath.js.map +1 -0
  222. package/dist/esm/src/utils/v3swap.d.ts +8 -0
  223. package/dist/esm/src/utils/v3swap.js +80 -0
  224. package/dist/esm/src/utils/v3swap.js.map +1 -0
  225. package/dist/types/src/constants.d.ts +23 -0
  226. package/dist/types/src/entities/index.d.ts +7 -0
  227. package/dist/types/src/entities/pool.d.ts +81 -0
  228. package/dist/types/src/entities/position.d.ts +131 -0
  229. package/dist/types/src/entities/route.d.ts +26 -0
  230. package/dist/types/src/entities/tick.d.ts +13 -0
  231. package/dist/types/src/entities/tickDataProvider.d.ts +31 -0
  232. package/dist/types/src/entities/tickListDataProvider.d.ts +15 -0
  233. package/dist/types/src/entities/trade.d.ts +220 -0
  234. package/dist/types/src/index.d.ts +10 -0
  235. package/dist/types/src/internalConstants.d.ts +6 -0
  236. package/dist/types/src/multicall.d.ts +10 -0
  237. package/dist/types/src/nonfungiblePositionManager.d.ts +159 -0
  238. package/dist/types/src/payments.d.ts +24 -0
  239. package/dist/types/src/quoter.d.ts +37 -0
  240. package/dist/types/src/selfPermit.d.ts +25 -0
  241. package/dist/types/src/staker.d.ts +101 -0
  242. package/dist/types/src/swapRouter.d.ts +51 -0
  243. package/dist/types/src/utils/calldata.d.ts +20 -0
  244. package/dist/types/src/utils/computePoolAddress.d.ts +20 -0
  245. package/dist/types/src/utils/encodeRouteToPath.d.ts +8 -0
  246. package/dist/types/src/utils/encodeSqrtRatioX96.d.ts +9 -0
  247. package/dist/types/src/utils/fullMath.d.ts +8 -0
  248. package/dist/types/src/utils/index.d.ts +18 -0
  249. package/dist/types/src/utils/isSorted.d.ts +7 -0
  250. package/dist/types/src/utils/liquidityMath.d.ts +8 -0
  251. package/dist/types/src/utils/maxLiquidityForAmounts.d.ts +14 -0
  252. package/dist/types/src/utils/mostSignificantBit.d.ts +2 -0
  253. package/dist/types/src/utils/nearestUsableTick.d.ts +6 -0
  254. package/dist/types/src/utils/position.d.ts +8 -0
  255. package/dist/types/src/utils/priceTickConversions.d.ts +15 -0
  256. package/dist/types/src/utils/sqrtPriceMath.d.ts +13 -0
  257. package/dist/types/src/utils/swapMath.d.ts +9 -0
  258. package/dist/types/src/utils/tickLibrary.d.ts +14 -0
  259. package/dist/types/src/utils/tickList.d.ts +23 -0
  260. package/dist/types/src/utils/tickMath.d.ts +34 -0
  261. package/dist/types/src/utils/v3swap.d.ts +8 -0
  262. package/package.json +72 -0
@@ -0,0 +1,220 @@
1
+ import { Currency, Percent, Price, CurrencyAmount, TradeType } from '@centurion_project/sdk-core';
2
+ import { Pool } from './pool';
3
+ import { Route } from './route';
4
+ /**
5
+ * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
6
+ * @template TInput The input token, either Ether or an ERC-20
7
+ * @template TOutput The output token, either Ether or an ERC-20
8
+ * @template TTradeType The trade type, either exact input or exact output
9
+ * @param a The first trade to compare
10
+ * @param b The second trade to compare
11
+ * @returns A sorted ordering for two neighboring elements in a trade array
12
+ */
13
+ export declare function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(a: Trade<TInput, TOutput, TTradeType>, b: Trade<TInput, TOutput, TTradeType>): number;
14
+ export interface BestTradeOptions {
15
+ maxNumResults?: number;
16
+ maxHops?: number;
17
+ }
18
+ /**
19
+ * Represents a trade executed against a set of routes where some percentage of the input is
20
+ * split across each route.
21
+ *
22
+ * Each route has its own set of pools. Pools can not be re-used across routes.
23
+ *
24
+ * Does not account for slippage, i.e., changes in price environment that can occur between
25
+ * the time the trade is submitted and when it is executed.
26
+ * @template TInput The input token, either Ether or an ERC-20
27
+ * @template TOutput The output token, either Ether or an ERC-20
28
+ * @template TTradeType The trade type, either exact input or exact output
29
+ */
30
+ export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
31
+ /**
32
+ * @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
33
+ * this will return an error.
34
+ *
35
+ * When the trade consists of just a single route, this returns the route of the trade,
36
+ * i.e. which pools the trade goes through.
37
+ */
38
+ get route(): Route<TInput, TOutput>;
39
+ /**
40
+ * The swaps of the trade, i.e. which routes and how much is swapped in each that
41
+ * make up the trade.
42
+ */
43
+ readonly swaps: {
44
+ route: Route<TInput, TOutput>;
45
+ inputAmount: CurrencyAmount<TInput>;
46
+ outputAmount: CurrencyAmount<TOutput>;
47
+ }[];
48
+ /**
49
+ * The type of the trade, either exact in or exact out.
50
+ */
51
+ readonly tradeType: TTradeType;
52
+ /**
53
+ * The cached result of the input amount computation
54
+ * @private
55
+ */
56
+ private _inputAmount;
57
+ /**
58
+ * The input amount for the trade assuming no slippage.
59
+ */
60
+ get inputAmount(): CurrencyAmount<TInput>;
61
+ /**
62
+ * The cached result of the output amount computation
63
+ * @private
64
+ */
65
+ private _outputAmount;
66
+ /**
67
+ * The output amount for the trade assuming no slippage.
68
+ */
69
+ get outputAmount(): CurrencyAmount<TOutput>;
70
+ /**
71
+ * The cached result of the computed execution price
72
+ * @private
73
+ */
74
+ private _executionPrice;
75
+ /**
76
+ * The price expressed in terms of output amount/input amount.
77
+ */
78
+ get executionPrice(): Price<TInput, TOutput>;
79
+ /**
80
+ * The cached result of the price impact computation
81
+ * @private
82
+ */
83
+ private _priceImpact;
84
+ /**
85
+ * Returns the percent difference between the route's mid price and the price impact
86
+ */
87
+ get priceImpact(): Percent;
88
+ /**
89
+ * Constructs an exact in trade with the given amount in and route
90
+ * @template TInput The input token, either Ether or an ERC-20
91
+ * @template TOutput The output token, either Ether or an ERC-20
92
+ * @param route The route of the exact in trade
93
+ * @param amountIn The amount being passed in
94
+ * @returns The exact in trade
95
+ */
96
+ static exactIn<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountIn: CurrencyAmount<TInput>): Promise<Trade<TInput, TOutput, TradeType.EXACT_INPUT>>;
97
+ /**
98
+ * Constructs an exact out trade with the given amount out and route
99
+ * @template TInput The input token, either Ether or an ERC-20
100
+ * @template TOutput The output token, either Ether or an ERC-20
101
+ * @param route The route of the exact out trade
102
+ * @param amountOut The amount returned by the trade
103
+ * @returns The exact out trade
104
+ */
105
+ static exactOut<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountOut: CurrencyAmount<TOutput>): Promise<Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>>;
106
+ /**
107
+ * Constructs a trade by simulating swaps through the given route
108
+ * @template TInput The input token, either Ether or an ERC-20.
109
+ * @template TOutput The output token, either Ether or an ERC-20.
110
+ * @template TTradeType The type of the trade, either exact in or exact out.
111
+ * @param route route to swap through
112
+ * @param amount the amount specified, either input or output, depending on tradeType
113
+ * @param tradeType whether the trade is an exact input or exact output swap
114
+ * @returns The route
115
+ */
116
+ static fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(route: Route<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType): Promise<Trade<TInput, TOutput, TTradeType>>;
117
+ /**
118
+ * Constructs a trade from routes by simulating swaps
119
+ *
120
+ * @template TInput The input token, either Ether or an ERC-20.
121
+ * @template TOutput The output token, either Ether or an ERC-20.
122
+ * @template TTradeType The type of the trade, either exact in or exact out.
123
+ * @param routes the routes to swap through and how much of the amount should be routed through each
124
+ * @param tradeType whether the trade is an exact input or exact output swap
125
+ * @returns The trade
126
+ */
127
+ static fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(routes: {
128
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
129
+ route: Route<TInput, TOutput>;
130
+ }[], tradeType: TTradeType): Promise<Trade<TInput, TOutput, TTradeType>>;
131
+ /**
132
+ * Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
133
+ * elsewhere and do not have any tick data
134
+ * @template TInput The input token, either Ether or an ERC-20
135
+ * @template TOutput The output token, either Ether or an ERC-20
136
+ * @template TTradeType The type of the trade, either exact in or exact out
137
+ * @param constructorArguments The arguments passed to the trade constructor
138
+ * @returns The unchecked trade
139
+ */
140
+ static createUncheckedTrade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(constructorArguments: {
141
+ route: Route<TInput, TOutput>;
142
+ inputAmount: CurrencyAmount<TInput>;
143
+ outputAmount: CurrencyAmount<TOutput>;
144
+ tradeType: TTradeType;
145
+ }): Trade<TInput, TOutput, TTradeType>;
146
+ /**
147
+ * Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
148
+ * elsewhere and do not have any tick data
149
+ * @template TInput The input token, either Ether or an ERC-20
150
+ * @template TOutput The output token, either Ether or an ERC-20
151
+ * @template TTradeType The type of the trade, either exact in or exact out
152
+ * @param constructorArguments The arguments passed to the trade constructor
153
+ * @returns The unchecked trade
154
+ */
155
+ static createUncheckedTradeWithMultipleRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(constructorArguments: {
156
+ routes: {
157
+ route: Route<TInput, TOutput>;
158
+ inputAmount: CurrencyAmount<TInput>;
159
+ outputAmount: CurrencyAmount<TOutput>;
160
+ }[];
161
+ tradeType: TTradeType;
162
+ }): Trade<TInput, TOutput, TTradeType>;
163
+ /**
164
+ * Construct a trade by passing in the pre-computed property values
165
+ * @param routes The routes through which the trade occurs
166
+ * @param tradeType The type of trade, exact input or exact output
167
+ */
168
+ private constructor();
169
+ /**
170
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
171
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
172
+ * @returns The amount out
173
+ */
174
+ minimumAmountOut(slippageTolerance: Percent, amountOut?: CurrencyAmount<TOutput>): CurrencyAmount<TOutput>;
175
+ /**
176
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
177
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
178
+ * @returns The amount in
179
+ */
180
+ maximumAmountIn(slippageTolerance: Percent, amountIn?: CurrencyAmount<TInput>): CurrencyAmount<TInput>;
181
+ /**
182
+ * Return the execution price after accounting for slippage tolerance
183
+ * @param slippageTolerance the allowed tolerated slippage
184
+ * @returns The execution price
185
+ */
186
+ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
187
+ /**
188
+ * Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
189
+ * amount to an output token, making at most `maxHops` hops.
190
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
191
+ * the amount in among multiple routes.
192
+ * @param pools the pools to consider in finding the best trade
193
+ * @param nextAmountIn exact amount of input currency to spend
194
+ * @param currencyOut the desired currency out
195
+ * @param maxNumResults maximum number of results to return
196
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
197
+ * @param currentPools used in recursion; the current list of pools
198
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
199
+ * @param bestTrades used in recursion; the current list of best trades
200
+ * @returns The exact in trade
201
+ */
202
+ static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pools: Pool[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPools?: Pool[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Promise<Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]>;
203
+ /**
204
+ * similar to the above method but instead targets a fixed output amount
205
+ * given a list of pools, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
206
+ * to an output token amount, making at most `maxHops` hops
207
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
208
+ * the amount in among multiple routes.
209
+ * @param pools the pools to consider in finding the best trade
210
+ * @param currencyIn the currency to spend
211
+ * @param currencyAmountOut the desired currency amount out
212
+ * @param nextAmountOut the exact amount of currency out
213
+ * @param maxNumResults maximum number of results to return
214
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
215
+ * @param currentPools used in recursion; the current list of pools
216
+ * @param bestTrades used in recursion; the current list of best trades
217
+ * @returns The exact out trade
218
+ */
219
+ static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(pools: Pool[], currencyIn: TInput, currencyAmountOut: CurrencyAmount<TOutput>, { maxNumResults, maxHops }?: BestTradeOptions, currentPools?: Pool[], nextAmountOut?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]): Promise<Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]>;
220
+ }
@@ -0,0 +1,440 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trade = exports.tradeComparator = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const sdk_core_1 = require("@centurion_project/sdk-core");
6
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
7
+ const internalConstants_1 = require("../internalConstants");
8
+ const pool_1 = require("./pool");
9
+ const route_1 = require("./route");
10
+ /**
11
+ * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
12
+ * @template TInput The input token, either Ether or an ERC-20
13
+ * @template TOutput The output token, either Ether or an ERC-20
14
+ * @template TTradeType The trade type, either exact input or exact output
15
+ * @param a The first trade to compare
16
+ * @param b The second trade to compare
17
+ * @returns A sorted ordering for two neighboring elements in a trade array
18
+ */
19
+ function tradeComparator(a, b) {
20
+ // must have same input and output token for comparison
21
+ (0, tiny_invariant_1.default)(a.inputAmount.currency.equals(b.inputAmount.currency), 'INPUT_CURRENCY');
22
+ (0, tiny_invariant_1.default)(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY');
23
+ if (a.outputAmount.equalTo(b.outputAmount)) {
24
+ if (a.inputAmount.equalTo(b.inputAmount)) {
25
+ // consider the number of hops since each hop costs gas
26
+ const aHops = a.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0);
27
+ const bHops = b.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0);
28
+ return aHops - bHops;
29
+ }
30
+ // trade A requires less input than trade B, so A should come first
31
+ if (a.inputAmount.lessThan(b.inputAmount)) {
32
+ return -1;
33
+ }
34
+ else {
35
+ return 1;
36
+ }
37
+ }
38
+ else {
39
+ // tradeA has less output than trade B, so should come second
40
+ if (a.outputAmount.lessThan(b.outputAmount)) {
41
+ return 1;
42
+ }
43
+ else {
44
+ return -1;
45
+ }
46
+ }
47
+ }
48
+ exports.tradeComparator = tradeComparator;
49
+ /**
50
+ * Represents a trade executed against a set of routes where some percentage of the input is
51
+ * split across each route.
52
+ *
53
+ * Each route has its own set of pools. Pools can not be re-used across routes.
54
+ *
55
+ * Does not account for slippage, i.e., changes in price environment that can occur between
56
+ * the time the trade is submitted and when it is executed.
57
+ * @template TInput The input token, either Ether or an ERC-20
58
+ * @template TOutput The output token, either Ether or an ERC-20
59
+ * @template TTradeType The trade type, either exact input or exact output
60
+ */
61
+ class Trade {
62
+ /**
63
+ * @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
64
+ * this will return an error.
65
+ *
66
+ * When the trade consists of just a single route, this returns the route of the trade,
67
+ * i.e. which pools the trade goes through.
68
+ */
69
+ get route() {
70
+ (0, tiny_invariant_1.default)(this.swaps.length === 1, 'MULTIPLE_ROUTES');
71
+ return this.swaps[0].route;
72
+ }
73
+ /**
74
+ * The input amount for the trade assuming no slippage.
75
+ */
76
+ get inputAmount() {
77
+ if (this._inputAmount) {
78
+ return this._inputAmount;
79
+ }
80
+ const inputCurrency = this.swaps[0].inputAmount.currency;
81
+ const totalInputFromRoutes = this.swaps
82
+ .map(({ inputAmount }) => inputAmount)
83
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(inputCurrency, 0));
84
+ this._inputAmount = totalInputFromRoutes;
85
+ return this._inputAmount;
86
+ }
87
+ /**
88
+ * The output amount for the trade assuming no slippage.
89
+ */
90
+ get outputAmount() {
91
+ if (this._outputAmount) {
92
+ return this._outputAmount;
93
+ }
94
+ const outputCurrency = this.swaps[0].outputAmount.currency;
95
+ const totalOutputFromRoutes = this.swaps
96
+ .map(({ outputAmount }) => outputAmount)
97
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(outputCurrency, 0));
98
+ this._outputAmount = totalOutputFromRoutes;
99
+ return this._outputAmount;
100
+ }
101
+ /**
102
+ * The price expressed in terms of output amount/input amount.
103
+ */
104
+ get executionPrice() {
105
+ var _a;
106
+ return ((_a = this._executionPrice) !== null && _a !== void 0 ? _a : (this._executionPrice = new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient)));
107
+ }
108
+ /**
109
+ * Returns the percent difference between the route's mid price and the price impact
110
+ */
111
+ get priceImpact() {
112
+ if (this._priceImpact) {
113
+ return this._priceImpact;
114
+ }
115
+ let spotOutputAmount = sdk_core_1.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
116
+ for (const { route, inputAmount } of this.swaps) {
117
+ const midPrice = route.midPrice;
118
+ spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
119
+ }
120
+ const priceImpact = spotOutputAmount.subtract(this.outputAmount).divide(spotOutputAmount);
121
+ this._priceImpact = new sdk_core_1.Percent(priceImpact.numerator, priceImpact.denominator);
122
+ return this._priceImpact;
123
+ }
124
+ /**
125
+ * Constructs an exact in trade with the given amount in and route
126
+ * @template TInput The input token, either Ether or an ERC-20
127
+ * @template TOutput The output token, either Ether or an ERC-20
128
+ * @param route The route of the exact in trade
129
+ * @param amountIn The amount being passed in
130
+ * @returns The exact in trade
131
+ */
132
+ static async exactIn(route, amountIn) {
133
+ return Trade.fromRoute(route, amountIn, sdk_core_1.TradeType.EXACT_INPUT);
134
+ }
135
+ /**
136
+ * Constructs an exact out trade with the given amount out and route
137
+ * @template TInput The input token, either Ether or an ERC-20
138
+ * @template TOutput The output token, either Ether or an ERC-20
139
+ * @param route The route of the exact out trade
140
+ * @param amountOut The amount returned by the trade
141
+ * @returns The exact out trade
142
+ */
143
+ static async exactOut(route, amountOut) {
144
+ return Trade.fromRoute(route, amountOut, sdk_core_1.TradeType.EXACT_OUTPUT);
145
+ }
146
+ /**
147
+ * Constructs a trade by simulating swaps through the given route
148
+ * @template TInput The input token, either Ether or an ERC-20.
149
+ * @template TOutput The output token, either Ether or an ERC-20.
150
+ * @template TTradeType The type of the trade, either exact in or exact out.
151
+ * @param route route to swap through
152
+ * @param amount the amount specified, either input or output, depending on tradeType
153
+ * @param tradeType whether the trade is an exact input or exact output swap
154
+ * @returns The route
155
+ */
156
+ static async fromRoute(route, amount, tradeType) {
157
+ const amounts = new Array(route.tokenPath.length);
158
+ let inputAmount;
159
+ let outputAmount;
160
+ if (tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
161
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.input), 'INPUT');
162
+ amounts[0] = amount.wrapped;
163
+ for (let i = 0; i < route.tokenPath.length - 1; i++) {
164
+ const pool = route.pools[i];
165
+ const [outputAmount] = await pool.getOutputAmount(amounts[i]);
166
+ amounts[i + 1] = outputAmount;
167
+ }
168
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
169
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
170
+ }
171
+ else {
172
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.output), 'OUTPUT');
173
+ amounts[amounts.length - 1] = amount.wrapped;
174
+ for (let i = route.tokenPath.length - 1; i > 0; i--) {
175
+ const pool = route.pools[i - 1];
176
+ const [inputAmount] = await pool.getInputAmount(amounts[i]);
177
+ amounts[i - 1] = inputAmount;
178
+ }
179
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amounts[0].numerator, amounts[0].denominator);
180
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
181
+ }
182
+ return new Trade({
183
+ routes: [{ inputAmount, outputAmount, route }],
184
+ tradeType,
185
+ });
186
+ }
187
+ /**
188
+ * Constructs a trade from routes by simulating swaps
189
+ *
190
+ * @template TInput The input token, either Ether or an ERC-20.
191
+ * @template TOutput The output token, either Ether or an ERC-20.
192
+ * @template TTradeType The type of the trade, either exact in or exact out.
193
+ * @param routes the routes to swap through and how much of the amount should be routed through each
194
+ * @param tradeType whether the trade is an exact input or exact output swap
195
+ * @returns The trade
196
+ */
197
+ static async fromRoutes(routes, tradeType) {
198
+ const populatedRoutes = [];
199
+ for (const { route, amount } of routes) {
200
+ const amounts = new Array(route.tokenPath.length);
201
+ let inputAmount;
202
+ let outputAmount;
203
+ if (tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
204
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.input), 'INPUT');
205
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
206
+ amounts[0] = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input.wrapped, amount.numerator, amount.denominator);
207
+ for (let i = 0; i < route.tokenPath.length - 1; i++) {
208
+ const pool = route.pools[i];
209
+ const [outputAmount] = await pool.getOutputAmount(amounts[i]);
210
+ amounts[i + 1] = outputAmount;
211
+ }
212
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
213
+ }
214
+ else {
215
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.output), 'OUTPUT');
216
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
217
+ amounts[amounts.length - 1] = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output.wrapped, amount.numerator, amount.denominator);
218
+ for (let i = route.tokenPath.length - 1; i > 0; i--) {
219
+ const pool = route.pools[i - 1];
220
+ const [inputAmount] = await pool.getInputAmount(amounts[i]);
221
+ amounts[i - 1] = inputAmount;
222
+ }
223
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amounts[0].numerator, amounts[0].denominator);
224
+ }
225
+ populatedRoutes.push({ route, inputAmount, outputAmount });
226
+ }
227
+ return new Trade({
228
+ routes: populatedRoutes,
229
+ tradeType,
230
+ });
231
+ }
232
+ /**
233
+ * Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
234
+ * elsewhere and do not have any tick data
235
+ * @template TInput The input token, either Ether or an ERC-20
236
+ * @template TOutput The output token, either Ether or an ERC-20
237
+ * @template TTradeType The type of the trade, either exact in or exact out
238
+ * @param constructorArguments The arguments passed to the trade constructor
239
+ * @returns The unchecked trade
240
+ */
241
+ static createUncheckedTrade(constructorArguments) {
242
+ return new Trade({
243
+ ...constructorArguments,
244
+ routes: [
245
+ {
246
+ inputAmount: constructorArguments.inputAmount,
247
+ outputAmount: constructorArguments.outputAmount,
248
+ route: constructorArguments.route,
249
+ },
250
+ ],
251
+ });
252
+ }
253
+ /**
254
+ * Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
255
+ * elsewhere and do not have any tick data
256
+ * @template TInput The input token, either Ether or an ERC-20
257
+ * @template TOutput The output token, either Ether or an ERC-20
258
+ * @template TTradeType The type of the trade, either exact in or exact out
259
+ * @param constructorArguments The arguments passed to the trade constructor
260
+ * @returns The unchecked trade
261
+ */
262
+ static createUncheckedTradeWithMultipleRoutes(constructorArguments) {
263
+ return new Trade(constructorArguments);
264
+ }
265
+ /**
266
+ * Construct a trade by passing in the pre-computed property values
267
+ * @param routes The routes through which the trade occurs
268
+ * @param tradeType The type of trade, exact input or exact output
269
+ */
270
+ constructor({ routes, tradeType, }) {
271
+ const inputCurrency = routes[0].inputAmount.currency;
272
+ const outputCurrency = routes[0].outputAmount.currency;
273
+ (0, tiny_invariant_1.default)(routes.every(({ route }) => inputCurrency.wrapped.equals(route.input.wrapped)), 'INPUT_CURRENCY_MATCH');
274
+ (0, tiny_invariant_1.default)(routes.every(({ route }) => outputCurrency.wrapped.equals(route.output.wrapped)), 'OUTPUT_CURRENCY_MATCH');
275
+ const numPools = routes.map(({ route }) => route.pools.length).reduce((total, cur) => total + cur, 0);
276
+ const poolAddressSet = new Set();
277
+ for (const { route } of routes) {
278
+ for (const pool of route.pools) {
279
+ poolAddressSet.add(pool_1.Pool.getAddress(pool.token0, pool.token1, pool.fee));
280
+ }
281
+ }
282
+ (0, tiny_invariant_1.default)(numPools === poolAddressSet.size, 'POOLS_DUPLICATED');
283
+ this.swaps = routes;
284
+ this.tradeType = tradeType;
285
+ }
286
+ /**
287
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
288
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
289
+ * @returns The amount out
290
+ */
291
+ minimumAmountOut(slippageTolerance, amountOut = this.outputAmount) {
292
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(internalConstants_1.ZERO), 'SLIPPAGE_TOLERANCE');
293
+ if (this.tradeType === sdk_core_1.TradeType.EXACT_OUTPUT) {
294
+ return amountOut;
295
+ }
296
+ else {
297
+ const slippageAdjustedAmountOut = new sdk_core_1.Fraction(internalConstants_1.ONE)
298
+ .add(slippageTolerance)
299
+ .invert()
300
+ .multiply(amountOut.quotient).quotient;
301
+ return sdk_core_1.CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
302
+ }
303
+ }
304
+ /**
305
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
306
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
307
+ * @returns The amount in
308
+ */
309
+ maximumAmountIn(slippageTolerance, amountIn = this.inputAmount) {
310
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(internalConstants_1.ZERO), 'SLIPPAGE_TOLERANCE');
311
+ if (this.tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
312
+ return amountIn;
313
+ }
314
+ else {
315
+ const slippageAdjustedAmountIn = new sdk_core_1.Fraction(internalConstants_1.ONE).add(slippageTolerance).multiply(amountIn.quotient).quotient;
316
+ return sdk_core_1.CurrencyAmount.fromRawAmount(amountIn.currency, slippageAdjustedAmountIn);
317
+ }
318
+ }
319
+ /**
320
+ * Return the execution price after accounting for slippage tolerance
321
+ * @param slippageTolerance the allowed tolerated slippage
322
+ * @returns The execution price
323
+ */
324
+ worstExecutionPrice(slippageTolerance) {
325
+ return new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
326
+ }
327
+ /**
328
+ * Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
329
+ * amount to an output token, making at most `maxHops` hops.
330
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
331
+ * the amount in among multiple routes.
332
+ * @param pools the pools to consider in finding the best trade
333
+ * @param nextAmountIn exact amount of input currency to spend
334
+ * @param currencyOut the desired currency out
335
+ * @param maxNumResults maximum number of results to return
336
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
337
+ * @param currentPools used in recursion; the current list of pools
338
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
339
+ * @param bestTrades used in recursion; the current list of best trades
340
+ * @returns The exact in trade
341
+ */
342
+ static async bestTradeExactIn(pools, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {},
343
+ // used in recursion.
344
+ currentPools = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
345
+ (0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
346
+ (0, tiny_invariant_1.default)(maxHops > 0, 'MAX_HOPS');
347
+ (0, tiny_invariant_1.default)(currencyAmountIn === nextAmountIn || currentPools.length > 0, 'INVALID_RECURSION');
348
+ const amountIn = nextAmountIn.wrapped;
349
+ const tokenOut = currencyOut.wrapped;
350
+ for (let i = 0; i < pools.length; i++) {
351
+ const pool = pools[i];
352
+ // pool irrelevant
353
+ if (!pool.token0.equals(amountIn.currency) && !pool.token1.equals(amountIn.currency))
354
+ continue;
355
+ let amountOut;
356
+ try {
357
+ ;
358
+ [amountOut] = await pool.getOutputAmount(amountIn);
359
+ }
360
+ catch (error) {
361
+ // input too low
362
+ if (error.isInsufficientInputAmountError) {
363
+ continue;
364
+ }
365
+ throw error;
366
+ }
367
+ // we have arrived at the output token, so this is the final trade of one of the paths
368
+ if (amountOut.currency.isToken && amountOut.currency.equals(tokenOut)) {
369
+ (0, sdk_core_1.sortedInsert)(bestTrades, await Trade.fromRoute(new route_1.Route([...currentPools, pool], currencyAmountIn.currency, currencyOut), currencyAmountIn, sdk_core_1.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
370
+ }
371
+ else if (maxHops > 1 && pools.length > 1) {
372
+ const poolsExcludingThisPool = pools.slice(0, i).concat(pools.slice(i + 1, pools.length));
373
+ // otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
374
+ await Trade.bestTradeExactIn(poolsExcludingThisPool, currencyAmountIn, currencyOut, {
375
+ maxNumResults,
376
+ maxHops: maxHops - 1,
377
+ }, [...currentPools, pool], amountOut, bestTrades);
378
+ }
379
+ }
380
+ return bestTrades;
381
+ }
382
+ /**
383
+ * similar to the above method but instead targets a fixed output amount
384
+ * given a list of pools, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
385
+ * to an output token amount, making at most `maxHops` hops
386
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
387
+ * the amount in among multiple routes.
388
+ * @param pools the pools to consider in finding the best trade
389
+ * @param currencyIn the currency to spend
390
+ * @param currencyAmountOut the desired currency amount out
391
+ * @param nextAmountOut the exact amount of currency out
392
+ * @param maxNumResults maximum number of results to return
393
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
394
+ * @param currentPools used in recursion; the current list of pools
395
+ * @param bestTrades used in recursion; the current list of best trades
396
+ * @returns The exact out trade
397
+ */
398
+ static async bestTradeExactOut(pools, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {},
399
+ // used in recursion.
400
+ currentPools = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
401
+ (0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
402
+ (0, tiny_invariant_1.default)(maxHops > 0, 'MAX_HOPS');
403
+ (0, tiny_invariant_1.default)(currencyAmountOut === nextAmountOut || currentPools.length > 0, 'INVALID_RECURSION');
404
+ const amountOut = nextAmountOut.wrapped;
405
+ const tokenIn = currencyIn.wrapped;
406
+ for (let i = 0; i < pools.length; i++) {
407
+ const pool = pools[i];
408
+ // pool irrelevant
409
+ if (!pool.token0.equals(amountOut.currency) && !pool.token1.equals(amountOut.currency))
410
+ continue;
411
+ let amountIn;
412
+ try {
413
+ ;
414
+ [amountIn] = await pool.getInputAmount(amountOut);
415
+ }
416
+ catch (error) {
417
+ // not enough liquidity in this pool
418
+ if (error.isInsufficientReservesError) {
419
+ continue;
420
+ }
421
+ throw error;
422
+ }
423
+ // we have arrived at the input token, so this is the first trade of one of the paths
424
+ if (amountIn.currency.equals(tokenIn)) {
425
+ (0, sdk_core_1.sortedInsert)(bestTrades, await Trade.fromRoute(new route_1.Route([pool, ...currentPools], currencyIn, currencyAmountOut.currency), currencyAmountOut, sdk_core_1.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
426
+ }
427
+ else if (maxHops > 1 && pools.length > 1) {
428
+ const poolsExcludingThisPool = pools.slice(0, i).concat(pools.slice(i + 1, pools.length));
429
+ // otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
430
+ await Trade.bestTradeExactOut(poolsExcludingThisPool, currencyIn, currencyAmountOut, {
431
+ maxNumResults,
432
+ maxHops: maxHops - 1,
433
+ }, [pool, ...currentPools], amountIn, bestTrades);
434
+ }
435
+ }
436
+ return bestTrades;
437
+ }
438
+ }
439
+ exports.Trade = Trade;
440
+ //# sourceMappingURL=trade.js.map