@rabbitio/ui-kit 1.0.0-beta.13 → 1.0.0-beta.15

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/dist/index.cjs CHANGED
@@ -2505,7 +2505,7 @@ var Cache = /*#__PURE__*/function () {
2505
2505
  return Cache;
2506
2506
  }();
2507
2507
 
2508
- function _catch$1(body, recover) {
2508
+ function _catch$3(body, recover) {
2509
2509
  try {
2510
2510
  var result = body();
2511
2511
  } catch (e) {
@@ -2521,7 +2521,7 @@ var EmailsApi = /*#__PURE__*/function () {
2521
2521
  EmailsApi.sendEmail = function sendEmail(subject, body) {
2522
2522
  try {
2523
2523
  var _this = this;
2524
- var _temp = _catch$1(function () {
2524
+ var _temp = _catch$3(function () {
2525
2525
  var url = window.location.protocol + "//" + window.location.host + "/api/v1/" + _this.serverEndpointEntity;
2526
2526
  return Promise.resolve(axios__default["default"].post(url, {
2527
2527
  subject: subject,
@@ -2858,7 +2858,7 @@ SwapProvider.SWAP_STATUSES = {
2858
2858
  FAILED: "failed" // public +
2859
2859
  };
2860
2860
 
2861
- function _catch(body, recover) {
2861
+ function _catch$2(body, recover) {
2862
2862
  try {
2863
2863
  var result = body();
2864
2864
  } catch (e) {
@@ -2902,7 +2902,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
2902
2902
  try {
2903
2903
  var _this2 = this;
2904
2904
  var loggerSource = "getDepositCurrencies";
2905
- return Promise.resolve(_catch(function () {
2905
+ return Promise.resolve(_catch$2(function () {
2906
2906
  return Promise.resolve(_this2._fetchSupportedCurrenciesIfNeeded()).then(function () {
2907
2907
  var _this2$_supportedCoin;
2908
2908
  Logger.log("We have " + ((_this2$_supportedCoin = _this2._supportedCoins) == null ? void 0 : _this2$_supportedCoin.length) + " supported coins, getting depositable", loggerSource);
@@ -2936,7 +2936,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
2936
2936
  try {
2937
2937
  var _this3 = this;
2938
2938
  var loggerSource = "getWithdrawalCurrencies";
2939
- return Promise.resolve(_catch(function () {
2939
+ return Promise.resolve(_catch$2(function () {
2940
2940
  return Promise.resolve(_this3._fetchSupportedCurrenciesIfNeeded()).then(function () {
2941
2941
  var _this3$_supportedCoin;
2942
2942
  Logger.log("We have " + ((_this3$_supportedCoin = _this3._supportedCoins) == null ? void 0 : _this3$_supportedCoin.length) + " supported coins, getting withdrawable", loggerSource);
@@ -2993,7 +2993,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
2993
2993
  try {
2994
2994
  var _this5 = this;
2995
2995
  var loggerSource = "_fetchSupportedCurrenciesIfNeeded";
2996
- return Promise.resolve(_catch(function () {
2996
+ return Promise.resolve(_catch$2(function () {
2997
2997
  var _temp = function (_this5$_supportedCoin) {
2998
2998
  if (!((_this5$_supportedCoin = _this5._supportedCoins) != null && _this5$_supportedCoin.length)) {
2999
2999
  return Promise.resolve(axios__default["default"].get(_this5._URL + "/api/v2/currencies")).then(function (rawResponse) {
@@ -3066,7 +3066,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3066
3066
  try {
3067
3067
  var _this6 = this;
3068
3068
  var loggerSource = "getCoinToUSDTRate";
3069
- return Promise.resolve(_catch(function () {
3069
+ return Promise.resolve(_catch$2(function () {
3070
3070
  return Promise.resolve(_this6._fetchSupportedCurrenciesIfNeeded()).then(function () {
3071
3071
  var _this6$_supportedCoin;
3072
3072
  // Using USDT TRC20 as usually fee in this network is smaller than ERC20 and this network is widely used for USDT
@@ -3128,7 +3128,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3128
3128
  try {
3129
3129
  var _this7 = this;
3130
3130
  var loggerSource = "getSwapInfo";
3131
- return Promise.resolve(_catch(function () {
3131
+ return Promise.resolve(_catch$2(function () {
3132
3132
  if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof amountCoins !== "string" || bignumber_js.BigNumber(amountCoins).lt("0")) {
3133
3133
  throw new Error("Wrong input params: " + amountCoins + " " + fromCoin.ticker + " -> " + toCoin.ticker + (fromCoin instanceof Coin) + (toCoin instanceof Coin));
3134
3134
  }
@@ -3258,7 +3258,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3258
3258
  var _this8 = this;
3259
3259
  var loggerSource = "createSwap";
3260
3260
  var partner = rawSwapData == null ? void 0 : rawSwapData.partner;
3261
- return Promise.resolve(_catch(function () {
3261
+ return Promise.resolve(_catch$2(function () {
3262
3262
  if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof amount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string") {
3263
3263
  throw new Error("Invalid input: " + fromCoin + " " + toCoin + " " + amount + " " + toAddress + " " + refundAddress);
3264
3264
  }
@@ -3377,7 +3377,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3377
3377
  try {
3378
3378
  var _this9 = this;
3379
3379
  var loggerSource = "getExistingSwapsDetailsAndStatus";
3380
- return Promise.resolve(_catch(function () {
3380
+ return Promise.resolve(_catch$2(function () {
3381
3381
  if (swapIds.find(function (id) {
3382
3382
  return typeof id !== "string";
3383
3383
  })) {
@@ -3385,7 +3385,7 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3385
3385
  }
3386
3386
  var getNotFailingOn404 = function getNotFailingOn404(swapId) {
3387
3387
  try {
3388
- return Promise.resolve(_catch(function () {
3388
+ return Promise.resolve(_catch$2(function () {
3389
3389
  return Promise.resolve(axios__default["default"].get(_this9._URL + "/api/v2/exchange/" + swapId));
3390
3390
  }, function (error) {
3391
3391
  var _error$response;
@@ -3467,6 +3467,827 @@ var SwapspaceSwapProvider = /*#__PURE__*/function (_SwapProvider) {
3467
3467
  return SwapspaceSwapProvider;
3468
3468
  }(SwapProvider);
3469
3469
 
3470
+ function _catch$1(body, recover) {
3471
+ try {
3472
+ var result = body();
3473
+ } catch (e) {
3474
+ return recover(e);
3475
+ }
3476
+ if (result && result.then) {
3477
+ return result.then(void 0, recover);
3478
+ }
3479
+ return result;
3480
+ }
3481
+ var _iteratorSymbol = typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
3482
+ function _settle(pact, state, value) {
3483
+ if (!pact.s) {
3484
+ if (value instanceof _Pact) {
3485
+ if (value.s) {
3486
+ if (state & 1) {
3487
+ state = value.s;
3488
+ }
3489
+ value = value.v;
3490
+ } else {
3491
+ value.o = _settle.bind(null, pact, state);
3492
+ return;
3493
+ }
3494
+ }
3495
+ if (value && value.then) {
3496
+ value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
3497
+ return;
3498
+ }
3499
+ pact.s = state;
3500
+ pact.v = value;
3501
+ var observer = pact.o;
3502
+ if (observer) {
3503
+ observer(pact);
3504
+ }
3505
+ }
3506
+ }
3507
+ var _Pact = /*#__PURE__*/function () {
3508
+ function _Pact() {}
3509
+ _Pact.prototype.then = function (onFulfilled, onRejected) {
3510
+ var result = new _Pact();
3511
+ var state = this.s;
3512
+ if (state) {
3513
+ var callback = state & 1 ? onFulfilled : onRejected;
3514
+ if (callback) {
3515
+ try {
3516
+ _settle(result, 1, callback(this.v));
3517
+ } catch (e) {
3518
+ _settle(result, 2, e);
3519
+ }
3520
+ return result;
3521
+ } else {
3522
+ return this;
3523
+ }
3524
+ }
3525
+ this.o = function (_this) {
3526
+ try {
3527
+ var value = _this.v;
3528
+ if (_this.s & 1) {
3529
+ _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
3530
+ } else if (onRejected) {
3531
+ _settle(result, 1, onRejected(value));
3532
+ } else {
3533
+ _settle(result, 2, value);
3534
+ }
3535
+ } catch (e) {
3536
+ _settle(result, 2, e);
3537
+ }
3538
+ };
3539
+ return result;
3540
+ };
3541
+ return _Pact;
3542
+ }();
3543
+ function _isSettledPact(thenable) {
3544
+ return thenable instanceof _Pact && thenable.s & 1;
3545
+ }
3546
+ function _forTo(array, body, check) {
3547
+ var i = -1,
3548
+ pact,
3549
+ reject;
3550
+ function _cycle(result) {
3551
+ try {
3552
+ while (++i < array.length && (!check || !check())) {
3553
+ result = body(i);
3554
+ if (result && result.then) {
3555
+ if (_isSettledPact(result)) {
3556
+ result = result.v;
3557
+ } else {
3558
+ result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
3559
+ return;
3560
+ }
3561
+ }
3562
+ }
3563
+ if (pact) {
3564
+ _settle(pact, 1, result);
3565
+ } else {
3566
+ pact = result;
3567
+ }
3568
+ } catch (e) {
3569
+ _settle(pact || (pact = new _Pact()), 2, e);
3570
+ }
3571
+ }
3572
+ _cycle();
3573
+ return pact;
3574
+ }
3575
+ function _forOf(target, body, check) {
3576
+ if (typeof target[_iteratorSymbol] === "function") {
3577
+ var _cycle = function _cycle(result) {
3578
+ try {
3579
+ while (!(step = iterator.next()).done && (!check || !check())) {
3580
+ result = body(step.value);
3581
+ if (result && result.then) {
3582
+ if (_isSettledPact(result)) {
3583
+ result = result.v;
3584
+ } else {
3585
+ result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
3586
+ return;
3587
+ }
3588
+ }
3589
+ }
3590
+ if (pact) {
3591
+ _settle(pact, 1, result);
3592
+ } else {
3593
+ pact = result;
3594
+ }
3595
+ } catch (e) {
3596
+ _settle(pact || (pact = new _Pact()), 2, e);
3597
+ }
3598
+ };
3599
+ var iterator = target[_iteratorSymbol](),
3600
+ step,
3601
+ pact,
3602
+ reject;
3603
+ _cycle();
3604
+ if (iterator["return"]) {
3605
+ var _fixup = function _fixup(value) {
3606
+ try {
3607
+ if (!step.done) {
3608
+ iterator["return"]();
3609
+ }
3610
+ } catch (e) {}
3611
+ return value;
3612
+ };
3613
+ if (pact && pact.then) {
3614
+ return pact.then(_fixup, function (e) {
3615
+ throw _fixup(e);
3616
+ });
3617
+ }
3618
+ _fixup();
3619
+ }
3620
+ return pact;
3621
+ }
3622
+ // No support for Symbol.iterator
3623
+ if (!("length" in target)) {
3624
+ throw new TypeError("Object is not iterable");
3625
+ }
3626
+ // Handle live collections properly
3627
+ var values = [];
3628
+ for (var i = 0; i < target.length; i++) {
3629
+ values.push(target[i]);
3630
+ }
3631
+ return _forTo(values, function (i) {
3632
+ return body(values[i]);
3633
+ }, check);
3634
+ }
3635
+ var SwapUtils = /*#__PURE__*/function () {
3636
+ function SwapUtils() {}
3637
+ /**
3638
+ * Retrieves min and max limits for swapping giving currencies.
3639
+ * Returns also conversion rate if possible with predefined amount logic.
3640
+ * Rate is how many "to" coins does 1 "from" coin contain.
3641
+ *
3642
+ * In case of errors returns one of reasons
3643
+ * - SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED
3644
+ * - one of SwapProvider.COMMON_ERRORS.*
3645
+ *
3646
+ * @param swapProvider {SwapProvider}
3647
+ * @param fromCoin {Coin} enabled coin (to swap amount from)
3648
+ * @param toCoin {Coin}
3649
+ * @return {Promise<{
3650
+ * result: true,
3651
+ * min: string,
3652
+ * fiatMin: (number|null),
3653
+ * max: string,
3654
+ * fiatMax: (number|null),
3655
+ * rate: (string|null),
3656
+ * }|{
3657
+ * result: false,
3658
+ * reason: string
3659
+ * }>}
3660
+ */
3661
+ SwapUtils.getInitialSwapData = function getInitialSwapData(swapProvider, fromCoin, toCoin) {
3662
+ try {
3663
+ var loggerSource = "getInitialSwapData";
3664
+ return Promise.resolve(_catch$1(function () {
3665
+ /* We use some amount here that should fit at least some of the limits of the swap providers.
3666
+ * So we are going to get some rate to be used as the default for the on-flight calculations before we get
3667
+ * the exact rate (that should be retrieved by getSwapCreationInfo method) for a specific amount.
3668
+ */
3669
+ var defaultAmountUsd = bignumber_js.BigNumber("300");
3670
+ return Promise.resolve(swapProvider.getCoinToUSDTRate(fromCoin)).then(function (coinUsdRate) {
3671
+ var coinAmountForDefaultUsdAmount = AmountUtils.trim(coinUsdRate.result ? defaultAmountUsd.div(coinUsdRate == null ? void 0 : coinUsdRate.rate) : defaultAmountUsd, fromCoin.digits);
3672
+ Logger.log("Init: " + coinAmountForDefaultUsdAmount + " " + fromCoin.ticker + "->" + toCoin.ticker, loggerSource);
3673
+ return Promise.resolve(swapProvider.getSwapInfo(fromCoin, toCoin, coinAmountForDefaultUsdAmount)).then(function (details) {
3674
+ if (!details) {
3675
+ throw new Error("The details are empty: " + safeStringify(details));
3676
+ }
3677
+ if (!details.result) {
3678
+ Logger.log("Failed with reason: " + details.reason + ". " + fromCoin.ticker + "->" + toCoin.ticker, loggerSource);
3679
+ if ((details == null ? void 0 : details.reason) === SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED || (details == null ? void 0 : details.reason) === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3680
+ return {
3681
+ result: false,
3682
+ reason: details.reason
3683
+ };
3684
+ } else {
3685
+ throw new Error("Unhandled error case: " + (details == null ? void 0 : details.reason));
3686
+ }
3687
+ }
3688
+ var fiatMin = null;
3689
+ var fiatMax = null;
3690
+ if ((coinUsdRate == null ? void 0 : coinUsdRate.rate) != null) {
3691
+ var usdDecimals = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
3692
+ fiatMin = bignumber_js.BigNumber(details == null ? void 0 : details.smallestMin).times(coinUsdRate.rate).toFixed(usdDecimals);
3693
+ fiatMax = bignumber_js.BigNumber(details == null ? void 0 : details.greatestMax).times(coinUsdRate.rate).toFixed(usdDecimals);
3694
+ }
3695
+ var result = {
3696
+ result: true,
3697
+ min: details == null ? void 0 : details.smallestMin,
3698
+ fiatMin: fiatMin,
3699
+ max: details == null ? void 0 : details.greatestMax,
3700
+ fiatMax: fiatMax,
3701
+ rate: AmountUtils.trim(details.rate, 30)
3702
+ };
3703
+ Logger.log("Returning: " + safeStringify(result), loggerSource);
3704
+ return result;
3705
+ });
3706
+ });
3707
+ }, function (e) {
3708
+ Logger.log("Failed to init swap: " + safeStringify(e), loggerSource);
3709
+ improveAndRethrow(e, loggerSource);
3710
+ }));
3711
+ } catch (e) {
3712
+ return Promise.reject(e);
3713
+ }
3714
+ };
3715
+ SwapUtils.safeHandleRequestsLimitExceeding = function safeHandleRequestsLimitExceeding() {
3716
+ (function () {
3717
+ try {
3718
+ var _temp = _catch$1(function () {
3719
+ return Promise.resolve(EmailsApi.sendEmail("AUTOMATIC EMAIL - SWAPSPACE REQUESTS LIMIT EXCEEDED", "Requests limit exceeded. Urgently ask swaps provider support for limit increasing")).then(function () {});
3720
+ }, function (e) {
3721
+ Logger.log("Failed to handle limit exceeding " + safeStringify(e), "_safeHandleRequestsLimitExceeding");
3722
+ });
3723
+ return _temp && _temp.then ? _temp.then(function () {}) : void 0;
3724
+ } catch (e) {
3725
+ Promise.reject(e);
3726
+ }
3727
+ })();
3728
+ }
3729
+
3730
+ /**
3731
+ * If some swap is not found by id then there is no item in return list.
3732
+ *
3733
+ * @param swapProvider {SwapProvider}
3734
+ * @param swapIds {string[]}
3735
+ * @return {Promise<{
3736
+ * result: true,
3737
+ * swaps: ExistingSwapWithFiatData[]
3738
+ * }|{
3739
+ * result: false,
3740
+ * reason: string
3741
+ * }>}
3742
+ */;
3743
+ SwapUtils.getExistingSwapsDetailsWithFiatAmounts = function getExistingSwapsDetailsWithFiatAmounts(swapProvider, swapIds) {
3744
+ try {
3745
+ return Promise.resolve(_catch$1(function () {
3746
+ return Promise.resolve(swapProvider.getExistingSwapsDetailsAndStatus(swapIds)).then(function (result) {
3747
+ var _temp5 = function () {
3748
+ if (result.result) {
3749
+ var _temp4 = function _temp4() {
3750
+ result.swaps = _extendedSwaps;
3751
+ };
3752
+ var _extendedSwaps = [];
3753
+ var _temp3 = _forOf(result.swaps, function (swap) {
3754
+ var _temp2 = function () {
3755
+ if (swap.status === SwapProvider.SWAP_STATUSES.REFUNDED) {
3756
+ return Promise.resolve(swapProvider.getCoinToUSDTRate(swap.fromCoin)).then(function (rate) {
3757
+ _extendedSwaps.push(ExistingSwapWithFiatData.fromExistingSwap(swap, (rate == null ? void 0 : rate.rate) != null ? bignumber_js.BigNumber(swap.fromAmount).times(rate.rate).toNumber() : null, (rate == null ? void 0 : rate.rate) != null ? bignumber_js.BigNumber(swap.toAmount).times(rate.rate).toNumber() : null, "USD", FiatCurrenciesService.getCurrencyDecimalCountByCode("USD")));
3758
+ });
3759
+ } else {
3760
+ return Promise.resolve(Promise.all([swapProvider.getCoinToUSDTRate(swap.fromCoin), swapProvider.getCoinToUSDTRate(swap.toCoin)])).then(function (_ref) {
3761
+ var fromCoinFiatRate = _ref[0],
3762
+ toConFiatRate = _ref[1];
3763
+ _extendedSwaps.push(ExistingSwapWithFiatData.fromExistingSwap(swap, (fromCoinFiatRate == null ? void 0 : fromCoinFiatRate.rate) != null ? bignumber_js.BigNumber(swap.fromAmount).times(fromCoinFiatRate.rate).toNumber() : null, (toConFiatRate == null ? void 0 : toConFiatRate.rate) != null ? bignumber_js.BigNumber(swap.toAmount).times(toConFiatRate.rate).toNumber() : null, "USD", FiatCurrenciesService.getCurrencyDecimalCountByCode("USD")));
3764
+ });
3765
+ }
3766
+ }();
3767
+ if (_temp2 && _temp2.then) return _temp2.then(function () {});
3768
+ });
3769
+ return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3);
3770
+ }
3771
+ }();
3772
+ return _temp5 && _temp5.then ? _temp5.then(function () {
3773
+ return result;
3774
+ }) : result;
3775
+ });
3776
+ }, function (e) {
3777
+ improveAndRethrow(e, "getExistingSwapsDetailsWithFiatAmounts");
3778
+ }));
3779
+ } catch (e) {
3780
+ return Promise.reject(e);
3781
+ }
3782
+ };
3783
+ return SwapUtils;
3784
+ }();
3785
+
3786
+ function _catch(body, recover) {
3787
+ try {
3788
+ var result = body();
3789
+ } catch (e) {
3790
+ return recover(e);
3791
+ }
3792
+ if (result && result.then) {
3793
+ return result.then(void 0, recover);
3794
+ }
3795
+ return result;
3796
+ }
3797
+ var PublicSwapService = /*#__PURE__*/function () {
3798
+ function PublicSwapService(API_KEYS_PROXY_URL, cache) {
3799
+ this._swapProvider = new SwapspaceSwapProvider(API_KEYS_PROXY_URL, cache, function () {
3800
+ return null;
3801
+ }, false);
3802
+ }
3803
+ var _proto = PublicSwapService.prototype;
3804
+ _proto.initialize = function initialize() {
3805
+ try {
3806
+ var _this = this;
3807
+ var _temp = _catch(function () {
3808
+ return Promise.resolve(_this._swapProvider.initialize()).then(function () {
3809
+ SwapUtils.safeHandleRequestsLimitExceeding();
3810
+ });
3811
+ }, function (e) {
3812
+ Logger.logError(e, "PublicSwapService.initialize");
3813
+ });
3814
+ return Promise.resolve(_temp && _temp.then ? _temp.then(function () {}) : void 0);
3815
+ } catch (e) {
3816
+ return Promise.reject(e);
3817
+ }
3818
+ };
3819
+ _proto.getCurrenciesListForPublicSwap = function getCurrenciesListForPublicSwap(currencyThatShouldNotBeFirst) {
3820
+ if (currencyThatShouldNotBeFirst === void 0) {
3821
+ currencyThatShouldNotBeFirst = null;
3822
+ }
3823
+ try {
3824
+ var _this2 = this;
3825
+ var loggerSource = "getCurrenciesListForPublicSwap";
3826
+ return Promise.resolve(_catch(function () {
3827
+ // TODO: [dev] this is temporary hack, change it to use dedicated lists inside UI and also here
3828
+ return Promise.resolve(currencyThatShouldNotBeFirst ? _this2._swapProvider.getWithdrawalCurrencies(currencyThatShouldNotBeFirst) : _this2._swapProvider.getDepositCurrencies()).then(function (result) {
3829
+ var _result$coins;
3830
+ if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3831
+ SwapUtils.safeHandleRequestsLimitExceeding();
3832
+ return {
3833
+ result: false,
3834
+ reason: _this2.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
3835
+ };
3836
+ }
3837
+ Logger.log("Retrieved " + (result == null || (_result$coins = result.coins) == null ? void 0 : _result$coins.length) + " supported currencies for swap", loggerSource);
3838
+ if (result.coins[0] === currencyThatShouldNotBeFirst && result.coins.length > 1) {
3839
+ var temp = result.coins[0];
3840
+ result.coins[0] = result.coins[1];
3841
+ result.coins[1] = temp;
3842
+ }
3843
+ return {
3844
+ result: true,
3845
+ coins: result.coins
3846
+ };
3847
+ });
3848
+ }, function (e) {
3849
+ improveAndRethrow(e, loggerSource);
3850
+ }));
3851
+ } catch (e) {
3852
+ return Promise.reject(e);
3853
+ }
3854
+ }
3855
+ /**
3856
+ * Retrieves initial data for swapping two coins.
3857
+ *
3858
+ * @param fromCoin {Coin}
3859
+ * @param toCoin {Coin}
3860
+ * @return {Promise<{
3861
+ * result: true,
3862
+ * min: string,
3863
+ * fiatMin: (number|null),
3864
+ * max: string,
3865
+ * fiatMax: (number|null),
3866
+ * rate: (string|null)
3867
+ * }|{
3868
+ * result: false,
3869
+ * reason: string
3870
+ * }>}
3871
+ */
3872
+ ;
3873
+ _proto.getInitialPublicSwapData = function getInitialPublicSwapData(fromCoin, toCoin) {
3874
+ try {
3875
+ var _this3 = this;
3876
+ return Promise.resolve(_catch(function () {
3877
+ return Promise.resolve(SwapUtils.getInitialSwapData(_this3._swapProvider, fromCoin, toCoin)).then(function (result) {
3878
+ if (!result.result) {
3879
+ if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3880
+ SwapUtils.safeHandleRequestsLimitExceeding();
3881
+ return {
3882
+ result: false,
3883
+ reason: _this3.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
3884
+ };
3885
+ }
3886
+ if (result.reason === SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED) {
3887
+ return {
3888
+ result: false,
3889
+ reason: _this3.PUBLIC_SWAP_DETAILS_FAIL_REASONS.PAIR_NOT_SUPPORTED
3890
+ };
3891
+ }
3892
+ }
3893
+ return result;
3894
+ });
3895
+ }, function (e) {
3896
+ improveAndRethrow(e, "getInitialPublicSwapData");
3897
+ }));
3898
+ } catch (e) {
3899
+ return Promise.reject(e);
3900
+ }
3901
+ }
3902
+ /**
3903
+ * Retrieves swap details that can be used to create swap.
3904
+ *
3905
+ * @param fromCoin {Coin}
3906
+ * @param toCoin {Coin}
3907
+ * @param fromAmountCoins {string}
3908
+ * @return {Promise<{
3909
+ * result: false,
3910
+ * reason: string,
3911
+ * min: (string|null),
3912
+ * max: (string|null),
3913
+ * rate: (string|undefined),
3914
+ * fiatMin: (number|null),
3915
+ * fiatMax: (number|null)
3916
+ * }|{
3917
+ * result: true,
3918
+ * swapCreationInfo: PublicSwapCreationInfo
3919
+ * }>}
3920
+ */
3921
+ ;
3922
+ _proto.getPublicSwapDetails = function getPublicSwapDetails(fromCoin, toCoin, fromAmountCoins) {
3923
+ try {
3924
+ var _this4 = this;
3925
+ var loggerSource = "getPublicSwapDetails";
3926
+ return Promise.resolve(_catch(function () {
3927
+ return Promise.resolve(_this4._swapProvider.getCoinToUSDTRate(fromCoin)).then(function (_this4$_swapProvider$) {
3928
+ var _this4$_swapProvider$2;
3929
+ var coinUsdtRate = (_this4$_swapProvider$2 = _this4$_swapProvider$ == null ? void 0 : _this4$_swapProvider$.rate) != null ? _this4$_swapProvider$2 : null;
3930
+ return Promise.resolve(_this4._swapProvider.getSwapInfo(fromCoin, toCoin, fromAmountCoins, coinUsdtRate)).then(function (details) {
3931
+ var _result$swapCreationI, _result$swapCreationI2;
3932
+ var min = details.result ? details.min : details.smallestMin;
3933
+ var max = details.result ? details.max : details.greatestMax;
3934
+ var fiatMin = null,
3935
+ fiatMax = null;
3936
+ if (coinUsdtRate != null) {
3937
+ if (min != null) {
3938
+ fiatMin = bignumber_js.BigNumber(min).times(coinUsdtRate).toFixed(_this4._fiatDecimalsCount);
3939
+ }
3940
+ if (max != null) {
3941
+ fiatMax = bignumber_js.BigNumber(max).times(coinUsdtRate).toFixed(_this4._fiatDecimalsCount);
3942
+ }
3943
+ }
3944
+ var composeFailResult = function composeFailResult(reason) {
3945
+ var _details$rate;
3946
+ return {
3947
+ result: false,
3948
+ reason: reason,
3949
+ min: min != null ? min : null,
3950
+ fiatMin: fiatMin,
3951
+ max: max != null ? max : null,
3952
+ fiatMax: fiatMax,
3953
+ rate: (_details$rate = details.rate) != null ? _details$rate : null
3954
+ };
3955
+ };
3956
+ if (!details.result) {
3957
+ if ((details == null ? void 0 : details.reason) === SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED) return composeFailResult(_this4.PUBLIC_SWAP_DETAILS_FAIL_REASONS.PAIR_NOT_SUPPORTED);else if ((details == null ? void 0 : details.reason) === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
3958
+ SwapUtils.safeHandleRequestsLimitExceeding();
3959
+ return composeFailResult(_this4.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED);
3960
+ }
3961
+ }
3962
+ var fromAmountBigNumber = bignumber_js.BigNumber(fromAmountCoins);
3963
+ if (typeof min === "string" && fromAmountBigNumber.lt(min)) {
3964
+ return composeFailResult(_this4.PUBLIC_SWAP_DETAILS_FAIL_REASONS.AMOUNT_LESS_THAN_MIN_SWAPPABLE);
3965
+ } else if (typeof max === "string" && fromAmountBigNumber.gt(max)) {
3966
+ return composeFailResult(_this4.PUBLIC_SWAP_DETAILS_FAIL_REASONS.AMOUNT_HIGHER_THAN_MAX_SWAPPABLE);
3967
+ }
3968
+ var toAmountCoins = AmountUtils.trim(fromAmountBigNumber.times(details.rate), fromCoin.digits);
3969
+ var result = {
3970
+ result: true,
3971
+ swapCreationInfo: new PublicSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, details.rate, details.rawSwapData, min, fiatMin, max, fiatMax, details.durationMinutesRange)
3972
+ };
3973
+ Logger.log("Result: " + safeStringify({
3974
+ result: result.result,
3975
+ swapCreationInfo: _extends({}, result.swapCreationInfo, {
3976
+ fromCoin: result == null || (_result$swapCreationI = result.swapCreationInfo) == null || (_result$swapCreationI = _result$swapCreationI.fromCoin) == null ? void 0 : _result$swapCreationI.ticker,
3977
+ toCoin: result == null || (_result$swapCreationI2 = result.swapCreationInfo) == null || (_result$swapCreationI2 = _result$swapCreationI2.toCoin) == null ? void 0 : _result$swapCreationI2.ticker
3978
+ })
3979
+ }), loggerSource);
3980
+ return result;
3981
+ });
3982
+ });
3983
+ }, function (e) {
3984
+ improveAndRethrow(e, loggerSource);
3985
+ }));
3986
+ } catch (e) {
3987
+ return Promise.reject(e);
3988
+ }
3989
+ }
3990
+ /**
3991
+ * Creates swap by given params.
3992
+ *
3993
+ * @param fromCoin {Coin}
3994
+ * @param toCoin {Coin}
3995
+ * @param fromAmount {string}
3996
+ * @param swapCreationInfo {PublicSwapCreationInfo}
3997
+ * @param toAddress {string}
3998
+ * @param refundAddress {string}
3999
+ * @return {Promise<{
4000
+ * result: true,
4001
+ * fiatCurrencyCode: string,
4002
+ * toCoin: Coin,
4003
+ * fromAmountFiat: (number|null),
4004
+ * address: string,
4005
+ * durationMinutesRange: string,
4006
+ * fromAmount: string,
4007
+ * toAmount: string,
4008
+ * toAmountFiat: (number|null),
4009
+ * fiatCurrencyDecimals: number,
4010
+ * fromCoin: Coin,
4011
+ * rate: string,
4012
+ * swapId: string
4013
+ * }|{
4014
+ * result: false,
4015
+ * reason: string
4016
+ * }>}
4017
+ */
4018
+ ;
4019
+ _proto.createPublicSwap = function createPublicSwap(fromCoin, toCoin, fromAmount, swapCreationInfo, toAddress, refundAddress, clientIp) {
4020
+ try {
4021
+ var _this5 = this;
4022
+ var loggerSource = "createPublicSwap";
4023
+ return Promise.resolve(_catch(function () {
4024
+ var _swapCreationInfo$fro, _swapCreationInfo$toC;
4025
+ if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof fromAmount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string" || !(swapCreationInfo instanceof PublicSwapCreationInfo)) {
4026
+ throw new Error("Wrong input: " + fromCoin.ticker + " " + toCoin.ticker + " " + fromAmount + " " + swapCreationInfo);
4027
+ }
4028
+ Logger.log("Start: " + fromAmount + " " + fromCoin.ticker + " -> " + toCoin.ticker + ". Details: " + safeStringify(_extends({}, swapCreationInfo, {
4029
+ fromCoin: swapCreationInfo == null || (_swapCreationInfo$fro = swapCreationInfo.fromCoin) == null ? void 0 : _swapCreationInfo$fro.ticker,
4030
+ toCoin: swapCreationInfo == null || (_swapCreationInfo$toC = swapCreationInfo.toCoin) == null ? void 0 : _swapCreationInfo$toC.ticker
4031
+ })), loggerSource);
4032
+ return Promise.resolve(_this5._swapProvider.createSwap(fromCoin, toCoin, fromAmount, toAddress, refundAddress, swapCreationInfo.rawSwapData, clientIp)).then(function (result) {
4033
+ var _exit;
4034
+ function _temp5(_result5) {
4035
+ if (_exit) return _result5;
4036
+ throw new Error("Unexpected result from provider " + safeStringify(result));
4037
+ }
4038
+ Logger.log("Created:" + safeStringify(_extends({}, result, {
4039
+ fromCoin: fromCoin == null ? void 0 : fromCoin.ticker,
4040
+ toCoin: toCoin == null ? void 0 : toCoin.ticker
4041
+ })), loggerSource);
4042
+ if (!(result != null && result.result)) {
4043
+ if ((result == null ? void 0 : result.reason) === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
4044
+ SwapUtils.safeHandleRequestsLimitExceeding();
4045
+ return {
4046
+ result: false,
4047
+ reason: _this5.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
4048
+ };
4049
+ }
4050
+ if ((result == null ? void 0 : result.reason) === SwapProvider.CREATION_FAIL_REASONS.RETRIABLE_FAIL) {
4051
+ // TODO: [feature, high] implement retrying if one partner fail and we have another partners task_id=a07e367e488f4a4899613ac9056fa359
4052
+ // return {
4053
+ // result: false,
4054
+ // reason: this.SWAP_CREATION_FAIL_REASONS.RETRIABLE_FAIL,
4055
+ // };
4056
+ }
4057
+ }
4058
+ var _temp4 = function () {
4059
+ if (result.result && result != null && result.swapId) {
4060
+ var _temp3 = function _temp3() {
4061
+ EventBusInstance.dispatch(_this5.PUBLIC_SWAP_CREATED_EVENT, null, fromCoin.ticker, toCoin.ticker, _fromAmountFiat);
4062
+ var toReturn = {
4063
+ result: true,
4064
+ swapId: result.swapId,
4065
+ fromCoin: fromCoin,
4066
+ toCoin: toCoin,
4067
+ fromAmount: result.fromAmount,
4068
+ toAmount: result.toAmount,
4069
+ fromAmountFiat: _fromAmountFiat,
4070
+ toAmountFiat: _toAmountFiat,
4071
+ fiatCurrencyCode: "USD",
4072
+ fiatCurrencyDecimals: _this5._fiatDecimalsCount,
4073
+ rate: result.rate,
4074
+ durationMinutesRange: swapCreationInfo.durationMinutesRange,
4075
+ address: result.fromAddress // CRITICAL: this is the address to send coins to swaps provider
4076
+ };
4077
+ _this5._savePublicSwapIdLocally(result.swapId);
4078
+ Logger.log("Returning: " + safeStringify(_extends({}, toReturn, {
4079
+ fromCoin: fromCoin == null ? void 0 : fromCoin.ticker,
4080
+ toCoin: toCoin == null ? void 0 : toCoin.ticker
4081
+ })), loggerSource);
4082
+ _exit = 1;
4083
+ return toReturn;
4084
+ };
4085
+ var _fromAmountFiat = null,
4086
+ _toAmountFiat = null;
4087
+ var _temp2 = _catch(function () {
4088
+ return Promise.resolve(_this5._swapProvider.getCoinToUSDTRate(fromCoin)).then(function (_this5$_swapProvider$) {
4089
+ var _this5$_swapProvider$3;
4090
+ var fromCoinUsdtRate = (_this5$_swapProvider$3 = _this5$_swapProvider$ == null ? void 0 : _this5$_swapProvider$.rate) != null ? _this5$_swapProvider$3 : null;
4091
+ return Promise.resolve(_this5._swapProvider.getCoinToUSDTRate(fromCoin)).then(function (_this5$_swapProvider$2) {
4092
+ var _this5$_swapProvider$4;
4093
+ var toCoinUsdtRate = (_this5$_swapProvider$4 = _this5$_swapProvider$2 == null ? void 0 : _this5$_swapProvider$2.rate) != null ? _this5$_swapProvider$4 : null;
4094
+ if (fromCoinUsdtRate != null && result.fromAmount != null) {
4095
+ _fromAmountFiat = bignumber_js.BigNumber(result.fromAmount).times(fromCoinUsdtRate).toFixed(_this5._fiatDecimalsCount);
4096
+ }
4097
+ if (toCoinUsdtRate != null && result.toAmount != null) {
4098
+ _toAmountFiat = bignumber_js.BigNumber(result.toAmount).times(toCoinUsdtRate).toFixed(_this5._fiatDecimalsCount);
4099
+ }
4100
+ });
4101
+ });
4102
+ }, function (e) {
4103
+ Logger.logError(e, loggerSource, "Failed to calculate fiat amounts for result");
4104
+ });
4105
+ return _temp2 && _temp2.then ? _temp2.then(_temp3) : _temp3(_temp2);
4106
+ }
4107
+ }();
4108
+ return _temp4 && _temp4.then ? _temp4.then(_temp5) : _temp5(_temp4);
4109
+ });
4110
+ }, function (e) {
4111
+ improveAndRethrow(e, loggerSource);
4112
+ }));
4113
+ } catch (e) {
4114
+ return Promise.reject(e);
4115
+ }
4116
+ }
4117
+ /**
4118
+ * Retrieves swap details and status for existing swaps by their ids.
4119
+ *
4120
+ * @param swapIds {string[]}
4121
+ * @return {Promise<{
4122
+ * result: true,
4123
+ * swaps: ExistingSwapWithFiatData[]
4124
+ * }|{
4125
+ * result: false,
4126
+ * reason: string
4127
+ * }>}
4128
+ * error reason is one of PUBLIC_SWAPS_COMMON_ERRORS
4129
+ */
4130
+ ;
4131
+ _proto.getPublicExistingSwapDetailsAndStatus = function getPublicExistingSwapDetailsAndStatus(swapIds) {
4132
+ try {
4133
+ var _this6 = this;
4134
+ var loggerSource = "getPublicExistingSwapDetailsAndStatus";
4135
+ return Promise.resolve(_catch(function () {
4136
+ return Promise.resolve(SwapUtils.getExistingSwapsDetailsWithFiatAmounts(_this6._swapProvider, swapIds)).then(function (result) {
4137
+ if (!(result != null && result.result)) {
4138
+ if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
4139
+ SwapUtils.safeHandleRequestsLimitExceeding();
4140
+ return {
4141
+ result: false,
4142
+ reason: _this6.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
4143
+ };
4144
+ }
4145
+ throw new Error("Unknown reason: " + (result == null ? void 0 : result.reason));
4146
+ }
4147
+ return result;
4148
+ });
4149
+ }, function (e) {
4150
+ improveAndRethrow(e, loggerSource);
4151
+ }));
4152
+ } catch (e) {
4153
+ return Promise.reject(e);
4154
+ }
4155
+ }
4156
+ /**
4157
+ * Retrieves the whole available swaps history by ids saved locally.
4158
+ *
4159
+ * @return {Promise<{
4160
+ * result: true,
4161
+ * swaps: ExistingSwapWithFiatData[]
4162
+ * }|{
4163
+ * result: false,
4164
+ * reason: string
4165
+ * }>}
4166
+ */
4167
+ ;
4168
+ _proto.getPublicSwapsHistory = function getPublicSwapsHistory() {
4169
+ try {
4170
+ var _exit2;
4171
+ var _this7 = this;
4172
+ return Promise.resolve(_catch(function () {
4173
+ function _temp7(_result7) {
4174
+ return _exit2 ? _result7 : {
4175
+ result: true,
4176
+ swaps: []
4177
+ };
4178
+ }
4179
+ var swapIds = _this7._getPublicSwapIdsSavedLocally();
4180
+ var _temp6 = function () {
4181
+ if (swapIds.length) {
4182
+ return Promise.resolve(_this7.getPublicExistingSwapDetailsAndStatus(swapIds)).then(function (_await$_this7$getPubl) {
4183
+ _exit2 = 1;
4184
+ return _await$_this7$getPubl;
4185
+ });
4186
+ }
4187
+ }();
4188
+ return _temp6 && _temp6.then ? _temp6.then(_temp7) : _temp7(_temp6);
4189
+ }, function (e) {
4190
+ improveAndRethrow(e, "getPublicSwapsHistory");
4191
+ }));
4192
+ } catch (e) {
4193
+ return Promise.reject(e);
4194
+ }
4195
+ }
4196
+ /**
4197
+ * @param swapId {string}
4198
+ * @private
4199
+ */
4200
+ ;
4201
+ _proto._savePublicSwapIdLocally = function _savePublicSwapIdLocally(swapId) {
4202
+ if (typeof window !== "undefined") {
4203
+ try {
4204
+ var saved = localStorage.getItem("publicSwapIds");
4205
+ var ids = typeof saved === "string" && saved.length > 0 ? saved.split(",") : [];
4206
+ ids.push(swapId);
4207
+ localStorage.setItem("publicSwapIds", ids.join(","));
4208
+ } catch (e) {
4209
+ improveAndRethrow(e, "_savePublicSwapIdLocally");
4210
+ }
4211
+ }
4212
+ }
4213
+
4214
+ /**
4215
+ * @private
4216
+ * @return {string[]}
4217
+ */;
4218
+ _proto._getPublicSwapIdsSavedLocally = function _getPublicSwapIdsSavedLocally() {
4219
+ if (typeof window !== "undefined") {
4220
+ try {
4221
+ var saved = localStorage.getItem("publicSwapIds");
4222
+ return typeof saved === "string" && saved.length > 0 ? saved.split(",") : [];
4223
+ } catch (e) {
4224
+ improveAndRethrow(e, "_getPublicSwapIdsSavedLocally");
4225
+ }
4226
+ }
4227
+ }
4228
+
4229
+ /**
4230
+ * @param coinOrTicker {Coin|string}
4231
+ * @return {string} icon URL (ready to use)
4232
+ */;
4233
+ _proto.getAssetIconUrl = function getAssetIconUrl(coinOrTicker) {
4234
+ return this._swapProvider.getIconUrl(coinOrTicker);
4235
+ }
4236
+
4237
+ /**
4238
+ * @param ticker {string}
4239
+ * @return {Coin|null}
4240
+ */;
4241
+ _proto.getCoinByTickerIfPresent = function getCoinByTickerIfPresent(ticker) {
4242
+ return this._swapProvider.getCoinByTickerIfPresent(ticker);
4243
+ }
4244
+
4245
+ /**
4246
+ * TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
4247
+ * @param asset {Coin}
4248
+ * @return {Promise<string|null>}
4249
+ */;
4250
+ _proto.getAssetToUsdtRate = function getAssetToUsdtRate(asset) {
4251
+ try {
4252
+ var _this8 = this;
4253
+ return Promise.resolve(_catch(function () {
4254
+ return Promise.resolve(_this8._swapProvider.getCoinToUSDTRate(asset)).then(function (result) {
4255
+ var _result$rate;
4256
+ return (_result$rate = result == null ? void 0 : result.rate) != null ? _result$rate : null;
4257
+ });
4258
+ }, function (e) {
4259
+ improveAndRethrow(e, "getAssetToUsdtRate");
4260
+ }));
4261
+ } catch (e) {
4262
+ return Promise.reject(e);
4263
+ }
4264
+ }
4265
+ /**
4266
+ * @param asset {Coin}
4267
+ * @param address {string}
4268
+ * @return {boolean}
4269
+ */
4270
+ ;
4271
+ _proto.isAddressValidForAsset = function isAddressValidForAsset(asset, address) {
4272
+ try {
4273
+ return this._swapProvider.isAddressValidForAsset(asset, address);
4274
+ } catch (e) {
4275
+ improveAndRethrow(e, "isAddressValidForAsset");
4276
+ }
4277
+ };
4278
+ return PublicSwapService;
4279
+ }();
4280
+ PublicSwapService.PUBLIC_SWAP_CREATED_EVENT = "publicSwapCreatedEvent";
4281
+ PublicSwapService.PUBLIC_SWAPS_COMMON_ERRORS = {
4282
+ REQUESTS_LIMIT_EXCEEDED: "requestsLimitExceeded"
4283
+ };
4284
+ PublicSwapService.PUBLIC_SWAP_DETAILS_FAIL_REASONS = {
4285
+ AMOUNT_LESS_THAN_MIN_SWAPPABLE: "amountLessThanMinSwappable",
4286
+ AMOUNT_HIGHER_THAN_MAX_SWAPPABLE: "amountHigherThanMaxSwappable",
4287
+ PAIR_NOT_SUPPORTED: "pairNotSupported"
4288
+ };
4289
+ PublicSwapService._fiatDecimalsCount = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
4290
+
3470
4291
  exports.AmountUtils = AmountUtils;
3471
4292
  exports.AssetIcon = AssetIcon;
3472
4293
  exports.Blockchain = Blockchain;
@@ -3482,8 +4303,10 @@ exports.Logger = Logger;
3482
4303
  exports.LogsStorage = LogsStorage;
3483
4304
  exports.Protocol = Protocol;
3484
4305
  exports.PublicSwapCreationInfo = PublicSwapCreationInfo;
4306
+ exports.PublicSwapService = PublicSwapService;
3485
4307
  exports.SupportChat = SupportChat;
3486
4308
  exports.SwapProvider = SwapProvider;
4309
+ exports.SwapUtils = SwapUtils;
3487
4310
  exports.SwapspaceSwapProvider = SwapspaceSwapProvider;
3488
4311
  exports.improveAndRethrow = improveAndRethrow;
3489
4312
  exports.safeStringify = safeStringify;