11import { BigNumber } from '@ethersproject/bignumber'
22import { ProtocolVersion } from '@uniswap/client-pools/dist/pools/v1/types_pb'
3- import { CurrencyAmount , Token } from '@uniswap/sdk-core'
3+ import { Currency , CurrencyAmount , Token } from '@uniswap/sdk-core'
44import { FeeAmount , Pool as PoolV3 , TICK_SPACINGS , TickMath as TickMathV3 , tickToPrice } from '@uniswap/v3-sdk'
5- import { Pool as PoolV4 } from '@uniswap/v4-sdk'
5+ import { Pool as PoolV4 , tickToPrice as tickToPriceV4 } from '@uniswap/v4-sdk'
66import { ChartHoverData , ChartModel , ChartModelParams } from 'components/Charts/ChartModel'
77import { LiquidityBarSeries } from 'components/Charts/LiquidityChart/liquidity-bar-series'
88import {
@@ -123,6 +123,7 @@ function maxAmount(token: Token) {
123123}
124124
125125/** Calculates tokens locked in the active tick range based on the current tick */
126+ // TODO(WEB-7564): determine how to support v4
126127async function calculateActiveRangeTokensLocked (
127128 token0 : Token ,
128129 token1 : Token ,
@@ -224,9 +225,10 @@ export async function calculateTokensLockedV3(
224225 }
225226}
226227
228+ // TODO(WEB-7564): determine if tick math needs to be converted to support v4
227229export async function calculateTokensLockedV4 (
228- token0 : Token ,
229- token1 : Token ,
230+ token0 : Currency ,
231+ token1 : Currency ,
230232 feeTier : FeeAmount ,
231233 tickSpacing : number ,
232234 hooks : string ,
@@ -278,8 +280,8 @@ export async function calculateTokensLockedV4(
278280}
279281
280282export function useLiquidityBarData ( {
281- tokenA ,
282- tokenB ,
283+ currencyA ,
284+ currencyB ,
283285 feeTier,
284286 isReversed,
285287 chainId,
@@ -288,8 +290,8 @@ export function useLiquidityBarData({
288290 hooks,
289291 poolId,
290292} : {
291- tokenA : Token
292- tokenB : Token
293+ currencyA : Currency
294+ currencyB : Currency
293295 feeTier : FeeAmount
294296 isReversed : boolean
295297 chainId : UniverseChainId
@@ -299,9 +301,15 @@ export function useLiquidityBarData({
299301 poolId ?: string
300302} ) {
301303 const { formatNumber, formatPrice } = useFormatter ( )
304+
305+ // Determine the correct tokens to use based on the protocol version
306+ // V3 requires tokens, V4 can handle native or tokens
307+ const tokenAWrapped = currencyA . wrapped
308+ const tokenBWrapped = currencyB . wrapped
309+
302310 const activePoolData = usePoolActiveLiquidity ( {
303- currencyA : tokenA ,
304- currencyB : tokenB ,
311+ currencyA,
312+ currencyB,
305313 feeAmount : feeTier ,
306314 version,
307315 poolId,
@@ -341,15 +349,18 @@ export function useLiquidityBarData({
341349 activeRangeIndex = index
342350 activeRangePercentage = ( activePoolData . currentTick - t . tick ) / TICK_SPACINGS [ feeTier ]
343351
344- price0 = tickToPrice ( tokenA , tokenB , t . tick )
352+ price0 =
353+ version === ProtocolVersion . V3
354+ ? tickToPrice ( tokenAWrapped , tokenBWrapped , t . tick )
355+ : tickToPriceV4 ( currencyA , currencyB , t . tick )
345356 price1 = price0 . invert ( )
346357 }
347358
348359 const { amount0Locked, amount1Locked } = await ( version === ProtocolVersion . V3
349- ? calculateTokensLockedV3 ( tokenA , tokenB , feeTier , t )
360+ ? calculateTokensLockedV3 ( tokenAWrapped , tokenBWrapped , feeTier , t )
350361 : calculateTokensLockedV4 (
351- tokenA ,
352- tokenB ,
362+ currencyA ,
363+ currencyB ,
353364 feeTier ,
354365 tickSpacing ?? TICK_SPACINGS [ feeTier ] ,
355366 hooks ?? ZERO_ADDRESS ,
@@ -379,8 +390,8 @@ export function useLiquidityBarData({
379390 // For active range, adjust amounts locked to adjust for where current tick/price is within the range
380391 if ( activeRangeIndex !== undefined && activeRangeData ) {
381392 const activeTickTvl = await calculateActiveRangeTokensLocked (
382- tokenA ,
383- tokenB ,
393+ tokenAWrapped ,
394+ tokenBWrapped ,
384395 feeTier ,
385396 ticksProcessed [ activeRangeIndex ] ,
386397 activePoolData ,
@@ -398,7 +409,20 @@ export function useLiquidityBarData({
398409 }
399410
400411 formatData ( )
401- } , [ activePoolData , tokenA , tokenB , formatNumber , formatPrice , isReversed , feeTier , version , tickSpacing , hooks ] )
412+ } , [
413+ activePoolData ,
414+ currencyA ,
415+ currencyB ,
416+ tokenAWrapped ,
417+ tokenBWrapped ,
418+ formatNumber ,
419+ formatPrice ,
420+ isReversed ,
421+ feeTier ,
422+ version ,
423+ tickSpacing ,
424+ hooks ,
425+ ] )
402426
403427 return { tickData, activeTick : activePoolData . activeTick , loading : activePoolData . isLoading || ! tickData }
404428}
0 commit comments