@lifi/perps-sdk 23.1.0 → 25.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 +102 -56
- 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 +15 -0
- package/dist/cjs/decimal/arithmetic.d.ts.map +1 -0
- package/dist/cjs/decimal/arithmetic.js +53 -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 +6 -2
- package/dist/cjs/decimal/compare.d.ts.map +1 -1
- package/dist/cjs/decimal/compare.js +15 -2
- package/dist/cjs/decimal/compare.js.map +1 -1
- package/dist/cjs/decimal/convert.d.ts +10 -6
- package/dist/cjs/decimal/convert.d.ts.map +1 -1
- package/dist/cjs/decimal/convert.js +34 -21
- 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 +25 -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 -52
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +5 -4
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +43 -25
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/errors/invalidInput.d.ts +3 -0
- package/dist/cjs/errors/invalidInput.d.ts.map +1 -0
- package/dist/cjs/errors/invalidInput.js +9 -0
- package/dist/cjs/errors/invalidInput.js.map +1 -0
- package/dist/cjs/index.d.ts +15 -12
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +69 -9
- 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 +131 -134
- 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 +93 -94
- 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 +6 -0
- package/dist/cjs/utils/warnOnce.d.ts.map +1 -1
- package/dist/cjs/utils/warnOnce.js +19 -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 +9 -0
- package/dist/cjs/websocket/wsLog.js.map +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts +3 -2
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +22 -21
- 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 +98 -0
- package/dist/esm/decimal/arithmetic.d.ts.map +1 -0
- package/dist/esm/decimal/arithmetic.js +126 -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 +40 -6
- package/dist/esm/decimal/compare.d.ts.map +1 -1
- package/dist/esm/decimal/compare.js +46 -6
- package/dist/esm/decimal/compare.js.map +1 -1
- package/dist/esm/decimal/convert.d.ts +50 -36
- package/dist/esm/decimal/convert.d.ts.map +1 -1
- package/dist/esm/decimal/convert.js +64 -42
- package/dist/esm/decimal/convert.js.map +1 -1
- package/dist/esm/decimal/decimalStringToBig.d.ts +25 -0
- package/dist/esm/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/esm/decimal/decimalStringToBig.js +37 -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 -70
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +29 -13
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +61 -44
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/errors/invalidInput.d.ts +4 -0
- package/dist/esm/errors/invalidInput.d.ts.map +1 -0
- package/dist/esm/errors/invalidInput.js +7 -0
- package/dist/esm/errors/invalidInput.js.map +1 -0
- package/dist/esm/index.d.ts +15 -12
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +11 -8
- 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 +84 -66
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +178 -175
- 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 +160 -156
- 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 +30 -16
- 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 +27 -0
- package/dist/esm/utils/warnOnce.d.ts.map +1 -1
- package/dist/esm/utils/warnOnce.js +36 -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 +19 -0
- package/dist/esm/websocket/wsLog.js.map +1 -1
- package/dist/esm/wire/orderAmounts.d.ts +9 -7
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +24 -20
- 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.d.ts +2 -2
- package/dist/esm/wire/transferable.js +5 -5
- 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 +98 -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 +40 -6
- package/dist/types/decimal/compare.d.ts.map +1 -1
- package/dist/types/decimal/convert.d.ts +50 -36
- package/dist/types/decimal/convert.d.ts.map +1 -1
- package/dist/types/decimal/decimalStringToBig.d.ts +25 -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 +29 -13
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/errors/invalidInput.d.ts +4 -0
- package/dist/types/errors/invalidInput.d.ts.map +1 -0
- package/dist/types/index.d.ts +15 -12
- 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 +84 -66
- 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 +30 -16
- 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 +27 -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 +9 -7
- 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/transferable.d.ts +2 -2
- 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 +170 -0
- package/src/decimal/big.ts +0 -5
- package/src/decimal/compare.ts +61 -11
- package/src/decimal/convert.ts +105 -63
- package/src/decimal/decimalStringToBig.ts +42 -0
- package/src/decimal/format.ts +81 -72
- package/src/decimal/parse.ts +78 -57
- package/src/errors/invalidInput.ts +10 -0
- package/src/index.ts +80 -12
- package/src/math/fills.ts +32 -17
- package/src/math/order.ts +279 -226
- package/src/math/position.ts +280 -196
- 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 +39 -16
- 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 +60 -0
- package/src/version.ts +1 -1
- package/src/websocket/PerpsWsClient.ts +25 -2
- package/src/websocket/wsLog.ts +44 -0
- package/src/wire/orderAmounts.ts +45 -26
- package/src/wire/refuel.ts +56 -20
- package/src/wire/transferable.ts +5 -5
- 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 -12
- package/dist/cjs/decimal/requireDecimal.js.map +0 -1
- package/dist/esm/decimal/requireDecimal.d.ts +0 -5
- package/dist/esm/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/esm/decimal/requireDecimal.js +0 -7
- package/dist/esm/decimal/requireDecimal.js.map +0 -1
- package/dist/types/decimal/requireDecimal.d.ts +0 -5
- package/dist/types/decimal/requireDecimal.d.ts.map +0 -1
- package/src/decimal/requireDecimal.ts +0 -8
|
@@ -1,267 +1,271 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Display-tier position formulas and margin-adjustment predicates. Every
|
|
3
|
-
* formula takes and gives
|
|
4
|
-
*
|
|
3
|
+
* formula takes and gives decimal strings; arithmetic is exact Big.js, with
|
|
4
|
+
* division to 40 decimal places. Each formula throws `ValidationError` on an
|
|
5
|
+
* input that does not match the decimal pattern and has a `safe*` pair that gives
|
|
6
|
+
* `undefined` in place of the throw.
|
|
5
7
|
*
|
|
6
8
|
* Sign convention: long = +1, short = -1. Sizes passed to the `estimate*`
|
|
7
9
|
* helpers are always non-negative magnitudes; direction is carried by
|
|
8
10
|
* `isLong`.
|
|
9
11
|
*/
|
|
10
12
|
import { MarginMode, PositionMarginAdjustment, } from '@lifi/perps-types';
|
|
11
|
-
import {
|
|
13
|
+
import { DivBig } from '../decimal/big.js';
|
|
14
|
+
import { bigToDecimalString, decimalStringToBig, decimalStringToDivBig, } from '../decimal/decimalStringToBig.js';
|
|
15
|
+
import { invalidInput } from '../errors/invalidInput.js';
|
|
16
|
+
import { createSafeFunction } from '../utils/createSafeFunction.js';
|
|
12
17
|
/**
|
|
13
|
-
*
|
|
18
|
+
* Notional value of a position: `|size| × price`.
|
|
14
19
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @param price - Current asset price
|
|
17
|
-
* @returns Notional value in USD
|
|
20
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
|
|
18
21
|
* @public
|
|
19
22
|
*/
|
|
20
23
|
export function calculateNotionalValue(size, price) {
|
|
21
|
-
|
|
22
|
-
return Number.NaN;
|
|
23
|
-
}
|
|
24
|
-
return new DivBig(size).abs().times(price).toNumber();
|
|
24
|
+
return bigToDecimalString(decimalStringToBig(size).abs().times(decimalStringToBig(price)));
|
|
25
25
|
}
|
|
26
|
+
/** @public */
|
|
27
|
+
export const safeCalculateNotionalValue = createSafeFunction('calculateNotionalValue', calculateNotionalValue);
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
29
|
+
* Unrealized PnL: `(currentPrice - entryPrice) × size`, where `size` is
|
|
30
|
+
* positive for a long and negative for a short.
|
|
28
31
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @param currentPrice - Current market price
|
|
31
|
-
* @param size - Position size (positive for long, negative for short)
|
|
32
|
-
* @returns Unrealized PnL in USD
|
|
32
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
|
|
33
33
|
* @public
|
|
34
34
|
*/
|
|
35
35
|
export function calculateUnrealizedPnl(entryPrice, currentPrice, size) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return new DivBig(currentPrice).minus(entryPrice).times(size).toNumber();
|
|
36
|
+
return bigToDecimalString(decimalStringToBig(currentPrice)
|
|
37
|
+
.minus(decimalStringToBig(entryPrice))
|
|
38
|
+
.times(decimalStringToBig(size)));
|
|
40
39
|
}
|
|
40
|
+
/** @public */
|
|
41
|
+
export const safeCalculateUnrealizedPnl = createSafeFunction('calculateUnrealizedPnl', calculateUnrealizedPnl);
|
|
41
42
|
/**
|
|
42
|
-
*
|
|
43
|
+
* Return on equity as a percentage (`'10'` for 10%): `pnl ÷ margin × 100`.
|
|
43
44
|
*
|
|
44
|
-
* @
|
|
45
|
-
*
|
|
46
|
-
* @returns ROE as percentage (e.g., 10 for 10%)
|
|
45
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
46
|
+
* decimal pattern or `margin` is zero.
|
|
47
47
|
* @public
|
|
48
48
|
*/
|
|
49
49
|
export function calculateRoe(pnl, margin) {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const marginBig = decimalStringToBig(margin);
|
|
51
|
+
const pnlBig = decimalStringToDivBig(pnl);
|
|
52
|
+
if (marginBig.eq(0)) {
|
|
53
|
+
throw invalidInput('margin', 'must not be zero');
|
|
52
54
|
}
|
|
53
|
-
|
|
54
|
-
return Number.NaN;
|
|
55
|
-
}
|
|
56
|
-
return new DivBig(pnl).div(margin).times(100).toNumber();
|
|
55
|
+
return bigToDecimalString(pnlBig.div(marginBig).times(100));
|
|
57
56
|
}
|
|
57
|
+
/** @public */
|
|
58
|
+
export const safeCalculateRoe = createSafeFunction('calculateRoe', calculateRoe);
|
|
58
59
|
/**
|
|
59
|
-
*
|
|
60
|
+
* Required margin: `notionalValue ÷ leverage`.
|
|
60
61
|
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
63
|
-
* @returns Required margin in USD
|
|
62
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern
|
|
63
|
+
* or `leverage` is zero.
|
|
64
64
|
* @public
|
|
65
65
|
*/
|
|
66
66
|
export function calculateRequiredMargin(notionalValue, leverage) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return Number.NaN;
|
|
67
|
+
const notionalBig = decimalStringToDivBig(notionalValue);
|
|
68
|
+
const leverageBig = decimalStringToBig(leverage);
|
|
69
|
+
if (leverageBig.eq(0)) {
|
|
70
|
+
throw invalidInput('leverage', 'must not be zero');
|
|
72
71
|
}
|
|
73
|
-
return
|
|
72
|
+
return bigToDecimalString(notionalBig.div(leverageBig));
|
|
74
73
|
}
|
|
74
|
+
/** @public */
|
|
75
|
+
export const safeCalculateRequiredMargin = createSafeFunction('calculateRequiredMargin', calculateRequiredMargin);
|
|
75
76
|
/**
|
|
76
|
-
* Distance from the current price to the liquidation price, as
|
|
77
|
-
* the current price.
|
|
77
|
+
* Distance from the current price to the liquidation price, as an absolute
|
|
78
|
+
* percentage of the current price.
|
|
78
79
|
*
|
|
79
|
-
* @
|
|
80
|
-
*
|
|
81
|
-
* @returns Absolute distance as a percentage, or 0 when current price is zero
|
|
80
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
81
|
+
* decimal pattern or `currentPrice` is zero.
|
|
82
82
|
* @public
|
|
83
83
|
*/
|
|
84
84
|
export function calculateLiquidationDistance(params) {
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
const liquidationPrice = decimalStringToDivBig(params.liquidationPrice);
|
|
86
|
+
const currentPrice = decimalStringToBig(params.currentPrice);
|
|
87
|
+
if (currentPrice.eq(0)) {
|
|
88
|
+
throw invalidInput('currentPrice', 'must not be zero');
|
|
88
89
|
}
|
|
89
|
-
return
|
|
90
|
+
return bigToDecimalString(liquidationPrice.minus(currentPrice).div(currentPrice).abs().times(100));
|
|
90
91
|
}
|
|
92
|
+
/** @public */
|
|
93
|
+
export const safeCalculateLiquidationDistance = createSafeFunction('calculateLiquidationDistance', calculateLiquidationDistance);
|
|
91
94
|
/**
|
|
92
|
-
* Effective leverage of an open position
|
|
95
|
+
* Effective leverage of an open position: `positionValueUsd ÷ marginUsd`.
|
|
93
96
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* @param positionValueUsd - Position notional value in USD
|
|
97
|
-
* @param marginUsd - Margin backing the position in USD
|
|
98
|
-
* @returns Effective leverage, or 0 when margin is zero
|
|
97
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
98
|
+
* decimal pattern or `marginUsd` is zero.
|
|
99
99
|
* @public
|
|
100
100
|
*/
|
|
101
101
|
export function calculateEffectiveLeverage(params) {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (!areFinite(positionValueUsd, marginUsd)) {
|
|
107
|
-
return Number.NaN;
|
|
102
|
+
const positionValue = decimalStringToDivBig(params.positionValueUsd);
|
|
103
|
+
const margin = decimalStringToBig(params.marginUsd);
|
|
104
|
+
if (margin.eq(0)) {
|
|
105
|
+
throw invalidInput('marginUsd', 'must not be zero');
|
|
108
106
|
}
|
|
109
|
-
return
|
|
107
|
+
return bigToDecimalString(positionValue.div(margin));
|
|
110
108
|
}
|
|
109
|
+
/** @public */
|
|
110
|
+
export const safeCalculateEffectiveLeverage = createSafeFunction('calculateEffectiveLeverage', calculateEffectiveLeverage);
|
|
111
111
|
/**
|
|
112
112
|
* Estimated liquidation price for an isolated-margin position, parameterised
|
|
113
113
|
* by the venue's maintenance margin rate.
|
|
114
114
|
*
|
|
115
115
|
* Standard isolated-margin model (new position, no existing exposure):
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* margin_available = margin_per_unit - maintenance_per_unit
|
|
119
|
-
* liq_price = entryPrice - side * margin_available / (1 - mmr * side)
|
|
116
|
+
* margin_available = entryPrice × (1 / leverage - mmr)
|
|
117
|
+
* liq_price = entryPrice - side × margin_available / (1 - mmr × side)
|
|
120
118
|
*
|
|
121
119
|
* For existing positions, prefer `Position.liquidationPrice` from the venue.
|
|
122
120
|
*
|
|
123
121
|
* @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
|
|
124
|
-
* (
|
|
125
|
-
* @
|
|
126
|
-
*
|
|
122
|
+
* (`'0.01'` = 1%).
|
|
123
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
124
|
+
* decimal pattern, `leverage` is zero, or `maintenanceMarginRate` gives a
|
|
125
|
+
* zero denominator (`1` for a long, `-1` for a short).
|
|
127
126
|
* @public
|
|
128
127
|
*/
|
|
129
128
|
export function estimateLiquidationPrice(params) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
const entryPrice = decimalStringToBig(params.entryPrice);
|
|
130
|
+
const leverage = decimalStringToBig(params.leverage);
|
|
131
|
+
const mmr = decimalStringToBig(params.maintenanceMarginRate);
|
|
132
|
+
const side = params.isLong ? 1 : -1;
|
|
133
|
+
const denominator = mmr.times(-side).plus(1);
|
|
134
|
+
if (leverage.eq(0)) {
|
|
135
|
+
throw invalidInput('leverage', 'must not be zero');
|
|
133
136
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (denominator === 0) {
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
if (!areFinite(entryPrice, leverage, maintenanceMarginRate)) {
|
|
140
|
-
return Number.NaN;
|
|
137
|
+
if (denominator.eq(0)) {
|
|
138
|
+
throw invalidInput('maintenanceMarginRate', `must not be ${params.isLong ? '1' : '-1'} for this side`);
|
|
141
139
|
}
|
|
142
|
-
const mmr = new DivBig(maintenanceMarginRate);
|
|
143
140
|
const marginAvailable = new DivBig(1)
|
|
144
141
|
.div(leverage)
|
|
145
142
|
.minus(mmr)
|
|
146
143
|
.times(entryPrice);
|
|
147
|
-
return
|
|
148
|
-
|
|
149
|
-
|
|
144
|
+
return bigToDecimalString(entryPrice.minus(marginAvailable.times(side).div(denominator)));
|
|
145
|
+
}
|
|
146
|
+
/** @public */
|
|
147
|
+
export const safeEstimateLiquidationPrice = createSafeFunction('estimateLiquidationPrice', estimateLiquidationPrice);
|
|
148
|
+
/**
|
|
149
|
+
* Estimate the liquidation price of a new isolated position from the flat
|
|
150
|
+
* `market.maintenanceMarginRate`.
|
|
151
|
+
*
|
|
152
|
+
* @returns The estimate, or `undefined` when the market carries no
|
|
153
|
+
* `maintenanceMarginRate`.
|
|
154
|
+
* @throws {PerpsError} `ValidationError` on an input that
|
|
155
|
+
* {@link estimateLiquidationPrice} rejects.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
export function estimateLiquidationPriceAtMarketRate(market, params) {
|
|
159
|
+
if (market.maintenanceMarginRate === undefined) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
return estimateLiquidationPrice({
|
|
163
|
+
entryPrice: params.entryPrice,
|
|
164
|
+
leverage: params.leverage,
|
|
165
|
+
isLong: params.isLong,
|
|
166
|
+
maintenanceMarginRate: market.maintenanceMarginRate,
|
|
167
|
+
});
|
|
150
168
|
}
|
|
169
|
+
/** @public */
|
|
170
|
+
export const safeEstimateLiquidationPriceAtMarketRate = createSafeFunction('estimateLiquidationPriceAtMarketRate', estimateLiquidationPriceAtMarketRate);
|
|
151
171
|
/**
|
|
152
172
|
* Whether an isolated position with this liquidation price is already past
|
|
153
173
|
* liquidation at `currentPrice`: a long liquidates when the price falls to its
|
|
154
174
|
* liquidation level, a short when it rises to it. A non-positive price on
|
|
155
175
|
* either side counts as unknown, so the result is `false`.
|
|
156
176
|
*
|
|
177
|
+
* @throws {PerpsError} `ValidationError` when a price does not match the decimal pattern.
|
|
157
178
|
* @public
|
|
158
179
|
*/
|
|
159
180
|
export function wouldImmediatelyLiquidate(params) {
|
|
160
|
-
const
|
|
161
|
-
|
|
181
|
+
const liquidationPrice = decimalStringToBig(params.liquidationPrice);
|
|
182
|
+
const currentPrice = decimalStringToBig(params.currentPrice);
|
|
183
|
+
if (currentPrice.lte(0) || liquidationPrice.lte(0)) {
|
|
162
184
|
return false;
|
|
163
185
|
}
|
|
164
|
-
return isLong
|
|
165
|
-
? liquidationPrice
|
|
166
|
-
: liquidationPrice
|
|
186
|
+
return params.isLong
|
|
187
|
+
? liquidationPrice.gte(currentPrice)
|
|
188
|
+
: liquidationPrice.lte(currentPrice);
|
|
167
189
|
}
|
|
190
|
+
/** @public */
|
|
191
|
+
export const safeWouldImmediatelyLiquidate = createSafeFunction('wouldImmediatelyLiquidate', wouldImmediatelyLiquidate);
|
|
168
192
|
/**
|
|
169
|
-
* Estimated average entry price after adding to an existing position
|
|
193
|
+
* Estimated average entry price after adding to an existing position: the
|
|
194
|
+
* size-weighted average of the current entry and the fill price. Both legs
|
|
195
|
+
* are in the same direction (an add, not a flip).
|
|
170
196
|
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* @param currentSize - Existing position size in coin units (>= 0).
|
|
176
|
-
* @param currentEntry - Existing position's average entry price.
|
|
177
|
-
* @param addSize - Size being added in coin units (>= 0).
|
|
178
|
-
* @param fillPrice - Price the new size is expected to fill at (mid for
|
|
179
|
-
* market, limitPrice for limit orders).
|
|
180
|
-
* @returns The new weighted-average entry price, or undefined if the inputs
|
|
181
|
-
* cannot produce a valid average (zero combined size, non-finite values).
|
|
197
|
+
* @param fillPrice - Expected fill price (mid for market, limit price for
|
|
198
|
+
* limit orders).
|
|
199
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
200
|
+
* decimal pattern or `currentSize + addSize` is not positive.
|
|
182
201
|
* @public
|
|
183
202
|
*/
|
|
184
203
|
export function estimateAverageEntryPrice(params) {
|
|
185
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
if (!areFinite(currentSize, addSize)) {
|
|
194
|
-
return Number.NaN;
|
|
204
|
+
const currentSize = decimalStringToDivBig(params.currentSize);
|
|
205
|
+
const addSize = decimalStringToBig(params.addSize);
|
|
206
|
+
const currentEntry = decimalStringToBig(params.currentEntry);
|
|
207
|
+
const fillPrice = decimalStringToBig(params.fillPrice);
|
|
208
|
+
const totalSize = currentSize.plus(addSize);
|
|
209
|
+
if (totalSize.lte(0)) {
|
|
210
|
+
throw invalidInput('currentSize + addSize', 'must be positive');
|
|
195
211
|
}
|
|
196
|
-
return
|
|
212
|
+
return bigToDecimalString(currentSize
|
|
197
213
|
.times(currentEntry)
|
|
198
|
-
.plus(
|
|
199
|
-
.div(
|
|
200
|
-
.toNumber();
|
|
214
|
+
.plus(addSize.times(fillPrice))
|
|
215
|
+
.div(totalSize));
|
|
201
216
|
}
|
|
217
|
+
/** @public */
|
|
218
|
+
export const safeEstimateAverageEntryPrice = createSafeFunction('estimateAverageEntryPrice', estimateAverageEntryPrice);
|
|
202
219
|
/**
|
|
203
|
-
* Estimated effective leverage after adding margin and notional
|
|
220
|
+
* Estimated effective leverage after adding margin and notional:
|
|
221
|
+
* `(currentNotional + addNotional) ÷ (currentMargin + addMargin)`.
|
|
204
222
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* margin the user is about to put up.
|
|
208
|
-
*
|
|
209
|
-
* @returns The new effective leverage, or undefined if total margin is
|
|
210
|
-
* non-positive.
|
|
223
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
224
|
+
* decimal pattern or `currentMargin + addMargin` is not positive.
|
|
211
225
|
* @public
|
|
212
226
|
*/
|
|
213
227
|
export function estimateNewLeverage(params) {
|
|
214
|
-
const
|
|
215
|
-
const totalMargin = currentMargin
|
|
216
|
-
if (totalMargin
|
|
217
|
-
|
|
228
|
+
const totalNotional = decimalStringToDivBig(params.currentNotional).plus(decimalStringToBig(params.addNotional));
|
|
229
|
+
const totalMargin = decimalStringToBig(params.currentMargin).plus(decimalStringToBig(params.addMargin));
|
|
230
|
+
if (totalMargin.lte(0)) {
|
|
231
|
+
throw invalidInput('currentMargin + addMargin', 'must be positive');
|
|
218
232
|
}
|
|
219
|
-
|
|
220
|
-
return Number.NaN;
|
|
221
|
-
}
|
|
222
|
-
return new DivBig(currentNotional)
|
|
223
|
-
.plus(addNotional)
|
|
224
|
-
.div(new DivBig(currentMargin).plus(addMargin))
|
|
225
|
-
.toNumber();
|
|
233
|
+
return bigToDecimalString(totalNotional.div(totalMargin));
|
|
226
234
|
}
|
|
235
|
+
/** @public */
|
|
236
|
+
export const safeEstimateNewLeverage = createSafeFunction('estimateNewLeverage', estimateNewLeverage);
|
|
227
237
|
/**
|
|
228
|
-
* Estimated unrealised PnL at the
|
|
229
|
-
*
|
|
230
|
-
* `pnl = (markPrice - entryPrice) * size * (isLong ? 1 : -1)`
|
|
238
|
+
* Estimated unrealised PnL at the mark price:
|
|
239
|
+
* `(markPrice - entryPrice) × size × (isLong ? 1 : -1)`.
|
|
231
240
|
*
|
|
232
241
|
* @param size - Position size as a non-negative magnitude.
|
|
242
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
|
|
233
243
|
* @public
|
|
234
244
|
*/
|
|
235
245
|
export function estimateUnrealizedPnl(params) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return new DivBig(markPrice)
|
|
241
|
-
.minus(entryPrice)
|
|
242
|
-
.times(size)
|
|
243
|
-
.times(isLong ? 1 : -1)
|
|
244
|
-
.toNumber();
|
|
246
|
+
return bigToDecimalString(decimalStringToBig(params.markPrice)
|
|
247
|
+
.minus(decimalStringToBig(params.entryPrice))
|
|
248
|
+
.times(decimalStringToBig(params.size))
|
|
249
|
+
.times(params.isLong ? 1 : -1));
|
|
245
250
|
}
|
|
251
|
+
/** @public */
|
|
252
|
+
export const safeEstimateUnrealizedPnl = createSafeFunction('estimateUnrealizedPnl', estimateUnrealizedPnl);
|
|
246
253
|
/**
|
|
247
|
-
* Realised PnL on the portion of a position being closed
|
|
248
|
-
*
|
|
249
|
-
* `rPnl = (closePrice - entryPrice) * closeSize * (isLong ? 1 : -1)`
|
|
254
|
+
* Realised PnL on the portion of a position being closed:
|
|
255
|
+
* `(closePrice - entryPrice) × closeSize × (isLong ? 1 : -1)`.
|
|
250
256
|
*
|
|
251
257
|
* @param closeSize - Size being closed as a non-negative magnitude.
|
|
258
|
+
* @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
|
|
252
259
|
* @public
|
|
253
260
|
*/
|
|
254
261
|
export function calculateRealizedPnl(params) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return new DivBig(closePrice)
|
|
260
|
-
.minus(entryPrice)
|
|
261
|
-
.times(closeSize)
|
|
262
|
-
.times(isLong ? 1 : -1)
|
|
263
|
-
.toNumber();
|
|
262
|
+
return bigToDecimalString(decimalStringToBig(params.closePrice)
|
|
263
|
+
.minus(decimalStringToBig(params.entryPrice))
|
|
264
|
+
.times(decimalStringToBig(params.closeSize))
|
|
265
|
+
.times(params.isLong ? 1 : -1));
|
|
264
266
|
}
|
|
267
|
+
/** @public */
|
|
268
|
+
export const safeCalculateRealizedPnl = createSafeFunction('calculateRealizedPnl', calculateRealizedPnl);
|
|
265
269
|
/**
|
|
266
270
|
* Whether this position can take a margin adjustment at all: it holds margin of
|
|
267
271
|
* its own and its market exposes individual position margin.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.js","sourceRoot":"","sources":["../../../src/math/position.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"position.js","sourceRoot":"","sources":["../../../src/math/position.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EACL,UA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AC,kBAAkB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;SAC5C,KAAK,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;SAC3C,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACjC,CAAA;AACH,CAAC;AAED,cAAc;AACd,MAAM,CAAC,MAAM,wBAAwB,GAAG,kBAAkB,CACxD,sBAAsB,EACtB,oBAAoB,CACrB,CAAA;AAED;;;;;GAKG;AACH,MAAM,UAAU,gCAAgC,CAAC,QAAkB;IACjE,OAAO,CACL,QAAQ,CAAC,UAAU,KAAK,UAAU,CAAC,QAAQ;QAC3C,QAAQ,CAAC,MAAM,CAAC,wBAAwB,KAAK,wBAAwB,CAAC,IAAI,CAC3E,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,6BAA6B,CAAC,QAAkB;IAC9D,OAAO,CACL,gCAAgC,CAAC,QAAQ,CAAC;QAC1C,QAAQ,CAAC,MAAM,CAAC,wBAAwB;YACtC,wBAAwB,CAAC,cAAc,CAC1C,CAAA;AACH,CAAC"}
|
|
@@ -17,6 +17,6 @@ export const LIFI_API_URL = 'https://li.quest/v1';
|
|
|
17
17
|
* @public
|
|
18
18
|
*/
|
|
19
19
|
export async function getGasRecommendation(client, params, options) {
|
|
20
|
-
return request(client.config, `${LIFI_API_URL}/gas/suggestion/${params.chainId}`, {}, options);
|
|
20
|
+
return request(client.config, `${LIFI_API_URL}/gas/suggestion/${params.chainId}`, { perpsSdkHeader: false }, options);
|
|
21
21
|
}
|
|
22
22
|
//# sourceMappingURL=getGasRecommendation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getGasRecommendation.js","sourceRoot":"","sources":["../../../src/services/getGasRecommendation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAIjD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAA;AAYjD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAsB,EACtB,MAAkC,EAClC,OAA2B;IAE3B,OAAO,OAAO,CACZ,MAAM,CAAC,MAAM,EACb,GAAG,YAAY,mBAAmB,MAAM,CAAC,OAAO,EAAE,EAClD,EAAE,
|
|
1
|
+
{"version":3,"file":"getGasRecommendation.js","sourceRoot":"","sources":["../../../src/services/getGasRecommendation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAIjD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAA;AAYjD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAsB,EACtB,MAAkC,EAClC,OAA2B;IAE3B,OAAO,OAAO,CACZ,MAAM,CAAC,MAAM,EACb,GAAG,YAAY,mBAAmB,MAAM,CAAC,OAAO,EAAE,EAClD,EAAE,cAAc,EAAE,KAAK,EAAE,EACzB,OAAO,CACR,CAAA;AACH,CAAC"}
|
|
@@ -13,7 +13,7 @@ export interface GetQuoteParams {
|
|
|
13
13
|
/** Buy consumes asks; sell consumes bids. */
|
|
14
14
|
side: QuoteSide;
|
|
15
15
|
/** USD notional to fill. */
|
|
16
|
-
size:
|
|
16
|
+
size: string;
|
|
17
17
|
/** Product family used to distinguish spot and perpetual markets. */
|
|
18
18
|
type: TradeType;
|
|
19
19
|
}
|
|
@@ -33,7 +33,7 @@ export interface GetQuoteParams {
|
|
|
33
33
|
* provider: 'hyperliquid',
|
|
34
34
|
* symbol: 'BTC',
|
|
35
35
|
* side: 'buy',
|
|
36
|
-
* size:
|
|
36
|
+
* size: '10000',
|
|
37
37
|
* type: 'perps',
|
|
38
38
|
* })
|
|
39
39
|
* console.log(quote.expectedFillPrice, quote.priceImpactBps)
|
|
@@ -15,7 +15,7 @@ import { requireProvider } from '../client/requireProvider.js';
|
|
|
15
15
|
* provider: 'hyperliquid',
|
|
16
16
|
* symbol: 'BTC',
|
|
17
17
|
* side: 'buy',
|
|
18
|
-
* size:
|
|
18
|
+
* size: '10000',
|
|
19
19
|
* type: 'perps',
|
|
20
20
|
* })
|
|
21
21
|
* console.log(quote.expectedFillPrice, quote.priceImpactBps)
|
|
@@ -29,7 +29,7 @@ export declare const QUOTE_THROTTLE_MS = 100;
|
|
|
29
29
|
* price, a throwing `onQuote`) is logged via `wsLog.listenerFailure` and the
|
|
30
30
|
* emission is skipped, on both the immediate and the trailing-timer path.
|
|
31
31
|
*
|
|
32
|
-
* @throws {PerpsError} `ValidationError` when `params.size`
|
|
32
|
+
* @throws {PerpsError} `ValidationError` when `params.size` does not match the decimal pattern.
|
|
33
33
|
* @throws {PerpsError} `MarketNotFound` when no market matches symbol+type.
|
|
34
34
|
* @internal
|
|
35
35
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveSubscribeQuote.d.ts","sourceRoot":"","sources":["../../../src/services/resolveSubscribeQuote.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EAIb,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"resolveSubscribeQuote.d.ts","sourceRoot":"","sources":["../../../src/services/resolveSubscribeQuote.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EAIb,MAAM,mBAAmB,CAAA;AAI1B,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAIvD;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,MAAM,CAAA;AAEpC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,cAAc,EACtB,QAAQ,EAAE,MAAM,EAChB,EAAE,EAAE,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,EACjC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,MAAM,IAAI,CAAC,CAiGrB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PerpsErrorCode, } from '@lifi/perps-types';
|
|
2
|
+
import { isDecimalString } from '../decimal/parse.js';
|
|
2
3
|
import { PerpsError } from '../errors/PerpsError.js';
|
|
3
4
|
import { buildQuote } from '../math/order.js';
|
|
4
5
|
import { wsLog } from '../websocket/wsLog.js';
|
|
@@ -31,13 +32,13 @@ export const QUOTE_THROTTLE_MS = 100;
|
|
|
31
32
|
* price, a throwing `onQuote`) is logged via `wsLog.listenerFailure` and the
|
|
32
33
|
* emission is skipped, on both the immediate and the trailing-timer path.
|
|
33
34
|
*
|
|
34
|
-
* @throws {PerpsError} `ValidationError` when `params.size`
|
|
35
|
+
* @throws {PerpsError} `ValidationError` when `params.size` does not match the decimal pattern.
|
|
35
36
|
* @throws {PerpsError} `MarketNotFound` when no market matches symbol+type.
|
|
36
37
|
* @internal
|
|
37
38
|
*/
|
|
38
39
|
export async function resolveSubscribeQuote(client, provider, ws, params, feeTier, onQuote) {
|
|
39
|
-
if (!
|
|
40
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Quote size must be a
|
|
40
|
+
if (!isDecimalString(params.size)) {
|
|
41
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, `Quote size must be a decimal string, got '${params.size}'`);
|
|
41
42
|
}
|
|
42
43
|
const market = await resolveQuoteMarket(client, provider, params);
|
|
43
44
|
// REST snapshot seeds the context; the marketContext subscription below
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveSubscribeQuote.js","sourceRoot":"","sources":["../../../src/services/resolveSubscribeQuote.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,cAAc,GACf,MAAM,mBAAmB,CAAA;AAC1B,OAAO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,GAAG,SAAS,CAAA;QACtB,CAAC;QACD,kBAAkB,EAAE,CAAA;QACpB,eAAe,EAAE,CAAA;IACnB,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -8,6 +8,11 @@ import { type RetryPolicy } from './retryPolicy.js';
|
|
|
8
8
|
export interface RequestOptions extends RequestInit {
|
|
9
9
|
/** Per-call retry override. Falls back to the resolved client-level policy. */
|
|
10
10
|
retry?: RetryPolicy | false;
|
|
11
|
+
/**
|
|
12
|
+
* Send `x-lifi-perps-sdk`. Defaults to `true`. The LI.FI API CORS preflight
|
|
13
|
+
* does not allow the header, so a browser blocks a LI.FI API call that sends it.
|
|
14
|
+
*/
|
|
15
|
+
perpsSdkHeader?: boolean;
|
|
11
16
|
/** Successful response metadata, delivered only after JSON decoding. */
|
|
12
17
|
onResponse?: (headers: Headers, requestStartedAt: number, responseReceivedAt: number) => void;
|
|
13
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/transport/request.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAI5E,OAAO,EAGL,KAAK,WAAW,EAEjB,MAAM,kBAAkB,CAAA;AAEzB;;;;GAIG;AACH,MAAM,WAAW,cAAe,SAAQ,WAAW;IACjD,+EAA+E;IAC/E,KAAK,CAAC,EAAE,WAAW,GAAG,KAAK,CAAA;IAC3B,wEAAwE;IACxE,UAAU,CAAC,EAAE,CACX,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,MAAM,EACxB,kBAAkB,EAAE,MAAM,KACvB,IAAI,CAAA;CACV;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,MAAM,EAAE,eAAe,EACvB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,cAAmB,EAC5B,UAAU,CAAC,EAAE,iBAAiB,GAC7B,OAAO,CAAC,CAAC,CAAC,CAgFZ;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC,GAC5D,MAAM,CAWR"}
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/transport/request.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAI5E,OAAO,EAGL,KAAK,WAAW,EAEjB,MAAM,kBAAkB,CAAA;AAEzB;;;;GAIG;AACH,MAAM,WAAW,cAAe,SAAQ,WAAW;IACjD,+EAA+E;IAC/E,KAAK,CAAC,EAAE,WAAW,GAAG,KAAK,CAAA;IAC3B;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,wEAAwE;IACxE,UAAU,CAAC,EAAE,CACX,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,MAAM,EACxB,kBAAkB,EAAE,MAAM,KACvB,IAAI,CAAA;CACV;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,MAAM,EAAE,eAAe,EACvB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,cAAmB,EAC5B,UAAU,CAAC,EAAE,iBAAiB,GAC7B,OAAO,CAAC,CAAC,CAAC,CAgFZ;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC,GAC5D,MAAM,CAWR"}
|
|
@@ -11,13 +11,13 @@ import { LIFI_REQUEST_KEY, LIFI_RETRY_DEFAULTS, resolveRetryPolicy, } from './re
|
|
|
11
11
|
* @internal
|
|
12
12
|
*/
|
|
13
13
|
export async function request(config, url, options = {}, sdkOptions) {
|
|
14
|
-
const { retry, onResponse, ...fetchInit } = options;
|
|
14
|
+
const { retry, onResponse, perpsSdkHeader = true, ...fetchInit } = options;
|
|
15
15
|
const policy = retry !== undefined
|
|
16
16
|
? resolveRetryPolicy(LIFI_RETRY_DEFAULTS, retry, LIFI_REQUEST_KEY)
|
|
17
17
|
: resolveRetryPolicy(LIFI_RETRY_DEFAULTS, config.retry, LIFI_REQUEST_KEY);
|
|
18
18
|
const headers = {
|
|
19
19
|
'Content-Type': 'application/json',
|
|
20
|
-
'x-lifi-perps-sdk': version,
|
|
20
|
+
...(perpsSdkHeader && { 'x-lifi-perps-sdk': version }),
|
|
21
21
|
...fetchInit.headers,
|
|
22
22
|
};
|
|
23
23
|
if (config.apiKey) {
|