@lifi/perps-sdk 21.1.0 → 23.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.
Files changed (170) hide show
  1. package/README.md +89 -16
  2. package/dist/cjs/client/PerpsClient.d.ts +9 -9
  3. package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
  4. package/dist/cjs/client/PerpsClient.js +12 -17
  5. package/dist/cjs/client/PerpsClient.js.map +1 -1
  6. package/dist/cjs/decimal/convert.d.ts +1 -5
  7. package/dist/cjs/decimal/convert.d.ts.map +1 -1
  8. package/dist/cjs/decimal/convert.js +10 -28
  9. package/dist/cjs/decimal/convert.js.map +1 -1
  10. package/dist/cjs/decimal/format.d.ts +2 -1
  11. package/dist/cjs/decimal/format.d.ts.map +1 -1
  12. package/dist/cjs/decimal/format.js.map +1 -1
  13. package/dist/cjs/decimal/parse.d.ts +2 -4
  14. package/dist/cjs/decimal/parse.d.ts.map +1 -1
  15. package/dist/cjs/decimal/parse.js +5 -2
  16. package/dist/cjs/decimal/parse.js.map +1 -1
  17. package/dist/cjs/decimal/requireDecimal.d.ts.map +1 -0
  18. package/dist/cjs/{wire → decimal}/requireDecimal.js +2 -1
  19. package/dist/cjs/decimal/requireDecimal.js.map +1 -0
  20. package/dist/cjs/index.d.ts +5 -5
  21. package/dist/cjs/index.d.ts.map +1 -1
  22. package/dist/cjs/index.js +6 -20
  23. package/dist/cjs/index.js.map +1 -1
  24. package/dist/cjs/math/fills.d.ts +3 -3
  25. package/dist/cjs/math/fills.d.ts.map +1 -1
  26. package/dist/cjs/math/fills.js.map +1 -1
  27. package/dist/cjs/math/order.d.ts +1 -8
  28. package/dist/cjs/math/order.d.ts.map +1 -1
  29. package/dist/cjs/math/order.js +6 -11
  30. package/dist/cjs/math/order.js.map +1 -1
  31. package/dist/cjs/math/position.d.ts +3 -8
  32. package/dist/cjs/math/position.d.ts.map +1 -1
  33. package/dist/cjs/math/position.js +12 -12
  34. package/dist/cjs/math/position.js.map +1 -1
  35. package/dist/cjs/services/resolveSubscribeQuote.d.ts +1 -1
  36. package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
  37. package/dist/cjs/services/resolveSubscribeQuote.js +24 -13
  38. package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
  39. package/dist/cjs/types/api.d.ts +4 -4
  40. package/dist/cjs/types/api.d.ts.map +1 -1
  41. package/dist/cjs/types/config.d.ts +0 -1
  42. package/dist/cjs/types/config.d.ts.map +1 -1
  43. package/dist/cjs/types/provider.d.ts +1 -1
  44. package/dist/cjs/types/provider.d.ts.map +1 -1
  45. package/dist/cjs/version.d.ts +1 -1
  46. package/dist/cjs/version.js +1 -1
  47. package/dist/cjs/wire/orderAmounts.d.ts +1 -2
  48. package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
  49. package/dist/cjs/wire/orderAmounts.js +19 -52
  50. package/dist/cjs/wire/orderAmounts.js.map +1 -1
  51. package/dist/cjs/wire/refuel.js +1 -1
  52. package/dist/cjs/wire/refuel.js.map +1 -1
  53. package/dist/cjs/wire/transferable.js +1 -1
  54. package/dist/cjs/wire/transferable.js.map +1 -1
  55. package/dist/cjs/wire/withdraw.d.ts.map +1 -1
  56. package/dist/cjs/wire/withdraw.js +1 -1
  57. package/dist/cjs/wire/withdraw.js.map +1 -1
  58. package/dist/esm/client/PerpsClient.d.ts +9 -9
  59. package/dist/esm/client/PerpsClient.d.ts.map +1 -1
  60. package/dist/esm/client/PerpsClient.js +12 -17
  61. package/dist/esm/client/PerpsClient.js.map +1 -1
  62. package/dist/esm/decimal/convert.d.ts +8 -24
  63. package/dist/esm/decimal/convert.d.ts.map +1 -1
  64. package/dist/esm/decimal/convert.js +16 -42
  65. package/dist/esm/decimal/convert.js.map +1 -1
  66. package/dist/esm/decimal/format.d.ts +2 -1
  67. package/dist/esm/decimal/format.d.ts.map +1 -1
  68. package/dist/esm/decimal/format.js.map +1 -1
  69. package/dist/esm/decimal/parse.d.ts +7 -20
  70. package/dist/esm/decimal/parse.d.ts.map +1 -1
  71. package/dist/esm/decimal/parse.js +9 -4
  72. package/dist/esm/decimal/parse.js.map +1 -1
  73. package/dist/esm/{wire → decimal}/requireDecimal.d.ts +1 -1
  74. package/dist/esm/decimal/requireDecimal.d.ts.map +1 -0
  75. package/dist/esm/{wire → decimal}/requireDecimal.js +3 -2
  76. package/dist/esm/decimal/requireDecimal.js.map +1 -0
  77. package/dist/esm/index.d.ts +5 -5
  78. package/dist/esm/index.d.ts.map +1 -1
  79. package/dist/esm/index.js +4 -4
  80. package/dist/esm/index.js.map +1 -1
  81. package/dist/esm/math/fills.d.ts +3 -3
  82. package/dist/esm/math/fills.d.ts.map +1 -1
  83. package/dist/esm/math/fills.js +1 -1
  84. package/dist/esm/math/fills.js.map +1 -1
  85. package/dist/esm/math/order.d.ts +3 -34
  86. package/dist/esm/math/order.d.ts.map +1 -1
  87. package/dist/esm/math/order.js +8 -26
  88. package/dist/esm/math/order.js.map +1 -1
  89. package/dist/esm/math/position.d.ts +16 -34
  90. package/dist/esm/math/position.d.ts.map +1 -1
  91. package/dist/esm/math/position.js +23 -36
  92. package/dist/esm/math/position.js.map +1 -1
  93. package/dist/esm/services/resolveSubscribeQuote.d.ts +6 -1
  94. package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
  95. package/dist/esm/services/resolveSubscribeQuote.js +29 -13
  96. package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
  97. package/dist/esm/types/api.d.ts +4 -4
  98. package/dist/esm/types/api.d.ts.map +1 -1
  99. package/dist/esm/types/config.d.ts +0 -11
  100. package/dist/esm/types/config.d.ts.map +1 -1
  101. package/dist/esm/types/provider.d.ts +1 -1
  102. package/dist/esm/types/provider.d.ts.map +1 -1
  103. package/dist/esm/version.d.ts +1 -1
  104. package/dist/esm/version.js +1 -1
  105. package/dist/esm/wire/orderAmounts.d.ts +15 -29
  106. package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
  107. package/dist/esm/wire/orderAmounts.js +28 -88
  108. package/dist/esm/wire/orderAmounts.js.map +1 -1
  109. package/dist/esm/wire/refuel.js +1 -1
  110. package/dist/esm/wire/refuel.js.map +1 -1
  111. package/dist/esm/wire/transferable.js +1 -1
  112. package/dist/esm/wire/transferable.js.map +1 -1
  113. package/dist/esm/wire/withdraw.d.ts.map +1 -1
  114. package/dist/esm/wire/withdraw.js +1 -1
  115. package/dist/esm/wire/withdraw.js.map +1 -1
  116. package/dist/types/client/PerpsClient.d.ts +9 -9
  117. package/dist/types/client/PerpsClient.d.ts.map +1 -1
  118. package/dist/types/decimal/convert.d.ts +8 -24
  119. package/dist/types/decimal/convert.d.ts.map +1 -1
  120. package/dist/types/decimal/format.d.ts +2 -1
  121. package/dist/types/decimal/format.d.ts.map +1 -1
  122. package/dist/types/decimal/parse.d.ts +7 -20
  123. package/dist/types/decimal/parse.d.ts.map +1 -1
  124. package/dist/types/{wire → decimal}/requireDecimal.d.ts +1 -1
  125. package/dist/types/decimal/requireDecimal.d.ts.map +1 -0
  126. package/dist/types/index.d.ts +5 -5
  127. package/dist/types/index.d.ts.map +1 -1
  128. package/dist/types/math/fills.d.ts +3 -3
  129. package/dist/types/math/fills.d.ts.map +1 -1
  130. package/dist/types/math/order.d.ts +3 -34
  131. package/dist/types/math/order.d.ts.map +1 -1
  132. package/dist/types/math/position.d.ts +16 -34
  133. package/dist/types/math/position.d.ts.map +1 -1
  134. package/dist/types/services/resolveSubscribeQuote.d.ts +6 -1
  135. package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
  136. package/dist/types/types/api.d.ts +4 -4
  137. package/dist/types/types/api.d.ts.map +1 -1
  138. package/dist/types/types/config.d.ts +0 -11
  139. package/dist/types/types/config.d.ts.map +1 -1
  140. package/dist/types/types/provider.d.ts +1 -1
  141. package/dist/types/types/provider.d.ts.map +1 -1
  142. package/dist/types/version.d.ts +1 -1
  143. package/dist/types/wire/orderAmounts.d.ts +15 -29
  144. package/dist/types/wire/orderAmounts.d.ts.map +1 -1
  145. package/dist/types/wire/withdraw.d.ts.map +1 -1
  146. package/package.json +2 -2
  147. package/src/client/PerpsClient.ts +41 -26
  148. package/src/decimal/convert.ts +27 -54
  149. package/src/decimal/format.ts +3 -1
  150. package/src/decimal/parse.ts +10 -6
  151. package/src/{wire → decimal}/requireDecimal.ts +3 -6
  152. package/src/index.ts +4 -21
  153. package/src/math/fills.ts +8 -4
  154. package/src/math/order.ts +8 -31
  155. package/src/math/position.ts +32 -43
  156. package/src/services/resolveSubscribeQuote.ts +37 -19
  157. package/src/types/api.ts +4 -3
  158. package/src/types/config.ts +0 -11
  159. package/src/types/provider.ts +1 -1
  160. package/src/version.ts +1 -1
  161. package/src/wire/orderAmounts.ts +35 -117
  162. package/src/wire/refuel.ts +1 -1
  163. package/src/wire/transferable.ts +1 -1
  164. package/src/wire/withdraw.ts +1 -1
  165. package/dist/cjs/wire/requireDecimal.d.ts.map +0 -1
  166. package/dist/cjs/wire/requireDecimal.js.map +0 -1
  167. package/dist/esm/wire/requireDecimal.d.ts.map +0 -1
  168. package/dist/esm/wire/requireDecimal.js.map +0 -1
  169. package/dist/types/wire/requireDecimal.d.ts.map +0 -1
  170. /package/dist/cjs/{wire → decimal}/requireDecimal.d.ts +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"orderAmounts.d.ts","sourceRoot":"","sources":["../../../src/wire/orderAmounts.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAElB,KAAK,MAAM,EAEZ,MAAM,mBAAmB,CAAA;AAI1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG1D,cAAc;AACd,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,cAAc,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,sEAAsE;IACtE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAA;IACpC,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,aAAa,CAAA;IACpB,0EAA0E;IAC1E,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,cAAc;AACd,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,MAAM,EAAE,aAAa,CAAA;IACrB,uCAAuC;IACvC,IAAI,EAAE,aAAa,CAAA;IACnB,oCAAoC;IACpC,QAAQ,EAAE,aAAa,CAAA;CACxB;AAqDD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,iBAAiB,GACvB,YAAY,GAAG,IAAI,CAqDrB"}
1
+ {"version":3,"file":"orderAmounts.d.ts","sourceRoot":"","sources":["../../../src/wire/orderAmounts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAI9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG1D,cAAc;AACd,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,cAAc,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,sEAAsE;IACtE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAA;IACpC,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,aAAa,CAAA;CACrB;AAED,cAAc;AACd,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,MAAM,EAAE,aAAa,CAAA;IACrB,sEAAsE;IACtE,IAAI,EAAE,aAAa,CAAA;IACnB,qDAAqD;IACrD,QAAQ,EAAE,aAAa,CAAA;CACxB;AAUD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,iBAAiB,GACvB,YAAY,GAAG,IAAI,CAiCrB"}
@@ -1 +1 @@
1
- {"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../../../src/wire/withdraw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAGjE;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,IAAI,CACP,mBAAmB,EACnB,WAAW,GAAG,eAAe,GAAG,eAAe,CAChD,GACA,aAAa,CASf"}
1
+ {"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../../../src/wire/withdraw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAEjE;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,IAAI,CACP,mBAAmB,EACnB,WAAW,GAAG,eAAe,GAAG,eAAe,CAChD,GACA,aAAa,CASf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/perps-sdk",
3
- "version": "21.1.0",
3
+ "version": "23.0.0",
4
4
  "description": "LI.FI Perps SDK for perpetuals trading",
5
5
  "homepage": "https://github.com/lifinance/perps-sdk",
6
6
  "bugs": {
@@ -46,7 +46,7 @@
46
46
  "big.js": "^7.0.1",
47
47
  "partysocket": "^1.1.19",
48
48
  "viem": "^2.55.18",
49
- "@lifi/perps-types": "^19.0.0"
49
+ "@lifi/perps-types": "^20.1.0"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"
@@ -7,6 +7,7 @@ import type {
7
7
  ActionStep,
8
8
  AvailableToTrade,
9
9
  CreateActionResponse,
10
+ DecimalString,
10
11
  ExecuteActionResponse,
11
12
  MarketRef,
12
13
  MarketSettings,
@@ -647,13 +648,16 @@ export class PerpsClient {
647
648
  * backend account fetch fails.
648
649
  * @public
649
650
  */
650
- async getAccount(params: {
651
- provider: string
652
- address: Address
653
- }): Promise<GetAccountResult> {
651
+ async getAccount(
652
+ params: {
653
+ provider: string
654
+ address: Address
655
+ },
656
+ options?: SDKRequestOptions
657
+ ): Promise<GetAccountResult> {
654
658
  const plugin = this.requireProvider(params.provider)
655
659
  const [response, metadata] = await Promise.all([
656
- fetchAccount(this.sdkClient, params),
660
+ fetchAccount(this.sdkClient, params, options),
657
661
  this.getProviderMetadata(params.provider),
658
662
  ])
659
663
  const settings = plugin.projectConfig(response.config, metadata.setup)
@@ -673,16 +677,19 @@ export class PerpsClient {
673
677
  * @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
674
678
  * @public
675
679
  */
676
- async getMarketSettings(params: {
677
- provider: string
678
- address: Address
679
- market: MarketRef
680
- }): Promise<MarketSettings> {
680
+ async getMarketSettings(
681
+ params: {
682
+ provider: string
683
+ address: Address
684
+ market: MarketRef
685
+ },
686
+ options?: SDKRequestOptions
687
+ ): Promise<MarketSettings> {
681
688
  const plugin = this.requireProvider(params.provider)
682
- return plugin.getMarketSettings({
683
- address: params.address,
684
- market: params.market,
685
- })
689
+ return plugin.getMarketSettings(
690
+ { address: params.address, market: params.market },
691
+ options
692
+ )
686
693
  }
687
694
 
688
695
  /**
@@ -753,7 +760,7 @@ export class PerpsClient {
753
760
  * venue formula reads is malformed.
754
761
  * @public
755
762
  */
756
- getPositionRemovableMargin(position: Position): string | undefined {
763
+ getPositionRemovableMargin(position: Position): DecimalString | undefined {
757
764
  return this.requireProvider(
758
765
  position.market.providerId
759
766
  ).positionRemovableMargin(position)
@@ -786,8 +793,12 @@ export class PerpsClient {
786
793
  * plugin's existence probe fails.
787
794
  * @public
788
795
  */
789
- async accountExists(provider: string, address: Address): Promise<boolean> {
790
- return this.requireProvider(provider).accountExists({ address })
796
+ async accountExists(
797
+ provider: string,
798
+ address: Address,
799
+ options?: SDKRequestOptions
800
+ ): Promise<boolean> {
801
+ return this.requireProvider(provider).accountExists({ address }, options)
791
802
  }
792
803
 
793
804
  /**
@@ -801,10 +812,11 @@ export class PerpsClient {
801
812
  * @public
802
813
  */
803
814
  async getDepositFlow(
804
- params: GetDepositFlowParams
815
+ params: GetDepositFlowParams,
816
+ options?: SDKRequestOptions
805
817
  ): Promise<DepositFlow | undefined> {
806
818
  const plugin = this.requireProvider(params.provider)
807
- return plugin.getDepositFlow?.({ address: params.address })
819
+ return plugin.getDepositFlow?.({ address: params.address }, options)
808
820
  }
809
821
 
810
822
  /**
@@ -818,10 +830,11 @@ export class PerpsClient {
818
830
  * @public
819
831
  */
820
832
  async getWithdrawFlow(
821
- params: GetWithdrawFlowParams
833
+ params: GetWithdrawFlowParams,
834
+ options?: SDKRequestOptions
822
835
  ): Promise<WithdrawFlow | undefined> {
823
836
  const plugin = this.requireProvider(params.provider)
824
- return plugin.getWithdrawFlow?.({ address: params.address })
837
+ return plugin.getWithdrawFlow?.({ address: params.address }, options)
825
838
  }
826
839
 
827
840
  /**
@@ -844,12 +857,14 @@ export class PerpsClient {
844
857
  * @public
845
858
  */
846
859
  async getWithdrawableBalances(
847
- params: GetWithdrawableBalancesParams
860
+ params: GetWithdrawableBalancesParams,
861
+ options?: SDKRequestOptions
848
862
  ): Promise<WithdrawableBalance[] | undefined> {
849
863
  const plugin = this.requireProvider(params.provider)
850
- const rows = await plugin.getWithdrawableBalances?.({
851
- address: params.address,
852
- })
864
+ const rows = await plugin.getWithdrawableBalances?.(
865
+ { address: params.address },
866
+ options
867
+ )
853
868
  if (rows === undefined) {
854
869
  return undefined
855
870
  }
@@ -1448,7 +1463,7 @@ export class PerpsClient {
1448
1463
  address: Address
1449
1464
  market: MarketRef
1450
1465
  action: 'add' | 'remove'
1451
- amount: string
1466
+ amount: DecimalString
1452
1467
  }): Promise<ExecuteActionResponse> {
1453
1468
  return this.execute({
1454
1469
  ...params,
@@ -2,7 +2,7 @@ import { type DecimalString, PerpsErrorCode } from '@lifi/perps-types'
2
2
  import Big from 'big.js'
3
3
  import { formatUnits } from 'viem'
4
4
  import { PerpsError } from '../errors/PerpsError.js'
5
- import { parseDecimal } from './parse.js'
5
+ import { requireDecimal } from './requireDecimal.js'
6
6
 
7
7
  /**
8
8
  * Off-grid resolution for {@link decimalToBaseUnits}:
@@ -14,19 +14,14 @@ import { parseDecimal } from './parse.js'
14
14
  */
15
15
  export type BaseUnitsRounding = 'truncate' | 'round'
16
16
 
17
- /**
18
- * @deprecated Use `BaseUnitsRounding`. Removed in the next major.
19
- */
20
- export type ScaleToIntegerPolicy = BaseUnitsRounding
21
-
22
17
  /**
23
18
  * Scale a decimal string to a scaled integer in exact decimal arithmetic — an
24
19
  * on-grid input maps to its exact scaled integer with no binary float
25
20
  * artifacts (`'0.29'` at 2 decimals is 29, never 28). Off-grid input resolves
26
21
  * per `rounding`; there is no silent default.
27
22
  *
28
- * @throws {PerpsError} `ValidationError` when `value` is not a decimal
29
- * numeric string, `decimals` is not a non-negative integer, or the scaled
23
+ * @throws {PerpsError} `ValidationError` when `value` is not a
24
+ * {@link DecimalString}, `decimals` is not a non-negative integer, or the scaled
30
25
  * result's magnitude exceeds `Number.MAX_SAFE_INTEGER`.
31
26
  * @public
32
27
  */
@@ -41,16 +36,7 @@ export const decimalToBaseUnits = (
41
36
  `Invalid decimals for integer scaling: ${decimals}`
42
37
  )
43
38
  }
44
- let parsed: Big
45
- try {
46
- parsed = new Big(value)
47
- } catch {
48
- throw new PerpsError(
49
- PerpsErrorCode.ValidationError,
50
- `Invalid decimal string for integer scaling: '${value}'`
51
- )
52
- }
53
- const scaled = parsed
39
+ const scaled = requireDecimal(value, 'decimalToBaseUnits(value)')
54
40
  .times(new Big(10).pow(decimals))
55
41
  .round(0, rounding === 'truncate' ? Big.roundDown : Big.roundHalfUp)
56
42
  if (scaled.abs().gt(Number.MAX_SAFE_INTEGER)) {
@@ -62,17 +48,15 @@ export const decimalToBaseUnits = (
62
48
  return scaled.toNumber()
63
49
  }
64
50
 
65
- /**
66
- * @deprecated Use `decimalToBaseUnits`. Removed in the next major.
67
- */
68
- export const scaleToInteger = decimalToBaseUnits
51
+ const BASE_UNITS_PATTERN = /^-?\d+$/
69
52
 
70
53
  /**
71
- * Convert a base-unit amount (bigint string) to a decimal string.
54
+ * Convert a base-unit amount (integer string) to a decimal string.
72
55
  *
73
56
  * @param amount - Amount in base units (e.g. "1000000" for 1 USDC)
74
57
  * @param decimals - Token decimals (e.g. 6 for USDC)
75
- * @returns Decimal string; `"0"` when `amount` is not a valid bigint
58
+ * @throws {PerpsError} `ValidationError` when `amount` is not an integer
59
+ * string, or `decimals` is not a non-negative integer.
76
60
  * @example
77
61
  * ```ts
78
62
  * baseUnitsToDecimal('1000000', 6) // '1'
@@ -80,27 +64,22 @@ export const scaleToInteger = decimalToBaseUnits
80
64
  * @public
81
65
  */
82
66
  export function baseUnitsToDecimal(
83
- amount: string,
67
+ amount: DecimalString,
84
68
  decimals: number
85
69
  ): DecimalString {
86
- try {
87
- return formatUnits(BigInt(amount), decimals)
88
- } catch {
89
- return '0'
70
+ if (!Number.isInteger(decimals) || decimals < 0) {
71
+ throw new PerpsError(
72
+ PerpsErrorCode.ValidationError,
73
+ `Invalid \`baseUnitsToDecimal(decimals)\`: ${decimals} is not a non-negative integer.`
74
+ )
90
75
  }
91
- }
92
-
93
- /**
94
- * @deprecated Use `baseUnitsToDecimal`. Removed in the next major.
95
- */
96
- export const fromBaseUnits = baseUnitsToDecimal
97
-
98
- /**
99
- * @deprecated Compose `parseDecimal(baseUnitsToDecimal(...))`. Removed in the
100
- * next major.
101
- */
102
- export function fromBaseUnitsNumber(amount: string, decimals: number): number {
103
- return parseDecimal(baseUnitsToDecimal(amount, decimals)) ?? 0
76
+ if (!BASE_UNITS_PATTERN.test(amount)) {
77
+ throw new PerpsError(
78
+ PerpsErrorCode.ValidationError,
79
+ `Invalid \`baseUnitsToDecimal(amount)\`: '${amount}' is not an integer base-unit string.`
80
+ )
81
+ }
82
+ return formatUnits(BigInt(amount), decimals)
104
83
  }
105
84
 
106
85
  /**
@@ -114,8 +93,8 @@ export function fromBaseUnitsNumber(amount: string, decimals: number): number {
114
93
  * float artifact: `'0.01'` at 18 decimals gives
115
94
  * `'0.010000000000000000'`, which `Number#toFixed` cannot.
116
95
  *
117
- * @throws {PerpsError} `ValidationError` when `value` is not a decimal
118
- * numeric string, or `decimals` is not a non-negative integer.
96
+ * @throws {PerpsError} `ValidationError` when `value` is not a
97
+ * {@link DecimalString}, or `decimals` is not a non-negative integer.
119
98
  * @public
120
99
  */
121
100
  export function truncateDecimal(
@@ -128,16 +107,10 @@ export function truncateDecimal(
128
107
  `Invalid decimals for truncation: ${decimals}`
129
108
  )
130
109
  }
131
- let parsed: Big
132
- try {
133
- parsed = new Big(value)
134
- } catch {
135
- throw new PerpsError(
136
- PerpsErrorCode.ValidationError,
137
- `Invalid decimal string for truncation: '${value}'`
138
- )
139
- }
140
- const truncated = parsed.round(decimals, Big.roundDown)
110
+ const truncated = requireDecimal(value, 'truncateDecimal(value)').round(
111
+ decimals,
112
+ Big.roundDown
113
+ )
141
114
  // big.js carries the sign through a round to zero; '-0.00' is not a spelling.
142
115
  return (truncated.eq(0) ? new Big(0) : truncated).toFixed(decimals)
143
116
  }
@@ -1,3 +1,5 @@
1
+ import type { DecimalString } from '@lifi/perps-types'
2
+
1
3
  /**
2
4
  * Options shared by the human-facing display formatters.
3
5
  *
@@ -28,7 +30,7 @@ export type RoundingMode = 'halfUp' | 'floor'
28
30
 
29
31
  const DEFAULT_PLACEHOLDER = '—'
30
32
 
31
- type FormatInput = number | string | null | undefined
33
+ type FormatInput = number | DecimalString | string | null | undefined
32
34
 
33
35
  /**
34
36
  * Extra decimal places used to normalise IEEE-754 representation noise before
@@ -1,4 +1,4 @@
1
- import type { DecimalString } from '@lifi/perps-types'
1
+ import { DECIMAL_PATTERN, type DecimalString } from '@lifi/perps-types'
2
2
 
3
3
  /**
4
4
  * Parse a string to a float, stripping common formatting artefacts.
@@ -24,11 +24,6 @@ function toFloat(value: string): number {
24
24
  return parseFloat(cleaned)
25
25
  }
26
26
 
27
- /**
28
- * @deprecated Use `parseDecimal`. Removed in the next major.
29
- */
30
- export const stringToFloat = toFloat
31
-
32
27
  const FORMATTED_NUMBER =
33
28
  /^(?:[+-]\s*)?(?:\$\s*)?(?:(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?:\s*(?:%|USD))?$/i
34
29
 
@@ -54,3 +49,12 @@ export function parseDecimal(
54
49
  const parsed = toFloat(value)
55
50
  return Number.isFinite(parsed) ? parsed : undefined
56
51
  }
52
+
53
+ /**
54
+ * Narrows an unknown value to a {@link DecimalString}.
55
+ *
56
+ * @public
57
+ */
58
+ export function isDecimalString(value: unknown): value is DecimalString {
59
+ return typeof value === 'string' && DECIMAL_PATTERN.test(value)
60
+ }
@@ -1,13 +1,10 @@
1
- import {
2
- type DecimalString,
3
- isDecimalString,
4
- PerpsErrorCode,
5
- } from '@lifi/perps-types'
1
+ import { type DecimalString, PerpsErrorCode } from '@lifi/perps-types'
6
2
  import Big from 'big.js'
7
3
  import { PerpsError } from '../errors/PerpsError.js'
4
+ import { isDecimalString } from './parse.js'
8
5
 
9
6
  /**
10
- * Parse a wire-tier decimal input, naming the field it came from when the
7
+ * Parse a decimal input, naming the field it came from when the
11
8
  * value breaks the {@link DecimalString} contract, so a malformed amount
12
9
  * fails at the SDK boundary instead of inside big.js.
13
10
  *
package/src/index.ts CHANGED
@@ -17,17 +17,11 @@ export {
17
17
  } from './client/createPerpsClient.js'
18
18
  export { PerpsClient } from './client/PerpsClient.js'
19
19
  export { requireProvider } from './client/requireProvider.js'
20
- export type {
21
- BaseUnitsRounding,
22
- ScaleToIntegerPolicy,
23
- } from './decimal/convert.js'
20
+ export type { BaseUnitsRounding } from './decimal/convert.js'
24
21
  export {
25
22
  baseUnitsToDecimal,
26
23
  decimalToBaseUnits,
27
- fromBaseUnits,
28
- fromBaseUnitsNumber,
29
24
  numberToDecimalString,
30
- scaleToInteger,
31
25
  truncateDecimal,
32
26
  } from './decimal/convert.js'
33
27
  export type { FormatOptions, RoundingMode } from './decimal/format.js'
@@ -39,7 +33,7 @@ export {
39
33
  formatSignedUsd,
40
34
  formatUsd,
41
35
  } from './decimal/format.js'
42
- export { parseDecimal, stringToFloat } from './decimal/parse.js'
36
+ export { isDecimalString, parseDecimal } from './decimal/parse.js'
43
37
  export { PerpsErrorMessage } from './errors/constants.js'
44
38
  export { PerpsError } from './errors/PerpsError.js'
45
39
  export {
@@ -52,18 +46,13 @@ export {
52
46
  applySlippage,
53
47
  buildQuote,
54
48
  calculateExpectedPnl,
55
- calculatePositionSize,
56
49
  calculateRealizedPnlPercent,
57
50
  calculateSize,
58
51
  calculateTriggerPercent,
59
52
  calculateTriggerPrice,
60
53
  estimateFees,
61
54
  estimateRealizedPnl,
62
- expectedRealizedPnlForOpenOrder,
63
- expectedRealizedPnlForTriggerOrder,
64
55
  findMatchingPosition,
65
- percentFromPrice,
66
- priceFromPercent,
67
56
  resolveCloseSize,
68
57
  walkOrderbook,
69
58
  } from './math/order.js'
@@ -75,18 +64,12 @@ export {
75
64
  calculateRequiredMargin,
76
65
  calculateRoe,
77
66
  calculateUnrealizedPnl,
78
- directionSign,
79
- effectiveLeverage,
80
67
  estimateAverageEntryPrice,
81
- estimateIsolatedLiquidationPrice,
82
68
  estimateLiquidationPrice,
83
69
  estimateNewLeverage,
84
70
  estimateUnrealizedPnl,
85
- liquidationDistancePercent,
86
- predictAverageEntryPrice,
87
- predictNewLeverage,
88
- predictUnrealizedPnl,
89
- realizedPnlOnClose,
71
+ positionSupportsMarginAdjustment,
72
+ positionSupportsMarginRemoval,
90
73
  wouldImmediatelyLiquidate,
91
74
  } from './math/position.js'
92
75
  // Registries
package/src/math/fills.ts CHANGED
@@ -1,4 +1,8 @@
1
- import { FillClassification, OrderSide } from '@lifi/perps-types'
1
+ import {
2
+ type DecimalString,
3
+ FillClassification,
4
+ OrderSide,
5
+ } from '@lifi/perps-types'
2
6
  import { parseDecimal } from '../decimal/parse.js'
3
7
 
4
8
  /**
@@ -19,9 +23,9 @@ export { FillClassification }
19
23
  * @public
20
24
  */
21
25
  export function classifyFillFromPosition(
22
- startPosition: string,
26
+ startPosition: DecimalString,
23
27
  side: string,
24
- sz: string
28
+ sz: DecimalString
25
29
  ): FillClassification {
26
30
  const start = parseFloat(startPosition)
27
31
  const delta = side === 'B' ? parseFloat(sz) : -parseFloat(sz)
@@ -66,7 +70,7 @@ export function classifyFillFromPosition(
66
70
  */
67
71
  export function classifyFill(
68
72
  side: OrderSide,
69
- realizedPnl: string | null | undefined
73
+ realizedPnl: DecimalString | null | undefined
70
74
  ): FillClassification {
71
75
  const isClose = realizedPnl != null && parseDecimal(realizedPnl) !== 0
72
76
  if (side === OrderSide.BUY) {
package/src/math/order.ts CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  } from '@lifi/perps-types'
23
23
  import type Big from 'big.js'
24
24
  import { areFinite, DivBig } from '../decimal/big.js'
25
+ import { numberToDecimalString } from '../decimal/convert.js'
25
26
  import { PerpsError } from '../errors/PerpsError.js'
26
27
  import {
27
28
  isActiveOrderStatus,
@@ -57,11 +58,6 @@ export function calculateSize(
57
58
  return new DivBig(marginUsd).times(leverage).div(price).toNumber()
58
59
  }
59
60
 
60
- /**
61
- * @deprecated Use `calculateSize`. Removed in the next major.
62
- */
63
- export const calculatePositionSize = calculateSize
64
-
65
61
  /**
66
62
  * Estimate trading fees.
67
63
  *
@@ -176,11 +172,6 @@ export function calculateTriggerPrice(
176
172
  return (isLong ? entry.plus(priceDelta) : entry.minus(priceDelta)).toNumber()
177
173
  }
178
174
 
179
- /**
180
- * @deprecated Use `calculateTriggerPrice`. Removed in the next major.
181
- */
182
- export const priceFromPercent = calculateTriggerPrice
183
-
184
175
  /**
185
176
  * Calculate the percentage gain/loss a trigger price realises.
186
177
  *
@@ -208,11 +199,6 @@ export function calculateTriggerPercent(
208
199
  return priceDiff.div(entryPrice).times(leverage).times(100).toNumber()
209
200
  }
210
201
 
211
- /**
212
- * @deprecated Use `calculateTriggerPercent`. Removed in the next major.
213
- */
214
- export const percentFromPrice = calculateTriggerPercent
215
-
216
202
  /**
217
203
  * Calculate realized PnL as a percentage of position value at close.
218
204
  *
@@ -334,7 +320,8 @@ interface BuildQuoteInput {
334
320
  * the market's `funding` (`null` for spot, which has none).
335
321
  *
336
322
  * @throws {PerpsError} `ValidationError` when a book level does not parse to
337
- * a finite number — see {@link walkOrderbook}.
323
+ * a finite number — see {@link walkOrderbook} — or when a quoted figure is
324
+ * not finite (a non-finite `sizeUsd`, mark price or taker fee).
338
325
  * @public
339
326
  */
340
327
  export function buildQuote(input: BuildQuoteInput): Quote {
@@ -356,14 +343,14 @@ export function buildQuote(input: BuildQuoteInput): Quote {
356
343
  marketId: market.id,
357
344
  type: input.type,
358
345
  side,
359
- sizeUsd: sizeUsd.toString(),
360
- baseSize: walk.baseSize.toString(),
346
+ sizeUsd: numberToDecimalString(sizeUsd),
347
+ baseSize: numberToDecimalString(walk.baseSize),
361
348
  markPrice: price.markPrice,
362
- expectedFillPrice: walk.vwap.toString(),
363
- priceImpactBps: priceImpactBps.toString(),
349
+ expectedFillPrice: numberToDecimalString(walk.vwap),
350
+ priceImpactBps: numberToDecimalString(priceImpactBps),
364
351
  feeTier,
365
352
  isDefaultFeeTier: true,
366
- feeUsd: feeUsd.toString(),
353
+ feeUsd: numberToDecimalString(feeUsd),
367
354
  funding: price.funding ?? null,
368
355
  insufficientLiquidity: walk.insufficientLiquidity,
369
356
  timestamp: input.timestamp,
@@ -530,13 +517,3 @@ export function estimateRealizedPnl(
530
517
  }
531
518
  return null
532
519
  }
533
-
534
- /**
535
- * @deprecated Use `estimateRealizedPnl`. Removed in the next major.
536
- */
537
- export const expectedRealizedPnlForOpenOrder = regularOrderRealizedPnl
538
-
539
- /**
540
- * @deprecated Use `estimateRealizedPnl`. Removed in the next major.
541
- */
542
- export const expectedRealizedPnlForTriggerOrder = triggerOrderRealizedPnl