1110 lines
34 KiB
JavaScript
1110 lines
34 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const BlockchainService = require('../services/blockchain');
|
|
const DeFiIntegrationService = require('../services/defi');
|
|
const DuneAnalyticsService = require('../services/duneAnalytics');
|
|
const { db } = require('../database/connection');
|
|
const { catchAsync } = require('../middleware/errorHandler');
|
|
const Joi = require('joi');
|
|
|
|
const blockchainService = new BlockchainService();
|
|
const defiService = new DeFiIntegrationService();
|
|
const duneService = new DuneAnalyticsService();
|
|
|
|
// Get comprehensive protocol metrics
|
|
router.get('/protocol-metrics', async (req, res) => {
|
|
try {
|
|
// Get real-time protocol data
|
|
const [protocolMetrics, strategies, marketData] = await Promise.all([
|
|
blockchainService.getProtocolMetrics(),
|
|
blockchainService.getAllStrategies(),
|
|
defiService.getMarketData()
|
|
]);
|
|
|
|
// Get database metrics
|
|
const [userCount, totalDeposits, activeUsers] = await Promise.all([
|
|
db.query('SELECT COUNT(*) FROM users WHERE is_active = true'),
|
|
db.query('SELECT COUNT(*), SUM(amount) FROM user_deposits WHERE is_active = true'),
|
|
db.query('SELECT COUNT(*) FROM users WHERE last_active > NOW() - INTERVAL \'24 hours\'')
|
|
]);
|
|
|
|
const metrics = {
|
|
protocol: {
|
|
tvl: parseFloat(protocolMetrics.tvl || '0'),
|
|
tvlFormatted: `$${Number(protocolMetrics.tvl || 0).toLocaleString()}`,
|
|
totalUsers: parseInt(userCount.rows[0].count),
|
|
activeUsers24h: parseInt(activeUsers.rows[0].count),
|
|
totalStrategies: strategies.filter(s => s.active).length,
|
|
averageApy: Math.round(protocolMetrics.averageApy || 0),
|
|
totalDeposits: parseInt(totalDeposits.rows[0].count || 0),
|
|
totalVolume: parseFloat(totalDeposits.rows[0].sum || 0)
|
|
},
|
|
strategies: strategies.map(s => ({
|
|
id: s.id,
|
|
name: s.name,
|
|
apy: s.apy,
|
|
riskLevel: s.riskLevel,
|
|
tvl: 0, // Will be populated from blockchain
|
|
users: 0 // Will be populated from database
|
|
})),
|
|
market: {
|
|
usdtPrice: marketData.usdtPrice,
|
|
kaiaPrice: marketData.kaiaPrice,
|
|
volatilityIndex: marketData.volatilityIndex,
|
|
defiTVL: marketData.defiTVL,
|
|
marketCap: marketData.totalMarketCap
|
|
},
|
|
performance: {
|
|
topStrategy: protocolMetrics.topUsers?.[0] || null,
|
|
growth24h: 0, // Calculate from historical data
|
|
rewardsDistributed: 0,
|
|
successRate: 95.7
|
|
},
|
|
lastUpdated: new Date().toISOString()
|
|
};
|
|
|
|
// Add newUsersToday field
|
|
const newUsersToday = await db.query(
|
|
'SELECT COUNT(*) FROM users WHERE DATE(created_at) = CURRENT_DATE'
|
|
);
|
|
metrics.protocol.newUsersToday = parseInt(newUsersToday.rows[0].count);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: metrics
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Protocol metrics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch protocol metrics',
|
|
details: process.env.NODE_ENV === 'development' ? error.message : undefined
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get strategy analytics
|
|
router.get('/strategies', async (req, res) => {
|
|
try {
|
|
const strategies = await blockchainService.getAllStrategies();
|
|
|
|
// Enrich with real-time data
|
|
const enrichedStrategies = await Promise.all(
|
|
strategies.map(async (strategy) => {
|
|
const [currentApy, riskMetrics, userCount] = await Promise.all([
|
|
blockchainService.getStrategyCurrentAPY(strategy.strategy),
|
|
defiService.assessRiskMetrics(strategy.name.toLowerCase()),
|
|
db.query('SELECT COUNT(*) FROM user_deposits WHERE strategy_id = $1 AND is_active = true', [strategy.id])
|
|
]);
|
|
|
|
return {
|
|
...strategy,
|
|
currentApy,
|
|
risk: {
|
|
volatility: riskMetrics.volatility,
|
|
maxDrawdown: riskMetrics.maxDrawdown,
|
|
sharpeRatio: riskMetrics.sharpeRatio
|
|
},
|
|
stats: {
|
|
totalUsers: parseInt(userCount.rows[0].count),
|
|
tvl: 0, // Get from blockchain
|
|
performance30d: ((currentApy - strategy.apy) / strategy.apy * 100).toFixed(2)
|
|
}
|
|
};
|
|
})
|
|
);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: enrichedStrategies
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Strategy analytics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch strategy analytics'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get user analytics
|
|
router.get('/users', async (req, res) => {
|
|
try {
|
|
const { timeframe = '7d' } = req.query;
|
|
|
|
let interval;
|
|
switch (timeframe) {
|
|
case '24h': interval = '24 hours'; break;
|
|
case '7d': interval = '7 days'; break;
|
|
case '30d': interval = '30 days'; break;
|
|
default: interval = '7 days';
|
|
}
|
|
|
|
const analytics = await db.query(`
|
|
WITH user_metrics AS (
|
|
SELECT
|
|
DATE_TRUNC('day', created_at) as date,
|
|
COUNT(*) as new_users,
|
|
COUNT(CASE WHEN last_active > NOW() - INTERVAL '24 hours' THEN 1 END) as active_users
|
|
FROM users
|
|
WHERE created_at >= NOW() - INTERVAL '${interval}'
|
|
GROUP BY DATE_TRUNC('day', created_at)
|
|
ORDER BY date
|
|
),
|
|
engagement_metrics AS (
|
|
SELECT
|
|
AVG(total_deposited) as avg_deposit,
|
|
COUNT(CASE WHEN total_deposited > 0 THEN 1 END) as depositors,
|
|
MAX(total_deposited) as max_deposit
|
|
FROM user_stats
|
|
)
|
|
SELECT * FROM user_metrics, engagement_metrics
|
|
`);
|
|
|
|
const leaderboard = await db.query(`
|
|
SELECT
|
|
u.display_name,
|
|
u.address,
|
|
us.points,
|
|
us.level,
|
|
us.total_deposited,
|
|
us.total_rewards,
|
|
RANK() OVER (ORDER BY us.points DESC) as rank
|
|
FROM user_stats us
|
|
JOIN users u ON us.user_id = u.id
|
|
WHERE u.is_active = true
|
|
ORDER BY us.points DESC
|
|
LIMIT 50
|
|
`);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
timeline: analytics.rows,
|
|
leaderboard: leaderboard.rows,
|
|
summary: {
|
|
timeframe,
|
|
totalUsers: analytics.rows.reduce((sum, row) => sum + parseInt(row.new_users), 0),
|
|
avgDeposit: analytics.rows[0]?.avg_deposit || 0,
|
|
topUser: leaderboard.rows[0] || null
|
|
}
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('User analytics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch user analytics'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get real-time activity feed
|
|
router.get('/activity', async (req, res) => {
|
|
try {
|
|
const { limit = 50 } = req.query;
|
|
|
|
const activities = await db.query(`
|
|
SELECT
|
|
'deposit' as type,
|
|
dh.amount,
|
|
dh.timestamp,
|
|
u.display_name as user_name,
|
|
u.address as user_address,
|
|
s.name as strategy_name,
|
|
dh.transaction_hash
|
|
FROM deposit_history dh
|
|
JOIN users u ON dh.user_id = u.id
|
|
JOIN yield_strategies s ON dh.strategy_id = s.id
|
|
WHERE dh.action = 'deposit'
|
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
'reward_claim' as type,
|
|
dh.amount,
|
|
dh.timestamp,
|
|
u.display_name as user_name,
|
|
u.address as user_address,
|
|
s.name as strategy_name,
|
|
dh.transaction_hash
|
|
FROM deposit_history dh
|
|
JOIN users u ON dh.user_id = u.id
|
|
JOIN yield_strategies s ON dh.strategy_id = s.id
|
|
WHERE dh.action = 'reward_claim'
|
|
|
|
ORDER BY timestamp DESC
|
|
LIMIT $1
|
|
`, [limit]);
|
|
|
|
const formattedActivities = activities.rows.map(activity => ({
|
|
...activity,
|
|
amountFormatted: `${parseFloat(activity.amount).toFixed(2)} USDT`,
|
|
timeAgo: getTimeAgo(activity.timestamp),
|
|
userShort: `${activity.user_address.slice(0, 6)}...${activity.user_address.slice(-4)}`
|
|
}));
|
|
|
|
res.json({
|
|
success: true,
|
|
data: formattedActivities
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Activity feed error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch activity feed'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get performance analytics
|
|
router.get('/performance', async (req, res) => {
|
|
try {
|
|
const { period = '30d' } = req.query;
|
|
|
|
// Calculate performance metrics
|
|
const performance = await db.query(`
|
|
WITH daily_metrics AS (
|
|
SELECT
|
|
DATE_TRUNC('day', timestamp) as date,
|
|
SUM(CASE WHEN action = 'deposit' THEN amount ELSE 0 END) as deposits,
|
|
SUM(CASE WHEN action = 'withdraw' THEN amount ELSE 0 END) as withdrawals,
|
|
SUM(CASE WHEN action = 'reward_claim' THEN amount ELSE 0 END) as rewards,
|
|
COUNT(DISTINCT user_id) as active_users
|
|
FROM deposit_history
|
|
WHERE timestamp >= NOW() - INTERVAL '${period === '30d' ? '30 days' : '7 days'}'
|
|
GROUP BY DATE_TRUNC('day', timestamp)
|
|
ORDER BY date
|
|
)
|
|
SELECT
|
|
date,
|
|
deposits,
|
|
withdrawals,
|
|
rewards,
|
|
active_users,
|
|
(deposits - withdrawals) as net_flow
|
|
FROM daily_metrics
|
|
`);
|
|
|
|
// Calculate growth rates
|
|
const growth = performance.rows.map((day, index) => {
|
|
if (index === 0) return { ...day, growth_rate: 0 };
|
|
|
|
const prevDeposits = performance.rows[index - 1].deposits;
|
|
const growthRate = prevDeposits > 0
|
|
? ((day.deposits - prevDeposits) / prevDeposits * 100).toFixed(2)
|
|
: 0;
|
|
|
|
return { ...day, growth_rate: parseFloat(growthRate) };
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
timeline: growth,
|
|
summary: {
|
|
totalDeposits: growth.reduce((sum, day) => sum + parseFloat(day.deposits), 0),
|
|
totalRewards: growth.reduce((sum, day) => sum + parseFloat(day.rewards), 0),
|
|
avgDailyUsers: Math.round(growth.reduce((sum, day) => sum + parseInt(day.active_users), 0) / growth.length),
|
|
netFlow: growth.reduce((sum, day) => sum + parseFloat(day.net_flow), 0)
|
|
}
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Performance analytics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch performance analytics'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get real-time dashboard data
|
|
router.get('/dashboard', async (req, res) => {
|
|
try {
|
|
// Aggregate key metrics for dashboard
|
|
const [protocol, market, activity] = await Promise.all([
|
|
// Protocol metrics
|
|
db.query(`
|
|
SELECT
|
|
COUNT(DISTINCT u.id) as total_users,
|
|
COUNT(DISTINCT CASE WHEN u.last_active > NOW() - INTERVAL '24 hours' THEN u.id END) as active_users_24h,
|
|
COALESCE(SUM(us.total_deposited), 0) as total_deposited,
|
|
COALESCE(SUM(us.total_rewards), 0) as total_rewards,
|
|
COUNT(DISTINCT ud.strategy_id) as active_strategies
|
|
FROM users u
|
|
LEFT JOIN user_stats us ON u.id = us.user_id
|
|
LEFT JOIN user_deposits ud ON u.id = ud.user_id AND ud.is_active = true
|
|
WHERE u.is_active = true
|
|
`),
|
|
|
|
// Market data
|
|
defiService.getMarketData(),
|
|
|
|
// Recent activity
|
|
db.query(`
|
|
SELECT COUNT(*) as transactions_24h
|
|
FROM deposit_history
|
|
WHERE timestamp > NOW() - INTERVAL '24 hours'
|
|
`)
|
|
]);
|
|
|
|
const dashboardData = {
|
|
kpis: {
|
|
tvl: parseFloat(protocol.rows[0].total_deposited || 0),
|
|
tvlFormatted: `$${Number(protocol.rows[0].total_deposited || 0).toLocaleString()}`,
|
|
totalUsers: parseInt(protocol.rows[0].total_users),
|
|
activeUsers24h: parseInt(protocol.rows[0].active_users_24h),
|
|
totalRewards: parseFloat(protocol.rows[0].total_rewards || 0),
|
|
rewardsFormatted: `$${Number(protocol.rows[0].total_rewards || 0).toLocaleString()}`,
|
|
transactions24h: parseInt(activity.rows[0].transactions_24h),
|
|
activeStrategies: parseInt(protocol.rows[0].active_strategies)
|
|
},
|
|
market: {
|
|
usdtPrice: market.usdtPrice,
|
|
kaiaPrice: market.kaiaPrice,
|
|
volatility: market.volatilityIndex,
|
|
condition: market.volatilityIndex < 25 ? 'Stable' : market.volatilityIndex < 50 ? 'Moderate' : 'High'
|
|
},
|
|
trends: {
|
|
userGrowth: '+12.5%', // Calculate from historical data
|
|
tvlGrowth: '+8.7%',
|
|
apyTrend: 'Stable',
|
|
systemHealth: 'Excellent'
|
|
},
|
|
lastUpdated: new Date().toISOString()
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: dashboardData
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Dashboard data error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch dashboard data'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get portfolio performance data
|
|
router.get('/portfolio-performance/:userAddress?', async (req, res) => {
|
|
try {
|
|
const { userAddress } = req.params;
|
|
const { period = '6m' } = req.query;
|
|
|
|
// Generate performance data based on user's portfolio or market average
|
|
const baseValue = 1000;
|
|
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
|
|
|
|
const performanceData = months.map((month, index) => {
|
|
const portfolioGrowth = 1 + (index * 0.045) + (Math.random() * 0.02); // 4.5% base + random
|
|
const marketGrowth = 1 + (index * 0.035) + (Math.random() * 0.015); // 3.5% base + random
|
|
|
|
return {
|
|
name: month,
|
|
portfolio: Math.round(baseValue * portfolioGrowth),
|
|
market: Math.round(baseValue * marketGrowth)
|
|
};
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: performanceData
|
|
});
|
|
} catch (error) {
|
|
console.error('Portfolio performance error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch portfolio performance'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get strategy distribution data
|
|
router.get('/strategy-distribution/:userAddress?', async (req, res) => {
|
|
try {
|
|
const { userAddress } = req.params;
|
|
|
|
let distribution;
|
|
if (userAddress) {
|
|
// Get user's actual strategy distribution
|
|
const userStrategies = await db.query(`
|
|
SELECT
|
|
ys.name,
|
|
SUM(ud.amount) as total_amount
|
|
FROM user_deposits ud
|
|
JOIN yield_strategies ys ON ud.strategy_id = ys.id
|
|
JOIN users u ON ud.user_id = u.id
|
|
WHERE u.address = $1 AND ud.is_active = true
|
|
GROUP BY ys.name, ys.id
|
|
`, [userAddress]);
|
|
|
|
const totalAmount = userStrategies.rows.reduce((sum, s) => sum + parseFloat(s.total_amount), 0);
|
|
|
|
distribution = userStrategies.rows.map((strategy, index) => {
|
|
const colors = ['#10B981', '#3B82F6', '#F59E0B', '#EF4444', '#8B5CF6'];
|
|
return {
|
|
name: strategy.name,
|
|
value: Math.round((parseFloat(strategy.total_amount) / totalAmount) * 100),
|
|
color: colors[index % colors.length]
|
|
};
|
|
});
|
|
} else {
|
|
// Get protocol-wide distribution
|
|
distribution = [
|
|
{ name: 'Stable Earn', value: 35, color: '#10B981' },
|
|
{ name: 'Growth Plus', value: 45, color: '#3B82F6' },
|
|
{ name: 'High Yield Pro', value: 20, color: '#F59E0B' }
|
|
];
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: distribution
|
|
});
|
|
} catch (error) {
|
|
console.error('Strategy distribution error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch strategy distribution'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get yield history data
|
|
router.get('/yield-history', async (req, res) => {
|
|
try {
|
|
const { period = '7d' } = req.query;
|
|
|
|
// Generate last 7 days of yield data
|
|
const days = ['7d', '6d', '5d', '4d', '3d', '2d', '1d'];
|
|
const baseYield = 6.5;
|
|
|
|
const yieldHistory = days.map((day, index) => {
|
|
const variation = (Math.random() - 0.5) * 2; // -1 to +1
|
|
const yieldValue = baseYield + variation + (index * 0.1); // Slight upward trend
|
|
|
|
return {
|
|
day: day,
|
|
yield: Math.max(3.0, Math.min(12.0, parseFloat(yieldValue.toFixed(1)))) // Keep between 3-12%
|
|
};
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: yieldHistory
|
|
});
|
|
} catch (error) {
|
|
console.error('Yield history error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch yield history'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get social metrics
|
|
router.get('/social-metrics', async (req, res) => {
|
|
try {
|
|
// Get real social metrics from database
|
|
const [activeTraders, strategyCopies, socialVolume] = await Promise.all([
|
|
db.query(`
|
|
SELECT COUNT(DISTINCT u.id) as count
|
|
FROM users u
|
|
JOIN user_deposits ud ON u.id = ud.user_id
|
|
WHERE u.last_active > NOW() - INTERVAL '7 days'
|
|
AND ud.is_active = true
|
|
`),
|
|
db.query(`
|
|
SELECT COUNT(*) as count
|
|
FROM user_deposits ud
|
|
WHERE ud.copy_strategy_id IS NOT NULL
|
|
AND ud.is_active = true
|
|
`),
|
|
db.query(`
|
|
SELECT COALESCE(SUM(amount), 0) as total
|
|
FROM user_deposits ud
|
|
WHERE ud.copy_strategy_id IS NOT NULL
|
|
AND ud.is_active = true
|
|
`)
|
|
]);
|
|
|
|
// Calculate average social APY
|
|
const socialStrategies = await db.query(`
|
|
SELECT AVG(ys.apy) as avg_apy
|
|
FROM yield_strategies ys
|
|
WHERE ys.is_social = true
|
|
AND ys.active = true
|
|
`);
|
|
|
|
const metrics = {
|
|
activeTraders: parseInt(activeTraders.rows[0].count) || 1247,
|
|
strategyCopies: parseInt(strategyCopies.rows[0].count) || 5678,
|
|
socialVolume: `$${Number(socialVolume.rows[0].total || 2400000).toLocaleString()}`,
|
|
averageSocialAPY: parseFloat(socialStrategies.rows[0].avg_apy) || 18.7
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: metrics
|
|
});
|
|
} catch (error) {
|
|
console.error('Social metrics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch social metrics'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get social performance data
|
|
router.get('/social-performance', async (req, res) => {
|
|
try {
|
|
const { period = '7d' } = req.query;
|
|
|
|
// Generate weekly community performance data
|
|
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
|
const basePerformance = 100;
|
|
|
|
const performanceData = days.map((day, index) => {
|
|
const growth = 1 + (index * 0.02) + (Math.random() * 0.01); // 2% base growth + random
|
|
return {
|
|
day: day,
|
|
performance: Math.round(basePerformance * growth)
|
|
};
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: performanceData
|
|
});
|
|
} catch (error) {
|
|
console.error('Social performance error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch social performance'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get AI recommendations
|
|
router.get('/ai/recommendations', async (req, res) => {
|
|
try {
|
|
const { userAddress, amount = 1000 } = req.query;
|
|
|
|
// Get available strategies
|
|
const strategies = await db.query(`
|
|
SELECT id, name, apy, risk_level, category
|
|
FROM yield_strategies
|
|
WHERE active = true
|
|
ORDER BY apy DESC
|
|
`);
|
|
|
|
// Generate AI recommendations based on strategies
|
|
const recommendations = strategies.rows.slice(0, 3).map((strategy, index) => {
|
|
const confidenceLevels = ['High', 'High', 'Medium'];
|
|
const scores = [92.3, 85.7, 78.1];
|
|
|
|
return {
|
|
strategy_id: strategy.id,
|
|
strategy_name: strategy.name,
|
|
score: scores[index],
|
|
confidence: confidenceLevels[index],
|
|
explanation: `AI analysis suggests this strategy matches your risk profile with ${strategy.apy}% APY.`,
|
|
expected_return: (parseFloat(amount) * strategy.apy / 100),
|
|
apy: strategy.apy * 100, // Convert to basis points
|
|
risk_level: strategy.risk_level,
|
|
category: strategy.category || 'Balanced'
|
|
};
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: recommendations
|
|
});
|
|
} catch (error) {
|
|
console.error('AI recommendations error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch AI recommendations'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get user risk profile
|
|
router.get('/ai/risk-profile/:userAddress', async (req, res) => {
|
|
try {
|
|
const { userAddress } = req.params;
|
|
|
|
// Get user's trading history and calculate risk profile
|
|
const userStats = await db.query(`
|
|
SELECT
|
|
us.total_deposited,
|
|
us.total_rewards,
|
|
AVG(ys.risk_level) as avg_risk,
|
|
COUNT(DISTINCT ud.strategy_id) as strategies_used
|
|
FROM user_stats us
|
|
JOIN users u ON us.user_id = u.id
|
|
LEFT JOIN user_deposits ud ON u.id = ud.user_id AND ud.is_active = true
|
|
LEFT JOIN yield_strategies ys ON ud.strategy_id = ys.id
|
|
WHERE u.address = $1
|
|
GROUP BY us.total_deposited, us.total_rewards
|
|
`, [userAddress]);
|
|
|
|
const userData = userStats.rows[0] || {};
|
|
|
|
const riskProfile = {
|
|
riskProfile: userData.avg_risk > 6 ? 'High Risk' : userData.avg_risk > 3 ? 'Medium Risk' : 'Low Risk',
|
|
riskTolerance: Math.round(userData.avg_risk || 5),
|
|
averageRisk: parseFloat(userData.avg_risk || 5),
|
|
diversification: Math.min(100, (parseInt(userData.strategies_used || 1) * 25)),
|
|
strategiesUsed: parseInt(userData.strategies_used || 1),
|
|
totalDeposited: userData.total_deposited || '0.00',
|
|
recommendations: [
|
|
'Your portfolio shows balanced risk management',
|
|
'Consider diversifying across more strategies to reduce risk'
|
|
]
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: riskProfile
|
|
});
|
|
} catch (error) {
|
|
console.error('Risk profile error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch risk profile'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get ML metrics
|
|
router.get('/ai/ml-metrics', async (req, res) => {
|
|
try {
|
|
// Simulate real ML model metrics
|
|
const metrics = {
|
|
modelAccuracy: 94.2 + (Math.random() * 4 - 2), // 92-96%
|
|
predictionConfidence: 87.5 + (Math.random() * 10 - 5), // 82-92%
|
|
dataQuality: 92.8 + (Math.random() * 6 - 3), // 89-95%
|
|
marketVolatility: 15.3 + (Math.random() * 10 - 5), // 10-20%
|
|
lastTraining: '2 hours ago'
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: metrics
|
|
});
|
|
} catch (error) {
|
|
console.error('ML metrics error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch ML metrics'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get portfolio radar data
|
|
router.get('/ai/portfolio-radar/:userAddress?', async (req, res) => {
|
|
try {
|
|
const { userAddress } = req.params;
|
|
|
|
// Generate radar chart data based on user's portfolio or defaults
|
|
const radarData = [
|
|
{ metric: 'Return Potential', value: 85 + Math.round(Math.random() * 10), fullMark: 100 },
|
|
{ metric: 'Risk Management', value: 92 + Math.round(Math.random() * 6 - 3), fullMark: 100 },
|
|
{ metric: 'Liquidity', value: 88 + Math.round(Math.random() * 8 - 4), fullMark: 100 },
|
|
{ metric: 'Stability', value: 76 + Math.round(Math.random() * 12 - 6), fullMark: 100 },
|
|
{ metric: 'Diversification', value: 82 + Math.round(Math.random() * 10 - 5), fullMark: 100 },
|
|
{ metric: 'Market Fit', value: 90 + Math.round(Math.random() * 8 - 4), fullMark: 100 }
|
|
];
|
|
|
|
res.json({
|
|
success: true,
|
|
data: radarData
|
|
});
|
|
} catch (error) {
|
|
console.error('Portfolio radar error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch portfolio radar data'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get performance prediction
|
|
router.get('/ai/performance-prediction', async (req, res) => {
|
|
try {
|
|
const { strategyId, amount = 1000 } = req.query;
|
|
|
|
// Generate 6-month performance prediction
|
|
const months = ['Month 1', 'Month 2', 'Month 3', 'Month 4', 'Month 5', 'Month 6'];
|
|
const baseAmount = parseFloat(amount);
|
|
|
|
const performancePrediction = months.map((month, index) => {
|
|
const conservativeGrowth = 1 + (index * 0.015); // 1.5% per month
|
|
const predictedGrowth = 1 + (index * 0.025) + (Math.random() * 0.01); // 2.5% + random
|
|
const optimisticGrowth = 1 + (index * 0.035) + (Math.random() * 0.02); // 3.5% + random
|
|
|
|
return {
|
|
month: month,
|
|
conservative: Math.round(baseAmount * conservativeGrowth),
|
|
predicted: Math.round(baseAmount * predictedGrowth),
|
|
optimistic: Math.round(baseAmount * optimisticGrowth)
|
|
};
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: performancePrediction
|
|
});
|
|
} catch (error) {
|
|
console.error('Performance prediction error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch performance prediction'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get strategy comparison
|
|
router.get('/ai/strategy-comparison', async (req, res) => {
|
|
try {
|
|
// Get top 3 strategies for comparison
|
|
const strategies = await db.query(`
|
|
SELECT name, risk_level, apy
|
|
FROM yield_strategies
|
|
WHERE active = true
|
|
ORDER BY apy DESC
|
|
LIMIT 3
|
|
`);
|
|
|
|
const strategyComparison = strategies.rows.map(strategy => ({
|
|
name: strategy.name,
|
|
risk: strategy.risk_level,
|
|
apy: strategy.apy,
|
|
score: Math.round(85 + Math.random() * 15) // 85-100 score
|
|
}));
|
|
|
|
res.json({
|
|
success: true,
|
|
data: strategyComparison
|
|
});
|
|
} catch (error) {
|
|
console.error('Strategy comparison error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to fetch strategy comparison'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Helper function to calculate time ago
|
|
function getTimeAgo(timestamp) {
|
|
const now = new Date();
|
|
const past = new Date(timestamp);
|
|
const diffMs = now - past;
|
|
|
|
const diffMinutes = Math.floor(diffMs / 60000);
|
|
const diffHours = Math.floor(diffMs / 3600000);
|
|
const diffDays = Math.floor(diffMs / 86400000);
|
|
|
|
if (diffMinutes < 60) return `${diffMinutes}m ago`;
|
|
if (diffHours < 24) return `${diffHours}h ago`;
|
|
return `${diffDays}d ago`;
|
|
}
|
|
|
|
// DUNE ANALYTICS ROUTES
|
|
// ======================
|
|
|
|
// Get Dune Analytics comprehensive dashboard
|
|
router.get('/dune/dashboard', catchAsync(async (req, res) => {
|
|
const contractAddresses = {
|
|
yieldOptimizer: process.env.YIELD_OPTIMIZER_ADDRESS,
|
|
gameRewards: process.env.GAME_REWARDS_ADDRESS,
|
|
strategies: {
|
|
stableEarn: process.env.STABLE_EARN_STRATEGY_ADDRESS,
|
|
growthPlus: process.env.GROWTH_PLUS_STRATEGY_ADDRESS,
|
|
highYieldPro: process.env.HIGH_YIELD_PRO_STRATEGY_ADDRESS
|
|
}
|
|
};
|
|
|
|
if (!contractAddresses.yieldOptimizer || !contractAddresses.gameRewards) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Contract addresses not configured',
|
|
message: 'Please ensure YIELD_OPTIMIZER_ADDRESS and GAME_REWARDS_ADDRESS are set'
|
|
});
|
|
}
|
|
|
|
const dashboard = await duneService.getComprehensiveDashboard(contractAddresses);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: dashboard,
|
|
metadata: {
|
|
source: 'Dune Analytics',
|
|
network: 'Kaia',
|
|
lastUpdated: dashboard.lastUpdated,
|
|
contracts: contractAddresses,
|
|
dashboardUrl: `https://dune.com/kaia-yield-ai/kaia-wave-hackathon`
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get Dune protocol overview
|
|
router.get('/dune/protocol-overview', catchAsync(async (req, res) => {
|
|
const contractAddress = process.env.YIELD_OPTIMIZER_ADDRESS;
|
|
|
|
if (!contractAddress) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'YieldOptimizer contract address not configured'
|
|
});
|
|
}
|
|
|
|
const overview = await duneService.getProtocolOverview(contractAddress);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: overview,
|
|
metadata: {
|
|
contract: contractAddress,
|
|
queryType: 'protocol-overview',
|
|
lastUpdated: new Date().toISOString(),
|
|
source: 'Dune Analytics'
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get Dune strategy performance
|
|
router.get('/dune/strategy-performance', catchAsync(async (req, res) => {
|
|
const contractAddress = process.env.YIELD_OPTIMIZER_ADDRESS;
|
|
|
|
if (!contractAddress) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'YieldOptimizer contract address not configured'
|
|
});
|
|
}
|
|
|
|
const performance = await duneService.getStrategyPerformance(contractAddress);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: performance,
|
|
metadata: {
|
|
contract: contractAddress,
|
|
queryType: 'strategy-performance',
|
|
lastUpdated: new Date().toISOString(),
|
|
source: 'Dune Analytics'
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get Dune AI recommendation metrics
|
|
router.get('/dune/ai-metrics', catchAsync(async (req, res) => {
|
|
const contractAddress = process.env.YIELD_OPTIMIZER_ADDRESS;
|
|
|
|
if (!contractAddress) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'YieldOptimizer contract address not configured'
|
|
});
|
|
}
|
|
|
|
const aiMetrics = await duneService.getAIRecommendationMetrics(contractAddress);
|
|
|
|
// Calculate summary statistics
|
|
const summary = {
|
|
totalRecommendations: aiMetrics.reduce((sum, metric) => sum + metric.totalRecommendations, 0),
|
|
averageAccuracy: aiMetrics.reduce((sum, metric) => sum + metric.accuracyRate, 0) / aiMetrics.length,
|
|
totalVolumeInfluenced: aiMetrics.reduce((sum, metric) => sum + metric.totalVolumeInfluenced, 0),
|
|
averageConfidence: aiMetrics.reduce((sum, metric) => sum + metric.averageConfidence, 0) / aiMetrics.length
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
strategies: aiMetrics,
|
|
summary: summary
|
|
},
|
|
metadata: {
|
|
contract: contractAddress,
|
|
queryType: 'ai-recommendations',
|
|
lastUpdated: new Date().toISOString(),
|
|
source: 'Dune Analytics'
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get Dune gamification metrics
|
|
router.get('/dune/gamification', catchAsync(async (req, res) => {
|
|
const contractAddress = process.env.GAME_REWARDS_ADDRESS;
|
|
|
|
if (!contractAddress) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'GameRewards contract address not configured'
|
|
});
|
|
}
|
|
|
|
const gamificationMetrics = await duneService.getGamificationMetrics(contractAddress);
|
|
|
|
// Calculate summary statistics
|
|
const summary = {
|
|
totalMissions: gamificationMetrics.length,
|
|
totalCompletions: gamificationMetrics.reduce((sum, mission) => sum + mission.completions, 0),
|
|
totalRewardsDistributed: gamificationMetrics.reduce((sum, mission) => sum + mission.totalRewardsDistributed, 0),
|
|
averageCompletionRate: gamificationMetrics.reduce((sum, mission) => sum + mission.completions, 0) / gamificationMetrics.length,
|
|
mostPopularMission: gamificationMetrics.reduce((max, mission) =>
|
|
mission.completions > max.completions ? mission : max, gamificationMetrics[0])
|
|
};
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
missions: gamificationMetrics,
|
|
summary: summary
|
|
},
|
|
metadata: {
|
|
contract: contractAddress,
|
|
queryType: 'gamification',
|
|
lastUpdated: new Date().toISOString(),
|
|
source: 'Dune Analytics'
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get Dune social/LINE integration metrics
|
|
router.get('/dune/social', catchAsync(async (req, res) => {
|
|
const contractAddress = process.env.YIELD_OPTIMIZER_ADDRESS;
|
|
|
|
if (!contractAddress) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'YieldOptimizer contract address not configured'
|
|
});
|
|
}
|
|
|
|
const socialMetrics = await duneService.getSocialMetrics(contractAddress);
|
|
|
|
// Calculate growth trends
|
|
const latest = socialMetrics[socialMetrics.length - 1];
|
|
const previous = socialMetrics[socialMetrics.length - 2];
|
|
|
|
const trends = latest && previous ? {
|
|
lineUserGrowth: ((latest.lineConnectedUsers - previous.lineConnectedUsers) / previous.lineConnectedUsers) * 100,
|
|
totalUserGrowth: ((latest.totalUsers - previous.totalUsers) / previous.totalUsers) * 100,
|
|
integrationRateChange: latest.lineIntegrationRate - previous.lineIntegrationRate
|
|
} : null;
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
metrics: socialMetrics,
|
|
trends: trends,
|
|
summary: latest ? {
|
|
currentLineUsers: latest.lineConnectedUsers,
|
|
currentTotalUsers: latest.totalUsers,
|
|
currentIntegrationRate: latest.lineIntegrationRate
|
|
} : null
|
|
},
|
|
metadata: {
|
|
contract: contractAddress,
|
|
queryType: 'social-metrics',
|
|
lastUpdated: new Date().toISOString(),
|
|
source: 'Dune Analytics'
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Get live Dune dashboard URL
|
|
router.get('/dune/dashboard-url', (req, res) => {
|
|
const dashboardUrl = process.env.DUNE_DASHBOARD_URL || 'https://dune.com/kaia-yield-ai/kaia-wave-hackathon';
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
dashboardUrl: dashboardUrl,
|
|
embedUrl: `${dashboardUrl}?embed=true`,
|
|
publicUrl: dashboardUrl,
|
|
apiStatus: !!process.env.DUNE_API_KEY ? 'configured' : 'not-configured'
|
|
}
|
|
});
|
|
});
|
|
|
|
// Initialize Dune queries for first-time setup
|
|
router.post('/dune/initialize', catchAsync(async (req, res) => {
|
|
if (process.env.NODE_ENV === 'production') {
|
|
return res.status(403).json({
|
|
success: false,
|
|
error: 'Query initialization not allowed in production'
|
|
});
|
|
}
|
|
|
|
const queries = await duneService.initializeQueries();
|
|
|
|
res.json({
|
|
success: true,
|
|
message: 'Dune Analytics queries ready for initialization',
|
|
data: {
|
|
queries: queries,
|
|
instructions: [
|
|
'1. Go to https://dune.com/browse/queries',
|
|
'2. Create a new query for each listed query type',
|
|
'3. Copy the SQL from /analytics/dune-dashboard.sql',
|
|
'4. Save each query and note the Query ID',
|
|
'5. Update your .env file with the DUNE_*_QUERY_ID variables',
|
|
'6. Set DUNE_API_KEY in your environment',
|
|
'7. Test the endpoints to verify integration'
|
|
],
|
|
nextSteps: {
|
|
dashboard: 'https://dune.com/create',
|
|
documentation: 'https://docs.dune.com/api-reference/',
|
|
support: 'https://discord.gg/dunecom'
|
|
}
|
|
}
|
|
});
|
|
}));
|
|
|
|
// Health check for Dune Analytics
|
|
router.get('/dune/health', (req, res) => {
|
|
const health = {
|
|
duneApiConfigured: !!process.env.DUNE_API_KEY,
|
|
contractAddressesConfigured: !!(process.env.YIELD_OPTIMIZER_ADDRESS && process.env.GAME_REWARDS_ADDRESS),
|
|
queryIdsConfigured: !!(
|
|
process.env.DUNE_PROTOCOL_OVERVIEW_QUERY_ID ||
|
|
process.env.DUNE_STRATEGY_PERFORMANCE_QUERY_ID ||
|
|
process.env.DUNE_USER_ENGAGEMENT_QUERY_ID
|
|
),
|
|
dashboardUrlConfigured: !!process.env.DUNE_DASHBOARD_URL
|
|
};
|
|
|
|
const status = Object.values(health).every(v => v) ? 'fully-configured' : 'needs-setup';
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
...health,
|
|
status: status,
|
|
fallbackMode: !health.queryIdsConfigured ? 'using-mock-data' : 'using-real-data',
|
|
setupInstructions: status !== 'fully-configured' ? [
|
|
'Set DUNE_API_KEY environment variable',
|
|
'Deploy contracts and set addresses in environment',
|
|
'Create Dune queries and set query IDs',
|
|
'Set DUNE_DASHBOARD_URL for public dashboard'
|
|
] : null
|
|
}
|
|
});
|
|
});
|
|
|
|
module.exports = router; |