@lifi/perps-sdk 23.0.0 → 24.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -45
- package/dist/cjs/client/PerpsClient.d.ts +3 -2
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +16 -9
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/decimal/arithmetic.d.ts +13 -0
- package/dist/cjs/decimal/arithmetic.d.ts.map +1 -0
- package/dist/cjs/decimal/arithmetic.js +48 -0
- package/dist/cjs/decimal/arithmetic.js.map +1 -0
- package/dist/cjs/decimal/big.d.ts +0 -1
- package/dist/cjs/decimal/big.d.ts.map +1 -1
- package/dist/cjs/decimal/big.js +0 -4
- package/dist/cjs/decimal/big.js.map +1 -1
- package/dist/cjs/decimal/compare.d.ts +7 -0
- package/dist/cjs/decimal/compare.d.ts.map +1 -0
- package/dist/cjs/decimal/compare.js +21 -0
- package/dist/cjs/decimal/compare.js.map +1 -0
- package/dist/cjs/decimal/convert.d.ts +12 -6
- package/dist/cjs/decimal/convert.d.ts.map +1 -1
- package/dist/cjs/decimal/convert.js +36 -14
- package/dist/cjs/decimal/convert.js.map +1 -1
- package/dist/cjs/decimal/decimalStringToBig.d.ts +5 -0
- package/dist/cjs/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/cjs/decimal/decimalStringToBig.js +27 -0
- package/dist/cjs/decimal/decimalStringToBig.js.map +1 -0
- package/dist/cjs/decimal/format.d.ts.map +1 -1
- package/dist/cjs/decimal/format.js +70 -49
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +4 -3
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +44 -19
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/index.d.ts +13 -10
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +66 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +1 -1
- package/dist/cjs/math/fills.d.ts.map +1 -1
- package/dist/cjs/math/fills.js +28 -13
- package/dist/cjs/math/fills.js.map +1 -1
- package/dist/cjs/math/order.d.ts +27 -16
- package/dist/cjs/math/order.d.ts.map +1 -1
- package/dist/cjs/math/order.js +106 -122
- package/dist/cjs/math/order.js.map +1 -1
- package/dist/cjs/math/position.d.ts +85 -35
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +88 -91
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/services/getGasRecommendation.js +1 -1
- package/dist/cjs/services/getGasRecommendation.js.map +1 -1
- package/dist/cjs/services/getQuote.d.ts +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.js +3 -2
- package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/cjs/transport/request.d.ts +1 -0
- package/dist/cjs/transport/request.d.ts.map +1 -1
- package/dist/cjs/transport/request.js +2 -2
- package/dist/cjs/transport/request.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -0
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +9 -5
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/types/withdrawal.d.ts +12 -4
- package/dist/cjs/types/withdrawal.d.ts.map +1 -1
- package/dist/cjs/utils/createSafeFunction.d.ts +2 -0
- package/dist/cjs/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/cjs/utils/createSafeFunction.js +15 -0
- package/dist/cjs/utils/createSafeFunction.js.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts +3 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.js +16 -0
- package/dist/cjs/utils/timestampToIsoString.js.map +1 -0
- package/dist/cjs/utils/warnOnce.d.ts +2 -0
- package/dist/cjs/utils/warnOnce.d.ts.map +1 -1
- package/dist/cjs/utils/warnOnce.js +13 -1
- package/dist/cjs/utils/warnOnce.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/websocket/PerpsWsClient.d.ts +2 -0
- package/dist/cjs/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/cjs/websocket/PerpsWsClient.js +14 -1
- package/dist/cjs/websocket/PerpsWsClient.js.map +1 -1
- package/dist/cjs/websocket/wsLog.d.ts +2 -0
- package/dist/cjs/websocket/wsLog.d.ts.map +1 -1
- package/dist/cjs/websocket/wsLog.js +8 -0
- package/dist/cjs/websocket/wsLog.js.map +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +6 -9
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/cjs/wire/refuel.d.ts +4 -1
- package/dist/cjs/wire/refuel.d.ts.map +1 -1
- package/dist/cjs/wire/refuel.js +23 -6
- package/dist/cjs/wire/refuel.js.map +1 -1
- package/dist/cjs/wire/transferable.js +3 -3
- package/dist/cjs/wire/transferable.js.map +1 -1
- package/dist/cjs/wire/withdraw.d.ts.map +1 -1
- package/dist/cjs/wire/withdraw.js +3 -3
- package/dist/cjs/wire/withdraw.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +17 -4
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +30 -8
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/decimal/arithmetic.d.ts +84 -0
- package/dist/esm/decimal/arithmetic.d.ts.map +1 -0
- package/dist/esm/decimal/arithmetic.js +110 -0
- package/dist/esm/decimal/arithmetic.js.map +1 -0
- package/dist/esm/decimal/big.d.ts +0 -2
- package/dist/esm/decimal/big.d.ts.map +1 -1
- package/dist/esm/decimal/big.js +0 -4
- package/dist/esm/decimal/big.js.map +1 -1
- package/dist/esm/decimal/compare.d.ts +45 -0
- package/dist/esm/decimal/compare.d.ts.map +1 -0
- package/dist/esm/decimal/compare.js +53 -0
- package/dist/esm/decimal/compare.js.map +1 -0
- package/dist/esm/decimal/convert.d.ts +62 -26
- package/dist/esm/decimal/convert.d.ts.map +1 -1
- package/dist/esm/decimal/convert.js +76 -25
- package/dist/esm/decimal/convert.js.map +1 -1
- package/dist/esm/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/esm/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/esm/decimal/decimalStringToBig.js +40 -0
- package/dist/esm/decimal/decimalStringToBig.js.map +1 -0
- package/dist/esm/decimal/format.d.ts +4 -1
- package/dist/esm/decimal/format.d.ts.map +1 -1
- package/dist/esm/decimal/format.js +71 -66
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +26 -7
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +59 -33
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/index.d.ts +13 -10
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +9 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +6 -3
- package/dist/esm/math/fills.d.ts.map +1 -1
- package/dist/esm/math/fills.js +31 -16
- package/dist/esm/math/fills.js.map +1 -1
- package/dist/esm/math/order.d.ts +85 -65
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +158 -166
- package/dist/esm/math/order.js.map +1 -1
- package/dist/esm/math/position.d.ts +155 -98
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +157 -155
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/services/getGasRecommendation.js +1 -1
- package/dist/esm/services/getGasRecommendation.js.map +1 -1
- package/dist/esm/services/getQuote.d.ts +2 -2
- package/dist/esm/services/getQuote.js +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js +4 -3
- package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/esm/transport/request.d.ts +5 -0
- package/dist/esm/transport/request.d.ts.map +1 -1
- package/dist/esm/transport/request.js +2 -2
- package/dist/esm/transport/request.js.map +1 -1
- package/dist/esm/types/api.d.ts +9 -0
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +26 -13
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/types/withdrawal.d.ts +32 -7
- package/dist/esm/types/withdrawal.d.ts.map +1 -1
- package/dist/esm/utils/createSafeFunction.d.ts +9 -0
- package/dist/esm/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/esm/utils/createSafeFunction.js +19 -0
- package/dist/esm/utils/createSafeFunction.js.map +1 -0
- package/dist/esm/utils/timestampToIsoString.d.ts +15 -0
- package/dist/esm/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/esm/utils/timestampToIsoString.js +24 -0
- package/dist/esm/utils/timestampToIsoString.js.map +1 -0
- package/dist/esm/utils/warnOnce.d.ts +16 -0
- package/dist/esm/utils/warnOnce.d.ts.map +1 -1
- package/dist/esm/utils/warnOnce.js +24 -0
- package/dist/esm/utils/warnOnce.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/esm/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/esm/websocket/PerpsWsClient.js +20 -2
- package/dist/esm/websocket/PerpsWsClient.js.map +1 -1
- package/dist/esm/websocket/wsLog.d.ts +12 -0
- package/dist/esm/websocket/wsLog.d.ts.map +1 -1
- package/dist/esm/websocket/wsLog.js +18 -0
- package/dist/esm/websocket/wsLog.js.map +1 -1
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +2 -2
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/esm/wire/refuel.d.ts +24 -14
- package/dist/esm/wire/refuel.d.ts.map +1 -1
- package/dist/esm/wire/refuel.js +38 -17
- package/dist/esm/wire/refuel.js.map +1 -1
- package/dist/esm/wire/transferable.js +3 -3
- package/dist/esm/wire/transferable.js.map +1 -1
- package/dist/esm/wire/withdraw.d.ts.map +1 -1
- package/dist/esm/wire/withdraw.js +3 -3
- package/dist/esm/wire/withdraw.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +17 -4
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/decimal/arithmetic.d.ts +84 -0
- package/dist/types/decimal/arithmetic.d.ts.map +1 -0
- package/dist/types/decimal/big.d.ts +0 -2
- package/dist/types/decimal/big.d.ts.map +1 -1
- package/dist/types/decimal/compare.d.ts +45 -0
- package/dist/types/decimal/compare.d.ts.map +1 -0
- package/dist/types/decimal/convert.d.ts +62 -26
- package/dist/types/decimal/convert.d.ts.map +1 -1
- package/dist/types/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/types/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/types/decimal/format.d.ts +4 -1
- package/dist/types/decimal/format.d.ts.map +1 -1
- package/dist/types/decimal/parse.d.ts +26 -7
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +6 -3
- package/dist/types/math/fills.d.ts.map +1 -1
- package/dist/types/math/order.d.ts +85 -65
- package/dist/types/math/order.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +155 -98
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/services/getQuote.d.ts +2 -2
- package/dist/types/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/types/transport/request.d.ts +5 -0
- package/dist/types/transport/request.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +9 -0
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +26 -13
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/types/withdrawal.d.ts +32 -7
- package/dist/types/types/withdrawal.d.ts.map +1 -1
- package/dist/types/utils/createSafeFunction.d.ts +9 -0
- package/dist/types/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/types/utils/timestampToIsoString.d.ts +15 -0
- package/dist/types/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/types/utils/warnOnce.d.ts +16 -0
- package/dist/types/utils/warnOnce.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/types/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/types/websocket/wsLog.d.ts +12 -0
- package/dist/types/websocket/wsLog.d.ts.map +1 -1
- package/dist/types/wire/orderAmounts.d.ts.map +1 -1
- package/dist/types/wire/refuel.d.ts +24 -14
- package/dist/types/wire/refuel.d.ts.map +1 -1
- package/dist/types/wire/withdraw.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +48 -10
- package/src/decimal/arithmetic.ts +144 -0
- package/src/decimal/big.ts +0 -5
- package/src/decimal/compare.ts +67 -0
- package/src/decimal/convert.ts +123 -44
- package/src/decimal/decimalStringToBig.ts +47 -0
- package/src/decimal/format.ts +81 -68
- package/src/decimal/parse.ts +73 -36
- package/src/index.ts +76 -8
- package/src/math/fills.ts +32 -17
- package/src/math/order.ts +261 -216
- package/src/math/position.ts +277 -194
- package/src/services/getGasRecommendation.ts +1 -1
- package/src/services/getQuote.ts +2 -2
- package/src/services/resolveSubscribeQuote.ts +4 -3
- package/src/transport/request.ts +7 -2
- package/src/types/api.ts +10 -0
- package/src/types/provider.ts +35 -13
- package/src/types/withdrawal.ts +35 -8
- package/src/utils/createSafeFunction.ts +20 -0
- package/src/utils/timestampToIsoString.ts +31 -0
- package/src/utils/warnOnce.ts +44 -0
- package/src/version.ts +1 -1
- package/src/websocket/PerpsWsClient.ts +25 -2
- package/src/websocket/wsLog.ts +31 -0
- package/src/wire/orderAmounts.ts +3 -2
- package/src/wire/refuel.ts +56 -20
- package/src/wire/transferable.ts +3 -3
- package/src/wire/withdraw.ts +3 -5
- package/dist/cjs/decimal/requireDecimal.d.ts +0 -4
- package/dist/cjs/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/cjs/decimal/requireDecimal.js +0 -17
- package/dist/cjs/decimal/requireDecimal.js.map +0 -1
- package/dist/esm/decimal/requireDecimal.d.ts +0 -11
- package/dist/esm/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/esm/decimal/requireDecimal.js +0 -18
- package/dist/esm/decimal/requireDecimal.js.map +0 -1
- package/dist/types/decimal/requireDecimal.d.ts +0 -11
- package/dist/types/decimal/requireDecimal.d.ts.map +0 -1
- package/src/decimal/requireDecimal.ts +0 -21
package/src/transport/request.ts
CHANGED
|
@@ -20,6 +20,11 @@ import {
|
|
|
20
20
|
export interface RequestOptions extends RequestInit {
|
|
21
21
|
/** Per-call retry override. Falls back to the resolved client-level policy. */
|
|
22
22
|
retry?: RetryPolicy | false
|
|
23
|
+
/**
|
|
24
|
+
* Send `x-lifi-perps-sdk`. Defaults to `true`. The LI.FI API CORS preflight
|
|
25
|
+
* does not allow the header, so a browser blocks a LI.FI API call that sends it.
|
|
26
|
+
*/
|
|
27
|
+
perpsSdkHeader?: boolean
|
|
23
28
|
/** Successful response metadata, delivered only after JSON decoding. */
|
|
24
29
|
onResponse?: (
|
|
25
30
|
headers: Headers,
|
|
@@ -40,7 +45,7 @@ export async function request<T>(
|
|
|
40
45
|
options: RequestOptions = {},
|
|
41
46
|
sdkOptions?: SDKRequestOptions
|
|
42
47
|
): Promise<T> {
|
|
43
|
-
const { retry, onResponse, ...fetchInit } = options
|
|
48
|
+
const { retry, onResponse, perpsSdkHeader = true, ...fetchInit } = options
|
|
44
49
|
const policy =
|
|
45
50
|
retry !== undefined
|
|
46
51
|
? resolveRetryPolicy(LIFI_RETRY_DEFAULTS, retry, LIFI_REQUEST_KEY)
|
|
@@ -48,7 +53,7 @@ export async function request<T>(
|
|
|
48
53
|
|
|
49
54
|
const headers: Record<string, string> = {
|
|
50
55
|
'Content-Type': 'application/json',
|
|
51
|
-
'x-lifi-perps-sdk': version,
|
|
56
|
+
...(perpsSdkHeader && { 'x-lifi-perps-sdk': version }),
|
|
52
57
|
...(fetchInit.headers as Record<string, string>),
|
|
53
58
|
}
|
|
54
59
|
|
package/src/types/api.ts
CHANGED
|
@@ -293,6 +293,16 @@ export interface GetWithdrawableBalancesParams {
|
|
|
293
293
|
address: Address
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Parameters for {@link PerpsClient.getWithdrawalTypes}.
|
|
298
|
+
*
|
|
299
|
+
* @public
|
|
300
|
+
*/
|
|
301
|
+
export interface GetWithdrawalTypesParams {
|
|
302
|
+
provider: string
|
|
303
|
+
address: Address
|
|
304
|
+
}
|
|
305
|
+
|
|
296
306
|
/**
|
|
297
307
|
* Parameters for {@link PerpsClient.getPortfolioHistory}.
|
|
298
308
|
*
|
package/src/types/provider.ts
CHANGED
|
@@ -37,7 +37,11 @@ import type {
|
|
|
37
37
|
SDKRequestOptions,
|
|
38
38
|
} from './config.js'
|
|
39
39
|
import type { DepositFlow } from './deposit.js'
|
|
40
|
-
import type {
|
|
40
|
+
import type {
|
|
41
|
+
ProviderWithdrawableBalance,
|
|
42
|
+
WithdrawalSourceTypes,
|
|
43
|
+
WithdrawFlow,
|
|
44
|
+
} from './withdrawal.js'
|
|
41
45
|
|
|
42
46
|
/**
|
|
43
47
|
* Low-level SDK client: resolved config, the optional end-user wallet, and the
|
|
@@ -147,8 +151,8 @@ export interface ActionSignerContribution {
|
|
|
147
151
|
*/
|
|
148
152
|
export interface LiquidationEstimateParams {
|
|
149
153
|
/** Entry price in quote currency per base unit. */
|
|
150
|
-
entryPrice:
|
|
151
|
-
leverage:
|
|
154
|
+
entryPrice: string
|
|
155
|
+
leverage: string
|
|
152
156
|
/** `true` for long, `false` for short. */
|
|
153
157
|
isLong: boolean
|
|
154
158
|
}
|
|
@@ -199,6 +203,15 @@ export interface ProviderGetWithdrawableBalancesParams {
|
|
|
199
203
|
address: Address
|
|
200
204
|
}
|
|
201
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Read params for {@link PerpsProviderPlugin.getWithdrawalTypes}.
|
|
208
|
+
*
|
|
209
|
+
* @public
|
|
210
|
+
*/
|
|
211
|
+
export interface ProviderGetWithdrawalTypesParams {
|
|
212
|
+
address: Address
|
|
213
|
+
}
|
|
214
|
+
|
|
202
215
|
/**
|
|
203
216
|
* Read params for {@link PerpsProvider.getPositions}.
|
|
204
217
|
*
|
|
@@ -322,7 +335,7 @@ export interface ProviderGetQuoteParams {
|
|
|
322
335
|
/** Trade direction used to choose asks for buys or bids for sells. */
|
|
323
336
|
side: QuoteSide
|
|
324
337
|
/** USD notional to fill. */
|
|
325
|
-
size:
|
|
338
|
+
size: string
|
|
326
339
|
/** Product family used to disambiguate spot and perpetual markets. */
|
|
327
340
|
type: TradeType
|
|
328
341
|
}
|
|
@@ -434,20 +447,33 @@ export interface PerpsProviderPlugin {
|
|
|
434
447
|
): Promise<WithdrawFlow>
|
|
435
448
|
|
|
436
449
|
/**
|
|
437
|
-
* The `(asset,
|
|
450
|
+
* The `(asset, category)` pairs `params.address` currently has something to
|
|
438
451
|
* withdraw from, keyed by provider-native asset id. Only the venue knows how
|
|
439
|
-
* its balance payload splits across
|
|
452
|
+
* its balance payload splits across categories, so the split is owned here; the
|
|
440
453
|
* per-asset venue minimum is applied by `PerpsClient.getWithdrawableBalances`,
|
|
441
454
|
* which holds the core asset registry.
|
|
442
455
|
*
|
|
443
|
-
* Optional: a provider whose withdrawals are not a per-
|
|
444
|
-
* it, and `PerpsClient.getWithdrawableBalances` then resolves
|
|
456
|
+
* Optional: a provider whose withdrawals are not a per-category selection
|
|
457
|
+
* omits it, and `PerpsClient.getWithdrawableBalances` then resolves
|
|
458
|
+
* `undefined`.
|
|
445
459
|
*/
|
|
446
460
|
getWithdrawableBalances?(
|
|
447
461
|
params: ProviderGetWithdrawableBalancesParams,
|
|
448
462
|
options?: SDKRequestOptions
|
|
449
463
|
): Promise<ProviderWithdrawableBalance[]>
|
|
450
464
|
|
|
465
|
+
/**
|
|
466
|
+
* The withdrawal types `params.address` can choose per withdrawable holding,
|
|
467
|
+
* each with its cap and the `withdrawalOptions` to send with it.
|
|
468
|
+
*
|
|
469
|
+
* Optional: a provider with one withdrawal type omits it, and
|
|
470
|
+
* `PerpsClient.getWithdrawalTypes` then resolves `undefined`.
|
|
471
|
+
*/
|
|
472
|
+
getWithdrawalTypes?(
|
|
473
|
+
params: ProviderGetWithdrawalTypesParams,
|
|
474
|
+
options?: SDKRequestOptions
|
|
475
|
+
): Promise<WithdrawalSourceTypes[]>
|
|
476
|
+
|
|
451
477
|
getPositions(
|
|
452
478
|
params: ProviderGetPositionsParams,
|
|
453
479
|
options?: SDKRequestOptions
|
|
@@ -573,10 +599,6 @@ export interface PerpsProviderPlugin {
|
|
|
573
599
|
* positions, prefer `Position.liquidationPrice` from the venue. Pure —
|
|
574
600
|
* does no I/O.
|
|
575
601
|
*
|
|
576
|
-
* Numbers, in and out, on purpose: this is a display-tier estimate that
|
|
577
|
-
* feeds a screen, not a wire amount. It is not a `snap*` method and must
|
|
578
|
-
* not be converted to {@link DecimalString}.
|
|
579
|
-
*
|
|
580
602
|
* @returns The estimated liquidation price, or `undefined` when the venue's
|
|
581
603
|
* model cannot be evaluated client-side (degenerate inputs, or `market`
|
|
582
604
|
* lacks the margin metadata the model needs).
|
|
@@ -584,7 +606,7 @@ export interface PerpsProviderPlugin {
|
|
|
584
606
|
estimateLiquidationPrice(
|
|
585
607
|
market: PerpsMarket,
|
|
586
608
|
params: LiquidationEstimateParams
|
|
587
|
-
):
|
|
609
|
+
): string | undefined
|
|
588
610
|
|
|
589
611
|
/**
|
|
590
612
|
* Margin that can be removed from `position` under the venue margin rules,
|
package/src/types/withdrawal.ts
CHANGED
|
@@ -2,22 +2,22 @@ import type {
|
|
|
2
2
|
ActionType,
|
|
3
3
|
Asset,
|
|
4
4
|
DecimalString,
|
|
5
|
-
|
|
5
|
+
WithdrawalAssetRef,
|
|
6
|
+
WithdrawalType,
|
|
6
7
|
} from '@lifi/perps-types'
|
|
7
8
|
import type { Address } from 'viem'
|
|
8
9
|
|
|
9
|
-
export type { WithdrawalRoute }
|
|
10
|
-
|
|
11
10
|
/**
|
|
12
|
-
* One withdrawable `(asset,
|
|
13
|
-
* by provider-native asset id and awaiting the core asset-metadata join.
|
|
11
|
+
* One withdrawable `(asset, category)` selection as the provider reports it,
|
|
12
|
+
* keyed by provider-native asset id and awaiting the core asset-metadata join.
|
|
14
13
|
*
|
|
15
14
|
* @public
|
|
16
15
|
*/
|
|
17
16
|
export interface ProviderWithdrawableBalance {
|
|
18
17
|
/** Provider-native `Asset.id`; never a display symbol. */
|
|
19
18
|
assetId: string
|
|
20
|
-
|
|
19
|
+
/** The provider category the row draws from; the same value as `Balance.categoryId`. */
|
|
20
|
+
categoryId: string
|
|
21
21
|
/** Withdrawable amount in the asset's own units. Always greater than zero. */
|
|
22
22
|
available: DecimalString
|
|
23
23
|
/**
|
|
@@ -41,7 +41,7 @@ export interface ProviderWithdrawableBalance {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
* One withdrawable `(asset,
|
|
44
|
+
* One withdrawable `(asset, category)` selection a caller can act on: the amount
|
|
45
45
|
* clears the asset's venue minimum, and `asset` carries the precision and
|
|
46
46
|
* minimum needed to scale and validate the withdrawal.
|
|
47
47
|
*
|
|
@@ -49,7 +49,8 @@ export interface ProviderWithdrawableBalance {
|
|
|
49
49
|
*/
|
|
50
50
|
export interface WithdrawableBalance {
|
|
51
51
|
asset: Asset
|
|
52
|
-
|
|
52
|
+
/** The provider category the row draws from; the same value as `Balance.categoryId`. */
|
|
53
|
+
categoryId: string
|
|
53
54
|
/** Withdrawable amount in the asset's own units. */
|
|
54
55
|
available: DecimalString
|
|
55
56
|
/**
|
|
@@ -77,6 +78,32 @@ export interface WithdrawableBalance {
|
|
|
77
78
|
isFeeDeducted?: boolean
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
/**
|
|
82
|
+
* One way a venue can settle a withdrawal from a holding.
|
|
83
|
+
*
|
|
84
|
+
* @public
|
|
85
|
+
*/
|
|
86
|
+
export interface WithdrawalTypeOption {
|
|
87
|
+
type: WithdrawalType
|
|
88
|
+
/** Largest `amount` this type accepts from the holding, in the asset's own units. */
|
|
89
|
+
max: DecimalString
|
|
90
|
+
/**
|
|
91
|
+
* Provider-specific options to pass unchanged as
|
|
92
|
+
* `WithdrawalParams.withdrawalOptions` when the user picks this type.
|
|
93
|
+
*/
|
|
94
|
+
withdrawalOptions: Record<string, unknown>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The withdrawal types a venue offers for one holding.
|
|
99
|
+
*
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export interface WithdrawalSourceTypes {
|
|
103
|
+
source: WithdrawalAssetRef
|
|
104
|
+
options: WithdrawalTypeOption[]
|
|
105
|
+
}
|
|
106
|
+
|
|
80
107
|
/**
|
|
81
108
|
* The venue accepts a withdrawal to `destination` now.
|
|
82
109
|
*
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap a throwing function so that it gives `undefined` in place of a throw.
|
|
3
|
+
* The wrapper logs a warning with `name` and the error, which carries the
|
|
4
|
+
* stack trace.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export function createSafeFunction<Args extends unknown[], Result>(
|
|
9
|
+
name: string,
|
|
10
|
+
throwingFunction: (...args: Args) => Result
|
|
11
|
+
): (...args: Args) => Result | undefined {
|
|
12
|
+
return (...args) => {
|
|
13
|
+
try {
|
|
14
|
+
return throwingFunction(...args)
|
|
15
|
+
} catch (error) {
|
|
16
|
+
console.warn(`[perps-sdk] ${name} failed`, error)
|
|
17
|
+
return undefined
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { PerpsErrorCode } from '@lifi/perps-types'
|
|
2
|
+
import { PerpsError } from '../errors/PerpsError.js'
|
|
3
|
+
import { createSafeFunction } from './createSafeFunction.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Read a venue time (epoch milliseconds or a date string) as an ISO-8601
|
|
7
|
+
* string.
|
|
8
|
+
*
|
|
9
|
+
* @throws {PerpsError} `ValidationError` when `Date` cannot read `value`.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export function timestampToIsoString(value: string | number): string {
|
|
13
|
+
const time = value === '' ? Number.NaN : new Date(value).getTime()
|
|
14
|
+
if (Number.isNaN(time)) {
|
|
15
|
+
throw new PerpsError(
|
|
16
|
+
PerpsErrorCode.ValidationError,
|
|
17
|
+
`'${String(value)}' is not a valid timestamp.`
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
return new Date(time).toISOString()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* {@link timestampToIsoString}, or `undefined` with a warning when it throws.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export const safeTimestampToIsoString = createSafeFunction(
|
|
29
|
+
'timestampToIsoString',
|
|
30
|
+
timestampToIsoString
|
|
31
|
+
)
|
package/src/utils/warnOnce.ts
CHANGED
|
@@ -27,3 +27,47 @@ export const createWarnOnce = (): ((key: string, message: string) => void) => {
|
|
|
27
27
|
console.warn(message)
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
const MAX_VALUE_CHARS = 64
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Describe a skipped venue row. The venue value is cut to a fixed length, so
|
|
35
|
+
* a warner key built from the text stays small.
|
|
36
|
+
*
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export const skippedRowMessage = (
|
|
40
|
+
source: string,
|
|
41
|
+
row: string,
|
|
42
|
+
field: string,
|
|
43
|
+
value: unknown,
|
|
44
|
+
expected: string
|
|
45
|
+
): string => {
|
|
46
|
+
const text = String(value)
|
|
47
|
+
const shown =
|
|
48
|
+
text.length > MAX_VALUE_CHARS ? `${text.slice(0, MAX_VALUE_CHARS)}…` : text
|
|
49
|
+
return `[${source}] skipping ${row} row: \`${field}\` is not a valid ${expected}: '${shown}'`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const warnSkippedRowOnce = createWarnOnce()
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Warn once for each provider, row, field and expected kind that a provider
|
|
56
|
+
* mapper skipped a venue row because a required field is not valid. The key
|
|
57
|
+
* holds no venue value, so a second bad value for the same field stays silent.
|
|
58
|
+
*
|
|
59
|
+
* @param expected - What the field must be. Defaults to `'decimal'`.
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
export const warnSkippedVenueRow = (
|
|
63
|
+
provider: string,
|
|
64
|
+
row: string,
|
|
65
|
+
field: string,
|
|
66
|
+
value: unknown,
|
|
67
|
+
expected = 'decimal'
|
|
68
|
+
): void => {
|
|
69
|
+
warnSkippedRowOnce(
|
|
70
|
+
`${provider}|${row}|${field}|${expected}`,
|
|
71
|
+
skippedRowMessage(provider, row, field, value, expected)
|
|
72
|
+
)
|
|
73
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
PerpsErrorCode,
|
|
3
|
+
type ProvidersResponse,
|
|
4
|
+
type Subscription,
|
|
5
|
+
} from '@lifi/perps-types'
|
|
2
6
|
import { PerpsError } from '../errors/PerpsError.js'
|
|
3
7
|
import { getProviders } from '../services/getProviders.js'
|
|
4
8
|
import type { GetQuoteParams } from '../services/getQuote.js'
|
|
@@ -70,6 +74,7 @@ export class PerpsWsClient {
|
|
|
70
74
|
private readonly options: PerpsWsClientOptions
|
|
71
75
|
private providers = new Map<string, WsProvider>()
|
|
72
76
|
private initPromises = new Map<string, Promise<WsProvider>>()
|
|
77
|
+
private discovery: Promise<ProvidersResponse> | undefined
|
|
73
78
|
private closed = false
|
|
74
79
|
|
|
75
80
|
constructor(client: PerpsSDKClient, options: PerpsWsClientOptions = {}) {
|
|
@@ -220,7 +225,7 @@ export class PerpsWsClient {
|
|
|
220
225
|
)
|
|
221
226
|
}
|
|
222
227
|
|
|
223
|
-
const { providers } = await
|
|
228
|
+
const { providers } = await this.discoverProviders()
|
|
224
229
|
this.assertOpen()
|
|
225
230
|
|
|
226
231
|
const providerInfo = providers.find((d) => d.key === provider)
|
|
@@ -236,4 +241,22 @@ export class PerpsWsClient {
|
|
|
236
241
|
this.providers.set(provider, wsProvider)
|
|
237
242
|
return wsProvider
|
|
238
243
|
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Shares one in-flight `/providers` request between concurrent venue
|
|
247
|
+
* inits. The slot clears on settlement, so a later init fetches fresh
|
|
248
|
+
* metadata and a rejection is never cached.
|
|
249
|
+
*/
|
|
250
|
+
private discoverProviders(): Promise<ProvidersResponse> {
|
|
251
|
+
if (this.discovery) {
|
|
252
|
+
return this.discovery
|
|
253
|
+
}
|
|
254
|
+
const discovery = getProviders(this.client).finally(() => {
|
|
255
|
+
if (this.discovery === discovery) {
|
|
256
|
+
this.discovery = undefined
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
this.discovery = discovery
|
|
260
|
+
return discovery
|
|
261
|
+
}
|
|
239
262
|
}
|
package/src/websocket/wsLog.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createWarnOnce, skippedRowMessage } from '../utils/warnOnce.js'
|
|
2
|
+
|
|
1
3
|
const MAX_PAYLOAD_CHARS = 512
|
|
2
4
|
|
|
3
5
|
const truncate = (raw: string): string =>
|
|
@@ -5,6 +7,8 @@ const truncate = (raw: string): string =>
|
|
|
5
7
|
? `${raw.slice(0, MAX_PAYLOAD_CHARS)}…(${raw.length} chars)`
|
|
6
8
|
: raw
|
|
7
9
|
|
|
10
|
+
const warnSkippedRowOnce = createWarnOnce()
|
|
11
|
+
|
|
8
12
|
/**
|
|
9
13
|
* Structured logger for WS message-handling and subscription failures, shared
|
|
10
14
|
* by the provider packages so a malformed frame, a throwing mapper, or a failed
|
|
@@ -58,6 +62,33 @@ export const wsLog = {
|
|
|
58
62
|
`[${provider}:ws] skipping item for unknown market '${marketId}'`
|
|
59
63
|
)
|
|
60
64
|
},
|
|
65
|
+
/**
|
|
66
|
+
* A frame row or order-book level with a required field that is not valid.
|
|
67
|
+
* Only that row is skipped. Logged once at `warn` for each provider, row and
|
|
68
|
+
* field; the key holds no venue value.
|
|
69
|
+
*/
|
|
70
|
+
skippedRow(
|
|
71
|
+
provider: string,
|
|
72
|
+
row: string,
|
|
73
|
+
field: string,
|
|
74
|
+
value: unknown
|
|
75
|
+
): void {
|
|
76
|
+
warnSkippedRowOnce(
|
|
77
|
+
`${provider}|${row}|${field}`,
|
|
78
|
+
skippedRowMessage(`${provider}:ws`, row, field, value, 'decimal')
|
|
79
|
+
)
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* A frame row the mapper rejected for a reason other than one field, such as
|
|
83
|
+
* a lifecycle state the SDK does not carry. Logged once at `warn` for each
|
|
84
|
+
* provider and row; the key holds no venue value.
|
|
85
|
+
*/
|
|
86
|
+
droppedRow(provider: string, row: string, reason: string): void {
|
|
87
|
+
warnSkippedRowOnce(
|
|
88
|
+
`${provider}|${row}|dropped`,
|
|
89
|
+
`[${provider}:ws] skipping ${row} row: ${reason}`
|
|
90
|
+
)
|
|
91
|
+
},
|
|
61
92
|
/**
|
|
62
93
|
* A background market-registry refetch rejected. Unknown-market items keep
|
|
63
94
|
* being skipped until the next cooldown-gated retry, so logged at `warn`.
|
package/src/wire/orderAmounts.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { DecimalString, Market } from '@lifi/perps-types'
|
|
2
|
-
import Big from 'big.js'
|
|
2
|
+
import type Big from 'big.js'
|
|
3
3
|
import { DivBig, TruncBig } from '../decimal/big.js'
|
|
4
|
+
import { isDecimalStringZero } from '../decimal/compare.js'
|
|
4
5
|
import { isDecimalString } from '../decimal/parse.js'
|
|
5
6
|
import type { PerpsSDKClient } from '../types/provider.js'
|
|
6
7
|
import { snapOrderSize } from './snap.js'
|
|
@@ -74,7 +75,7 @@ export function calculateOrderAmounts(
|
|
|
74
75
|
? amount.div(price).toFixed()
|
|
75
76
|
: amount.times(leverage).div(price).toFixed()
|
|
76
77
|
const size = snapOrderSize(sdk, market, requested)
|
|
77
|
-
if (
|
|
78
|
+
if (isDecimalStringZero(size)) {
|
|
78
79
|
return null
|
|
79
80
|
}
|
|
80
81
|
|
package/src/wire/refuel.ts
CHANGED
|
@@ -1,32 +1,55 @@
|
|
|
1
1
|
import { type DecimalString, PerpsErrorCode } from '@lifi/perps-types'
|
|
2
|
+
import type Big from 'big.js'
|
|
2
3
|
import { DivBig } from '../decimal/big.js'
|
|
3
|
-
import {
|
|
4
|
+
import { BASE_UNITS_PATTERN } from '../decimal/convert.js'
|
|
5
|
+
import { decimalStringToBig } from '../decimal/decimalStringToBig.js'
|
|
4
6
|
import { PerpsError } from '../errors/PerpsError.js'
|
|
5
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Headroom, in percent, that {@link calculateRefuelAmount} adds over the gas
|
|
10
|
+
* deficit to pay the refuel route's own swap and bridge fees.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export const REFUEL_FEE_MARGIN_PERCENT = 20
|
|
15
|
+
|
|
6
16
|
/** @public */
|
|
7
17
|
export interface RefuelAmountInput {
|
|
8
|
-
/**
|
|
9
|
-
|
|
18
|
+
/** Native gas amount the LI.FI gas suggestion recommends, in base units. */
|
|
19
|
+
recommendedAmount: DecimalString
|
|
20
|
+
/** USD value of `recommendedAmount`. */
|
|
21
|
+
recommendedUsd: DecimalString
|
|
22
|
+
/** Native balance the wallet holds on the recommendation's chain, in base units. */
|
|
23
|
+
nativeBalance: DecimalString
|
|
10
24
|
/** USD price of one whole source token. */
|
|
11
25
|
priceUsd: DecimalString
|
|
12
26
|
/** Decimals of the source token, which the result is spelled to exactly. */
|
|
13
27
|
decimals: number
|
|
14
28
|
}
|
|
15
29
|
|
|
30
|
+
function requireBaseUnits(value: DecimalString, field: string): Big {
|
|
31
|
+
if (!BASE_UNITS_PATTERN.test(value)) {
|
|
32
|
+
throw new PerpsError(
|
|
33
|
+
PerpsErrorCode.ValidationError,
|
|
34
|
+
`Invalid \`${field}\`: '${value}' is not an integer base-unit string.`
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
return new DivBig(value)
|
|
38
|
+
}
|
|
39
|
+
|
|
16
40
|
/**
|
|
17
|
-
* Source-token amount
|
|
18
|
-
* the
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* The quotient does not terminate for most prices, which is why this is a
|
|
23
|
-
* big.js primitive: an 18-decimal token needs a ceiling at 10^-18, far past
|
|
24
|
-
* what a `number` can carry.
|
|
41
|
+
* Source-token amount that buys the native gas the wallet lacks: the part of
|
|
42
|
+
* the recommendation above `nativeBalance`, valued pro rata on
|
|
43
|
+
* `recommendedUsd`, plus {@link REFUEL_FEE_MARGIN_PERCENT}. Rounded **up**
|
|
44
|
+
* onto the token's decimal grid so a refuel never lands short, and spelled
|
|
45
|
+
* with trailing zeros to `decimals` to seed an amount input.
|
|
25
46
|
*
|
|
26
|
-
* @returns `undefined` when
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
47
|
+
* @returns `undefined` when the wallet already holds the recommendation, or
|
|
48
|
+
* when `recommendedAmount`, `recommendedUsd` or `priceUsd` is not greater
|
|
49
|
+
* than zero: a route with nothing to refuel, not a failure.
|
|
50
|
+
* @throws {PerpsError} `ValidationError` when a base-unit field is not an
|
|
51
|
+
* integer string, a USD field is not a {@link DecimalString}, or `decimals`
|
|
52
|
+
* is not a non-negative integer.
|
|
30
53
|
* @public
|
|
31
54
|
*/
|
|
32
55
|
export function calculateRefuelAmount(
|
|
@@ -39,13 +62,26 @@ export function calculateRefuelAmount(
|
|
|
39
62
|
`Invalid \`decimals\` for a refuel amount: ${decimals}`
|
|
40
63
|
)
|
|
41
64
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
const recommended = requireBaseUnits(
|
|
66
|
+
input.recommendedAmount,
|
|
67
|
+
'recommendedAmount'
|
|
68
|
+
)
|
|
69
|
+
const balance = requireBaseUnits(input.nativeBalance, 'nativeBalance')
|
|
70
|
+
const recommendedUsd = decimalStringToBig(input.recommendedUsd)
|
|
71
|
+
const priceUsd = decimalStringToBig(input.priceUsd)
|
|
72
|
+
const deficit = recommended.minus(balance)
|
|
73
|
+
if (
|
|
74
|
+
!recommended.gt(0) ||
|
|
75
|
+
!recommendedUsd.gt(0) ||
|
|
76
|
+
!priceUsd.gt(0) ||
|
|
77
|
+
!deficit.gt(0)
|
|
78
|
+
) {
|
|
45
79
|
return undefined
|
|
46
80
|
}
|
|
47
|
-
return
|
|
48
|
-
.
|
|
81
|
+
return recommendedUsd
|
|
82
|
+
.times(deficit)
|
|
83
|
+
.times(100 + REFUEL_FEE_MARGIN_PERCENT)
|
|
84
|
+
.div(recommended.times(100).times(priceUsd))
|
|
49
85
|
.round(decimals, DivBig.roundUp)
|
|
50
86
|
.toFixed(decimals)
|
|
51
87
|
}
|
package/src/wire/transferable.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DecimalString } from '@lifi/perps-types'
|
|
2
|
-
import {
|
|
2
|
+
import { decimalStringToBig } from '../decimal/decimalStringToBig.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The part of `units` that a venue-wide free figure releases: never below
|
|
@@ -17,8 +17,8 @@ export function calculateTransferable(
|
|
|
17
17
|
venueFigure: DecimalString,
|
|
18
18
|
units: DecimalString
|
|
19
19
|
): DecimalString {
|
|
20
|
-
const figure =
|
|
21
|
-
const held =
|
|
20
|
+
const figure = decimalStringToBig(venueFigure)
|
|
21
|
+
const held = decimalStringToBig(units)
|
|
22
22
|
if (figure.lt(0)) {
|
|
23
23
|
return '0'
|
|
24
24
|
}
|
package/src/wire/withdraw.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DecimalString } from '@lifi/perps-types'
|
|
2
|
-
import {
|
|
2
|
+
import { decimalStringToBig } from '../decimal/decimalStringToBig.js'
|
|
3
3
|
import type { WithdrawableBalance } from '../types/withdrawal.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -18,12 +18,10 @@ export function calculateWithdrawMax(
|
|
|
18
18
|
'available' | 'withdrawalFee' | 'isFeeDeducted'
|
|
19
19
|
>
|
|
20
20
|
): DecimalString {
|
|
21
|
-
const available =
|
|
21
|
+
const available = decimalStringToBig(row.available)
|
|
22
22
|
if (row.isFeeDeducted !== false || row.withdrawalFee === undefined) {
|
|
23
23
|
return available.toFixed()
|
|
24
24
|
}
|
|
25
|
-
const funded = available.minus(
|
|
26
|
-
requireDecimal(row.withdrawalFee, 'withdrawalFee')
|
|
27
|
-
)
|
|
25
|
+
const funded = available.minus(decimalStringToBig(row.withdrawalFee))
|
|
28
26
|
return funded.gt(0) ? funded.toFixed() : '0'
|
|
29
27
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"requireDecimal.d.ts","sourceRoot":"","sources":["../../../src/decimal/requireDecimal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAkB,MAAM,mBAAmB,CAAA;AACtE,OAAO,GAAG,MAAM,QAAQ,CAAA;AAWxB,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAQvE"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.requireDecimal = requireDecimal;
|
|
7
|
-
const perps_types_1 = require("@lifi/perps-types");
|
|
8
|
-
const big_js_1 = __importDefault(require("big.js"));
|
|
9
|
-
const PerpsError_js_1 = require("../errors/PerpsError.js");
|
|
10
|
-
const parse_js_1 = require("./parse.js");
|
|
11
|
-
function requireDecimal(value, field) {
|
|
12
|
-
if (!(0, parse_js_1.isDecimalString)(value)) {
|
|
13
|
-
throw new PerpsError_js_1.PerpsError(perps_types_1.PerpsErrorCode.ValidationError, `Invalid \`${field}\`: '${value}' is not a decimal string.`);
|
|
14
|
-
}
|
|
15
|
-
return new big_js_1.default(value);
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=requireDecimal.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"requireDecimal.js","sourceRoot":"","sources":["../../../src/decimal/requireDecimal.ts"],"names":[],"mappings":";;;;;AAYA,wCAQC;AApBD,mDAAsE;AACtE,oDAAwB;AACxB,2DAAoD;AACpD,yCAA4C;AAS5C,SAAgB,cAAc,CAAC,KAAoB,EAAE,KAAa;IAChE,IAAI,CAAC,IAAA,0BAAe,EAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,0BAAU,CAClB,4BAAc,CAAC,eAAe,EAC9B,aAAa,KAAK,QAAQ,KAAK,4BAA4B,CAC5D,CAAA;IACH,CAAC;IACD,OAAO,IAAI,gBAAG,CAAC,KAAK,CAAC,CAAA;AACvB,CAAC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import Big from 'big.js';
|
|
3
|
-
/**
|
|
4
|
-
* Parse a decimal input, naming the field it came from when the
|
|
5
|
-
* value breaks the {@link DecimalString} contract, so a malformed amount
|
|
6
|
-
* fails at the SDK boundary instead of inside big.js.
|
|
7
|
-
*
|
|
8
|
-
* @throws {PerpsError} `ValidationError` naming `field`.
|
|
9
|
-
*/
|
|
10
|
-
export declare function requireDecimal(value: DecimalString, field: string): Big;
|
|
11
|
-
//# sourceMappingURL=requireDecimal.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"requireDecimal.d.ts","sourceRoot":"","sources":["../../../src/decimal/requireDecimal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAkB,MAAM,mBAAmB,CAAA;AACtE,OAAO,GAAG,MAAM,QAAQ,CAAA;AAIxB;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAQvE"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { PerpsErrorCode } from '@lifi/perps-types';
|
|
2
|
-
import Big from 'big.js';
|
|
3
|
-
import { PerpsError } from '../errors/PerpsError.js';
|
|
4
|
-
import { isDecimalString } from './parse.js';
|
|
5
|
-
/**
|
|
6
|
-
* Parse a decimal input, naming the field it came from when the
|
|
7
|
-
* value breaks the {@link DecimalString} contract, so a malformed amount
|
|
8
|
-
* fails at the SDK boundary instead of inside big.js.
|
|
9
|
-
*
|
|
10
|
-
* @throws {PerpsError} `ValidationError` naming `field`.
|
|
11
|
-
*/
|
|
12
|
-
export function requireDecimal(value, field) {
|
|
13
|
-
if (!isDecimalString(value)) {
|
|
14
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid \`${field}\`: '${value}' is not a decimal string.`);
|
|
15
|
-
}
|
|
16
|
-
return new Big(value);
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=requireDecimal.js.map
|