@@ -21,6 +21,7 @@ import { PoolKey } from './entities/pool'
21
21
import { toAddress } from './utils/currencyMap'
22
22
import { MSG_SENDER } from './actionConstants'
23
23
import { V4PositionPlanner } from './utils'
24
+ import JSBI from 'jsbi'
24
25
25
26
describe ( 'PositionManager' , ( ) => {
26
27
const currency0 = new Token ( 1 , '0x0000000000000000000000000000000000000001' , 18 , 't0' , 'currency0' )
@@ -157,6 +158,10 @@ describe('PositionManager', () => {
157
158
tickUpper : TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
158
159
liquidity : 5000000 ,
159
160
} )
161
+
162
+ // These are the max amounts that the user can possibly add to the position
163
+ const { amount0, amount1 } = position . mintAmounts
164
+
160
165
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
161
166
recipient,
162
167
slippageTolerance,
@@ -165,14 +170,18 @@ describe('PositionManager', () => {
165
170
166
171
// Rebuild the calldata with the planner for the expected mint.
167
172
// Note that this test verifies that the applied logic in addCallParameters is correct but does not necessarily test the validity of the calldata itself.
168
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
173
+ const {
174
+ amount0 : adjustedAmount0 ,
175
+ amount1 : adjustedAmount1 ,
176
+ liquidity : liquidityMax ,
177
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
169
178
planner . addAction ( Actions . MINT_POSITION , [
170
179
pool_0_1 . poolKey ,
171
180
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
172
181
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
173
- 5000000 ,
174
- toHex ( amount0Max ) ,
175
- toHex ( amount1Max ) ,
182
+ toHex ( liquidityMax ) ,
183
+ toHex ( amount0 ) ,
184
+ toHex ( amount1 ) ,
176
185
recipient ,
177
186
EMPTY_BYTES ,
178
187
] )
@@ -181,6 +190,11 @@ describe('PositionManager', () => {
181
190
182
191
expect ( calldata ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
183
192
expect ( value ) . toEqual ( '0x00' )
193
+
194
+ // The adjusted amounts are less than or equal to the max amounts
195
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
196
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
197
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
184
198
} )
185
199
186
200
it ( 'succeeds for increase' , ( ) => {
@@ -191,6 +205,9 @@ describe('PositionManager', () => {
191
205
liquidity : 666 ,
192
206
} )
193
207
208
+ // These are the max amounts that the user can possibly add to the position
209
+ const { amount0, amount1 } = position . mintAmounts
210
+
194
211
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
195
212
tokenId,
196
213
slippageTolerance,
@@ -199,18 +216,27 @@ describe('PositionManager', () => {
199
216
200
217
// Rebuild the calldata with the planner for increase
201
218
const planner = new V4Planner ( )
202
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
219
+ const {
220
+ amount0 : adjustedAmount0 ,
221
+ amount1 : adjustedAmount1 ,
222
+ liquidity : liquidityMax ,
223
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
203
224
planner . addAction ( Actions . INCREASE_LIQUIDITY , [
204
225
tokenId . toString ( ) ,
205
- 666 ,
206
- toHex ( amount0Max ) ,
207
- toHex ( amount1Max ) ,
226
+ toHex ( liquidityMax ) ,
227
+ toHex ( amount0 ) ,
228
+ toHex ( amount1 ) ,
208
229
EMPTY_BYTES ,
209
230
] )
210
231
// Expect there to be a settle pair call afterwards
211
232
planner . addAction ( Actions . SETTLE_PAIR , [ toAddress ( pool_0_1 . currency0 ) , toAddress ( pool_0_1 . currency1 ) ] )
212
233
expect ( calldata ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
213
234
expect ( value ) . toEqual ( '0x00' )
235
+
236
+ // The adjusted amounts are less than or equal to the max amounts
237
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
238
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
239
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
214
240
} )
215
241
216
242
it ( 'succeeds when createPool is true' , ( ) => {
@@ -220,6 +246,9 @@ describe('PositionManager', () => {
220
246
tickUpper : TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
221
247
liquidity : 90000000000000 ,
222
248
} )
249
+
250
+ // These are the max amounts that the user can possibly add to the position
251
+ const { amount0, amount1 } = position . mintAmounts
223
252
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
224
253
recipient,
225
254
slippageTolerance,
@@ -235,21 +264,30 @@ describe('PositionManager', () => {
235
264
V4PositionManager . INTERFACE . encodeFunctionData ( 'initializePool' , [ pool_0_1 . poolKey , SQRT_PRICE_1_1 . toString ( ) ] )
236
265
)
237
266
const planner = new V4Planner ( )
238
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
267
+ const {
268
+ amount0 : adjustedAmount0 ,
269
+ amount1 : adjustedAmount1 ,
270
+ liquidity : liquidityMax ,
271
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
239
272
// Expect position to be minted correctly
240
273
planner . addAction ( Actions . MINT_POSITION , [
241
274
pool_0_1 . poolKey ,
242
275
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
243
276
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
244
- 90000000000000 ,
245
- toHex ( amount0Max ) ,
246
- toHex ( amount1Max ) ,
277
+ toHex ( liquidityMax ) ,
278
+ toHex ( amount0 ) ,
279
+ toHex ( amount1 ) ,
247
280
recipient ,
248
281
EMPTY_BYTES ,
249
282
] )
250
283
planner . addAction ( Actions . SETTLE_PAIR , [ toAddress ( pool_0_1 . currency0 ) , toAddress ( pool_0_1 . currency1 ) ] )
251
284
expect ( calldataList [ 1 ] ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
252
285
expect ( value ) . toEqual ( '0x00' )
286
+
287
+ // The adjusted amounts are less than or equal to the max amounts
288
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
289
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
290
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
253
291
} )
254
292
255
293
it ( 'succeeds when useNative is set' , ( ) => {
@@ -259,6 +297,9 @@ describe('PositionManager', () => {
259
297
tickUpper : TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
260
298
liquidity : 1 ,
261
299
} )
300
+
301
+ // These are the max amounts that the user can possibly add to the position
302
+ const { amount0, amount1 } = position . mintAmounts
262
303
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
263
304
recipient,
264
305
slippageTolerance,
@@ -269,24 +310,32 @@ describe('PositionManager', () => {
269
310
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
270
311
271
312
const planner = new V4Planner ( )
272
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
313
+ const {
314
+ amount0 : adjustedAmount0 ,
315
+ amount1 : adjustedAmount1 ,
316
+ liquidity : liquidityMax ,
317
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
273
318
// Expect position to be minted correctly
274
319
planner . addAction ( Actions . MINT_POSITION , [
275
320
pool_1_eth . poolKey ,
276
321
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
277
322
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
278
- 1 ,
279
- toHex ( amount0Max ) ,
280
- toHex ( amount1Max ) ,
323
+ toHex ( liquidityMax ) ,
324
+ toHex ( amount0 ) ,
325
+ toHex ( amount1 ) ,
281
326
recipient ,
282
327
EMPTY_BYTES ,
283
328
] )
284
329
285
330
planner . addAction ( Actions . SETTLE_PAIR , [ toAddress ( pool_1_eth . currency0 ) , toAddress ( pool_1_eth . currency1 ) ] )
286
331
planner . addAction ( Actions . SWEEP , [ toAddress ( pool_1_eth . currency0 ) , MSG_SENDER ] )
287
332
expect ( calldata ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
333
+ expect ( value ) . toEqual ( toHex ( amount0 ) )
288
334
289
- expect ( value ) . toEqual ( toHex ( amount0Max ) )
335
+ // The adjusted amounts are less than or equal to the max amounts
336
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
337
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
338
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
290
339
} )
291
340
292
341
it ( 'succeeds when migrate is true' , ( ) => {
@@ -296,6 +345,9 @@ describe('PositionManager', () => {
296
345
tickUpper : TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
297
346
liquidity : 1 ,
298
347
} )
348
+
349
+ // These are the max amounts that the user can possibly add to the position
350
+ const { amount0, amount1 } = position . mintAmounts
299
351
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
300
352
recipient,
301
353
slippageTolerance,
@@ -305,15 +357,19 @@ describe('PositionManager', () => {
305
357
306
358
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
307
359
const planner = new V4Planner ( )
308
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
360
+ const {
361
+ amount0 : adjustedAmount0 ,
362
+ amount1 : adjustedAmount1 ,
363
+ liquidity : liquidityMax ,
364
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
309
365
// Expect position to be minted correctly
310
366
planner . addAction ( Actions . MINT_POSITION , [
311
367
pool_0_1 . poolKey ,
312
368
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
313
369
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
314
- 1 ,
315
- toHex ( amount0Max ) ,
316
- toHex ( amount1Max ) ,
370
+ toHex ( liquidityMax ) ,
371
+ toHex ( amount0 ) ,
372
+ toHex ( amount1 ) ,
317
373
recipient ,
318
374
EMPTY_BYTES ,
319
375
] )
@@ -325,6 +381,11 @@ describe('PositionManager', () => {
325
381
expect ( calldata ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
326
382
327
383
expect ( value ) . toEqual ( '0x00' )
384
+
385
+ // The adjusted amounts are less than or equal to the max amounts
386
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
387
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
388
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
328
389
} )
329
390
330
391
it ( 'succeeds when migrating to an eth position' , ( ) => {
@@ -334,6 +395,9 @@ describe('PositionManager', () => {
334
395
tickUpper : TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
335
396
liquidity : 1 ,
336
397
} )
398
+
399
+ // These are the max amounts that the user can possibly add to the position
400
+ const { amount0, amount1 } = position . mintAmounts
337
401
const { calldata, value } = V4PositionManager . addCallParameters ( position , {
338
402
recipient,
339
403
slippageTolerance,
@@ -344,15 +408,19 @@ describe('PositionManager', () => {
344
408
345
409
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
346
410
const planner = new V4Planner ( )
347
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
411
+ const {
412
+ amount0 : adjustedAmount0 ,
413
+ amount1 : adjustedAmount1 ,
414
+ liquidity : liquidityMax ,
415
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
348
416
// Expect position to be minted correctly
349
417
planner . addAction ( Actions . MINT_POSITION , [
350
418
pool_1_eth . poolKey ,
351
419
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
352
420
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
353
- 1 ,
354
- toHex ( amount0Max ) ,
355
- toHex ( amount1Max ) ,
421
+ toHex ( liquidityMax ) ,
422
+ toHex ( amount0 ) ,
423
+ toHex ( amount1 ) ,
356
424
recipient ,
357
425
EMPTY_BYTES ,
358
426
] )
@@ -365,6 +433,11 @@ describe('PositionManager', () => {
365
433
expect ( calldata ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
366
434
367
435
expect ( value ) . toEqual ( '0x00' )
436
+
437
+ // The adjusted amounts are less than or equal to the max amounts
438
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
439
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
440
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
368
441
} )
369
442
370
443
it ( 'succeeds for batchPermit' , ( ) => {
@@ -375,6 +448,9 @@ describe('PositionManager', () => {
375
448
liquidity : 1 ,
376
449
} )
377
450
451
+ // These are the max amounts that the user can possibly add to the position
452
+ const { amount0, amount1 } = position . mintAmounts
453
+
378
454
const batchPermit : BatchPermitOptions = {
379
455
owner : mockOwner ,
380
456
permitBatch : {
@@ -403,21 +479,30 @@ describe('PositionManager', () => {
403
479
)
404
480
405
481
const planner = new V4Planner ( )
406
- const { amount0 : amount0Max , amount1 : amount1Max } = position . mintAmountsWithSlippage ( slippageTolerance )
482
+ const {
483
+ amount0 : adjustedAmount0 ,
484
+ amount1 : adjustedAmount1 ,
485
+ liquidity : liquidityMax ,
486
+ } = position . maxAmountsAndLiquidityWithSlippage ( slippageTolerance )
407
487
408
488
planner . addAction ( Actions . MINT_POSITION , [
409
489
pool_0_1 . poolKey ,
410
490
- TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
411
491
TICK_SPACINGS [ FeeAmount . MEDIUM ] ,
412
- 1 ,
413
- toHex ( amount0Max ) ,
414
- toHex ( amount1Max ) ,
492
+ toHex ( liquidityMax ) ,
493
+ toHex ( amount0 ) ,
494
+ toHex ( amount1 ) ,
415
495
recipient ,
416
496
EMPTY_BYTES ,
417
497
] )
418
498
planner . addAction ( Actions . SETTLE_PAIR , [ toAddress ( pool_0_1 . currency0 ) , toAddress ( pool_0_1 . currency1 ) ] )
419
499
expect ( calldataList [ 1 ] ) . toEqual ( V4PositionManager . encodeModifyLiquidities ( planner . finalize ( ) , deadline ) )
420
500
expect ( value ) . toEqual ( '0x00' )
501
+
502
+ // The adjusted amounts are less than or equal to the max amounts
503
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount0 , amount0 ) ) . toBe ( true )
504
+ expect ( JSBI . lessThanOrEqual ( adjustedAmount1 , amount1 ) ) . toBe ( true )
505
+ expect ( JSBI . equal ( adjustedAmount0 , amount0 ) || JSBI . equal ( adjustedAmount1 , amount1 ) ) . toBe ( true )
421
506
} )
422
507
} )
423
508
0 commit comments