@lifi/perps-sdk-provider-hyperliquid 39.0.2 → 39.1.1
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 +39 -9
- package/dist/cjs/HyperliquidProvider.d.ts.map +1 -1
- package/dist/cjs/HyperliquidProvider.js +3 -1
- package/dist/cjs/HyperliquidProvider.js.map +1 -1
- package/dist/cjs/services/getActivity.d.ts.map +1 -1
- package/dist/cjs/services/getActivity.js +6 -2
- package/dist/cjs/services/getActivity.js.map +1 -1
- package/dist/cjs/services/getMarginHealth.d.ts +10 -0
- package/dist/cjs/services/getMarginHealth.d.ts.map +1 -0
- package/dist/cjs/services/getMarginHealth.js +92 -0
- package/dist/cjs/services/getMarginHealth.js.map +1 -0
- package/dist/cjs/services/getPortfolioHistory.d.ts +2 -1
- package/dist/cjs/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/cjs/services/getPortfolioHistory.js +78 -28
- package/dist/cjs/services/getPortfolioHistory.js.map +1 -1
- package/dist/cjs/utils/mapActivity.d.ts +4 -0
- package/dist/cjs/utils/mapActivity.d.ts.map +1 -1
- package/dist/cjs/utils/mapActivity.js +40 -3
- package/dist/cjs/utils/mapActivity.js.map +1 -1
- package/dist/esm/HyperliquidProvider.d.ts.map +1 -1
- package/dist/esm/HyperliquidProvider.js +3 -1
- package/dist/esm/HyperliquidProvider.js.map +1 -1
- package/dist/esm/services/getActivity.d.ts.map +1 -1
- package/dist/esm/services/getActivity.js +7 -3
- package/dist/esm/services/getActivity.js.map +1 -1
- package/dist/esm/services/getMarginHealth.d.ts +26 -0
- package/dist/esm/services/getMarginHealth.d.ts.map +1 -0
- package/dist/esm/services/getMarginHealth.js +108 -0
- package/dist/esm/services/getMarginHealth.js.map +1 -0
- package/dist/esm/services/getPortfolioHistory.d.ts +7 -2
- package/dist/esm/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/esm/services/getPortfolioHistory.js +95 -31
- package/dist/esm/services/getPortfolioHistory.js.map +1 -1
- package/dist/esm/utils/mapActivity.d.ts +5 -0
- package/dist/esm/utils/mapActivity.d.ts.map +1 -1
- package/dist/esm/utils/mapActivity.js +42 -4
- package/dist/esm/utils/mapActivity.js.map +1 -1
- package/dist/types/HyperliquidProvider.d.ts.map +1 -1
- package/dist/types/services/getActivity.d.ts.map +1 -1
- package/dist/types/services/getMarginHealth.d.ts +26 -0
- package/dist/types/services/getMarginHealth.d.ts.map +1 -0
- package/dist/types/services/getPortfolioHistory.d.ts +7 -2
- package/dist/types/services/getPortfolioHistory.d.ts.map +1 -1
- package/dist/types/utils/mapActivity.d.ts +5 -0
- package/dist/types/utils/mapActivity.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/HyperliquidProvider.ts +14 -5
- package/src/services/getActivity.ts +9 -3
- package/src/services/getMarginHealth.ts +193 -0
- package/src/services/getPortfolioHistory.ts +137 -48
- package/src/utils/mapActivity.ts +54 -7
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addDecimalString,
|
|
3
|
+
divideDecimalString,
|
|
4
|
+
getMarketRegistry,
|
|
5
|
+
isDecimalStringGreaterThan,
|
|
6
|
+
PerpsError,
|
|
7
|
+
type SDKRequestOptions,
|
|
8
|
+
subtractDecimalString,
|
|
9
|
+
} from '@lifi/perps-sdk'
|
|
10
|
+
import {
|
|
11
|
+
type DecimalString,
|
|
12
|
+
type MarginHealth,
|
|
13
|
+
PerpsErrorCode,
|
|
14
|
+
} from '@lifi/perps-types'
|
|
15
|
+
import type { Address } from 'viem'
|
|
16
|
+
import { MAIN_DEX_NAME, MAIN_MARKET_ID, PROVIDER_KEY } from '../constants.js'
|
|
17
|
+
import type { HyperliquidContext } from '../context.js'
|
|
18
|
+
import {
|
|
19
|
+
HlAbstractionMode,
|
|
20
|
+
type HlClearinghouseState,
|
|
21
|
+
type HlSpotClearinghouseState,
|
|
22
|
+
} from '../types/index.js'
|
|
23
|
+
import { perpsDexNames } from '../utils/index.js'
|
|
24
|
+
import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
|
|
25
|
+
|
|
26
|
+
// Cross positions liquidate when account value falls below maintenance margin
|
|
27
|
+
// (hyperliquid-docs/trading/margining).
|
|
28
|
+
const LIQUIDATION_THRESHOLD = '1'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Parameters for {@link getMarginHealth}.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export interface GetMarginHealthParams {
|
|
36
|
+
address: Address
|
|
37
|
+
/** The dex `categoryId` in Standard mode. Defaults to the main dex. */
|
|
38
|
+
categoryId?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Exposure without positive equity has no ratio. */
|
|
42
|
+
const calculateRatio = (
|
|
43
|
+
maintenanceMargin: DecimalString,
|
|
44
|
+
equity: DecimalString
|
|
45
|
+
): DecimalString | undefined => {
|
|
46
|
+
if (!isDecimalStringGreaterThan(maintenanceMargin, '0')) {
|
|
47
|
+
return '0'
|
|
48
|
+
}
|
|
49
|
+
return isDecimalStringGreaterThan(equity, '0')
|
|
50
|
+
? divideDecimalString(maintenanceMargin, equity)
|
|
51
|
+
: undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `computeUnifiedAccountRatio` from hyperliquid-docs
|
|
56
|
+
* trading/account-abstraction-modes#unified-account-ratio: the largest ratio
|
|
57
|
+
* over the collateral tokens.
|
|
58
|
+
*/
|
|
59
|
+
const calculateUnifiedRatio = (
|
|
60
|
+
states: readonly HlClearinghouseState[],
|
|
61
|
+
dexTokens: readonly string[],
|
|
62
|
+
spotState: HlSpotClearinghouseState
|
|
63
|
+
): DecimalString | undefined => {
|
|
64
|
+
const crossMarginByToken = new Map<string, DecimalString>()
|
|
65
|
+
const isolatedMarginByToken = new Map<string, DecimalString>()
|
|
66
|
+
states.forEach((state, i) => {
|
|
67
|
+
const token = dexTokens[i]
|
|
68
|
+
crossMarginByToken.set(
|
|
69
|
+
token,
|
|
70
|
+
addDecimalString(
|
|
71
|
+
crossMarginByToken.get(token) ?? '0',
|
|
72
|
+
state.crossMaintenanceMarginUsed
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
for (const { position } of state.assetPositions) {
|
|
76
|
+
if (position.leverage.type === 'isolated') {
|
|
77
|
+
isolatedMarginByToken.set(
|
|
78
|
+
token,
|
|
79
|
+
addDecimalString(
|
|
80
|
+
isolatedMarginByToken.get(token) ?? '0',
|
|
81
|
+
position.marginUsed
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
let largest: DecimalString = '0'
|
|
89
|
+
for (const [token, crossMargin] of crossMarginByToken) {
|
|
90
|
+
const spotTotal =
|
|
91
|
+
spotState.balances.find((b) => String(b.token) === token)?.total ?? '0'
|
|
92
|
+
const ratio = calculateRatio(
|
|
93
|
+
crossMargin,
|
|
94
|
+
subtractDecimalString(spotTotal, isolatedMarginByToken.get(token) ?? '0')
|
|
95
|
+
)
|
|
96
|
+
if (ratio === undefined) {
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
99
|
+
if (isDecimalStringGreaterThan(ratio, largest)) {
|
|
100
|
+
largest = ratio
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return largest
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Cross-margin health for the account's abstraction mode. Standard mode reads
|
|
108
|
+
* the cross pool of one dex. Unified mode reads the whole account. Portfolio
|
|
109
|
+
* margin and DEX abstraction resolve `undefined`: the account read carries no
|
|
110
|
+
* input for their venue metric.
|
|
111
|
+
*
|
|
112
|
+
* @throws {PerpsError} On a `categoryId` that is not a perps dex, Hyperliquid
|
|
113
|
+
* REST error, network, or parsing failures.
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
export const getMarginHealth = async (
|
|
117
|
+
{ client, apiUrl }: HyperliquidContext,
|
|
118
|
+
params: GetMarginHealthParams,
|
|
119
|
+
options?: SDKRequestOptions
|
|
120
|
+
): Promise<MarginHealth | undefined> => {
|
|
121
|
+
const infoOpts = hlInfoOptions(client, options)
|
|
122
|
+
const [abstraction, markets] = await Promise.all([
|
|
123
|
+
infoRequest<HlAbstractionMode | null>(
|
|
124
|
+
apiUrl,
|
|
125
|
+
{ type: 'userAbstraction', user: params.address },
|
|
126
|
+
infoOpts
|
|
127
|
+
),
|
|
128
|
+
getMarketRegistry(client, PROVIDER_KEY).sync(),
|
|
129
|
+
])
|
|
130
|
+
const fetchState = (dex: string) =>
|
|
131
|
+
infoRequest<HlClearinghouseState>(
|
|
132
|
+
apiUrl,
|
|
133
|
+
{
|
|
134
|
+
type: 'clearinghouseState',
|
|
135
|
+
user: params.address,
|
|
136
|
+
...(dex ? { dex } : {}),
|
|
137
|
+
},
|
|
138
|
+
infoOpts
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
switch (abstraction) {
|
|
142
|
+
case null:
|
|
143
|
+
case HlAbstractionMode.DEFAULT:
|
|
144
|
+
case HlAbstractionMode.DISABLED: {
|
|
145
|
+
const categoryId = params.categoryId ?? MAIN_MARKET_ID
|
|
146
|
+
const dex = categoryId === MAIN_MARKET_ID ? MAIN_DEX_NAME : categoryId
|
|
147
|
+
if (!perpsDexNames(markets).includes(dex)) {
|
|
148
|
+
const err = new PerpsError(
|
|
149
|
+
PerpsErrorCode.ValidationError,
|
|
150
|
+
`Hyperliquid has no perps dex with categoryId '${categoryId}'.`
|
|
151
|
+
)
|
|
152
|
+
err.tool = PROVIDER_KEY
|
|
153
|
+
throw err
|
|
154
|
+
}
|
|
155
|
+
const state = await fetchState(dex)
|
|
156
|
+
const ratio = calculateRatio(
|
|
157
|
+
state.crossMaintenanceMarginUsed,
|
|
158
|
+
state.crossMarginSummary.accountValue
|
|
159
|
+
)
|
|
160
|
+
return ratio === undefined
|
|
161
|
+
? undefined
|
|
162
|
+
: { ratio, liquidationThreshold: LIQUIDATION_THRESHOLD }
|
|
163
|
+
}
|
|
164
|
+
case HlAbstractionMode.UNIFIED_ACCOUNT: {
|
|
165
|
+
const tokenByDex = new Map(
|
|
166
|
+
markets.map((m) => [
|
|
167
|
+
m.categoryId === MAIN_MARKET_ID ? MAIN_DEX_NAME : m.categoryId,
|
|
168
|
+
m.quoteAsset.id,
|
|
169
|
+
])
|
|
170
|
+
)
|
|
171
|
+
const dexNames = perpsDexNames(markets)
|
|
172
|
+
const [states, spotState] = await Promise.all([
|
|
173
|
+
Promise.all(dexNames.map(fetchState)),
|
|
174
|
+
infoRequest<HlSpotClearinghouseState>(
|
|
175
|
+
apiUrl,
|
|
176
|
+
{ type: 'spotClearinghouseState', user: params.address },
|
|
177
|
+
infoOpts
|
|
178
|
+
),
|
|
179
|
+
])
|
|
180
|
+
const ratio = calculateUnifiedRatio(
|
|
181
|
+
states,
|
|
182
|
+
// Always present: `perpsDexNames` reads the same markets.
|
|
183
|
+
dexNames.map((dex) => tokenByDex.get(dex)!),
|
|
184
|
+
spotState
|
|
185
|
+
)
|
|
186
|
+
return ratio === undefined
|
|
187
|
+
? undefined
|
|
188
|
+
: { ratio, liquidationThreshold: LIQUIDATION_THRESHOLD }
|
|
189
|
+
}
|
|
190
|
+
default:
|
|
191
|
+
return undefined
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildPortfolioHistoryWindow,
|
|
3
|
+
createWarnOnce,
|
|
2
4
|
PerpsError,
|
|
5
|
+
type PortfolioHistoryWindow,
|
|
3
6
|
type SDKRequestOptions,
|
|
4
7
|
warnSkippedVenueRow,
|
|
5
8
|
} from '@lifi/perps-sdk'
|
|
6
9
|
import {
|
|
7
10
|
type DecimalString,
|
|
8
11
|
PerpsErrorCode,
|
|
12
|
+
type PortfolioHistoryBounds,
|
|
9
13
|
type PortfolioHistoryPoint,
|
|
10
14
|
type PortfolioHistoryRange,
|
|
11
15
|
type PortfolioHistoryResponse,
|
|
@@ -29,23 +33,27 @@ import { getLedgerHistory } from './getLedgerHistory.js'
|
|
|
29
33
|
export interface GetPortfolioHistoryParams {
|
|
30
34
|
address: Address
|
|
31
35
|
range: PortfolioHistoryRange
|
|
36
|
+
bounds?: PortfolioHistoryBounds
|
|
32
37
|
}
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
'
|
|
39
|
-
'
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
/** The venue presets of one account scope, narrowest first. */
|
|
40
|
+
const COMBINED_PERIODS: readonly HlPortfolioPeriod[] = [
|
|
41
|
+
'day',
|
|
42
|
+
'week',
|
|
43
|
+
'month',
|
|
44
|
+
'allTime',
|
|
45
|
+
]
|
|
46
|
+
const PERP_PERIODS: readonly HlPortfolioPeriod[] = [
|
|
47
|
+
'perpDay',
|
|
48
|
+
'perpWeek',
|
|
49
|
+
'perpMonth',
|
|
50
|
+
'perpAllTime',
|
|
51
|
+
]
|
|
52
|
+
const PERIOD_INDEX_BY_RANGE: Record<PortfolioHistoryRange, number> = {
|
|
53
|
+
'24h': 0,
|
|
54
|
+
'7d': 1,
|
|
55
|
+
'30d': 2,
|
|
56
|
+
all: 3,
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
/** The samples with a finite timestamp; each other sample is skipped. */
|
|
@@ -66,17 +74,21 @@ const validSamples = (
|
|
|
66
74
|
/**
|
|
67
75
|
* Join the window's two series on their timestamps. `pnlHistory` is cumulative
|
|
68
76
|
* from the window's start, so a point with no PnL sample carries the last
|
|
69
|
-
* sampled value — `'0'` before the first one.
|
|
77
|
+
* sampled value — `'0'` before the first one. `totalPnl` is the last valid
|
|
78
|
+
* PnL sample.
|
|
70
79
|
*/
|
|
71
80
|
const joinWindow = (
|
|
72
|
-
|
|
73
|
-
pnlHistory: [number, DecimalString][],
|
|
81
|
+
window: HlPortfolioWindow,
|
|
74
82
|
period: HlPortfolioPeriod
|
|
75
|
-
): PortfolioHistoryPoint[] => {
|
|
83
|
+
): { points: PortfolioHistoryPoint[]; totalPnl: DecimalString | undefined } => {
|
|
84
|
+
const pnlHistory = validSamples(window.pnlHistory, 'portfolio PnL sample')
|
|
76
85
|
const pnlByTimestamp = new Map(pnlHistory)
|
|
77
86
|
let matched = 0
|
|
78
87
|
let cumulative = '0'
|
|
79
|
-
const points =
|
|
88
|
+
const points = validSamples(
|
|
89
|
+
window.accountValueHistory,
|
|
90
|
+
'portfolio value sample'
|
|
91
|
+
).map(([timestamp, accountValue]) => {
|
|
80
92
|
const sample = pnlByTimestamp.get(timestamp)
|
|
81
93
|
if (sample !== undefined) {
|
|
82
94
|
matched += 1
|
|
@@ -91,14 +103,83 @@ const joinWindow = (
|
|
|
91
103
|
'account-value timestamp.'
|
|
92
104
|
)
|
|
93
105
|
}
|
|
94
|
-
return points
|
|
106
|
+
return { points, totalPnl: pnlHistory.at(-1)?.[1] }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const resolveWindow = (
|
|
110
|
+
portfolio: HlPortfolio,
|
|
111
|
+
period: HlPortfolioPeriod
|
|
112
|
+
): HlPortfolioWindow => {
|
|
113
|
+
const window = portfolio.find(([key]) => key === period)?.[1]
|
|
114
|
+
if (window === undefined) {
|
|
115
|
+
throw new PerpsError(
|
|
116
|
+
PerpsErrorCode.ThirdPartyError,
|
|
117
|
+
`Hyperliquid portfolio response has no '${period}' window.`
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
return window
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const warnUnjoinablePresetOnce = createWarnOnce()
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The bounded native samples when they hold an opening sample at or before
|
|
127
|
+
* `bounds.start`. Otherwise the samples of the narrowest wider preset of the
|
|
128
|
+
* same scope that holds one, or the native samples without an opening when
|
|
129
|
+
* no preset holds one. A wider preset the venue did not send, or whose
|
|
130
|
+
* series do not join, is skipped: the native samples are already usable, so
|
|
131
|
+
* the wider preset warns instead of failing the read.
|
|
132
|
+
*/
|
|
133
|
+
const resolveBoundedWindow = (
|
|
134
|
+
portfolio: HlPortfolio,
|
|
135
|
+
periods: readonly HlPortfolioPeriod[],
|
|
136
|
+
nativeIndex: number,
|
|
137
|
+
nativePoints: PortfolioHistoryPoint[],
|
|
138
|
+
bounds: PortfolioHistoryBounds
|
|
139
|
+
): PortfolioHistoryWindow => {
|
|
140
|
+
const native = buildPortfolioHistoryWindow(PROVIDER_KEY, nativePoints, bounds)
|
|
141
|
+
if (native.opening !== undefined) {
|
|
142
|
+
return native
|
|
143
|
+
}
|
|
144
|
+
for (const period of periods.slice(nativeIndex + 1)) {
|
|
145
|
+
const window = portfolio.find(([key]) => key === period)?.[1]
|
|
146
|
+
if (window === undefined) {
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
let widerPoints: PortfolioHistoryPoint[]
|
|
150
|
+
try {
|
|
151
|
+
widerPoints = joinWindow(window, period).points
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (!(error instanceof PerpsError)) {
|
|
154
|
+
throw error
|
|
155
|
+
}
|
|
156
|
+
warnUnjoinablePresetOnce(
|
|
157
|
+
period,
|
|
158
|
+
`[${PROVIDER_KEY}] portfolio window '${period}' gives no opening sample: ${error.message}`
|
|
159
|
+
)
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
const candidate = buildPortfolioHistoryWindow(
|
|
163
|
+
PROVIDER_KEY,
|
|
164
|
+
widerPoints,
|
|
165
|
+
bounds
|
|
166
|
+
)
|
|
167
|
+
if (candidate.opening !== undefined) {
|
|
168
|
+
return candidate
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return native
|
|
95
172
|
}
|
|
96
173
|
|
|
97
174
|
/**
|
|
98
175
|
* Read the account's portfolio value and cumulative PnL for `params.range`
|
|
99
176
|
* from one `portfolio` info request. An account whose collateral lives
|
|
100
177
|
* per-dex reads the perps-only window; a unified or portfolio-margin account
|
|
101
|
-
* holds its collateral in spot, so it reads the combined window.
|
|
178
|
+
* holds its collateral in spot, so it reads the combined window. With
|
|
179
|
+
* `params.bounds`, the read takes the opening sample from the native preset,
|
|
180
|
+
* or from a wider preset of the same scope when the native one has none, and
|
|
181
|
+
* reads the ledger from the first retained sample. `volume` stays the native
|
|
182
|
+
* preset total.
|
|
102
183
|
* @public
|
|
103
184
|
*/
|
|
104
185
|
export async function getPortfolioHistory(
|
|
@@ -124,25 +205,29 @@ export async function getPortfolioHistory(
|
|
|
124
205
|
infoOpts
|
|
125
206
|
),
|
|
126
207
|
])
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
throw new PerpsError(
|
|
133
|
-
PerpsErrorCode.ThirdPartyError,
|
|
134
|
-
`Hyperliquid portfolio response has no '${period}' window.`
|
|
135
|
-
)
|
|
136
|
-
}
|
|
208
|
+
const combined = isUnifiedAbstraction(abstraction)
|
|
209
|
+
const periods = combined ? COMBINED_PERIODS : PERP_PERIODS
|
|
210
|
+
const nativeIndex = PERIOD_INDEX_BY_RANGE[params.range]
|
|
211
|
+
const nativeWindow = resolveWindow(portfolio, periods[nativeIndex])
|
|
212
|
+
const native = joinWindow(nativeWindow, periods[nativeIndex])
|
|
137
213
|
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
214
|
+
const { bounds } = params
|
|
215
|
+
const selected: PortfolioHistoryWindow =
|
|
216
|
+
bounds === undefined
|
|
217
|
+
? { points: native.points }
|
|
218
|
+
: resolveBoundedWindow(
|
|
219
|
+
portfolio,
|
|
220
|
+
periods,
|
|
221
|
+
nativeIndex,
|
|
222
|
+
native.points,
|
|
223
|
+
bounds
|
|
224
|
+
)
|
|
225
|
+
const retained =
|
|
226
|
+
selected.opening === undefined
|
|
227
|
+
? selected.points
|
|
228
|
+
: [selected.opening, ...selected.points]
|
|
229
|
+
const first = retained[0]
|
|
230
|
+
const last = retained.at(-1)
|
|
146
231
|
const ledger =
|
|
147
232
|
first !== undefined &&
|
|
148
233
|
last !== undefined &&
|
|
@@ -157,15 +242,19 @@ export async function getPortfolioHistory(
|
|
|
157
242
|
infoOpts
|
|
158
243
|
)
|
|
159
244
|
: []
|
|
245
|
+
const withTransfers = mapPortfolioTransfers(
|
|
246
|
+
retained,
|
|
247
|
+
ledger,
|
|
248
|
+
params.address,
|
|
249
|
+
combined
|
|
250
|
+
)
|
|
251
|
+
const openingCount = selected.opening === undefined ? 0 : 1
|
|
252
|
+
const points = withTransfers.slice(openingCount)
|
|
160
253
|
return {
|
|
161
254
|
range: params.range,
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
isUnifiedAbstraction(abstraction)
|
|
167
|
-
),
|
|
168
|
-
volume: window.vlm,
|
|
169
|
-
totalPnl: pnlHistory.at(-1)?.[1],
|
|
255
|
+
...(openingCount === 0 ? {} : { opening: withTransfers[0] }),
|
|
256
|
+
points,
|
|
257
|
+
volume: nativeWindow.vlm,
|
|
258
|
+
totalPnl: bounds === undefined ? native.totalPnl : points.at(-1)?.pnl,
|
|
170
259
|
}
|
|
171
260
|
}
|
package/src/utils/mapActivity.ts
CHANGED
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
type AssetRegistry,
|
|
3
3
|
ExplorerChainId,
|
|
4
4
|
explorerTxUrl,
|
|
5
|
-
PerpsError,
|
|
6
5
|
safeAddDecimalString,
|
|
7
6
|
safeMultiplyDecimalString,
|
|
8
7
|
safeSubtractDecimalString,
|
|
@@ -23,7 +22,7 @@ import type {
|
|
|
23
22
|
TransferActivity,
|
|
24
23
|
WithdrawalActivity,
|
|
25
24
|
} from '@lifi/perps-types'
|
|
26
|
-
import { ActivityType
|
|
25
|
+
import { ActivityType } from '@lifi/perps-types'
|
|
27
26
|
import { zeroHash } from 'viem'
|
|
28
27
|
import type {
|
|
29
28
|
HlBorrowLendOperation,
|
|
@@ -101,6 +100,24 @@ const resolveLedgerAsset = (
|
|
|
101
100
|
return asset
|
|
102
101
|
}
|
|
103
102
|
|
|
103
|
+
/** Whether a transfer delta names the queried account as sender and recipient. */
|
|
104
|
+
export const isSameAccountTransfer = (
|
|
105
|
+
delta: { user: string; destination: string },
|
|
106
|
+
queriedAddress: string
|
|
107
|
+
): boolean => {
|
|
108
|
+
const queried = queriedAddress.toLowerCase()
|
|
109
|
+
return (
|
|
110
|
+
delta.user.toLowerCase() === queried &&
|
|
111
|
+
delta.destination.toLowerCase() === queried
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// A `send` names spot `"spot"`, the main perp dex `""`, and other perp dexes by name.
|
|
116
|
+
const resolveSendBalance = (
|
|
117
|
+
dex: unknown
|
|
118
|
+
): InternalTransferBalance | undefined =>
|
|
119
|
+
typeof dex === 'string' ? (dex === 'spot' ? 'spot' : 'perps') : undefined
|
|
120
|
+
|
|
104
121
|
/** Map supported ledger entries, preserving venue amounts and operation metadata. */
|
|
105
122
|
export const mapLedgerEntry = (
|
|
106
123
|
entry: HlLedgerUpdate,
|
|
@@ -119,6 +136,36 @@ export const mapLedgerEntry = (
|
|
|
119
136
|
}
|
|
120
137
|
const base = { id: entry.hash, provider: providerKey, timestamp }
|
|
121
138
|
|
|
139
|
+
if (isSendAssetDelta(delta) && isSameAccountTransfer(delta, queriedAddress)) {
|
|
140
|
+
const sourceBalance = resolveSendBalance(delta.sourceDex)
|
|
141
|
+
const destinationBalance = resolveSendBalance(delta.destinationDex)
|
|
142
|
+
if (
|
|
143
|
+
sourceBalance === undefined ||
|
|
144
|
+
destinationBalance === undefined ||
|
|
145
|
+
sourceBalance === destinationBalance
|
|
146
|
+
) {
|
|
147
|
+
return null
|
|
148
|
+
}
|
|
149
|
+
const asset = resolveLedgerAsset(delta.token, assetRegistry, providerKey)
|
|
150
|
+
if (asset === null) {
|
|
151
|
+
return null
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
...base,
|
|
155
|
+
type: ActivityType.INTERNAL_TRANSFER,
|
|
156
|
+
sourceBalance,
|
|
157
|
+
destinationBalance,
|
|
158
|
+
asset,
|
|
159
|
+
amount: delta.amount,
|
|
160
|
+
meta: {
|
|
161
|
+
transferType: 'sendAsset',
|
|
162
|
+
sourceDex: delta.sourceDex,
|
|
163
|
+
destinationDex: delta.destinationDex,
|
|
164
|
+
},
|
|
165
|
+
explorerLink: explorerTxUrl(ExplorerChainId.HYPERLIQUID, entry.hash),
|
|
166
|
+
} satisfies InternalTransferActivity
|
|
167
|
+
}
|
|
168
|
+
|
|
122
169
|
if (
|
|
123
170
|
isSpotTransferDelta(delta) ||
|
|
124
171
|
isSendAssetDelta(delta) ||
|
|
@@ -127,7 +174,7 @@ export const mapLedgerEntry = (
|
|
|
127
174
|
const queried = queriedAddress.toLowerCase()
|
|
128
175
|
const sender = delta.user.toLowerCase()
|
|
129
176
|
const recipient = delta.destination.toLowerCase()
|
|
130
|
-
if (
|
|
177
|
+
if (isSameAccountTransfer(delta, queriedAddress)) {
|
|
131
178
|
return null
|
|
132
179
|
}
|
|
133
180
|
const asset = isCollateralTransferDelta(delta)
|
|
@@ -278,10 +325,10 @@ export const mapLedgerEntry = (
|
|
|
278
325
|
const queried = queriedAddress.toLowerCase()
|
|
279
326
|
const vault = delta.vault.toLowerCase()
|
|
280
327
|
if (delta.type === 'vaultDeposit' && queried === vault) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
328
|
+
warnSkippedVenueRow(providerKey, 'ledger', 'depositor', undefined, {
|
|
329
|
+
expected: 'address',
|
|
330
|
+
})
|
|
331
|
+
return null
|
|
285
332
|
}
|
|
286
333
|
if (
|
|
287
334
|
delta.type === 'vaultWithdraw' &&
|