From 4523195b78e832bacd254210a300b0c917144d3b Mon Sep 17 00:00:00 2001 From: surbhi Date: Wed, 3 Dec 2025 13:33:42 -0400 Subject: [PATCH] price bug fix --- src/hooks/usePartyPlanner.ts | 76 ++++++++++++------------------------ 1 file changed, 26 insertions(+), 50 deletions(-) diff --git a/src/hooks/usePartyPlanner.ts b/src/hooks/usePartyPlanner.ts index 3070677..ad713a9 100644 --- a/src/hooks/usePartyPlanner.ts +++ b/src/hooks/usePartyPlanner.ts @@ -464,41 +464,12 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) { let tvlStr: string | undefined; if (isWorking) { - // Fetch pool price (use first token as quote, index 0) - try { - const priceRaw = await publicClient.readContract({ - address: partyInfoAddress as `0x${string}`, - abi: IPartyInfoABI, - functionName: 'poolPrice', - args: [poolAddress, BigInt(0)], - }); - - const price = BigInt(priceRaw as bigint | number); - - if (price === 0n) { - priceStr = undefined; - } else { - // Convert Q64 format to decimal (price = priceValue / 2^64) - const Q64 = 2n ** 64n; - const isNegative = price < 0n; - const absPrice = isNegative ? -price : price; - const priceFloat = Number(absPrice) / Number(Q64); - const finalPrice = isNegative ? -priceFloat : priceFloat; - - priceStr = `$${finalPrice.toFixed(4)}`; - } - } catch (err) { - console.error(`Error fetching pool price for ${poolAddress}:`, err); - priceStr = undefined; - } - - // Calculate TVL (approximate by getting first token balance and doubling it) + // Fetch token decimals and balance first (needed for both price and TVL) try { if (tokens && tokens.length > 0) { const firstTokenAddress = tokens[0]; - - // Get token decimals and balance - const [decimals, balance] = await Promise.all([ + // Get token decimals, balance, and pool price in parallel + const [decimals, balance, priceRaw] = await Promise.all([ publicClient.readContract({ address: firstTokenAddress as `0x${string}`, abi: ERC20ABI, @@ -510,16 +481,36 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) { functionName: 'balanceOf', args: [poolAddress], }) as Promise, + publicClient.readContract({ + address: partyInfoAddress as `0x${string}`, + abi: IPartyInfoABI, + functionName: 'poolPrice', + args: [poolAddress, BigInt(0)], + }), ]); - // Convert balance to float and double it for total TVL approximation + // Calculate pool price using actual token decimals + const price = BigInt(priceRaw as bigint | number); + if (price === 0n) { + priceStr = undefined; + } else { + // Convert Q64 format to decimal (price = priceValue / 2^64) + const Q64 = 2n ** 64n; + const priceFloat = Number(price) / Number(Q64); + + // Adjust for token decimals (poolPrice assumes 18 decimals, adjust based on actual token decimals) + const finalPrice = priceFloat * (Math.pow(10, 18) / Math.pow(10, decimals)); + priceStr = `$${finalPrice.toFixed(4)}`; + } + + // Calculate TVL (approximate by getting first token balance and multiplying by 3) 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); + console.error(`Error fetching pool price/TVL for ${poolAddress}:`, err); + priceStr = undefined; tvlStr = undefined; } } @@ -632,8 +623,6 @@ export function useSwapMintAmounts( let calculatedSlippage: number | undefined; try { - console.log('input token index', inputTokenIndex); - const poolPriceInt128 = await publicClient.readContract({ address: partyInfoAddress as `0x${string}`, abi: IPartyInfoABI, @@ -716,14 +705,12 @@ export function useBurnSwapAmounts( try { setLoading(true); setError(null); - console.log('fetching swap amounts') // Get chain ID and contract address const chainId = await publicClient.getChainId(); // @ts-ignore const partyInfoAddress = (chainInfo as Record)[chainId.toString()]?.v1?.PartyInfo; if (!partyInfoAddress) { - console.log('errores here') setError('PartyInfo contract not found for current chain'); setBurnSwapAmounts(null); return; @@ -762,17 +749,6 @@ export function useBurnSwapAmounts( const swapPrice = (outAmountDecimal + feeDecimal) / lpAmountDecimal; calculatedSlippage = ((poolPrice - swapPrice) / poolPrice) * 100; - console.log('burnSwap slippage calculation:', { - poolPrice, - swapPrice, - calculatedSlippage, - outAmountDecimal, - feeDecimal, - lpAmountDecimal, - outAmount: result[0].toString(), - fee: result[1].toString(), - lpAmount: lpAmount.toString(), - }); } catch (slippageErr) { console.error(`Error calculating slippage for burnSwap:`, slippageErr); }