@ecency/wallets 1.4.7 → 1.4.8

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.
@@ -229,55 +229,67 @@ var cacheGet = (key) => {
229
229
  const v = cache.get(key);
230
230
  return v === undefinedValue ? void 0 : v;
231
231
  };
232
- function getCoinGeckoPriceQueryOptions(currency) {
232
+ var CURRENCY_TO_TOKEN_MAP = {
233
+ ["BTC" /* BTC */]: "btc",
234
+ ["ETH" /* ETH */]: "eth",
235
+ ["SOL" /* SOL */]: "sol",
236
+ ["TON" /* TON */]: "ton",
237
+ ["TRX" /* TRON */]: "trx",
238
+ ["APT" /* APT */]: "apt",
239
+ ["BNB" /* BNB */]: "bnb",
240
+ HBD: "hbd",
241
+ HIVE: "hive"
242
+ };
243
+ var MARKET_DATA_CACHE_KEY = "market-data/latest";
244
+ var normalizeCurrencyToToken = (currency) => {
245
+ const upperCased = currency.toUpperCase();
246
+ return CURRENCY_TO_TOKEN_MAP[upperCased] ?? currency.toLowerCase();
247
+ };
248
+ function getTokenPriceQueryOptions(currency) {
233
249
  return reactQuery.queryOptions({
234
- queryKey: ["ecency-wallets", "coingecko-price", currency],
250
+ queryKey: ["ecency-wallets", "market-data", currency],
235
251
  queryFn: async () => {
236
- let curr = currency;
237
- switch (currency) {
238
- case "BTC" /* BTC */:
239
- curr = "bitcoin";
240
- break;
241
- case "ETH" /* ETH */:
242
- curr = "ethereum";
243
- break;
244
- case "SOL" /* SOL */:
245
- curr = "solana";
246
- break;
247
- case "TON" /* TON */:
248
- curr = "ton";
249
- break;
250
- case "TRX" /* TRON */:
251
- curr = "tron";
252
- break;
253
- case "APT" /* APT */:
254
- curr = "aptos";
255
- break;
256
- case "BNB" /* BNB */:
257
- curr = "binancecoin";
258
- break;
259
- case "TON" /* TON */:
260
- curr = "the-open-network";
261
- break;
262
- default:
263
- curr = currency;
252
+ if (!currency) {
253
+ throw new Error(
254
+ "[SDK][Wallets][MarketData] \u2013 currency wasn`t provided"
255
+ );
264
256
  }
265
- let rate = cacheGet("gecko");
266
- let response;
267
- if (rate) {
268
- response = rate;
269
- } else {
257
+ if (!sdk.CONFIG.privateApiHost) {
258
+ throw new Error(
259
+ "[SDK][Wallets][MarketData] \u2013 privateApiHost isn`t configured"
260
+ );
261
+ }
262
+ const token = normalizeCurrencyToToken(currency);
263
+ let marketData = cacheGet(MARKET_DATA_CACHE_KEY);
264
+ if (!marketData) {
270
265
  const httpResponse = await fetch(
271
- `https://api.coingecko.com/api/v3/simple/price?ids=${curr}&vs_currencies=usd`,
266
+ `${sdk.CONFIG.privateApiHost}/private-api/market-data/latest`,
272
267
  {
273
268
  method: "GET"
274
269
  }
275
270
  );
271
+ if (!httpResponse.ok) {
272
+ throw new Error(
273
+ `[SDK][Wallets][MarketData] \u2013 failed to fetch latest market data (${httpResponse.status})`
274
+ );
275
+ }
276
276
  const data = await httpResponse.json();
277
- cacheSet("gecko", data === void 0 ? undefinedValue : data);
278
- response = data;
277
+ cacheSet(MARKET_DATA_CACHE_KEY, data);
278
+ marketData = data;
279
+ }
280
+ const tokenData = marketData[token];
281
+ if (!tokenData) {
282
+ throw new Error(
283
+ `[SDK][Wallets][MarketData] \u2013 missing market data for token: ${token}`
284
+ );
285
+ }
286
+ const usdQuote = tokenData.quotes?.usd;
287
+ if (!usdQuote) {
288
+ throw new Error(
289
+ `[SDK][Wallets][MarketData] \u2013 missing USD quote for token: ${token}`
290
+ );
279
291
  }
280
- return +response[curr].usd;
292
+ return Number(usdQuote.price);
281
293
  },
282
294
  enabled: !!currency
283
295
  });
@@ -773,11 +785,12 @@ function getHbdAssetGeneralInfoQueryOptions(username) {
773
785
  );
774
786
  let price = 1;
775
787
  try {
776
- const response = await fetch(
777
- "https://api.coingecko.com/api/v3/simple/price?ids=hive_dollar&vs_currencies=usd"
788
+ await sdk.CONFIG.queryClient.prefetchQuery(
789
+ getTokenPriceQueryOptions("HBD")
778
790
  );
779
- const data = await response.json();
780
- const marketPrice = data.hive_dollar?.usd;
791
+ const marketPrice = sdk.CONFIG.queryClient.getQueryData(
792
+ getTokenPriceQueryOptions("HBD").queryKey
793
+ ) ?? 0;
781
794
  if (typeof marketPrice === "number" && Number.isFinite(marketPrice)) {
782
795
  price = marketPrice;
783
796
  }
@@ -2513,10 +2526,10 @@ function getAptAssetGeneralInfoQueryOptions(username) {
2513
2526
  getAptAssetBalanceQueryOptions(address).queryKey
2514
2527
  ) ?? 0) / 1e8;
2515
2528
  await sdk.CONFIG.queryClient.prefetchQuery(
2516
- getCoinGeckoPriceQueryOptions("APT")
2529
+ getTokenPriceQueryOptions("APT")
2517
2530
  );
2518
2531
  const price = sdk.CONFIG.queryClient.getQueryData(
2519
- getCoinGeckoPriceQueryOptions("APT").queryKey
2532
+ getTokenPriceQueryOptions("APT").queryKey
2520
2533
  ) ?? 0;
2521
2534
  return {
2522
2535
  name: "APT",
@@ -2564,10 +2577,10 @@ function getBnbAssetGeneralInfoQueryOptions(username) {
2564
2577
  getBnbAssetBalanceQueryOptions(address).queryKey
2565
2578
  ) ?? 0) / 1e18;
2566
2579
  await sdk.CONFIG.queryClient.prefetchQuery(
2567
- getCoinGeckoPriceQueryOptions("BNB")
2580
+ getTokenPriceQueryOptions("BNB")
2568
2581
  );
2569
2582
  const price = sdk.CONFIG.queryClient.getQueryData(
2570
- getCoinGeckoPriceQueryOptions("BNB").queryKey
2583
+ getTokenPriceQueryOptions("BNB").queryKey
2571
2584
  ) ?? 0;
2572
2585
  return {
2573
2586
  name: "BNB",
@@ -2615,10 +2628,10 @@ function getBtcAssetGeneralInfoQueryOptions(username) {
2615
2628
  getBtcAssetBalanceQueryOptions(address).queryKey
2616
2629
  ) ?? 0) / 1e8;
2617
2630
  await sdk.CONFIG.queryClient.prefetchQuery(
2618
- getCoinGeckoPriceQueryOptions("BTC")
2631
+ getTokenPriceQueryOptions("BTC")
2619
2632
  );
2620
2633
  const price = sdk.CONFIG.queryClient.getQueryData(
2621
- getCoinGeckoPriceQueryOptions("BTC").queryKey
2634
+ getTokenPriceQueryOptions("BTC").queryKey
2622
2635
  ) ?? 0;
2623
2636
  return {
2624
2637
  name: "BTC",
@@ -2666,10 +2679,10 @@ function getEthAssetGeneralInfoQueryOptions(username) {
2666
2679
  getEthAssetBalanceQueryOptions(address).queryKey
2667
2680
  ) ?? 0) / 1e18;
2668
2681
  await sdk.CONFIG.queryClient.prefetchQuery(
2669
- getCoinGeckoPriceQueryOptions("ETH")
2682
+ getTokenPriceQueryOptions("ETH")
2670
2683
  );
2671
2684
  const price = sdk.CONFIG.queryClient.getQueryData(
2672
- getCoinGeckoPriceQueryOptions("ETH").queryKey
2685
+ getTokenPriceQueryOptions("ETH").queryKey
2673
2686
  ) ?? 0;
2674
2687
  return {
2675
2688
  name: "ETH",
@@ -2717,10 +2730,10 @@ function getSolAssetGeneralInfoQueryOptions(username) {
2717
2730
  getSolAssetBalanceQueryOptions(address).queryKey
2718
2731
  ) ?? 0) / 1e9;
2719
2732
  await sdk.CONFIG.queryClient.prefetchQuery(
2720
- getCoinGeckoPriceQueryOptions("SOL")
2733
+ getTokenPriceQueryOptions("SOL")
2721
2734
  );
2722
2735
  const price = sdk.CONFIG.queryClient.getQueryData(
2723
- getCoinGeckoPriceQueryOptions("SOL").queryKey
2736
+ getTokenPriceQueryOptions("SOL").queryKey
2724
2737
  ) ?? 0;
2725
2738
  return {
2726
2739
  name: "SOL",
@@ -2768,10 +2781,10 @@ function getTonAssetGeneralInfoQueryOptions(username) {
2768
2781
  getTonAssetBalanceQueryOptions(address).queryKey
2769
2782
  ) ?? 0) / 1e9;
2770
2783
  await sdk.CONFIG.queryClient.prefetchQuery(
2771
- getCoinGeckoPriceQueryOptions("TON")
2784
+ getTokenPriceQueryOptions("TON")
2772
2785
  );
2773
2786
  const price = sdk.CONFIG.queryClient.getQueryData(
2774
- getCoinGeckoPriceQueryOptions("TON").queryKey
2787
+ getTokenPriceQueryOptions("TON").queryKey
2775
2788
  ) ?? 0;
2776
2789
  return {
2777
2790
  name: "TON",
@@ -2819,10 +2832,10 @@ function getTronAssetGeneralInfoQueryOptions(username) {
2819
2832
  getTronAssetBalanceQueryOptions(address).queryKey
2820
2833
  ) ?? 0) / 1e6;
2821
2834
  await sdk.CONFIG.queryClient.prefetchQuery(
2822
- getCoinGeckoPriceQueryOptions("TRX")
2835
+ getTokenPriceQueryOptions("TRX")
2823
2836
  );
2824
2837
  const price = sdk.CONFIG.queryClient.getQueryData(
2825
- getCoinGeckoPriceQueryOptions("TRX").queryKey
2838
+ getTokenPriceQueryOptions("TRX").queryKey
2826
2839
  ) ?? 0;
2827
2840
  return {
2828
2841
  name: "TRX",
@@ -3399,7 +3412,6 @@ exports.getAccountWalletAssetInfoQueryOptions = getAccountWalletAssetInfoQueryOp
3399
3412
  exports.getAccountWalletListQueryOptions = getAccountWalletListQueryOptions;
3400
3413
  exports.getAllTokensListQueryOptions = getAllTokensListQueryOptions;
3401
3414
  exports.getBoundFetch = getBoundFetch;
3402
- exports.getCoinGeckoPriceQueryOptions = getCoinGeckoPriceQueryOptions;
3403
3415
  exports.getHbdAssetGeneralInfoQueryOptions = getHbdAssetGeneralInfoQueryOptions;
3404
3416
  exports.getHbdAssetTransactionsQueryOptions = getHbdAssetTransactionsQueryOptions;
3405
3417
  exports.getHiveAssetGeneralInfoQueryOptions = getHiveAssetGeneralInfoQueryOptions;
@@ -3424,6 +3436,7 @@ exports.getPointsQueryOptions = getPointsQueryOptions;
3424
3436
  exports.getSpkAssetGeneralInfoQueryOptions = getSpkAssetGeneralInfoQueryOptions;
3425
3437
  exports.getSpkMarketsQueryOptions = getSpkMarketsQueryOptions;
3426
3438
  exports.getTokenOperationsQueryOptions = getTokenOperationsQueryOptions;
3439
+ exports.getTokenPriceQueryOptions = getTokenPriceQueryOptions;
3427
3440
  exports.getWallet = getWallet;
3428
3441
  exports.isEmptyDate = isEmptyDate;
3429
3442
  exports.lockLarynx = lockLarynx;