@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,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.v3Swap = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const swapMath_1 = require("./swapMath");
6
+ const liquidityMath_1 = require("./liquidityMath");
7
+ const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
8
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
9
+ const tickMath_1 = require("./tickMath");
10
+ const internalConstants_1 = require("../internalConstants");
11
+ async function v3Swap(fee, sqrtRatioX96, tickCurrent, liquidity, tickSpacing, tickDataProvider, zeroForOne, amountSpecified, sqrtPriceLimitX96) {
12
+ if (!sqrtPriceLimitX96)
13
+ sqrtPriceLimitX96 = zeroForOne
14
+ ? jsbi_1.default.add(tickMath_1.TickMath.MIN_SQRT_RATIO, internalConstants_1.ONE)
15
+ : jsbi_1.default.subtract(tickMath_1.TickMath.MAX_SQRT_RATIO, internalConstants_1.ONE);
16
+ if (zeroForOne) {
17
+ (0, tiny_invariant_1.default)(jsbi_1.default.greaterThan(sqrtPriceLimitX96, tickMath_1.TickMath.MIN_SQRT_RATIO), 'RATIO_MIN');
18
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThan(sqrtPriceLimitX96, sqrtRatioX96), 'RATIO_CURRENT');
19
+ }
20
+ else {
21
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThan(sqrtPriceLimitX96, tickMath_1.TickMath.MAX_SQRT_RATIO), 'RATIO_MAX');
22
+ (0, tiny_invariant_1.default)(jsbi_1.default.greaterThan(sqrtPriceLimitX96, sqrtRatioX96), 'RATIO_CURRENT');
23
+ }
24
+ const exactInput = jsbi_1.default.greaterThanOrEqual(amountSpecified, internalConstants_1.ZERO);
25
+ // keep track of swap state
26
+ const state = {
27
+ amountSpecifiedRemaining: amountSpecified,
28
+ amountCalculated: internalConstants_1.ZERO,
29
+ sqrtPriceX96: sqrtRatioX96,
30
+ tick: tickCurrent,
31
+ liquidity: liquidity,
32
+ };
33
+ // start swap while loop
34
+ while (jsbi_1.default.notEqual(state.amountSpecifiedRemaining, internalConstants_1.ZERO) && state.sqrtPriceX96 !== sqrtPriceLimitX96) {
35
+ let step = {};
36
+ step.sqrtPriceStartX96 = state.sqrtPriceX96;
37
+ [step.tickNext, step.initialized] = await tickDataProvider.nextInitializedTickWithinOneWord(state.tick, zeroForOne, tickSpacing);
38
+ if (step.tickNext < tickMath_1.TickMath.MIN_TICK) {
39
+ step.tickNext = tickMath_1.TickMath.MIN_TICK;
40
+ }
41
+ else if (step.tickNext > tickMath_1.TickMath.MAX_TICK) {
42
+ step.tickNext = tickMath_1.TickMath.MAX_TICK;
43
+ }
44
+ step.sqrtPriceNextX96 = tickMath_1.TickMath.getSqrtRatioAtTick(step.tickNext);
45
+ [state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount] = swapMath_1.SwapMath.computeSwapStep(state.sqrtPriceX96, (zeroForOne
46
+ ? jsbi_1.default.lessThan(step.sqrtPriceNextX96, sqrtPriceLimitX96)
47
+ : jsbi_1.default.greaterThan(step.sqrtPriceNextX96, sqrtPriceLimitX96))
48
+ ? sqrtPriceLimitX96
49
+ : step.sqrtPriceNextX96, state.liquidity, state.amountSpecifiedRemaining, fee);
50
+ if (exactInput) {
51
+ state.amountSpecifiedRemaining = jsbi_1.default.subtract(state.amountSpecifiedRemaining, jsbi_1.default.add(step.amountIn, step.feeAmount));
52
+ state.amountCalculated = jsbi_1.default.subtract(state.amountCalculated, step.amountOut);
53
+ }
54
+ else {
55
+ state.amountSpecifiedRemaining = jsbi_1.default.add(state.amountSpecifiedRemaining, step.amountOut);
56
+ state.amountCalculated = jsbi_1.default.add(state.amountCalculated, jsbi_1.default.add(step.amountIn, step.feeAmount));
57
+ }
58
+ // TODO
59
+ if (jsbi_1.default.equal(state.sqrtPriceX96, step.sqrtPriceNextX96)) {
60
+ // if the tick is initialized, run the tick transition
61
+ if (step.initialized) {
62
+ let liquidityNet = jsbi_1.default.BigInt((await tickDataProvider.getTick(step.tickNext)).liquidityNet);
63
+ // if we're moving leftward, we interpret liquidityNet as the opposite sign
64
+ // safe because liquidityNet cannot be type(int128).min
65
+ if (zeroForOne)
66
+ liquidityNet = jsbi_1.default.multiply(liquidityNet, internalConstants_1.NEGATIVE_ONE);
67
+ state.liquidity = liquidityMath_1.LiquidityMath.addDelta(state.liquidity, liquidityNet);
68
+ }
69
+ state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
70
+ }
71
+ else if (jsbi_1.default.notEqual(state.sqrtPriceX96, step.sqrtPriceStartX96)) {
72
+ // updated comparison function
73
+ // recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
74
+ state.tick = tickMath_1.TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
75
+ }
76
+ }
77
+ return {
78
+ amountCalculated: state.amountCalculated,
79
+ sqrtRatioX96: state.sqrtPriceX96,
80
+ liquidity: state.liquidity,
81
+ tickCurrent: state.tick,
82
+ };
83
+ }
84
+ exports.v3Swap = v3Swap;
85
+ //# sourceMappingURL=v3swap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"v3swap.js","sourceRoot":"","sources":["../../../../src/utils/v3swap.ts"],"names":[],"mappings":";;;;AAAA,yCAAqC;AACrC,mDAA+C;AAC/C,wDAAuB;AACvB,4EAAsC;AACtC,yCAAqC;AACrC,4DAA8D;AAavD,KAAK,UAAU,MAAM,CAC1B,GAAS,EACT,YAAkB,EAClB,WAAmB,EACnB,SAAe,EACf,WAAmB,EACnB,gBAAkC,EAClC,UAAmB,EACnB,eAAqB,EACrB,iBAAwB;IAExB,IAAI,CAAC,iBAAiB;QACpB,iBAAiB,GAAG,UAAU;YAC5B,CAAC,CAAC,cAAI,CAAC,GAAG,CAAC,mBAAQ,CAAC,cAAc,EAAE,uBAAG,CAAC;YACxC,CAAC,CAAC,cAAI,CAAC,QAAQ,CAAC,mBAAQ,CAAC,cAAc,EAAE,uBAAG,CAAC,CAAA;IAEjD,IAAI,UAAU,EAAE;QACd,IAAA,wBAAS,EAAC,cAAI,CAAC,WAAW,CAAC,iBAAiB,EAAE,mBAAQ,CAAC,cAAc,CAAC,EAAE,WAAW,CAAC,CAAA;QACpF,IAAA,wBAAS,EAAC,cAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,YAAY,CAAC,EAAE,eAAe,CAAC,CAAA;KAC3E;SAAM;QACL,IAAA,wBAAS,EAAC,cAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,mBAAQ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gB,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;gBAC5F,2EAA2E;gBAC3E,uDAAuD;gBACvD,IAAI,UAAU;oBAAE,YAAY,GAAG,cAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,gCAAY,CAAC,CAAA;gBAExE,KAAK,CAAC,SAAS,GAAG,6BAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,SAAS,EAAE,YAAY,CAAC,CAAA;aACxE;YAED,KAAK,CAAC,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAA;SAC5D;aAAM,IAAI,cAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,EAAE;YACpE,8BAA8B;YAC9B,uGAAuG;YACvG,KAAK,CAAC,IAAI,GAAG,mBAAQ,CAAC,kBAAkB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAA;SAC7D;KACF;IAED,OAAO;QACL,gBAAgB,EAAE,KAAK,CAAC,gBAAgB;QACxC,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,WAAW,EAAE,KAAK,CAAC,IAAI;KACxB,CAAA;AACH,CAAC;AA5GD,wBA4GC"}
@@ -0,0 +1,23 @@
1
+ import { ChainId } from '@centurion_project/sdk-core';
2
+ export declare const FACTORY_ADDRESS = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
3
+ export declare const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";
4
+ export declare const POOL_INIT_CODE_HASH = "0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54";
5
+ export declare function poolInitCodeHash(chainId?: ChainId): string;
6
+ /**
7
+ * The default factory enabled fee amounts, denominated in hundredths of bips.
8
+ */
9
+ export declare enum FeeAmount {
10
+ LOWEST = 100,
11
+ LOW_200 = 200,
12
+ LOW_300 = 300,
13
+ LOW_400 = 400,
14
+ LOW = 500,
15
+ MEDIUM = 3000,
16
+ HIGH = 10000
17
+ }
18
+ /**
19
+ * The default factory tick spacings by fee amount.
20
+ */
21
+ export declare const TICK_SPACINGS: {
22
+ [amount in FeeAmount]: number;
23
+ };
@@ -0,0 +1,40 @@
1
+ import { ChainId } from '@centurion_project/sdk-core';
2
+ export const FACTORY_ADDRESS = '0x1F98431c8aD98523631AE4a59f267346ea31F984';
3
+ export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
4
+ // @deprecated please use poolInitCodeHash(chainId: ChainId)
5
+ export const POOL_INIT_CODE_HASH = '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54';
6
+ export function poolInitCodeHash(chainId) {
7
+ switch (chainId) {
8
+ case ChainId.ZKSYNC:
9
+ return '0x010013f177ea1fcbc4520f9a3ca7cd2d1d77959e05aa66484027cb38e712aeed';
10
+ default:
11
+ return POOL_INIT_CODE_HASH;
12
+ }
13
+ }
14
+ /**
15
+ * The default factory enabled fee amounts, denominated in hundredths of bips.
16
+ */
17
+ export var FeeAmount;
18
+ (function (FeeAmount) {
19
+ FeeAmount[FeeAmount["LOWEST"] = 100] = "LOWEST";
20
+ FeeAmount[FeeAmount["LOW_200"] = 200] = "LOW_200";
21
+ FeeAmount[FeeAmount["LOW_300"] = 300] = "LOW_300";
22
+ FeeAmount[FeeAmount["LOW_400"] = 400] = "LOW_400";
23
+ FeeAmount[FeeAmount["LOW"] = 500] = "LOW";
24
+ FeeAmount[FeeAmount["MEDIUM"] = 3000] = "MEDIUM";
25
+ FeeAmount[FeeAmount["HIGH"] = 10000] = "HIGH";
26
+ })(FeeAmount || (FeeAmount = {}));
27
+ /**
28
+ * The default factory tick spacings by fee amount.
29
+ */
30
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
+ export const TICK_SPACINGS = {
32
+ [FeeAmount.LOWEST]: 1,
33
+ [FeeAmount.LOW_200]: 4,
34
+ [FeeAmount.LOW_300]: 6,
35
+ [FeeAmount.LOW_400]: 8,
36
+ [FeeAmount.LOW]: 10,
37
+ [FeeAmount.MEDIUM]: 60,
38
+ [FeeAmount.HIGH]: 200,
39
+ };
40
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAA;AAErD,MAAM,CAAC,MAAM,eAAe,GAAG,4CAA4C,CAAA;AAE3E,MAAM,CAAC,MAAM,YAAY,GAAG,4CAA4C,CAAA;AAExE,4DAA4D;AAC5D,MAAM,CAAC,MAAM,mBAAmB,GAAG,oEAAoE,CAAA;AAEvG,MAAM,UAAU,gBAAgB,CAAC,OAAiB;IAChD,QAAQ,OAAO,EAAE;QACf,KAAK,OAAO,CAAC,MAAM;YACjB,OAAO,oEAAoE,CAAA;QAC7E;YACE,OAAO,mBAAmB,CAAA;KAC7B;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAN,IAAY,SAQX;AARD,WAAY,SAAS;IACnB,+CAAY,CAAA;IACZ,iDAAa,CAAA;IACb,iDAAa,CAAA;IACb,iDAAa,CAAA;IACb,yCAAS,CAAA;IACT,gDAAa,CAAA;IACb,6CAAY,CAAA;AACd,CAAC,EARW,SAAS,KAAT,SAAS,QAQpB;AAED;;GAEG;AACH,6DAA6D;AAC7D,MAAM,CAAC,MAAM,aAAa,GAAsC;IAC9D,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;IACrB,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;IACtB,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;IACtB,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;IACtB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE;IACnB,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,EAAE;IACtB,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,GAAG;CACtB,CAAA"}
@@ -0,0 +1,7 @@
1
+ export * from './pool';
2
+ export * from './position';
3
+ export * from './route';
4
+ export * from './tick';
5
+ export * from './trade';
6
+ export * from './tickDataProvider';
7
+ export * from './tickListDataProvider';
@@ -0,0 +1,8 @@
1
+ export * from './pool';
2
+ export * from './position';
3
+ export * from './route';
4
+ export * from './tick';
5
+ export * from './trade';
6
+ export * from './tickDataProvider';
7
+ export * from './tickListDataProvider';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/entities/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA;AACtB,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA;AACvB,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA"}
@@ -0,0 +1,81 @@
1
+ import { BigintIsh, CurrencyAmount, Price, Token } from '@centurion_project/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import { FeeAmount } from '../constants';
4
+ import { Tick, TickConstructorArgs } from './tick';
5
+ import { TickDataProvider } from './tickDataProvider';
6
+ /**
7
+ * Represents a V3 pool
8
+ */
9
+ export declare class Pool {
10
+ readonly token0: Token;
11
+ readonly token1: Token;
12
+ readonly fee: FeeAmount;
13
+ readonly sqrtRatioX96: JSBI;
14
+ readonly liquidity: JSBI;
15
+ readonly tickCurrent: number;
16
+ readonly tickDataProvider: TickDataProvider;
17
+ private _token0Price?;
18
+ private _token1Price?;
19
+ static getAddress(tokenA: Token, tokenB: Token, fee: FeeAmount, initCodeHashManualOverride?: string, factoryAddressOverride?: string): string;
20
+ /**
21
+ * Construct a pool
22
+ * @param tokenA One of the tokens in the pool
23
+ * @param tokenB The other token in the pool
24
+ * @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
25
+ * @param sqrtRatioX96 The sqrt of the current ratio of amounts of token1 to token0
26
+ * @param liquidity The current value of in range liquidity
27
+ * @param tickCurrent The current tick of the pool
28
+ * @param ticks The current state of the pool ticks or a data provider that can return tick data
29
+ */
30
+ constructor(tokenA: Token, tokenB: Token, fee: FeeAmount, sqrtRatioX96: BigintIsh, liquidity: BigintIsh, tickCurrent: number, ticks?: TickDataProvider | (Tick | TickConstructorArgs)[]);
31
+ /**
32
+ * Returns true if the token is either token0 or token1
33
+ * @param token The token to check
34
+ * @returns True if token is either token0 or token
35
+ */
36
+ involvesToken(token: Token): boolean;
37
+ /**
38
+ * Returns the current mid price of the pool in terms of token0, i.e. the ratio of token1 over token0
39
+ */
40
+ get token0Price(): Price<Token, Token>;
41
+ /**
42
+ * Returns the current mid price of the pool in terms of token1, i.e. the ratio of token0 over token1
43
+ */
44
+ get token1Price(): Price<Token, Token>;
45
+ /**
46
+ * Return the price of the given token in terms of the other token in the pool.
47
+ * @param token The token to return price of
48
+ * @returns The price of the given token, in terms of the other.
49
+ */
50
+ priceOf(token: Token): Price<Token, Token>;
51
+ /**
52
+ * Returns the chain ID of the tokens in the pool.
53
+ */
54
+ get chainId(): number;
55
+ /**
56
+ * Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
57
+ * @param inputAmount The input amount for which to quote the output amount
58
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit
59
+ * @returns The output amount and the pool with updated state
60
+ */
61
+ getOutputAmount(inputAmount: CurrencyAmount<Token>, sqrtPriceLimitX96?: JSBI): Promise<[CurrencyAmount<Token>, Pool]>;
62
+ /**
63
+ * Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
64
+ * @param outputAmount the output amount for which to quote the input amount
65
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
66
+ * @returns The input amount and the pool with updated state
67
+ */
68
+ getInputAmount(outputAmount: CurrencyAmount<Token>, sqrtPriceLimitX96?: JSBI): Promise<[CurrencyAmount<Token>, Pool]>;
69
+ /**
70
+ * Executes a swap
71
+ * @param zeroForOne Whether the amount in is token0 or token1
72
+ * @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
73
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
74
+ * @returns amountCalculated
75
+ * @returns sqrtRatioX96
76
+ * @returns liquidity
77
+ * @returns tickCurrent
78
+ */
79
+ private swap;
80
+ get tickSpacing(): number;
81
+ }
@@ -0,0 +1,137 @@
1
+ import { CurrencyAmount, Price } from '@centurion_project/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import invariant from 'tiny-invariant';
4
+ import { FACTORY_ADDRESS, TICK_SPACINGS } from '../constants';
5
+ import { NEGATIVE_ONE, Q192 } from '../internalConstants';
6
+ import { computePoolAddress } from '../utils/computePoolAddress';
7
+ import { v3Swap } from '../utils/v3swap';
8
+ import { TickMath } from '../utils/tickMath';
9
+ import { NoTickDataProvider } from './tickDataProvider';
10
+ import { TickListDataProvider } from './tickListDataProvider';
11
+ /**
12
+ * By default, pools will not allow operations that require ticks.
13
+ */
14
+ const NO_TICK_DATA_PROVIDER_DEFAULT = new NoTickDataProvider();
15
+ /**
16
+ * Represents a V3 pool
17
+ */
18
+ export class Pool {
19
+ static getAddress(tokenA, tokenB, fee, initCodeHashManualOverride, factoryAddressOverride) {
20
+ return computePoolAddress({
21
+ factoryAddress: factoryAddressOverride !== null && factoryAddressOverride !== void 0 ? factoryAddressOverride : FACTORY_ADDRESS,
22
+ fee,
23
+ tokenA,
24
+ tokenB,
25
+ initCodeHashManualOverride,
26
+ });
27
+ }
28
+ /**
29
+ * Construct a pool
30
+ * @param tokenA One of the tokens in the pool
31
+ * @param tokenB The other token in the pool
32
+ * @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
33
+ * @param sqrtRatioX96 The sqrt of the current ratio of amounts of token1 to token0
34
+ * @param liquidity The current value of in range liquidity
35
+ * @param tickCurrent The current tick of the pool
36
+ * @param ticks The current state of the pool ticks or a data provider that can return tick data
37
+ */
38
+ constructor(tokenA, tokenB, fee, sqrtRatioX96, liquidity, tickCurrent, ticks = NO_TICK_DATA_PROVIDER_DEFAULT) {
39
+ invariant(Number.isInteger(fee) && fee < 1000000, 'FEE');
40
+ const tickCurrentSqrtRatioX96 = TickMath.getSqrtRatioAtTick(tickCurrent);
41
+ const nextTickSqrtRatioX96 = TickMath.getSqrtRatioAtTick(tickCurrent + 1);
42
+ invariant(JSBI.greaterThanOrEqual(JSBI.BigInt(sqrtRatioX96), tickCurrentSqrtRatioX96) &&
43
+ JSBI.lessThanOrEqual(JSBI.BigInt(sqrtRatioX96), nextTickSqrtRatioX96), 'PRICE_BOUNDS');
44
+ [this.token0, this.token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
45
+ this.fee = fee;
46
+ this.sqrtRatioX96 = JSBI.BigInt(sqrtRatioX96);
47
+ this.liquidity = JSBI.BigInt(liquidity);
48
+ this.tickCurrent = tickCurrent;
49
+ this.tickDataProvider = Array.isArray(ticks) ? new TickListDataProvider(ticks, TICK_SPACINGS[fee]) : ticks;
50
+ }
51
+ /**
52
+ * Returns true if the token is either token0 or token1
53
+ * @param token The token to check
54
+ * @returns True if token is either token0 or token
55
+ */
56
+ involvesToken(token) {
57
+ return token.equals(this.token0) || token.equals(this.token1);
58
+ }
59
+ /**
60
+ * Returns the current mid price of the pool in terms of token0, i.e. the ratio of token1 over token0
61
+ */
62
+ get token0Price() {
63
+ var _a;
64
+ return ((_a = this._token0Price) !== null && _a !== void 0 ? _a : (this._token0Price = new Price(this.token0, this.token1, Q192, JSBI.multiply(this.sqrtRatioX96, this.sqrtRatioX96))));
65
+ }
66
+ /**
67
+ * Returns the current mid price of the pool in terms of token1, i.e. the ratio of token0 over token1
68
+ */
69
+ get token1Price() {
70
+ var _a;
71
+ return ((_a = this._token1Price) !== null && _a !== void 0 ? _a : (this._token1Price = new Price(this.token1, this.token0, JSBI.multiply(this.sqrtRatioX96, this.sqrtRatioX96), Q192)));
72
+ }
73
+ /**
74
+ * Return the price of the given token in terms of the other token in the pool.
75
+ * @param token The token to return price of
76
+ * @returns The price of the given token, in terms of the other.
77
+ */
78
+ priceOf(token) {
79
+ invariant(this.involvesToken(token), 'TOKEN');
80
+ return token.equals(this.token0) ? this.token0Price : this.token1Price;
81
+ }
82
+ /**
83
+ * Returns the chain ID of the tokens in the pool.
84
+ */
85
+ get chainId() {
86
+ return this.token0.chainId;
87
+ }
88
+ /**
89
+ * Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
90
+ * @param inputAmount The input amount for which to quote the output amount
91
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit
92
+ * @returns The output amount and the pool with updated state
93
+ */
94
+ async getOutputAmount(inputAmount, sqrtPriceLimitX96) {
95
+ invariant(this.involvesToken(inputAmount.currency), 'TOKEN');
96
+ const zeroForOne = inputAmount.currency.equals(this.token0);
97
+ const { amountCalculated: outputAmount, sqrtRatioX96, liquidity, tickCurrent, } = await this.swap(zeroForOne, inputAmount.quotient, sqrtPriceLimitX96);
98
+ const outputToken = zeroForOne ? this.token1 : this.token0;
99
+ return [
100
+ CurrencyAmount.fromRawAmount(outputToken, JSBI.multiply(outputAmount, NEGATIVE_ONE)),
101
+ new Pool(this.token0, this.token1, this.fee, sqrtRatioX96, liquidity, tickCurrent, this.tickDataProvider),
102
+ ];
103
+ }
104
+ /**
105
+ * Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
106
+ * @param outputAmount the output amount for which to quote the input amount
107
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
108
+ * @returns The input amount and the pool with updated state
109
+ */
110
+ async getInputAmount(outputAmount, sqrtPriceLimitX96) {
111
+ invariant(outputAmount.currency.isToken && this.involvesToken(outputAmount.currency), 'TOKEN');
112
+ const zeroForOne = outputAmount.currency.equals(this.token1);
113
+ const { amountCalculated: inputAmount, sqrtRatioX96, liquidity, tickCurrent, } = await this.swap(zeroForOne, JSBI.multiply(outputAmount.quotient, NEGATIVE_ONE), sqrtPriceLimitX96);
114
+ const inputToken = zeroForOne ? this.token0 : this.token1;
115
+ return [
116
+ CurrencyAmount.fromRawAmount(inputToken, inputAmount),
117
+ new Pool(this.token0, this.token1, this.fee, sqrtRatioX96, liquidity, tickCurrent, this.tickDataProvider),
118
+ ];
119
+ }
120
+ /**
121
+ * Executes a swap
122
+ * @param zeroForOne Whether the amount in is token0 or token1
123
+ * @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
124
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
125
+ * @returns amountCalculated
126
+ * @returns sqrtRatioX96
127
+ * @returns liquidity
128
+ * @returns tickCurrent
129
+ */
130
+ async swap(zeroForOne, amountSpecified, sqrtPriceLimitX96) {
131
+ return v3Swap(JSBI.BigInt(this.fee), this.sqrtRatioX96, this.tickCurrent, this.liquidity, this.tickSpacing, this.tickDataProvider, zeroForOne, amountSpecified, sqrtPriceLimitX96);
132
+ }
133
+ get tickSpacing() {
134
+ return TICK_SPACINGS[this.fee];
135
+ }
136
+ }
137
+ //# sourceMappingURL=pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../../src/entities/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,cAAc,EAAE,KAAK,EAAS,MAAM,6BAA6B,CAAA;AACrF,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,SAAS,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,eAAe,EAAa,aAAa,EAAE,MAAM,cAAc,CAAA;AACxE,OAAO,EAAE,YAAY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WAAW,EAChB,IAAI,CAAC,gBAAgB,EACrB,UAAU,EACV,eAAe,EACf,iBAAiB,CAClB,CAAA;IACH,CAAC;IAED,IAAW,WAAW;QACpB,OAAO,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAChC,CAAC;CACF"}
@@ -0,0 +1,131 @@
1
+ import { BigintIsh, Percent, Price, CurrencyAmount, Token } from '@centurion_project/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import { Pool } from './pool';
4
+ interface PositionConstructorArgs {
5
+ pool: Pool;
6
+ tickLower: number;
7
+ tickUpper: number;
8
+ liquidity: BigintIsh;
9
+ }
10
+ /**
11
+ * Represents a position on a Uniswap V3 Pool
12
+ */
13
+ export declare class Position {
14
+ readonly pool: Pool;
15
+ readonly tickLower: number;
16
+ readonly tickUpper: number;
17
+ readonly liquidity: JSBI;
18
+ private _token0Amount;
19
+ private _token1Amount;
20
+ private _mintAmounts;
21
+ /**
22
+ * Constructs a position for a given pool with the given liquidity
23
+ * @param pool For which pool the liquidity is assigned
24
+ * @param liquidity The amount of liquidity that is in the position
25
+ * @param tickLower The lower tick of the position
26
+ * @param tickUpper The upper tick of the position
27
+ */
28
+ constructor({ pool, liquidity, tickLower, tickUpper }: PositionConstructorArgs);
29
+ /**
30
+ * Returns the price of token0 at the lower tick
31
+ */
32
+ get token0PriceLower(): Price<Token, Token>;
33
+ /**
34
+ * Returns the price of token0 at the upper tick
35
+ */
36
+ get token0PriceUpper(): Price<Token, Token>;
37
+ /**
38
+ * Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
39
+ */
40
+ get amount0(): CurrencyAmount<Token>;
41
+ /**
42
+ * Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
43
+ */
44
+ get amount1(): CurrencyAmount<Token>;
45
+ /**
46
+ * Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
47
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
48
+ * @returns The sqrt ratios after slippage
49
+ */
50
+ private ratiosAfterSlippage;
51
+ /**
52
+ * Returns the minimum amounts that must be sent in order to safely mint the amount of liquidity held by the position
53
+ * with the given slippage tolerance
54
+ * @param slippageTolerance Tolerance of unfavorable slippage from the current price
55
+ * @returns The amounts, with slippage
56
+ */
57
+ mintAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
58
+ amount0: JSBI;
59
+ amount1: JSBI;
60
+ }>;
61
+ /**
62
+ * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
63
+ * position with the given slippage tolerance
64
+ * @param slippageTolerance tolerance of unfavorable slippage from the current price
65
+ * @returns The amounts, with slippage
66
+ */
67
+ burnAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
68
+ amount0: JSBI;
69
+ amount1: JSBI;
70
+ }>;
71
+ /**
72
+ * Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
73
+ * the current price for the pool
74
+ */
75
+ get mintAmounts(): Readonly<{
76
+ amount0: JSBI;
77
+ amount1: JSBI;
78
+ }>;
79
+ /**
80
+ * Computes the maximum amount of liquidity received for a given amount of token0, token1,
81
+ * and the prices at the tick boundaries.
82
+ * @param pool The pool for which the position should be created
83
+ * @param tickLower The lower tick of the position
84
+ * @param tickUpper The upper tick of the position
85
+ * @param amount0 token0 amount
86
+ * @param amount1 token1 amount
87
+ * @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
88
+ * not what core can theoretically support
89
+ * @returns The amount of liquidity for the position
90
+ */
91
+ static fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision, }: {
92
+ pool: Pool;
93
+ tickLower: number;
94
+ tickUpper: number;
95
+ amount0: BigintIsh;
96
+ amount1: BigintIsh;
97
+ useFullPrecision: boolean;
98
+ }): Position;
99
+ /**
100
+ * Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
101
+ * @param pool The pool for which the position is created
102
+ * @param tickLower The lower tick
103
+ * @param tickUpper The upper tick
104
+ * @param amount0 The desired amount of token0
105
+ * @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
106
+ * not what core can theoretically support
107
+ * @returns The position
108
+ */
109
+ static fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision, }: {
110
+ pool: Pool;
111
+ tickLower: number;
112
+ tickUpper: number;
113
+ amount0: BigintIsh;
114
+ useFullPrecision: boolean;
115
+ }): Position;
116
+ /**
117
+ * Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
118
+ * @param pool The pool for which the position is created
119
+ * @param tickLower The lower tick
120
+ * @param tickUpper The upper tick
121
+ * @param amount1 The desired amount of token1
122
+ * @returns The position
123
+ */
124
+ static fromAmount1({ pool, tickLower, tickUpper, amount1, }: {
125
+ pool: Pool;
126
+ tickLower: number;
127
+ tickUpper: number;
128
+ amount1: BigintIsh;
129
+ }): Position;
130
+ }
131
+ export {};