From 2fbebed014f3a762143142931c52f46856c2473f Mon Sep 17 00:00:00 2001 From: surbhi Date: Tue, 11 Nov 2025 18:28:32 -0400 Subject: [PATCH] adding token prices --- src/components/stake-form.tsx | 22 +++++++++++++--- src/hooks/usePartyPlanner.ts | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/components/stake-form.tsx b/src/components/stake-form.tsx index 0547aee..5d7b591 100644 --- a/src/components/stake-form.tsx +++ b/src/components/stake-form.tsx @@ -345,8 +345,15 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) { {pool.symbol} {pool.name} - {pool.price && ( - {pool.price} + {(pool.price || pool.tvl) && ( +
+ {pool.price && ( + {pool.price} + )} + {pool.tvl && ( + TVL: {pool.tvl} + )} +
)} @@ -400,8 +407,15 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) { {pool.symbol} {pool.name} - {pool.price && ( - {pool.price} + {(pool.price || pool.tvl) && ( +
+ {pool.price && ( + {pool.price} + )} + {pool.tvl && ( + TVL: {pool.tvl} + )} +
)} diff --git a/src/hooks/usePartyPlanner.ts b/src/hooks/usePartyPlanner.ts index 5d4d322..d570a2f 100644 --- a/src/hooks/usePartyPlanner.ts +++ b/src/hooks/usePartyPlanner.ts @@ -9,6 +9,19 @@ import IPartyPoolViewerABI from '@/contracts/IPartyPoolViewerABI'; import IPartyInfoABI from '@/contracts/IPartyInfoABI'; import { ERC20ABI } from '@/contracts/ERC20ABI'; +// Helper function to format large numbers with K, M, B suffixes +function formatTVL(value: number): string { + if (value >= 1_000_000_000) { + return `$${(value / 1_000_000_000).toFixed(2)}B`; + } else if (value >= 1_000_000) { + return `$${(value / 1_000_000).toFixed(2)}M`; + } else if (value >= 1_000) { + return `$${(value / 1_000).toFixed(2)}K`; + } else { + return `$${value.toFixed(2)}`; + } +} + export function useGetAllTokens(offset: number = 0, limit: number = 100) { const publicClient = usePublicClient(); const [mounted, setMounted] = useState(false); @@ -334,6 +347,7 @@ export interface PoolDetails { symbol: string; tokens: readonly `0x${string}`[]; price?: string; // Formatted price string + tvl?: string; // Formatted TVL string (e.g., "$1.2M") } export function useGetAllPools(offset: number = 0, limit: number = 100) { @@ -443,12 +457,45 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) { priceStr = undefined; } + // Calculate TVL (approximate by getting first token balance and doubling it) + let tvlStr: string | undefined; + try { + if (tokens && tokens.length > 0) { + const firstTokenAddress = tokens[0]; + + // Get token decimals and balance + const [decimals, balance] = await Promise.all([ + publicClient.readContract({ + address: firstTokenAddress as `0x${string}`, + abi: ERC20ABI, + functionName: 'decimals', + }) as Promise, + publicClient.readContract({ + address: firstTokenAddress as `0x${string}`, + abi: ERC20ABI, + functionName: 'balanceOf', + args: [poolAddress], + }) as Promise, + ]); + + // Convert balance to float and double it for total TVL approximation + const tokenBalance = Number(balance) / Math.pow(10, decimals); + const approximateTVL = tokenBalance * 3; + + tvlStr = formatTVL(approximateTVL); + } + } catch (err) { + console.error(`Error fetching TVL for ${poolAddress}:`, err); + tvlStr = undefined; + } + details.push({ address: poolAddress, name: name as string, symbol: symbol as string, tokens: tokens as readonly `0x${string}`[], price: priceStr, + tvl: tvlStr, }); } } catch (err) {