price bug fix

This commit is contained in:
2025-12-03 13:33:42 -04:00
parent 068286919a
commit 4523195b78

View File

@@ -464,41 +464,12 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
let tvlStr: string | undefined; let tvlStr: string | undefined;
if (isWorking) { if (isWorking) {
// Fetch pool price (use first token as quote, index 0) // Fetch token decimals and balance first (needed for both price and TVL)
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)
try { try {
if (tokens && tokens.length > 0) { if (tokens && tokens.length > 0) {
const firstTokenAddress = tokens[0]; const firstTokenAddress = tokens[0];
// Get token decimals, balance, and pool price in parallel
// Get token decimals and balance const [decimals, balance, priceRaw] = await Promise.all([
const [decimals, balance] = await Promise.all([
publicClient.readContract({ publicClient.readContract({
address: firstTokenAddress as `0x${string}`, address: firstTokenAddress as `0x${string}`,
abi: ERC20ABI, abi: ERC20ABI,
@@ -510,16 +481,36 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
functionName: 'balanceOf', functionName: 'balanceOf',
args: [poolAddress], args: [poolAddress],
}) as Promise<bigint>, }) as Promise<bigint>,
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 tokenBalance = Number(balance) / Math.pow(10, decimals);
const approximateTVL = tokenBalance * 3; const approximateTVL = tokenBalance * 3;
tvlStr = formatTVL(approximateTVL); tvlStr = formatTVL(approximateTVL);
} }
} catch (err) { } catch (err) {
console.error(`Error fetching TVL for ${poolAddress}:`, err); console.error(`Error fetching pool price/TVL for ${poolAddress}:`, err);
priceStr = undefined;
tvlStr = undefined; tvlStr = undefined;
} }
} }
@@ -632,8 +623,6 @@ export function useSwapMintAmounts(
let calculatedSlippage: number | undefined; let calculatedSlippage: number | undefined;
try { try {
console.log('input token index', inputTokenIndex);
const poolPriceInt128 = await publicClient.readContract({ const poolPriceInt128 = await publicClient.readContract({
address: partyInfoAddress as `0x${string}`, address: partyInfoAddress as `0x${string}`,
abi: IPartyInfoABI, abi: IPartyInfoABI,
@@ -716,14 +705,12 @@ export function useBurnSwapAmounts(
try { try {
setLoading(true); setLoading(true);
setError(null); setError(null);
console.log('fetching swap amounts')
// Get chain ID and contract address // Get chain ID and contract address
const chainId = await publicClient.getChainId(); const chainId = await publicClient.getChainId();
// @ts-ignore // @ts-ignore
const partyInfoAddress = (chainInfo as Record<string, { v1: { PartyInfo: string } }>)[chainId.toString()]?.v1?.PartyInfo; const partyInfoAddress = (chainInfo as Record<string, { v1: { PartyInfo: string } }>)[chainId.toString()]?.v1?.PartyInfo;
if (!partyInfoAddress) { if (!partyInfoAddress) {
console.log('errores here')
setError('PartyInfo contract not found for current chain'); setError('PartyInfo contract not found for current chain');
setBurnSwapAmounts(null); setBurnSwapAmounts(null);
return; return;
@@ -762,17 +749,6 @@ export function useBurnSwapAmounts(
const swapPrice = (outAmountDecimal + feeDecimal) / lpAmountDecimal; const swapPrice = (outAmountDecimal + feeDecimal) / lpAmountDecimal;
calculatedSlippage = ((poolPrice - swapPrice) / poolPrice) * 100; 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) { } catch (slippageErr) {
console.error(`Error calculating slippage for burnSwap:`, slippageErr); console.error(`Error calculating slippage for burnSwap:`, slippageErr);
} }