Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/components/PoolMetrics/PoolMetrics.jsx
Original file line number Diff line number Diff line change
@@ -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) => {
Comment on lines +5 to +7
// 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;
Comment on lines +2 to +13
Comment on lines +2 to +13
Comment on lines +2 to +13
return apr;
};

return (
<div>
{/* Pool metrics display component */}
</div>
);
};

export default PoolMetrics;
66 changes: 66 additions & 0 deletions src/utils/poolCalculations.js
Original file line number Diff line number Diff line change
@@ -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
};
Comment on lines +12 to +16

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;
};
Comment on lines +21 to +26

export { calculatePoolMetrics };

import { formatUnits } from './format';

Comment on lines +30 to +31
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;
};