solana-multiagent/tools/birdeye/getTrendingTokens.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-02-17 15:21:20 +07:00
import { tool } from 'ai';
import { z } from "zod";
import { queryBirdeye } from "./client.js";
const getTrendingTokens = async (offset = 0, limit = 10) => {
limit = Math.min(limit, 20);
const result = await queryBirdeye("defi/token_trending", {
sort_by: "rank",
sort_type: "asc",
offset,
limit
});
return result.tokens
};
export const getTrendingTokensTool = (userId, chatId, responseId, dataStream) => tool({
description: "Retrieve current trending Solana tokens, with options for pagination. Use this if someone asks you for recommendations on a trending meme coin.",
parameters: z.object({
offset: z.number().optional().describe("Offset for pagination"),
limit: z
.number()
.optional()
.default(10)
.describe("Limit for the number of results (max: 20)"),
}),
execute: async ({ offset, limit }) => {
try {
console.log(`${userId} ${chatId} called getTrendingTokens with offset: ${offset} | limit: ${limit}`);
const tokens = await getTrendingTokens(offset, limit);
if (tokens.length === 0) {
return "No trending tokens found.";
}
if (tokens.length > 0 && tokens[0].address) {
tokens.map(token => {
dataStream.writeMessageAnnotation({
id: responseId,
tool_type: 'chart',
content: token.address.toString(),
});
});
}
return JSON.stringify(tokens);
} catch (error) {
console.error("Error fetching trending tokens:", error.message);
return "An error occurred while fetching trending tokens.";
}
},
});