@agentek/tools
Version:
Blockchain tools for AI agents
65 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateApyStats = calculateApyStats;
exports.calculateTvlStats = calculateTvlStats;
exports.extractTimeSeriesData = extractTimeSeriesData;
exports.calculateStabilityScore = calculateStabilityScore;
const helpers_js_1 = require("./helpers.js");
// Calculate statistics for APY values
function calculateApyStats(values) {
if (values.length === 0) {
return {
average: '0.00%',
min: '0.00%',
max: '0.00%',
volatility: '0.00%'
};
}
const avg = values.reduce((sum, val) => sum + val, 0) / values.length;
const min = Math.min(...values);
const max = Math.max(...values);
// Calculate volatility (standard deviation)
const variance = values.reduce((sum, val) => sum + Math.pow(val - avg, 2), 0) / values.length;
const volatility = Math.sqrt(variance);
return {
average: `${avg.toFixed(2)}%`,
min: `${min.toFixed(2)}%`,
max: `${max.toFixed(2)}%`,
volatility: `${volatility.toFixed(2)}%`
};
}
// Calculate statistics for TVL values
function calculateTvlStats(values) {
if (values.length === 0) {
return {
average: '$0.00',
min: '$0.00',
max: '$0.00'
};
}
const avg = values.reduce((sum, val) => sum + val, 0) / values.length;
const min = Math.min(...values);
const max = Math.max(...values);
return {
average: (0, helpers_js_1.formatUSD)(avg),
min: (0, helpers_js_1.formatUSD)(min),
max: (0, helpers_js_1.formatUSD)(max)
};
}
// Extract and filter time series data
function extractTimeSeriesData(data, days) {
// Sort data by timestamp (oldest to newest)
const sortedData = [...data].sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
// Filter data by requested days
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
return sortedData.filter(point => new Date(point.timestamp) >= cutoffDate);
}
// Calculate stability score (inverse of normalized volatility)
function calculateStabilityScore(avgApy, volatility) {
if (avgApy === 0 || volatility === 0)
return 100; // Avoid division by zero
// Higher score means more stable (100 is max)
return Math.min(100, Math.max(0, 100 - (volatility / avgApy * 100)));
}
//# sourceMappingURL=stats.js.map