58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
![]() |
import { tool } from 'ai';
|
||
|
import { z } from "zod";
|
||
|
import { queryBirdeye } from "./client.js";
|
||
|
|
||
|
|
||
|
export const getCurrentPrices = async ( addressArray ) => {
|
||
|
const params = {
|
||
|
list_address: addressArray.join(","),
|
||
|
chain: "solana"
|
||
|
};
|
||
|
|
||
|
const response = await queryBirdeye("defi/multi_price", params);
|
||
|
|
||
|
const responseObj = Object.entries(response);
|
||
|
const result = {};
|
||
|
|
||
|
if (responseObj.length > 0) {
|
||
|
for (const [key, value] of responseObj) {
|
||
|
if(value != null && value.value && value.priceChange24h){
|
||
|
result[key] = {
|
||
|
value: value.value,
|
||
|
priceChange24h: value.priceChange24h,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
|
||
|
export const getCurrentPricesTool = (userId, chatId, responseId, dataStream) => tool({
|
||
|
description: "Retrieve the latest prices for a list of token addresses on the Solana blockchain.",
|
||
|
parameters: z.object({
|
||
|
addressArray: z.array(z.string()).describe("List of token addresses to fetch prices for"),
|
||
|
}),
|
||
|
execute: async ({ addressArray }) => {
|
||
|
try {
|
||
|
console.log(`${userId} ${chatId} called getCurrentPrices with addressArray: ${addressArray}`);
|
||
|
const prices = await getCurrentPrices(addressArray);
|
||
|
|
||
|
if (prices) {
|
||
|
addressArray.forEach(address => {
|
||
|
dataStream.writeMessageAnnotation({
|
||
|
id: responseId,
|
||
|
tool_type: 'chart',
|
||
|
content: address,
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return JSON.stringify(prices);
|
||
|
} catch (error) {
|
||
|
console.error(`Error executing getCurrentPrices for addressArray ${addressArray}:`, error.message);
|
||
|
return `Failed to fetch prices for addresses: ${addressArray.join(", ")}.`;
|
||
|
}
|
||
|
},
|
||
|
});
|