@kittycorn-labs/v4-sdk 1.1.30 → 2.0.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 (197) hide show
  1. package/README.md +9 -0
  2. package/dist/{PositionManager.d.ts → cjs/src/PositionManager.d.ts} +169 -169
  3. package/dist/cjs/src/PositionManager.js +242 -0
  4. package/dist/cjs/src/PositionManager.js.map +1 -0
  5. package/dist/cjs/src/abis/IMulticall.json +23 -0
  6. package/dist/{actionConstants.d.ts → cjs/src/actionConstants.d.ts} +1 -1
  7. package/dist/cjs/src/actionConstants.js +6 -0
  8. package/dist/cjs/src/actionConstants.js.map +1 -0
  9. package/dist/{entities → cjs/src/entities}/index.d.ts +4 -4
  10. package/dist/cjs/src/entities/index.js +8 -0
  11. package/dist/cjs/src/entities/index.js.map +1 -0
  12. package/dist/{entities → cjs/src/entities}/pool.d.ts +103 -103
  13. package/dist/cjs/src/entities/pool.js +196 -0
  14. package/dist/cjs/src/entities/pool.js.map +1 -0
  15. package/dist/{entities → cjs/src/entities}/position.d.ts +144 -144
  16. package/dist/cjs/src/entities/position.js +273 -0
  17. package/dist/cjs/src/entities/position.js.map +1 -0
  18. package/dist/{entities → cjs/src/entities}/route.d.ts +28 -28
  19. package/dist/cjs/src/entities/route.js +89 -0
  20. package/dist/cjs/src/entities/route.js.map +1 -0
  21. package/dist/{entities → cjs/src/entities}/trade.d.ts +220 -220
  22. package/dist/cjs/src/entities/trade.js +441 -0
  23. package/dist/cjs/src/entities/trade.js.map +1 -0
  24. package/dist/{index.d.ts → cjs/src/index.d.ts} +4 -4
  25. package/dist/cjs/src/index.js +8 -0
  26. package/dist/cjs/src/index.js.map +1 -0
  27. package/dist/{internalConstants.d.ts → cjs/src/internalConstants.d.ts} +46 -46
  28. package/dist/cjs/src/internalConstants.js +69 -0
  29. package/dist/cjs/src/internalConstants.js.map +1 -0
  30. package/dist/{multicall.d.ts → cjs/src/multicall.d.ts} +10 -10
  31. package/dist/cjs/src/multicall.js +26 -0
  32. package/dist/cjs/src/multicall.js.map +1 -0
  33. package/dist/cjs/src/tokenize.d.ts +17 -0
  34. package/dist/cjs/src/tokenize.js +42 -0
  35. package/dist/cjs/src/tokenize.js.map +1 -0
  36. package/dist/{utils → cjs/src/utils}/calldata.d.ts +20 -20
  37. package/dist/cjs/src/utils/calldata.js +20 -0
  38. package/dist/cjs/src/utils/calldata.js.map +1 -0
  39. package/dist/{utils → cjs/src/utils}/currencyMap.d.ts +2 -2
  40. package/dist/cjs/src/utils/currencyMap.js +15 -0
  41. package/dist/cjs/src/utils/currencyMap.js.map +1 -0
  42. package/dist/{utils → cjs/src/utils}/encodeRouteToPath.d.ts +10 -10
  43. package/dist/cjs/src/utils/encodeRouteToPath.js +26 -0
  44. package/dist/cjs/src/utils/encodeRouteToPath.js.map +1 -0
  45. package/dist/{utils → cjs/src/utils}/hook.d.ts +45 -45
  46. package/dist/cjs/src/utils/hook.js +97 -0
  47. package/dist/cjs/src/utils/hook.js.map +1 -0
  48. package/dist/{utils → cjs/src/utils}/index.d.ts +11 -11
  49. package/dist/cjs/src/utils/index.js +15 -0
  50. package/dist/cjs/src/utils/index.js.map +1 -0
  51. package/dist/{utils → cjs/src/utils}/pathCurrency.d.ts +4 -4
  52. package/dist/cjs/src/utils/pathCurrency.js +27 -0
  53. package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
  54. package/dist/{utils → cjs/src/utils}/positionManagerAbi.d.ts +102 -102
  55. package/dist/cjs/src/utils/positionManagerAbi.js +1318 -0
  56. package/dist/cjs/src/utils/positionManagerAbi.js.map +1 -0
  57. package/dist/{utils → cjs/src/utils}/priceTickConversions.d.ts +20 -20
  58. package/dist/cjs/src/utils/priceTickConversions.js +55 -0
  59. package/dist/cjs/src/utils/priceTickConversions.js.map +1 -0
  60. package/dist/{utils → cjs/src/utils}/sortsBefore.d.ts +2 -2
  61. package/dist/cjs/src/utils/sortsBefore.js +12 -0
  62. package/dist/cjs/src/utils/sortsBefore.js.map +1 -0
  63. package/dist/{utils → cjs/src/utils}/v4BaseActionsParser.d.ts +49 -45
  64. package/dist/cjs/src/utils/v4BaseActionsParser.js +193 -0
  65. package/dist/cjs/src/utils/v4BaseActionsParser.js.map +1 -0
  66. package/dist/{utils → cjs/src/utils}/v4Planner.d.ts +71 -55
  67. package/dist/cjs/src/utils/v4Planner.js +267 -0
  68. package/dist/cjs/src/utils/v4Planner.js.map +1 -0
  69. package/dist/{utils → cjs/src/utils}/v4PositionPlanner.d.ts +13 -12
  70. package/dist/cjs/src/utils/v4PositionPlanner.js +61 -0
  71. package/dist/cjs/src/utils/v4PositionPlanner.js.map +1 -0
  72. package/dist/esm/package.json +1 -0
  73. package/dist/esm/src/PositionManager.d.ts +169 -0
  74. package/dist/esm/src/PositionManager.js +237 -0
  75. package/dist/esm/src/PositionManager.js.map +1 -0
  76. package/dist/esm/src/abis/IMulticall.json +23 -0
  77. package/dist/esm/src/actionConstants.d.ts +1 -0
  78. package/dist/esm/src/actionConstants.js +3 -0
  79. package/dist/esm/src/actionConstants.js.map +1 -0
  80. package/dist/esm/src/entities/index.d.ts +4 -0
  81. package/dist/esm/src/entities/index.js +5 -0
  82. package/dist/esm/src/entities/index.js.map +1 -0
  83. package/dist/esm/src/entities/pool.d.ts +103 -0
  84. package/dist/esm/src/entities/pool.js +191 -0
  85. package/dist/esm/src/entities/pool.js.map +1 -0
  86. package/dist/esm/src/entities/position.d.ts +144 -0
  87. package/dist/esm/src/entities/position.js +268 -0
  88. package/dist/esm/src/entities/position.js.map +1 -0
  89. package/dist/esm/src/entities/route.d.ts +28 -0
  90. package/dist/esm/src/entities/route.js +84 -0
  91. package/dist/esm/src/entities/route.js.map +1 -0
  92. package/dist/esm/src/entities/trade.d.ts +220 -0
  93. package/dist/esm/src/entities/trade.js +435 -0
  94. package/dist/esm/src/entities/trade.js.map +1 -0
  95. package/dist/esm/src/index.d.ts +4 -0
  96. package/dist/esm/src/index.js +5 -0
  97. package/dist/esm/src/index.js.map +1 -0
  98. package/dist/esm/src/internalConstants.d.ts +46 -0
  99. package/dist/esm/src/internalConstants.js +65 -0
  100. package/dist/esm/src/internalConstants.js.map +1 -0
  101. package/dist/esm/src/multicall.d.ts +10 -0
  102. package/dist/esm/src/multicall.js +21 -0
  103. package/dist/esm/src/multicall.js.map +1 -0
  104. package/dist/esm/src/tokenize.d.ts +17 -0
  105. package/dist/esm/src/tokenize.js +37 -0
  106. package/dist/esm/src/tokenize.js.map +1 -0
  107. package/dist/esm/src/utils/calldata.d.ts +20 -0
  108. package/dist/esm/src/utils/calldata.js +15 -0
  109. package/dist/esm/src/utils/calldata.js.map +1 -0
  110. package/dist/esm/src/utils/currencyMap.d.ts +2 -0
  111. package/dist/esm/src/utils/currencyMap.js +11 -0
  112. package/dist/esm/src/utils/currencyMap.js.map +1 -0
  113. package/dist/esm/src/utils/encodeRouteToPath.d.ts +10 -0
  114. package/dist/esm/src/utils/encodeRouteToPath.js +22 -0
  115. package/dist/esm/src/utils/encodeRouteToPath.js.map +1 -0
  116. package/dist/esm/src/utils/hook.d.ts +45 -0
  117. package/dist/esm/src/utils/hook.js +92 -0
  118. package/dist/esm/src/utils/hook.js.map +1 -0
  119. package/dist/esm/src/utils/index.d.ts +11 -0
  120. package/dist/esm/src/utils/index.js +12 -0
  121. package/dist/esm/src/utils/index.js.map +1 -0
  122. package/dist/esm/src/utils/pathCurrency.d.ts +4 -0
  123. package/dist/esm/src/utils/pathCurrency.js +22 -0
  124. package/dist/esm/src/utils/pathCurrency.js.map +1 -0
  125. package/dist/esm/src/utils/positionManagerAbi.d.ts +102 -0
  126. package/dist/esm/src/utils/positionManagerAbi.js +1315 -0
  127. package/dist/esm/src/utils/positionManagerAbi.js.map +1 -0
  128. package/dist/esm/src/utils/priceTickConversions.d.ts +20 -0
  129. package/dist/esm/src/utils/priceTickConversions.js +49 -0
  130. package/dist/esm/src/utils/priceTickConversions.js.map +1 -0
  131. package/dist/esm/src/utils/sortsBefore.d.ts +2 -0
  132. package/dist/esm/src/utils/sortsBefore.js +8 -0
  133. package/dist/esm/src/utils/sortsBefore.js.map +1 -0
  134. package/dist/esm/src/utils/v4BaseActionsParser.d.ts +49 -0
  135. package/dist/esm/src/utils/v4BaseActionsParser.js +189 -0
  136. package/dist/esm/src/utils/v4BaseActionsParser.js.map +1 -0
  137. package/dist/esm/src/utils/v4Planner.d.ts +71 -0
  138. package/dist/esm/src/utils/v4Planner.js +261 -0
  139. package/dist/esm/src/utils/v4Planner.js.map +1 -0
  140. package/dist/esm/src/utils/v4PositionPlanner.d.ts +13 -0
  141. package/dist/esm/src/utils/v4PositionPlanner.js +57 -0
  142. package/dist/esm/src/utils/v4PositionPlanner.js.map +1 -0
  143. package/dist/types/src/PositionManager.d.ts +170 -0
  144. package/dist/types/src/PositionManager.d.ts.map +1 -0
  145. package/dist/types/src/actionConstants.d.ts +2 -0
  146. package/dist/types/src/actionConstants.d.ts.map +1 -0
  147. package/dist/types/src/entities/index.d.ts +5 -0
  148. package/dist/types/src/entities/index.d.ts.map +1 -0
  149. package/dist/types/src/entities/pool.d.ts +104 -0
  150. package/dist/types/src/entities/pool.d.ts.map +1 -0
  151. package/dist/types/src/entities/position.d.ts +145 -0
  152. package/dist/types/src/entities/position.d.ts.map +1 -0
  153. package/dist/types/src/entities/route.d.ts +29 -0
  154. package/dist/types/src/entities/route.d.ts.map +1 -0
  155. package/dist/types/src/entities/trade.d.ts +221 -0
  156. package/dist/types/src/entities/trade.d.ts.map +1 -0
  157. package/dist/types/src/index.d.ts +5 -0
  158. package/dist/types/src/index.d.ts.map +1 -0
  159. package/dist/types/src/internalConstants.d.ts +47 -0
  160. package/dist/types/src/internalConstants.d.ts.map +1 -0
  161. package/dist/types/src/multicall.d.ts +11 -0
  162. package/dist/types/src/multicall.d.ts.map +1 -0
  163. package/dist/types/src/tokenize.d.ts +18 -0
  164. package/dist/types/src/tokenize.d.ts.map +1 -0
  165. package/dist/types/src/utils/calldata.d.ts +21 -0
  166. package/dist/types/src/utils/calldata.d.ts.map +1 -0
  167. package/dist/types/src/utils/currencyMap.d.ts +3 -0
  168. package/dist/types/src/utils/currencyMap.d.ts.map +1 -0
  169. package/dist/types/src/utils/encodeRouteToPath.d.ts +11 -0
  170. package/dist/types/src/utils/encodeRouteToPath.d.ts.map +1 -0
  171. package/dist/types/src/utils/hook.d.ts +46 -0
  172. package/dist/types/src/utils/hook.d.ts.map +1 -0
  173. package/dist/types/src/utils/index.d.ts +12 -0
  174. package/dist/types/src/utils/index.d.ts.map +1 -0
  175. package/dist/types/src/utils/pathCurrency.d.ts +5 -0
  176. package/dist/types/src/utils/pathCurrency.d.ts.map +1 -0
  177. package/dist/types/src/utils/positionManagerAbi.d.ts +103 -0
  178. package/dist/types/src/utils/positionManagerAbi.d.ts.map +1 -0
  179. package/dist/types/src/utils/priceTickConversions.d.ts +21 -0
  180. package/dist/types/src/utils/priceTickConversions.d.ts.map +1 -0
  181. package/dist/types/src/utils/sortsBefore.d.ts +3 -0
  182. package/dist/types/src/utils/sortsBefore.d.ts.map +1 -0
  183. package/dist/types/src/utils/v4BaseActionsParser.d.ts +50 -0
  184. package/dist/types/src/utils/v4BaseActionsParser.d.ts.map +1 -0
  185. package/dist/types/src/utils/v4Planner.d.ts +72 -0
  186. package/dist/types/src/utils/v4Planner.d.ts.map +1 -0
  187. package/dist/types/src/utils/v4PositionPlanner.d.ts +14 -0
  188. package/dist/types/src/utils/v4PositionPlanner.d.ts.map +1 -0
  189. package/package.json +28 -46
  190. package/dist/index.js +0 -8
  191. package/dist/tokenizeAddresses.d.ts +0 -25
  192. package/dist/v4-sdk.cjs.development.js +0 -3633
  193. package/dist/v4-sdk.cjs.development.js.map +0 -1
  194. package/dist/v4-sdk.cjs.production.min.js +0 -2
  195. package/dist/v4-sdk.cjs.production.min.js.map +0 -1
  196. package/dist/v4-sdk.esm.js +0 -3597
  197. package/dist/v4-sdk.esm.js.map +0 -1
@@ -0,0 +1,144 @@
1
+ import { BigintIsh, Percent, Price, CurrencyAmount, Currency } from '@kittycorn-labs/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import { Pool } from './pool';
4
+ import { AllowanceTransferPermitBatch } from '../PositionManager';
5
+ interface PositionConstructorArgs {
6
+ pool: Pool;
7
+ liquidity: BigintIsh;
8
+ tickLower: number;
9
+ tickUpper: number;
10
+ }
11
+ /**
12
+ * Represents a position on a Uniswap V4 Pool
13
+ * @dev Similar to the V3 implementation
14
+ * - using Currency instead of Token
15
+ * - keep in mind that Pool and liquidity must be fetched from the pool manager
16
+ */
17
+ export declare class Position {
18
+ readonly pool: Pool;
19
+ readonly tickLower: number;
20
+ readonly tickUpper: number;
21
+ readonly liquidity: JSBI;
22
+ private _token0Amount;
23
+ private _token1Amount;
24
+ private _mintAmounts;
25
+ /**
26
+ * Constructs a position for a given pool with the given liquidity
27
+ * @param pool For which pool the liquidity is assigned
28
+ * @param liquidity The amount of liquidity that is in the position
29
+ * @param tickLower The lower tick of the position
30
+ * @param tickUpper The upper tick of the position
31
+ */
32
+ constructor({ pool, liquidity, tickLower, tickUpper }: PositionConstructorArgs);
33
+ /**
34
+ * Returns the price of token0 at the lower tick
35
+ */
36
+ get token0PriceLower(): Price<Currency, Currency>;
37
+ /**
38
+ * Returns the price of token0 at the upper tick
39
+ */
40
+ get token0PriceUpper(): Price<Currency, Currency>;
41
+ /**
42
+ * Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
43
+ */
44
+ get amount0(): CurrencyAmount<Currency>;
45
+ /**
46
+ * Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
47
+ */
48
+ get amount1(): CurrencyAmount<Currency>;
49
+ /**
50
+ * Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
51
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
52
+ * @returns The sqrt ratios after slippage
53
+ */
54
+ private ratiosAfterSlippage;
55
+ /**
56
+ * Returns the maximum amount of token0 and token1 that must be sent in order to safely mint the amount of liquidity held by the position
57
+ * with the given slippage tolerance
58
+ * @param slippageTolerance Tolerance of unfavorable slippage from the current price
59
+ * @returns The amounts, with slippage
60
+ * @dev In v4, minting and increasing is protected by maximum amounts of token0 and token1.
61
+ */
62
+ mintAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
63
+ amount0: JSBI;
64
+ amount1: JSBI;
65
+ }>;
66
+ /**
67
+ * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
68
+ * position with the given slippage tolerance
69
+ * @param slippageTolerance tolerance of unfavorable slippage from the current price
70
+ * @returns The amounts, with slippage
71
+ */
72
+ burnAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
73
+ amount0: JSBI;
74
+ amount1: JSBI;
75
+ }>;
76
+ /**
77
+ * Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
78
+ * the current price for the pool
79
+ */
80
+ get mintAmounts(): Readonly<{
81
+ amount0: JSBI;
82
+ amount1: JSBI;
83
+ }>;
84
+ /**
85
+ * Returns the AllowanceTransferPermitBatch for adding liquidity to a position
86
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
87
+ * @param spender The spender of the permit (should usually be the PositionManager)
88
+ * @param nonce A valid permit2 nonce
89
+ * @param deadline The deadline for the permit
90
+ */
91
+ permitBatchData(slippageTolerance: Percent, spender: string, nonce: BigintIsh, deadline: BigintIsh): AllowanceTransferPermitBatch;
92
+ /**
93
+ * Computes the maximum amount of liquidity received for a given amount of token0, token1,
94
+ * and the prices at the tick boundaries.
95
+ * @param pool The pool for which the position should be created
96
+ * @param tickLower The lower tick of the position
97
+ * @param tickUpper The upper tick of the position
98
+ * @param amount0 token0 amountzw
99
+ * @param amount1 token1 amount
100
+ * @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
101
+ * not what core can theoretically support
102
+ * @returns The amount of liquidity for the position
103
+ */
104
+ static fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision, }: {
105
+ pool: Pool;
106
+ tickLower: number;
107
+ tickUpper: number;
108
+ amount0: BigintIsh;
109
+ amount1: BigintIsh;
110
+ useFullPrecision: boolean;
111
+ }): Position;
112
+ /**
113
+ * Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
114
+ * @param pool The pool for which the position is created
115
+ * @param tickLower The lower tick
116
+ * @param tickUpper The upper tick
117
+ * @param amount0 The desired amount of token0
118
+ * @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
119
+ * not what core can theoretically support
120
+ * @returns The position
121
+ */
122
+ static fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision, }: {
123
+ pool: Pool;
124
+ tickLower: number;
125
+ tickUpper: number;
126
+ amount0: BigintIsh;
127
+ useFullPrecision: boolean;
128
+ }): Position;
129
+ /**
130
+ * Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
131
+ * @param pool The pool for which the position is created
132
+ * @param tickLower The lower tick
133
+ * @param tickUpper The upper tick
134
+ * @param amount1 The desired amount of token1
135
+ * @returns The position
136
+ */
137
+ static fromAmount1({ pool, tickLower, tickUpper, amount1, }: {
138
+ pool: Pool;
139
+ tickLower: number;
140
+ tickUpper: number;
141
+ amount1: BigintIsh;
142
+ }): Position;
143
+ }
144
+ export {};
@@ -0,0 +1,268 @@
1
+ import { Percent, CurrencyAmount, MaxUint256 } from '@kittycorn-labs/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import invariant from 'tiny-invariant';
4
+ import { Pool } from './pool.js';
5
+ import { encodeSqrtRatioX96, maxLiquidityForAmounts, SqrtPriceMath, TickMath } from '@kittycorn-labs/v3-sdk';
6
+ import { ZERO } from '../internalConstants.js';
7
+ import { tickToPrice } from '../utils/priceTickConversions.js';
8
+ /**
9
+ * Represents a position on a Uniswap V4 Pool
10
+ * @dev Similar to the V3 implementation
11
+ * - using Currency instead of Token
12
+ * - keep in mind that Pool and liquidity must be fetched from the pool manager
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.currency0, this.pool.currency1, this.tickLower);
40
+ }
41
+ /**
42
+ * Returns the price of token0 at the upper tick
43
+ */
44
+ get token0PriceUpper() {
45
+ return tickToPrice(this.pool.currency0, this.pool.currency1, 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) {
52
+ if (this.pool.tickCurrent < this.tickLower) {
53
+ this._token0Amount = CurrencyAmount.fromRawAmount(this.pool.currency0, 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.currency0, SqrtPriceMath.getAmount0Delta(this.pool.sqrtRatioX96, TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, false));
57
+ }
58
+ else {
59
+ this._token0Amount = CurrencyAmount.fromRawAmount(this.pool.currency0, 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) {
69
+ if (this.pool.tickCurrent < this.tickLower) {
70
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.currency1, ZERO);
71
+ }
72
+ else if (this.pool.tickCurrent < this.tickUpper) {
73
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.currency1, SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), this.pool.sqrtRatioX96, this.liquidity, false));
74
+ }
75
+ else {
76
+ this._token1Amount = CurrencyAmount.fromRawAmount(this.pool.currency1, 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 maximum amount of token0 and token1 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
+ * @dev In v4, minting and increasing is protected by maximum amounts of token0 and token1.
108
+ */
109
+ mintAmountsWithSlippage(slippageTolerance) {
110
+ // get lower/upper prices
111
+ // these represent the lowest and highest prices that the pool is allowed to "slip" to
112
+ const { sqrtRatioX96Upper, sqrtRatioX96Lower } = this.ratiosAfterSlippage(slippageTolerance);
113
+ // construct counterfactual pools from the lower bounded price and the upper bounded price
114
+ const poolLower = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, this.pool.tickSpacing, this.pool.hooks, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
115
+ const poolUpper = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, this.pool.tickSpacing, this.pool.hooks, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
116
+ // Note: Slippage derivation in v4 is different from v3.
117
+ // When creating a position (minting) or adding to a position (increasing) slippage is bounded by the MAXIMUM amount in in token0 and token1.
118
+ // The largest amount of token1 will happen when the price slips up, so we use the poolUpper to get amount1.
119
+ // The largest amount of token0 will happen when the price slips down, so we use the poolLower to get amount0.
120
+ // Ie...We want the larger amounts, which occurs at the upper price for amount1...
121
+ const { amount1 } = new Position({
122
+ pool: poolUpper,
123
+ liquidity: this.liquidity,
124
+ tickLower: this.tickLower,
125
+ tickUpper: this.tickUpper,
126
+ }).mintAmounts;
127
+ // ...and the lower for amount0
128
+ const { amount0 } = new Position({
129
+ pool: poolLower,
130
+ liquidity: this.liquidity,
131
+ tickLower: this.tickLower,
132
+ tickUpper: this.tickUpper,
133
+ }).mintAmounts;
134
+ return { amount0, amount1 };
135
+ }
136
+ /**
137
+ * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
138
+ * position with the given slippage tolerance
139
+ * @param slippageTolerance tolerance of unfavorable slippage from the current price
140
+ * @returns The amounts, with slippage
141
+ */
142
+ burnAmountsWithSlippage(slippageTolerance) {
143
+ // get lower/upper prices
144
+ const { sqrtRatioX96Upper, sqrtRatioX96Lower } = this.ratiosAfterSlippage(slippageTolerance);
145
+ // construct counterfactual pools
146
+ const poolLower = new Pool(this.pool.currency0, this.pool.currency1, this.pool.fee, this.pool.tickSpacing, this.pool.hooks, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
147
+ const poolUpper = new Pool(this.pool.currency0, this.pool.currency1, this.pool.fee, this.pool.tickSpacing, this.pool.hooks, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
148
+ // we want the smaller amounts...
149
+ // ...which occurs at the upper price for amount0...
150
+ const amount0 = new Position({
151
+ pool: poolUpper,
152
+ liquidity: this.liquidity,
153
+ tickLower: this.tickLower,
154
+ tickUpper: this.tickUpper,
155
+ }).amount0;
156
+ // ...and the lower for amount1
157
+ const amount1 = new Position({
158
+ pool: poolLower,
159
+ liquidity: this.liquidity,
160
+ tickLower: this.tickLower,
161
+ tickUpper: this.tickUpper,
162
+ }).amount1;
163
+ return { amount0: amount0.quotient, amount1: amount1.quotient };
164
+ }
165
+ /**
166
+ * Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
167
+ * the current price for the pool
168
+ */
169
+ get mintAmounts() {
170
+ if (this._mintAmounts === null) {
171
+ if (this.pool.tickCurrent < this.tickLower) {
172
+ return {
173
+ amount0: SqrtPriceMath.getAmount0Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
174
+ amount1: ZERO,
175
+ };
176
+ }
177
+ else if (this.pool.tickCurrent < this.tickUpper) {
178
+ return {
179
+ amount0: SqrtPriceMath.getAmount0Delta(this.pool.sqrtRatioX96, TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
180
+ amount1: SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), this.pool.sqrtRatioX96, this.liquidity, true),
181
+ };
182
+ }
183
+ else {
184
+ return {
185
+ amount0: ZERO,
186
+ amount1: SqrtPriceMath.getAmount1Delta(TickMath.getSqrtRatioAtTick(this.tickLower), TickMath.getSqrtRatioAtTick(this.tickUpper), this.liquidity, true),
187
+ };
188
+ }
189
+ }
190
+ return this._mintAmounts;
191
+ }
192
+ /**
193
+ * Returns the AllowanceTransferPermitBatch for adding liquidity to a position
194
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
195
+ * @param spender The spender of the permit (should usually be the PositionManager)
196
+ * @param nonce A valid permit2 nonce
197
+ * @param deadline The deadline for the permit
198
+ */
199
+ permitBatchData(slippageTolerance, spender, nonce, deadline) {
200
+ const { amount0, amount1 } = this.mintAmountsWithSlippage(slippageTolerance);
201
+ return {
202
+ details: [
203
+ {
204
+ token: this.pool.currency0.wrapped.address,
205
+ amount: amount0,
206
+ expiration: deadline,
207
+ nonce: nonce,
208
+ },
209
+ {
210
+ token: this.pool.currency1.wrapped.address,
211
+ amount: amount1,
212
+ expiration: deadline,
213
+ nonce: nonce,
214
+ },
215
+ ],
216
+ spender,
217
+ sigDeadline: deadline,
218
+ };
219
+ }
220
+ /**
221
+ * Computes the maximum amount of liquidity received for a given amount of token0, token1,
222
+ * and the prices at the tick boundaries.
223
+ * @param pool The pool for which the position should be created
224
+ * @param tickLower The lower tick of the position
225
+ * @param tickUpper The upper tick of the position
226
+ * @param amount0 token0 amountzw
227
+ * @param amount1 token1 amount
228
+ * @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
229
+ * not what core can theoretically support
230
+ * @returns The amount of liquidity for the position
231
+ */
232
+ static fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision, }) {
233
+ const sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower);
234
+ const sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(tickUpper);
235
+ return new Position({
236
+ pool,
237
+ tickLower,
238
+ tickUpper,
239
+ liquidity: maxLiquidityForAmounts(pool.sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision),
240
+ });
241
+ }
242
+ /**
243
+ * Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
244
+ * @param pool The pool for which the position is created
245
+ * @param tickLower The lower tick
246
+ * @param tickUpper The upper tick
247
+ * @param amount0 The desired amount of token0
248
+ * @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
249
+ * not what core can theoretically support
250
+ * @returns The position
251
+ */
252
+ static fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision, }) {
253
+ return Position.fromAmounts({ pool, tickLower, tickUpper, amount0, amount1: MaxUint256, useFullPrecision });
254
+ }
255
+ /**
256
+ * Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
257
+ * @param pool The pool for which the position is created
258
+ * @param tickLower The lower tick
259
+ * @param tickUpper The upper tick
260
+ * @param amount1 The desired amount of token1
261
+ * @returns The position
262
+ */
263
+ static fromAmount1({ pool, tickLower, tickUpper, amount1, }) {
264
+ // this function always uses full precision,
265
+ return Position.fromAmounts({ pool, tickLower, tickUpper, amount0: MaxUint256, amount1, useFullPrecision: true });
266
+ }
267
+ }
268
+ //# sourceMappingURL=position.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"position.js","sourceRoot":"","sources":["../../../../src/entities/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,OAAO,EAAS,cAAc,EAAY,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAC1G,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO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currency, Price } from '@kittycorn-labs/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 currency
6
+ * @template TOutput The output currency
7
+ */
8
+ export declare class Route<TInput extends Currency, TOutput extends Currency> {
9
+ readonly pools: Pool[];
10
+ readonly currencyPath: Currency[];
11
+ readonly input: TInput;
12
+ readonly output: TOutput;
13
+ readonly pathInput: Currency;
14
+ readonly pathOutput: Currency;
15
+ private _midPrice;
16
+ /**
17
+ * Creates an instance of route.
18
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
19
+ * @param input The input currency
20
+ * @param output The output currency
21
+ */
22
+ constructor(pools: Pool[], input: TInput, output: TOutput);
23
+ get chainId(): number;
24
+ /**
25
+ * Returns the mid price of the route
26
+ */
27
+ get midPrice(): Price<TInput, TOutput>;
28
+ }
@@ -0,0 +1,84 @@
1
+ import invariant from 'tiny-invariant';
2
+ import { Price } from '@kittycorn-labs/sdk-core';
3
+ import { getPathCurrency } from '../utils/pathCurrency.js';
4
+ /**
5
+ * Represents a list of pools through which a swap can occur
6
+ * @template TInput The input currency
7
+ * @template TOutput The output currency
8
+ */
9
+ export class Route {
10
+ /**
11
+ * Creates an instance of route.
12
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
13
+ * @param input The input currency
14
+ * @param output The output currency
15
+ */
16
+ constructor(pools, input, output) {
17
+ var _a;
18
+ this._midPrice = null;
19
+ invariant(pools.length > 0, 'POOLS');
20
+ const chainId = pools[0].chainId;
21
+ const allOnSameChain = pools.every((pool) => pool.chainId === chainId);
22
+ invariant(allOnSameChain, 'CHAIN_IDS');
23
+ /**
24
+ * function throws if pools do not involve the input and output currency or the native/wrapped equivalent
25
+ **/
26
+ this.pathInput = getPathCurrency(input, pools[0]);
27
+ this.pathOutput = getPathCurrency(output, pools[pools.length - 1]);
28
+ // If the input is native and the first pool is an eth-weth pool, that means we already wrapped the input to weth
29
+ // so we need to set the path input to be the wrapped input
30
+ if (pools[0].currency0.wrapped.equals(pools[0].currency1)) {
31
+ if (this.pathInput.isNative && ((_a = pools[1]) === null || _a === void 0 ? void 0 : _a.currency0.isNative)) {
32
+ this.pathInput = pools[0].currency1;
33
+ }
34
+ else if (this.pathInput.equals(pools[0].currency1) && pools[1] && !pools[1].currency0.isNative) {
35
+ this.pathInput = pools[0].currency0;
36
+ }
37
+ }
38
+ /**
39
+ * Normalizes currency0-currency1 order and selects the next currency/fee step to add to the path
40
+ * */
41
+ const currencyPath = [this.pathInput];
42
+ for (const [i, pool] of pools.entries()) {
43
+ const currentInputCurrency = currencyPath[i];
44
+ invariant(currentInputCurrency.equals(pool.currency0) || currentInputCurrency.equals(pool.currency1), 'PATH');
45
+ const nextCurrency = currentInputCurrency.equals(pool.currency0) ? pool.currency1 : pool.currency0;
46
+ currencyPath.push(nextCurrency);
47
+ }
48
+ this.pools = pools;
49
+ this.currencyPath = currencyPath;
50
+ this.input = input;
51
+ this.output = output !== null && output !== void 0 ? output : currencyPath[currencyPath.length - 1];
52
+ }
53
+ get chainId() {
54
+ return this.pools[0].chainId;
55
+ }
56
+ /**
57
+ * Returns the mid price of the route
58
+ */
59
+ get midPrice() {
60
+ if (this._midPrice !== null)
61
+ return this._midPrice;
62
+ const price = this.pools.slice(1).reduce(({ nextInput, price }, pool) => {
63
+ return nextInput.equals(pool.currency0)
64
+ ? {
65
+ nextInput: pool.currency1,
66
+ price: price.multiply(pool.currency0Price),
67
+ }
68
+ : {
69
+ nextInput: pool.currency0,
70
+ price: price.multiply(pool.currency1Price),
71
+ };
72
+ }, this.pools[0].currency0.equals(this.pathInput)
73
+ ? {
74
+ nextInput: this.pools[0].currency1,
75
+ price: this.pools[0].currency0Price,
76
+ }
77
+ : {
78
+ nextInput: this.pools[0].currency0,
79
+ price: this.pools[0].currency1Price,
80
+ }).price;
81
+ return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator));
82
+ }
83
+ }
84
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+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