@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.modern.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: {} };
|
|
@@ -2312,6 +2313,21 @@ class Cache {
|
|
|
2312
2313
|
}
|
|
2313
2314
|
}
|
|
2314
2315
|
|
|
2316
|
+
class EmailsApi {
|
|
2317
|
+
static async sendEmail(subject, body) {
|
|
2318
|
+
try {
|
|
2319
|
+
const url = `${window.location.protocol + "//" + window.location.host}/api/v1/${this.serverEndpointEntity}`;
|
|
2320
|
+
await axios.post(url, {
|
|
2321
|
+
subject,
|
|
2322
|
+
body
|
|
2323
|
+
});
|
|
2324
|
+
} catch (e) {
|
|
2325
|
+
improveAndRethrow(e, "sendEmail", subject + body);
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
EmailsApi.serverEndpointEntity = "emails";
|
|
2330
|
+
|
|
2315
2331
|
class ExistingSwap {
|
|
2316
2332
|
/**
|
|
2317
2333
|
* @param swapId {string}
|
|
@@ -2354,6 +2370,78 @@ class ExistingSwap {
|
|
|
2354
2370
|
}
|
|
2355
2371
|
}
|
|
2356
2372
|
|
|
2373
|
+
class ExistingSwapWithFiatData extends ExistingSwap {
|
|
2374
|
+
/**
|
|
2375
|
+
* @param swapId {string}
|
|
2376
|
+
* @param status {SwapProvider.SWAP_STATUSES}
|
|
2377
|
+
* @param createdAt {number}
|
|
2378
|
+
* @param expiresAt {number}
|
|
2379
|
+
* @param confirmations {number}
|
|
2380
|
+
* @param rate {string}
|
|
2381
|
+
* @param refundAddress {string}
|
|
2382
|
+
* @param fromCoin {Coin}
|
|
2383
|
+
* @param fromAmount {string}
|
|
2384
|
+
* @param fromTransactionId {string}
|
|
2385
|
+
* @param toCoin {Coin}
|
|
2386
|
+
* @param toAmount {string}
|
|
2387
|
+
* @param toTransactionId {string|null}
|
|
2388
|
+
* @param toAddress {string}
|
|
2389
|
+
* @param partner {string}
|
|
2390
|
+
* @param fromAmountFiat {number}
|
|
2391
|
+
* @param toAmountFiat {number}
|
|
2392
|
+
* @param fiatCurrencyCode {string}
|
|
2393
|
+
* @param fiatCurrencyDecimals {number}
|
|
2394
|
+
*/
|
|
2395
|
+
constructor(swapId, status, createdAt, expiresAt, confirmations, rate, refundAddress, payToAddress, fromCoin, fromAmount, fromTransactionId, fromTransactionLink, toCoin, toAmount, toTransactionId, toTransactionLink, toAddress, partner, fromAmountFiat, toAmountFiat, fiatCurrencyCode, fiatCurrencyDecimals) {
|
|
2396
|
+
super(swapId, status, createdAt, expiresAt, confirmations, rate, refundAddress, payToAddress, fromCoin, fromAmount, fromTransactionId, fromTransactionLink, toCoin, toAmount, toTransactionId, toTransactionLink, toAddress, partner);
|
|
2397
|
+
this.fromAmountFiat = fromAmountFiat;
|
|
2398
|
+
this.toAmountFiat = toAmountFiat;
|
|
2399
|
+
this.fiatCurrencyCode = fiatCurrencyCode;
|
|
2400
|
+
this.fiatCurrencyDecimals = fiatCurrencyDecimals;
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
/**
|
|
2404
|
+
* @param existingSwap {ExistingSwap}
|
|
2405
|
+
* @param fromAmountFiat {number}
|
|
2406
|
+
* @param toAmountFiat {number}
|
|
2407
|
+
* @param fiatCurrencyCode {string}
|
|
2408
|
+
* @param fiatCurrencyDecimals {number}
|
|
2409
|
+
* @return {ExistingSwapWithFiatData}
|
|
2410
|
+
*/
|
|
2411
|
+
static fromExistingSwap(existingSwap, fromAmountFiat, toAmountFiat, fiatCurrencyCode, fiatCurrencyDecimals) {
|
|
2412
|
+
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);
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
class PublicSwapCreationInfo {
|
|
2417
|
+
/**
|
|
2418
|
+
* @param fromCoin {Coin}
|
|
2419
|
+
* @param toCoin {Coin}
|
|
2420
|
+
* @param fromAmountCoins {string}
|
|
2421
|
+
* @param toAmountCoins {string}
|
|
2422
|
+
* @param rate {string}
|
|
2423
|
+
* @param rawSwapData {Object}
|
|
2424
|
+
* @param min {string}
|
|
2425
|
+
* @param fiatMin {number}
|
|
2426
|
+
* @param max {string}
|
|
2427
|
+
* @param fiatMax {number}
|
|
2428
|
+
* @param durationMinutesRange {string}
|
|
2429
|
+
*/
|
|
2430
|
+
constructor(fromCoin, toCoin, fromAmountCoins, toAmountCoins, rate, rawSwapData, min, fiatMin, max, fiatMax, durationMinutesRange) {
|
|
2431
|
+
this.fromCoin = fromCoin;
|
|
2432
|
+
this.toCoin = toCoin;
|
|
2433
|
+
this.fromAmountCoins = fromAmountCoins;
|
|
2434
|
+
this.toAmountCoins = toAmountCoins;
|
|
2435
|
+
this.rate = rate;
|
|
2436
|
+
this.rawSwapData = rawSwapData;
|
|
2437
|
+
this.min = min;
|
|
2438
|
+
this.fiatMin = fiatMin;
|
|
2439
|
+
this.max = max;
|
|
2440
|
+
this.fiatMax = fiatMax;
|
|
2441
|
+
this.durationMinutesRange = durationMinutesRange;
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2357
2445
|
class SwapProvider {
|
|
2358
2446
|
/**
|
|
2359
2447
|
* @return {Promise<void>}
|
|
@@ -2998,5 +3086,573 @@ class SwapspaceSwapProvider extends SwapProvider {
|
|
|
2998
3086
|
}
|
|
2999
3087
|
}
|
|
3000
3088
|
|
|
3001
|
-
|
|
3089
|
+
class SwapUtils {
|
|
3090
|
+
/**
|
|
3091
|
+
* Retrieves min and max limits for swapping giving currencies.
|
|
3092
|
+
* Returns also conversion rate if possible with predefined amount logic.
|
|
3093
|
+
* Rate is how many "to" coins does 1 "from" coin contain.
|
|
3094
|
+
*
|
|
3095
|
+
* In case of errors returns one of reasons
|
|
3096
|
+
* - SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED
|
|
3097
|
+
* - one of SwapProvider.COMMON_ERRORS.*
|
|
3098
|
+
*
|
|
3099
|
+
* @param swapProvider {SwapProvider}
|
|
3100
|
+
* @param fromCoin {Coin} enabled coin (to swap amount from)
|
|
3101
|
+
* @param toCoin {Coin}
|
|
3102
|
+
* @return {Promise<{
|
|
3103
|
+
* result: true,
|
|
3104
|
+
* min: string,
|
|
3105
|
+
* fiatMin: (number|null),
|
|
3106
|
+
* max: string,
|
|
3107
|
+
* fiatMax: (number|null),
|
|
3108
|
+
* rate: (string|null),
|
|
3109
|
+
* }|{
|
|
3110
|
+
* result: false,
|
|
3111
|
+
* reason: string
|
|
3112
|
+
* }>}
|
|
3113
|
+
*/
|
|
3114
|
+
static async getInitialSwapData(swapProvider, fromCoin, toCoin) {
|
|
3115
|
+
const loggerSource = "getInitialSwapData";
|
|
3116
|
+
try {
|
|
3117
|
+
/* We use some amount here that should fit at least some of the limits of the swap providers.
|
|
3118
|
+
* So we are going to get some rate to be used as the default for the on-flight calculations before we get
|
|
3119
|
+
* the exact rate (that should be retrieved by getSwapCreationInfo method) for a specific amount.
|
|
3120
|
+
*/
|
|
3121
|
+
const defaultAmountUsd = BigNumber("300");
|
|
3122
|
+
const coinUsdRate = await swapProvider.getCoinToUSDTRate(fromCoin);
|
|
3123
|
+
const coinAmountForDefaultUsdAmount = AmountUtils.trim(coinUsdRate.result ? defaultAmountUsd.div(coinUsdRate == null ? void 0 : coinUsdRate.rate) : defaultAmountUsd, fromCoin.digits);
|
|
3124
|
+
Logger.log(`Init: ${coinAmountForDefaultUsdAmount} ${fromCoin.ticker}->${toCoin.ticker}`, loggerSource);
|
|
3125
|
+
const details = await swapProvider.getSwapInfo(fromCoin, toCoin, coinAmountForDefaultUsdAmount);
|
|
3126
|
+
if (!details) {
|
|
3127
|
+
throw new Error("The details are empty: " + safeStringify(details));
|
|
3128
|
+
}
|
|
3129
|
+
if (!details.result) {
|
|
3130
|
+
Logger.log(`Failed with reason: ${details.reason}. ${fromCoin.ticker}->${toCoin.ticker}`, loggerSource);
|
|
3131
|
+
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) {
|
|
3132
|
+
return {
|
|
3133
|
+
result: false,
|
|
3134
|
+
reason: details.reason
|
|
3135
|
+
};
|
|
3136
|
+
} else {
|
|
3137
|
+
throw new Error("Unhandled error case: " + (details == null ? void 0 : details.reason));
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
let fiatMin = null;
|
|
3141
|
+
let fiatMax = null;
|
|
3142
|
+
if ((coinUsdRate == null ? void 0 : coinUsdRate.rate) != null) {
|
|
3143
|
+
const usdDecimals = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
|
|
3144
|
+
fiatMin = BigNumber(details == null ? void 0 : details.smallestMin).times(coinUsdRate.rate).toFixed(usdDecimals);
|
|
3145
|
+
fiatMax = BigNumber(details == null ? void 0 : details.greatestMax).times(coinUsdRate.rate).toFixed(usdDecimals);
|
|
3146
|
+
}
|
|
3147
|
+
const result = {
|
|
3148
|
+
result: true,
|
|
3149
|
+
min: details == null ? void 0 : details.smallestMin,
|
|
3150
|
+
fiatMin: fiatMin,
|
|
3151
|
+
max: details == null ? void 0 : details.greatestMax,
|
|
3152
|
+
fiatMax: fiatMax,
|
|
3153
|
+
rate: AmountUtils.trim(details.rate, 30)
|
|
3154
|
+
};
|
|
3155
|
+
Logger.log(`Returning: ${safeStringify(result)}`, loggerSource);
|
|
3156
|
+
return result;
|
|
3157
|
+
} catch (e) {
|
|
3158
|
+
Logger.log(`Failed to init swap: ${safeStringify(e)}`, loggerSource);
|
|
3159
|
+
improveAndRethrow(e, loggerSource);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
static safeHandleRequestsLimitExceeding() {
|
|
3163
|
+
(async function () {
|
|
3164
|
+
try {
|
|
3165
|
+
await EmailsApi.sendEmail("AUTOMATIC EMAIL - SWAPSPACE REQUESTS LIMIT EXCEEDED", "Requests limit exceeded. Urgently ask swaps provider support for limit increasing");
|
|
3166
|
+
} catch (e) {
|
|
3167
|
+
Logger.log(`Failed to handle limit exceeding ${safeStringify(e)}`, "_safeHandleRequestsLimitExceeding");
|
|
3168
|
+
}
|
|
3169
|
+
})();
|
|
3170
|
+
}
|
|
3171
|
+
|
|
3172
|
+
/**
|
|
3173
|
+
* If some swap is not found by id then there is no item in return list.
|
|
3174
|
+
*
|
|
3175
|
+
* @param swapProvider {SwapProvider}
|
|
3176
|
+
* @param swapIds {string[]}
|
|
3177
|
+
* @return {Promise<{
|
|
3178
|
+
* result: true,
|
|
3179
|
+
* swaps: ExistingSwapWithFiatData[]
|
|
3180
|
+
* }|{
|
|
3181
|
+
* result: false,
|
|
3182
|
+
* reason: string
|
|
3183
|
+
* }>}
|
|
3184
|
+
*/
|
|
3185
|
+
static async getExistingSwapsDetailsWithFiatAmounts(swapProvider, swapIds) {
|
|
3186
|
+
try {
|
|
3187
|
+
const result = await swapProvider.getExistingSwapsDetailsAndStatus(swapIds);
|
|
3188
|
+
if (result.result) {
|
|
3189
|
+
const extendedSwaps = [];
|
|
3190
|
+
for (let swap of result.swaps) {
|
|
3191
|
+
if (swap.status === SwapProvider.SWAP_STATUSES.REFUNDED) {
|
|
3192
|
+
const rate = await swapProvider.getCoinToUSDTRate(swap.fromCoin);
|
|
3193
|
+
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")));
|
|
3194
|
+
} else {
|
|
3195
|
+
const [fromCoinFiatRate, toConFiatRate] = await Promise.all([swapProvider.getCoinToUSDTRate(swap.fromCoin), swapProvider.getCoinToUSDTRate(swap.toCoin)]);
|
|
3196
|
+
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")));
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
result.swaps = extendedSwaps;
|
|
3200
|
+
}
|
|
3201
|
+
return result;
|
|
3202
|
+
} catch (e) {
|
|
3203
|
+
improveAndRethrow(e, "getExistingSwapsDetailsWithFiatAmounts");
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
|
|
3208
|
+
const API_KEYS_PROXY_URL = `${window.location.protocol + "//" + window.location.host}/api/v1/proxy`;
|
|
3209
|
+
const cache = new Cache(EventBusInstance);
|
|
3210
|
+
class PublicSwapService {
|
|
3211
|
+
static async initialize() {
|
|
3212
|
+
try {
|
|
3213
|
+
await this._swapProvider.initialize();
|
|
3214
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3215
|
+
} catch (e) {
|
|
3216
|
+
Logger.logError(e, "PublicSwapService.initialize");
|
|
3217
|
+
}
|
|
3218
|
+
}
|
|
3219
|
+
static async getCurrenciesListForPublicSwap(currencyThatShouldNotBeFirst = null) {
|
|
3220
|
+
const loggerSource = "getCurrenciesListForPublicSwap";
|
|
3221
|
+
try {
|
|
3222
|
+
var _result$coins;
|
|
3223
|
+
// TODO: [dev] this is temporary hack, change it to use dedicated lists inside UI and also here
|
|
3224
|
+
const result = currencyThatShouldNotBeFirst ? await this._swapProvider.getWithdrawalCurrencies(currencyThatShouldNotBeFirst) : await this._swapProvider.getDepositCurrencies();
|
|
3225
|
+
if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
|
|
3226
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3227
|
+
return {
|
|
3228
|
+
result: false,
|
|
3229
|
+
reason: this.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
|
|
3230
|
+
};
|
|
3231
|
+
}
|
|
3232
|
+
Logger.log(`Retrieved ${result == null || (_result$coins = result.coins) == null ? void 0 : _result$coins.length} supported currencies for swap`, loggerSource);
|
|
3233
|
+
if (result.coins[0] === currencyThatShouldNotBeFirst && result.coins.length > 1) {
|
|
3234
|
+
let temp = result.coins[0];
|
|
3235
|
+
result.coins[0] = result.coins[1];
|
|
3236
|
+
result.coins[1] = temp;
|
|
3237
|
+
}
|
|
3238
|
+
return {
|
|
3239
|
+
result: true,
|
|
3240
|
+
coins: result.coins
|
|
3241
|
+
};
|
|
3242
|
+
} catch (e) {
|
|
3243
|
+
improveAndRethrow(e, loggerSource);
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
|
|
3247
|
+
/**
|
|
3248
|
+
* Retrieves initial data for swapping two coins.
|
|
3249
|
+
*
|
|
3250
|
+
* @param fromCoin {Coin}
|
|
3251
|
+
* @param toCoin {Coin}
|
|
3252
|
+
* @return {Promise<{
|
|
3253
|
+
* result: true,
|
|
3254
|
+
* min: string,
|
|
3255
|
+
* fiatMin: (number|null),
|
|
3256
|
+
* max: string,
|
|
3257
|
+
* fiatMax: (number|null),
|
|
3258
|
+
* rate: (string|null)
|
|
3259
|
+
* }|{
|
|
3260
|
+
* result: false,
|
|
3261
|
+
* reason: string
|
|
3262
|
+
* }>}
|
|
3263
|
+
*/
|
|
3264
|
+
static async getInitialPublicSwapData(fromCoin, toCoin) {
|
|
3265
|
+
try {
|
|
3266
|
+
const result = await SwapUtils.getInitialSwapData(this._swapProvider, fromCoin, toCoin);
|
|
3267
|
+
if (!result.result) {
|
|
3268
|
+
if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
|
|
3269
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3270
|
+
return {
|
|
3271
|
+
result: false,
|
|
3272
|
+
reason: this.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
|
|
3273
|
+
};
|
|
3274
|
+
}
|
|
3275
|
+
if (result.reason === SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED) {
|
|
3276
|
+
return {
|
|
3277
|
+
result: false,
|
|
3278
|
+
reason: this.PUBLIC_SWAP_DETAILS_FAIL_REASONS.PAIR_NOT_SUPPORTED
|
|
3279
|
+
};
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
return result;
|
|
3283
|
+
} catch (e) {
|
|
3284
|
+
improveAndRethrow(e, "getInitialPublicSwapData");
|
|
3285
|
+
}
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
/**
|
|
3289
|
+
* Retrieves swap details that can be used to create swap.
|
|
3290
|
+
*
|
|
3291
|
+
* @param fromCoin {Coin}
|
|
3292
|
+
* @param toCoin {Coin}
|
|
3293
|
+
* @param fromAmountCoins {string}
|
|
3294
|
+
* @return {Promise<{
|
|
3295
|
+
* result: false,
|
|
3296
|
+
* reason: string,
|
|
3297
|
+
* min: (string|null),
|
|
3298
|
+
* max: (string|null),
|
|
3299
|
+
* rate: (string|undefined),
|
|
3300
|
+
* fiatMin: (number|null),
|
|
3301
|
+
* fiatMax: (number|null)
|
|
3302
|
+
* }|{
|
|
3303
|
+
* result: true,
|
|
3304
|
+
* swapCreationInfo: PublicSwapCreationInfo
|
|
3305
|
+
* }>}
|
|
3306
|
+
*/
|
|
3307
|
+
static async getPublicSwapDetails(fromCoin, toCoin, fromAmountCoins) {
|
|
3308
|
+
const loggerSource = "getPublicSwapDetails";
|
|
3309
|
+
try {
|
|
3310
|
+
var _await$this$_swapProv, _await$this$_swapProv2, _result$swapCreationI, _result$swapCreationI2;
|
|
3311
|
+
const coinUsdtRate = (_await$this$_swapProv = (_await$this$_swapProv2 = await this._swapProvider.getCoinToUSDTRate(fromCoin)) == null ? void 0 : _await$this$_swapProv2.rate) != null ? _await$this$_swapProv : null;
|
|
3312
|
+
const details = await this._swapProvider.getSwapInfo(fromCoin, toCoin, fromAmountCoins, coinUsdtRate);
|
|
3313
|
+
const min = details.result ? details.min : details.smallestMin;
|
|
3314
|
+
const max = details.result ? details.max : details.greatestMax;
|
|
3315
|
+
let fiatMin = null,
|
|
3316
|
+
fiatMax = null;
|
|
3317
|
+
if (coinUsdtRate != null) {
|
|
3318
|
+
if (min != null) {
|
|
3319
|
+
fiatMin = BigNumber(min).times(coinUsdtRate).toFixed(this._fiatDecimalsCount);
|
|
3320
|
+
}
|
|
3321
|
+
if (max != null) {
|
|
3322
|
+
fiatMax = BigNumber(max).times(coinUsdtRate).toFixed(this._fiatDecimalsCount);
|
|
3323
|
+
}
|
|
3324
|
+
}
|
|
3325
|
+
const composeFailResult = reason => {
|
|
3326
|
+
var _details$rate;
|
|
3327
|
+
return {
|
|
3328
|
+
result: false,
|
|
3329
|
+
reason: reason,
|
|
3330
|
+
min: min != null ? min : null,
|
|
3331
|
+
fiatMin: fiatMin,
|
|
3332
|
+
max: max != null ? max : null,
|
|
3333
|
+
fiatMax: fiatMax,
|
|
3334
|
+
rate: (_details$rate = details.rate) != null ? _details$rate : null
|
|
3335
|
+
};
|
|
3336
|
+
};
|
|
3337
|
+
if (!details.result) {
|
|
3338
|
+
if ((details == null ? void 0 : details.reason) === SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED) return composeFailResult(this.PUBLIC_SWAP_DETAILS_FAIL_REASONS.PAIR_NOT_SUPPORTED);else if ((details == null ? void 0 : details.reason) === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
|
|
3339
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3340
|
+
return composeFailResult(this.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED);
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
const fromAmountBigNumber = BigNumber(fromAmountCoins);
|
|
3344
|
+
if (typeof min === "string" && fromAmountBigNumber.lt(min)) {
|
|
3345
|
+
return composeFailResult(this.PUBLIC_SWAP_DETAILS_FAIL_REASONS.AMOUNT_LESS_THAN_MIN_SWAPPABLE);
|
|
3346
|
+
} else if (typeof max === "string" && fromAmountBigNumber.gt(max)) {
|
|
3347
|
+
return composeFailResult(this.PUBLIC_SWAP_DETAILS_FAIL_REASONS.AMOUNT_HIGHER_THAN_MAX_SWAPPABLE);
|
|
3348
|
+
}
|
|
3349
|
+
const toAmountCoins = AmountUtils.trim(fromAmountBigNumber.times(details.rate), fromCoin.digits);
|
|
3350
|
+
const result = {
|
|
3351
|
+
result: true,
|
|
3352
|
+
swapCreationInfo: new PublicSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, details.rate, details.rawSwapData, min, fiatMin, max, fiatMax, details.durationMinutesRange)
|
|
3353
|
+
};
|
|
3354
|
+
Logger.log(`Result: ${safeStringify({
|
|
3355
|
+
result: result.result,
|
|
3356
|
+
swapCreationInfo: _extends({}, result.swapCreationInfo, {
|
|
3357
|
+
fromCoin: result == null || (_result$swapCreationI = result.swapCreationInfo) == null || (_result$swapCreationI = _result$swapCreationI.fromCoin) == null ? void 0 : _result$swapCreationI.ticker,
|
|
3358
|
+
toCoin: result == null || (_result$swapCreationI2 = result.swapCreationInfo) == null || (_result$swapCreationI2 = _result$swapCreationI2.toCoin) == null ? void 0 : _result$swapCreationI2.ticker
|
|
3359
|
+
})
|
|
3360
|
+
})}`, loggerSource);
|
|
3361
|
+
return result;
|
|
3362
|
+
} catch (e) {
|
|
3363
|
+
improveAndRethrow(e, loggerSource);
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
|
|
3367
|
+
/**
|
|
3368
|
+
* Creates swap by given params.
|
|
3369
|
+
*
|
|
3370
|
+
* @param fromCoin {Coin}
|
|
3371
|
+
* @param toCoin {Coin}
|
|
3372
|
+
* @param fromAmount {string}
|
|
3373
|
+
* @param swapCreationInfo {PublicSwapCreationInfo}
|
|
3374
|
+
* @param toAddress {string}
|
|
3375
|
+
* @param refundAddress {string}
|
|
3376
|
+
* @return {Promise<{
|
|
3377
|
+
* result: true,
|
|
3378
|
+
* fiatCurrencyCode: string,
|
|
3379
|
+
* toCoin: Coin,
|
|
3380
|
+
* fromAmountFiat: (number|null),
|
|
3381
|
+
* address: string,
|
|
3382
|
+
* durationMinutesRange: string,
|
|
3383
|
+
* fromAmount: string,
|
|
3384
|
+
* toAmount: string,
|
|
3385
|
+
* toAmountFiat: (number|null),
|
|
3386
|
+
* fiatCurrencyDecimals: number,
|
|
3387
|
+
* fromCoin: Coin,
|
|
3388
|
+
* rate: string,
|
|
3389
|
+
* swapId: string
|
|
3390
|
+
* }|{
|
|
3391
|
+
* result: false,
|
|
3392
|
+
* reason: string
|
|
3393
|
+
* }>}
|
|
3394
|
+
*/
|
|
3395
|
+
static async createPublicSwap(fromCoin, toCoin, fromAmount, swapCreationInfo, toAddress, refundAddress, clientIp) {
|
|
3396
|
+
const loggerSource = "createPublicSwap";
|
|
3397
|
+
try {
|
|
3398
|
+
var _swapCreationInfo$fro, _swapCreationInfo$toC;
|
|
3399
|
+
if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof fromAmount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string" || !(swapCreationInfo instanceof PublicSwapCreationInfo)) {
|
|
3400
|
+
throw new Error(`Wrong input: ${fromCoin.ticker} ${toCoin.ticker} ${fromAmount} ${swapCreationInfo}`);
|
|
3401
|
+
}
|
|
3402
|
+
Logger.log(`Start: ${fromAmount} ${fromCoin.ticker} -> ${toCoin.ticker}. Details: ${safeStringify(_extends({}, swapCreationInfo, {
|
|
3403
|
+
fromCoin: swapCreationInfo == null || (_swapCreationInfo$fro = swapCreationInfo.fromCoin) == null ? void 0 : _swapCreationInfo$fro.ticker,
|
|
3404
|
+
toCoin: swapCreationInfo == null || (_swapCreationInfo$toC = swapCreationInfo.toCoin) == null ? void 0 : _swapCreationInfo$toC.ticker
|
|
3405
|
+
}))}`, loggerSource);
|
|
3406
|
+
const result = await this._swapProvider.createSwap(fromCoin, toCoin, fromAmount, toAddress, refundAddress, swapCreationInfo.rawSwapData, clientIp);
|
|
3407
|
+
Logger.log(`Created:${safeStringify(_extends({}, result, {
|
|
3408
|
+
fromCoin: fromCoin == null ? void 0 : fromCoin.ticker,
|
|
3409
|
+
toCoin: toCoin == null ? void 0 : toCoin.ticker
|
|
3410
|
+
}))}`, loggerSource);
|
|
3411
|
+
if (!(result != null && result.result)) {
|
|
3412
|
+
if ((result == null ? void 0 : result.reason) === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
|
|
3413
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3414
|
+
return {
|
|
3415
|
+
result: false,
|
|
3416
|
+
reason: this.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
|
|
3417
|
+
};
|
|
3418
|
+
}
|
|
3419
|
+
if ((result == null ? void 0 : result.reason) === SwapProvider.CREATION_FAIL_REASONS.RETRIABLE_FAIL) {
|
|
3420
|
+
// TODO: [feature, high] implement retrying if one partner fail and we have another partners task_id=a07e367e488f4a4899613ac9056fa359
|
|
3421
|
+
// return {
|
|
3422
|
+
// result: false,
|
|
3423
|
+
// reason: this.SWAP_CREATION_FAIL_REASONS.RETRIABLE_FAIL,
|
|
3424
|
+
// };
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
if (result.result && result != null && result.swapId) {
|
|
3428
|
+
let fromAmountFiat = null,
|
|
3429
|
+
toAmountFiat = null;
|
|
3430
|
+
try {
|
|
3431
|
+
var _await$this$_swapProv3, _await$this$_swapProv4, _await$this$_swapProv5, _await$this$_swapProv6;
|
|
3432
|
+
const fromCoinUsdtRate = (_await$this$_swapProv3 = (_await$this$_swapProv4 = await this._swapProvider.getCoinToUSDTRate(fromCoin)) == null ? void 0 : _await$this$_swapProv4.rate) != null ? _await$this$_swapProv3 : null;
|
|
3433
|
+
const toCoinUsdtRate = (_await$this$_swapProv5 = (_await$this$_swapProv6 = await this._swapProvider.getCoinToUSDTRate(fromCoin)) == null ? void 0 : _await$this$_swapProv6.rate) != null ? _await$this$_swapProv5 : null;
|
|
3434
|
+
if (fromCoinUsdtRate != null && result.fromAmount != null) {
|
|
3435
|
+
fromAmountFiat = BigNumber(result.fromAmount).times(fromCoinUsdtRate).toFixed(this._fiatDecimalsCount);
|
|
3436
|
+
}
|
|
3437
|
+
if (toCoinUsdtRate != null && result.toAmount != null) {
|
|
3438
|
+
toAmountFiat = BigNumber(result.toAmount).times(toCoinUsdtRate).toFixed(this._fiatDecimalsCount);
|
|
3439
|
+
}
|
|
3440
|
+
} catch (e) {
|
|
3441
|
+
Logger.logError(e, loggerSource, "Failed to calculate fiat amounts for result");
|
|
3442
|
+
}
|
|
3443
|
+
EventBusInstance.dispatch(this.PUBLIC_SWAP_CREATED_EVENT, null, fromCoin.ticker, toCoin.ticker, fromAmountFiat);
|
|
3444
|
+
const toReturn = {
|
|
3445
|
+
result: true,
|
|
3446
|
+
swapId: result.swapId,
|
|
3447
|
+
fromCoin: fromCoin,
|
|
3448
|
+
toCoin: toCoin,
|
|
3449
|
+
fromAmount: result.fromAmount,
|
|
3450
|
+
toAmount: result.toAmount,
|
|
3451
|
+
fromAmountFiat: fromAmountFiat,
|
|
3452
|
+
toAmountFiat: toAmountFiat,
|
|
3453
|
+
fiatCurrencyCode: "USD",
|
|
3454
|
+
fiatCurrencyDecimals: this._fiatDecimalsCount,
|
|
3455
|
+
rate: result.rate,
|
|
3456
|
+
durationMinutesRange: swapCreationInfo.durationMinutesRange,
|
|
3457
|
+
address: result.fromAddress // CRITICAL: this is the address to send coins to swaps provider
|
|
3458
|
+
};
|
|
3459
|
+
this._savePublicSwapIdLocally(result.swapId);
|
|
3460
|
+
Logger.log(`Returning: ${safeStringify(_extends({}, toReturn, {
|
|
3461
|
+
fromCoin: fromCoin == null ? void 0 : fromCoin.ticker,
|
|
3462
|
+
toCoin: toCoin == null ? void 0 : toCoin.ticker
|
|
3463
|
+
}))}`, loggerSource);
|
|
3464
|
+
return toReturn;
|
|
3465
|
+
}
|
|
3466
|
+
throw new Error(`Unexpected result from provider ${safeStringify(result)}`);
|
|
3467
|
+
} catch (e) {
|
|
3468
|
+
improveAndRethrow(e, loggerSource);
|
|
3469
|
+
}
|
|
3470
|
+
}
|
|
3471
|
+
|
|
3472
|
+
/**
|
|
3473
|
+
* Retrieves swap details and status for existing swaps by their ids.
|
|
3474
|
+
*
|
|
3475
|
+
* @param swapIds {string[]}
|
|
3476
|
+
* @return {Promise<{
|
|
3477
|
+
* result: true,
|
|
3478
|
+
* swaps: ExistingSwapWithFiatData[]
|
|
3479
|
+
* }|{
|
|
3480
|
+
* result: false,
|
|
3481
|
+
* reason: string
|
|
3482
|
+
* }>}
|
|
3483
|
+
* error reason is one of PUBLIC_SWAPS_COMMON_ERRORS
|
|
3484
|
+
*/
|
|
3485
|
+
static async getPublicExistingSwapDetailsAndStatus(swapIds) {
|
|
3486
|
+
const loggerSource = "getPublicExistingSwapDetailsAndStatus";
|
|
3487
|
+
try {
|
|
3488
|
+
const result = await SwapUtils.getExistingSwapsDetailsWithFiatAmounts(this._swapProvider, swapIds);
|
|
3489
|
+
if (!(result != null && result.result)) {
|
|
3490
|
+
if (result.reason === SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED) {
|
|
3491
|
+
SwapUtils.safeHandleRequestsLimitExceeding();
|
|
3492
|
+
return {
|
|
3493
|
+
result: false,
|
|
3494
|
+
reason: this.PUBLIC_SWAPS_COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
|
|
3495
|
+
};
|
|
3496
|
+
}
|
|
3497
|
+
throw new Error("Unknown reason: " + (result == null ? void 0 : result.reason));
|
|
3498
|
+
}
|
|
3499
|
+
return result;
|
|
3500
|
+
} catch (e) {
|
|
3501
|
+
improveAndRethrow(e, loggerSource);
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
|
|
3505
|
+
/**
|
|
3506
|
+
* Retrieves the whole available swaps history by ids saved locally.
|
|
3507
|
+
*
|
|
3508
|
+
* @return {Promise<{
|
|
3509
|
+
* result: true,
|
|
3510
|
+
* swaps: ExistingSwapWithFiatData[]
|
|
3511
|
+
* }|{
|
|
3512
|
+
* result: false,
|
|
3513
|
+
* reason: string
|
|
3514
|
+
* }>}
|
|
3515
|
+
*/
|
|
3516
|
+
static async getPublicSwapsHistory() {
|
|
3517
|
+
try {
|
|
3518
|
+
const swapIds = this._getPublicSwapIdsSavedLocally();
|
|
3519
|
+
if (swapIds.length) {
|
|
3520
|
+
return await this.getPublicExistingSwapDetailsAndStatus(swapIds);
|
|
3521
|
+
}
|
|
3522
|
+
return {
|
|
3523
|
+
result: true,
|
|
3524
|
+
swaps: []
|
|
3525
|
+
};
|
|
3526
|
+
} catch (e) {
|
|
3527
|
+
improveAndRethrow(e, "getPublicSwapsHistory");
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
|
|
3531
|
+
/**
|
|
3532
|
+
* @param swapId {string}
|
|
3533
|
+
* @private
|
|
3534
|
+
*/
|
|
3535
|
+
static _savePublicSwapIdLocally(swapId) {
|
|
3536
|
+
if (typeof window !== "undefined") {
|
|
3537
|
+
try {
|
|
3538
|
+
const saved = localStorage.getItem("publicSwapIds");
|
|
3539
|
+
const ids = typeof saved === "string" && saved.length > 0 ? saved.split(",") : [];
|
|
3540
|
+
ids.push(swapId);
|
|
3541
|
+
localStorage.setItem("publicSwapIds", ids.join(","));
|
|
3542
|
+
} catch (e) {
|
|
3543
|
+
improveAndRethrow(e, "_savePublicSwapIdLocally");
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
}
|
|
3547
|
+
|
|
3548
|
+
/**
|
|
3549
|
+
* @private
|
|
3550
|
+
* @return {string[]}
|
|
3551
|
+
*/
|
|
3552
|
+
static _getPublicSwapIdsSavedLocally() {
|
|
3553
|
+
if (typeof window !== "undefined") {
|
|
3554
|
+
try {
|
|
3555
|
+
const saved = localStorage.getItem("publicSwapIds");
|
|
3556
|
+
return typeof saved === "string" && saved.length > 0 ? saved.split(",") : [];
|
|
3557
|
+
} catch (e) {
|
|
3558
|
+
improveAndRethrow(e, "_getPublicSwapIdsSavedLocally");
|
|
3559
|
+
}
|
|
3560
|
+
}
|
|
3561
|
+
}
|
|
3562
|
+
|
|
3563
|
+
/**
|
|
3564
|
+
* @param coinOrTicker {Coin|string}
|
|
3565
|
+
* @return {string} icon URL (ready to use)
|
|
3566
|
+
*/
|
|
3567
|
+
static getAssetIconUrl(coinOrTicker) {
|
|
3568
|
+
return this._swapProvider.getIconUrl(coinOrTicker);
|
|
3569
|
+
}
|
|
3570
|
+
|
|
3571
|
+
/**
|
|
3572
|
+
* @param ticker {string}
|
|
3573
|
+
* @return {Coin|null}
|
|
3574
|
+
*/
|
|
3575
|
+
static getCoinByTickerIfPresent(ticker) {
|
|
3576
|
+
return this._swapProvider.getCoinByTickerIfPresent(ticker);
|
|
3577
|
+
}
|
|
3578
|
+
|
|
3579
|
+
/**
|
|
3580
|
+
* TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
|
|
3581
|
+
* @param asset {Coin}
|
|
3582
|
+
* @return {Promise<string|null>}
|
|
3583
|
+
*/
|
|
3584
|
+
static async getAssetToUsdtRate(asset) {
|
|
3585
|
+
try {
|
|
3586
|
+
var _result$rate;
|
|
3587
|
+
const result = await this._swapProvider.getCoinToUSDTRate(asset);
|
|
3588
|
+
return (_result$rate = result == null ? void 0 : result.rate) != null ? _result$rate : null;
|
|
3589
|
+
} catch (e) {
|
|
3590
|
+
improveAndRethrow(e, "getAssetToUsdtRate");
|
|
3591
|
+
}
|
|
3592
|
+
}
|
|
3593
|
+
|
|
3594
|
+
/**
|
|
3595
|
+
* @param asset {Coin}
|
|
3596
|
+
* @param address {string}
|
|
3597
|
+
* @return {boolean}
|
|
3598
|
+
*/
|
|
3599
|
+
static isAddressValidForAsset(asset, address) {
|
|
3600
|
+
try {
|
|
3601
|
+
return this._swapProvider.isAddressValidForAsset(asset, address);
|
|
3602
|
+
} catch (e) {
|
|
3603
|
+
improveAndRethrow(e, "isAddressValidForAsset");
|
|
3604
|
+
}
|
|
3605
|
+
}
|
|
3606
|
+
|
|
3607
|
+
// TODO: [dev] Remove if we don't need this inside the public swap steps
|
|
3608
|
+
// /**
|
|
3609
|
+
// * TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
|
|
3610
|
+
// * @param asset {Coin}
|
|
3611
|
+
// * @param amount {string}
|
|
3612
|
+
// * @return {Promise<string|null>}
|
|
3613
|
+
// */
|
|
3614
|
+
// static async convertSingleCoinAmountToUsdtOrNull(asset, amount) {
|
|
3615
|
+
// try {
|
|
3616
|
+
// const result = await this._swapProvider.getCoinToUSDTRate(asset);
|
|
3617
|
+
// if (result?.rate != null) {
|
|
3618
|
+
// const decimals = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
|
|
3619
|
+
// return BigNumber(amount).div(result?.rate).toFixed(decimals);
|
|
3620
|
+
// }
|
|
3621
|
+
// return null;
|
|
3622
|
+
// } catch (e) {
|
|
3623
|
+
// improveAndRethrow(e, "convertSingleCoinAmountToUsdtOrNull");
|
|
3624
|
+
// }
|
|
3625
|
+
// }
|
|
3626
|
+
//
|
|
3627
|
+
// /**
|
|
3628
|
+
// * TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
|
|
3629
|
+
// * @param asset {Coin}
|
|
3630
|
+
// * @param amount {string}
|
|
3631
|
+
// * @return {Promise<string|null>}
|
|
3632
|
+
// */
|
|
3633
|
+
// static async convertSingleUsdtAmountToCoinOrNull(asset, amount) {
|
|
3634
|
+
// try {
|
|
3635
|
+
// const result = await this._swapProvider.getCoinToUSDTRate(asset);
|
|
3636
|
+
// if (result?.rate != null) {
|
|
3637
|
+
// return BigNumber(amount).times(result?.rate).toFixed(asset.digits);
|
|
3638
|
+
// }
|
|
3639
|
+
// return null;
|
|
3640
|
+
// } catch (e) {
|
|
3641
|
+
// improveAndRethrow(e, "convertSingleUsdtAmountToCoinOrNull");
|
|
3642
|
+
// }
|
|
3643
|
+
// }
|
|
3644
|
+
}
|
|
3645
|
+
PublicSwapService.PUBLIC_SWAP_CREATED_EVENT = "publicSwapCreatedEvent";
|
|
3646
|
+
PublicSwapService._swapProvider = new SwapspaceSwapProvider(API_KEYS_PROXY_URL, cache, () => null, false);
|
|
3647
|
+
PublicSwapService.PUBLIC_SWAPS_COMMON_ERRORS = {
|
|
3648
|
+
REQUESTS_LIMIT_EXCEEDED: "requestsLimitExceeded"
|
|
3649
|
+
};
|
|
3650
|
+
PublicSwapService.PUBLIC_SWAP_DETAILS_FAIL_REASONS = {
|
|
3651
|
+
AMOUNT_LESS_THAN_MIN_SWAPPABLE: "amountLessThanMinSwappable",
|
|
3652
|
+
AMOUNT_HIGHER_THAN_MAX_SWAPPABLE: "amountHigherThanMaxSwappable",
|
|
3653
|
+
PAIR_NOT_SUPPORTED: "pairNotSupported"
|
|
3654
|
+
};
|
|
3655
|
+
PublicSwapService._fiatDecimalsCount = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
|
|
3656
|
+
|
|
3657
|
+
export { AmountUtils, AssetIcon, Blockchain, Button, Cache, Coin, EmailsApi, ExistingSwap, ExistingSwapWithFiatData, FiatCurrenciesService, LoadingDots, Logger, LogsStorage, Protocol, PublicSwapCreationInfo, PublicSwapService, SupportChat, SwapProvider, SwapUtils, SwapspaceSwapProvider, improveAndRethrow, safeStringify };
|
|
3002
3658
|
//# sourceMappingURL=index.modern.js.map
|