1- import { Percent } from '@uniswap/sdk-core'
2- import { type Position , V4PositionManager } from '@uniswap/v4-sdk'
1+ import { DEFAULT_SLIPPAGE_TOLERANCE } from '@/constants/common'
2+ import { percentFromBips } from '@/helpers/percent'
3+ import type { UniDevKitV4Instance } from '@/types'
4+ import { getDefaultDeadline } from '@/utils/getDefaultDeadline'
5+ import { getPosition } from '@/utils/getPosition'
6+ import { V4PositionManager } from '@uniswap/v4-sdk'
37
48/**
59 * Parameters required to build the calldata for removing liquidity from a Uniswap v4 position.
610 */
711export interface BuildRemoveLiquidityCallDataParams {
8- /**
9- * The position object representing the liquidity position to modify.
10- */
11- position : Position
12-
1312 /**
1413 * The percentage of liquidity to remove from the position.
1514 */
1615 liquidityPercentage : number
1716
1817 /**
19- * The deadline for the transaction .
18+ * The tokenId of the position to remove liquidity from .
2019 */
21- deadline : string
20+ tokenId : string
2221
2322 /**
2423 * The slippage tolerance for the transaction.
2524 */
26- slippageTolerance : number
25+ slippageTolerance ? : number
2726
2827 /**
29- * The tokenId of the position to remove liquidity from.
28+ * The deadline for the transaction. (default: 5 minutes from now)
3029 */
31- tokenId : string
30+ deadline ? : string
3231}
3332
3433/**
@@ -41,7 +40,7 @@ export interface BuildRemoveLiquidityCallDataParams {
4140 * ```typescript
4241 * const { calldata, value } = buildRemoveLiquidityCallData({
4342 * position,
44- * liquidityPercentage: new Percent(1, 1) , // 100%
43+ * liquidityPercentage: 10_000 , // 100%
4544 * });
4645 *
4746 * const tx = await sendTransaction({
@@ -51,18 +50,29 @@ export interface BuildRemoveLiquidityCallDataParams {
5150 * });
5251 * ```
5352 */
54- export function buildRemoveLiquidityCallData ( {
55- position,
56- liquidityPercentage,
57- deadline,
58- slippageTolerance,
59- tokenId,
60- } : BuildRemoveLiquidityCallDataParams ) {
53+ export async function buildRemoveLiquidityCallData (
54+ {
55+ liquidityPercentage,
56+ deadline : deadlineParam ,
57+ slippageTolerance,
58+ tokenId,
59+ } : BuildRemoveLiquidityCallDataParams ,
60+ instance : UniDevKitV4Instance ,
61+ ) {
62+ // Get position data
63+ const positionData = await getPosition ( { tokenId } , instance )
64+ if ( ! positionData ) {
65+ throw new Error ( 'Position not found' )
66+ }
67+
68+ const deadline = deadlineParam ?? ( await getDefaultDeadline ( instance ) ) . toString ( )
69+
70+ // Build remove liquidity call data
6171 try {
62- const { calldata, value } = V4PositionManager . removeCallParameters ( position , {
63- slippageTolerance : new Percent ( slippageTolerance , 100 ) ,
72+ const { calldata, value } = V4PositionManager . removeCallParameters ( positionData . position , {
73+ slippageTolerance : percentFromBips ( slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE ) ,
6474 deadline : deadline ,
65- liquidityPercentage : new Percent ( liquidityPercentage , 100 ) ,
75+ liquidityPercentage : percentFromBips ( liquidityPercentage ) ,
6676 tokenId : tokenId ,
6777 } )
6878
0 commit comments