@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.
- package/dist/cjs/services/getActivity.d.ts.map +1 -1
- package/dist/cjs/services/getActivity.js +8 -1
- package/dist/cjs/services/getActivity.js.map +1 -1
- package/dist/cjs/types/account.d.ts +5 -0
- package/dist/cjs/types/account.d.ts.map +1 -1
- package/dist/cjs/types/account.js.map +1 -1
- package/dist/cjs/types/ledger.d.ts +24 -1
- package/dist/cjs/types/ledger.d.ts.map +1 -1
- package/dist/cjs/types/ledger.js +8 -1
- package/dist/cjs/types/ledger.js.map +1 -1
- package/dist/cjs/utils/mapActivity.d.ts.map +1 -1
- package/dist/cjs/utils/mapActivity.js +47 -39
- package/dist/cjs/utils/mapActivity.js.map +1 -1
- package/dist/cjs/utils/mapOrder.d.ts.map +1 -1
- package/dist/cjs/utils/mapOrder.js +2 -1
- package/dist/cjs/utils/mapOrder.js.map +1 -1
- package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
- package/dist/cjs/utils/mapPosition.js +1 -0
- package/dist/cjs/utils/mapPosition.js.map +1 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.js +2 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/esm/services/getActivity.d.ts.map +1 -1
- package/dist/esm/services/getActivity.js +10 -1
- package/dist/esm/services/getActivity.js.map +1 -1
- package/dist/esm/types/account.d.ts +11 -0
- package/dist/esm/types/account.d.ts.map +1 -1
- package/dist/esm/types/account.js.map +1 -1
- package/dist/esm/types/ledger.d.ts +65 -1
- package/dist/esm/types/ledger.d.ts.map +1 -1
- package/dist/esm/types/ledger.js +22 -0
- package/dist/esm/types/ledger.js.map +1 -1
- package/dist/esm/utils/mapActivity.d.ts +2 -2
- package/dist/esm/utils/mapActivity.d.ts.map +1 -1
- package/dist/esm/utils/mapActivity.js +56 -45
- package/dist/esm/utils/mapActivity.js.map +1 -1
- package/dist/esm/utils/mapOrder.d.ts.map +1 -1
- package/dist/esm/utils/mapOrder.js +2 -1
- package/dist/esm/utils/mapOrder.js.map +1 -1
- package/dist/esm/utils/mapPosition.d.ts +3 -1
- package/dist/esm/utils/mapPosition.d.ts.map +1 -1
- package/dist/esm/utils/mapPosition.js +4 -1
- package/dist/esm/utils/mapPosition.js.map +1 -1
- package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/HyperliquidWsProvider.js +2 -1
- package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/types/services/getActivity.d.ts.map +1 -1
- package/dist/types/types/account.d.ts +11 -0
- package/dist/types/types/account.d.ts.map +1 -1
- package/dist/types/types/ledger.d.ts +65 -1
- package/dist/types/types/ledger.d.ts.map +1 -1
- package/dist/types/utils/mapActivity.d.ts +2 -2
- package/dist/types/utils/mapActivity.d.ts.map +1 -1
- package/dist/types/utils/mapOrder.d.ts.map +1 -1
- package/dist/types/utils/mapPosition.d.ts +3 -1
- package/dist/types/utils/mapPosition.d.ts.map +1 -1
- package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/services/getActivity.ts +13 -1
- package/src/types/account.ts +11 -0
- package/src/types/ledger.ts +84 -0
- package/src/utils/mapActivity.ts +66 -52
- package/src/utils/mapOrder.ts +2 -1
- package/src/utils/mapPosition.ts +4 -1
- package/src/websocket/HyperliquidWsProvider.ts +2 -1
package/src/utils/mapActivity.ts
CHANGED
|
@@ -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 {
|
|
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-
|
|
20
|
-
*
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
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
|
/**
|
package/src/utils/mapOrder.ts
CHANGED
|
@@ -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
|
-
|
|
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()
|
package/src/utils/mapPosition.ts
CHANGED
|
@@ -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
|
-
|
|
1023
|
+
originalSize: o.origSz,
|
|
1024
|
+
remainingSize: o.sz,
|
|
1024
1025
|
price: o.limitPx,
|
|
1025
1026
|
filledSize: filled.toString(),
|
|
1026
1027
|
reduceOnly: o.reduceOnly ?? false,
|