@rabbitio/ui-kit 1.0.0-beta.4 → 1.0.0-beta.40

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/README.md +23 -16
  3. package/dist/index.cjs +5336 -9
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.css +4480 -1635
  6. package/dist/index.css.map +1 -1
  7. package/dist/index.modern.js +3766 -11
  8. package/dist/index.modern.js.map +1 -1
  9. package/dist/index.module.js +5298 -11
  10. package/dist/index.module.js.map +1 -1
  11. package/dist/index.umd.js +5335 -12
  12. package/dist/index.umd.js.map +1 -1
  13. package/index.js +1 -1
  14. package/package.json +16 -22
  15. package/src/common/adapters/axiosAdapter.js +35 -0
  16. package/src/common/amountUtils.js +434 -0
  17. package/src/common/errorUtils.js +42 -0
  18. package/src/common/external-apis/apiGroups.js +55 -0
  19. package/src/common/fiatCurrenciesService.js +161 -0
  20. package/src/common/models/blockchain.js +10 -0
  21. package/src/common/models/coin.js +157 -0
  22. package/src/common/models/protocol.js +5 -0
  23. package/src/common/utils/cache.js +268 -0
  24. package/src/common/utils/emailAPI.js +18 -0
  25. package/src/common/utils/logging/logger.js +48 -0
  26. package/src/common/utils/logging/logsStorage.js +61 -0
  27. package/src/common/utils/postponeExecution.js +11 -0
  28. package/src/common/utils/safeStringify.js +50 -0
  29. package/src/components/atoms/AssetIcon/AssetIcon.jsx +55 -0
  30. package/src/components/atoms/AssetIcon/asset-icon.module.scss +42 -0
  31. package/{stories → src/components}/atoms/LoadingDots/LoadingDots.module.scss +1 -1
  32. package/src/components/atoms/SupportChat/SupportChat.jsx +48 -0
  33. package/{stories → src/components}/atoms/buttons/Button/Button.jsx +11 -7
  34. package/{stories → src/components}/atoms/buttons/Button/Button.module.scss +6 -1
  35. package/src/components/hooks/useCallHandlingErrors.js +26 -0
  36. package/src/components/hooks/useReferredState.js +24 -0
  37. package/src/components/utils/uiUtils.js +14 -0
  38. package/src/components/utils/urlQueryUtils.js +87 -0
  39. package/src/index.js +52 -0
  40. package/src/robustExteranlApiCallerService/cacheAndConcurrentRequestsResolver.js +559 -0
  41. package/src/robustExteranlApiCallerService/cachedRobustExternalApiCallerService.js +188 -0
  42. package/src/robustExteranlApiCallerService/cancelProcessing.js +29 -0
  43. package/src/robustExteranlApiCallerService/concurrentCalculationsMetadataHolder.js +103 -0
  44. package/src/robustExteranlApiCallerService/externalApiProvider.js +156 -0
  45. package/src/robustExteranlApiCallerService/externalServicesStatsCollector.js +82 -0
  46. package/src/robustExteranlApiCallerService/robustExternalAPICallerService.js +386 -0
  47. package/src/swaps-lib/external-apis/swapProvider.js +201 -0
  48. package/src/swaps-lib/external-apis/swapspaceSwapProvider.js +877 -0
  49. package/src/swaps-lib/models/baseSwapCreationInfo.js +40 -0
  50. package/src/swaps-lib/models/existingSwap.js +70 -0
  51. package/src/swaps-lib/models/existingSwapWithFiatData.js +130 -0
  52. package/src/swaps-lib/services/publicSwapService.js +674 -0
  53. package/src/swaps-lib/utils/swapUtils.js +219 -0
  54. package/stories/index.js +0 -2
  55. /package/{stories → src/components}/atoms/LoadingDots/LoadingDots.jsx +0 -0
@@ -0,0 +1,877 @@
1
+ import axios from "axios";
2
+ import { BigNumber } from "bignumber.js";
3
+
4
+ import { AmountUtils } from "../../common/amountUtils.js";
5
+ import { improveAndRethrow } from "../../common/errorUtils.js";
6
+ import { SwapProvider } from "./swapProvider.js";
7
+ import { ExistingSwap } from "../models/existingSwap.js";
8
+ import { Coin } from "../../common/models/coin.js";
9
+ import { Protocol } from "../../common/models/protocol.js";
10
+ import { safeStringify } from "../../common/utils/safeStringify.js";
11
+ import { Logger } from "../../common/utils/logging/logger.js";
12
+
13
+ export const BANNED_PARTNERS = ["stealthex", "changee", "coincraddle"];
14
+ const FALLBACK_ICON_URL = "https://rabbit.io/asset-icons/fallback.svg";
15
+
16
+ export class SwapspaceSwapProvider extends SwapProvider {
17
+ constructor(
18
+ apiKeysProxyUrl,
19
+ cache,
20
+ customCoinBuilder = (coin, network) => null,
21
+ useRestrictedCoinsSet = true
22
+ ) {
23
+ super();
24
+ this._supportedCoins = [];
25
+ this._URL = `${apiKeysProxyUrl}`;
26
+ this._maxRateDigits = 20;
27
+ this.useRestrictedCoinsSet = useRestrictedCoinsSet;
28
+ this._customCoinBuilder = customCoinBuilder;
29
+ this._cache = cache;
30
+ }
31
+
32
+ getSwapCreationInfoTtlMs() {
33
+ /* Actually 2 minutes and only relevant for some partners, but we use it
34
+ * (and even a bit smaller value) for better consistency */
35
+ return 110000;
36
+ }
37
+
38
+ async getDepositCurrencies() {
39
+ const loggerSource = "getDepositCurrencies";
40
+ try {
41
+ await this._fetchSupportedCurrenciesIfNeeded();
42
+ Logger.log(
43
+ `We have ${this._supportedCoins?.length} supported coins, getting depositable`,
44
+ loggerSource
45
+ );
46
+ return {
47
+ result: true,
48
+ coins: this._supportedCoins
49
+ .filter((item) => item.deposit)
50
+ .map((item) => item.coin),
51
+ };
52
+ } catch (e) {
53
+ if (e?.response?.status === 429) {
54
+ return {
55
+ result: false,
56
+ reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED,
57
+ };
58
+ }
59
+ improveAndRethrow(e, loggerSource);
60
+ }
61
+ }
62
+
63
+ async getAllSupportedCurrencies() {
64
+ const loggerSource = "getAllSupportedCurrencies";
65
+ try {
66
+ await this._fetchSupportedCurrenciesIfNeeded();
67
+ Logger.log(
68
+ `We have ${this._supportedCoins?.length} supported coins returning`,
69
+ loggerSource
70
+ );
71
+ return {
72
+ result: true,
73
+ coins: this._supportedCoins.map((item) => item.coin),
74
+ };
75
+ } catch (e) {
76
+ if (e?.response?.status === 429) {
77
+ return {
78
+ result: false,
79
+ reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED,
80
+ };
81
+ }
82
+ improveAndRethrow(e, loggerSource);
83
+ }
84
+ }
85
+
86
+ async getWithdrawalCurrencies(exceptCurrency = null) {
87
+ const loggerSource = "getWithdrawalCurrencies";
88
+ try {
89
+ await this._fetchSupportedCurrenciesIfNeeded();
90
+ Logger.log(
91
+ `We have ${this._supportedCoins?.length} supported coins, getting withdrawable`,
92
+ loggerSource
93
+ );
94
+ return {
95
+ result: true,
96
+ coins: this._supportedCoins
97
+ .filter(
98
+ (item) =>
99
+ item.withdrawal &&
100
+ (!exceptCurrency ||
101
+ item.coin?.ticker !== exceptCurrency?.ticker)
102
+ )
103
+ .map((item) => item.coin),
104
+ };
105
+ } catch (e) {
106
+ if (e?.response?.status === 429) {
107
+ return {
108
+ result: false,
109
+ reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED,
110
+ };
111
+ }
112
+ improveAndRethrow(e, loggerSource);
113
+ }
114
+ }
115
+
116
+ async initialize() {
117
+ await this._fetchSupportedCurrenciesIfNeeded();
118
+ }
119
+
120
+ getIconUrl(coinOrTicker) {
121
+ const loggerSource = "getIconUrl";
122
+ try {
123
+ let coin = coinOrTicker;
124
+ if (!(coinOrTicker instanceof Coin)) {
125
+ coin = this._supportedCoins.find(
126
+ (i) => i.coin.ticker === coinOrTicker
127
+ )?.coin;
128
+ }
129
+ return (
130
+ this._supportedCoins.find(
131
+ (item) => item.coin?.ticker === coin?.ticker
132
+ )?.iconURL ?? FALLBACK_ICON_URL
133
+ );
134
+ } catch (e) {
135
+ improveAndRethrow(e, loggerSource);
136
+ }
137
+ }
138
+
139
+ async _fetchSupportedCurrenciesIfNeeded() {
140
+ const loggerSource = "_fetchSupportedCurrenciesIfNeeded";
141
+ try {
142
+ if (!this._supportedCoins?.length) {
143
+ const rawResponse = await axios.get(
144
+ `${this._URL}/api/v2/currencies`
145
+ );
146
+ Logger.log(
147
+ `Retrieved ${rawResponse?.data?.length}`,
148
+ loggerSource
149
+ );
150
+ let allowedCoins = rawResponse?.data ?? [];
151
+ Logger.log(`Allowed cnt ${allowedCoins.length}`, loggerSource);
152
+
153
+ this._supportedCoins = allowedCoins
154
+ .map((item) => {
155
+ let coin = this._customCoinBuilder(
156
+ item.code,
157
+ item.network
158
+ );
159
+ if (!coin && !this.useRestrictedCoinsSet) {
160
+ /** Building coin object for coin that isn't supported OOB in Rabbit.
161
+ * We are doing this way to be able to use extended coins set for swaps.
162
+ * These temporary built coins are only for in-swap use, and we omit some usual
163
+ * coin details here.
164
+ * Ideally we should add some new abstractions e.g. BaseCoin:
165
+ * Coin will extend BaseCoin, SwapCoin will extend BaseCoin etc.
166
+ * But for now it is reasonable to use this simpler approach.
167
+ */
168
+ const code = item.code.toUpperCase();
169
+ const network = item.network.toUpperCase();
170
+ const ticker = `${code}${
171
+ code === network ? "" : network
172
+ }`;
173
+ const defaultDecimalPlacesForCoinNotSupportedOOB = 8;
174
+ const defaultMinConfirmationsForCoinNotSupportedOOB = 1;
175
+ // TODO: [dev] maybe we should recognize standard protocols?
176
+ coin = new Coin(
177
+ item.name,
178
+ ticker,
179
+ code,
180
+ defaultDecimalPlacesForCoinNotSupportedOOB,
181
+ null,
182
+ "",
183
+ null,
184
+ null,
185
+ defaultMinConfirmationsForCoinNotSupportedOOB,
186
+ null,
187
+ [],
188
+ 60000,
189
+ null, // We cannot recognize blockchain from swapspace data
190
+ code !== network ? new Protocol(network) : null,
191
+ item.contractAddress || null,
192
+ false
193
+ );
194
+ }
195
+ if (coin) {
196
+ return {
197
+ coin: coin,
198
+ code: item.code,
199
+ network: item.network,
200
+ hasExtraId: item.hasExtraId,
201
+ extraIdName: item.extraIdName,
202
+ isPopular: !!item?.popular,
203
+ iconURL: item.icon
204
+ ? `https://storage.swapspace.co${item.icon}`
205
+ : FALLBACK_ICON_URL,
206
+ deposit: item.deposit ?? false,
207
+ withdrawal: item.withdrawal ?? false,
208
+ validationRegexp: item.validationRegexp ?? null,
209
+ };
210
+ }
211
+
212
+ return [];
213
+ })
214
+ .flat();
215
+ this._putPopularCoinsFirst();
216
+ }
217
+ } catch (e) {
218
+ improveAndRethrow(e, loggerSource);
219
+ }
220
+ }
221
+
222
+ /**
223
+ * This method sort internal list putting popular (as swapspace thinks) coins to the top.
224
+ * This is just for users of this API if they don't care about the sorting - we just improve a list a bit this way.
225
+ * @private
226
+ */
227
+ _putPopularCoinsFirst() {
228
+ this._supportedCoins.sort((i1, i2) => {
229
+ if (i1.isPopular && !i2.isPopular) return -1;
230
+ if (i2.isPopular && !i1.isPopular) return 1;
231
+ return i1.coin.ticker > i2.coin.ticker
232
+ ? 1
233
+ : i1.coin.ticker < i2.coin.ticker
234
+ ? -1
235
+ : 0;
236
+ });
237
+ }
238
+
239
+ async getCoinToUSDTRate(coin) {
240
+ const loggerSource = "getCoinToUSDTRate";
241
+ try {
242
+ if (!coin) return null;
243
+
244
+ await this._fetchSupportedCurrenciesIfNeeded();
245
+
246
+ // Using USDT TRC20 as usually fee in this network is smaller than ERC20 and this network is widely used for USDT
247
+ const usdtTrc20 = this._supportedCoins.find(
248
+ (i) => i.coin.ticker === "USDTTRC20"
249
+ )?.coin;
250
+ if (!usdtTrc20) {
251
+ return { result: false };
252
+ }
253
+ const cached = this._cache.get(
254
+ "swapspace_usdt_rate_" + coin.ticker
255
+ );
256
+ if (cached != null) {
257
+ return {
258
+ result: true,
259
+ rate: cached,
260
+ };
261
+ }
262
+
263
+ Logger.log(
264
+ "Loading USDT->coin rate as not found in cache:",
265
+ coin?.ticker
266
+ );
267
+ const result = await this.getSwapInfo(usdtTrc20, coin, "5000");
268
+ if (!result.result) {
269
+ return { result: false };
270
+ }
271
+
272
+ // This calculation is not precise as we cannot recognize the actual fee and network fee. Just approximate.
273
+ const standardSwapspaceFeeMultiplier = 1.004; // usually 0.2%
274
+ const rate = BigNumber(1)
275
+ .div(
276
+ BigNumber(result.rate).times(standardSwapspaceFeeMultiplier)
277
+ )
278
+ .toString();
279
+ this._cache.put(
280
+ "swapspace_usdt_rate_" + coin.ticker,
281
+ rate,
282
+ 15 * 60000 // 15 minutes
283
+ );
284
+ return {
285
+ result: true,
286
+ rate: rate,
287
+ };
288
+ } catch (e) {
289
+ improveAndRethrow(e, loggerSource);
290
+ }
291
+ }
292
+
293
+ getCoinByTickerIfPresent(ticker) {
294
+ try {
295
+ const item = this._supportedCoins.find(
296
+ (i) => i.coin.ticker === ticker
297
+ );
298
+ return item?.coin ?? null;
299
+ } catch (e) {
300
+ improveAndRethrow(e, "getCoinByTickerIfPresent");
301
+ }
302
+ }
303
+
304
+ async getSwapInfo(fromCoin, toCoin, amountCoins, fromCoinToUsdRate = null) {
305
+ const loggerSource = "getSwapInfo";
306
+ try {
307
+ if (
308
+ !(fromCoin instanceof Coin) ||
309
+ !(toCoin instanceof Coin) ||
310
+ typeof amountCoins !== "string" ||
311
+ BigNumber(amountCoins).lt("0")
312
+ ) {
313
+ throw new Error(
314
+ `Wrong input params: ${amountCoins} ${fromCoin.ticker} -> ${toCoin.ticker}` +
315
+ (fromCoin instanceof Coin) +
316
+ (toCoin instanceof Coin)
317
+ );
318
+ }
319
+ const fromCoinSwapspaceDetails = this._supportedCoins.find(
320
+ (i) => i.coin?.ticker === fromCoin?.ticker
321
+ );
322
+ const toCoinSwapspaceDetails = this._supportedCoins.find(
323
+ (i) => i.coin?.ticker === toCoin?.ticker
324
+ );
325
+ if (!fromCoinSwapspaceDetails || !toCoinSwapspaceDetails) {
326
+ throw new Error(
327
+ "Failed to find swapspace coin details for: " +
328
+ fromCoin.ticker +
329
+ " -> " +
330
+ toCoin.ticker
331
+ );
332
+ }
333
+ if (
334
+ !fromCoinSwapspaceDetails.deposit ||
335
+ !toCoinSwapspaceDetails.withdrawal
336
+ ) {
337
+ return {
338
+ result: false,
339
+ reason: SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED,
340
+ };
341
+ }
342
+ /* Here we use not documented parameter 'estimated=false'. This parameter controls whether we want to use
343
+ * cached rate values stored in swapspace cache. Their support says they store at most for 30 sec.
344
+ * But we are better off using the most actual rates.
345
+ */
346
+ const response = await axios.get(
347
+ `${this._URL}/api/v2/amounts?fromCurrency=${fromCoinSwapspaceDetails.code}&fromNetwork=${fromCoinSwapspaceDetails.network}&toNetwork=${toCoinSwapspaceDetails.network}&toCurrency=${toCoinSwapspaceDetails.code}&amount=${amountCoins}&float=true&estimated=false`
348
+ );
349
+ Logger.log(
350
+ `Retrieved ${response?.data?.length} options`,
351
+ loggerSource
352
+ );
353
+ const options = Array.isArray(response.data) ? response.data : [];
354
+ const exchangesSupportingThePair = options.filter(
355
+ (exchange) =>
356
+ exchange?.exists &&
357
+ !BANNED_PARTNERS.find(
358
+ (bannedPartner) => bannedPartner === exchange?.partner
359
+ ) &&
360
+ exchange?.fixed === false &&
361
+ (exchange.min === 0 ||
362
+ exchange.max === 0 ||
363
+ exchange.max > exchange.min ||
364
+ ((typeof exchange.min !== "number" ||
365
+ typeof exchange.max !== "number") &&
366
+ exchange.toAmount > 0))
367
+ );
368
+ Logger.log(
369
+ `${exchangesSupportingThePair?.length} of them have exist=true`,
370
+ loggerSource
371
+ );
372
+ if (!exchangesSupportingThePair.length) {
373
+ return {
374
+ result: false,
375
+ reason: SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED,
376
+ };
377
+ }
378
+ const availableExchanges = exchangesSupportingThePair.filter(
379
+ (exchange) =>
380
+ typeof exchange?.toAmount === "number" &&
381
+ exchange.toAmount > 0
382
+ );
383
+ Logger.log(
384
+ `Available (having amountTo): ${safeStringify(
385
+ availableExchanges
386
+ )}`,
387
+ loggerSource
388
+ );
389
+ // min=0 or max=0 means there is no limit for the partner
390
+ let smallestMin = null;
391
+ if (
392
+ exchangesSupportingThePair.find((ex) =>
393
+ BigNumber(ex.min).isZero()
394
+ ) == null
395
+ ) {
396
+ smallestMin = exchangesSupportingThePair.reduce((prev, cur) => {
397
+ if (
398
+ typeof cur.min === "number" &&
399
+ (prev === null || BigNumber(cur.min).lt(prev))
400
+ )
401
+ return BigNumber(cur.min);
402
+ return prev;
403
+ }, null);
404
+ }
405
+ let greatestMax = null;
406
+ if (
407
+ exchangesSupportingThePair.find((ex) =>
408
+ BigNumber(ex.max).isZero()
409
+ ) == null
410
+ ) {
411
+ greatestMax = exchangesSupportingThePair.reduce((prev, cur) => {
412
+ if (
413
+ typeof cur.max === "number" &&
414
+ (prev === null || BigNumber(cur.max).gt(prev))
415
+ )
416
+ return BigNumber(cur.max);
417
+ return prev;
418
+ }, null);
419
+ }
420
+ let extraCoinsToFitMinMax = "0";
421
+ if (
422
+ typeof fromCoinToUsdRate === "string" &&
423
+ BigNumber(fromCoinToUsdRate).gt("0")
424
+ ) {
425
+ const extraUsdToFitMinMax = BigNumber("1"); // We correct the limits as the exact limit can fluctuate and cause failed swap creation
426
+ extraCoinsToFitMinMax = AmountUtils.trim(
427
+ extraUsdToFitMinMax.div(fromCoinToUsdRate),
428
+ fromCoin.digits
429
+ );
430
+ }
431
+ if (smallestMin instanceof BigNumber) {
432
+ smallestMin = AmountUtils.trim(
433
+ smallestMin.plus(extraCoinsToFitMinMax),
434
+ fromCoin.digits
435
+ );
436
+ }
437
+ if (greatestMax instanceof BigNumber) {
438
+ if (greatestMax > extraCoinsToFitMinMax) {
439
+ greatestMax = AmountUtils.trim(
440
+ greatestMax.minus(extraCoinsToFitMinMax),
441
+ fromCoin.digits
442
+ );
443
+ } else {
444
+ greatestMax = "0";
445
+ }
446
+ }
447
+
448
+ if (availableExchanges.length) {
449
+ const sorted = availableExchanges.sort(
450
+ (op1, op2) => op2.toAmount - op1.toAmount
451
+ );
452
+ const bestOpt = sorted[0];
453
+ Logger.log(
454
+ `Returning first option after sorting: ${safeStringify(
455
+ bestOpt
456
+ )}`,
457
+ loggerSource
458
+ );
459
+ let max = null;
460
+ let min = null;
461
+ if (extraCoinsToFitMinMax != null) {
462
+ if (typeof bestOpt.max === "number" && bestOpt.max !== 0) {
463
+ max = BigNumber(bestOpt.max).minus(
464
+ extraCoinsToFitMinMax
465
+ );
466
+ max = AmountUtils.trim(
467
+ max.lt(0) ? "0" : max,
468
+ fromCoin.digits
469
+ );
470
+ }
471
+ if (typeof bestOpt.min === "number" && bestOpt.min !== 0) {
472
+ min = AmountUtils.trim(
473
+ BigNumber(bestOpt.min).plus(extraCoinsToFitMinMax),
474
+ fromCoin.digits
475
+ );
476
+ }
477
+ }
478
+
479
+ const rate =
480
+ bestOpt.toAmount && bestOpt.fromAmount
481
+ ? BigNumber(bestOpt.toAmount).div(bestOpt.fromAmount)
482
+ : null;
483
+ return {
484
+ result: true,
485
+ min: min,
486
+ max: max,
487
+ smallestMin: smallestMin,
488
+ greatestMax: greatestMax,
489
+ rate:
490
+ rate != null
491
+ ? AmountUtils.trim(rate, this._maxRateDigits)
492
+ : null,
493
+ durationMinutesRange: bestOpt.duration ?? null,
494
+ rawSwapData: bestOpt,
495
+ };
496
+ }
497
+ const result = {
498
+ result: false,
499
+ reason:
500
+ smallestMin && BigNumber(amountCoins).lt(smallestMin)
501
+ ? SwapProvider.NO_SWAPS_REASONS.TOO_LOW
502
+ : greatestMax && BigNumber(amountCoins).gt(greatestMax)
503
+ ? SwapProvider.NO_SWAPS_REASONS.TOO_HIGH
504
+ : SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED,
505
+ smallestMin: smallestMin,
506
+ greatestMax: greatestMax,
507
+ };
508
+ Logger.log(
509
+ `Returning result ${safeStringify(result)}`,
510
+ loggerSource
511
+ );
512
+ return result;
513
+ } catch (e) {
514
+ if (e?.response?.status === 429) {
515
+ return {
516
+ result: false,
517
+ reason: SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED,
518
+ };
519
+ }
520
+ Logger.log(
521
+ `Internal swapspace/rabbit error when getting swap options ${safeStringify(
522
+ e
523
+ )}`,
524
+ loggerSource
525
+ );
526
+ improveAndRethrow(e, loggerSource);
527
+ }
528
+ }
529
+
530
+ async createSwap(
531
+ fromCoin,
532
+ toCoin,
533
+ amount,
534
+ toAddress,
535
+ refundAddress,
536
+ rawSwapData,
537
+ clientIpAddress,
538
+ toCurrencyExtraId = "",
539
+ refundExtraId = ""
540
+ ) {
541
+ const loggerSource = "createSwap";
542
+ const partner = rawSwapData?.partner;
543
+ try {
544
+ if (
545
+ !(fromCoin instanceof Coin) ||
546
+ !(toCoin instanceof Coin) ||
547
+ typeof amount !== "string" ||
548
+ typeof toAddress !== "string" ||
549
+ typeof refundAddress !== "string"
550
+ ) {
551
+ throw new Error(
552
+ `Invalid input: ${fromCoin} ${toCoin} ${amount} ${toAddress} ${refundAddress}`
553
+ );
554
+ }
555
+ if (
556
+ typeof partner !== "string" ||
557
+ typeof rawSwapData?.fromCurrency !== "string" ||
558
+ typeof rawSwapData?.fromNetwork !== "string" ||
559
+ typeof rawSwapData?.toCurrency !== "string" ||
560
+ typeof rawSwapData?.toNetwork !== "string" ||
561
+ typeof rawSwapData?.id !== "string" // can be just empty
562
+ ) {
563
+ throw new Error(
564
+ `Invalid raw swap data: ${safeStringify(rawSwapData)}`
565
+ );
566
+ }
567
+
568
+ await this._fetchSupportedCurrenciesIfNeeded();
569
+ const requestData = {
570
+ partner: partner,
571
+ fromCurrency: rawSwapData?.fromCurrency,
572
+ fromNetwork: rawSwapData?.fromNetwork,
573
+ toCurrency: rawSwapData?.toCurrency,
574
+ toNetwork: rawSwapData?.toNetwork,
575
+ address: toAddress,
576
+ amount: amount,
577
+ fixed: false,
578
+ extraId: toCurrencyExtraId ?? "",
579
+ refundExtraId: refundExtraId ?? "", // This param is not documented. But the refund is usually manual so this is not critical.
580
+ rateId: rawSwapData?.id,
581
+ userIp: clientIpAddress,
582
+ refund: refundAddress,
583
+ };
584
+
585
+ Logger.log(
586
+ `Sending create request: ${safeStringify(requestData)}`,
587
+ loggerSource
588
+ );
589
+ const response = await axios.post(
590
+ `${this._URL}/api/v2/exchange`,
591
+ requestData
592
+ );
593
+ const result = response.data;
594
+ Logger.log(
595
+ `Creation result ${safeStringify(result)}`,
596
+ loggerSource
597
+ );
598
+
599
+ if (result?.id) {
600
+ if (
601
+ typeof result?.from?.amount !== "number" ||
602
+ typeof result?.from?.address !== "string" ||
603
+ typeof result?.to?.amount !== "number" ||
604
+ typeof result?.to?.address !== "string"
605
+ )
606
+ throw new Error(`Wrong swap creation result ${result}`);
607
+ /* We use the returned rate preferably but if the retrieved
608
+ * rate 0/null/undefined we calculate it manually */
609
+ let rate = result.rate;
610
+ if (typeof rate !== "number" || BigNumber(rate).isZero()) {
611
+ rate = BigNumber(result?.to?.amount).div(
612
+ result?.from?.amount
613
+ );
614
+ } else {
615
+ rate = BigNumber(rate);
616
+ }
617
+
618
+ return {
619
+ result: true,
620
+ swapId: result?.id,
621
+ fromCoin: fromCoin,
622
+ fromAmount: AmountUtils.trim(
623
+ result?.from?.amount,
624
+ fromCoin.digits
625
+ ),
626
+ fromAddress: result?.from?.address,
627
+ toCoin: toCoin,
628
+ toAmount: AmountUtils.trim(
629
+ result?.to?.amount,
630
+ toCoin.digits
631
+ ),
632
+ toAddress: result?.to?.address,
633
+ fromCurrencyExtraId: result?.from?.extraId ?? "",
634
+ rate: AmountUtils.trim(rate, this._maxRateDigits),
635
+ };
636
+ }
637
+ const errorMessage = `Swap creation succeeded but the response is wrong: ${safeStringify(
638
+ response
639
+ )}`;
640
+ Logger.log(errorMessage, loggerSource);
641
+ throw new Error(errorMessage);
642
+ } catch (e) {
643
+ Logger.logError(
644
+ e,
645
+ loggerSource,
646
+ `Failed to create swap. Error is: ${safeStringify(e)}`
647
+ );
648
+ const composeFailResult = (reason) => ({
649
+ result: false,
650
+ reason: reason,
651
+ partner: partner,
652
+ });
653
+ const status = e?.response?.status;
654
+ const data = e?.response?.data;
655
+ if (status === 429) {
656
+ Logger.log(
657
+ `Returning fail - RPS limit exceeded ${data}`,
658
+ loggerSource
659
+ );
660
+ return composeFailResult(
661
+ SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
662
+ );
663
+ }
664
+ const texts422 = [
665
+ "Pair cannot be processed by",
666
+ "Currency not found",
667
+ "Amount maximum is",
668
+ "Amount minimum is",
669
+ ];
670
+ const text403 = "IP address is forbidden";
671
+ if (
672
+ typeof data === "string" &&
673
+ ((status === 403 && data.includes(text403)) ||
674
+ (status === 422 &&
675
+ texts422.find((text) => data.includes(text))))
676
+ ) {
677
+ Logger.log(
678
+ `Returning retriable fail: ${status} - ${data}, ${partner}`,
679
+ loggerSource
680
+ );
681
+ return composeFailResult(
682
+ SwapProvider.CREATION_FAIL_REASONS.RETRIABLE_FAIL
683
+ );
684
+ }
685
+ Logger.log(
686
+ `Internal swapspace/rabbit error for ${partner}: ${safeStringify(
687
+ e
688
+ )}`,
689
+ loggerSource
690
+ );
691
+ improveAndRethrow(e, loggerSource);
692
+ }
693
+ }
694
+
695
+ _mapSwapspaceStatusToRabbitStatus(status, isExpiredByTime) {
696
+ switch (status) {
697
+ case "waiting":
698
+ if (isExpiredByTime) {
699
+ return SwapProvider.SWAP_STATUSES.EXPIRED;
700
+ }
701
+ return SwapProvider.SWAP_STATUSES.WAITING_FOR_PAYMENT;
702
+ case "confirming":
703
+ return SwapProvider.SWAP_STATUSES.CONFIRMING;
704
+ case "exchanging":
705
+ return SwapProvider.SWAP_STATUSES.EXCHANGING;
706
+ case "sending":
707
+ return SwapProvider.SWAP_STATUSES.PAYMENT_RECEIVED;
708
+ case "finished":
709
+ return SwapProvider.SWAP_STATUSES.COMPLETED;
710
+ case "verifying":
711
+ return SwapProvider.SWAP_STATUSES.EXCHANGING;
712
+ case "refunded":
713
+ return SwapProvider.SWAP_STATUSES.REFUNDED;
714
+ case "expired":
715
+ return SwapProvider.SWAP_STATUSES.EXPIRED;
716
+ case "failed":
717
+ return SwapProvider.SWAP_STATUSES.FAILED;
718
+ default:
719
+ throw new Error(`Unknown swapspace status: ${status}`);
720
+ }
721
+ }
722
+
723
+ async getExistingSwapsDetailsAndStatus(swapIds) {
724
+ const loggerSource = "getExistingSwapsDetailsAndStatus";
725
+ try {
726
+ if (swapIds.find((id) => typeof id !== "string")) {
727
+ throw new Error(
728
+ "Swap id is not string: " + safeStringify(swapIds)
729
+ );
730
+ }
731
+ const getNotFailingOn404 = async (swapId) => {
732
+ try {
733
+ return await axios.get(
734
+ `${this._URL}/api/v2/exchange/${swapId}`
735
+ );
736
+ } catch (error) {
737
+ if (error?.response?.status === 404) return [];
738
+ throw error;
739
+ }
740
+ };
741
+ const responses = await Promise.all(
742
+ swapIds.map((swapId) => getNotFailingOn404(swapId))
743
+ );
744
+ const wo404 = responses.flat();
745
+ Logger.log(
746
+ "All swaps RAW: " + JSON.stringify(wo404.map((r) => r.data)),
747
+ loggerSource
748
+ );
749
+ const swaps = wo404
750
+ .map((r) => r.data)
751
+ .map((swap, index) => {
752
+ const fromCoin = this._supportedCoins.find(
753
+ (i) =>
754
+ i.code === swap.from.code &&
755
+ i.network === swap.from.network
756
+ )?.coin;
757
+ const toCoin = this._supportedCoins.find(
758
+ (i) =>
759
+ i.code === swap.to.code &&
760
+ i.network === swap.to.network
761
+ )?.coin;
762
+ if (!fromCoin || !toCoin) {
763
+ return []; // We skip swaps with not supported coins for now
764
+ }
765
+
766
+ const toUtcTimestamp = (timeStr) =>
767
+ Date.parse(
768
+ timeStr.match(/.+[Zz]$/) ? timeStr : `${timeStr}Z`
769
+ );
770
+ const expiresAt = toUtcTimestamp(swap.timestamps.expiresAt);
771
+ const isExpiredByTime = expiresAt < Date.now();
772
+ const status = this._mapSwapspaceStatusToRabbitStatus(
773
+ swap.status,
774
+ isExpiredByTime
775
+ );
776
+ const toDigits =
777
+ status === SwapProvider.SWAP_STATUSES.REFUNDED
778
+ ? fromCoin.digits
779
+ : toCoin.digits;
780
+ const addressToSendCoinsToSwapspace = swap.from.address;
781
+ return new ExistingSwap(
782
+ swapIds[index],
783
+ status,
784
+ toUtcTimestamp(swap.timestamps.createdAt),
785
+ expiresAt,
786
+ swap.confirmations,
787
+ AmountUtils.trim(swap.rate, this._maxRateDigits),
788
+ swap.refundAddress,
789
+ addressToSendCoinsToSwapspace,
790
+ fromCoin,
791
+ AmountUtils.trim(swap.from.amount, fromCoin.digits),
792
+ swap.from.transactionHash,
793
+ swap.blockExplorerTransactionUrl.from,
794
+ toCoin,
795
+ AmountUtils.trim(swap.to.amount, toDigits),
796
+ swap.to.transactionHash,
797
+ swap.blockExplorerTransactionUrl.to,
798
+ swap.to.address,
799
+ swap.partner,
800
+ swap.from.extraId ?? null,
801
+ swap.to.extraId ?? null,
802
+ swap.refundExtraId ?? null
803
+ );
804
+ })
805
+ .flat();
806
+ Logger.log(
807
+ `Swap details result ${safeStringify(swaps)}`,
808
+ loggerSource
809
+ );
810
+ return { result: true, swaps: swaps };
811
+ } catch (e) {
812
+ Logger.logError(
813
+ e,
814
+ loggerSource,
815
+ `Failed to get swap details. Error is: ${safeStringify(e)}`
816
+ );
817
+ const composeFailResult = (reason) => ({
818
+ result: false,
819
+ reason: reason,
820
+ });
821
+ const status = e?.response?.status;
822
+ const data = e?.response?.data;
823
+ if (status === 429) {
824
+ Logger.log(
825
+ `Returning fail - RPS limit exceeded ${data}`,
826
+ loggerSource
827
+ );
828
+ return composeFailResult(
829
+ SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
830
+ );
831
+ }
832
+ improveAndRethrow(e, loggerSource);
833
+ }
834
+ }
835
+
836
+ isAddressValidForAsset(asset, address) {
837
+ try {
838
+ const assetData = this._supportedCoins.find(
839
+ (i) => i.coin?.ticker === asset?.ticker
840
+ );
841
+ if (assetData) {
842
+ let corrected = assetData.validationRegexp.trim();
843
+ corrected =
844
+ corrected[0] === "/" ? corrected.slice(1) : corrected;
845
+ corrected =
846
+ corrected[corrected.length - 1] === "/"
847
+ ? corrected.slice(0, corrected.length - 1)
848
+ : corrected;
849
+ return address.match(corrected) != null;
850
+ }
851
+ } catch (e) {
852
+ Logger.logError(e, "isAddressValidForAsset");
853
+ }
854
+ return false;
855
+ }
856
+
857
+ getExtraIdNameIfPresent(asset) {
858
+ try {
859
+ const assetData = this._supportedCoins.find(
860
+ (i) => i.coin?.ticker === asset?.ticker
861
+ );
862
+ if (assetData?.hasExtraId) {
863
+ if (
864
+ assetData?.extraIdName == null ||
865
+ assetData?.extraIdName === ""
866
+ ) {
867
+ // We return some default name if the extraIdName is empty
868
+ return "ID";
869
+ }
870
+ return assetData?.extraIdName;
871
+ }
872
+ return null;
873
+ } catch (e) {
874
+ improveAndRethrow(e, "getExtraIdNameIfPresent");
875
+ }
876
+ }
877
+ }