mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-23 07:36:44 +00:00
35 lines
904 B
TypeScript
35 lines
904 B
TypeScript
import { Tool } from "langchain/tools";
|
|
import { SolanaAgentKit } from "../../agent";
|
|
|
|
/**
|
|
* Tool to fetch the price of a token in USDC
|
|
*/
|
|
export class SolanaFetchPriceTool extends Tool {
|
|
name = "solana_fetch_price";
|
|
description = `Fetch the price of a given token in USDC.
|
|
|
|
Inputs:
|
|
- tokenId: string, the mint address of the token, e.g., "JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN"`;
|
|
|
|
constructor(private solanaKit: SolanaAgentKit) {
|
|
super();
|
|
}
|
|
|
|
async _call(input: string): Promise<string> {
|
|
try {
|
|
const price = await this.solanaKit.fetchTokenPrice(input.trim());
|
|
return JSON.stringify({
|
|
status: "success",
|
|
tokenId: input.trim(),
|
|
priceInUSDC: price,
|
|
});
|
|
} catch (error: any) {
|
|
return JSON.stringify({
|
|
status: "error",
|
|
message: error.message,
|
|
code: error.code || "UNKNOWN_ERROR",
|
|
});
|
|
}
|
|
}
|
|
}
|