@7kprotocol/sdk-ts 2.3.1 → 2.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -29,21 +29,49 @@ function getTokenPrice(id_1) {
|
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
+
const chunkArray = (array, chunkSize) => {
|
|
33
|
+
const chunks = [];
|
|
34
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
35
|
+
chunks.push(array.slice(i, i + chunkSize));
|
|
36
|
+
}
|
|
37
|
+
return chunks;
|
|
38
|
+
};
|
|
39
|
+
const MAX_TOTAL_IDS = 500;
|
|
40
|
+
const MAX_IDS_PER_REQUEST = 100;
|
|
32
41
|
function getTokenPrices(ids_1) {
|
|
33
42
|
return __awaiter(this, arguments, void 0, function* (ids, vsCoin = tokens_1.NATIVE_USDC_TOKEN_TYPE) {
|
|
34
43
|
try {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
const limitedIds = ids.slice(0, MAX_TOTAL_IDS).map(token_1.normalizeTokenType);
|
|
45
|
+
const idChunks = chunkArray(limitedIds, MAX_IDS_PER_REQUEST);
|
|
46
|
+
const responses = yield Promise.all(idChunks.map((chunk) => __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const response = yield fetch(`${exports.PRICES_API}/price`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
},
|
|
52
|
+
body: JSON.stringify({
|
|
53
|
+
ids: chunk,
|
|
54
|
+
vsCoin,
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
const pricesRes = (yield response.json());
|
|
58
|
+
return pricesRes;
|
|
59
|
+
})));
|
|
60
|
+
const combinedPrices = responses.reduce((acc, pricesRes) => {
|
|
61
|
+
Object.keys(pricesRes).forEach((id) => {
|
|
62
|
+
var _a;
|
|
63
|
+
acc[id] = Number(((_a = pricesRes[id]) === null || _a === void 0 ? void 0 : _a.price) || 0);
|
|
64
|
+
});
|
|
65
|
+
return acc;
|
|
66
|
+
}, {});
|
|
67
|
+
const finalPrices = limitedIds.reduce((acc, id) => {
|
|
68
|
+
acc[id] = combinedPrices[id] || 0;
|
|
41
69
|
return acc;
|
|
42
70
|
}, {});
|
|
43
|
-
return
|
|
71
|
+
return finalPrices;
|
|
44
72
|
}
|
|
45
73
|
catch (error) {
|
|
46
|
-
return ids.reduce((acc, id) => {
|
|
74
|
+
return ids.slice(0, MAX_TOTAL_IDS).reduce((acc, id) => {
|
|
47
75
|
acc[id] = 0;
|
|
48
76
|
return acc;
|
|
49
77
|
}, {});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/prices/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,yBAAyB,CAAC;AAOjD,wBAAsB,aAAa,CACjC,EAAE,EAAE,MAAM,EACV,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,CAUjB;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/prices/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,yBAAyB,CAAC;AAOjD,wBAAsB,aAAa,CACjC,EAAE,EAAE,MAAM,EACV,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,CAUjB;AAYD,wBAAsB,cAAc,CAClC,GAAG,EAAE,MAAM,EAAE,EACb,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAkDjC;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAEnD"}
|
|
@@ -11,19 +11,47 @@ export async function getTokenPrice(id, vsCoin = NATIVE_USDC_TOKEN_TYPE) {
|
|
|
11
11
|
return 0;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
const chunkArray = (array, chunkSize) => {
|
|
15
|
+
const chunks = [];
|
|
16
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
17
|
+
chunks.push(array.slice(i, i + chunkSize));
|
|
18
|
+
}
|
|
19
|
+
return chunks;
|
|
20
|
+
};
|
|
21
|
+
const MAX_TOTAL_IDS = 500;
|
|
22
|
+
const MAX_IDS_PER_REQUEST = 100;
|
|
14
23
|
export async function getTokenPrices(ids, vsCoin = NATIVE_USDC_TOKEN_TYPE) {
|
|
15
24
|
try {
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
const limitedIds = ids.slice(0, MAX_TOTAL_IDS).map(normalizeTokenType);
|
|
26
|
+
const idChunks = chunkArray(limitedIds, MAX_IDS_PER_REQUEST);
|
|
27
|
+
const responses = await Promise.all(idChunks.map(async (chunk) => {
|
|
28
|
+
const response = await fetch(`${PRICES_API}/price`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
ids: chunk,
|
|
35
|
+
vsCoin,
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
const pricesRes = (await response.json());
|
|
39
|
+
return pricesRes;
|
|
40
|
+
}));
|
|
41
|
+
const combinedPrices = responses.reduce((acc, pricesRes) => {
|
|
42
|
+
Object.keys(pricesRes).forEach((id) => {
|
|
43
|
+
acc[id] = Number(pricesRes[id]?.price || 0);
|
|
44
|
+
});
|
|
45
|
+
return acc;
|
|
46
|
+
}, {});
|
|
47
|
+
const finalPrices = limitedIds.reduce((acc, id) => {
|
|
48
|
+
acc[id] = combinedPrices[id] || 0;
|
|
21
49
|
return acc;
|
|
22
50
|
}, {});
|
|
23
|
-
return
|
|
51
|
+
return finalPrices;
|
|
24
52
|
}
|
|
25
53
|
catch (error) {
|
|
26
|
-
return ids.reduce((acc, id) => {
|
|
54
|
+
return ids.slice(0, MAX_TOTAL_IDS).reduce((acc, id) => {
|
|
27
55
|
acc[id] = 0;
|
|
28
56
|
return acc;
|
|
29
57
|
}, {});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/prices/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,yBAAyB,CAAC;AAOjD,wBAAsB,aAAa,CACjC,EAAE,EAAE,MAAM,EACV,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,CAUjB;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/features/prices/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,yBAAyB,CAAC;AAOjD,wBAAsB,aAAa,CACjC,EAAE,EAAE,MAAM,EACV,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,CAUjB;AAYD,wBAAsB,cAAc,CAClC,GAAG,EAAE,MAAM,EAAE,EACb,MAAM,SAAyB,GAC9B,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAkDjC;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAEnD"}
|