@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
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 { decimalStringToDivBig } 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 does not match the decimal pattern, 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 = decimalStringToDivBig(input.recommendedUsd)
|
|
71
|
+
const priceUsd = decimalStringToDivBig(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
|
|
@@ -9,16 +9,16 @@ import { requireDecimal } from '../decimal/requireDecimal.js'
|
|
|
9
9
|
* @param venueFigure - The venue's free figure for the category, which can
|
|
10
10
|
* exceed `units` or fall below zero.
|
|
11
11
|
* @param units - Units held in the category, the upper bound of the clamp.
|
|
12
|
-
* @throws {PerpsError} `ValidationError` when either input
|
|
13
|
-
*
|
|
12
|
+
* @throws {PerpsError} `ValidationError` when either input does not match the
|
|
13
|
+
* decimal pattern, naming the field.
|
|
14
14
|
* @public
|
|
15
15
|
*/
|
|
16
16
|
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,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,GAAG,MAAM,QAAQ,CAAA;AAIxB,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAEvE"}
|
|
@@ -1,12 +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 big_js_1 = __importDefault(require("big.js"));
|
|
8
|
-
const parse_js_1 = require("./parse.js");
|
|
9
|
-
function requireDecimal(value, field) {
|
|
10
|
-
return new big_js_1.default((0, parse_js_1.validateDecimalString)(value, field));
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=requireDecimal.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"requireDecimal.js","sourceRoot":"","sources":["../../../src/decimal/requireDecimal.ts"],"names":[],"mappings":";;;;;AAKA,wCAEC;AAND,oDAAwB;AACxB,yCAAkD;AAGlD,SAAgB,cAAc,CAAC,KAAoB,EAAE,KAAa;IAChE,OAAO,IAAI,gBAAG,CAAC,IAAA,gCAAqB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;AACrD,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import Big from 'big.js';
|
|
3
|
-
/** Parse a decimal input after {@link validateDecimalString} accepts it. */
|
|
4
|
-
export declare function requireDecimal(value: DecimalString, field: string): Big;
|
|
5
|
-
//# 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,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,GAAG,MAAM,QAAQ,CAAA;AAGxB,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAEvE"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import Big from 'big.js';
|
|
2
|
-
import { validateDecimalString } from './parse.js';
|
|
3
|
-
/** Parse a decimal input after {@link validateDecimalString} accepts it. */
|
|
4
|
-
export function requireDecimal(value, field) {
|
|
5
|
-
return new Big(validateDecimalString(value, field));
|
|
6
|
-
}
|
|
7
|
-
//# sourceMappingURL=requireDecimal.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"requireDecimal.js","sourceRoot":"","sources":["../../../src/decimal/requireDecimal.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAElD,4EAA4E;AAC5E,MAAM,UAAU,cAAc,CAAC,KAAoB,EAAE,KAAa;IAChE,OAAO,IAAI,GAAG,CAAC,qBAAqB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;AACrD,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import Big from 'big.js';
|
|
3
|
-
/** Parse a decimal input after {@link validateDecimalString} accepts it. */
|
|
4
|
-
export declare function requireDecimal(value: DecimalString, field: string): Big;
|
|
5
|
-
//# 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,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,GAAG,MAAM,QAAQ,CAAA;AAGxB,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAEvE"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types'
|
|
2
|
-
import Big from 'big.js'
|
|
3
|
-
import { validateDecimalString } from './parse.js'
|
|
4
|
-
|
|
5
|
-
/** Parse a decimal input after {@link validateDecimalString} accepts it. */
|
|
6
|
-
export function requireDecimal(value: DecimalString, field: string): Big {
|
|
7
|
-
return new Big(validateDecimalString(value, field))
|
|
8
|
-
}
|