|
| 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.QUANTMETRICS; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "token_metrics-get-quantmetrics", |
| 9 | + name: "Get Quantmetrics", |
| 10 | + description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/quantmetrics)`, |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + tokenMetrics, |
| 15 | + // Filter props based on endpoint configuration and API documentation |
| 16 | + tokenId: { |
| 17 | + ...FILTER_DEFINITIONS.token_id, |
| 18 | + description: "Comma Separated Token IDs. Click here to access the list of token IDs. Example: 3375,3306", |
| 19 | + }, |
| 20 | + symbol: { |
| 21 | + ...FILTER_DEFINITIONS.symbol, |
| 22 | + description: "Comma Separated Token Symbols. Click here to access the list of token symbols. Example: BTC,ETH", |
| 23 | + }, |
| 24 | + category: { |
| 25 | + ...FILTER_DEFINITIONS.category, |
| 26 | + description: "Comma separated category name. Click here to access the list of categories. Example: layer-1,nft", |
| 27 | + }, |
| 28 | + exchange: { |
| 29 | + ...FILTER_DEFINITIONS.exchange, |
| 30 | + description: "Comma separated exchange name. Click here to access the list of exchanges. Example: binance,gate", |
| 31 | + }, |
| 32 | + marketCap: { |
| 33 | + ...FILTER_DEFINITIONS.market_cap, |
| 34 | + description: "Minimum MarketCap in $. Example: 1000000000", |
| 35 | + }, |
| 36 | + volume: { |
| 37 | + ...FILTER_DEFINITIONS.volume, |
| 38 | + description: "Minimum 24h trading volume in $. Example: 1000000000", |
| 39 | + }, |
| 40 | + fdv: { |
| 41 | + ...FILTER_DEFINITIONS.fdv, |
| 42 | + description: "Minimum fully diluted valuation in $. Example: 1000000000", |
| 43 | + }, |
| 44 | + // Pagination props |
| 45 | + limit: { |
| 46 | + propDefinition: [ |
| 47 | + tokenMetrics, |
| 48 | + "limit", |
| 49 | + ], |
| 50 | + description: "Limit the number of items in response. Defaults to 50", |
| 51 | + default: 50, |
| 52 | + }, |
| 53 | + page: { |
| 54 | + propDefinition: [ |
| 55 | + tokenMetrics, |
| 56 | + "page", |
| 57 | + ], |
| 58 | + description: "Enables pagination and data retrieval control by skipping a specified number of items before fetching data. Page should be a non-negative integer, with 1 indicating the beginning of the dataset. Defaults to 1", |
| 59 | + default: 1, |
| 60 | + }, |
| 61 | + }, |
| 62 | + async run({ $ }) { |
| 63 | + // Build parameters using utility function |
| 64 | + const params = buildParams(this, endpoint.filters); |
| 65 | + |
| 66 | + try { |
| 67 | + const response = await this.tokenMetrics.getQuantmetrics({ |
| 68 | + $, |
| 69 | + params, |
| 70 | + }); |
| 71 | + |
| 72 | + // Generate summary using utility function |
| 73 | + const filterSummary = generateFilterSummary(this, endpoint.filters); |
| 74 | + |
| 75 | + // Use $ context for export |
| 76 | + if ($ && $.export) { |
| 77 | + const dataLength = response.data?.length || 0; |
| 78 | + $.export("$summary", `Successfully retrieved quantmetrics for ${dataLength} tokens${filterSummary}`); |
| 79 | + } |
| 80 | + |
| 81 | + return response; |
| 82 | + } catch (error) { |
| 83 | + // Enhanced error handling |
| 84 | + const errorMessage = error.response?.data?.message || error.message || "An error occurred"; |
| 85 | + const statusCode = error.response?.status; |
| 86 | + |
| 87 | + if ($ && $.export) { |
| 88 | + $.export("$summary", `Error: ${errorMessage}`); |
| 89 | + } |
| 90 | + |
| 91 | + // Throw a more descriptive error |
| 92 | + throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`); |
| 93 | + } |
| 94 | + }, |
| 95 | +}; |
0 commit comments