@lifi/perps-sdk 23.0.0 → 24.0.0
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/README.md +85 -45
- package/dist/cjs/client/PerpsClient.d.ts +3 -2
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +16 -9
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/decimal/arithmetic.d.ts +13 -0
- package/dist/cjs/decimal/arithmetic.d.ts.map +1 -0
- package/dist/cjs/decimal/arithmetic.js +48 -0
- package/dist/cjs/decimal/arithmetic.js.map +1 -0
- package/dist/cjs/decimal/big.d.ts +0 -1
- package/dist/cjs/decimal/big.d.ts.map +1 -1
- package/dist/cjs/decimal/big.js +0 -4
- package/dist/cjs/decimal/big.js.map +1 -1
- package/dist/cjs/decimal/compare.d.ts +7 -0
- package/dist/cjs/decimal/compare.d.ts.map +1 -0
- package/dist/cjs/decimal/compare.js +21 -0
- package/dist/cjs/decimal/compare.js.map +1 -0
- package/dist/cjs/decimal/convert.d.ts +12 -6
- package/dist/cjs/decimal/convert.d.ts.map +1 -1
- package/dist/cjs/decimal/convert.js +36 -14
- package/dist/cjs/decimal/convert.js.map +1 -1
- package/dist/cjs/decimal/decimalStringToBig.d.ts +5 -0
- package/dist/cjs/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/cjs/decimal/decimalStringToBig.js +27 -0
- package/dist/cjs/decimal/decimalStringToBig.js.map +1 -0
- package/dist/cjs/decimal/format.d.ts.map +1 -1
- package/dist/cjs/decimal/format.js +70 -49
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +4 -3
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +44 -19
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/index.d.ts +13 -10
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +66 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +1 -1
- package/dist/cjs/math/fills.d.ts.map +1 -1
- package/dist/cjs/math/fills.js +28 -13
- package/dist/cjs/math/fills.js.map +1 -1
- package/dist/cjs/math/order.d.ts +27 -16
- package/dist/cjs/math/order.d.ts.map +1 -1
- package/dist/cjs/math/order.js +106 -122
- package/dist/cjs/math/order.js.map +1 -1
- package/dist/cjs/math/position.d.ts +85 -35
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +88 -91
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/services/getGasRecommendation.js +1 -1
- package/dist/cjs/services/getGasRecommendation.js.map +1 -1
- package/dist/cjs/services/getQuote.d.ts +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.js +3 -2
- package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/cjs/transport/request.d.ts +1 -0
- package/dist/cjs/transport/request.d.ts.map +1 -1
- package/dist/cjs/transport/request.js +2 -2
- package/dist/cjs/transport/request.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -0
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +9 -5
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/types/withdrawal.d.ts +12 -4
- package/dist/cjs/types/withdrawal.d.ts.map +1 -1
- package/dist/cjs/utils/createSafeFunction.d.ts +2 -0
- package/dist/cjs/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/cjs/utils/createSafeFunction.js +15 -0
- package/dist/cjs/utils/createSafeFunction.js.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts +3 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.js +16 -0
- package/dist/cjs/utils/timestampToIsoString.js.map +1 -0
- package/dist/cjs/utils/warnOnce.d.ts +2 -0
- package/dist/cjs/utils/warnOnce.d.ts.map +1 -1
- package/dist/cjs/utils/warnOnce.js +13 -1
- package/dist/cjs/utils/warnOnce.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/websocket/PerpsWsClient.d.ts +2 -0
- package/dist/cjs/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/cjs/websocket/PerpsWsClient.js +14 -1
- package/dist/cjs/websocket/PerpsWsClient.js.map +1 -1
- package/dist/cjs/websocket/wsLog.d.ts +2 -0
- package/dist/cjs/websocket/wsLog.d.ts.map +1 -1
- package/dist/cjs/websocket/wsLog.js +8 -0
- package/dist/cjs/websocket/wsLog.js.map +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +6 -9
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/cjs/wire/refuel.d.ts +4 -1
- package/dist/cjs/wire/refuel.d.ts.map +1 -1
- package/dist/cjs/wire/refuel.js +23 -6
- package/dist/cjs/wire/refuel.js.map +1 -1
- package/dist/cjs/wire/transferable.js +3 -3
- package/dist/cjs/wire/transferable.js.map +1 -1
- package/dist/cjs/wire/withdraw.d.ts.map +1 -1
- package/dist/cjs/wire/withdraw.js +3 -3
- package/dist/cjs/wire/withdraw.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +17 -4
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +30 -8
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/decimal/arithmetic.d.ts +84 -0
- package/dist/esm/decimal/arithmetic.d.ts.map +1 -0
- package/dist/esm/decimal/arithmetic.js +110 -0
- package/dist/esm/decimal/arithmetic.js.map +1 -0
- package/dist/esm/decimal/big.d.ts +0 -2
- package/dist/esm/decimal/big.d.ts.map +1 -1
- package/dist/esm/decimal/big.js +0 -4
- package/dist/esm/decimal/big.js.map +1 -1
- package/dist/esm/decimal/compare.d.ts +45 -0
- package/dist/esm/decimal/compare.d.ts.map +1 -0
- package/dist/esm/decimal/compare.js +53 -0
- package/dist/esm/decimal/compare.js.map +1 -0
- package/dist/esm/decimal/convert.d.ts +62 -26
- package/dist/esm/decimal/convert.d.ts.map +1 -1
- package/dist/esm/decimal/convert.js +76 -25
- package/dist/esm/decimal/convert.js.map +1 -1
- package/dist/esm/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/esm/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/esm/decimal/decimalStringToBig.js +40 -0
- package/dist/esm/decimal/decimalStringToBig.js.map +1 -0
- package/dist/esm/decimal/format.d.ts +4 -1
- package/dist/esm/decimal/format.d.ts.map +1 -1
- package/dist/esm/decimal/format.js +71 -66
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +26 -7
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +59 -33
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/index.d.ts +13 -10
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +9 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +6 -3
- package/dist/esm/math/fills.d.ts.map +1 -1
- package/dist/esm/math/fills.js +31 -16
- package/dist/esm/math/fills.js.map +1 -1
- package/dist/esm/math/order.d.ts +85 -65
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +158 -166
- package/dist/esm/math/order.js.map +1 -1
- package/dist/esm/math/position.d.ts +155 -98
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +157 -155
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/services/getGasRecommendation.js +1 -1
- package/dist/esm/services/getGasRecommendation.js.map +1 -1
- package/dist/esm/services/getQuote.d.ts +2 -2
- package/dist/esm/services/getQuote.js +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js +4 -3
- package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/esm/transport/request.d.ts +5 -0
- package/dist/esm/transport/request.d.ts.map +1 -1
- package/dist/esm/transport/request.js +2 -2
- package/dist/esm/transport/request.js.map +1 -1
- package/dist/esm/types/api.d.ts +9 -0
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +26 -13
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/types/withdrawal.d.ts +32 -7
- package/dist/esm/types/withdrawal.d.ts.map +1 -1
- package/dist/esm/utils/createSafeFunction.d.ts +9 -0
- package/dist/esm/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/esm/utils/createSafeFunction.js +19 -0
- package/dist/esm/utils/createSafeFunction.js.map +1 -0
- package/dist/esm/utils/timestampToIsoString.d.ts +15 -0
- package/dist/esm/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/esm/utils/timestampToIsoString.js +24 -0
- package/dist/esm/utils/timestampToIsoString.js.map +1 -0
- package/dist/esm/utils/warnOnce.d.ts +16 -0
- package/dist/esm/utils/warnOnce.d.ts.map +1 -1
- package/dist/esm/utils/warnOnce.js +24 -0
- package/dist/esm/utils/warnOnce.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/esm/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/esm/websocket/PerpsWsClient.js +20 -2
- package/dist/esm/websocket/PerpsWsClient.js.map +1 -1
- package/dist/esm/websocket/wsLog.d.ts +12 -0
- package/dist/esm/websocket/wsLog.d.ts.map +1 -1
- package/dist/esm/websocket/wsLog.js +18 -0
- package/dist/esm/websocket/wsLog.js.map +1 -1
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +2 -2
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/esm/wire/refuel.d.ts +24 -14
- package/dist/esm/wire/refuel.d.ts.map +1 -1
- package/dist/esm/wire/refuel.js +38 -17
- package/dist/esm/wire/refuel.js.map +1 -1
- package/dist/esm/wire/transferable.js +3 -3
- package/dist/esm/wire/transferable.js.map +1 -1
- package/dist/esm/wire/withdraw.d.ts.map +1 -1
- package/dist/esm/wire/withdraw.js +3 -3
- package/dist/esm/wire/withdraw.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +17 -4
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/decimal/arithmetic.d.ts +84 -0
- package/dist/types/decimal/arithmetic.d.ts.map +1 -0
- package/dist/types/decimal/big.d.ts +0 -2
- package/dist/types/decimal/big.d.ts.map +1 -1
- package/dist/types/decimal/compare.d.ts +45 -0
- package/dist/types/decimal/compare.d.ts.map +1 -0
- package/dist/types/decimal/convert.d.ts +62 -26
- package/dist/types/decimal/convert.d.ts.map +1 -1
- package/dist/types/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/types/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/types/decimal/format.d.ts +4 -1
- package/dist/types/decimal/format.d.ts.map +1 -1
- package/dist/types/decimal/parse.d.ts +26 -7
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +6 -3
- package/dist/types/math/fills.d.ts.map +1 -1
- package/dist/types/math/order.d.ts +85 -65
- package/dist/types/math/order.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +155 -98
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/services/getQuote.d.ts +2 -2
- package/dist/types/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/types/transport/request.d.ts +5 -0
- package/dist/types/transport/request.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +9 -0
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +26 -13
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/types/withdrawal.d.ts +32 -7
- package/dist/types/types/withdrawal.d.ts.map +1 -1
- package/dist/types/utils/createSafeFunction.d.ts +9 -0
- package/dist/types/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/types/utils/timestampToIsoString.d.ts +15 -0
- package/dist/types/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/types/utils/warnOnce.d.ts +16 -0
- package/dist/types/utils/warnOnce.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/types/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/types/websocket/wsLog.d.ts +12 -0
- package/dist/types/websocket/wsLog.d.ts.map +1 -1
- package/dist/types/wire/orderAmounts.d.ts.map +1 -1
- package/dist/types/wire/refuel.d.ts +24 -14
- package/dist/types/wire/refuel.d.ts.map +1 -1
- package/dist/types/wire/withdraw.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +48 -10
- package/src/decimal/arithmetic.ts +144 -0
- package/src/decimal/big.ts +0 -5
- package/src/decimal/compare.ts +67 -0
- package/src/decimal/convert.ts +123 -44
- package/src/decimal/decimalStringToBig.ts +47 -0
- package/src/decimal/format.ts +81 -68
- package/src/decimal/parse.ts +73 -36
- package/src/index.ts +76 -8
- package/src/math/fills.ts +32 -17
- package/src/math/order.ts +261 -216
- package/src/math/position.ts +277 -194
- package/src/services/getGasRecommendation.ts +1 -1
- package/src/services/getQuote.ts +2 -2
- package/src/services/resolveSubscribeQuote.ts +4 -3
- package/src/transport/request.ts +7 -2
- package/src/types/api.ts +10 -0
- package/src/types/provider.ts +35 -13
- package/src/types/withdrawal.ts +35 -8
- package/src/utils/createSafeFunction.ts +20 -0
- package/src/utils/timestampToIsoString.ts +31 -0
- package/src/utils/warnOnce.ts +44 -0
- package/src/version.ts +1 -1
- package/src/websocket/PerpsWsClient.ts +25 -2
- package/src/websocket/wsLog.ts +31 -0
- package/src/wire/orderAmounts.ts +3 -2
- package/src/wire/refuel.ts +56 -20
- package/src/wire/transferable.ts +3 -3
- package/src/wire/withdraw.ts +3 -5
- package/dist/cjs/decimal/requireDecimal.d.ts +0 -4
- package/dist/cjs/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/cjs/decimal/requireDecimal.js +0 -17
- package/dist/cjs/decimal/requireDecimal.js.map +0 -1
- package/dist/esm/decimal/requireDecimal.d.ts +0 -11
- package/dist/esm/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/esm/decimal/requireDecimal.js +0 -18
- package/dist/esm/decimal/requireDecimal.js.map +0 -1
- package/dist/types/decimal/requireDecimal.d.ts +0 -11
- package/dist/types/decimal/requireDecimal.d.ts.map +0 -1
- package/src/decimal/requireDecimal.ts +0 -21
|
@@ -2,53 +2,92 @@ import { PerpsErrorCode } from '@lifi/perps-types';
|
|
|
2
2
|
import Big from 'big.js';
|
|
3
3
|
import { formatUnits } from 'viem';
|
|
4
4
|
import { PerpsError } from '../errors/PerpsError.js';
|
|
5
|
-
import {
|
|
5
|
+
import { createSafeFunction } from '../utils/createSafeFunction.js';
|
|
6
|
+
import { bigToDecimalString, decimalStringToBig } from './decimalStringToBig.js';
|
|
7
|
+
import { isDecimalString } from './parse.js';
|
|
8
|
+
const BIG_ROUNDING = {
|
|
9
|
+
truncate: Big.roundDown,
|
|
10
|
+
round: Big.roundHalfUp,
|
|
11
|
+
up: Big.roundUp,
|
|
12
|
+
};
|
|
6
13
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* per `rounding`; there is no silent default.
|
|
14
|
+
* Round a decimal string to `decimals` places with exact decimal arithmetic.
|
|
15
|
+
* The result has no trailing zeros: `roundDecimalString('1.50', 1, 'round')`
|
|
16
|
+
* is `'1.5'`.
|
|
11
17
|
*
|
|
12
|
-
* @throws {PerpsError} `ValidationError` when `value` is not a
|
|
13
|
-
*
|
|
14
|
-
*
|
|
18
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a decimal string,
|
|
19
|
+
* or `decimals` is not a non-negative integer.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export function roundDecimalString(value, decimals, rounding) {
|
|
23
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
24
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid decimals for rounding: ${decimals}`);
|
|
25
|
+
}
|
|
26
|
+
return bigToDecimalString(decimalStringToBig(value).round(decimals, BIG_ROUNDING[rounding]));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* {@link roundDecimalString}, or `undefined` with a warning when it throws.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export const safeRoundDecimalString = createSafeFunction('roundDecimalString', roundDecimalString);
|
|
34
|
+
/**
|
|
35
|
+
* Scale a decimal string to an integer at `decimals` places in exact decimal
|
|
36
|
+
* arithmetic: `'0.29'` at 2 decimals is 29, never 28. Off-grid input resolves
|
|
37
|
+
* per `rounding`. No `$`, `%` or `,` is removed first.
|
|
38
|
+
*
|
|
39
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a decimal string,
|
|
40
|
+
* `decimals` is not a non-negative integer, or the result's magnitude
|
|
41
|
+
* exceeds `Number.MAX_SAFE_INTEGER`.
|
|
15
42
|
* @public
|
|
16
43
|
*/
|
|
17
|
-
export
|
|
44
|
+
export function decimalStringToScaledInteger(value, decimals, rounding) {
|
|
45
|
+
if (!isDecimalString(value)) {
|
|
46
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `'${value}' is not a decimal string.`);
|
|
47
|
+
}
|
|
18
48
|
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
19
49
|
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid decimals for integer scaling: ${decimals}`);
|
|
20
50
|
}
|
|
21
|
-
const scaled =
|
|
51
|
+
const scaled = new Big(value)
|
|
22
52
|
.times(new Big(10).pow(decimals))
|
|
23
|
-
.round(0, rounding
|
|
53
|
+
.round(0, BIG_ROUNDING[rounding]);
|
|
24
54
|
if (scaled.abs().gt(Number.MAX_SAFE_INTEGER)) {
|
|
25
55
|
throw new PerpsError(PerpsErrorCode.ValidationError, `Scaled value ${scaled.toFixed()} exceeds Number.MAX_SAFE_INTEGER and cannot be encoded exactly.`);
|
|
26
56
|
}
|
|
27
57
|
return scaled.toNumber();
|
|
28
|
-
}
|
|
29
|
-
const BASE_UNITS_PATTERN = /^-?\d+$/;
|
|
58
|
+
}
|
|
30
59
|
/**
|
|
31
|
-
*
|
|
60
|
+
* {@link decimalStringToScaledInteger}, or `undefined` with a warning when it
|
|
61
|
+
* throws.
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export const safeDecimalStringToScaledInteger = createSafeFunction('decimalStringToScaledInteger', decimalStringToScaledInteger);
|
|
66
|
+
export const BASE_UNITS_PATTERN = /^-?\d+$/;
|
|
67
|
+
/**
|
|
68
|
+
* Convert a scaled integer string (a token amount in base units) to a decimal
|
|
69
|
+
* string: `scaledIntegerToDecimalString('1000000', 6)` is `'1'`.
|
|
32
70
|
*
|
|
33
|
-
* @param amount - Amount in base units (e.g. "1000000" for 1 USDC)
|
|
34
|
-
* @param decimals - Token decimals (e.g. 6 for USDC)
|
|
35
71
|
* @throws {PerpsError} `ValidationError` when `amount` is not an integer
|
|
36
72
|
* string, or `decimals` is not a non-negative integer.
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* baseUnitsToDecimal('1000000', 6) // '1'
|
|
40
|
-
* ```
|
|
41
73
|
* @public
|
|
42
74
|
*/
|
|
43
|
-
export function
|
|
75
|
+
export function scaledIntegerToDecimalString(amount, decimals) {
|
|
44
76
|
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
45
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid \`
|
|
77
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid \`scaledIntegerToDecimalString(decimals)\`: ${decimals} is not a non-negative integer.`);
|
|
46
78
|
}
|
|
47
79
|
if (!BASE_UNITS_PATTERN.test(amount)) {
|
|
48
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid \`
|
|
80
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid \`scaledIntegerToDecimalString(amount)\`: '${amount}' is not an integer base-unit string.`);
|
|
49
81
|
}
|
|
50
82
|
return formatUnits(BigInt(amount), decimals);
|
|
51
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* {@link scaledIntegerToDecimalString}, or `undefined` with a warning when it
|
|
86
|
+
* throws.
|
|
87
|
+
*
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
export const safeScaledIntegerToDecimalString = createSafeFunction('scaledIntegerToDecimalString', scaledIntegerToDecimalString);
|
|
52
91
|
/**
|
|
53
92
|
* Round `value` down to `decimals` and pad the result to exactly that many
|
|
54
93
|
* decimal places. The padding is the point: this seeds a fixed-decimal input,
|
|
@@ -61,17 +100,23 @@ export function baseUnitsToDecimal(amount, decimals) {
|
|
|
61
100
|
* `'0.010000000000000000'`, which `Number#toFixed` cannot.
|
|
62
101
|
*
|
|
63
102
|
* @throws {PerpsError} `ValidationError` when `value` is not a
|
|
64
|
-
*
|
|
103
|
+
* decimal string, or `decimals` is not a non-negative integer.
|
|
65
104
|
* @public
|
|
66
105
|
*/
|
|
67
106
|
export function truncateDecimal(value, decimals) {
|
|
68
107
|
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
69
108
|
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid decimals for truncation: ${decimals}`);
|
|
70
109
|
}
|
|
71
|
-
const truncated =
|
|
110
|
+
const truncated = decimalStringToBig(value).round(decimals, Big.roundDown);
|
|
72
111
|
// big.js carries the sign through a round to zero; '-0.00' is not a spelling.
|
|
73
112
|
return (truncated.eq(0) ? new Big(0) : truncated).toFixed(decimals);
|
|
74
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* {@link truncateDecimal}, or `undefined` with a warning when it throws.
|
|
116
|
+
*
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
export const safeTruncateDecimal = createSafeFunction('truncateDecimal', truncateDecimal);
|
|
75
120
|
/**
|
|
76
121
|
* Spell a `number` as a {@link DecimalString} in plain notation: `1e-7`
|
|
77
122
|
* becomes `'0.0000001'`, never `'1e-7'`. The boundary helper for a venue or
|
|
@@ -89,4 +134,10 @@ export function numberToDecimalString(value) {
|
|
|
89
134
|
const parsed = new Big(value);
|
|
90
135
|
return (parsed.eq(0) ? new Big(0) : parsed).toFixed();
|
|
91
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* {@link numberToDecimalString}, or `undefined` with a warning when it throws.
|
|
139
|
+
*
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
export const safeNumberToDecimalString = createSafeFunction('numberToDecimalString', numberToDecimalString);
|
|
92
143
|
//# sourceMappingURL=convert.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.js","sourceRoot":"","sources":["../../../src/decimal/convert.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"convert.js","sourceRoot":"","sources":["../../../src/decimal/convert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,YAA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, `%` and `,` are removed
|
|
4
|
+
* first, so `'$1,234.5'` reads as `1234.5`; the rest must match
|
|
5
|
+
* `DECIMAL_PATTERN`, so an exponent form such as `'1e5'` is rejected.
|
|
6
|
+
*
|
|
7
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a string or the
|
|
8
|
+
* cleaned string is not a decimal string.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function decimalStringToBig(value: string): Big;
|
|
12
|
+
/**
|
|
13
|
+
* Spell a `Big` as a canonical decimal string, with no exponent and no `-0`.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare function bigToDecimalString(value: Big): string;
|
|
18
|
+
/**
|
|
19
|
+
* {@link decimalStringToBig} as a `DivBig`, so a later `div` keeps 40 decimal
|
|
20
|
+
* places.
|
|
21
|
+
*
|
|
22
|
+
* @throws {PerpsError} `ValidationError` as {@link decimalStringToBig}.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare function decimalStringToDivBig(value: string): Big;
|
|
26
|
+
//# sourceMappingURL=decimalStringToBig.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decimalStringToBig.d.ts","sourceRoot":"","sources":["../../../src/decimal/decimalStringToBig.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,QAAQ,CAAA;AAMxB;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAUrD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAErD;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAExD"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { DECIMAL_PATTERN, PerpsErrorCode } from '@lifi/perps-types';
|
|
2
|
+
import Big from 'big.js';
|
|
3
|
+
import { PerpsError } from '../errors/PerpsError.js';
|
|
4
|
+
import { DivBig } from './big.js';
|
|
5
|
+
const DISPLAY_CHARACTERS = /[\s$%,]/g;
|
|
6
|
+
/**
|
|
7
|
+
* Read a decimal string as a `Big`. Whitespace, `$`, `%` and `,` are removed
|
|
8
|
+
* first, so `'$1,234.5'` reads as `1234.5`; the rest must match
|
|
9
|
+
* `DECIMAL_PATTERN`, so an exponent form such as `'1e5'` is rejected.
|
|
10
|
+
*
|
|
11
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a string or the
|
|
12
|
+
* cleaned string is not a decimal string.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export function decimalStringToBig(value) {
|
|
16
|
+
const cleaned = typeof value === 'string' ? value.replace(DISPLAY_CHARACTERS, '') : '';
|
|
17
|
+
if (!DECIMAL_PATTERN.test(cleaned)) {
|
|
18
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `'${String(value)}' is not a decimal string.`);
|
|
19
|
+
}
|
|
20
|
+
return new Big(cleaned);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Spell a `Big` as a canonical decimal string, with no exponent and no `-0`.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export function bigToDecimalString(value) {
|
|
28
|
+
return value.eq(0) ? '0' : value.toFixed();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* {@link decimalStringToBig} as a `DivBig`, so a later `div` keeps 40 decimal
|
|
32
|
+
* places.
|
|
33
|
+
*
|
|
34
|
+
* @throws {PerpsError} `ValidationError` as {@link decimalStringToBig}.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export function decimalStringToDivBig(value) {
|
|
38
|
+
return new DivBig(decimalStringToBig(value));
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=decimalStringToBig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decimalStringToBig.js","sourceRoot":"","sources":["../../../src/decimal/decimalStringToBig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEjC,MAAM,kBAAkB,GAAG,UAAU,CAAA;AAErC;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,MAAM,OAAO,GACX,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IACxE,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,UAAU,CAClB,cAAc,CAAC,eAAe,EAC9B,IAAI,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAC9C,CAAA;IACH,CAAC;IACD,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,CAAA;AACzB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAU;IAC3C,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAA;AAC5C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAa;IACjD,OAAO,IAAI,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAA;AAC9C,CAAC"}
|
|
@@ -7,7 +7,10 @@ import type { DecimalString } from '@lifi/perps-types';
|
|
|
7
7
|
export interface FormatOptions {
|
|
8
8
|
/** Fixed number of decimal places. Defaults are per-formatter. */
|
|
9
9
|
decimals?: number;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Rendered when the value is null, undefined, blank or a non-finite number.
|
|
12
|
+
* A string that is not a decimal is shown unchanged. Defaults to `'—'`.
|
|
13
|
+
*/
|
|
11
14
|
placeholder?: string;
|
|
12
15
|
/** BCP 47 locale controlling digit grouping and separators (e.g. `'en-US'`). */
|
|
13
16
|
locale?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/decimal/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/decimal/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAItD;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,YAAY,CAAA;CACxB;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,OAAO,CAAA;AAI7C,KAAK,WAAW,GAAG,MAAM,GAAG,aAAa,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;AA+ErE;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAgBR;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAgBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAgBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAgBR;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAwBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAqBR"}
|
|
@@ -1,62 +1,49 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { decimalStringToBig } from './decimalStringToBig.js';
|
|
1
3
|
const DEFAULT_PLACEHOLDER = '—';
|
|
2
4
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
5
|
+
* Read a display input as a `Big`: `$`, `%`, `,` and whitespace are removed
|
|
6
|
+
* from a string first. `null` means the formatter renders the placeholder;
|
|
7
|
+
* a string it cannot read is returned unchanged for display.
|
|
6
8
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* when the value is null/undefined/blank or does not resolve to a finite
|
|
13
|
-
* number, so callers can render a placeholder instead of `$0.00` or `$NaN`.
|
|
14
|
-
*/
|
|
15
|
-
function toFiniteNumber(value) {
|
|
16
|
-
let n;
|
|
17
|
-
if (typeof value === 'string') {
|
|
18
|
-
const cleaned = value.replace(/[$,\s]/g, '');
|
|
19
|
-
if (!cleaned) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
n = Number(cleaned);
|
|
23
|
-
}
|
|
24
|
-
else if (typeof value === 'number') {
|
|
25
|
-
n = value;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
9
|
+
function toBig(value) {
|
|
10
|
+
if (typeof value === 'number') {
|
|
11
|
+
return Number.isFinite(value) ? new Big(value) : null;
|
|
12
|
+
}
|
|
13
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
28
14
|
return null;
|
|
29
15
|
}
|
|
30
|
-
|
|
16
|
+
try {
|
|
17
|
+
return decimalStringToBig(value);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
31
22
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* the guard digits are chopped: `99.999` at 2dp → `99.99`, `0.29` → `0.29`.
|
|
37
|
-
*/
|
|
38
|
-
function truncateAbs(abs, decimals) {
|
|
39
|
-
const s = abs.toFixed(Math.min(decimals + FLOAT_GUARD_DIGITS, 100));
|
|
40
|
-
const dot = s.indexOf('.');
|
|
41
|
-
const cut = decimals === 0 ? dot : dot + 1 + decimals;
|
|
42
|
-
return Number(s.slice(0, cut));
|
|
23
|
+
function decimalSeparator(locale) {
|
|
24
|
+
return (new Intl.NumberFormat(locale)
|
|
25
|
+
.formatToParts(1.5)
|
|
26
|
+
.find((part) => part.type === 'decimal')?.value ?? '.');
|
|
43
27
|
}
|
|
44
28
|
/**
|
|
45
|
-
* Reduce `
|
|
29
|
+
* Reduce `value` to `decimals` places then split into a sign and the
|
|
46
30
|
* locale-formatted absolute body. The sign is derived from the reduced value
|
|
47
31
|
* so magnitudes that collapse to zero render without a spurious `-`/`+`.
|
|
48
32
|
* `signed` emits `+` for positives; `rounding` selects half-up or truncation.
|
|
49
33
|
*/
|
|
50
|
-
function signAndBody(
|
|
51
|
-
const reduced = rounding === 'floor'
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
34
|
+
function signAndBody(value, decimals, locale, grouping, rounding, signed) {
|
|
35
|
+
const reduced = value.round(decimals, rounding === 'floor' ? Big.roundDown : Big.roundHalfUp);
|
|
36
|
+
const sign = reduced.lt(0) ? '-' : signed && reduced.gt(0) ? '+' : '';
|
|
37
|
+
const [integerPart = '0', fractionPart] = reduced
|
|
38
|
+
.abs()
|
|
39
|
+
.toFixed(decimals)
|
|
40
|
+
.split('.');
|
|
41
|
+
const integerBody = grouping
|
|
42
|
+
? BigInt(integerPart).toLocaleString(locale)
|
|
43
|
+
: integerPart;
|
|
44
|
+
const body = fractionPart === undefined
|
|
45
|
+
? integerBody
|
|
46
|
+
: `${integerBody}${decimalSeparator(locale)}${fractionPart}`;
|
|
60
47
|
return { sign, body };
|
|
61
48
|
}
|
|
62
49
|
/**
|
|
@@ -64,13 +51,13 @@ function signAndBody(n, decimals, locale, grouping, rounding, signed) {
|
|
|
64
51
|
* `>=1` → 2, `>=0.1` → 4, `>=0.01` → 5, otherwise 6.
|
|
65
52
|
*/
|
|
66
53
|
function autoDecimals(abs) {
|
|
67
|
-
if (abs
|
|
54
|
+
if (abs.gte(1)) {
|
|
68
55
|
return 2;
|
|
69
56
|
}
|
|
70
|
-
if (abs
|
|
57
|
+
if (abs.gte('0.1')) {
|
|
71
58
|
return 4;
|
|
72
59
|
}
|
|
73
|
-
if (abs
|
|
60
|
+
if (abs.gte('0.01')) {
|
|
74
61
|
return 5;
|
|
75
62
|
}
|
|
76
63
|
return 6;
|
|
@@ -89,10 +76,13 @@ function autoDecimals(abs) {
|
|
|
89
76
|
*/
|
|
90
77
|
export function formatNumber(value, options = {}) {
|
|
91
78
|
const { decimals = 2, placeholder = DEFAULT_PLACEHOLDER, locale, rounding = 'halfUp', } = options;
|
|
92
|
-
const n =
|
|
79
|
+
const n = toBig(value);
|
|
93
80
|
if (n === null) {
|
|
94
81
|
return placeholder;
|
|
95
82
|
}
|
|
83
|
+
if (typeof n === 'string') {
|
|
84
|
+
return n;
|
|
85
|
+
}
|
|
96
86
|
const { sign, body } = signAndBody(n, decimals, locale, true, rounding, false);
|
|
97
87
|
return `${sign}${body}`;
|
|
98
88
|
}
|
|
@@ -106,10 +96,13 @@ export function formatNumber(value, options = {}) {
|
|
|
106
96
|
*/
|
|
107
97
|
export function formatUsd(value, options = {}) {
|
|
108
98
|
const { decimals = 2, placeholder = DEFAULT_PLACEHOLDER, locale, rounding = 'halfUp', } = options;
|
|
109
|
-
const n =
|
|
99
|
+
const n = toBig(value);
|
|
110
100
|
if (n === null) {
|
|
111
101
|
return placeholder;
|
|
112
102
|
}
|
|
103
|
+
if (typeof n === 'string') {
|
|
104
|
+
return n;
|
|
105
|
+
}
|
|
113
106
|
const { sign, body } = signAndBody(n, decimals, locale, true, rounding, false);
|
|
114
107
|
return `${sign}$${body}`;
|
|
115
108
|
}
|
|
@@ -124,10 +117,13 @@ export function formatUsd(value, options = {}) {
|
|
|
124
117
|
*/
|
|
125
118
|
export function formatSignedUsd(value, options = {}) {
|
|
126
119
|
const { decimals = 2, placeholder = DEFAULT_PLACEHOLDER, locale, rounding = 'halfUp', } = options;
|
|
127
|
-
const n =
|
|
120
|
+
const n = toBig(value);
|
|
128
121
|
if (n === null) {
|
|
129
122
|
return placeholder;
|
|
130
123
|
}
|
|
124
|
+
if (typeof n === 'string') {
|
|
125
|
+
return n;
|
|
126
|
+
}
|
|
131
127
|
const { sign, body } = signAndBody(n, decimals, locale, true, rounding, true);
|
|
132
128
|
return `${sign}$${body}`;
|
|
133
129
|
}
|
|
@@ -142,10 +138,13 @@ export function formatSignedUsd(value, options = {}) {
|
|
|
142
138
|
*/
|
|
143
139
|
export function formatSignedPercent(value, options = {}) {
|
|
144
140
|
const { decimals = 2, placeholder = DEFAULT_PLACEHOLDER, locale, rounding = 'halfUp', } = options;
|
|
145
|
-
const n =
|
|
141
|
+
const n = toBig(value);
|
|
146
142
|
if (n === null) {
|
|
147
143
|
return placeholder;
|
|
148
144
|
}
|
|
145
|
+
if (typeof n === 'string') {
|
|
146
|
+
return n;
|
|
147
|
+
}
|
|
149
148
|
const { sign, body } = signAndBody(n, decimals, locale, false, rounding, true);
|
|
150
149
|
return `${sign}${body}%`;
|
|
151
150
|
}
|
|
@@ -161,13 +160,16 @@ export function formatSignedPercent(value, options = {}) {
|
|
|
161
160
|
*/
|
|
162
161
|
export function formatPrice(value, options = {}) {
|
|
163
162
|
const { placeholder = DEFAULT_PLACEHOLDER, locale, rounding = 'halfUp', } = options;
|
|
164
|
-
const n =
|
|
163
|
+
const n = toBig(value);
|
|
165
164
|
if (n === null) {
|
|
166
165
|
return placeholder;
|
|
167
166
|
}
|
|
168
|
-
|
|
167
|
+
if (typeof n === 'string') {
|
|
168
|
+
return n;
|
|
169
|
+
}
|
|
170
|
+
const abs = n.abs();
|
|
169
171
|
const decimals = options.decimals ?? autoDecimals(abs);
|
|
170
|
-
const { sign, body } = signAndBody(n, decimals, locale, abs
|
|
172
|
+
const { sign, body } = signAndBody(n, decimals, locale, abs.gte(1000), rounding, false);
|
|
171
173
|
return `${sign}$${body}`;
|
|
172
174
|
}
|
|
173
175
|
/**
|
|
@@ -181,20 +183,23 @@ export function formatPrice(value, options = {}) {
|
|
|
181
183
|
*/
|
|
182
184
|
export function formatCompactUsd(value, options = {}) {
|
|
183
185
|
const { decimals = 2, placeholder = DEFAULT_PLACEHOLDER } = options;
|
|
184
|
-
const n =
|
|
186
|
+
const n = toBig(value);
|
|
185
187
|
if (n === null) {
|
|
186
188
|
return placeholder;
|
|
187
189
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
190
|
+
if (typeof n === 'string') {
|
|
191
|
+
return n;
|
|
192
|
+
}
|
|
193
|
+
const sign = n.lt(0) ? '-' : '';
|
|
194
|
+
const abs = n.abs();
|
|
195
|
+
if (abs.gte(1_000_000_000)) {
|
|
196
|
+
return `${sign}$${abs.div(1_000_000_000).toFixed(decimals)}B`;
|
|
192
197
|
}
|
|
193
|
-
if (abs
|
|
194
|
-
return `${sign}$${(
|
|
198
|
+
if (abs.gte(1_000_000)) {
|
|
199
|
+
return `${sign}$${abs.div(1_000_000).toFixed(decimals)}M`;
|
|
195
200
|
}
|
|
196
|
-
if (abs
|
|
197
|
-
return `${sign}$${(
|
|
201
|
+
if (abs.gte(1_000)) {
|
|
202
|
+
return `${sign}$${abs.div(1_000).toFixed(decimals)}K`;
|
|
198
203
|
}
|
|
199
204
|
return `${sign}$${abs.toFixed(decimals)}`;
|
|
200
205
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","sourceRoot":"","sources":["../../../src/decimal/format.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../../../src/decimal/format.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAiC5D,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAI/B;;;;GAIG;AACH,SAAS,KAAK,CAAC,KAAkB;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IACvD,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,MAA0B;IAClD,OAAO,CACL,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;SAC1B,aAAa,CAAC,GAAG,CAAC;SAClB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,EAAE,KAAK,IAAI,GAAG,CACzD,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAClB,KA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C,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;IAC/B,MAAM,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,CAAA;IACnB,IAAI,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,CAAC;QAC3B,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAA;IAC/D,CAAC;IACD,IAAI,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;QACvB,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAA;IAC3D,CAAC;IACD,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACnB,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAA;IACvD,CAAC;IACD,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAA;AAC3C,CAAC"}
|
|
@@ -1,16 +1,35 @@
|
|
|
1
|
-
import { type DecimalString } from '@lifi/perps-types';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Read a decimal string as a JS float, for a chart or pixel value only. A
|
|
3
|
+
* float holds about 15 significant digits, so the result can lose precision;
|
|
4
|
+
* never use it for an amount sent to a venue.
|
|
6
5
|
*
|
|
6
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a decimal string
|
|
7
|
+
* or is too large for a finite float.
|
|
7
8
|
* @public
|
|
8
9
|
*/
|
|
9
|
-
export declare function
|
|
10
|
+
export declare function decimalStringToNumber(value: string): number;
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
+
* {@link decimalStringToNumber}, or `undefined` with a warning when it throws.
|
|
12
13
|
*
|
|
13
14
|
* @public
|
|
14
15
|
*/
|
|
15
|
-
export declare
|
|
16
|
+
export declare const safeDecimalStringToNumber: (value: string) => number | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Narrows an unknown value to a {@link DecimalString}. No characters are
|
|
19
|
+
* removed first.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare function isDecimalString(value: unknown): value is string;
|
|
24
|
+
/**
|
|
25
|
+
* Read a venue value (a string or a number) as a decimal string, for a
|
|
26
|
+
* money-path value that must not go on wrong. A number or an exponent string
|
|
27
|
+
* is spelled out in full; no `$`, `%` or `,` is removed.
|
|
28
|
+
*
|
|
29
|
+
* @param tool - Provider key set on the error.
|
|
30
|
+
* @throws {PerpsError} `SDKError` naming `field` when `value` is not a finite
|
|
31
|
+
* decimal.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare function unknownToDecimalString(value: unknown, field: string, tool: string): string;
|
|
16
35
|
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/decimal/parse.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/decimal/parse.ts"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAS3D;AAED;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,uCAGrC,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE/D;AA2BD;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,GACX,MAAM,CAWR"}
|