diff --git a/src/components/PoolMetrics.js b/src/components/PoolMetrics.js new file mode 100644 index 0000000..64fea49 --- /dev/null +++ b/src/components/PoolMetrics.js @@ -0,0 +1,78 @@ +import { calculatePoolMetrics } from '../utils/poolCalculations'; + +export const getPoolMetrics = (poolData) => { + return calculatePoolMetrics(poolData); +}; + +export const calculatePoolMetrics = (data) => { + // Placeholder for actual pool metrics calculation + return {}; +}; + +export const formatPoolMetrics = (rawMetrics) => { + // Ensure proper formatting and validation of pool metrics + if (!rawMetrics || typeof rawMetrics !== 'object') { + return { error: 'Invalid pool data provided' }; + } + + const formattedMetrics = { + tvl: rawMetrics.tvl || 0, + volume24h: rawMetrics.volume24h || 0, + fees24h: rawMetrics.fees24h || 0, + apr: rawMetrics.apr || 0 + }; + + // Validate numeric values are reasonable + if (formattedMetrics.tvl < 0) formattedMetrics.tvl = 0; + if (formattedMetrics.volume24h < 0) formattedMetrics.volume24h = 0; + if (formattedMetrics.fees24h < 0) formattedMetrics.fees24h = 0; + if (formattedMetrics.apr < 0 || formattedMetrics.apr > 1000) formattedMetrics.apr = 0; + + return formattedMetrics; +}; +export const calculatePoolMetrics = (poolData) => { + if (!poolData) { + return null; + } + + // Validate input data + const validatedData = validatePoolData(poolData); + + // Calculate TVL (Total Value Locked) + const tvl = calculateTVL(validatedData); + + // Calculate 24h volume + const volume24h = validatedData.volume24h || 0; + + // Calculate fees + const fees24h = calculateFees24h(validatedData); + + // Calculate APR + const apr = calculateAPR(validatedData); + + return { + tvl, + volume24h, + fees24h, + apr + }; +}; + +const validatePoolData = (data) => { + if (!data) return {}; + return data; +}; + +const calculateTVL = (data) => { + return data.tvl || 0; +}; + +const calculateFees24h = (data) => { + if (!data || !data.fees24h) return 0; + return data.fees24h; +}; + +const calculateAPR = (data) => { + if (!data) return 0; + return data.apr || 0; +}; \ No newline at end of file diff --git a/src/components/PoolMetrics/PoolMetrics.jsx b/src/components/PoolMetrics/PoolMetrics.jsx new file mode 100644 index 0000000..9fb715b --- /dev/null +++ b/src/components/PoolMetrics/PoolMetrics.jsx @@ -0,0 +1,24 @@ +import React, { useState, useEffect } from 'react'; +import { getPoolData } from '../../utils/poolData'; + +const PoolMetrics = ({ poolId }) => { + const [metrics, setMetrics] = useState({}); + + const calculateAPR = (pool) => { + // Calculate APR based on pool rewards + if (!pool || !pool.totalLiquidity || !pool.rewardPerDay) return 0; + + // Fixed: Correct APR calculation formula + if (!pool.totalLiquidity || pool.totalLiquidity === 0) return 0; + const apr = ((pool.rewardPerDay * 365) / pool.totalLiquidity) * 100; + return apr; + }; + + return ( +
+ {/* Pool metrics display component */} +
+ ); +}; + +export default PoolMetrics; \ No newline at end of file diff --git a/src/utils/poolCalculations.js b/src/utils/poolCalculations.js new file mode 100644 index 0000000..21f151b --- /dev/null +++ b/src/utils/poolCalculations.js @@ -0,0 +1,66 @@ +// Utility functions for pool metric calculations +const calculatePoolMetrics = (poolData) => { + // Validate inputs to prevent incorrect calculations + if (!poolData.totalLiquidity || poolData.totalLiquidity <= 0) { + return { + apr: 0, + apy: 0, + dailyApr: 0 + }; + } + + const metrics = { + apr: (poolData.dailyReward * 365) / poolData.totalLiquidity * 100, + apy: calculateAPY(poolData.dailyReward, poolData.totalLiquidity), + dailyApr: (poolData.dailyReward / poolData.totalLiquidity) * 100 + }; + + return metrics; +}; + +const calculateAPY = (dailyReward, totalLiquidity) => { + if (!dailyReward || !totalLiquidity) return 0; + // APY = (1 + (dailyReward/totalLiquidity))^365 - 1 + const dailyRate = dailyReward / totalLiquidity; + return Math.pow(1 + dailyRate, 365) - 1; +}; + +export { calculatePoolMetrics }; + +import { formatUnits } from './format'; + +export const getPoolData = async (poolId) => { + try { + // Mock implementation - real implementation would fetch from API + const poolData = { + totalLiquidity: 1000000, + dailyReward: 2500, + }; + + // Validate that we have proper numbers + if (poolData.totalLiquidity <= 0) { + throw new Error('Invalid pool data values'); + } + + // Calculate APR correctly + poolData.apr = (poolData.dailyReward / poolData.totalLiquidity) * 365 * 100; + + return poolData; + } catch (error) { + console.error('Error fetching pool data:', error); + return null; + } +}; + +export const calculateCorrectAPR = (dailyReward, totalLiquidity) => { + // Defensive check for valid numbers + if (!totalLiquidity || totalLiquidity <= 0) { + return 0; + } + const aprValue = (dailyReward / totalLiquidity) * 365 * 100; + return aprValue; +}; + +export const validatePoolMetrics = (metrics) => { + return metrics && metrics.totalLiquidity > 0 && metrics.dailyReward > 0; +}; \ No newline at end of file