|
| 1 | +import tokenMetrics from "../../token_metrics.app.mjs"; |
| 2 | +import { ENDPOINTS, FILTER_DEFINITIONS } from "../../common/constants.mjs"; |
| 3 | +import { buildParams, generateFilterSummary } from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +const endpoint = ENDPOINTS.TOP_MARKET_CAP_TOKENS; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "token_metrics-get-top-market-cap-tokens", |
| 9 | + name: "Get Top Market Cap Tokens", |
| 10 | + description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/top-market-cap-tokens)`, |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + tokenMetrics, |
| 15 | + // Filter props based on endpoint configuration and API documentation |
| 16 | + topK: { |
| 17 | + ...FILTER_DEFINITIONS.top_k, |
| 18 | + description: "Specifies the number of top cryptocurrencies to retrieve, based on their market capitalization. Example: 100", |
| 19 | + }, |
| 20 | + }, |
| 21 | + async run({ $ }) { |
| 22 | + // Build parameters using utility function |
| 23 | + const params = buildParams(this, endpoint.filters); |
| 24 | + |
| 25 | + try { |
| 26 | + const response = await this.tokenMetrics.getTopMarketCapTokens({ |
| 27 | + $, |
| 28 | + params, |
| 29 | + }); |
| 30 | + |
| 31 | + // Generate summary using utility function |
| 32 | + const filterSummary = generateFilterSummary(this, endpoint.filters); |
| 33 | + |
| 34 | + // Use $ context for export |
| 35 | + if ($ && $.export) { |
| 36 | + const dataLength = response.data?.length || 0; |
| 37 | + $.export("$summary", `Successfully retrieved ${dataLength} top market cap tokens${filterSummary}`); |
| 38 | + } |
| 39 | + |
| 40 | + return response; |
| 41 | + } catch (error) { |
| 42 | + // Enhanced error handling |
| 43 | + const errorMessage = error.response?.data?.message || error.message || "An error occurred"; |
| 44 | + const statusCode = error.response?.status; |
| 45 | + |
| 46 | + if ($ && $.export) { |
| 47 | + $.export("$summary", `Error: ${errorMessage}`); |
| 48 | + } |
| 49 | + |
| 50 | + // Throw a more descriptive error |
| 51 | + throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`); |
| 52 | + } |
| 53 | + }, |
| 54 | +}; |
0 commit comments