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