/**
* Tool: Portfolio Analytics
* Provides comprehensive portfolio metrics and analytics
*/
async function portfolioAnalyticsTool(args, poolService, analyticsService) {
const { wallet } = args;
if (!wallet) {
throw new Error("Wallet address is required");
}
try {
// Get all positions
const positions = await poolService.getLPPositions(wallet);
if (positions.length === 0) {
return {
content: [
{
type: "text",
text: `No positions found for wallet: ${wallet}`,
},
],
};
}
// Analyze portfolio
const analytics = await analyticsService.analyzePortfolio(positions);
// Format analytics output
let analyticsText = `**Portfolio Analytics for ${wallet}**\n\n`;
analyticsText += `**Overview:**\n`;
analyticsText += `- Total Positions: ${analytics.positionCount}\n`;
analyticsText += `- Estimated Total Value: $${analytics.totalValue.toFixed(2)}\n`;
analyticsText += `- Average IL: ${analytics.averageIL.toFixed(2)}%\n\n`;
analyticsText += `**Position Breakdown:**\n`;
analytics.positions.forEach((pos, idx) => {
analyticsText +=
`\n${idx + 1}. Pool: ${pos.poolAddress.slice(0, 8)}...\n` +
` - LP Balance: ${pos.lpBalance}\n` +
` - Estimated Value: $${pos.estimatedValue.toFixed(2)}\n` +
` - IL: ${pos.impermanentLoss.toFixed(2)}%\n`;
});
analyticsText += `\n**Risk Assessment:**\n`;
const avgIL = Math.abs(analytics.averageIL);
if (avgIL < 2) {
analyticsText += `✅ Low Risk - Portfolio is performing well`;
} else if (avgIL < 5) {
analyticsText += `⚠️ Medium Risk - Monitor positions closely`;
} else {
analyticsText += `🔴 High Risk - Consider rebalancing`;
}
return {
content: [
{
type: "text",
text: analyticsText,
},
],
};
} catch (error) {
throw new Error(`Failed to get portfolio analytics: ${error.message}`);
}
}
module.exports = { portfolioAnalyticsTool };