UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

70 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMarketEventsTool = void 0; const zod_1 = require("zod"); const client_js_1 = require("../client.js"); const getMarketEventsParams = zod_1.z.object({ showOnly: zod_1.z .enum([ "trending_events", "popular_events", "firmed_date", "confirmed_by_representatives", ]) .optional() .describe("Filter events by category: 'trending_events', 'popular_events', 'firmed_date', or 'confirmed_by_representatives'. Omit to return all events."), }); const BASE_URL = "https://developers.coinmarketcal.com/v1"; const createMarketEventsTool = (apiKey) => { return (0, client_js_1.createTool)({ name: "getMarketEvents", description: "Fetches upcoming cryptocurrency market events from CoinMarketCal (e.g. token launches, airdrops, listings, forks). Optionally filter by event category. Returns up to 50 events with dates, coins, proof links, and community votes.", parameters: getMarketEventsParams, execute: async (_client, args) => { const url = new URL(`${BASE_URL}/events`); Object.entries(args).forEach(([key, value]) => { if (value !== undefined) { url.searchParams.append(key, value.toString()); } }); url.searchParams.append("lang", "en"); url.searchParams.append("showViews", "true"); url.searchParams.append("showVotes", "true"); url.searchParams.append("max", "50"); // hardcoded, allow choice ? const options = { method: "GET", headers: { Accept: "application/json", "Accept-Encoding": "deflate, gzip", "x-api-key": apiKey, }, }; try { const response = await fetch(url.toString(), options); if (!response.ok) { switch (response.status) { case 400: throw new Error("Bad Request: Invalid parameters"); case 403: throw new Error("Invalid API key"); case 429: throw new Error("Quota exceeded or too many requests"); default: throw new Error(`API error: ${response.status} ${response.statusText}`); } } const json = (await response.json()).body; const filteredEvents = json.map((event) => { const { vote_history, view_history, ...rest } = event; return rest; }); return filteredEvents; } catch (err) { throw new Error(`Failed to fetch market events: ${err instanceof Error ? err.message : "Unknown error"}`); } }, }); }; exports.createMarketEventsTool = createMarketEventsTool; //# sourceMappingURL=tools.js.map