59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
import { tool } from 'ai';
|
|
import { z } from "zod";
|
|
import { queryBirdeye } from "./client.js";
|
|
|
|
|
|
const getPortfolio = async ( walletAddress ) => {
|
|
const params = {
|
|
wallet: walletAddress,
|
|
};
|
|
|
|
const response = await queryBirdeye("v1/wallet/token_list", params);
|
|
|
|
const top20Items = response.items
|
|
.sort((a, b) => b.valueUsd - a.valueUsd)
|
|
.slice(0, 20)
|
|
.map(item => ({
|
|
name: item.name,
|
|
symbol: item.symbol,
|
|
logoURI: item.logoURI,
|
|
priceUsd: item.priceUsd,
|
|
valueUsd: item.valueUsd,
|
|
}));
|
|
|
|
return {
|
|
wallet: walletAddress,
|
|
totalUsd: response.totalUsd,
|
|
items: top20Items,
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export const getPortfolioTool = (userId, chatId,responseId, dataStream) => tool({
|
|
description: "Fetch the top 20 holdings of a Solana wallet, including token balances and USD price.",
|
|
parameters: z.object({
|
|
walletAddress: z.string().describe("Solana wallet address to fetch portfolio data for"),
|
|
}),
|
|
execute: async ({ walletAddress }) => {
|
|
try {
|
|
console.log(`${userId} ${chatId} called getPortfolio with walletAddress: ${walletAddress}`);
|
|
const portfolio = await getPortfolio(walletAddress);
|
|
|
|
if (portfolio) {
|
|
dataStream.writeMessageAnnotation({
|
|
id: responseId,
|
|
tool_type: 'portfolio',
|
|
content: walletAddress,
|
|
});
|
|
|
|
}
|
|
|
|
return JSON.stringify(portfolio);
|
|
} catch (error) {
|
|
console.error(`Error executing getPortfolio for walletAddress ${walletAddress}:`, error.message);
|
|
return `Failed to fetch portfolio for wallet address: ${walletAddress}.`;
|
|
}
|
|
},
|
|
});
|