Skip to content

Commit 9868b59

Browse files
added correlation tokens endpoint action
1 parent 0f5997a commit 9868b59

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.CORRELATION;
6+
7+
export default {
8+
key: "token_metrics-get-correlation",
9+
name: "Get Correlation",
10+
description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/correlation)`,
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: gate,binance",
31+
},
32+
// Pagination props
33+
limit: {
34+
propDefinition: [
35+
tokenMetrics,
36+
"limit",
37+
],
38+
description: "Limit the number of items in response. Defaults to 50",
39+
default: 50,
40+
},
41+
page: {
42+
propDefinition: [
43+
tokenMetrics,
44+
"page",
45+
],
46+
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",
47+
default: 1,
48+
},
49+
},
50+
async run({ $ }) {
51+
// Build parameters using utility function
52+
const params = buildParams(this, endpoint.filters);
53+
54+
try {
55+
const response = await this.tokenMetrics.getCorrelation({
56+
$,
57+
params,
58+
});
59+
60+
// Generate summary using utility function
61+
const filterSummary = generateFilterSummary(this, endpoint.filters);
62+
63+
// Use $ context for export
64+
if ($ && $.export) {
65+
const dataLength = response.data?.length || 0;
66+
$.export("$summary", `Successfully retrieved correlation data for ${dataLength} tokens${filterSummary}`);
67+
}
68+
69+
return response;
70+
} catch (error) {
71+
// Enhanced error handling
72+
const errorMessage = error.response?.data?.message || error.message || "An error occurred";
73+
const statusCode = error.response?.status;
74+
75+
if ($ && $.export) {
76+
$.export("$summary", `Error: ${errorMessage}`);
77+
}
78+
79+
// Throw a more descriptive error
80+
throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`);
81+
}
82+
},
83+
};

components/token_metrics/common/constants.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ export const ENDPOINTS = {
188188
"symbol",
189189
],
190190
},
191+
CORRELATION: {
192+
path: "/correlation",
193+
description: "Get the Top 10 and Bottom 10 correlation of tokens with the top 100 market cap tokens",
194+
filters: [
195+
"token_id",
196+
"symbol",
197+
"category",
198+
"exchange",
199+
],
200+
},
191201
};
192202

193203
// Common filter definitions that can be reused across endpoints

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,15 @@ export default {
261261
params,
262262
});
263263
},
264+
async getCorrelation({
265+
$ = this,
266+
params = {},
267+
}) {
268+
return this.makeApiCall({
269+
$,
270+
endpoint: "/correlation",
271+
params,
272+
});
273+
},
264274
},
265275
};

0 commit comments

Comments
 (0)