Skip to content

Commit cd8b855

Browse files
added top market cap tokens endpoint action
1 parent d3ddaea commit cd8b855

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
};

components/token_metrics/common/constants.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ export const ENDPOINTS = {
145145
description: "Get the latest list of crypto investors and their scores",
146146
filters: [],
147147
},
148+
TOP_MARKET_CAP_TOKENS: {
149+
path: "/top-market-cap-tokens",
150+
description: "Get the list of coins for top market cap",
151+
filters: [
152+
"top_k",
153+
],
154+
},
148155
};
149156

150157
// Common filter definitions that can be reused across endpoints
@@ -252,6 +259,12 @@ export const FILTER_DEFINITIONS = {
252259
],
253260
default: "active",
254261
},
262+
top_k: {
263+
type: "integer",
264+
label: "Top K",
265+
description: "Specifies the number of top cryptocurrencies to retrieve, based on their market capitalization. Example: 100",
266+
optional: true,
267+
},
255268
};
256269

257270
// Common error messages

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,15 @@ export default {
211211
params,
212212
});
213213
},
214+
async getTopMarketCapTokens({
215+
$ = this,
216+
params = {},
217+
}) {
218+
return this.makeApiCall({
219+
$,
220+
endpoint: "/top-market-cap-tokens",
221+
params,
222+
});
223+
},
214224
},
215225
};

0 commit comments

Comments
 (0)