@rabbitio/ui-kit 1.0.0-beta.3 → 1.0.0-beta.30

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