@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 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
@@ -1,7 +1,7 @@
1
- import { Token, TokenList } from "@talismn/chaindata-provider";
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: Token): token is Token & WithCoingeckoId;
7
+ export declare function hasCoingeckoId(token: IToken): token is IToken & WithCoingeckoId;
@@ -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
- return __awaiter(this, void 0, void 0, function* () {
40
- // create a map from `coingeckoId` -> `tokenId` for each token
41
- const coingeckoIdToTokenIds = Object.values(tokens)
42
- // ignore testnet tokens
43
- .filter(({ isTestnet }) => !isTestnet)
44
- // ignore tokens which don't have a coingeckoId
45
- .filter(hasCoingeckoId)
46
- // get each token's coingeckoId
47
- .reduce((coingeckoIdToTokenIds, { id, coingeckoId }) => {
48
- if (!coingeckoIdToTokenIds[coingeckoId])
49
- coingeckoIdToTokenIds[coingeckoId] = [];
50
- coingeckoIdToTokenIds[coingeckoId].push(id);
51
- return coingeckoIdToTokenIds;
52
- }, {});
53
- // create a list of coingeckoIds we want to fetch
54
- const coingeckoIds = Object.keys(coingeckoIdToTokenIds);
55
- // skip network request if there is nothing for us to fetch
56
- if (coingeckoIds.length < 1)
57
- return {};
58
- // construct a coingecko request
59
- const idsSerialized = coingeckoIds.join(",");
60
- const currenciesSerialized = coingeckoCurrencies.join(",");
61
- const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
62
- // fetch the token prices from coingecko
63
- // the response should be in the format:
64
- // {
65
- // [coingeckoId]: {
66
- // [currency]: rate
67
- // }
68
- // }
69
- const coingeckoPrices = yield axios_1.default
70
- .get(queryUrl)
71
- .then((response) => response.data);
72
- // build a TokenRatesList from the token prices result
73
- const ratesList = Object.fromEntries(coingeckoIds.flatMap((coingeckoId) => {
74
- const tokenIds = coingeckoIdToTokenIds[coingeckoId];
75
- const rates = (0, types_1.NewTokenRates)();
76
- for (const currency of coingeckoCurrencies) {
77
- rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
78
- }
79
- return tokenIds.map((tokenId) => [tokenId, rates]);
80
- }));
81
- // return the TokenRatesList
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.9",
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.1.9",
33
+ "@talismn/chaindata-provider": "^0.2.0",
34
34
  "axios": "^0.27.2"
35
35
  },
36
36
  "devDependencies": {