Skip to content

feat(v4-sdk): additional options when migrating #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions sdks/v4-sdk/src/PositionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ describe('PositionManager', () => {
recipient,
slippageTolerance,
deadline,
migrate: true,
additionalAmount0: 0,
additionalAmount1: 0,
})

// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
Expand Down Expand Up @@ -338,7 +339,8 @@ describe('PositionManager', () => {
recipient,
slippageTolerance,
deadline,
migrate: true,
additionalAmount0: 0,
additionalAmount1: 0,
useNative: currency_native,
})

Expand Down Expand Up @@ -367,6 +369,85 @@ describe('PositionManager', () => {
expect(value).toEqual('0x00')
})

it('succeeds when migrating out of range and need to transfer eth', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love a test for non-eth too, and for ETH-WETH wrapping combos if they are to be supported too

const position: Position = new Position({
pool: pool_1_eth,
tickLower: TICK_SPACINGS[FeeAmount.MEDIUM],
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM] * 2,
liquidity: 1,
})
const { calldata, value } = V4PositionManager.addCallParameters(position, {
recipient,
slippageTolerance,
deadline,
additionalAmount0: 1,
additionalAmount1: 0,
useNative: currency_native,
})

// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
const planner = new V4Planner()
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
// Expect position to be minted correctly
planner.addAction(Actions.MINT_POSITION, [
pool_1_eth.poolKey,
TICK_SPACINGS[FeeAmount.MEDIUM],
TICK_SPACINGS[FeeAmount.MEDIUM] * 2,
1,
toHex(amount0Max),
toHex(amount1Max),
recipient,
EMPTY_BYTES,
])

planner.addAction(Actions.SETTLE, [toAddress(pool_1_eth.currency0), OPEN_DELTA, false])
planner.addAction(Actions.SETTLE, [toAddress(pool_1_eth.currency1), OPEN_DELTA, false])
planner.addAction(Actions.SWEEP, [toAddress(pool_1_eth.currency0), recipient])
planner.addAction(Actions.SWEEP, [toAddress(pool_1_eth.currency1), recipient])
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))

expect(value).toEqual('0x00')
})

it('succeeds when migrating out of range and need to transfer extra currency', () => {
const position: Position = new Position({
pool: pool_0_1,
tickLower: TICK_SPACINGS[FeeAmount.MEDIUM],
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM] * 2,
liquidity: 1,
})
const { calldata, value } = V4PositionManager.addCallParameters(position, {
recipient,
slippageTolerance,
deadline,
additionalAmount0: 1,
additionalAmount1: 0,
})

// Rebuild the data with the planner for the expected mint. MUST sweep since we are using the native currency.
const planner = new V4Planner()
const { amount0: amount0Max, amount1: amount1Max } = position.mintAmountsWithSlippage(slippageTolerance)
// Expect position to be minted correctly
planner.addAction(Actions.MINT_POSITION, [
pool_0_1.poolKey,
TICK_SPACINGS[FeeAmount.MEDIUM],
TICK_SPACINGS[FeeAmount.MEDIUM] * 2,
1,
toHex(amount0Max),
toHex(amount1Max),
recipient,
EMPTY_BYTES,
])

planner.addAction(Actions.SETTLE, [toAddress(pool_0_1.currency0), OPEN_DELTA, false])
planner.addAction(Actions.SETTLE, [toAddress(pool_0_1.currency1), OPEN_DELTA, false])
planner.addAction(Actions.SWEEP, [toAddress(pool_0_1.currency0), recipient])
planner.addAction(Actions.SWEEP, [toAddress(pool_0_1.currency1), recipient])
expect(calldata).toEqual(V4PositionManager.encodeModifyLiquidities(planner.finalize(), deadline))

expect(value).toEqual('0x00')
})

it('succeeds for batchPermit', () => {
const position: Position = new Position({
pool: pool_0_1,
Expand Down
34 changes: 15 additions & 19 deletions sdks/v4-sdk/src/PositionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export interface MintSpecificOptions {
* Initial price to set on the pool if creating
*/
sqrtPriceX96?: BigintIsh

/**
* Whether the mint is part of a migration from V3 to V4.
*/
migrate?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we are pulling out migrate into its own object; might need to change trading api impl

}

/**
Expand Down Expand Up @@ -114,21 +109,15 @@ export interface CollectSpecificOptions {
recipient: string
}

export interface TransferOptions {
export interface MigrateSpecificOptions {
/**
* The account sending the NFT.
* The additional amount of currency0 that needs to be transferred if needed
*/
sender: string

additionalAmount0: BigintIsh
/**
* The account that should receive the NFT.
* The additional amount of currency1 that needs to be transferred if needed
*/
recipient: string

/**
* The id of the token being sent.
*/
tokenId: BigintIsh
additionalAmount1: BigintIsh
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not too bad it seems on the Trading API side to send in; curious how to derive the amount, seems it wouldnt bee to tedious to compute from earlier convos


export interface PermitDetails {
Expand Down Expand Up @@ -183,9 +172,10 @@ export interface NFTPermitData {
}

export type MintOptions = CommonOptions & CommonAddLiquidityOptions & MintSpecificOptions
export type MigrateOptions = CommonOptions & CommonAddLiquidityOptions & MintSpecificOptions & MigrateSpecificOptions
export type IncreaseLiquidityOptions = CommonOptions & CommonAddLiquidityOptions & ModifyPositionSpecificOptions

export type AddLiquidityOptions = MintOptions | IncreaseLiquidityOptions
export type AddLiquidityOptions = MintOptions | IncreaseLiquidityOptions | MigrateOptions

export type RemoveLiquidityOptions = CommonOptions & RemoveLiquiditySpecificOptions & ModifyPositionSpecificOptions

Expand All @@ -196,6 +186,10 @@ function isMint(options: AddLiquidityOptions): options is MintOptions {
return Object.keys(options).some((k) => k === 'recipient')
}

function isMigrate(options: AddLiquidityOptions): options is MigrateOptions {
return Object.keys(options).some((k) => k === 'additionalAmount0')
}

function shouldCreatePool(options: MintOptions): boolean {
if (options.createPool) {
invariant(options.sqrtPriceX96 !== undefined, NO_SQRT_PRICE)
Expand Down Expand Up @@ -284,9 +278,11 @@ export abstract class V4PositionManager {

let value: string = toHex(0)

let needToSendEth = isMigrate(options) && options.useNative && options.additionalAmount0 > '0'

// If migrating, we need to settle and sweep both currencies individually
if (isMint(options) && options.migrate) {
if (options.useNative) {
if (isMigrate(options)) {
if (options.useNative && !needToSendEth) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the case where neededCurrency is ETH and the position is in WETH this path should still be taken i think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the opposite right? it would need to wrap not unwrap

// unwrap the exact amount needed to send to the pool manager
planner.addUnwrap(OPEN_DELTA)
// payer is v4 position manager
Expand Down
Loading