@lifi/perps-sdk-provider-hyperliquid 35.1.0 → 36.1.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 +58 -19
- package/dist/cjs/services/fillLeverages.d.ts +6 -0
- package/dist/cjs/services/fillLeverages.d.ts.map +1 -0
- package/dist/cjs/services/fillLeverages.js +29 -0
- package/dist/cjs/services/fillLeverages.js.map +1 -0
- package/dist/cjs/services/getActivity.d.ts.map +1 -1
- package/dist/cjs/services/getActivity.js +33 -17
- package/dist/cjs/services/getActivity.js.map +1 -1
- package/dist/cjs/services/getFills.d.ts +1 -1
- package/dist/cjs/services/getFills.d.ts.map +1 -1
- package/dist/cjs/services/getFills.js +9 -2
- package/dist/cjs/services/getFills.js.map +1 -1
- package/dist/cjs/services/getLedgerHistory.d.ts +17 -0
- package/dist/cjs/services/getLedgerHistory.d.ts.map +1 -0
- package/dist/cjs/services/getLedgerHistory.js +19 -0
- package/dist/cjs/services/getLedgerHistory.js.map +1 -0
- package/dist/cjs/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/cjs/services/getPortfolioHistory.js +15 -1
- package/dist/cjs/services/getPortfolioHistory.js.map +1 -1
- package/dist/cjs/types/ledger.d.ts +25 -1
- package/dist/cjs/types/ledger.d.ts.map +1 -1
- package/dist/cjs/types/ledger.js +16 -1
- package/dist/cjs/types/ledger.js.map +1 -1
- package/dist/cjs/utils/explorer.d.ts.map +1 -1
- package/dist/cjs/utils/explorer.js +2 -1
- package/dist/cjs/utils/explorer.js.map +1 -1
- package/dist/cjs/utils/infoClient.d.ts.map +1 -1
- package/dist/cjs/utils/infoClient.js +2 -1
- package/dist/cjs/utils/infoClient.js.map +1 -1
- package/dist/cjs/utils/mapActivity.d.ts.map +1 -1
- package/dist/cjs/utils/mapActivity.js +65 -22
- package/dist/cjs/utils/mapActivity.js.map +1 -1
- package/dist/cjs/utils/mapFill.d.ts.map +1 -1
- package/dist/cjs/utils/mapFill.js +2 -2
- package/dist/cjs/utils/mapFill.js.map +1 -1
- package/dist/cjs/utils/mapPortfolioTransfers.d.ts +4 -0
- package/dist/cjs/utils/mapPortfolioTransfers.d.ts.map +1 -0
- package/dist/cjs/utils/mapPortfolioTransfers.js +157 -0
- package/dist/cjs/utils/mapPortfolioTransfers.js.map +1 -0
- package/dist/cjs/websocket/HyperliquidWsProvider.d.ts +3 -2
- package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.js +14 -5
- package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/esm/services/fillLeverages.d.ts +14 -0
- package/dist/esm/services/fillLeverages.d.ts.map +1 -0
- package/dist/esm/services/fillLeverages.js +33 -0
- package/dist/esm/services/fillLeverages.js.map +1 -0
- package/dist/esm/services/getActivity.d.ts.map +1 -1
- package/dist/esm/services/getActivity.js +35 -18
- package/dist/esm/services/getActivity.js.map +1 -1
- package/dist/esm/services/getFills.d.ts +3 -2
- package/dist/esm/services/getFills.d.ts.map +1 -1
- package/dist/esm/services/getFills.js +12 -4
- package/dist/esm/services/getFills.js.map +1 -1
- package/dist/esm/services/getLedgerHistory.d.ts +19 -0
- package/dist/esm/services/getLedgerHistory.d.ts.map +1 -0
- package/dist/esm/services/getLedgerHistory.js +16 -0
- package/dist/esm/services/getLedgerHistory.js.map +1 -0
- package/dist/esm/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/esm/services/getPortfolioHistory.js +15 -1
- package/dist/esm/services/getPortfolioHistory.js.map +1 -1
- package/dist/esm/types/ledger.d.ts +45 -1
- package/dist/esm/types/ledger.d.ts.map +1 -1
- package/dist/esm/types/ledger.js +27 -0
- package/dist/esm/types/ledger.js.map +1 -1
- package/dist/esm/utils/explorer.d.ts.map +1 -1
- package/dist/esm/utils/explorer.js +5 -4
- package/dist/esm/utils/explorer.js.map +1 -1
- package/dist/esm/utils/infoClient.d.ts.map +1 -1
- package/dist/esm/utils/infoClient.js +3 -2
- package/dist/esm/utils/infoClient.js.map +1 -1
- package/dist/esm/utils/mapActivity.d.ts +1 -7
- package/dist/esm/utils/mapActivity.d.ts.map +1 -1
- package/dist/esm/utils/mapActivity.js +68 -36
- package/dist/esm/utils/mapActivity.js.map +1 -1
- package/dist/esm/utils/mapFill.d.ts +2 -0
- package/dist/esm/utils/mapFill.d.ts.map +1 -1
- package/dist/esm/utils/mapFill.js +5 -3
- package/dist/esm/utils/mapFill.js.map +1 -1
- package/dist/esm/utils/mapPortfolioTransfers.d.ts +5 -0
- package/dist/esm/utils/mapPortfolioTransfers.d.ts.map +1 -0
- package/dist/esm/utils/mapPortfolioTransfers.js +154 -0
- package/dist/esm/utils/mapPortfolioTransfers.js.map +1 -0
- package/dist/esm/websocket/HyperliquidWsProvider.d.ts +3 -2
- package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/HyperliquidWsProvider.js +16 -6
- package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/types/services/fillLeverages.d.ts +14 -0
- package/dist/types/services/fillLeverages.d.ts.map +1 -0
- package/dist/types/services/getActivity.d.ts.map +1 -1
- package/dist/types/services/getFills.d.ts +3 -2
- package/dist/types/services/getFills.d.ts.map +1 -1
- package/dist/types/services/getLedgerHistory.d.ts +19 -0
- package/dist/types/services/getLedgerHistory.d.ts.map +1 -0
- package/dist/types/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/types/types/ledger.d.ts +45 -1
- package/dist/types/types/ledger.d.ts.map +1 -1
- package/dist/types/utils/explorer.d.ts.map +1 -1
- package/dist/types/utils/infoClient.d.ts.map +1 -1
- package/dist/types/utils/mapActivity.d.ts +1 -7
- package/dist/types/utils/mapActivity.d.ts.map +1 -1
- package/dist/types/utils/mapFill.d.ts +2 -0
- package/dist/types/utils/mapFill.d.ts.map +1 -1
- package/dist/types/utils/mapPortfolioTransfers.d.ts +5 -0
- package/dist/types/utils/mapPortfolioTransfers.d.ts.map +1 -0
- package/dist/types/websocket/HyperliquidWsProvider.d.ts +3 -2
- package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/services/fillLeverages.ts +60 -0
- package/src/services/getActivity.ts +46 -37
- package/src/services/getFills.ts +23 -4
- package/src/services/getLedgerHistory.ts +42 -0
- package/src/services/getPortfolioHistory.ts +28 -4
- package/src/types/ledger.ts +77 -0
- package/src/utils/explorer.ts +7 -3
- package/src/utils/infoClient.ts +5 -2
- package/src/utils/mapActivity.ts +90 -36
- package/src/utils/mapFill.ts +40 -34
- package/src/utils/mapPortfolioTransfers.ts +203 -0
- package/src/websocket/HyperliquidWsProvider.ts +31 -5
package/src/types/ledger.ts
CHANGED
|
@@ -162,6 +162,43 @@ export type HlBorrowLendDelta = {
|
|
|
162
162
|
interestAmount: string
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
/** A priority gas charge in the specified token's units. @public */
|
|
166
|
+
export type HlGossipPriorityGasAuctionDelta = {
|
|
167
|
+
type: 'gossipPriorityGasAuction'
|
|
168
|
+
token: string
|
|
169
|
+
amount: string
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** An account activation charge in the specified token's units. @public */
|
|
173
|
+
export type HlAccountActivationGasDelta = {
|
|
174
|
+
type: 'accountActivationGas'
|
|
175
|
+
token: string
|
|
176
|
+
amount: string
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Claimed reward income in the specified token's units. @public */
|
|
180
|
+
export type HlRewardsClaimDelta = {
|
|
181
|
+
type: 'rewardsClaim'
|
|
182
|
+
token: string
|
|
183
|
+
amount: string
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** USDC movement between one account's spot and perps balances. @public */
|
|
187
|
+
export type HlAccountClassTransferDelta = {
|
|
188
|
+
type: 'accountClassTransfer'
|
|
189
|
+
usdc: string
|
|
190
|
+
toPerp: boolean
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Narrow account-class transfers only when the venue gives a direction and an amount. @public */
|
|
194
|
+
export const isAccountClassTransferDelta = (
|
|
195
|
+
delta: HlLedgerDelta
|
|
196
|
+
): delta is HlAccountClassTransferDelta =>
|
|
197
|
+
delta.type === 'accountClassTransfer' &&
|
|
198
|
+
'toPerp' in delta &&
|
|
199
|
+
typeof delta.toPerp === 'boolean' &&
|
|
200
|
+
hasUsdcAmount(delta)
|
|
201
|
+
|
|
165
202
|
/**
|
|
166
203
|
* Union of known Hyperliquid non-funding ledger deltas plus an open fallback
|
|
167
204
|
* for endpoint variants the provider does not map.
|
|
@@ -177,6 +214,10 @@ export type HlLedgerDelta =
|
|
|
177
214
|
| HlLiquidationDelta
|
|
178
215
|
| HlCStakingTransferDelta
|
|
179
216
|
| HlBorrowLendDelta
|
|
217
|
+
| HlGossipPriorityGasAuctionDelta
|
|
218
|
+
| HlAccountActivationGasDelta
|
|
219
|
+
| HlRewardsClaimDelta
|
|
220
|
+
| HlAccountClassTransferDelta
|
|
180
221
|
| {
|
|
181
222
|
type: string
|
|
182
223
|
usdc?: string
|
|
@@ -263,6 +304,42 @@ export const isBorrowLendDelta = (
|
|
|
263
304
|
'operation' in delta &&
|
|
264
305
|
HL_BORROW_LEND_OPERATIONS.has(delta.operation)
|
|
265
306
|
|
|
307
|
+
const hasTokenAmount = (delta: HlLedgerDelta): boolean =>
|
|
308
|
+
'token' in delta &&
|
|
309
|
+
typeof delta.token === 'string' &&
|
|
310
|
+
'amount' in delta &&
|
|
311
|
+
typeof delta.amount === 'string'
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Type guard for `HlGossipPriorityGasAuctionDelta`. The token and amount are
|
|
315
|
+
* part of the guard: a row without them names no charge.
|
|
316
|
+
* @public
|
|
317
|
+
*/
|
|
318
|
+
export const isGossipPriorityGasAuctionDelta = (
|
|
319
|
+
delta: HlLedgerDelta
|
|
320
|
+
): delta is HlGossipPriorityGasAuctionDelta =>
|
|
321
|
+
delta.type === 'gossipPriorityGasAuction' && hasTokenAmount(delta)
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Type guard for `HlAccountActivationGasDelta`. Same token-and-amount
|
|
325
|
+
* requirement as `isGossipPriorityGasAuctionDelta`.
|
|
326
|
+
* @public
|
|
327
|
+
*/
|
|
328
|
+
export const isAccountActivationGasDelta = (
|
|
329
|
+
delta: HlLedgerDelta
|
|
330
|
+
): delta is HlAccountActivationGasDelta =>
|
|
331
|
+
delta.type === 'accountActivationGas' && hasTokenAmount(delta)
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Type guard for `HlRewardsClaimDelta`. Same token-and-amount requirement as
|
|
335
|
+
* `isGossipPriorityGasAuctionDelta`.
|
|
336
|
+
* @public
|
|
337
|
+
*/
|
|
338
|
+
export const isRewardsClaimDelta = (
|
|
339
|
+
delta: HlLedgerDelta
|
|
340
|
+
): delta is HlRewardsClaimDelta =>
|
|
341
|
+
delta.type === 'rewardsClaim' && hasTokenAmount(delta)
|
|
342
|
+
|
|
266
343
|
/**
|
|
267
344
|
* One timestamped non-funding ledger update. `time` is milliseconds since
|
|
268
345
|
* epoch and `hash` is the upstream transaction identifier.
|
package/src/utils/explorer.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
createWarnOnce,
|
|
3
3
|
ExplorerChainId,
|
|
4
4
|
explorerTxUrl,
|
|
5
|
+
isAbortRejection,
|
|
5
6
|
PerpsError,
|
|
6
7
|
} from '@lifi/perps-sdk'
|
|
7
8
|
import type { Order, OrderBase } from '@lifi/perps-types'
|
|
@@ -119,9 +120,12 @@ export const withExplorerLinks = async (
|
|
|
119
120
|
try {
|
|
120
121
|
txs = await fetchUserTransactions(address, options)
|
|
121
122
|
} catch (error) {
|
|
122
|
-
//
|
|
123
|
-
// not
|
|
124
|
-
if (
|
|
123
|
+
// A caller can abort with a `PerpsError` reason, so the type alone does
|
|
124
|
+
// not tell a cancel from an explorer failure.
|
|
125
|
+
if (
|
|
126
|
+
isAbortRejection(error, options.signal) ||
|
|
127
|
+
!(error instanceof PerpsError)
|
|
128
|
+
) {
|
|
125
129
|
throw error
|
|
126
130
|
}
|
|
127
131
|
// The message stays out of the key: a transport failure names hosts and
|
package/src/utils/infoClient.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
errorCodeFromStatus,
|
|
3
3
|
fetchWithRetry,
|
|
4
|
-
|
|
4
|
+
isAbortRejection,
|
|
5
5
|
PerpsError,
|
|
6
6
|
type PerpsSDKClient,
|
|
7
7
|
type ResolvedRetryPolicy,
|
|
@@ -143,7 +143,10 @@ export async function hlPostJson<T>(
|
|
|
143
143
|
|
|
144
144
|
return (await response.json()) as T
|
|
145
145
|
} catch (error) {
|
|
146
|
-
if (
|
|
146
|
+
if (
|
|
147
|
+
error instanceof PerpsError ||
|
|
148
|
+
isAbortRejection(error, options?.signal)
|
|
149
|
+
) {
|
|
147
150
|
throw error
|
|
148
151
|
}
|
|
149
152
|
const err = new PerpsError(
|
package/src/utils/mapActivity.ts
CHANGED
|
@@ -13,9 +13,13 @@ import type {
|
|
|
13
13
|
Asset,
|
|
14
14
|
DepositActivity,
|
|
15
15
|
Fee,
|
|
16
|
+
FeeActivity,
|
|
16
17
|
FundingActivity,
|
|
18
|
+
InternalTransferActivity,
|
|
19
|
+
InternalTransferBalance,
|
|
17
20
|
LiquidationActivity,
|
|
18
21
|
MarketDisplay,
|
|
22
|
+
RewardActivity,
|
|
19
23
|
TransferActivity,
|
|
20
24
|
WithdrawalActivity,
|
|
21
25
|
} from '@lifi/perps-types'
|
|
@@ -28,11 +32,15 @@ import type {
|
|
|
28
32
|
HlUserFill,
|
|
29
33
|
} from '../types/index.js'
|
|
30
34
|
import {
|
|
35
|
+
isAccountActivationGasDelta,
|
|
36
|
+
isAccountClassTransferDelta,
|
|
31
37
|
isBorrowLendDelta,
|
|
32
38
|
isCollateralTransferDelta,
|
|
33
39
|
isCStakingTransferDelta,
|
|
34
40
|
isDepositDelta,
|
|
41
|
+
isGossipPriorityGasAuctionDelta,
|
|
35
42
|
isLiquidationDelta,
|
|
43
|
+
isRewardsClaimDelta,
|
|
36
44
|
isSendAssetDelta,
|
|
37
45
|
isSpotTransferDelta,
|
|
38
46
|
isVaultTransferDelta,
|
|
@@ -56,15 +64,17 @@ const HL_NATIVE_TOKEN_SYMBOL = 'HYPE'
|
|
|
56
64
|
// Hyperliquid reserves spot token index 0 for USDC.
|
|
57
65
|
const HL_COLLATERAL_ASSET_ID = '0'
|
|
58
66
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
const BORROW_LEND_BALANCES: Record<
|
|
68
|
+
HlBorrowLendOperation,
|
|
69
|
+
{
|
|
70
|
+
sourceBalance: InternalTransferBalance
|
|
71
|
+
destinationBalance: InternalTransferBalance
|
|
72
|
+
}
|
|
73
|
+
> = {
|
|
74
|
+
supply: { sourceBalance: 'spot', destinationBalance: 'borrowLend' },
|
|
75
|
+
withdraw: { sourceBalance: 'borrowLend', destinationBalance: 'spot' },
|
|
76
|
+
borrow: { sourceBalance: 'borrowLend', destinationBalance: 'spot' },
|
|
77
|
+
repay: { sourceBalance: 'spot', destinationBalance: 'borrowLend' },
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/**
|
|
@@ -91,13 +101,7 @@ const resolveLedgerAsset = (
|
|
|
91
101
|
return asset
|
|
92
102
|
}
|
|
93
103
|
|
|
94
|
-
/**
|
|
95
|
-
* Map supported ledger entries; exclude same-account moves and unsupported types.
|
|
96
|
-
* Staking and borrow-lend moves map with the queried account as counterparty.
|
|
97
|
-
* Missing collateral throws; an unregistered spot token skips the row, and
|
|
98
|
-
* unresolved liquidation markets omit only those positions.
|
|
99
|
-
* @public
|
|
100
|
-
*/
|
|
104
|
+
/** Map supported ledger entries, preserving venue amounts and operation metadata. */
|
|
101
105
|
export const mapLedgerEntry = (
|
|
102
106
|
entry: HlLedgerUpdate,
|
|
103
107
|
providerKey: string,
|
|
@@ -176,39 +180,59 @@ export const mapLedgerEntry = (
|
|
|
176
180
|
} satisfies TransferActivity
|
|
177
181
|
}
|
|
178
182
|
|
|
179
|
-
if (
|
|
180
|
-
|
|
183
|
+
if (
|
|
184
|
+
isCStakingTransferDelta(delta) ||
|
|
185
|
+
isBorrowLendDelta(delta) ||
|
|
186
|
+
isAccountClassTransferDelta(delta)
|
|
187
|
+
) {
|
|
188
|
+
const accountClass = isAccountClassTransferDelta(delta)
|
|
189
|
+
const staking = isCStakingTransferDelta(delta)
|
|
190
|
+
const asset = accountClass
|
|
191
|
+
? assetRegistry.require(HL_COLLATERAL_ASSET_ID)
|
|
192
|
+
: resolveLedgerAsset(delta.token, assetRegistry, providerKey)
|
|
181
193
|
if (asset === null) {
|
|
182
194
|
return null
|
|
183
195
|
}
|
|
184
|
-
|
|
185
|
-
|
|
196
|
+
const balances = accountClass
|
|
197
|
+
? ({
|
|
198
|
+
sourceBalance: delta.toPerp ? 'spot' : 'perps',
|
|
199
|
+
destinationBalance: delta.toPerp ? 'perps' : 'spot',
|
|
200
|
+
} as const)
|
|
201
|
+
: staking
|
|
202
|
+
? ({
|
|
203
|
+
sourceBalance: delta.isDeposit ? 'spot' : 'staking',
|
|
204
|
+
destinationBalance: delta.isDeposit ? 'staking' : 'spot',
|
|
205
|
+
} as const)
|
|
206
|
+
: BORROW_LEND_BALANCES[delta.operation]
|
|
207
|
+
const amount = accountClass ? delta.usdc : delta.amount
|
|
208
|
+
const token = accountClass ? HL_COLLATERAL_SYMBOL : delta.token
|
|
209
|
+
// Preserve the staking zero-hash identity's IN/OUT segment.
|
|
210
|
+
const action = accountClass
|
|
211
|
+
? `${balances.sourceBalance}:${balances.destinationBalance}`
|
|
212
|
+
: staking
|
|
213
|
+
? delta.isDeposit
|
|
214
|
+
? 'OUT'
|
|
215
|
+
: 'IN'
|
|
216
|
+
: delta.operation
|
|
186
217
|
const hasTransaction = entry.hash !== zeroHash
|
|
187
|
-
const direction = isCStakingTransferDelta(delta)
|
|
188
|
-
? delta.isDeposit
|
|
189
|
-
? 'OUT'
|
|
190
|
-
: 'IN'
|
|
191
|
-
: BORROW_LEND_DIRECTION[delta.operation]
|
|
192
|
-
const action = isCStakingTransferDelta(delta) ? direction : delta.operation
|
|
193
218
|
return {
|
|
194
219
|
...base,
|
|
195
220
|
...(hasTransaction
|
|
196
221
|
? {}
|
|
197
222
|
: {
|
|
198
|
-
id: `${delta.type}:${
|
|
223
|
+
id: `${delta.type}:${token}:${action}:${amount}:${base.timestamp}`,
|
|
199
224
|
}),
|
|
200
|
-
type: ActivityType.
|
|
201
|
-
|
|
202
|
-
counterpartyAddress: queriedAddress.toLowerCase(),
|
|
225
|
+
type: ActivityType.INTERNAL_TRANSFER,
|
|
226
|
+
...balances,
|
|
203
227
|
asset,
|
|
204
|
-
amount
|
|
205
|
-
meta:
|
|
206
|
-
? {
|
|
207
|
-
: {
|
|
228
|
+
amount,
|
|
229
|
+
meta: isBorrowLendDelta(delta)
|
|
230
|
+
? {
|
|
208
231
|
transferType: delta.type,
|
|
209
232
|
operation: delta.operation,
|
|
210
233
|
interestAmount: delta.interestAmount,
|
|
211
|
-
}
|
|
234
|
+
}
|
|
235
|
+
: { transferType: delta.type },
|
|
212
236
|
...(hasTransaction
|
|
213
237
|
? {
|
|
214
238
|
explorerLink: explorerTxUrl(
|
|
@@ -217,7 +241,37 @@ export const mapLedgerEntry = (
|
|
|
217
241
|
),
|
|
218
242
|
}
|
|
219
243
|
: {}),
|
|
220
|
-
} satisfies
|
|
244
|
+
} satisfies InternalTransferActivity
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (
|
|
248
|
+
isGossipPriorityGasAuctionDelta(delta) ||
|
|
249
|
+
isAccountActivationGasDelta(delta) ||
|
|
250
|
+
isRewardsClaimDelta(delta)
|
|
251
|
+
) {
|
|
252
|
+
const asset = resolveLedgerAsset(delta.token, assetRegistry, providerKey)
|
|
253
|
+
if (asset === null) {
|
|
254
|
+
return null
|
|
255
|
+
}
|
|
256
|
+
const hasTransaction = entry.hash !== zeroHash
|
|
257
|
+
return {
|
|
258
|
+
...base,
|
|
259
|
+
id: hasTransaction
|
|
260
|
+
? entry.hash
|
|
261
|
+
: `${delta.type}:${delta.token}:${delta.amount}:${timestamp}`,
|
|
262
|
+
type: isRewardsClaimDelta(delta) ? ActivityType.REWARD : ActivityType.FEE,
|
|
263
|
+
asset,
|
|
264
|
+
amount: delta.amount,
|
|
265
|
+
meta: { ledgerType: delta.type },
|
|
266
|
+
...(hasTransaction
|
|
267
|
+
? {
|
|
268
|
+
explorerLink: explorerTxUrl(
|
|
269
|
+
ExplorerChainId.HYPERLIQUID,
|
|
270
|
+
entry.hash
|
|
271
|
+
),
|
|
272
|
+
}
|
|
273
|
+
: {}),
|
|
274
|
+
} satisfies FeeActivity | RewardActivity
|
|
221
275
|
}
|
|
222
276
|
|
|
223
277
|
if (isVaultTransferDelta(delta)) {
|
package/src/utils/mapFill.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildFill,
|
|
2
3
|
classifyFillFromPosition,
|
|
3
4
|
ExplorerChainId,
|
|
4
5
|
explorerTxUrl,
|
|
@@ -52,6 +53,8 @@ const mapFillLiquidation = (
|
|
|
52
53
|
|
|
53
54
|
/** Maps a fill using the market category; the order type stays absent.
|
|
54
55
|
* Invalid times return `undefined`. The venue does not report the order type.
|
|
56
|
+
* A closing fill gets `closedSize` and `entryPrice`, but no `returnOnEquity`:
|
|
57
|
+
* the fill carries no leverage.
|
|
55
58
|
* @param address - Fill owner; only the liquidated account carries `liquidation`.
|
|
56
59
|
* @public
|
|
57
60
|
*/
|
|
@@ -74,39 +77,42 @@ export const mapFill = (
|
|
|
74
77
|
const side = fill.side === 'B' ? OrderSide.BUY : OrderSide.SELL
|
|
75
78
|
const liquidation = mapFillLiquidation(fill, address)
|
|
76
79
|
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
80
|
+
return buildFill(
|
|
81
|
+
{
|
|
82
|
+
...(isSpotCategory(market.categoryId)
|
|
83
|
+
? { realizedPnl: fill.closedPnl }
|
|
84
|
+
: { realizedPnl: fill.closedPnl === '0' ? null : fill.closedPnl }),
|
|
85
|
+
id: String(fill.tid),
|
|
86
|
+
orderId: String(fill.oid),
|
|
87
|
+
clientOrderId: fill.cloid,
|
|
88
|
+
market,
|
|
89
|
+
side,
|
|
90
|
+
size,
|
|
91
|
+
price: fill.px,
|
|
92
|
+
liquidity: fill.crossed ? LiquidityRole.TAKER : LiquidityRole.MAKER,
|
|
93
|
+
filledSize: size,
|
|
94
|
+
fee: {
|
|
95
|
+
amount: fill.fee,
|
|
96
|
+
asset: feeAsset,
|
|
97
|
+
},
|
|
98
|
+
builderFee:
|
|
99
|
+
fill.builderFee === undefined
|
|
100
|
+
? undefined
|
|
101
|
+
: { amount: fill.builderFee, asset: feeAsset },
|
|
102
|
+
startPosition: fill.startPosition,
|
|
103
|
+
// A TWAP slice fill carries the zero hash: no venue transaction to link.
|
|
104
|
+
explorerLink: explorerTxUrl(
|
|
105
|
+
ExplorerChainId.HYPERLIQUID,
|
|
106
|
+
fill.hash === zeroHash ? undefined : fill.hash
|
|
107
|
+
),
|
|
108
|
+
classification: isSpotCategory(market.categoryId)
|
|
109
|
+
? side === OrderSide.BUY
|
|
110
|
+
? FillClassification.SPOT_BUY
|
|
111
|
+
: FillClassification.SPOT_SELL
|
|
112
|
+
: classifyFillFromPosition(fill.startPosition, side, size),
|
|
113
|
+
createdAt,
|
|
114
|
+
...(liquidation === undefined ? {} : { liquidation }),
|
|
93
115
|
},
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
? undefined
|
|
97
|
-
: { amount: fill.builderFee, asset: feeAsset },
|
|
98
|
-
startPosition: fill.startPosition,
|
|
99
|
-
// A TWAP slice fill carries the zero hash: no venue transaction to link.
|
|
100
|
-
explorerLink: explorerTxUrl(
|
|
101
|
-
ExplorerChainId.HYPERLIQUID,
|
|
102
|
-
fill.hash === zeroHash ? undefined : fill.hash
|
|
103
|
-
),
|
|
104
|
-
classification: isSpotCategory(market.categoryId)
|
|
105
|
-
? side === OrderSide.BUY
|
|
106
|
-
? FillClassification.SPOT_BUY
|
|
107
|
-
: FillClassification.SPOT_SELL
|
|
108
|
-
: classifyFillFromPosition(fill.startPosition, side, size),
|
|
109
|
-
createdAt,
|
|
110
|
-
...(liquidation === undefined ? {} : { liquidation }),
|
|
111
|
-
}
|
|
116
|
+
undefined
|
|
117
|
+
)
|
|
112
118
|
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDecimalString,
|
|
3
|
+
safeAddDecimalString,
|
|
4
|
+
safeIsDecimalStringZero,
|
|
5
|
+
safeSubtractDecimalString,
|
|
6
|
+
} from '@lifi/perps-sdk'
|
|
7
|
+
import type { PortfolioHistoryPoint } from '@lifi/perps-types'
|
|
8
|
+
import { isAddress } from 'viem'
|
|
9
|
+
import {
|
|
10
|
+
type HlLedgerDelta,
|
|
11
|
+
type HlLedgerUpdate,
|
|
12
|
+
isBorrowLendDelta,
|
|
13
|
+
isCollateralTransferDelta,
|
|
14
|
+
isCStakingTransferDelta,
|
|
15
|
+
isDepositDelta,
|
|
16
|
+
isSendAssetDelta,
|
|
17
|
+
isSpotTransferDelta,
|
|
18
|
+
isVaultTransferDelta,
|
|
19
|
+
isWithdrawDelta,
|
|
20
|
+
} from '../types/index.js'
|
|
21
|
+
|
|
22
|
+
const NON_CAPITAL_DELTAS: Readonly<Record<string, true>> = {
|
|
23
|
+
funding: true,
|
|
24
|
+
liquidation: true,
|
|
25
|
+
rewardsClaim: true,
|
|
26
|
+
vaultLeaderCommission: true,
|
|
27
|
+
deployGasAuction: true,
|
|
28
|
+
gossipPriorityGasAuction: true,
|
|
29
|
+
accountActivationGas: true,
|
|
30
|
+
spotGenesis: true,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const parseUnsignedAmount = (value: unknown): string | undefined =>
|
|
34
|
+
isDecimalString(value) && !value.startsWith('-') ? value : undefined
|
|
35
|
+
|
|
36
|
+
const parseSignedAmount = (
|
|
37
|
+
value: unknown,
|
|
38
|
+
incoming: boolean
|
|
39
|
+
): string | undefined => {
|
|
40
|
+
const amount = parseUnsignedAmount(value)
|
|
41
|
+
return amount === undefined
|
|
42
|
+
? undefined
|
|
43
|
+
: incoming
|
|
44
|
+
? amount
|
|
45
|
+
: safeSubtractDecimalString('0', amount)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const parseTokenUsd = (delta: {
|
|
49
|
+
token: string
|
|
50
|
+
amount: string
|
|
51
|
+
usdcValue?: string
|
|
52
|
+
}): string | undefined => {
|
|
53
|
+
const amount = parseUnsignedAmount(delta.amount)
|
|
54
|
+
if (amount === undefined) {
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
return delta.token.split(':')[0] === 'USDC'
|
|
58
|
+
? amount
|
|
59
|
+
: parseUnsignedAmount(delta.usdcValue)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const classifyCapitalMovement = (
|
|
63
|
+
delta: HlLedgerDelta,
|
|
64
|
+
queried: string,
|
|
65
|
+
combined: boolean
|
|
66
|
+
): string | undefined => {
|
|
67
|
+
if (isDepositDelta(delta)) {
|
|
68
|
+
return parseSignedAmount(delta.usdc, true)
|
|
69
|
+
}
|
|
70
|
+
if (isWithdrawDelta(delta)) {
|
|
71
|
+
return parseSignedAmount(delta.usdc, false)
|
|
72
|
+
}
|
|
73
|
+
if (
|
|
74
|
+
isCollateralTransferDelta(delta) ||
|
|
75
|
+
isSpotTransferDelta(delta) ||
|
|
76
|
+
isSendAssetDelta(delta)
|
|
77
|
+
) {
|
|
78
|
+
if (
|
|
79
|
+
!isAddress(delta.user, { strict: false }) ||
|
|
80
|
+
!isAddress(delta.destination, { strict: false })
|
|
81
|
+
) {
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
const sender = delta.user.toLowerCase()
|
|
85
|
+
const recipient = delta.destination.toLowerCase()
|
|
86
|
+
let sourceIncluded = combined || !isSpotTransferDelta(delta)
|
|
87
|
+
let destinationIncluded = sourceIncluded
|
|
88
|
+
if (isSendAssetDelta(delta)) {
|
|
89
|
+
sourceIncluded = combined || delta.sourceDex !== 'spot'
|
|
90
|
+
destinationIncluded = combined || delta.destinationDex !== 'spot'
|
|
91
|
+
}
|
|
92
|
+
const outgoing = sender === queried && sourceIncluded
|
|
93
|
+
const incoming = recipient === queried && destinationIncluded
|
|
94
|
+
if (incoming === outgoing) {
|
|
95
|
+
return '0'
|
|
96
|
+
}
|
|
97
|
+
return parseSignedAmount(
|
|
98
|
+
isCollateralTransferDelta(delta) ? delta.usdc : parseTokenUsd(delta),
|
|
99
|
+
incoming
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
if (isVaultTransferDelta(delta)) {
|
|
103
|
+
if (!isAddress(delta.vault, { strict: false })) {
|
|
104
|
+
return undefined
|
|
105
|
+
}
|
|
106
|
+
const vault = delta.vault.toLowerCase()
|
|
107
|
+
if (delta.type === 'vaultDeposit') {
|
|
108
|
+
return vault === queried
|
|
109
|
+
? undefined
|
|
110
|
+
: parseSignedAmount(delta.usdc, false)
|
|
111
|
+
}
|
|
112
|
+
if (!isAddress(delta.user, { strict: false })) {
|
|
113
|
+
return undefined
|
|
114
|
+
}
|
|
115
|
+
const user = delta.user.toLowerCase()
|
|
116
|
+
if (vault === queried && user === queried) {
|
|
117
|
+
return '0'
|
|
118
|
+
}
|
|
119
|
+
if (vault === queried) {
|
|
120
|
+
return parseSignedAmount(delta.requestedUsd, false)
|
|
121
|
+
}
|
|
122
|
+
return user === queried
|
|
123
|
+
? parseSignedAmount(delta.netWithdrawnUsd, true)
|
|
124
|
+
: undefined
|
|
125
|
+
}
|
|
126
|
+
if (delta.type === 'cStakingTransfer' || delta.type === 'borrowLend') {
|
|
127
|
+
if (!combined) {
|
|
128
|
+
return '0'
|
|
129
|
+
}
|
|
130
|
+
if (isCStakingTransferDelta(delta)) {
|
|
131
|
+
return parseSignedAmount(parseTokenUsd(delta), !delta.isDeposit)
|
|
132
|
+
}
|
|
133
|
+
if (!isBorrowLendDelta(delta)) {
|
|
134
|
+
return undefined
|
|
135
|
+
}
|
|
136
|
+
const interest = parseUnsignedAmount(delta.interestAmount)
|
|
137
|
+
if (interest === undefined) {
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
const releases =
|
|
141
|
+
delta.operation === 'withdraw' || delta.operation === 'borrow'
|
|
142
|
+
let amount = parseTokenUsd(delta)
|
|
143
|
+
if (
|
|
144
|
+
amount !== undefined &&
|
|
145
|
+
(delta.operation === 'withdraw' || delta.operation === 'repay') &&
|
|
146
|
+
!safeIsDecimalStringZero(interest)
|
|
147
|
+
) {
|
|
148
|
+
amount = safeSubtractDecimalString(amount, interest)
|
|
149
|
+
}
|
|
150
|
+
return parseSignedAmount(amount, releases)
|
|
151
|
+
}
|
|
152
|
+
if (delta.type === 'accountClassTransfer') {
|
|
153
|
+
if (combined) {
|
|
154
|
+
return '0'
|
|
155
|
+
}
|
|
156
|
+
return typeof delta.toPerp === 'boolean'
|
|
157
|
+
? parseSignedAmount(delta.usdc, delta.toPerp)
|
|
158
|
+
: undefined
|
|
159
|
+
}
|
|
160
|
+
if (delta.type === 'vaultCreate') {
|
|
161
|
+
return typeof delta.vault === 'string' &&
|
|
162
|
+
isAddress(delta.vault, { strict: false }) &&
|
|
163
|
+
delta.vault.toLowerCase() !== queried
|
|
164
|
+
? parseSignedAmount(delta.usdc, false)
|
|
165
|
+
: undefined
|
|
166
|
+
}
|
|
167
|
+
return Object.hasOwn(NON_CAPITAL_DELTAS, delta.type) ? '0' : undefined
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Add cumulative USD flows for the selected spot-and-perps or perps-only balances. */
|
|
171
|
+
export const mapPortfolioTransfers = (
|
|
172
|
+
points: PortfolioHistoryPoint[],
|
|
173
|
+
ledger: HlLedgerUpdate[],
|
|
174
|
+
address: string,
|
|
175
|
+
combined: boolean
|
|
176
|
+
): PortfolioHistoryPoint[] => {
|
|
177
|
+
const first = points[0]
|
|
178
|
+
if (first === undefined) {
|
|
179
|
+
return []
|
|
180
|
+
}
|
|
181
|
+
const ordered = ledger
|
|
182
|
+
.filter((entry) => entry.time > first.timestamp)
|
|
183
|
+
.sort((a, b) => a.time - b.time)
|
|
184
|
+
const queried = address.toLowerCase()
|
|
185
|
+
let index = 0
|
|
186
|
+
let cumulative: string | undefined = '0'
|
|
187
|
+
return points.map((point) => {
|
|
188
|
+
while (index < ordered.length && ordered[index].time <= point.timestamp) {
|
|
189
|
+
const entry = ordered[index++]
|
|
190
|
+
if (cumulative !== undefined) {
|
|
191
|
+
const movement = classifyCapitalMovement(entry.delta, queried, combined)
|
|
192
|
+
cumulative =
|
|
193
|
+
movement === undefined
|
|
194
|
+
? undefined
|
|
195
|
+
: safeAddDecimalString(cumulative, movement)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
...point,
|
|
200
|
+
...(cumulative === undefined ? {} : { netTransfers: cumulative }),
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
}
|