@lifi/perps-sdk 36.3.0 → 36.4.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 +16 -3
- package/dist/cjs/decimal/big.d.ts +1 -0
- package/dist/cjs/decimal/big.d.ts.map +1 -1
- package/dist/cjs/decimal/big.js +4 -1
- package/dist/cjs/decimal/big.js.map +1 -1
- package/dist/cjs/decimal/format.d.ts +9 -1
- package/dist/cjs/decimal/format.d.ts.map +1 -1
- package/dist/cjs/decimal/format.js +18 -8
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +6 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +1 -1
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/esm/decimal/big.d.ts +5 -0
- package/dist/esm/decimal/big.d.ts.map +1 -1
- package/dist/esm/decimal/big.js +7 -0
- package/dist/esm/decimal/big.js.map +1 -1
- package/dist/esm/decimal/format.d.ts +35 -1
- package/dist/esm/decimal/format.d.ts.map +1 -1
- package/dist/esm/decimal/format.js +39 -19
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/wire/orderAmounts.d.ts +7 -3
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +5 -3
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/types/decimal/big.d.ts +5 -0
- package/dist/types/decimal/big.d.ts.map +1 -1
- package/dist/types/decimal/format.d.ts +35 -1
- package/dist/types/decimal/format.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/wire/orderAmounts.d.ts +7 -3
- package/dist/types/wire/orderAmounts.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/decimal/big.ts +8 -0
- package/src/decimal/format.ts +84 -19
- package/src/index.ts +3 -0
- package/src/version.ts +1 -1
- package/src/wire/orderAmounts.ts +9 -5
package/src/decimal/format.ts
CHANGED
|
@@ -37,6 +37,31 @@ export interface FormatOptions {
|
|
|
37
37
|
rounding?: RoundingMode
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Options for {@link formatNumber}.
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export interface NumberFormatOptions extends FormatOptions {
|
|
46
|
+
/** Drops trailing fraction zeros: `50.00` renders as `50`, `12.50` as `12.5`. */
|
|
47
|
+
trimZeros?: boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Options for {@link formatCompactNumber}.
|
|
52
|
+
*
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export interface CompactNumberOptions extends NumberFormatOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Significant digits of the compact mantissa, with trailing zeros dropped
|
|
58
|
+
* (`84.5K`, `1M`). Replaces `decimals` when set.
|
|
59
|
+
*/
|
|
60
|
+
significantDigits?: number
|
|
61
|
+
/** Magnitudes below this value render as {@link formatNumber}, with no suffix. */
|
|
62
|
+
compactFrom?: DecimalString
|
|
63
|
+
}
|
|
64
|
+
|
|
40
65
|
/**
|
|
41
66
|
* Options for {@link formatCompactUsd}.
|
|
42
67
|
*
|
|
@@ -102,17 +127,21 @@ function signAndBody(
|
|
|
102
127
|
locale: string | undefined,
|
|
103
128
|
grouping: boolean,
|
|
104
129
|
rounding: RoundingMode,
|
|
105
|
-
signed: boolean
|
|
130
|
+
signed: boolean,
|
|
131
|
+
trimZeros = false
|
|
106
132
|
): { sign: '+' | '-' | ''; body: string } {
|
|
107
133
|
const reduced = value.round(
|
|
108
134
|
decimals,
|
|
109
135
|
rounding === 'floor' ? Big.roundDown : Big.roundHalfUp
|
|
110
136
|
)
|
|
111
137
|
const sign = reduced.lt(0) ? '-' : signed && reduced.gt(0) ? '+' : ''
|
|
112
|
-
const [integerPart = '0',
|
|
138
|
+
const [integerPart = '0', paddedFraction] = reduced
|
|
113
139
|
.abs()
|
|
114
140
|
.toFixed(decimals)
|
|
115
141
|
.split('.')
|
|
142
|
+
const fractionPart = trimZeros
|
|
143
|
+
? paddedFraction?.replace(/0+$/, '') || undefined
|
|
144
|
+
: paddedFraction
|
|
116
145
|
const integerBody = grouping
|
|
117
146
|
? BigInt(integerPart).toLocaleString(locale)
|
|
118
147
|
: integerPart
|
|
@@ -154,13 +183,14 @@ function autoDecimals(abs: Big): number {
|
|
|
154
183
|
*/
|
|
155
184
|
export function formatNumber(
|
|
156
185
|
value: FormatInput,
|
|
157
|
-
options:
|
|
186
|
+
options: NumberFormatOptions = {}
|
|
158
187
|
): string {
|
|
159
188
|
const {
|
|
160
189
|
decimals = 2,
|
|
161
190
|
placeholder = DEFAULT_PLACEHOLDER,
|
|
162
191
|
locale,
|
|
163
192
|
rounding = 'halfUp',
|
|
193
|
+
trimZeros,
|
|
164
194
|
} = options
|
|
165
195
|
const n = toBig(value)
|
|
166
196
|
if (n === null) {
|
|
@@ -169,7 +199,15 @@ export function formatNumber(
|
|
|
169
199
|
if (typeof n === 'string') {
|
|
170
200
|
return n
|
|
171
201
|
}
|
|
172
|
-
const { sign, body } = signAndBody(
|
|
202
|
+
const { sign, body } = signAndBody(
|
|
203
|
+
n,
|
|
204
|
+
decimals,
|
|
205
|
+
locale,
|
|
206
|
+
true,
|
|
207
|
+
rounding,
|
|
208
|
+
false,
|
|
209
|
+
trimZeros
|
|
210
|
+
)
|
|
173
211
|
return `${sign}${body}`
|
|
174
212
|
}
|
|
175
213
|
|
|
@@ -348,20 +386,11 @@ function compactMantissa(
|
|
|
348
386
|
: roundToSignificantDigits(scaled, significantDigits, rounding)
|
|
349
387
|
}
|
|
350
388
|
|
|
351
|
-
/**
|
|
352
|
-
|
|
353
|
-
* `$789.0K`, `$12.34`.
|
|
354
|
-
*
|
|
355
|
-
* Values below 1000 (or `compactFrom`) render as {@link formatUsd}. Negatives
|
|
356
|
-
* place the `-` before the `$`. `rounding: 'floor'` truncates toward zero, so
|
|
357
|
-
* a compact balance is never overstated. Null/undefined/non-finite input
|
|
358
|
-
* renders the placeholder.
|
|
359
|
-
*
|
|
360
|
-
* @public
|
|
361
|
-
*/
|
|
362
|
-
export function formatCompactUsd(
|
|
389
|
+
/** The compact core: `plain` renders the value below the tiers and each mantissa. */
|
|
390
|
+
function formatCompact(
|
|
363
391
|
value: FormatInput,
|
|
364
|
-
options:
|
|
392
|
+
options: CompactNumberOptions,
|
|
393
|
+
plain: typeof formatNumber
|
|
365
394
|
): string {
|
|
366
395
|
const {
|
|
367
396
|
decimals = 2,
|
|
@@ -370,6 +399,7 @@ export function formatCompactUsd(
|
|
|
370
399
|
rounding = 'halfUp',
|
|
371
400
|
significantDigits,
|
|
372
401
|
compactFrom,
|
|
402
|
+
trimZeros,
|
|
373
403
|
} = options
|
|
374
404
|
const n = toBig(value)
|
|
375
405
|
if (n === null) {
|
|
@@ -384,7 +414,7 @@ export function formatCompactUsd(
|
|
|
384
414
|
compareDecimalStrings(abs, numberToDecimalString(COMPACT_TIER_SIZE)) < 0 ||
|
|
385
415
|
(compactFrom !== undefined && compareDecimalStrings(abs, compactFrom) < 0)
|
|
386
416
|
) {
|
|
387
|
-
return
|
|
417
|
+
return plain(value, options)
|
|
388
418
|
}
|
|
389
419
|
const decimalRounding = COMPACT_DECIMAL_ROUNDING[rounding]
|
|
390
420
|
const render = (mantissa: string, suffix: string): string => {
|
|
@@ -392,7 +422,7 @@ export function formatCompactUsd(
|
|
|
392
422
|
significantDigits === undefined
|
|
393
423
|
? decimals
|
|
394
424
|
: (mantissa.split('.')[1]?.length ?? 0)
|
|
395
|
-
return `${
|
|
425
|
+
return `${plain(mantissa, { locale, decimals: mantissaDecimals, trimZeros })}${suffix}`
|
|
396
426
|
}
|
|
397
427
|
// The tier is picked after rounding, so 999,999.99 promotes to M, not 1000K.
|
|
398
428
|
for (const [divisor, suffix] of COMPACT_TIERS) {
|
|
@@ -423,3 +453,38 @@ export function formatCompactUsd(
|
|
|
423
453
|
'B'
|
|
424
454
|
)
|
|
425
455
|
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Format a number compactly with a `B`/`M`/`K` suffix and no currency symbol:
|
|
459
|
+
* `1.23B`, `45.60M`, `789.00K`, `12.34`.
|
|
460
|
+
*
|
|
461
|
+
* Values below 1000 (or `compactFrom`) render as {@link formatNumber}.
|
|
462
|
+
* `rounding: 'floor'` truncates toward zero, so a compact balance is never
|
|
463
|
+
* overstated. Null/undefined/non-finite input renders the placeholder.
|
|
464
|
+
*
|
|
465
|
+
* @public
|
|
466
|
+
*/
|
|
467
|
+
export function formatCompactNumber(
|
|
468
|
+
value: FormatInput,
|
|
469
|
+
options: CompactNumberOptions = {}
|
|
470
|
+
): string {
|
|
471
|
+
return formatCompact(value, options, formatNumber)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Format a USD value compactly with a `B`/`M`/`K` suffix: `$1.23B`, `$45.6M`,
|
|
476
|
+
* `$789.0K`, `$12.34`.
|
|
477
|
+
*
|
|
478
|
+
* Values below 1000 (or `compactFrom`) render as {@link formatUsd}. Negatives
|
|
479
|
+
* place the `-` before the `$`. `rounding: 'floor'` truncates toward zero, so
|
|
480
|
+
* a compact balance is never overstated. Null/undefined/non-finite input
|
|
481
|
+
* renders the placeholder.
|
|
482
|
+
*
|
|
483
|
+
* @public
|
|
484
|
+
*/
|
|
485
|
+
export function formatCompactUsd(
|
|
486
|
+
value: FormatInput,
|
|
487
|
+
options: CompactUsdOptions = {}
|
|
488
|
+
): string {
|
|
489
|
+
return formatCompact(value, options, formatUsd)
|
|
490
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -57,11 +57,14 @@ export {
|
|
|
57
57
|
scaledIntegerToDecimalString,
|
|
58
58
|
} from './decimal/convert.js'
|
|
59
59
|
export type {
|
|
60
|
+
CompactNumberOptions,
|
|
60
61
|
CompactUsdOptions,
|
|
61
62
|
FormatOptions,
|
|
63
|
+
NumberFormatOptions,
|
|
62
64
|
RoundingMode,
|
|
63
65
|
} from './decimal/format.js'
|
|
64
66
|
export {
|
|
67
|
+
formatCompactNumber,
|
|
65
68
|
formatCompactUsd,
|
|
66
69
|
formatNumber,
|
|
67
70
|
formatPrice,
|
package/src/version.ts
CHANGED
package/src/wire/orderAmounts.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
PerpsErrorCode,
|
|
5
5
|
} from '@lifi/perps-types'
|
|
6
6
|
import type Big from 'big.js'
|
|
7
|
-
import {
|
|
7
|
+
import { RoundUpBig, TruncBig } from '../decimal/big.js'
|
|
8
8
|
import { isDecimalStringZero } from '../decimal/compare.js'
|
|
9
9
|
import { isDecimalString } from '../decimal/parse.js'
|
|
10
10
|
import { invalidInput } from '../errors/invalidInput.js'
|
|
@@ -48,8 +48,10 @@ export type OrderAmountsInput = OrderAmountsInputBase &
|
|
|
48
48
|
export interface OrderAmounts {
|
|
49
49
|
/**
|
|
50
50
|
* The held `amount`, byte-identical, when the user holds the margin;
|
|
51
|
-
* otherwise `notional ÷ leverage` or `notional × IMF ÷ 10000`,
|
|
52
|
-
*
|
|
51
|
+
* otherwise `notional ÷ leverage` or `notional × IMF ÷ 10000`, rounded up at
|
|
52
|
+
* 40 places so that, held, it sizes the same order. A value of at most 40
|
|
53
|
+
* places at or above the exact margin is at or above it too. A display and
|
|
54
|
+
* comparison value that never reaches a venue.
|
|
53
55
|
*/
|
|
54
56
|
margin: DecimalString
|
|
55
57
|
/** Snapped onto the venue lot grid; the one amount an order sends. */
|
|
@@ -109,7 +111,9 @@ function resolveMarginRate(
|
|
|
109
111
|
* size alone; no quote amount has a grid. A held margin returns byte-identical,
|
|
110
112
|
* `notional` is the snapped size × price, and a derived margin is
|
|
111
113
|
* `notional ÷ leverage`, or `notional × initialMarginFractionBps ÷ 10000`
|
|
112
|
-
* from a native IMF,
|
|
114
|
+
* from a native IMF, rounded up at 40 places. Held back as `'margin'` with the
|
|
115
|
+
* same price and margin rate, a derived margin sizes the same order. A value
|
|
116
|
+
* of at most 40 places at or above the exact margin is at or above it too.
|
|
113
117
|
*
|
|
114
118
|
* @throws {PerpsError} `ValidationError` that names `amount` or `price` when
|
|
115
119
|
* it is not a positive decimal, `initialMarginFractionBps` when it is
|
|
@@ -147,7 +151,7 @@ export function calculateOrderAmounts(input: OrderAmountsInput): OrderAmounts {
|
|
|
147
151
|
margin:
|
|
148
152
|
held === 'margin'
|
|
149
153
|
? input.amount
|
|
150
|
-
: new
|
|
154
|
+
: new RoundUpBig(notional)
|
|
151
155
|
.times(rateNumerator)
|
|
152
156
|
.div(rateDenominator)
|
|
153
157
|
.toFixed(),
|