@talismn/token-rates 0.1.9 → 0.1.11
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.
- package/CHANGELOG.md +14 -0
- package/dist/TokenRates.d.ts +2 -2
- package/dist/TokenRates.js +44 -55
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# @talismn/token-rates
|
2
2
|
|
3
|
+
## 0.1.11
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- @talismn/chaindata-provider@0.2.0
|
8
|
+
|
9
|
+
## 0.1.10
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- fix: a variety of improvements from the wallet integration
|
14
|
+
- Updated dependencies
|
15
|
+
- @talismn/chaindata-provider@0.1.10
|
16
|
+
|
3
17
|
## 0.1.9
|
4
18
|
|
5
19
|
### Patch Changes
|
package/dist/TokenRates.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import {
|
1
|
+
import { IToken, TokenList } from "@talismn/chaindata-provider";
|
2
2
|
import { TokenRatesList } from "./types";
|
3
3
|
export declare function fetchTokenRates(tokens: TokenList): Promise<TokenRatesList>;
|
4
4
|
export interface WithCoingeckoId {
|
5
5
|
coingeckoId: string;
|
6
6
|
}
|
7
|
-
export declare function hasCoingeckoId(token:
|
7
|
+
export declare function hasCoingeckoId(token: IToken): token is IToken & WithCoingeckoId;
|
package/dist/TokenRates.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
4
|
};
|
@@ -35,52 +26,50 @@ const coingeckoCurrencies = [
|
|
35
26
|
// 'dot',
|
36
27
|
];
|
37
28
|
// export function tokenRates(tokens: WithCoingeckoId[]): TokenRatesList {}
|
38
|
-
function fetchTokenRates(tokens) {
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
const
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
return ratesList;
|
83
|
-
});
|
29
|
+
async function fetchTokenRates(tokens) {
|
30
|
+
// create a map from `coingeckoId` -> `tokenId` for each token
|
31
|
+
const coingeckoIdToTokenIds = Object.values(tokens)
|
32
|
+
// ignore testnet tokens
|
33
|
+
.filter(({ isTestnet }) => !isTestnet)
|
34
|
+
// ignore tokens which don't have a coingeckoId
|
35
|
+
.filter(hasCoingeckoId)
|
36
|
+
// get each token's coingeckoId
|
37
|
+
.reduce((coingeckoIdToTokenIds, { id, coingeckoId }) => {
|
38
|
+
if (!coingeckoIdToTokenIds[coingeckoId])
|
39
|
+
coingeckoIdToTokenIds[coingeckoId] = [];
|
40
|
+
coingeckoIdToTokenIds[coingeckoId].push(id);
|
41
|
+
return coingeckoIdToTokenIds;
|
42
|
+
}, {});
|
43
|
+
// create a list of coingeckoIds we want to fetch
|
44
|
+
const coingeckoIds = Object.keys(coingeckoIdToTokenIds);
|
45
|
+
// skip network request if there is nothing for us to fetch
|
46
|
+
if (coingeckoIds.length < 1)
|
47
|
+
return {};
|
48
|
+
// construct a coingecko request
|
49
|
+
const idsSerialized = coingeckoIds.join(",");
|
50
|
+
const currenciesSerialized = coingeckoCurrencies.join(",");
|
51
|
+
const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
|
52
|
+
// fetch the token prices from coingecko
|
53
|
+
// the response should be in the format:
|
54
|
+
// {
|
55
|
+
// [coingeckoId]: {
|
56
|
+
// [currency]: rate
|
57
|
+
// }
|
58
|
+
// }
|
59
|
+
const coingeckoPrices = await axios_1.default
|
60
|
+
.get(queryUrl)
|
61
|
+
.then((response) => response.data);
|
62
|
+
// build a TokenRatesList from the token prices result
|
63
|
+
const ratesList = Object.fromEntries(coingeckoIds.flatMap((coingeckoId) => {
|
64
|
+
const tokenIds = coingeckoIdToTokenIds[coingeckoId];
|
65
|
+
const rates = (0, types_1.NewTokenRates)();
|
66
|
+
for (const currency of coingeckoCurrencies) {
|
67
|
+
rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
|
68
|
+
}
|
69
|
+
return tokenIds.map((tokenId) => [tokenId, rates]);
|
70
|
+
}));
|
71
|
+
// return the TokenRatesList
|
72
|
+
return ratesList;
|
84
73
|
}
|
85
74
|
exports.fetchTokenRates = fetchTokenRates;
|
86
75
|
function hasCoingeckoId(token) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@talismn/token-rates",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.11",
|
4
4
|
"author": "Talisman",
|
5
5
|
"homepage": "https://talisman.xyz",
|
6
6
|
"license": "UNLICENSED",
|
@@ -30,7 +30,7 @@
|
|
30
30
|
"clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
|
31
31
|
},
|
32
32
|
"dependencies": {
|
33
|
-
"@talismn/chaindata-provider": "^0.
|
33
|
+
"@talismn/chaindata-provider": "^0.2.0",
|
34
34
|
"axios": "^0.27.2"
|
35
35
|
},
|
36
36
|
"devDependencies": {
|