@lifi/perps-sdk 23.1.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 +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 +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 -52
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +4 -4
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +42 -25
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/index.d.ts +13 -11
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +64 -8
- 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 +39 -5
- package/dist/esm/decimal/compare.d.ts.map +1 -1
- package/dist/esm/decimal/compare.js +45 -5
- package/dist/esm/decimal/compare.js.map +1 -1
- 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 -70
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +22 -13
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +56 -46
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/index.d.ts +13 -11
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +9 -7
- 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 +39 -5
- package/dist/types/decimal/compare.d.ts.map +1 -1
- 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 +22 -13
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -11
- 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 +60 -10
- package/src/decimal/convert.ts +123 -44
- package/src/decimal/decimalStringToBig.ts +47 -0
- package/src/decimal/format.ts +81 -72
- package/src/decimal/parse.ts +71 -61
- package/src/index.ts +73 -10
- 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 -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,269 @@
|
|
|
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 is not a decimal string 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
|
-
import { MarginMode, PositionMarginAdjustment, } from '@lifi/perps-types';
|
|
11
|
-
import {
|
|
12
|
+
import { MarginMode, PerpsErrorCode, PositionMarginAdjustment, } from '@lifi/perps-types';
|
|
13
|
+
import { DivBig } from '../decimal/big.js';
|
|
14
|
+
import { numberToDecimalString } from '../decimal/convert.js';
|
|
15
|
+
import { bigToDecimalString, decimalStringToBig, decimalStringToDivBig, } from '../decimal/decimalStringToBig.js';
|
|
16
|
+
import { PerpsError } from '../errors/PerpsError.js';
|
|
17
|
+
import { createSafeFunction } from '../utils/createSafeFunction.js';
|
|
12
18
|
/**
|
|
13
|
-
*
|
|
19
|
+
* Notional value of a position: `|size| × price`.
|
|
14
20
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @param price - Current asset price
|
|
17
|
-
* @returns Notional value in USD
|
|
21
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
18
22
|
* @public
|
|
19
23
|
*/
|
|
20
24
|
export function calculateNotionalValue(size, price) {
|
|
21
|
-
|
|
22
|
-
return Number.NaN;
|
|
23
|
-
}
|
|
24
|
-
return new DivBig(size).abs().times(price).toNumber();
|
|
25
|
+
return bigToDecimalString(decimalStringToBig(size).abs().times(decimalStringToBig(price)));
|
|
25
26
|
}
|
|
27
|
+
/** @public */
|
|
28
|
+
export const safeCalculateNotionalValue = createSafeFunction('calculateNotionalValue', calculateNotionalValue);
|
|
26
29
|
/**
|
|
27
|
-
*
|
|
30
|
+
* Unrealized PnL: `(currentPrice - entryPrice) × size`, where `size` is
|
|
31
|
+
* positive for a long and negative for a short.
|
|
28
32
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @param currentPrice - Current market price
|
|
31
|
-
* @param size - Position size (positive for long, negative for short)
|
|
32
|
-
* @returns Unrealized PnL in USD
|
|
33
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
33
34
|
* @public
|
|
34
35
|
*/
|
|
35
36
|
export function calculateUnrealizedPnl(entryPrice, currentPrice, size) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return new DivBig(currentPrice).minus(entryPrice).times(size).toNumber();
|
|
37
|
+
return bigToDecimalString(decimalStringToBig(currentPrice)
|
|
38
|
+
.minus(decimalStringToBig(entryPrice))
|
|
39
|
+
.times(decimalStringToBig(size)));
|
|
40
40
|
}
|
|
41
|
+
/** @public */
|
|
42
|
+
export const safeCalculateUnrealizedPnl = createSafeFunction('calculateUnrealizedPnl', calculateUnrealizedPnl);
|
|
41
43
|
/**
|
|
42
|
-
*
|
|
44
|
+
* Return on equity as a percentage (`'10'` for 10%): `pnl ÷ margin × 100`.
|
|
45
|
+
* A zero margin gives `'0'`.
|
|
43
46
|
*
|
|
44
|
-
* @
|
|
45
|
-
* @param margin - Initial margin in USD
|
|
46
|
-
* @returns ROE as percentage (e.g., 10 for 10%)
|
|
47
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
47
48
|
* @public
|
|
48
49
|
*/
|
|
49
50
|
export function calculateRoe(pnl, margin) {
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
const marginBig = decimalStringToBig(margin);
|
|
52
|
+
const pnlBig = decimalStringToDivBig(pnl);
|
|
53
|
+
if (marginBig.eq(0)) {
|
|
54
|
+
return '0';
|
|
52
55
|
}
|
|
53
|
-
|
|
54
|
-
return Number.NaN;
|
|
55
|
-
}
|
|
56
|
-
return new DivBig(pnl).div(margin).times(100).toNumber();
|
|
56
|
+
return bigToDecimalString(pnlBig.div(marginBig).times(100));
|
|
57
57
|
}
|
|
58
|
+
/** @public */
|
|
59
|
+
export const safeCalculateRoe = createSafeFunction('calculateRoe', calculateRoe);
|
|
58
60
|
/**
|
|
59
|
-
*
|
|
61
|
+
* Required margin: `notionalValue ÷ leverage`.
|
|
60
62
|
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
63
|
-
* @returns Required margin in USD
|
|
63
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string
|
|
64
|
+
* or `leverage` is zero.
|
|
64
65
|
* @public
|
|
65
66
|
*/
|
|
66
67
|
export function calculateRequiredMargin(notionalValue, leverage) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return Number.NaN;
|
|
68
|
+
const notionalBig = decimalStringToDivBig(notionalValue);
|
|
69
|
+
const leverageBig = decimalStringToBig(leverage);
|
|
70
|
+
if (leverageBig.eq(0)) {
|
|
71
|
+
throw new PerpsError(PerpsErrorCode.ValidationError, 'Leverage must not be zero.');
|
|
72
72
|
}
|
|
73
|
-
return
|
|
73
|
+
return bigToDecimalString(notionalBig.div(leverageBig));
|
|
74
74
|
}
|
|
75
|
+
/** @public */
|
|
76
|
+
export const safeCalculateRequiredMargin = createSafeFunction('calculateRequiredMargin', calculateRequiredMargin);
|
|
75
77
|
/**
|
|
76
|
-
* Distance from the current price to the liquidation price, as
|
|
77
|
-
* the current price.
|
|
78
|
+
* Distance from the current price to the liquidation price, as an absolute
|
|
79
|
+
* percentage of the current price. A zero current price gives `'0'`.
|
|
78
80
|
*
|
|
79
|
-
* @
|
|
80
|
-
* @param currentPrice - Current market price
|
|
81
|
-
* @returns Absolute distance as a percentage, or 0 when current price is zero
|
|
81
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
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
|
+
return '0';
|
|
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`.
|
|
96
|
+
* A zero margin gives `'0'`.
|
|
93
97
|
*
|
|
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
|
|
98
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
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
|
+
return '0';
|
|
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
|
-
* @returns
|
|
126
|
-
*
|
|
122
|
+
* (`'0.01'` = 1%).
|
|
123
|
+
* @returns The estimate, or `undefined` for a zero leverage or a zero
|
|
124
|
+
* denominator.
|
|
125
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
127
126
|
* @public
|
|
128
127
|
*/
|
|
129
128
|
export function estimateLiquidationPrice(params) {
|
|
130
|
-
const
|
|
131
|
-
|
|
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) || denominator.eq(0)) {
|
|
132
135
|
return undefined;
|
|
133
136
|
}
|
|
134
|
-
const side = isLong ? 1 : -1;
|
|
135
|
-
const denominator = 1 - maintenanceMarginRate * side;
|
|
136
|
-
if (denominator === 0) {
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
if (!areFinite(entryPrice, leverage, maintenanceMarginRate)) {
|
|
140
|
-
return Number.NaN;
|
|
141
|
-
}
|
|
142
|
-
const mmr = new DivBig(maintenanceMarginRate);
|
|
143
137
|
const marginAvailable = new DivBig(1)
|
|
144
138
|
.div(leverage)
|
|
145
139
|
.minus(mmr)
|
|
146
140
|
.times(entryPrice);
|
|
147
|
-
return
|
|
148
|
-
|
|
149
|
-
|
|
141
|
+
return bigToDecimalString(entryPrice.minus(marginAvailable.times(side).div(denominator)));
|
|
142
|
+
}
|
|
143
|
+
/** @public */
|
|
144
|
+
export const safeEstimateLiquidationPrice = createSafeFunction('estimateLiquidationPrice', estimateLiquidationPrice);
|
|
145
|
+
/**
|
|
146
|
+
* Estimate the liquidation price of a new isolated position from the flat
|
|
147
|
+
* `market.maintenanceMarginRate`.
|
|
148
|
+
*
|
|
149
|
+
* @returns The estimate, or `undefined` when the market carries no
|
|
150
|
+
* `maintenanceMarginRate` or `estimateLiquidationPrice` gives `undefined`.
|
|
151
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
export function estimateLiquidationPriceAtMarketRate(market, params) {
|
|
155
|
+
if (market.maintenanceMarginRate === undefined) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
return estimateLiquidationPrice({
|
|
159
|
+
entryPrice: params.entryPrice,
|
|
160
|
+
leverage: params.leverage,
|
|
161
|
+
isLong: params.isLong,
|
|
162
|
+
maintenanceMarginRate: numberToDecimalString(market.maintenanceMarginRate),
|
|
163
|
+
});
|
|
150
164
|
}
|
|
165
|
+
/** @public */
|
|
166
|
+
export const safeEstimateLiquidationPriceAtMarketRate = createSafeFunction('estimateLiquidationPriceAtMarketRate', estimateLiquidationPriceAtMarketRate);
|
|
151
167
|
/**
|
|
152
168
|
* Whether an isolated position with this liquidation price is already past
|
|
153
169
|
* liquidation at `currentPrice`: a long liquidates when the price falls to its
|
|
154
170
|
* liquidation level, a short when it rises to it. A non-positive price on
|
|
155
171
|
* either side counts as unknown, so the result is `false`.
|
|
156
172
|
*
|
|
173
|
+
* @throws {PerpsError} `ValidationError` when a price is not a decimal string.
|
|
157
174
|
* @public
|
|
158
175
|
*/
|
|
159
176
|
export function wouldImmediatelyLiquidate(params) {
|
|
160
|
-
const
|
|
161
|
-
|
|
177
|
+
const liquidationPrice = decimalStringToBig(params.liquidationPrice);
|
|
178
|
+
const currentPrice = decimalStringToBig(params.currentPrice);
|
|
179
|
+
if (currentPrice.lte(0) || liquidationPrice.lte(0)) {
|
|
162
180
|
return false;
|
|
163
181
|
}
|
|
164
|
-
return isLong
|
|
165
|
-
? liquidationPrice
|
|
166
|
-
: liquidationPrice
|
|
182
|
+
return params.isLong
|
|
183
|
+
? liquidationPrice.gte(currentPrice)
|
|
184
|
+
: liquidationPrice.lte(currentPrice);
|
|
167
185
|
}
|
|
186
|
+
/** @public */
|
|
187
|
+
export const safeWouldImmediatelyLiquidate = createSafeFunction('wouldImmediatelyLiquidate', wouldImmediatelyLiquidate);
|
|
168
188
|
/**
|
|
169
|
-
* Estimated average entry price after adding to an existing position
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* the size of each leg in coin units. Both legs assumed in the same direction
|
|
173
|
-
* (this helper is for adding to, not flipping, a position).
|
|
189
|
+
* Estimated average entry price after adding to an existing position: the
|
|
190
|
+
* size-weighted average of the current entry and the fill price. Both legs
|
|
191
|
+
* are in the same direction (an add, not a flip).
|
|
174
192
|
*
|
|
175
|
-
* @param
|
|
176
|
-
*
|
|
177
|
-
* @
|
|
178
|
-
*
|
|
179
|
-
*
|
|
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).
|
|
193
|
+
* @param fillPrice - Expected fill price (mid for market, limit price for
|
|
194
|
+
* limit orders).
|
|
195
|
+
* @returns The new average entry price, or `undefined` when the combined size
|
|
196
|
+
* is not positive.
|
|
197
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
182
198
|
* @public
|
|
183
199
|
*/
|
|
184
200
|
export function estimateAverageEntryPrice(params) {
|
|
185
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
201
|
+
const currentSize = decimalStringToDivBig(params.currentSize);
|
|
202
|
+
const addSize = decimalStringToBig(params.addSize);
|
|
203
|
+
const currentEntry = decimalStringToBig(params.currentEntry);
|
|
204
|
+
const fillPrice = decimalStringToBig(params.fillPrice);
|
|
205
|
+
const totalSize = currentSize.plus(addSize);
|
|
206
|
+
if (totalSize.lte(0)) {
|
|
188
207
|
return undefined;
|
|
189
208
|
}
|
|
190
|
-
|
|
191
|
-
return undefined;
|
|
192
|
-
}
|
|
193
|
-
if (!areFinite(currentSize, addSize)) {
|
|
194
|
-
return Number.NaN;
|
|
195
|
-
}
|
|
196
|
-
return new DivBig(currentSize)
|
|
209
|
+
return bigToDecimalString(currentSize
|
|
197
210
|
.times(currentEntry)
|
|
198
|
-
.plus(
|
|
199
|
-
.div(
|
|
200
|
-
.toNumber();
|
|
211
|
+
.plus(addSize.times(fillPrice))
|
|
212
|
+
.div(totalSize));
|
|
201
213
|
}
|
|
214
|
+
/** @public */
|
|
215
|
+
export const safeEstimateAverageEntryPrice = createSafeFunction('estimateAverageEntryPrice', estimateAverageEntryPrice);
|
|
202
216
|
/**
|
|
203
|
-
* Estimated effective leverage after adding margin and notional
|
|
217
|
+
* Estimated effective leverage after adding margin and notional:
|
|
218
|
+
* `(currentNotional + addNotional) ÷ (currentMargin + addMargin)`.
|
|
204
219
|
*
|
|
205
|
-
* leverage
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* @returns The new effective leverage, or undefined if total margin is
|
|
210
|
-
* non-positive.
|
|
220
|
+
* @returns The new leverage, or `undefined` when the total margin is not
|
|
221
|
+
* positive.
|
|
222
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
211
223
|
* @public
|
|
212
224
|
*/
|
|
213
225
|
export function estimateNewLeverage(params) {
|
|
214
|
-
const
|
|
215
|
-
const totalMargin = currentMargin
|
|
216
|
-
if (totalMargin
|
|
226
|
+
const totalNotional = decimalStringToDivBig(params.currentNotional).plus(decimalStringToBig(params.addNotional));
|
|
227
|
+
const totalMargin = decimalStringToBig(params.currentMargin).plus(decimalStringToBig(params.addMargin));
|
|
228
|
+
if (totalMargin.lte(0)) {
|
|
217
229
|
return undefined;
|
|
218
230
|
}
|
|
219
|
-
|
|
220
|
-
return Number.NaN;
|
|
221
|
-
}
|
|
222
|
-
return new DivBig(currentNotional)
|
|
223
|
-
.plus(addNotional)
|
|
224
|
-
.div(new DivBig(currentMargin).plus(addMargin))
|
|
225
|
-
.toNumber();
|
|
231
|
+
return bigToDecimalString(totalNotional.div(totalMargin));
|
|
226
232
|
}
|
|
233
|
+
/** @public */
|
|
234
|
+
export const safeEstimateNewLeverage = createSafeFunction('estimateNewLeverage', estimateNewLeverage);
|
|
227
235
|
/**
|
|
228
|
-
* Estimated unrealised PnL at the
|
|
229
|
-
*
|
|
230
|
-
* `pnl = (markPrice - entryPrice) * size * (isLong ? 1 : -1)`
|
|
236
|
+
* Estimated unrealised PnL at the mark price:
|
|
237
|
+
* `(markPrice - entryPrice) × size × (isLong ? 1 : -1)`.
|
|
231
238
|
*
|
|
232
239
|
* @param size - Position size as a non-negative magnitude.
|
|
240
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
233
241
|
* @public
|
|
234
242
|
*/
|
|
235
243
|
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();
|
|
244
|
+
return bigToDecimalString(decimalStringToBig(params.markPrice)
|
|
245
|
+
.minus(decimalStringToBig(params.entryPrice))
|
|
246
|
+
.times(decimalStringToBig(params.size))
|
|
247
|
+
.times(params.isLong ? 1 : -1));
|
|
245
248
|
}
|
|
249
|
+
/** @public */
|
|
250
|
+
export const safeEstimateUnrealizedPnl = createSafeFunction('estimateUnrealizedPnl', estimateUnrealizedPnl);
|
|
246
251
|
/**
|
|
247
|
-
* Realised PnL on the portion of a position being closed
|
|
248
|
-
*
|
|
249
|
-
* `rPnl = (closePrice - entryPrice) * closeSize * (isLong ? 1 : -1)`
|
|
252
|
+
* Realised PnL on the portion of a position being closed:
|
|
253
|
+
* `(closePrice - entryPrice) × closeSize × (isLong ? 1 : -1)`.
|
|
250
254
|
*
|
|
251
255
|
* @param closeSize - Size being closed as a non-negative magnitude.
|
|
256
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
252
257
|
* @public
|
|
253
258
|
*/
|
|
254
259
|
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();
|
|
260
|
+
return bigToDecimalString(decimalStringToBig(params.closePrice)
|
|
261
|
+
.minus(decimalStringToBig(params.entryPrice))
|
|
262
|
+
.times(decimalStringToBig(params.closeSize))
|
|
263
|
+
.times(params.isLong ? 1 : -1));
|
|
264
264
|
}
|
|
265
|
+
/** @public */
|
|
266
|
+
export const safeCalculateRealizedPnl = createSafeFunction('calculateRealizedPnl', calculateRealizedPnl);
|
|
265
267
|
/**
|
|
266
268
|
* Whether this position can take a margin adjustment at all: it holds margin of
|
|
267
269
|
* 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,UAAU,EACV,cAAc,EAGd,wBAAwB,GACzB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AAC7D,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,kCAAkC,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAEnE;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAY,EAAE,KAAa;IAChE,OAAO,kBAAkB,CACvB,kBAAkB,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAChE,CAAA;AACH,CAAC;AAED,cAAc;AACd,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAC1D,wBAAwB,EACxB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AK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACjC,CAAA;AACH,CAAC;AAED,cAAc;AACd,MAAM,CAAC,MAAM,yBAAyB,GAAG,kBAAkB,CACzD,uBAAuB,EACvB,qBAAqB,CACtB,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAKpC;IACC,OAAO,kBAAkB,CACvB,kBAAkB,CAAC,MAAM,CAAC,UAAU,CAAC;SAClC,KAAK,CAAC,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` is not
|
|
32
|
+
* @throws {PerpsError} `ValidationError` when `params.size` is not a decimal string.
|
|
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` is not
|
|
35
|
+
* @throws {PerpsError} `ValidationError` when `params.size` is not a decimal string.
|
|
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,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAO7C,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAEzE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,CAAA;AAEpC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,MAAsB,EACtB,QAAgB,EAChB,EAAiC,EACjC,MAA8B,EAC9B,OAAgB,EAChB,OAAsB;IAEtB,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"resolveSubscribeQuote.js","sourceRoot":"","sources":["../../../src/services/resolveSubscribeQuote.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,cAAc,GACf,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAO7C,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAEzE;;;;;;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CAAA;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,kBAAkB,EAAE,CAAA;QACpB,MAAM,KAAK,CAAA;IACb,CAAC;IAED,IAAI,QAAQ,GAAG,KAAK,CAAA;IACpB,OAAO,GAAG,EAAE;QACV,IAAI,QAAQ,EAAE,CAAC;YACb,OAAM;QACR,CAAC;QACD,QAAQ,GAAG,IAAI,CAAA;QACf,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,YAAY,CAAC,QAAQ,CAAC,CAAA;YACtB,QAAQ,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,GA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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) {
|