@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,209 @@
1
+ import { BigNumber } from "bignumber.js";
2
+
3
+ import { AmountUtils } from "../../common/amountUtils.js";
4
+ import { FiatCurrenciesService } from "../../common/fiatCurrenciesService.js";
5
+ import { improveAndRethrow } from "../../common/errorUtils.js";
6
+ import { ExistingSwapWithFiatData } from "../models/existingSwapWithFiatData.js";
7
+ import { EmailsApi } from "../../common/utils/emailAPI.js";
8
+ import { SwapProvider } from "../external-apis/swapProvider.js";
9
+ import { Logger } from "../../common/utils/logging/logger.js";
10
+ import { safeStringify } from "../../common/utils/safeStringify.js";
11
+
12
+ export class SwapUtils {
13
+ /**
14
+ * Retrieves min and max limits for swapping giving currencies.
15
+ * Returns also conversion rate if possible with predefined amount logic.
16
+ * Rate is how many "to" coins does 1 "from" coin contain.
17
+ *
18
+ * In case of errors returns one of reasons
19
+ * - SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED
20
+ * - one of SwapProvider.COMMON_ERRORS.*
21
+ *
22
+ * @param swapProvider {SwapProvider}
23
+ * @param fromCoin {Coin} enabled coin (to swap amount from)
24
+ * @param toCoin {Coin}
25
+ * @return {Promise<{
26
+ * result: true,
27
+ * min: string,
28
+ * fiatMin: (number|null),
29
+ * max: string,
30
+ * fiatMax: (number|null),
31
+ * rate: (string|null),
32
+ * }|{
33
+ * result: false,
34
+ * reason: string
35
+ * }>}
36
+ */
37
+ static async getInitialSwapData(swapProvider, fromCoin, toCoin) {
38
+ const loggerSource = "getInitialSwapData";
39
+ try {
40
+ /* We use some amount here that should fit at least some of the limits of the swap providers.
41
+ * So we are going to get some rate to be used as the default for the on-flight calculations before we get
42
+ * the exact rate (that should be retrieved by getSwapCreationInfo method) for a specific amount.
43
+ */
44
+ const defaultAmountUsd = BigNumber("300");
45
+ const coinUsdRate = await swapProvider.getCoinToUSDTRate(fromCoin);
46
+ const coinAmountForDefaultUsdAmount = AmountUtils.trim(
47
+ coinUsdRate.result
48
+ ? defaultAmountUsd.div(coinUsdRate?.rate)
49
+ : defaultAmountUsd,
50
+ fromCoin.digits
51
+ );
52
+ Logger.log(
53
+ `Init: ${coinAmountForDefaultUsdAmount} ${fromCoin.ticker}->${toCoin.ticker}`,
54
+ loggerSource
55
+ );
56
+ const details = await swapProvider.getSwapInfo(
57
+ fromCoin,
58
+ toCoin,
59
+ coinAmountForDefaultUsdAmount
60
+ );
61
+ if (!details) {
62
+ throw new Error(
63
+ "The details are empty: " + safeStringify(details)
64
+ );
65
+ }
66
+ if (!details.result) {
67
+ Logger.log(
68
+ `Failed with reason: ${details.reason}. ${fromCoin.ticker}->${toCoin.ticker}`,
69
+ loggerSource
70
+ );
71
+ if (
72
+ details?.reason ===
73
+ SwapProvider.NO_SWAPS_REASONS.NOT_SUPPORTED ||
74
+ details?.reason ===
75
+ SwapProvider.COMMON_ERRORS.REQUESTS_LIMIT_EXCEEDED
76
+ ) {
77
+ return { result: false, reason: details.reason };
78
+ } else {
79
+ throw new Error("Unhandled error case: " + details?.reason);
80
+ }
81
+ }
82
+ let fiatMin = null;
83
+ let fiatMax = null;
84
+ if (coinUsdRate?.rate != null) {
85
+ const usdDecimals =
86
+ FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
87
+ fiatMin = BigNumber(details?.smallestMin)
88
+ .times(coinUsdRate.rate)
89
+ .toFixed(usdDecimals);
90
+ fiatMax = BigNumber(details?.greatestMax)
91
+ .times(coinUsdRate.rate)
92
+ .toFixed(usdDecimals);
93
+ }
94
+
95
+ const result = {
96
+ result: true,
97
+ min: details?.smallestMin,
98
+ fiatMin: fiatMin,
99
+ max: details?.greatestMax,
100
+ fiatMax: fiatMax,
101
+ rate: AmountUtils.trim(details.rate, 30),
102
+ };
103
+ Logger.log(`Returning: ${safeStringify(result)}`, loggerSource);
104
+ return result;
105
+ } catch (e) {
106
+ Logger.logError(
107
+ e,
108
+ loggerSource,
109
+ `Failed to init swap: ${safeStringify(e)}`
110
+ );
111
+ improveAndRethrow(e, loggerSource);
112
+ }
113
+ }
114
+
115
+ static safeHandleRequestsLimitExceeding() {
116
+ (async () => {
117
+ try {
118
+ await EmailsApi.sendEmail(
119
+ "AUTOMATIC EMAIL - SWAPSPACE REQUESTS LIMIT EXCEEDED",
120
+ "Requests limit exceeded. Urgently ask swaps provider support for limit increasing"
121
+ );
122
+ } catch (e) {
123
+ Logger.log(
124
+ `Failed to handle limit exceeding ${safeStringify(e)}`,
125
+ "_safeHandleRequestsLimitExceeding"
126
+ );
127
+ }
128
+ })();
129
+ }
130
+
131
+ /**
132
+ * If some swap is not found by id then there is no item in return list.
133
+ *
134
+ * @param swapProvider {SwapProvider}
135
+ * @param swapIds {string[]}
136
+ * @return {Promise<{
137
+ * result: true,
138
+ * swaps: ExistingSwapWithFiatData[]
139
+ * }|{
140
+ * result: false,
141
+ * reason: string
142
+ * }>}
143
+ */
144
+ static async getExistingSwapsDetailsWithFiatAmounts(swapProvider, swapIds) {
145
+ try {
146
+ const result =
147
+ await swapProvider.getExistingSwapsDetailsAndStatus(swapIds);
148
+ if (result.result) {
149
+ const extendedSwaps = [];
150
+ for (let swap of result.swaps) {
151
+ if (swap.status === SwapProvider.SWAP_STATUSES.REFUNDED) {
152
+ const rate = await swapProvider.getCoinToUSDTRate(
153
+ swap.fromCoin
154
+ );
155
+ extendedSwaps.push(
156
+ ExistingSwapWithFiatData.fromExistingSwap(
157
+ swap,
158
+ rate?.rate != null
159
+ ? BigNumber(swap.fromAmount)
160
+ .times(rate.rate)
161
+ .toNumber()
162
+ : null,
163
+ rate?.rate != null
164
+ ? BigNumber(swap.toAmount)
165
+ .times(rate.rate)
166
+ .toNumber()
167
+ : null,
168
+ "USD",
169
+ FiatCurrenciesService.getCurrencyDecimalCountByCode(
170
+ "USD"
171
+ )
172
+ )
173
+ );
174
+ } else {
175
+ const [fromCoinFiatRate, toConFiatRate] =
176
+ await Promise.all([
177
+ swapProvider.getCoinToUSDTRate(swap.fromCoin),
178
+ swapProvider.getCoinToUSDTRate(swap.toCoin),
179
+ ]);
180
+ extendedSwaps.push(
181
+ ExistingSwapWithFiatData.fromExistingSwap(
182
+ swap,
183
+ fromCoinFiatRate?.rate != null
184
+ ? BigNumber(swap.fromAmount)
185
+ .times(fromCoinFiatRate.rate)
186
+ .toNumber()
187
+ : null,
188
+ toConFiatRate?.rate != null
189
+ ? BigNumber(swap.toAmount)
190
+ .times(toConFiatRate.rate)
191
+ .toNumber()
192
+ : null,
193
+ "USD",
194
+ FiatCurrenciesService.getCurrencyDecimalCountByCode(
195
+ "USD"
196
+ )
197
+ )
198
+ );
199
+ }
200
+ }
201
+ result.swaps = extendedSwaps;
202
+ }
203
+
204
+ return result;
205
+ } catch (e) {
206
+ improveAndRethrow(e, "getExistingSwapsDetailsWithFiatAmounts");
207
+ }
208
+ }
209
+ }
package/stories/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { Button } from "./atoms/buttons/Button/Button.jsx";
2
- export { LoadingDots } from "./atoms/LoadingDots/LoadingDots.jsx";