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