Skip to content

Commit ddc8881

Browse files
committed
feat: removeLiquidity & CollectFees
1 parent f09a982 commit ddc8881

File tree

6 files changed

+128
-30
lines changed

6 files changed

+128
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uniswap-dev-kit",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "A modern TypeScript library for integrating Uniswap into your dapp.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/uniDevKitV4.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {
55
BuildAddLiquidityCallDataResult,
66
BuildAddLiquidityParams,
77
} from '@/types/utils/buildAddLiquidityCallData'
8+
import type { BuildCollectFeesCallDataParams } from '@/types/utils/buildCollectFeesCallData'
9+
import type { BuildRemoveLiquidityCallDataParams } from '@/types/utils/buildRemoveLiquidityCallData'
810
import type { PoolParams } from '@/types/utils/getPool'
911
import type { GetPoolKeyFromPoolIdParams } from '@/types/utils/getPoolKeyFromPoolId'
1012
import type { GetPositionParams, GetPositionResponse } from '@/types/utils/getPosition'
@@ -17,10 +19,8 @@ import type {
1719
PreparePermit2DataResult,
1820
} from '@/types/utils/permit2'
1921
import { buildAddLiquidityCallData } from '@/utils/buildAddLiquidityCallData'
20-
import {
21-
type BuildRemoveLiquidityCallDataParams,
22-
buildRemoveLiquidityCallData,
23-
} from '@/utils/buildRemoveLiquidityCallData'
22+
import { buildCollectFeesCallData } from '@/utils/buildCollectFeesCallData'
23+
import { buildRemoveLiquidityCallData } from '@/utils/buildRemoveLiquidityCallData'
2424
import { buildSwapCallData } from '@/utils/buildSwapCallData'
2525
import { getPool } from '@/utils/getPool'
2626
import { getPoolKeyFromPoolId } from '@/utils/getPoolKeyFromPoolId'
@@ -248,4 +248,14 @@ export class UniDevKitV4 {
248248
async buildRemoveLiquidityCallData(params: BuildRemoveLiquidityCallDataParams) {
249249
return buildRemoveLiquidityCallData(params, this.instance)
250250
}
251+
252+
/**
253+
* Builds a collect fees call data for a given collect fees parameters.
254+
* @param params @type {BuildCollectFeesCallDataParams}
255+
* @returns Promise resolving to collect fees call data including calldata and value
256+
* @throws Error if SDK instance is not found or if collect fees call data is invalid
257+
*/
258+
async buildCollectFeesCallData(params: BuildCollectFeesCallDataParams) {
259+
return buildCollectFeesCallData(params, this.instance)
260+
}
251261
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Parameters required to build the calldata for collecting fees from a Uniswap v4 position.
3+
*/
4+
export interface BuildCollectFeesCallDataParams {
5+
/**
6+
* The tokenId of the position to collect fees from.
7+
*/
8+
tokenId: string
9+
10+
/**
11+
* The recipient address for collected fees.
12+
*/
13+
recipient: string
14+
15+
/**
16+
* Optional deadline for the transaction (default: 5 minutes from now).
17+
*/
18+
deadline?: string
19+
20+
/**
21+
* The slippage tolerance for the transaction.
22+
*/
23+
slippageTolerance?: number
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Parameters required to build the calldata for removing liquidity from a Uniswap v4 position.
3+
*/
4+
export interface BuildRemoveLiquidityCallDataParams {
5+
/**
6+
* The percentage of liquidity to remove from the position.
7+
*/
8+
liquidityPercentage: number
9+
10+
/**
11+
* The tokenId of the position to remove liquidity from.
12+
*/
13+
tokenId: string
14+
15+
/**
16+
* The slippage tolerance for the transaction.
17+
*/
18+
slippageTolerance?: number
19+
20+
/**
21+
* The deadline for the transaction. (default: 5 minutes from now)
22+
*/
23+
deadline?: string
24+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { DEFAULT_SLIPPAGE_TOLERANCE } from '@/constants/common'
2+
import { percentFromBips } from '@/helpers/percent'
3+
import type { UniDevKitV4Instance } from '@/types'
4+
import type { BuildCollectFeesCallDataParams } from '@/types/utils/buildCollectFeesCallData'
5+
import { getDefaultDeadline } from '@/utils/getDefaultDeadline'
6+
import { getPosition } from '@/utils/getPosition'
7+
import { V4PositionManager } from '@uniswap/v4-sdk'
8+
9+
/**
10+
* Builds the calldata and value required to collect fees from a Uniswap v4 position.
11+
*
12+
* @param params - The parameters for collecting fees.
13+
* @param instance - The UniDevKit instance for accessing pool state.
14+
* @returns An object containing the calldata and the value to send with the transaction.
15+
*
16+
* @example
17+
* ```ts
18+
* const { calldata, value } = await buildCollectFeesCallData({
19+
* tokenId: '1234',
20+
* recipient: userAddress,
21+
* slippageTolerance: 50, // 0.5%
22+
* deadline: '1234567890',
23+
* }, instance)
24+
*
25+
* const tx = await sendTransaction({
26+
* to: PositionManager.address,
27+
* data: calldata,
28+
* value,
29+
* })
30+
* ```
31+
*/
32+
export async function buildCollectFeesCallData(
33+
{
34+
tokenId,
35+
recipient,
36+
deadline: deadlineParam,
37+
slippageTolerance,
38+
}: BuildCollectFeesCallDataParams,
39+
instance: UniDevKitV4Instance,
40+
) {
41+
const positionData = await getPosition({ tokenId }, instance)
42+
if (!positionData) {
43+
throw new Error('Position not found')
44+
}
45+
46+
const deadline = deadlineParam ?? (await getDefaultDeadline(instance)).toString()
47+
48+
try {
49+
const { calldata, value } = V4PositionManager.collectCallParameters(positionData.position, {
50+
recipient,
51+
deadline,
52+
tokenId,
53+
slippageTolerance: percentFromBips(slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE),
54+
})
55+
56+
return {
57+
calldata,
58+
value,
59+
}
60+
} catch (error) {
61+
console.error(error)
62+
throw error
63+
}
64+
}

src/utils/buildRemoveLiquidityCallData.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
import { DEFAULT_SLIPPAGE_TOLERANCE } from '@/constants/common'
22
import { percentFromBips } from '@/helpers/percent'
33
import type { UniDevKitV4Instance } from '@/types'
4+
import type { BuildRemoveLiquidityCallDataParams } from '@/types/utils/buildRemoveLiquidityCallData'
45
import { getDefaultDeadline } from '@/utils/getDefaultDeadline'
56
import { getPosition } from '@/utils/getPosition'
67
import { V4PositionManager } from '@uniswap/v4-sdk'
78

8-
/**
9-
* Parameters required to build the calldata for removing liquidity from a Uniswap v4 position.
10-
*/
11-
export interface BuildRemoveLiquidityCallDataParams {
12-
/**
13-
* The percentage of liquidity to remove from the position.
14-
*/
15-
liquidityPercentage: number
16-
17-
/**
18-
* The tokenId of the position to remove liquidity from.
19-
*/
20-
tokenId: string
21-
22-
/**
23-
* The slippage tolerance for the transaction.
24-
*/
25-
slippageTolerance?: number
26-
27-
/**
28-
* The deadline for the transaction. (default: 5 minutes from now)
29-
*/
30-
deadline?: string
31-
}
32-
339
/**
3410
* Builds the calldata and value required to remove liquidity from a Uniswap v4 position.
3511
*

0 commit comments

Comments
 (0)