@rabbitio/ui-kit 1.0.0-beta.22 → 1.0.0-beta.24

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.
@@ -2492,6 +2492,16 @@ class SwapProvider {
2492
2492
  throw new Error("Not implemented in base");
2493
2493
  }
2494
2494
 
2495
+ /**
2496
+ * Retrieves all currencies supported by this swap provider.
2497
+ * Returns one of SwapProvider.COMMON_ERRORS in case of processable fail.
2498
+ *
2499
+ * @return {Promise<({ result: true, coins: Coin[] }|{ result: false, reason: string })>}
2500
+ */
2501
+ async getAllSupportedCurrencies() {
2502
+ throw new Error("Not implemented in base");
2503
+ }
2504
+
2495
2505
  /**
2496
2506
  * Retrieves all deposit currencies supported by this swap provider.
2497
2507
  * Returns one of SwapProvider.COMMON_ERRORS in case of processable fail.
@@ -2538,6 +2548,7 @@ class SwapProvider {
2538
2548
  * null min or max signals there is no corresponding limitation. undefined means that the limits were not retrieved.
2539
2549
  * For fail result on of SwapProvider.NO_SWAPS_REASONS or SwapProvider.COMMON_ERRORS reasons will be returned.
2540
2550
  *
2551
+ * WARNING: MUST return NOT_SUPPORTED error code for any case when pair is not available/supported (Should not throw random errors for this case)
2541
2552
  * @param fromCoin {Coin}
2542
2553
  * @param toCoin {Coin}
2543
2554
  * @param amountCoins {string}
@@ -2687,12 +2698,33 @@ class SwapspaceSwapProvider extends SwapProvider {
2687
2698
  improveAndRethrow(e, loggerSource);
2688
2699
  }
2689
2700
  }
2701
+ async getAllSupportedCurrencies() {
2702
+ const loggerSource = "getAllSupportedCurrencies";
2703
+ try {
2704
+ var _this$_supportedCoins2;
2705
+ await this._fetchSupportedCurrenciesIfNeeded();
2706
+ Logger.log(`We have ${(_this$_supportedCoins2 = this._supportedCoins) == null ? void 0 : _this$_supportedCoins2.length} supported coins returning`, loggerSource);
2707
+ return {
2708
+ result: true,
2709
+ coins: this._supportedCoins.map(item => item.coin)
2710
+ };
2711
+ } catch (e) {
2712
+ var _e$response2;
2713
+ if ((e == null || (_e$response2 = e.response) == null ? void 0 : _e$response2.status) === 429) {
2714
+ return {
2715
+ result: false,
2716
+ reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
2717
+ };
2718
+ }
2719
+ improveAndRethrow(e, loggerSource);
2720
+ }
2721
+ }
2690
2722
  async getWithdrawalCurrencies(exceptCurrency = null) {
2691
2723
  const loggerSource = "getWithdrawalCurrencies";
2692
2724
  try {
2693
- var _this$_supportedCoins2;
2725
+ var _this$_supportedCoins3;
2694
2726
  await this._fetchSupportedCurrenciesIfNeeded();
2695
- Logger.log(`We have ${(_this$_supportedCoins2 = this._supportedCoins) == null ? void 0 : _this$_supportedCoins2.length} supported coins, getting withdrawable`, loggerSource);
2727
+ Logger.log(`We have ${(_this$_supportedCoins3 = this._supportedCoins) == null ? void 0 : _this$_supportedCoins3.length} supported coins, getting withdrawable`, loggerSource);
2696
2728
  return {
2697
2729
  result: true,
2698
2730
  coins: this._supportedCoins.filter(item => {
@@ -2701,8 +2733,8 @@ class SwapspaceSwapProvider extends SwapProvider {
2701
2733
  }).map(item => item.coin)
2702
2734
  };
2703
2735
  } catch (e) {
2704
- var _e$response2;
2705
- if ((e == null || (_e$response2 = e.response) == null ? void 0 : _e$response2.status) === 429) {
2736
+ var _e$response3;
2737
+ if ((e == null || (_e$response3 = e.response) == null ? void 0 : _e$response3.status) === 429) {
2706
2738
  return {
2707
2739
  result: false,
2708
2740
  reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
@@ -2717,16 +2749,16 @@ class SwapspaceSwapProvider extends SwapProvider {
2717
2749
  getIconUrl(coinOrTicker) {
2718
2750
  const loggerSource = "getIconUrl";
2719
2751
  try {
2720
- var _this$_supportedCoins4, _this$_supportedCoins5;
2752
+ var _this$_supportedCoins5, _this$_supportedCoins6;
2721
2753
  let coin = coinOrTicker;
2722
2754
  if (!(coinOrTicker instanceof Coin)) {
2723
- var _this$_supportedCoins3;
2724
- coin = (_this$_supportedCoins3 = this._supportedCoins.find(i => i.coin.ticker === coinOrTicker)) == null ? void 0 : _this$_supportedCoins3.coin;
2755
+ var _this$_supportedCoins4;
2756
+ coin = (_this$_supportedCoins4 = this._supportedCoins.find(i => i.coin.ticker === coinOrTicker)) == null ? void 0 : _this$_supportedCoins4.coin;
2725
2757
  }
2726
- return (_this$_supportedCoins4 = (_this$_supportedCoins5 = this._supportedCoins.find(item => {
2758
+ return (_this$_supportedCoins5 = (_this$_supportedCoins6 = this._supportedCoins.find(item => {
2727
2759
  var _item$coin2, _coin;
2728
2760
  return ((_item$coin2 = item.coin) == null ? void 0 : _item$coin2.ticker) === ((_coin = coin) == null ? void 0 : _coin.ticker);
2729
- })) == null ? void 0 : _this$_supportedCoins5.iconURL) != null ? _this$_supportedCoins4 : FALLBACK_ICON_URL;
2761
+ })) == null ? void 0 : _this$_supportedCoins6.iconURL) != null ? _this$_supportedCoins5 : FALLBACK_ICON_URL;
2730
2762
  } catch (e) {
2731
2763
  improveAndRethrow(e, loggerSource);
2732
2764
  }
@@ -2734,8 +2766,8 @@ class SwapspaceSwapProvider extends SwapProvider {
2734
2766
  async _fetchSupportedCurrenciesIfNeeded() {
2735
2767
  const loggerSource = "_fetchSupportedCurrenciesIfNeeded";
2736
2768
  try {
2737
- var _this$_supportedCoins6;
2738
- if (!((_this$_supportedCoins6 = this._supportedCoins) != null && _this$_supportedCoins6.length)) {
2769
+ var _this$_supportedCoins7;
2770
+ if (!((_this$_supportedCoins7 = this._supportedCoins) != null && _this$_supportedCoins7.length)) {
2739
2771
  var _rawResponse$data, _rawResponse$data2;
2740
2772
  const rawResponse = await axios.get(`${this._URL}/api/v2/currencies`);
2741
2773
  Logger.log(`Retrieved ${rawResponse == null || (_rawResponse$data = rawResponse.data) == null ? void 0 : _rawResponse$data.length} currencies`, loggerSource);
@@ -2799,12 +2831,12 @@ class SwapspaceSwapProvider extends SwapProvider {
2799
2831
  async getCoinToUSDTRate(coin) {
2800
2832
  const loggerSource = "getCoinToUSDTRate";
2801
2833
  try {
2802
- var _this$_supportedCoins7;
2834
+ var _this$_supportedCoins8;
2803
2835
  if (!coin) return null;
2804
2836
  await this._fetchSupportedCurrenciesIfNeeded();
2805
2837
 
2806
2838
  // Using USDT TRC20 as usually fee in this network is smaller than ERC20 and this network is widely used for USDT
2807
- const usdtTrc20 = (_this$_supportedCoins7 = this._supportedCoins.find(i => i.coin.ticker === "USDTTRC20")) == null ? void 0 : _this$_supportedCoins7.coin;
2839
+ const usdtTrc20 = (_this$_supportedCoins8 = this._supportedCoins.find(i => i.coin.ticker === "USDTTRC20")) == null ? void 0 : _this$_supportedCoins8.coin;
2808
2840
  if (!usdtTrc20) {
2809
2841
  return {
2810
2842
  result: false
@@ -2865,6 +2897,12 @@ class SwapspaceSwapProvider extends SwapProvider {
2865
2897
  if (!fromCoinSwapspaceDetails || !toCoinSwapspaceDetails) {
2866
2898
  throw new Error("Failed to find swapspace coin details for: " + fromCoin.ticker + " -> " + toCoin.ticker);
2867
2899
  }
2900
+ if (!fromCoinSwapspaceDetails.deposit || !toCoinSwapspaceDetails.withdrawal) {
2901
+ return {
2902
+ result: false,
2903
+ reason: SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED
2904
+ };
2905
+ }
2868
2906
  /* Here we use not documented parameter 'estimated=false'. This parameter controls whether we want to use
2869
2907
  * cached rate values stored in swapspace cache. Their support says they store at most for 30 sec.
2870
2908
  * But we are better off using the most actual rates.
@@ -2949,8 +2987,8 @@ class SwapspaceSwapProvider extends SwapProvider {
2949
2987
  Logger.log(`Returning result ${safeStringify(result)}`, loggerSource);
2950
2988
  return result;
2951
2989
  } catch (e) {
2952
- var _e$response3;
2953
- if ((e == null || (_e$response3 = e.response) == null ? void 0 : _e$response3.status) === 429) {
2990
+ var _e$response4;
2991
+ if ((e == null || (_e$response4 = e.response) == null ? void 0 : _e$response4.status) === 429) {
2954
2992
  return {
2955
2993
  result: false,
2956
2994
  reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
@@ -2964,7 +3002,7 @@ class SwapspaceSwapProvider extends SwapProvider {
2964
3002
  const loggerSource = "createSwap";
2965
3003
  const partner = rawSwapData == null ? void 0 : rawSwapData.partner;
2966
3004
  try {
2967
- var _this$_supportedCoins8, _this$_supportedCoins9;
3005
+ var _this$_supportedCoins9, _this$_supportedCoins10;
2968
3006
  if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof amount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string") {
2969
3007
  throw new Error(`Invalid input: ${fromCoin} ${toCoin} ${amount} ${toAddress} ${refundAddress}`);
2970
3008
  }
@@ -2973,10 +3011,10 @@ class SwapspaceSwapProvider extends SwapProvider {
2973
3011
  throw new Error(`Invalid raw swap data: ${safeStringify(rawSwapData)}`);
2974
3012
  }
2975
3013
  await this._fetchSupportedCurrenciesIfNeeded();
2976
- const toCurrencyExtraId = (_this$_supportedCoins8 = (_this$_supportedCoins9 = this._supportedCoins.find(item => {
3014
+ const toCurrencyExtraId = (_this$_supportedCoins9 = (_this$_supportedCoins10 = this._supportedCoins.find(item => {
2977
3015
  var _item$coin4;
2978
3016
  return ((_item$coin4 = item.coin) == null ? void 0 : _item$coin4.ticker) === (toCoin == null ? void 0 : toCoin.ticker);
2979
- })) == null ? void 0 : _this$_supportedCoins9.extraId) != null ? _this$_supportedCoins8 : "";
3017
+ })) == null ? void 0 : _this$_supportedCoins10.extraId) != null ? _this$_supportedCoins9 : "";
2980
3018
  const requestData = {
2981
3019
  partner: partner,
2982
3020
  fromCurrency: rawSwapData == null ? void 0 : rawSwapData.fromCurrency,
@@ -3023,15 +3061,15 @@ class SwapspaceSwapProvider extends SwapProvider {
3023
3061
  Logger.log(errorMessage, loggerSource);
3024
3062
  throw new Error(errorMessage);
3025
3063
  } catch (e) {
3026
- var _e$response4, _e$response5;
3064
+ var _e$response5, _e$response6;
3027
3065
  Logger.logError(e, loggerSource, `Failed to create swap. Error is: ${safeStringify(e)}`);
3028
3066
  const composeFailResult = reason => ({
3029
3067
  result: false,
3030
3068
  reason: reason,
3031
3069
  partner: partner
3032
3070
  });
3033
- const status = e == null || (_e$response4 = e.response) == null ? void 0 : _e$response4.status;
3034
- const data = e == null || (_e$response5 = e.response) == null ? void 0 : _e$response5.data;
3071
+ const status = e == null || (_e$response5 = e.response) == null ? void 0 : _e$response5.status;
3072
+ const data = e == null || (_e$response6 = e.response) == null ? void 0 : _e$response6.data;
3035
3073
  if (status === 429) {
3036
3074
  Logger.log(`Returning fail - RPS limit exceeded ${data}`, loggerSource);
3037
3075
  return composeFailResult(SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED);
@@ -3092,9 +3130,9 @@ class SwapspaceSwapProvider extends SwapProvider {
3092
3130
  const responses = await Promise.all(swapIds.map(swapId => getNotFailingOn404(swapId)));
3093
3131
  const wo404 = responses.flat();
3094
3132
  const swaps = wo404.map(r => r.data).map((swap, index) => {
3095
- var _this$_supportedCoins10, _this$_supportedCoins11;
3096
- const fromCoin = (_this$_supportedCoins10 = this._supportedCoins.find(i => i.code === swap.from.code && i.network === swap.from.network)) == null ? void 0 : _this$_supportedCoins10.coin;
3097
- const toCoin = (_this$_supportedCoins11 = this._supportedCoins.find(i => i.code === swap.to.code && i.network === swap.to.network)) == null ? void 0 : _this$_supportedCoins11.coin;
3133
+ var _this$_supportedCoins11, _this$_supportedCoins12;
3134
+ const fromCoin = (_this$_supportedCoins11 = this._supportedCoins.find(i => i.code === swap.from.code && i.network === swap.from.network)) == null ? void 0 : _this$_supportedCoins11.coin;
3135
+ const toCoin = (_this$_supportedCoins12 = this._supportedCoins.find(i => i.code === swap.to.code && i.network === swap.to.network)) == null ? void 0 : _this$_supportedCoins12.coin;
3098
3136
  if (!fromCoin || !toCoin) {
3099
3137
  return []; // We skip swaps with not supported coins for now
3100
3138
  }
@@ -3112,14 +3150,14 @@ class SwapspaceSwapProvider extends SwapProvider {
3112
3150
  swaps: swaps
3113
3151
  };
3114
3152
  } catch (e) {
3115
- var _e$response6, _e$response7;
3153
+ var _e$response7, _e$response8;
3116
3154
  Logger.logError(e, loggerSource, `Failed to get swap details. Error is: ${safeStringify(e)}`);
3117
3155
  const composeFailResult = reason => ({
3118
3156
  result: false,
3119
3157
  reason: reason
3120
3158
  });
3121
- const status = e == null || (_e$response6 = e.response) == null ? void 0 : _e$response6.status;
3122
- const data = e == null || (_e$response7 = e.response) == null ? void 0 : _e$response7.data;
3159
+ const status = e == null || (_e$response7 = e.response) == null ? void 0 : _e$response7.status;
3160
+ const data = e == null || (_e$response8 = e.response) == null ? void 0 : _e$response8.data;
3123
3161
  if (status === 429) {
3124
3162
  Logger.log(`Returning fail - RPS limit exceeded ${data}`, loggerSource);
3125
3163
  return composeFailResult(SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED);
@@ -3276,6 +3314,27 @@ class PublicSwapService {
3276
3314
  Logger.logError(e, "PublicSwapService.initialize");
3277
3315
  }
3278
3316
  }
3317
+ async getAllSupportedCurrenciesListForPublicSwap() {
3318
+ const loggerSource = "getAllSupportedCurrenciesListForPublicSwap";
3319
+ try {
3320
+ var _result$coins;
3321
+ const result = await this._swapProvider.getAllSupportedCurrencies();
3322
+ if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3323
+ SwapUtils.safeHandleRequestsLimitExceeding();
3324
+ return {
3325
+ result: false,
3326
+ reason: PublicSwapService.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
3327
+ };
3328
+ }
3329
+ Logger.log(`Retrieved ${result == null || (_result$coins = result.coins) == null ? void 0 : _result$coins.length} supported currencies for swap`, loggerSource);
3330
+ return {
3331
+ result: true,
3332
+ coins: result.coins
3333
+ };
3334
+ } catch (e) {
3335
+ improveAndRethrow(e, "getDepositCurrenciesListForPublicSwap");
3336
+ }
3337
+ }
3279
3338
  async getDepositCurrenciesListForPublicSwap() {
3280
3339
  try {
3281
3340
  return await this._getCurrenciesListForPublicSwap(false);
@@ -3293,7 +3352,7 @@ class PublicSwapService {
3293
3352
  async _getCurrenciesListForPublicSwap(withdraw = false) {
3294
3353
  const loggerSource = "getCurrenciesListForPublicSwap";
3295
3354
  try {
3296
- var _result$coins;
3355
+ var _result$coins2;
3297
3356
  const result = withdraw ? await this._swapProvider.getWithdrawalCurrencies() : await this._swapProvider.getDepositCurrencies();
3298
3357
  if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3299
3358
  SwapUtils.safeHandleRequestsLimitExceeding();
@@ -3302,12 +3361,7 @@ class PublicSwapService {
3302
3361
  reason: PublicSwapService.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
3303
3362
  };
3304
3363
  }
3305
- Logger.log(`Retrieved ${result == null || (_result$coins = result.coins) == null ? void 0 : _result$coins.length} supported currencies for swap`, loggerSource);
3306
- if (result.coins.length > 1) {
3307
- let temp = result.coins[0];
3308
- result.coins[0] = result.coins[1];
3309
- result.coins[1] = temp;
3310
- }
3364
+ Logger.log(`Retrieved ${result == null || (_result$coins2 = result.coins) == null ? void 0 : _result$coins2.length} supported currencies for swap`, loggerSource);
3311
3365
  return {
3312
3366
  result: true,
3313
3367
  coins: result.coins