@@ -2,59 +2,55 @@ import { V4PositionManagerAbi } from "@/constants/abis/V4PositionMananger";
22import { V4StateViewAbi } from "@/constants/abis/V4StateView" ;
33import { getInstance } from "@/core/uniDevKitV4Factory" ;
44import { decodePositionInfo } from "@/helpers/positions" ;
5+ import type {
6+ GetPositionParams ,
7+ GetPositionResponse ,
8+ } from "@/types/utils/getPosition" ;
59import { getTokens } from "@/utils/getTokens" ;
6- import type { Token } from "@uniswap/sdk-core" ;
710import { Pool , Position as V4Position } from "@uniswap/v4-sdk" ;
811
9- interface PositionResult {
10- token0 : Token ;
11- token1 : Token ;
12- amounts : {
13- amount0 : string ;
14- amount1 : string ;
15- } ;
16- tickLower : number ;
17- tickUpper : number ;
18- liquidity : bigint ;
19- poolId : `0x${string } `;
20- }
21-
12+ /**
13+ * Retrieves a Uniswap V4 position instance for a given token ID.
14+ * @param params Position parameters including token ID
15+ * @param chainId Optional chain ID where the position exists
16+ * @returns Promise resolving to position data
17+ * @throws Error if SDK instance is not found or if position data is invalid
18+ */
2219export async function getPosition (
23- tokenId : string ,
20+ params : GetPositionParams ,
2421 chainId ?: number ,
25- ) : Promise < PositionResult | undefined > {
22+ ) : Promise < GetPositionResponse > {
2623 const sdk = getInstance ( chainId ) ;
27- if ( ! sdk ) throw new Error ( "SDK not initialized" ) ;
24+ if ( ! sdk ) {
25+ throw new Error ( "SDK not found. Please create an instance first." ) ;
26+ }
2827
2928 const client = sdk . getClient ( ) ;
3029 const positionManager = sdk . getContractAddress ( "positionManager" ) ;
3130 const stateView = sdk . getContractAddress ( "stateView" ) ;
3231
32+ // Fetch poolKey and raw position info
3333 const [ poolAndPositionInfo , liquidity ] = await client . multicall ( {
3434 allowFailure : false ,
3535 contracts : [
3636 {
3737 address : positionManager ,
3838 abi : V4PositionManagerAbi ,
3939 functionName : "getPoolAndPositionInfo" ,
40- args : [ BigInt ( tokenId ) ] ,
40+ args : [ BigInt ( params . tokenId ) ] ,
4141 } ,
4242 {
4343 address : positionManager ,
4444 abi : V4PositionManagerAbi ,
4545 functionName : "getPositionLiquidity" ,
46- args : [ BigInt ( tokenId ) ] ,
46+ args : [ BigInt ( params . tokenId ) ] ,
4747 } ,
4848 ] ,
4949 } ) ;
5050
5151 const [ poolKey , rawInfo ] = poolAndPositionInfo ;
52-
5352 const { currency0, currency1, fee, tickSpacing, hooks } = poolKey ;
5453
55- console . log ( "poolKey" , poolKey ) ;
56- console . log ( "rawInfo" , rawInfo ) ;
57-
5854 if ( liquidity === 0n ) {
5955 throw new Error ( "Liquidity is 0" ) ;
6056 }
@@ -119,15 +115,11 @@ export async function getPosition(
119115 } ) ;
120116
121117 return {
118+ position,
119+ pool,
122120 token0,
123121 token1,
124- amounts : {
125- amount0 : position . amount0 . toSignificant ( 6 ) ,
126- amount1 : position . amount1 . toSignificant ( 6 ) ,
127- } ,
128- tickLower,
129- tickUpper,
130- liquidity,
131122 poolId,
123+ tokenId : params . tokenId ,
132124 } ;
133125}
0 commit comments