@rabbitio/ui-kit 1.0.0-beta.2 → 1.0.0-beta.21

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