@agentek/tools
Version:
Blockchain tools for AI agents
60 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCryptoPriceTool = void 0;
const zod_1 = require("zod");
const client_js_1 = require("../client.js");
exports.getCryptoPriceTool = (0, client_js_1.createTool)({
name: "getCryptoPrice",
description: "Get the current price of a cryptocurrency in USD",
parameters: zod_1.z.object({
symbol: zod_1.z.string().describe("Cryptocurrency symbol (e.g., BTC, ETH, SOL) or CoinGecko ID (e.g., bitcoin, ethereum, solana) for better accuracy and wider asset support")
}),
execute: async (_client, args) => {
const { symbol } = args;
const normalizedSymbol = symbol.toUpperCase().trim();
try {
const response = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=${mapSymbolToId(normalizedSymbol)}&vs_currencies=usd`);
if (!response.ok) {
throw new Error(`CoinGecko API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
const id = mapSymbolToId(normalizedSymbol);
if (!data[id]) {
throw new Error(`Price data not found for ${normalizedSymbol}`);
}
return {
symbol: normalizedSymbol,
price: data[id].usd,
currency: "USD",
timestamp: new Date().toISOString(),
source: "CoinGecko"
};
}
catch (error) {
throw new Error(`Error fetching price for ${normalizedSymbol}: ${error instanceof Error ? error.message : String(error)}`);
}
}
});
// Map common symbols to CoinGecko IDs
function mapSymbolToId(symbol) {
const mapping = {
"BTC": "bitcoin",
"ETH": "ethereum",
"SOL": "solana",
"AVAX": "avalanche-2",
"MATIC": "matic-network",
"BNB": "binancecoin",
"DOT": "polkadot",
"ADA": "cardano",
"XRP": "ripple",
"DOGE": "dogecoin",
"SHIB": "shiba-inu",
"ARB": "arbitrum",
"OP": "optimism",
"LINK": "chainlink",
"UNI": "uniswap",
"AAVE": "aave",
};
return mapping[symbol] || symbol.toLowerCase();
}
//# sourceMappingURL=tools.js.map