@lifi/perps-sdk-provider-hyperliquid 0.0.0-preview-cbbc415 → 0.0.0-preview-e0d34a1

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 (65) hide show
  1. package/dist/cjs/services/getActivity.d.ts.map +1 -1
  2. package/dist/cjs/services/getActivity.js +8 -1
  3. package/dist/cjs/services/getActivity.js.map +1 -1
  4. package/dist/cjs/types/account.d.ts +5 -0
  5. package/dist/cjs/types/account.d.ts.map +1 -1
  6. package/dist/cjs/types/account.js.map +1 -1
  7. package/dist/cjs/types/ledger.d.ts +24 -1
  8. package/dist/cjs/types/ledger.d.ts.map +1 -1
  9. package/dist/cjs/types/ledger.js +8 -1
  10. package/dist/cjs/types/ledger.js.map +1 -1
  11. package/dist/cjs/utils/mapActivity.d.ts.map +1 -1
  12. package/dist/cjs/utils/mapActivity.js +47 -39
  13. package/dist/cjs/utils/mapActivity.js.map +1 -1
  14. package/dist/cjs/utils/mapOrder.d.ts.map +1 -1
  15. package/dist/cjs/utils/mapOrder.js +2 -1
  16. package/dist/cjs/utils/mapOrder.js.map +1 -1
  17. package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
  18. package/dist/cjs/utils/mapPosition.js +1 -0
  19. package/dist/cjs/utils/mapPosition.js.map +1 -1
  20. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  21. package/dist/cjs/websocket/HyperliquidWsProvider.js +2 -1
  22. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  23. package/dist/esm/services/getActivity.d.ts.map +1 -1
  24. package/dist/esm/services/getActivity.js +10 -1
  25. package/dist/esm/services/getActivity.js.map +1 -1
  26. package/dist/esm/types/account.d.ts +11 -0
  27. package/dist/esm/types/account.d.ts.map +1 -1
  28. package/dist/esm/types/account.js.map +1 -1
  29. package/dist/esm/types/ledger.d.ts +65 -1
  30. package/dist/esm/types/ledger.d.ts.map +1 -1
  31. package/dist/esm/types/ledger.js +22 -0
  32. package/dist/esm/types/ledger.js.map +1 -1
  33. package/dist/esm/utils/mapActivity.d.ts +2 -2
  34. package/dist/esm/utils/mapActivity.d.ts.map +1 -1
  35. package/dist/esm/utils/mapActivity.js +56 -45
  36. package/dist/esm/utils/mapActivity.js.map +1 -1
  37. package/dist/esm/utils/mapOrder.d.ts.map +1 -1
  38. package/dist/esm/utils/mapOrder.js +2 -1
  39. package/dist/esm/utils/mapOrder.js.map +1 -1
  40. package/dist/esm/utils/mapPosition.d.ts +3 -1
  41. package/dist/esm/utils/mapPosition.d.ts.map +1 -1
  42. package/dist/esm/utils/mapPosition.js +4 -1
  43. package/dist/esm/utils/mapPosition.js.map +1 -1
  44. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  45. package/dist/esm/websocket/HyperliquidWsProvider.js +2 -1
  46. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  47. package/dist/types/services/getActivity.d.ts.map +1 -1
  48. package/dist/types/types/account.d.ts +11 -0
  49. package/dist/types/types/account.d.ts.map +1 -1
  50. package/dist/types/types/ledger.d.ts +65 -1
  51. package/dist/types/types/ledger.d.ts.map +1 -1
  52. package/dist/types/utils/mapActivity.d.ts +2 -2
  53. package/dist/types/utils/mapActivity.d.ts.map +1 -1
  54. package/dist/types/utils/mapOrder.d.ts.map +1 -1
  55. package/dist/types/utils/mapPosition.d.ts +3 -1
  56. package/dist/types/utils/mapPosition.d.ts.map +1 -1
  57. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  58. package/package.json +3 -3
  59. package/src/services/getActivity.ts +13 -1
  60. package/src/types/account.ts +11 -0
  61. package/src/types/ledger.ts +84 -0
  62. package/src/utils/mapActivity.ts +66 -52
  63. package/src/utils/mapOrder.ts +2 -1
  64. package/src/utils/mapPosition.ts +4 -1
  65. package/src/websocket/HyperliquidWsProvider.ts +2 -1
@@ -9,15 +9,24 @@ import type {
9
9
  } from '@lifi/perps-types'
10
10
  import { ActivityType } from '@lifi/perps-types'
11
11
  import type { HlFundingUpdate, HlLedgerUpdate } from '../types/index.js'
12
- import { isSendAssetDelta, isSpotTransferDelta } from '../types/index.js'
12
+ import {
13
+ isDepositDelta,
14
+ isLiquidationDelta,
15
+ isSendAssetDelta,
16
+ isSpotTransferDelta,
17
+ isWithdrawDelta,
18
+ } from '../types/index.js'
19
+
20
+ /** Hyperliquid settles every perp deposit and withdrawal in USDC. */
21
+ const HL_COLLATERAL_SYMBOL = 'USDC'
13
22
 
14
23
  /**
15
24
  * Map a Hyperliquid non-funding ledger entry to an ActivityItem.
16
25
  *
17
26
  * Direction for `spotTransfer` and `sendAsset` is derived from
18
27
  * `queriedAddress` matching the delta's `user` (OUT) or `destination` (IN).
19
- * Returns null for unsupported delta types and for same-user `sendAsset`
20
- * dex moves (where `user === destination === queriedAddress`).
28
+ * Returns null for unsupported delta types and for same-account moves, where
29
+ * `user === destination === queriedAddress`.
21
30
  * @public
22
31
  */
23
32
  export const mapLedgerEntry = (
@@ -40,6 +49,13 @@ export const mapLedgerEntry = (
40
49
  const queried = queriedAddress.toLowerCase()
41
50
  const sender = delta.user.toLowerCase()
42
51
  const recipient = delta.destination.toLowerCase()
52
+
53
+ // A transfer reports a movement between two accounts. A row whose sender
54
+ // and recipient are the queried account moves nothing between accounts.
55
+ if (sender === recipient && sender === queried) {
56
+ return null
57
+ }
58
+
43
59
  const direction: 'IN' | 'OUT' = queried === sender ? 'OUT' : 'IN'
44
60
  const counterpartyAddress = direction === 'OUT' ? recipient : sender
45
61
  const meta: Record<string, unknown> = {
@@ -110,59 +126,57 @@ export const mapLedgerEntry = (
110
126
  } satisfies TransferActivity
111
127
  }
112
128
 
113
- switch (delta.type) {
114
- case 'deposit':
115
- return {
116
- ...base,
117
- type: ActivityType.DEPOSIT,
118
- amount: delta.usdc ?? '0',
119
- explorerLink: entry.hash
120
- ? `https://scan.li.fi/tx/${entry.hash}`
121
- : undefined,
122
- } satisfies DepositActivity
123
-
124
- case 'withdraw':
125
- return {
126
- ...base,
127
- type: ActivityType.WITHDRAWAL,
128
- amount: delta.usdc ?? '0',
129
- fee: (delta as { fee?: string }).fee ?? '0',
130
- explorerLink: entry.hash
131
- ? `https://scan.li.fi/tx/${entry.hash}`
132
- : undefined,
133
- } satisfies WithdrawalActivity
129
+ if (isDepositDelta(delta)) {
130
+ return {
131
+ ...base,
132
+ type: ActivityType.DEPOSIT,
133
+ asset: HL_COLLATERAL_SYMBOL,
134
+ amount: delta.usdc,
135
+ explorerLink: entry.hash
136
+ ? `https://scan.li.fi/tx/${entry.hash}`
137
+ : undefined,
138
+ } satisfies DepositActivity
139
+ }
134
140
 
135
- case 'liquidation': {
136
- const d = delta as unknown as {
137
- type: string
138
- liquidatedNtlPos: string
139
- accountValue: string
140
- leverageType: string
141
- liquidatedPositions?: { coin: string; szi: string }[]
142
- }
143
- const liquidatedPositions = (d.liquidatedPositions ?? []).map((p) => ({
144
- market: resolveMarket(p.coin),
145
- size: p.szi,
146
- }))
147
- // Liquidation rows must point to at least one market. Drop entries with
148
- // missing/empty positions so downstream consumers can rely on that
149
- // invariant and avoid rendering a market-less liquidation card.
150
- if (liquidatedPositions.length === 0) {
151
- return null
152
- }
153
- return {
154
- ...base,
155
- type: ActivityType.LIQUIDATION,
156
- liquidatedNotionalPosition: d.liquidatedNtlPos,
157
- accountValue: d.accountValue,
158
- leverageType: d.leverageType,
159
- liquidatedPositions,
160
- } satisfies LiquidationActivity
161
- }
141
+ if (isWithdrawDelta(delta)) {
142
+ return {
143
+ ...base,
144
+ type: ActivityType.WITHDRAWAL,
145
+ asset: HL_COLLATERAL_SYMBOL,
146
+ amount: delta.usdc,
147
+ ...(delta.fee === undefined ? {} : { fee: delta.fee }),
148
+ explorerLink: entry.hash
149
+ ? `https://scan.li.fi/tx/${entry.hash}`
150
+ : undefined,
151
+ } satisfies WithdrawalActivity
152
+ }
162
153
 
163
- default:
154
+ if (isLiquidationDelta(delta)) {
155
+ const liquidatedPositions = (delta.liquidatedPositions ?? []).map((p) => ({
156
+ market: resolveMarket(p.coin),
157
+ size: p.szi,
158
+ }))
159
+ // Liquidation rows must point to at least one market. Drop entries with
160
+ // missing/empty positions so downstream consumers can rely on that
161
+ // invariant and avoid rendering a market-less liquidation card.
162
+ if (liquidatedPositions.length === 0) {
164
163
  return null
164
+ }
165
+ return {
166
+ ...base,
167
+ type: ActivityType.LIQUIDATION,
168
+ ...(delta.liquidatedNtlPos === undefined
169
+ ? {}
170
+ : { liquidatedNotionalPosition: delta.liquidatedNtlPos }),
171
+ ...(delta.accountValue === undefined
172
+ ? {}
173
+ : { accountValue: delta.accountValue }),
174
+ leverageType: delta.leverageType,
175
+ liquidatedPositions,
176
+ } satisfies LiquidationActivity
165
177
  }
178
+
179
+ return null
166
180
  }
167
181
 
168
182
  /**
@@ -96,7 +96,8 @@ export const mapOpenOrder = (
96
96
  market,
97
97
  side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
98
98
  type: mapOrderType(o.orderType),
99
- size: o.sz,
99
+ originalSize: o.origSz,
100
+ remainingSize: o.sz,
100
101
  price: o.limitPx,
101
102
  filledSize: o.origSz
102
103
  ? (parseFloat(o.origSz) - parseFloat(o.sz)).toString()
@@ -15,7 +15,9 @@ export const isOpenAssetPosition = (ap: HlAssetPosition): boolean =>
15
15
  /**
16
16
  * Map a non-zero Hyperliquid position payload to the SDK's normalized
17
17
  * position. Signed wire size determines side; decimal strings remain strings
18
- * in the normalized response.
18
+ * in the normalized response. `cumFunding.sinceOpen` is negated because
19
+ * Hyperliquid signs funding paid as positive and `accruedFunding` signs it as
20
+ * negative.
19
21
  * @public
20
22
  */
21
23
  export const mapPosition = (
@@ -41,6 +43,7 @@ export const mapPosition = (
41
43
  markPrice: szi.eq(0) ? '0' : positionValue.div(szi.abs()).toFixed(),
42
44
  liquidationPrice: pos.liquidationPx ?? '0',
43
45
  unrealizedPnl: pos.unrealizedPnl,
46
+ accruedFunding: new Big(pos.cumFunding.sinceOpen).neg().toFixed(),
44
47
  leverage: ap.position.leverage.value,
45
48
  marginUsed,
46
49
  initialMarginRequirement: positionValue.div(leverage).toFixed(),
@@ -1020,7 +1020,8 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
1020
1020
  market,
1021
1021
  side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
1022
1022
  type,
1023
- size: o.sz,
1023
+ originalSize: o.origSz,
1024
+ remainingSize: o.sz,
1024
1025
  price: o.limitPx,
1025
1026
  filledSize: filled.toString(),
1026
1027
  reduceOnly: o.reduceOnly ?? false,