@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.
- package/CHANGELOG.md +0 -0
- package/README.md +23 -16
- package/dist/index.cjs +4404 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +8757 -1
- package/dist/index.css.map +1 -1
- package/dist/index.modern.js +3692 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +4375 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +4406 -1
- package/dist/index.umd.js.map +1 -1
- package/index.js +1 -1
- package/package.json +17 -24
- package/src/common/amountUtils.js +423 -0
- package/src/common/errorUtils.js +27 -0
- package/src/common/fiatCurrenciesService.js +161 -0
- package/src/common/models/blockchain.js +10 -0
- package/src/common/models/coin.js +157 -0
- package/src/common/models/protocol.js +5 -0
- package/src/common/utils/cache.js +268 -0
- package/src/common/utils/emailAPI.js +18 -0
- package/src/common/utils/logging/logger.js +48 -0
- package/src/common/utils/logging/logsStorage.js +61 -0
- package/src/common/utils/safeStringify.js +50 -0
- package/src/components/atoms/AssetIcon/AssetIcon.jsx +55 -0
- package/src/components/atoms/AssetIcon/asset-icon.module.scss +42 -0
- package/{stories → src/components}/atoms/LoadingDots/LoadingDots.module.scss +1 -1
- package/src/components/atoms/SupportChat/SupportChat.jsx +40 -0
- package/{stories → src/components}/atoms/buttons/Button/Button.jsx +6 -6
- package/{stories → src/components}/atoms/buttons/Button/Button.module.scss +6 -1
- package/src/components/hooks/useCallHandlingErrors.js +26 -0
- package/src/components/hooks/useReferredState.js +24 -0
- package/src/index.js +33 -0
- package/src/swaps-lib/external-apis/swapProvider.js +169 -0
- package/src/swaps-lib/external-apis/swapspaceSwapProvider.js +812 -0
- package/src/swaps-lib/models/baseSwapCreationInfo.js +40 -0
- package/src/swaps-lib/models/existingSwap.js +58 -0
- package/src/swaps-lib/models/existingSwapWithFiatData.js +115 -0
- package/src/swaps-lib/services/publicSwapService.js +602 -0
- package/src/swaps-lib/utils/swapUtils.js +209 -0
- package/stories/index.js +0 -2
- /package/{stories → src/components}/atoms/LoadingDots/LoadingDots.jsx +0 -0
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./src/index.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rabbitio/ui-kit",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.21",
|
|
4
4
|
"description": "Rabbit.io react.js components kit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -11,13 +11,12 @@
|
|
|
11
11
|
"require": "./dist/index.cjs",
|
|
12
12
|
"default": "./dist/index.modern.js"
|
|
13
13
|
},
|
|
14
|
-
"./next": "./index.js"
|
|
14
|
+
"./next": "./index.js",
|
|
15
|
+
"./index.css": "./dist/index.css"
|
|
15
16
|
},
|
|
16
17
|
"scripts": {
|
|
17
|
-
"build": "rm -rf dist && microbundle",
|
|
18
|
-
"
|
|
19
|
-
"build-prod": "rm -rf dist && NODE_ENV=production webpack --mode production",
|
|
20
|
-
"prepublish": "npm run build-prod",
|
|
18
|
+
"build": "rm -rf dist && microbundle --no-compress --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react --globals react/jsx-runtime=jsx --visualize",
|
|
19
|
+
"prepublish": "npm run build",
|
|
21
20
|
"storybook": "storybook dev -p 6006",
|
|
22
21
|
"build-storybook": "storybook build"
|
|
23
22
|
},
|
|
@@ -35,19 +34,17 @@
|
|
|
35
34
|
"author": "admin@rabbit.io",
|
|
36
35
|
"license": "MIT",
|
|
37
36
|
"peerDependencies": {
|
|
38
|
-
"react": ">=
|
|
39
|
-
"react-dom": ">=
|
|
40
|
-
"react-router-dom": ">=6.21.1"
|
|
37
|
+
"react": ">=18.2.0",
|
|
38
|
+
"react-dom": ">=18.2.0"
|
|
41
39
|
},
|
|
42
40
|
"dependencies": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
41
|
+
"axios": "1.6.7",
|
|
42
|
+
"bignumber.js": "9.1.2",
|
|
43
|
+
"eventbusjs": "0.2.0",
|
|
44
|
+
"react": ">=18.2.0",
|
|
45
|
+
"react-dom": ">=18.2.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@babel/core": "^7.23.7",
|
|
49
|
-
"@babel/preset-env": "^7.23.7",
|
|
50
|
-
"@babel/preset-react": "^7.23.3",
|
|
51
48
|
"@storybook/addon-actions": "^7.6.6",
|
|
52
49
|
"@storybook/addon-backgrounds": "^7.6.6",
|
|
53
50
|
"@storybook/addon-essentials": "^7.6.6",
|
|
@@ -60,23 +57,19 @@
|
|
|
60
57
|
"@storybook/react": "^7.6.6",
|
|
61
58
|
"@storybook/react-webpack5": "^7.6.6",
|
|
62
59
|
"@storybook/test": "^7.6.6",
|
|
63
|
-
"babel-loader": "^9.1.3",
|
|
64
|
-
"css-loader": "^6.8.1",
|
|
65
60
|
"microbundle": "^0.15.1",
|
|
66
|
-
"node-sass": "^7.0.3",
|
|
67
61
|
"prettier": "^3.1.1",
|
|
68
62
|
"prop-types": "^15.8.1",
|
|
69
63
|
"resolve-url-loader": "^5.0.0",
|
|
70
|
-
"sass": "^1.
|
|
71
|
-
"sass-loader": "^
|
|
64
|
+
"sass": "^1.70.0",
|
|
65
|
+
"sass-loader": "^14.1.0",
|
|
72
66
|
"storybook": "^7.6.6",
|
|
73
|
-
"style-loader": "^3.3.3"
|
|
74
|
-
"webpack": "^5.89.0",
|
|
75
|
-
"webpack-cli": "^5.1.4"
|
|
67
|
+
"style-loader": "^3.3.3"
|
|
76
68
|
},
|
|
77
69
|
"prettier": {
|
|
78
70
|
"trailingComma": "es5",
|
|
79
71
|
"tabWidth": 4,
|
|
80
|
-
"singleQuote": false
|
|
72
|
+
"singleQuote": false,
|
|
73
|
+
"printWidth": 80
|
|
81
74
|
}
|
|
82
75
|
}
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
|
|
3
|
+
import { FiatCurrenciesService } from "./fiatCurrenciesService.js";
|
|
4
|
+
import { improveAndRethrow } from "./errorUtils.js";
|
|
5
|
+
|
|
6
|
+
// TODO: [dev] return addCommasToAmountString internal method to encapsulate commas adding
|
|
7
|
+
|
|
8
|
+
export class AmountUtils {
|
|
9
|
+
static significantDecimalCount = 8;
|
|
10
|
+
static collapsedDecimalCount = 2;
|
|
11
|
+
static maxTotalLength = 12;
|
|
12
|
+
static extraSmallMaxTotalLength = 9; // >=10 breaks transactions list (mobile) and it is hard to avoid this
|
|
13
|
+
static periods = "..";
|
|
14
|
+
|
|
15
|
+
static defaultFiatParams = {
|
|
16
|
+
ticker: true, // If true, currency code will be shown
|
|
17
|
+
enableCurrencySymbols: true, // Enables currency symbols where available. Requires "ticker: true"
|
|
18
|
+
collapsible: true, // Enables minimization of amounts over 1 million (example: 1.52M)
|
|
19
|
+
limitTotalLength: true, // Limits the total amount length to maxTotalLength
|
|
20
|
+
extraSmallLength: false, // Limits the total amount length to extraSmallMaxTotalLength
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
static fiatXs(amount, code) {
|
|
24
|
+
return this.fiat(amount, code, { extraSmallLength: true });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Universal method for rendering of fiat amounts, taking into account the rules of
|
|
29
|
+
* the passed fiat currency code.
|
|
30
|
+
*
|
|
31
|
+
* TODO: [feature, high] remove 'number' from accepted types list task_id=1e692bcfabbe487a9d1638fc8ff17448
|
|
32
|
+
* @param amount {BigNumber|number|string|null|undefined} The number value to be trimmed
|
|
33
|
+
* @param currencyCode {string|null} The currency code. Can be omitted if { ticker: false } in the config
|
|
34
|
+
* @param [passedParams={}] {object} Formatting parameters
|
|
35
|
+
* @return {string} Formatted fiat amount string
|
|
36
|
+
*/
|
|
37
|
+
static fiat(amount, currencyCode, passedParams = {}) {
|
|
38
|
+
try {
|
|
39
|
+
const params = { ...this.defaultFiatParams, ...passedParams };
|
|
40
|
+
|
|
41
|
+
if (
|
|
42
|
+
this._checkIfAmountInvalid(amount, true) ||
|
|
43
|
+
typeof currencyCode !== "string"
|
|
44
|
+
)
|
|
45
|
+
return "NULL";
|
|
46
|
+
|
|
47
|
+
const currencySymbol =
|
|
48
|
+
FiatCurrenciesService.getCurrencySymbolByCode(currencyCode);
|
|
49
|
+
const currencyDecimalCount =
|
|
50
|
+
FiatCurrenciesService.getCurrencyDecimalCountByCode(
|
|
51
|
+
currencyCode
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const trimmedByMaxDigits = BigNumber(amount).toFixed(
|
|
55
|
+
currencyDecimalCount,
|
|
56
|
+
BigNumber.ROUND_FLOOR
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
let processedAmount = BigNumber(trimmedByMaxDigits);
|
|
60
|
+
if (
|
|
61
|
+
params.collapsible &&
|
|
62
|
+
processedAmount.gte(BigNumber("1000000"))
|
|
63
|
+
) {
|
|
64
|
+
processedAmount = this._collapseToMillionsAndFormat(
|
|
65
|
+
processedAmount,
|
|
66
|
+
this.collapsedDecimalCount,
|
|
67
|
+
params
|
|
68
|
+
);
|
|
69
|
+
} else {
|
|
70
|
+
const limitResult = this._limitTotalAmountLengthIfNeeded(
|
|
71
|
+
trimmedByMaxDigits,
|
|
72
|
+
params
|
|
73
|
+
);
|
|
74
|
+
processedAmount = BigNumber(
|
|
75
|
+
limitResult.processedAmount
|
|
76
|
+
).toFormat(); // Adds commas to integer part
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Add the currency code or currency symbol, if symbol is enabled and available
|
|
80
|
+
if (params.ticker) {
|
|
81
|
+
if (
|
|
82
|
+
typeof currencySymbol === "string" &&
|
|
83
|
+
params.enableCurrencySymbols
|
|
84
|
+
) {
|
|
85
|
+
processedAmount =
|
|
86
|
+
currencySymbol +
|
|
87
|
+
(currencySymbol.length > 1 ? " " : "") +
|
|
88
|
+
processedAmount;
|
|
89
|
+
} else {
|
|
90
|
+
processedAmount = processedAmount + " " + currencyCode;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return processedAmount;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
improveAndRethrow(e, "fiat", `Passed: ${amount}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static defaultCryptoParams = {
|
|
101
|
+
ticker: true, // If true, asset ticker will be shown
|
|
102
|
+
collapsible: true, // Enables minimization of amounts over 1 million (example: 1.52M)
|
|
103
|
+
trim: true, // Cuts the right part of the amount if necessary, and adds ".." in the end
|
|
104
|
+
limitTotalLength: true, // Limits the total amount length to maxTotalLength
|
|
105
|
+
extraSmallLength: false, // Limits the total amount length to extraSmallMaxTotalLength
|
|
106
|
+
periods: true, // Whether we add periods ("..") as suffix for trimmed numbers
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
static cryptoWoTicker(amount, digits) {
|
|
110
|
+
return this.crypto(amount, null, digits, { ticker: false });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static cryptoWoTickerXs(amount, digits) {
|
|
114
|
+
return this.crypto(amount, null, digits, {
|
|
115
|
+
ticker: false,
|
|
116
|
+
extraSmallLength: true,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static cryptoXs(amount, ticker, digits) {
|
|
121
|
+
return this.crypto(amount, ticker, digits, {
|
|
122
|
+
extraSmallLength: true,
|
|
123
|
+
periods: false,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static cryptoFull(amount, ticker, digits) {
|
|
128
|
+
return this.crypto(amount, ticker, digits, {
|
|
129
|
+
collapsible: false,
|
|
130
|
+
trim: false,
|
|
131
|
+
limitTotalLength: false,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Universal method for rendering of crypto amounts, taking into account the rules of
|
|
137
|
+
* the passed ticker. Requires the number of digits after period to be less of equal to
|
|
138
|
+
* the number of digits, supported by the passed ticker.
|
|
139
|
+
*
|
|
140
|
+
* @param amount {BigNumber|string|null|undefined} The number value to be formatted
|
|
141
|
+
* @param ticker {string|null} Coin ticker
|
|
142
|
+
* @param [digits=8] {number} max digits after the dot
|
|
143
|
+
* @param passedParams {object} Formatting parameters
|
|
144
|
+
* @return {string} Formatted crypto amount string
|
|
145
|
+
*/
|
|
146
|
+
static crypto(
|
|
147
|
+
amount,
|
|
148
|
+
ticker,
|
|
149
|
+
digits = this.significantDecimalCount,
|
|
150
|
+
passedParams
|
|
151
|
+
) {
|
|
152
|
+
try {
|
|
153
|
+
const params = { ...this.defaultCryptoParams, ...passedParams };
|
|
154
|
+
|
|
155
|
+
if (
|
|
156
|
+
this._checkIfAmountInvalid(amount) ||
|
|
157
|
+
(typeof ticker !== "string" && params.ticker)
|
|
158
|
+
)
|
|
159
|
+
return "NULL";
|
|
160
|
+
|
|
161
|
+
let addPeriods = false;
|
|
162
|
+
|
|
163
|
+
const amountBigNumber = BigNumber(amount);
|
|
164
|
+
|
|
165
|
+
let processedAmount = amountBigNumber.toFixed(
|
|
166
|
+
digits,
|
|
167
|
+
BigNumber.ROUND_FLOOR
|
|
168
|
+
);
|
|
169
|
+
processedAmount =
|
|
170
|
+
this.removeRedundantRightZerosFromNumberString(processedAmount);
|
|
171
|
+
const originalAmountDecimalPlaces =
|
|
172
|
+
BigNumber(processedAmount).decimalPlaces();
|
|
173
|
+
// Check decimal count and throw an error, if the amount has more decimal digits than supported by the asset
|
|
174
|
+
if (originalAmountDecimalPlaces > digits) {
|
|
175
|
+
const errorMessage = `An attempt to render a crypto value with too many digits after period was made: ${amount}, allowed digits: ${digits}. This is a no-op, since the logical and visually rendered values would differ, which is not acceptable for crypto amounts. Please trim the amount before rendering, using the trimCryptoAmountByCoin(amount, coin) method.`;
|
|
176
|
+
// throw new Error(errorMessage);
|
|
177
|
+
// eslint-disable-next-line no-console
|
|
178
|
+
console.log(errorMessage, "crypto");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Shortening the value to general significant number of digits after period
|
|
182
|
+
if (params.trim) {
|
|
183
|
+
processedAmount =
|
|
184
|
+
this.removeRedundantRightZerosFromNumberString(
|
|
185
|
+
amountBigNumber.toFixed(
|
|
186
|
+
this.significantDecimalCount,
|
|
187
|
+
BigNumber.ROUND_FLOOR
|
|
188
|
+
)
|
|
189
|
+
);
|
|
190
|
+
addPeriods =
|
|
191
|
+
originalAmountDecimalPlaces > this.significantDecimalCount;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const limitResult = this._limitTotalAmountLengthIfNeeded(
|
|
195
|
+
processedAmount,
|
|
196
|
+
params
|
|
197
|
+
);
|
|
198
|
+
processedAmount = limitResult.processedAmount;
|
|
199
|
+
addPeriods ||= limitResult.addPeriods;
|
|
200
|
+
|
|
201
|
+
let wereMillionsCollapsed = false;
|
|
202
|
+
if (params.collapsible && amountBigNumber.gte("1000000")) {
|
|
203
|
+
// Collapse the 1M+ amounts if applicable
|
|
204
|
+
processedAmount = this._collapseToMillionsAndFormat(
|
|
205
|
+
BigNumber(processedAmount),
|
|
206
|
+
this.collapsedDecimalCount,
|
|
207
|
+
params
|
|
208
|
+
);
|
|
209
|
+
wereMillionsCollapsed = true;
|
|
210
|
+
} else {
|
|
211
|
+
// Add separators to integer part of the amount
|
|
212
|
+
processedAmount = BigNumber(processedAmount).toFormat();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Adding periods, if the amount was shortened
|
|
216
|
+
if (params.periods && addPeriods && !wereMillionsCollapsed) {
|
|
217
|
+
processedAmount = processedAmount + this.periods;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Adding an adaptive (printable/full) ticker
|
|
221
|
+
if (params.ticker && ticker) {
|
|
222
|
+
processedAmount = processedAmount + " " + ticker;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return processedAmount;
|
|
226
|
+
} catch (e) {
|
|
227
|
+
improveAndRethrow(
|
|
228
|
+
e,
|
|
229
|
+
"crypto",
|
|
230
|
+
`Passed: ${amount}, ${ticker}, ${digits}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static _checkIfAmountInvalid(amount, allowNumbers = false) {
|
|
236
|
+
return (
|
|
237
|
+
amount == null ||
|
|
238
|
+
amount === "" ||
|
|
239
|
+
(!BigNumber.isBigNumber(amount) &&
|
|
240
|
+
typeof amount !== "string" &&
|
|
241
|
+
(!allowNumbers || typeof amount !== "number"))
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Trims all digits after period that exceed the number of digits provided.
|
|
247
|
+
* Use this everywhere when calculating some amount value to ensure the result is correct in terms
|
|
248
|
+
* of max digits allowed by specific currency.
|
|
249
|
+
*
|
|
250
|
+
* @param amount {BigNumber|number|string|null|undefined} The number value to be trimmed.
|
|
251
|
+
* HEX strings also allowed "0x..." and JS hex numbers
|
|
252
|
+
* @param digits {number} allowed digits
|
|
253
|
+
* @return {string|null} String with trimmed number or null for invalid amount
|
|
254
|
+
*/
|
|
255
|
+
static trim(amount, digits) {
|
|
256
|
+
try {
|
|
257
|
+
if (this._checkIfAmountInvalid(amount, true)) return null;
|
|
258
|
+
return BigNumber(amount).toFixed(digits, BigNumber.ROUND_FLOOR);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
improveAndRethrow(e, "trim", `Passed: ${amount}, ${digits}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @param amount {BigNumber|number|string|null|undefined} The number value to be trimmed.
|
|
266
|
+
* HEX strings also allowed "0x..." and JS hex numbers
|
|
267
|
+
* @return {string|null}
|
|
268
|
+
*/
|
|
269
|
+
static intStr(amount) {
|
|
270
|
+
return this.trim(amount, 0);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Shortens the line length by using a "1.52M" representation of big amounts.
|
|
275
|
+
*
|
|
276
|
+
* @param amountBigNumber {BigNumber} The number value to be trimmed
|
|
277
|
+
* @param decimalCount {number} The number of digits after period to keep in millions representation
|
|
278
|
+
* @param params {object} params object
|
|
279
|
+
* @return {string} A shortened string, converted into "millions" format, if the amount exceeds 1 million
|
|
280
|
+
*/
|
|
281
|
+
static _collapseToMillionsAndFormat(
|
|
282
|
+
amountBigNumber,
|
|
283
|
+
decimalCount,
|
|
284
|
+
params = {}
|
|
285
|
+
) {
|
|
286
|
+
try {
|
|
287
|
+
// TODO: [feature, moderate] use local format here - take from JS locales (comma/dot etc.)
|
|
288
|
+
const millionBigNumber = BigNumber("1000000");
|
|
289
|
+
const millions = amountBigNumber
|
|
290
|
+
.div(millionBigNumber)
|
|
291
|
+
.toFixed(decimalCount, BigNumber.ROUND_FLOOR);
|
|
292
|
+
const limitedResult = this._limitTotalAmountLengthIfNeeded(
|
|
293
|
+
millions,
|
|
294
|
+
params
|
|
295
|
+
);
|
|
296
|
+
const formatted = BigNumber(
|
|
297
|
+
limitedResult.processedAmount
|
|
298
|
+
).toFormat();
|
|
299
|
+
|
|
300
|
+
return formatted + "M";
|
|
301
|
+
} catch (e) {
|
|
302
|
+
improveAndRethrow(
|
|
303
|
+
e,
|
|
304
|
+
"_collapseAmountAndFormat",
|
|
305
|
+
`Passed: ${amountBigNumber.toFixed()}, ${decimalCount}`
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* @param amountString {string} The amount to be restricted by length
|
|
312
|
+
* @param params {object} Params object used for formatting
|
|
313
|
+
* @return {{processedAmount:string, addPeriods: boolean}} A shortened string
|
|
314
|
+
*/
|
|
315
|
+
static _limitTotalAmountLengthIfNeeded(amountString, params) {
|
|
316
|
+
try {
|
|
317
|
+
let addPeriods = false;
|
|
318
|
+
if (params.limitTotalLength || params.extraSmallLength) {
|
|
319
|
+
const maxLength = params.extraSmallLength
|
|
320
|
+
? this.extraSmallMaxTotalLength
|
|
321
|
+
: this.maxTotalLength;
|
|
322
|
+
if (amountString.length > maxLength) {
|
|
323
|
+
const delta = amountString.length - maxLength;
|
|
324
|
+
const currentDecimalsCount =
|
|
325
|
+
BigNumber(amountString).decimalPlaces();
|
|
326
|
+
const newDecimalCount = currentDecimalsCount - delta;
|
|
327
|
+
amountString = BigNumber(amountString).toFixed(
|
|
328
|
+
newDecimalCount > 2 ? newDecimalCount : 2,
|
|
329
|
+
BigNumber.ROUND_FLOOR
|
|
330
|
+
);
|
|
331
|
+
addPeriods = currentDecimalsCount > newDecimalCount;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return { addPeriods: addPeriods, processedAmount: amountString };
|
|
336
|
+
} catch (e) {
|
|
337
|
+
improveAndRethrow(
|
|
338
|
+
e,
|
|
339
|
+
"_limitTotalAmountLengthIfNeeded",
|
|
340
|
+
`Passed: ${amountString}, ${params}`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Safely composes rate string (handles small/big rates)
|
|
347
|
+
*
|
|
348
|
+
* @param leftTicker {string}
|
|
349
|
+
* @param rightTicker {string}
|
|
350
|
+
* @param rate {number|string|BigNumber}
|
|
351
|
+
* @param [rightCurrencyDigitsAfterDots=8] {number}
|
|
352
|
+
* @return {string}
|
|
353
|
+
*/
|
|
354
|
+
static composeRateText(
|
|
355
|
+
leftTicker,
|
|
356
|
+
rightTicker,
|
|
357
|
+
rate,
|
|
358
|
+
rightCurrencyDigitsAfterDots = this.significantDecimalCount
|
|
359
|
+
) {
|
|
360
|
+
try {
|
|
361
|
+
/* Here we try to calculate a clear rate for the user. The difficulty is that the rate value can be pretty
|
|
362
|
+
* small as some coins have significantly higher price than the other. For such cases we calculate
|
|
363
|
+
* not the "1 <coin_A> is X <coin B>" but "Y <coin_A> is X <coin B>" where Y is one of the powers of 100.
|
|
364
|
+
*/
|
|
365
|
+
let leftNumber = BigNumber("1");
|
|
366
|
+
const multiplier = BigNumber("100");
|
|
367
|
+
const maxAttemptsToGetRate = 10;
|
|
368
|
+
let right = null;
|
|
369
|
+
const rateBigNumber = BigNumber(rate);
|
|
370
|
+
for (let i = 0; i < maxAttemptsToGetRate; ++i) {
|
|
371
|
+
const rightNumberAttempt = rateBigNumber
|
|
372
|
+
.times(leftNumber)
|
|
373
|
+
.toFixed(
|
|
374
|
+
rightCurrencyDigitsAfterDots,
|
|
375
|
+
BigNumber.ROUND_FLOOR
|
|
376
|
+
);
|
|
377
|
+
if (!BigNumber(rightNumberAttempt).eq(BigNumber("0"))) {
|
|
378
|
+
right = BigNumber(rightNumberAttempt);
|
|
379
|
+
break;
|
|
380
|
+
} else {
|
|
381
|
+
leftNumber = leftNumber.times(multiplier);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const leftAmountString = AmountUtils.intStr(leftNumber);
|
|
385
|
+
const rightAmountString =
|
|
386
|
+
right != null
|
|
387
|
+
? right.toFixed(
|
|
388
|
+
rightCurrencyDigitsAfterDots,
|
|
389
|
+
BigNumber.ROUND_FLOOR
|
|
390
|
+
)
|
|
391
|
+
: null;
|
|
392
|
+
return `${leftAmountString} ${leftTicker} ~ ${
|
|
393
|
+
rightAmountString ?? "?"
|
|
394
|
+
} ${rightTicker}`;
|
|
395
|
+
} catch (e) {
|
|
396
|
+
// eslint-disable-next-line no-console
|
|
397
|
+
console.log("composeRateText", e);
|
|
398
|
+
}
|
|
399
|
+
return "-";
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* @param numberAsAString {string}
|
|
404
|
+
* @return {string}
|
|
405
|
+
*/
|
|
406
|
+
static removeRedundantRightZerosFromNumberString(numberAsAString) {
|
|
407
|
+
try {
|
|
408
|
+
const parts = ("" + numberAsAString).split(".");
|
|
409
|
+
let right = parts[1];
|
|
410
|
+
while (right?.length && right[right.length - 1] === "0") {
|
|
411
|
+
right = right.slice(0, right.length - 1);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return `${parts[0]}${right?.length ? `.${right}` : ""}`;
|
|
415
|
+
} catch (e) {
|
|
416
|
+
improveAndRethrow(
|
|
417
|
+
e,
|
|
418
|
+
"removeRedundantRightZerosFromNumberString",
|
|
419
|
+
`Passed: ${numberAsAString}`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function improves the passed error object (its message) by adding the passed function name
|
|
3
|
+
* and additional message to it.
|
|
4
|
+
* This is useful as Javascript doesn't guarantee the stack-traces, so we should manually add these details to errors
|
|
5
|
+
* to be able to troubleshoot.
|
|
6
|
+
*
|
|
7
|
+
* @param e {Error}
|
|
8
|
+
* @param settingFunction {string}
|
|
9
|
+
* @param [additionalMessage=""] {string|undefined}
|
|
10
|
+
* @throws {Error} always rethrows the original passed error but with an improved message
|
|
11
|
+
*/
|
|
12
|
+
export function improveAndRethrow(e, settingFunction, additionalMessage = "") {
|
|
13
|
+
const message = improvedErrorMessage(e, settingFunction, additionalMessage);
|
|
14
|
+
if (e) {
|
|
15
|
+
e.message = message;
|
|
16
|
+
throw e; // to preserve existing stacktrace if present
|
|
17
|
+
}
|
|
18
|
+
throw new Error(message);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function improvedErrorMessage(e, settingFunction, additionalMessage) {
|
|
22
|
+
let message = `\nFunction call ${settingFunction ?? ""} failed. `;
|
|
23
|
+
e && e.message && (message += `Error message: ${e.message}. `);
|
|
24
|
+
additionalMessage && (message += `${additionalMessage} `);
|
|
25
|
+
|
|
26
|
+
return message;
|
|
27
|
+
}
|