@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,243 @@
1
+ import { MaxUint256, Percent, CurrencyAmount } from '@centurion_project/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import invariant from 'tiny-invariant';
4
+ import { ZERO } from '../internalConstants';
5
+ import { maxLiquidityForAmounts } from '../utils/maxLiquidityForAmounts';
6
+ import { tickToPrice } from '../utils/priceTickConversions';
7
+ import { SqrtPriceMath } from '../utils/sqrtPriceMath';
8
+ import { TickMath } from '../utils/tickMath';
9
+ import { encodeSqrtRatioX96 } from '../utils/encodeSqrtRatioX96';
10
+ import { Pool } from './pool';
11
+ /**
12
+ * Represents a position on a Uniswap V3 Pool
13
+ */
14
+ export class Position {
15
+ /**
16
+ * Constructs a position for a given pool with the given liquidity
17
+ * @param pool For which pool the liquidity is assigned
18
+ * @param liquidity The amount of liquidity that is in the position
19
+ * @param tickLower The lower tick of the position
20
+ * @param tickUpper The upper tick of the position
21
+ */
22
+ constructor({ pool, liquidity, tickLower, tickUpper }) {
23
+ // cached resuts for the getters
24
+ this._token0Amount = null;
25
+ this._token1Amount = null;
26
+ this._mintAmounts = null;
27
+ invariant(tickLower < tickUpper, 'TICK_ORDER');
28
+ invariant(tickLower >= TickMath.MIN_TICK && tickLower % pool.tickSpacing === 0, 'TICK_LOWER');
29
+ invariant(tickUpper <= TickMath.MAX_TICK && tickUpper % pool.tickSpacing === 0, 'TICK_UPPER');
30
+ this.pool = pool;
31
+ this.tickLower = tickLower;
32
+ this.tickUpper = tickUpper;
33
+ this.liquidity = JSBI.BigInt(liquidity);
34
+ }
35
+ /**
36
+ * Returns the price of token0 at the lower tick
37
+ */
38
+ get token0PriceLower() {
39
+ return tickToPrice(this.pool.token0, this.pool.token1, this.tickLower);
40
+ }
41
+ /**
42
+ * Returns the price of token0 at the upper tick
43
+ */
44
+ get token0PriceUpper() {
45
+ return tickToPrice(this.pool.token0, this.pool.token1, this.tickUpper);
46
+ }
47
+ /**
48
+ * Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
49
+ */
50
+ get amount0() {
51
+ if (this._token0Amount === null) {
52
+ if (this.pool.tickCurrent < this.tickLower) {
53
+ this._token0Amount = CurrencyAmount.fromRawAmount(this.pool.token0, SqrtPriceMath.getAmount0Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, false));
54
+ }
55
+ else if (this.pool.tickCurrent < this.tickUpper) {
56
+ this._token0Amount = CurrencyAmount.fromRawAmount(this.pool.token0, SqrtPriceMath.getAmount0Delta(this.pool.sqrtRatioX96, TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, false));
57
+ }
58
+ else {
59
+ this._token0Amount = CurrencyAmount.fromRawAmount(this.pool.token0, ZERO);
60
+ }
61
+ }
62
+ return this._token0Amount;
63
+ }
64
+ /**
65
+ * Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
66
+ */
67
+ get amount1() {
68
+ if (this._token1Amount === null) {
69
+ if (this.pool.tickCurrent < this.tickLower) {
70
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.token1, ZERO);
71
+ }
72
+ else if (this.pool.tickCurrent < this.tickUpper) {
73
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.token1, SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), this.pool.sqrtRatioX96, this.liquidity, false));
74
+ }
75
+ else {
76
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.token1, SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, false));
77
+ }
78
+ }
79
+ return this._token1Amount;
80
+ }
81
+ /**
82
+ * Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
83
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
84
+ * @returns The sqrt ratios after slippage
85
+ */
86
+ ratiosAfterSlippage(slippageTolerance) {
87
+ const priceLower = this.pool.token0Price.asFraction.multiply(new Percent(1).subtract(slippageTolerance));
88
+ const priceUpper = this.pool.token0Price.asFraction.multiply(slippageTolerance.add(1));
89
+ let sqrtRatioX96Lower = encodeSqrtRatioX96(priceLower.numerator, priceLower.denominator);
90
+ if (JSBI.lessThanOrEqual(sqrtRatioX96Lower, TickMath.MIN_SQRT_RATIO)) {
91
+ sqrtRatioX96Lower = JSBI.add(TickMath.MIN_SQRT_RATIO, JSBI.BigInt(1));
92
+ }
93
+ let sqrtRatioX96Upper = encodeSqrtRatioX96(priceUpper.numerator, priceUpper.denominator);
94
+ if (JSBI.greaterThanOrEqual(sqrtRatioX96Upper, TickMath.MAX_SQRT_RATIO)) {
95
+ sqrtRatioX96Upper = JSBI.subtract(TickMath.MAX_SQRT_RATIO, JSBI.BigInt(1));
96
+ }
97
+ return {
98
+ sqrtRatioX96Lower,
99
+ sqrtRatioX96Upper,
100
+ };
101
+ }
102
+ /**
103
+ * Returns the minimum amounts that must be sent in order to safely mint the amount of liquidity held by the position
104
+ * with the given slippage tolerance
105
+ * @param slippageTolerance Tolerance of unfavorable slippage from the current price
106
+ * @returns The amounts, with slippage
107
+ */
108
+ mintAmountsWithSlippage(slippageTolerance) {
109
+ // get lower/upper prices
110
+ const { sqrtRatioX96Upper, sqrtRatioX96Lower } = this.ratiosAfterSlippage(slippageTolerance);
111
+ // construct counterfactual pools
112
+ const poolLower = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
113
+ const poolUpper = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
114
+ // because the router is imprecise, we need to calculate the position that will be created (assuming no slippage)
115
+ const positionThatWillBeCreated = Position.fromAmounts({
116
+ pool: this.pool,
117
+ tickLower: this.tickLower,
118
+ tickUpper: this.tickUpper,
119
+ ...this.mintAmounts,
120
+ useFullPrecision: false,
121
+ });
122
+ // we want the smaller amounts...
123
+ // ...which occurs at the upper price for amount0...
124
+ const { amount0 } = new Position({
125
+ pool: poolUpper,
126
+ liquidity: positionThatWillBeCreated.liquidity,
127
+ tickLower: this.tickLower,
128
+ tickUpper: this.tickUpper,
129
+ }).mintAmounts;
130
+ // ...and the lower for amount1
131
+ const { amount1 } = new Position({
132
+ pool: poolLower,
133
+ liquidity: positionThatWillBeCreated.liquidity,
134
+ tickLower: this.tickLower,
135
+ tickUpper: this.tickUpper,
136
+ }).mintAmounts;
137
+ return { amount0, amount1 };
138
+ }
139
+ /**
140
+ * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
141
+ * position with the given slippage tolerance
142
+ * @param slippageTolerance tolerance of unfavorable slippage from the current price
143
+ * @returns The amounts, with slippage
144
+ */
145
+ burnAmountsWithSlippage(slippageTolerance) {
146
+ // get lower/upper prices
147
+ const { sqrtRatioX96Upper, sqrtRatioX96Lower } = this.ratiosAfterSlippage(slippageTolerance);
148
+ // construct counterfactual pools
149
+ const poolLower = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
150
+ const poolUpper = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
151
+ // we want the smaller amounts...
152
+ // ...which occurs at the upper price for amount0...
153
+ const amount0 = new Position({
154
+ pool: poolUpper,
155
+ liquidity: this.liquidity,
156
+ tickLower: this.tickLower,
157
+ tickUpper: this.tickUpper,
158
+ }).amount0;
159
+ // ...and the lower for amount1
160
+ const amount1 = new Position({
161
+ pool: poolLower,
162
+ liquidity: this.liquidity,
163
+ tickLower: this.tickLower,
164
+ tickUpper: this.tickUpper,
165
+ }).amount1;
166
+ return { amount0: amount0.quotient, amount1: amount1.quotient };
167
+ }
168
+ /**
169
+ * Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
170
+ * the current price for the pool
171
+ */
172
+ get mintAmounts() {
173
+ if (this._mintAmounts === null) {
174
+ if (this.pool.tickCurrent < this.tickLower) {
175
+ return {
176
+ amount0: SqrtPriceMath.getAmount0Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
177
+ amount1: ZERO,
178
+ };
179
+ }
180
+ else if (this.pool.tickCurrent < this.tickUpper) {
181
+ return {
182
+ amount0: SqrtPriceMath.getAmount0Delta(this.pool.sqrtRatioX96, TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
183
+ amount1: SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), this.pool.sqrtRatioX96, this.liquidity, true),
184
+ };
185
+ }
186
+ else {
187
+ return {
188
+ amount0: ZERO,
189
+ amount1: SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
190
+ };
191
+ }
192
+ }
193
+ return this._mintAmounts;
194
+ }
195
+ /**
196
+ * Computes the maximum amount of liquidity received for a given amount of token0, token1,
197
+ * and the prices at the tick boundaries.
198
+ * @param pool The pool for which the position should be created
199
+ * @param tickLower The lower tick of the position
200
+ * @param tickUpper The upper tick of the position
201
+ * @param amount0 token0 amount
202
+ * @param amount1 token1 amount
203
+ * @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
204
+ * not what core can theoretically support
205
+ * @returns The amount of liquidity for the position
206
+ */
207
+ static fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision, }) {
208
+ const sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower);
209
+ const sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(tickUpper);
210
+ return new Position({
211
+ pool,
212
+ tickLower,
213
+ tickUpper,
214
+ liquidity: maxLiquidityForAmounts(pool.sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision),
215
+ });
216
+ }
217
+ /**
218
+ * Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
219
+ * @param pool The pool for which the position is created
220
+ * @param tickLower The lower tick
221
+ * @param tickUpper The upper tick
222
+ * @param amount0 The desired amount of token0
223
+ * @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
224
+ * not what core can theoretically support
225
+ * @returns The position
226
+ */
227
+ static fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision, }) {
228
+ return Position.fromAmounts({ pool, tickLower, tickUpper, amount0, amount1: MaxUint256, useFullPrecision });
229
+ }
230
+ /**
231
+ * Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
232
+ * @param pool The pool for which the position is created
233
+ * @param tickLower The lower tick
234
+ * @param tickUpper The upper tick
235
+ * @param amount1 The desired amount of token1
236
+ * @returns The position
237
+ */
238
+ static fromAmount1({ pool, tickLower, tickUpper, amount1, }) {
239
+ // this function always uses full precision,
240
+ return Position.fromAmounts({ pool, tickLower, tickUpper, amount0: MaxUint256, amount1, useFullPrecision: true });
241
+ }
242
+ }
243
+ //# sourceMappingURL=position.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"position.js","sourceRoot":"","sources":["../../../../src/entities/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,UAAU,EAAE,OAAO,EAAS,cAAc,EAAS,MAAM,6BAA6B,CAAA;AAC1G,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,SAAS,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAA;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAA;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAS7B;;GAEG;AACH,MAAM,OAAO,QAAQ;IAWnB;;;;;;OAMG;IACH,YAAmB,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAA2B;QAZrF,gCAAgC;QACxB,kBAAa,GAAiC,IAAI,CAAA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UAAU,EAAE,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAA;IACnH,CAAC;CACF"}
@@ -0,0 +1,26 @@
1
+ import { Currency, Price, Token } from '@centurion_project/sdk-core';
2
+ import { Pool } from './pool';
3
+ /**
4
+ * Represents a list of pools through which a swap can occur
5
+ * @template TInput The input token
6
+ * @template TOutput The output token
7
+ */
8
+ export declare class Route<TInput extends Currency, TOutput extends Currency> {
9
+ readonly pools: Pool[];
10
+ readonly tokenPath: Token[];
11
+ readonly input: TInput;
12
+ readonly output: TOutput;
13
+ private _midPrice;
14
+ /**
15
+ * Creates an instance of route.
16
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
17
+ * @param input The input token
18
+ * @param output The output token
19
+ */
20
+ constructor(pools: Pool[], input: TInput, output: TOutput);
21
+ get chainId(): number;
22
+ /**
23
+ * Returns the mid price of the route
24
+ */
25
+ get midPrice(): Price<TInput, TOutput>;
26
+ }
@@ -0,0 +1,70 @@
1
+ import invariant from 'tiny-invariant';
2
+ import { Price } from '@centurion_project/sdk-core';
3
+ /**
4
+ * Represents a list of pools through which a swap can occur
5
+ * @template TInput The input token
6
+ * @template TOutput The output token
7
+ */
8
+ export class Route {
9
+ /**
10
+ * Creates an instance of route.
11
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
12
+ * @param input The input token
13
+ * @param output The output token
14
+ */
15
+ constructor(pools, input, output) {
16
+ this._midPrice = null;
17
+ invariant(pools.length > 0, 'POOLS');
18
+ const chainId = pools[0].chainId;
19
+ const allOnSameChain = pools.every((pool) => pool.chainId === chainId);
20
+ invariant(allOnSameChain, 'CHAIN_IDS');
21
+ const wrappedInput = input.wrapped;
22
+ invariant(pools[0].involvesToken(wrappedInput), 'INPUT');
23
+ invariant(pools[pools.length - 1].involvesToken(output.wrapped), 'OUTPUT');
24
+ /**
25
+ * Normalizes token0-token1 order and selects the next token/fee step to add to the path
26
+ * */
27
+ const tokenPath = [wrappedInput];
28
+ for (const [i, pool] of pools.entries()) {
29
+ const currentInputToken = tokenPath[i];
30
+ invariant(currentInputToken.equals(pool.token0) || currentInputToken.equals(pool.token1), 'PATH');
31
+ const nextToken = currentInputToken.equals(pool.token0) ? pool.token1 : pool.token0;
32
+ tokenPath.push(nextToken);
33
+ }
34
+ this.pools = pools;
35
+ this.tokenPath = tokenPath;
36
+ this.input = input;
37
+ this.output = output !== null && output !== void 0 ? output : tokenPath[tokenPath.length - 1];
38
+ }
39
+ get chainId() {
40
+ return this.pools[0].chainId;
41
+ }
42
+ /**
43
+ * Returns the mid price of the route
44
+ */
45
+ get midPrice() {
46
+ if (this._midPrice !== null)
47
+ return this._midPrice;
48
+ const price = this.pools.slice(1).reduce(({ nextInput, price }, pool) => {
49
+ return nextInput.equals(pool.token0)
50
+ ? {
51
+ nextInput: pool.token1,
52
+ price: price.multiply(pool.token0Price),
53
+ }
54
+ : {
55
+ nextInput: pool.token0,
56
+ price: price.multiply(pool.token1Price),
57
+ };
58
+ }, this.pools[0].token0.equals(this.input.wrapped)
59
+ ? {
60
+ nextInput: this.pools[0].token1,
61
+ price: this.pools[0].token0Price,
62
+ }
63
+ : {
64
+ nextInput: this.pools[0].token0,
65
+ price: this.pools[0].token1Price,
66
+ }).price;
67
+ return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator));
68
+ }
69
+ }
70
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../src/entities/route.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAA;AAEtC,OAAO,EAAY,KAAK,EAAS,MAAM,6BAA6B,CAAA;AAGpE;;;;GAIG;AACH,MAAM,OAAO,KAAK;IAQhB;;;;;OAKG;IACH,YAAmB,KAAa,EAAE,KAAa,EAAE,MAAe;QARxD,cAAS,GAAkC,IAAI,CAAA;QASrD,SAAS,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,OAAO,CAAC,CAAA;QAEp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CAAA;QAEP,OAAO,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,CAAA;IAClG,CAAC;CACF"}
@@ -0,0 +1,13 @@
1
+ import JSBI from 'jsbi';
2
+ import { BigintIsh } from '@centurion_project/sdk-core';
3
+ export interface TickConstructorArgs {
4
+ index: number;
5
+ liquidityGross: BigintIsh;
6
+ liquidityNet: BigintIsh;
7
+ }
8
+ export declare class Tick {
9
+ readonly index: number;
10
+ readonly liquidityGross: JSBI;
11
+ readonly liquidityNet: JSBI;
12
+ constructor({ index, liquidityGross, liquidityNet }: TickConstructorArgs);
13
+ }
@@ -0,0 +1,12 @@
1
+ import JSBI from 'jsbi';
2
+ import invariant from 'tiny-invariant';
3
+ import { TickMath } from '../utils';
4
+ export class Tick {
5
+ constructor({ index, liquidityGross, liquidityNet }) {
6
+ invariant(index >= TickMath.MIN_TICK && index <= TickMath.MAX_TICK, 'TICK');
7
+ this.index = index;
8
+ this.liquidityGross = JSBI.BigInt(liquidityGross);
9
+ this.liquidityNet = JSBI.BigInt(liquidityNet);
10
+ }
11
+ }
12
+ //# sourceMappingURL=tick.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tick.js","sourceRoot":"","sources":["../../../../src/entities/tick.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,SAAS,MAAM,gBAAgB,CAAA;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAQnC,MAAM,OAAO,IAAI;IAKf,YAAY,EAAE,KAAK,EAAE,cAAc,EAAE,YAAY,EAAuB;QACtE,SAAS,CAAC,KAAK,IAAI,QAAQ,CAAC,QAAQ,IAAI,KAAK,IAAI,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;QAC3E,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAA;QACjD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;IAC/C,CAAC;CACF"}
@@ -0,0 +1,31 @@
1
+ import { BigintIsh } from '@centurion_project/sdk-core';
2
+ /**
3
+ * Provides information about ticks
4
+ */
5
+ export interface TickDataProvider {
6
+ /**
7
+ * Return information corresponding to a specific tick
8
+ * @param tick the tick to load
9
+ */
10
+ getTick(tick: number): Promise<{
11
+ liquidityNet: BigintIsh;
12
+ }>;
13
+ /**
14
+ * Return the next tick that is initialized within a single word
15
+ * @param tick The current tick
16
+ * @param lte Whether the next tick should be lte the current tick
17
+ * @param tickSpacing The tick spacing of the pool
18
+ */
19
+ nextInitializedTickWithinOneWord(tick: number, lte: boolean, tickSpacing: number): Promise<[number, boolean]>;
20
+ }
21
+ /**
22
+ * This tick data provider does not know how to fetch any tick data. It throws whenever it is required. Useful if you
23
+ * do not need to load tick data for your use case.
24
+ */
25
+ export declare class NoTickDataProvider implements TickDataProvider {
26
+ private static ERROR_MESSAGE;
27
+ getTick(_tick: number): Promise<{
28
+ liquidityNet: BigintIsh;
29
+ }>;
30
+ nextInitializedTickWithinOneWord(_tick: number, _lte: boolean, _tickSpacing: number): Promise<[number, boolean]>;
31
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * This tick data provider does not know how to fetch any tick data. It throws whenever it is required. Useful if you
3
+ * do not need to load tick data for your use case.
4
+ */
5
+ export class NoTickDataProvider {
6
+ async getTick(_tick) {
7
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
8
+ }
9
+ async nextInitializedTickWithinOneWord(_tick, _lte, _tickSpacing) {
10
+ throw new Error(NoTickDataProvider.ERROR_MESSAGE);
11
+ }
12
+ }
13
+ NoTickDataProvider.ERROR_MESSAGE = 'No tick data provider was given';
14
+ //# sourceMappingURL=tickDataProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tickDataProvider.js","sourceRoot":"","sources":["../../../../src/entities/tickDataProvider.ts"],"names":[],"mappings":"AAqBA;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAE7B,KAAK,CAAC,OAAO,CAAC,KAAa;QACzB,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAA;IACnD,CAAC;IAED,KAAK,CAAC,gCAAgC,CACpC,KAAa,EACb,IAAa,EACb,YAAoB;QAEpB,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAA;IACnD,CAAC;;AAXc,gCAAa,GAAG,iCAAiC,CAAA"}
@@ -0,0 +1,15 @@
1
+ import { BigintIsh } from '@centurion_project/sdk-core';
2
+ import { Tick, TickConstructorArgs } from './tick';
3
+ import { TickDataProvider } from './tickDataProvider';
4
+ /**
5
+ * A data provider for ticks that is backed by an in-memory array of ticks.
6
+ */
7
+ export declare class TickListDataProvider implements TickDataProvider {
8
+ private ticks;
9
+ constructor(ticks: (Tick | TickConstructorArgs)[], tickSpacing: number);
10
+ getTick(tick: number): Promise<{
11
+ liquidityNet: BigintIsh;
12
+ liquidityGross: BigintIsh;
13
+ }>;
14
+ nextInitializedTickWithinOneWord(tick: number, lte: boolean, tickSpacing: number): Promise<[number, boolean]>;
15
+ }
@@ -0,0 +1,19 @@
1
+ import { TickList } from '../utils/tickList';
2
+ import { Tick } from './tick';
3
+ /**
4
+ * A data provider for ticks that is backed by an in-memory array of ticks.
5
+ */
6
+ export class TickListDataProvider {
7
+ constructor(ticks, tickSpacing) {
8
+ const ticksMapped = ticks.map((t) => (t instanceof Tick ? t : new Tick(t)));
9
+ TickList.validateList(ticksMapped, tickSpacing);
10
+ this.ticks = ticksMapped;
11
+ }
12
+ async getTick(tick) {
13
+ return TickList.getTick(this.ticks, tick);
14
+ }
15
+ async nextInitializedTickWithinOneWord(tick, lte, tickSpacing) {
16
+ return TickList.nextInitializedTickWithinOneWord(this.ticks, tick, lte, tickSpacing);
17
+ }
18
+ }
19
+ //# sourceMappingURL=tickListDataProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tickListDataProvider.js","sourceRoot":"","sources":["../../../../src/entities/tickListDataProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,IAAI,EAAuB,MAAM,QAAQ,CAAA;AAGlD;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAG/B,YAAY,KAAqC,EAAE,WAAmB;QACpE,MAAM,WAAW,GAAW,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QACnF,QAAQ,CAAC,YAAY,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;QAC/C,IAAI,CAAC,KAAK,GAAG,WAAW,CAAA;IAC1B,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAY;QACxB,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;IAED,KAAK,CAAC,gCAAgC,CAAC,IAAY,EAAE,GAAY,EAAE,WAAmB;QACpF,OAAO,QAAQ,CAAC,gCAAgC,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,WAAW,CAAC,CAAA;IACtF,CAAC;CACF"}