Skip to content

Commit 136cc03

Browse files
authored
fix(v4-sdk): Revert v4 sdk change (#359)
1 parent f5337ac commit 136cc03

File tree

4 files changed

+60
-316
lines changed

4 files changed

+60
-316
lines changed

sdks/v4-sdk/src/PositionManager.test.ts

Lines changed: 29 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { PoolKey } from './entities/pool'
2121
import { toAddress } from './utils/currencyMap'
2222
import { MSG_SENDER } from './actionConstants'
2323
import { V4PositionPlanner } from './utils'
24-
import JSBI from 'jsbi'
2524

2625
describe('PositionManager', () => {
2726
const currency0 = new Token(1, '0x0000000000000000000000000000000000000001', 18, 't0', 'currency0')
@@ -158,10 +157,6 @@ describe('PositionManager', () => {
158157
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
159158
liquidity: 5000000,
160159
})
161-
162-
// These are the max amounts that the user can possibly add to the position
163-
const { amount0, amount1 } = position.mintAmounts
164-
165160
const { calldata, value } = V4PositionManager.addCallParameters(position, {
166161
recipient,
167162
slippageTolerance,
@@ -170,18 +165,14 @@ describe('PositionManager', () => {
170165

171166
// Rebuild the calldata with the planner for the expected mint.
172167
// Note that this test verifies that the applied logic in addCallParameters is correct but does not necessarily test the validity of the calldata itself.
173-
const {
174-
amount0: adjustedAmount0,
175-
amount1: adjustedAmount1,
176-
liquidity: liquidityMax,
177-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
168+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
178169
planner.addAction(Actions.MINT_POSITION, [
179170
pool_0_1.poolKey,
180171
-TICK_SPACINGS[FeeAmount.MEDIUM],
181172
TICK_SPACINGS[FeeAmount.MEDIUM],
182-
toHex(liquidityMax),
183-
toHex(amount0),
184-
toHex(amount1),
173+
5000000,
174+
toHex(amount0Max),
175+
toHex(amount1Max),
185176
recipient,
186177
EMPTY_BYTES,
187178
])
@@ -190,11 +181,6 @@ describe('PositionManager', () => {
190181

191182
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
192183
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)
198184
})
199185

200186
it('succeeds for increase', () => {
@@ -205,9 +191,6 @@ describe('PositionManager', () => {
205191
liquidity: 666,
206192
})
207193

208-
// These are the max amounts that the user can possibly add to the position
209-
const { amount0, amount1 } = position.mintAmounts
210-
211194
const { calldata, value } = V4PositionManager.addCallParameters(position, {
212195
tokenId,
213196
slippageTolerance,
@@ -216,27 +199,18 @@ describe('PositionManager', () => {
216199

217200
// Rebuild the calldata with the planner for increase
218201
const planner = new V4Planner()
219-
const {
220-
amount0: adjustedAmount0,
221-
amount1: adjustedAmount1,
222-
liquidity: liquidityMax,
223-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
202+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
224203
planner.addAction(Actions.INCREASE_LIQUIDITY, [
225204
tokenId.toString(),
226-
toHex(liquidityMax),
227-
toHex(amount0),
228-
toHex(amount1),
205+
666,
206+
toHex(amount0Max),
207+
toHex(amount1Max),
229208
EMPTY_BYTES,
230209
])
231210
// Expect there to be a settle pair call afterwards
232211
planner.addAction(Actions.SETTLE_PAIR, [toAddress(pool_0_1.currency0), toAddress(pool_0_1.currency1)])
233212
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
234213
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)
240214
})
241215

242216
it('succeeds when createPool is true', () => {
@@ -246,9 +220,6 @@ describe('PositionManager', () => {
246220
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
247221
liquidity: 90000000000000,
248222
})
249-
250-
// These are the max amounts that the user can possibly add to the position
251-
const { amount0, amount1 } = position.mintAmounts
252223
const { calldata, value } = V4PositionManager.addCallParameters(position, {
253224
recipient,
254225
slippageTolerance,
@@ -264,30 +235,21 @@ describe('PositionManager', () => {
264235
V4PositionManager.INTERFACE.encodeFunctionData('initializePool', [pool_0_1.poolKey, SQRT_PRICE_1_1.toString()])
265236
)
266237
const planner = new V4Planner()
267-
const {
268-
amount0: adjustedAmount0,
269-
amount1: adjustedAmount1,
270-
liquidity: liquidityMax,
271-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
238+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
272239
// Expect position to be minted correctly
273240
planner.addAction(Actions.MINT_POSITION, [
274241
pool_0_1.poolKey,
275242
-TICK_SPACINGS[FeeAmount.MEDIUM],
276243
TICK_SPACINGS[FeeAmount.MEDIUM],
277-
toHex(liquidityMax),
278-
toHex(amount0),
279-
toHex(amount1),
244+
90000000000000,
245+
toHex(amount0Max),
246+
toHex(amount1Max),
280247
recipient,
281248
EMPTY_BYTES,
282249
])
283250
planner.addAction(Actions.SETTLE_PAIR, [toAddress(pool_0_1.currency0), toAddress(pool_0_1.currency1)])
284251
expect(calldataList[1]).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
285252
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)
291253
})
292254

293255
it('succeeds when useNative is set', () => {
@@ -297,9 +259,6 @@ describe('PositionManager', () => {
297259
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
298260
liquidity: 1,
299261
})
300-
301-
// These are the max amounts that the user can possibly add to the position
302-
const { amount0, amount1 } = position.mintAmounts
303262
const { calldata, value } = V4PositionManager.addCallParameters(position, {
304263
recipient,
305264
slippageTolerance,
@@ -310,32 +269,24 @@ describe('PositionManager', () => {
310269
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
311270

312271
const planner = new V4Planner()
313-
const {
314-
amount0: adjustedAmount0,
315-
amount1: adjustedAmount1,
316-
liquidity: liquidityMax,
317-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
272+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
318273
// Expect position to be minted correctly
319274
planner.addAction(Actions.MINT_POSITION, [
320275
pool_1_eth.poolKey,
321276
-TICK_SPACINGS[FeeAmount.MEDIUM],
322277
TICK_SPACINGS[FeeAmount.MEDIUM],
323-
toHex(liquidityMax),
324-
toHex(amount0),
325-
toHex(amount1),
278+
1,
279+
toHex(amount0Max),
280+
toHex(amount1Max),
326281
recipient,
327282
EMPTY_BYTES,
328283
])
329284

330285
planner.addAction(Actions.SETTLE_PAIR, [toAddress(pool_1_eth.currency0), toAddress(pool_1_eth.currency1)])
331286
planner.addAction(Actions.SWEEP, [toAddress(pool_1_eth.currency0), MSG_SENDER])
332287
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
333-
expect(value).toEqual(toHex(amount0))
334288

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)
289+
expect(value).toEqual(toHex(amount0Max))
339290
})
340291

341292
it('succeeds when migrate is true', () => {
@@ -345,9 +296,6 @@ describe('PositionManager', () => {
345296
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
346297
liquidity: 1,
347298
})
348-
349-
// These are the max amounts that the user can possibly add to the position
350-
const { amount0, amount1 } = position.mintAmounts
351299
const { calldata, value } = V4PositionManager.addCallParameters(position, {
352300
recipient,
353301
slippageTolerance,
@@ -357,19 +305,15 @@ describe('PositionManager', () => {
357305

358306
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
359307
const planner = new V4Planner()
360-
const {
361-
amount0: adjustedAmount0,
362-
amount1: adjustedAmount1,
363-
liquidity: liquidityMax,
364-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
308+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
365309
// Expect position to be minted correctly
366310
planner.addAction(Actions.MINT_POSITION, [
367311
pool_0_1.poolKey,
368312
-TICK_SPACINGS[FeeAmount.MEDIUM],
369313
TICK_SPACINGS[FeeAmount.MEDIUM],
370-
toHex(liquidityMax),
371-
toHex(amount0),
372-
toHex(amount1),
314+
1,
315+
toHex(amount0Max),
316+
toHex(amount1Max),
373317
recipient,
374318
EMPTY_BYTES,
375319
])
@@ -381,11 +325,6 @@ describe('PositionManager', () => {
381325
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
382326

383327
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)
389328
})
390329

391330
it('succeeds when migrating to an eth position', () => {
@@ -395,9 +334,6 @@ describe('PositionManager', () => {
395334
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
396335
liquidity: 1,
397336
})
398-
399-
// These are the max amounts that the user can possibly add to the position
400-
const { amount0, amount1 } = position.mintAmounts
401337
const { calldata, value } = V4PositionManager.addCallParameters(position, {
402338
recipient,
403339
slippageTolerance,
@@ -408,19 +344,15 @@ describe('PositionManager', () => {
408344

409345
// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
410346
const planner = new V4Planner()
411-
const {
412-
amount0: adjustedAmount0,
413-
amount1: adjustedAmount1,
414-
liquidity: liquidityMax,
415-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
347+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
416348
// Expect position to be minted correctly
417349
planner.addAction(Actions.MINT_POSITION, [
418350
pool_1_eth.poolKey,
419351
-TICK_SPACINGS[FeeAmount.MEDIUM],
420352
TICK_SPACINGS[FeeAmount.MEDIUM],
421-
toHex(liquidityMax),
422-
toHex(amount0),
423-
toHex(amount1),
353+
1,
354+
toHex(amount0Max),
355+
toHex(amount1Max),
424356
recipient,
425357
EMPTY_BYTES,
426358
])
@@ -433,11 +365,6 @@ describe('PositionManager', () => {
433365
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
434366

435367
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)
441368
})
442369

443370
it('succeeds for batchPermit', () => {
@@ -448,9 +375,6 @@ describe('PositionManager', () => {
448375
liquidity: 1,
449376
})
450377

451-
// These are the max amounts that the user can possibly add to the position
452-
const { amount0, amount1 } = position.mintAmounts
453-
454378
const batchPermit: BatchPermitOptions = {
455379
owner: mockOwner,
456380
permitBatch: {
@@ -479,30 +403,21 @@ describe('PositionManager', () => {
479403
)
480404

481405
const planner = new V4Planner()
482-
const {
483-
amount0: adjustedAmount0,
484-
amount1: adjustedAmount1,
485-
liquidity: liquidityMax,
486-
} = position.maxAmountsAndLiquidityWithSlippage(slippageTolerance)
406+
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
487407

488408
planner.addAction(Actions.MINT_POSITION, [
489409
pool_0_1.poolKey,
490410
-TICK_SPACINGS[FeeAmount.MEDIUM],
491411
TICK_SPACINGS[FeeAmount.MEDIUM],
492-
toHex(liquidityMax),
493-
toHex(amount0),
494-
toHex(amount1),
412+
1,
413+
toHex(amount0Max),
414+
toHex(amount1Max),
495415
recipient,
496416
EMPTY_BYTES,
497417
])
498418
planner.addAction(Actions.SETTLE_PAIR, [toAddress(pool_0_1.currency0), toAddress(pool_0_1.currency1)])
499419
expect(calldataList[1]).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))
500420
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)
506421
})
507422
})
508423

sdks/v4-sdk/src/PositionManager.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,9 @@ export abstract class V4PositionManager {
249249
)
250250

251251
// adjust for slippage
252-
const {
253-
amount0: amount0Max,
254-
amount1: amount1Max,
255-
liquidity: liquidityMax,
256-
} = position.maxAmountsAndLiquidityWithSlippage(options.slippageTolerance)
252+
const maximumAmounts = position.mintAmountsWithSlippage(options.slippageTolerance)
253+
const amount0Max = toHex(maximumAmounts.amount0)
254+
const amount1Max = toHex(maximumAmounts.amount1)
257255

258256
// We use permit2 to approve tokens to the position manager
259257
if (options.batchPermit) {
@@ -273,15 +271,15 @@ export abstract class V4PositionManager {
273271
position.pool,
274272
position.tickLower,
275273
position.tickUpper,
276-
liquidityMax,
274+
position.liquidity,
277275
amount0Max,
278276
amount1Max,
279277
recipient,
280278
options.hookData
281279
)
282280
} else {
283281
// increase
284-
planner.addIncrease(options.tokenId, liquidityMax, amount0Max, amount1Max, options.hookData)
282+
planner.addIncrease(options.tokenId, position.liquidity, amount0Max, amount1Max, options.hookData)
285283
}
286284

287285
let value: string = toHex(0)

0 commit comments

Comments
 (0)