@talismn/token-rates 2.0.2 → 2.0.4

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.
@@ -12,6 +12,10 @@ export declare const SUPPORTED_CURRENCIES: {
12
12
  readonly name: "Polkadot";
13
13
  readonly symbol: "D";
14
14
  };
15
+ readonly tao: {
16
+ readonly name: "Bittensor";
17
+ readonly symbol: "τ";
18
+ };
15
19
  readonly usd: {
16
20
  readonly name: "US Dollar";
17
21
  readonly symbol: "$";
@@ -33,6 +33,10 @@ const SUPPORTED_CURRENCIES = {
33
33
  name: "Polkadot",
34
34
  symbol: "D"
35
35
  },
36
+ tao: {
37
+ name: "Bittensor",
38
+ symbol: "τ"
39
+ },
36
40
  usd: {
37
41
  name: "US Dollar",
38
42
  symbol: "$"
@@ -108,6 +112,7 @@ const newTokenRates = () => ({
108
112
  btc: null,
109
113
  eth: null,
110
114
  dot: null,
115
+ tao: null,
111
116
  usd: null,
112
117
  cny: null,
113
118
  eur: null,
@@ -184,14 +189,44 @@ async function fetchTokenRates(tokens, currencyIds = ALL_CURRENCY_IDS, config =
184
189
 
185
190
  // skip network request if there is nothing for us to fetch
186
191
  if (coingeckoIds.length < 1) return {};
192
+
193
+ // If `currencyIds` includes `tao`, we need to always fetch the `bittensor` coingeckoId and the `usd` currency,
194
+ // we can use these to calculate the currency rate for TAO relative to all other tokens.
195
+ //
196
+ // We support showing balances in TAO just like we support BTC/ETH/DOT, but coingecko doesn't support TAO as a vs currency rate.
197
+ // We can macgyver our own TOKEN<>TAO rate by combining the TOKEN<>USD data with the TAO<>USD data.
198
+ const hasVsTao = currencyIds.includes("tao");
199
+ const [effectiveCoingeckoIds, effectiveCurrencyIds] = hasVsTao ? [[...new Set(coingeckoIds).add("bittensor")], [...new Set(
200
+ // don't request `tao` from coingecko (we calculate it from `usd`)
201
+ currencyIds.filter(c => c !== "tao"))
202
+ // always include `usd` (so we can calculate `tao`)
203
+ .add("usd")]] : [coingeckoIds, currencyIds];
187
204
  const response = await fetch(`${config.apiUrl}/token-rates`, {
188
205
  method: "POST",
189
206
  body: JSON.stringify({
190
- coingeckoIds,
191
- currencyIds
207
+ coingeckoIds: effectiveCoingeckoIds,
208
+ currencyIds: effectiveCurrencyIds
192
209
  })
193
210
  });
194
211
  const rawTokenRates = await response.json();
212
+ if (hasVsTao) {
213
+ // calculate the TAO<>USD rate
214
+ const effectiveTaoIndex = effectiveCoingeckoIds.indexOf("bittensor");
215
+ const effectiveUsdIndex = effectiveCurrencyIds.indexOf("usd");
216
+ const taoUsdRate = rawTokenRates[effectiveTaoIndex]?.[effectiveUsdIndex]?.[0];
217
+
218
+ // insert TOKEN<>TAO rate (calculated based on TAO<>USD rate and TOKEN<>USD rate) into each TOKEN
219
+ const taoIndex = currencyIds.indexOf("tao");
220
+ rawTokenRates.forEach(rates => {
221
+ // get TOKEN<>USD rate
222
+ const usdRate = rates?.[effectiveUsdIndex]?.[0];
223
+ // calculate TOKEN<>TAO rate
224
+ const taoRate = usdRate !== null && taoUsdRate !== null && taoUsdRate !== 0 ? usdRate / taoUsdRate : null;
225
+
226
+ // insert at the correct location (based on the index of `tao` in `currencyIds`)
227
+ rates?.splice(taoIndex, 0, [taoRate, null, null]);
228
+ });
229
+ }
195
230
  const tokenRates = parseTokenRatesFromApi(rawTokenRates, coingeckoIds, currencyIds);
196
231
 
197
232
  // build a TokenRatesList from the token prices result
@@ -33,6 +33,10 @@ const SUPPORTED_CURRENCIES = {
33
33
  name: "Polkadot",
34
34
  symbol: "D"
35
35
  },
36
+ tao: {
37
+ name: "Bittensor",
38
+ symbol: "τ"
39
+ },
36
40
  usd: {
37
41
  name: "US Dollar",
38
42
  symbol: "$"
@@ -108,6 +112,7 @@ const newTokenRates = () => ({
108
112
  btc: null,
109
113
  eth: null,
110
114
  dot: null,
115
+ tao: null,
111
116
  usd: null,
112
117
  cny: null,
113
118
  eur: null,
@@ -184,14 +189,44 @@ async function fetchTokenRates(tokens, currencyIds = ALL_CURRENCY_IDS, config =
184
189
 
185
190
  // skip network request if there is nothing for us to fetch
186
191
  if (coingeckoIds.length < 1) return {};
192
+
193
+ // If `currencyIds` includes `tao`, we need to always fetch the `bittensor` coingeckoId and the `usd` currency,
194
+ // we can use these to calculate the currency rate for TAO relative to all other tokens.
195
+ //
196
+ // We support showing balances in TAO just like we support BTC/ETH/DOT, but coingecko doesn't support TAO as a vs currency rate.
197
+ // We can macgyver our own TOKEN<>TAO rate by combining the TOKEN<>USD data with the TAO<>USD data.
198
+ const hasVsTao = currencyIds.includes("tao");
199
+ const [effectiveCoingeckoIds, effectiveCurrencyIds] = hasVsTao ? [[...new Set(coingeckoIds).add("bittensor")], [...new Set(
200
+ // don't request `tao` from coingecko (we calculate it from `usd`)
201
+ currencyIds.filter(c => c !== "tao"))
202
+ // always include `usd` (so we can calculate `tao`)
203
+ .add("usd")]] : [coingeckoIds, currencyIds];
187
204
  const response = await fetch(`${config.apiUrl}/token-rates`, {
188
205
  method: "POST",
189
206
  body: JSON.stringify({
190
- coingeckoIds,
191
- currencyIds
207
+ coingeckoIds: effectiveCoingeckoIds,
208
+ currencyIds: effectiveCurrencyIds
192
209
  })
193
210
  });
194
211
  const rawTokenRates = await response.json();
212
+ if (hasVsTao) {
213
+ // calculate the TAO<>USD rate
214
+ const effectiveTaoIndex = effectiveCoingeckoIds.indexOf("bittensor");
215
+ const effectiveUsdIndex = effectiveCurrencyIds.indexOf("usd");
216
+ const taoUsdRate = rawTokenRates[effectiveTaoIndex]?.[effectiveUsdIndex]?.[0];
217
+
218
+ // insert TOKEN<>TAO rate (calculated based on TAO<>USD rate and TOKEN<>USD rate) into each TOKEN
219
+ const taoIndex = currencyIds.indexOf("tao");
220
+ rawTokenRates.forEach(rates => {
221
+ // get TOKEN<>USD rate
222
+ const usdRate = rates?.[effectiveUsdIndex]?.[0];
223
+ // calculate TOKEN<>TAO rate
224
+ const taoRate = usdRate !== null && taoUsdRate !== null && taoUsdRate !== 0 ? usdRate / taoUsdRate : null;
225
+
226
+ // insert at the correct location (based on the index of `tao` in `currencyIds`)
227
+ rates?.splice(taoIndex, 0, [taoRate, null, null]);
228
+ });
229
+ }
195
230
  const tokenRates = parseTokenRatesFromApi(rawTokenRates, coingeckoIds, currencyIds);
196
231
 
197
232
  // build a TokenRatesList from the token prices result
@@ -31,6 +31,10 @@ const SUPPORTED_CURRENCIES = {
31
31
  name: "Polkadot",
32
32
  symbol: "D"
33
33
  },
34
+ tao: {
35
+ name: "Bittensor",
36
+ symbol: "τ"
37
+ },
34
38
  usd: {
35
39
  name: "US Dollar",
36
40
  symbol: "$"
@@ -106,6 +110,7 @@ const newTokenRates = () => ({
106
110
  btc: null,
107
111
  eth: null,
108
112
  dot: null,
113
+ tao: null,
109
114
  usd: null,
110
115
  cny: null,
111
116
  eur: null,
@@ -182,14 +187,44 @@ async function fetchTokenRates(tokens, currencyIds = ALL_CURRENCY_IDS, config =
182
187
 
183
188
  // skip network request if there is nothing for us to fetch
184
189
  if (coingeckoIds.length < 1) return {};
190
+
191
+ // If `currencyIds` includes `tao`, we need to always fetch the `bittensor` coingeckoId and the `usd` currency,
192
+ // we can use these to calculate the currency rate for TAO relative to all other tokens.
193
+ //
194
+ // We support showing balances in TAO just like we support BTC/ETH/DOT, but coingecko doesn't support TAO as a vs currency rate.
195
+ // We can macgyver our own TOKEN<>TAO rate by combining the TOKEN<>USD data with the TAO<>USD data.
196
+ const hasVsTao = currencyIds.includes("tao");
197
+ const [effectiveCoingeckoIds, effectiveCurrencyIds] = hasVsTao ? [[...new Set(coingeckoIds).add("bittensor")], [...new Set(
198
+ // don't request `tao` from coingecko (we calculate it from `usd`)
199
+ currencyIds.filter(c => c !== "tao"))
200
+ // always include `usd` (so we can calculate `tao`)
201
+ .add("usd")]] : [coingeckoIds, currencyIds];
185
202
  const response = await fetch(`${config.apiUrl}/token-rates`, {
186
203
  method: "POST",
187
204
  body: JSON.stringify({
188
- coingeckoIds,
189
- currencyIds
205
+ coingeckoIds: effectiveCoingeckoIds,
206
+ currencyIds: effectiveCurrencyIds
190
207
  })
191
208
  });
192
209
  const rawTokenRates = await response.json();
210
+ if (hasVsTao) {
211
+ // calculate the TAO<>USD rate
212
+ const effectiveTaoIndex = effectiveCoingeckoIds.indexOf("bittensor");
213
+ const effectiveUsdIndex = effectiveCurrencyIds.indexOf("usd");
214
+ const taoUsdRate = rawTokenRates[effectiveTaoIndex]?.[effectiveUsdIndex]?.[0];
215
+
216
+ // insert TOKEN<>TAO rate (calculated based on TAO<>USD rate and TOKEN<>USD rate) into each TOKEN
217
+ const taoIndex = currencyIds.indexOf("tao");
218
+ rawTokenRates.forEach(rates => {
219
+ // get TOKEN<>USD rate
220
+ const usdRate = rates?.[effectiveUsdIndex]?.[0];
221
+ // calculate TOKEN<>TAO rate
222
+ const taoRate = usdRate !== null && taoUsdRate !== null && taoUsdRate !== 0 ? usdRate / taoUsdRate : null;
223
+
224
+ // insert at the correct location (based on the index of `tao` in `currencyIds`)
225
+ rates?.splice(taoIndex, 0, [taoRate, null, null]);
226
+ });
227
+ }
193
228
  const tokenRates = parseTokenRatesFromApi(rawTokenRates, coingeckoIds, currencyIds);
194
229
 
195
230
  // build a TokenRatesList from the token prices result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/token-rates",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "dexie": "^4.0.9",
25
- "@talismn/chaindata-provider": "0.10.1"
25
+ "@talismn/chaindata-provider": "0.10.3"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/jest": "^29.5.14",