@lifi/perps-types 0.1.1-alpha.2 → 0.1.1-alpha.21
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/package.json +24 -7
- package/providers/hyperliquid/package.json +3 -3
- package/src/_cjs/{market.js → action.js} +1 -1
- package/src/_cjs/action.js.map +1 -0
- package/src/_cjs/{trading.js → asset.js} +1 -1
- package/src/_cjs/asset.js.map +1 -0
- package/src/_cjs/enums.js +69 -15
- package/src/_cjs/enums.js.map +1 -1
- package/src/_cjs/index.js +3 -4
- package/src/_cjs/index.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/assetId.js +11 -9
- package/src/_cjs/providers/hyperliquid/assetId.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/mappers/activity.js +66 -0
- package/src/_cjs/providers/hyperliquid/mappers/activity.js.map +1 -0
- package/src/_cjs/providers/hyperliquid/mappers/{market.js → asset.js} +9 -9
- package/src/_cjs/providers/hyperliquid/mappers/asset.js.map +1 -0
- package/src/_cjs/providers/hyperliquid/mappers/fill.js +64 -0
- package/src/_cjs/providers/hyperliquid/mappers/fill.js.map +1 -0
- package/src/_cjs/providers/hyperliquid/mappers/index.js +12 -7
- package/src/_cjs/providers/hyperliquid/mappers/index.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/mappers/order.js +59 -10
- package/src/_cjs/providers/hyperliquid/mappers/order.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/mappers/position.js +7 -5
- package/src/_cjs/providers/hyperliquid/mappers/position.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/mappers/shared.js +0 -10
- package/src/_cjs/providers/hyperliquid/mappers/shared.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/types.js +7 -0
- package/src/_cjs/providers/hyperliquid/types.js.map +1 -1
- package/src/_cjs/{withdrawal.js → providers.js} +1 -1
- package/src/_cjs/providers.js.map +1 -0
- package/src/_esm/action.js +2 -0
- package/src/_esm/action.js.map +1 -0
- package/src/_esm/asset.js +2 -0
- package/src/_esm/asset.js.map +1 -0
- package/src/_esm/enums.js +68 -14
- package/src/_esm/enums.js.map +1 -1
- package/src/_esm/index.js +3 -4
- package/src/_esm/index.js.map +1 -1
- package/src/_esm/providers/hyperliquid/assetId.js +13 -11
- package/src/_esm/providers/hyperliquid/assetId.js.map +1 -1
- package/src/_esm/providers/hyperliquid/mappers/activity.js +69 -0
- package/src/_esm/providers/hyperliquid/mappers/activity.js.map +1 -0
- package/src/_esm/providers/hyperliquid/mappers/{market.js → asset.js} +7 -7
- package/src/_esm/providers/hyperliquid/mappers/asset.js.map +1 -0
- package/src/_esm/providers/hyperliquid/mappers/fill.js +62 -0
- package/src/_esm/providers/hyperliquid/mappers/fill.js.map +1 -0
- package/src/_esm/providers/hyperliquid/mappers/index.js +4 -4
- package/src/_esm/providers/hyperliquid/mappers/index.js.map +1 -1
- package/src/_esm/providers/hyperliquid/mappers/order.js +56 -9
- package/src/_esm/providers/hyperliquid/mappers/order.js.map +1 -1
- package/src/_esm/providers/hyperliquid/mappers/position.js +7 -5
- package/src/_esm/providers/hyperliquid/mappers/position.js.map +1 -1
- package/src/_esm/providers/hyperliquid/mappers/shared.js +1 -12
- package/src/_esm/providers/hyperliquid/mappers/shared.js.map +1 -1
- package/src/_esm/providers/hyperliquid/types.js +12 -2
- package/src/_esm/providers/hyperliquid/types.js.map +1 -1
- package/src/_esm/providers.js +2 -0
- package/src/_esm/providers.js.map +1 -0
- package/src/_types/account.d.ts +73 -18
- package/src/_types/account.d.ts.map +1 -1
- package/src/_types/action.d.ts +136 -0
- package/src/_types/action.d.ts.map +1 -0
- package/src/_types/{market.d.ts → asset.d.ts} +23 -34
- package/src/_types/asset.d.ts.map +1 -0
- package/src/_types/enums.d.ts +54 -5
- package/src/_types/enums.d.ts.map +1 -1
- package/src/_types/index.d.ts +3 -4
- package/src/_types/index.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/assetId.d.ts +8 -6
- package/src/_types/providers/hyperliquid/assetId.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/mappers/activity.d.ts +13 -0
- package/src/_types/providers/hyperliquid/mappers/activity.d.ts.map +1 -0
- package/src/_types/providers/hyperliquid/mappers/asset.d.ts +4 -0
- package/src/_types/providers/hyperliquid/mappers/asset.d.ts.map +1 -0
- package/src/_types/providers/hyperliquid/mappers/fill.d.ts +6 -0
- package/src/_types/providers/hyperliquid/mappers/fill.d.ts.map +1 -0
- package/src/_types/providers/hyperliquid/mappers/index.d.ts +4 -4
- package/src/_types/providers/hyperliquid/mappers/index.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/mappers/order.d.ts +8 -3
- package/src/_types/providers/hyperliquid/mappers/order.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/mappers/position.d.ts +1 -1
- package/src/_types/providers/hyperliquid/mappers/position.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/mappers/shared.d.ts +0 -6
- package/src/_types/providers/hyperliquid/mappers/shared.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/types.d.ts +55 -11
- package/src/_types/providers/hyperliquid/types.d.ts.map +1 -1
- package/src/_types/providers.d.ts +22 -0
- package/src/_types/providers.d.ts.map +1 -0
- package/src/_types/subscriptions.d.ts +24 -18
- package/src/_types/subscriptions.d.ts.map +1 -1
- package/src/account.ts +94 -18
- package/src/action.ts +182 -0
- package/src/asset.ts +88 -0
- package/src/enums.ts +63 -3
- package/src/index.ts +3 -4
- package/src/providers/hyperliquid/assetId.ts +18 -12
- package/src/providers/hyperliquid/mappers/activity.ts +94 -0
- package/src/providers/hyperliquid/mappers/{market.ts → asset.ts} +9 -12
- package/src/providers/hyperliquid/mappers/fill.ts +77 -0
- package/src/providers/hyperliquid/mappers/index.ts +10 -4
- package/src/providers/hyperliquid/mappers/order.ts +62 -15
- package/src/providers/hyperliquid/mappers/position.ts +7 -10
- package/src/providers/hyperliquid/mappers/shared.ts +0 -15
- package/src/providers/hyperliquid/types.ts +100 -20
- package/src/providers.ts +25 -0
- package/src/subscriptions.ts +22 -20
- package/src/_cjs/authorization.js +0 -3
- package/src/_cjs/authorization.js.map +0 -1
- package/src/_cjs/market.js.map +0 -1
- package/src/_cjs/providers/hyperliquid/mappers/history.js +0 -22
- package/src/_cjs/providers/hyperliquid/mappers/history.js.map +0 -1
- package/src/_cjs/providers/hyperliquid/mappers/market.js.map +0 -1
- package/src/_cjs/trading.js.map +0 -1
- package/src/_cjs/withdrawal.js.map +0 -1
- package/src/_esm/authorization.js +0 -2
- package/src/_esm/authorization.js.map +0 -1
- package/src/_esm/market.js +0 -2
- package/src/_esm/market.js.map +0 -1
- package/src/_esm/providers/hyperliquid/mappers/history.js +0 -18
- package/src/_esm/providers/hyperliquid/mappers/history.js.map +0 -1
- package/src/_esm/providers/hyperliquid/mappers/market.js.map +0 -1
- package/src/_esm/trading.js +0 -2
- package/src/_esm/trading.js.map +0 -1
- package/src/_esm/withdrawal.js +0 -2
- package/src/_esm/withdrawal.js.map +0 -1
- package/src/_types/authorization.d.ts +0 -39
- package/src/_types/authorization.d.ts.map +0 -1
- package/src/_types/market.d.ts.map +0 -1
- package/src/_types/providers/hyperliquid/mappers/history.d.ts +0 -4
- package/src/_types/providers/hyperliquid/mappers/history.d.ts.map +0 -1
- package/src/_types/providers/hyperliquid/mappers/market.d.ts +0 -4
- package/src/_types/providers/hyperliquid/mappers/market.d.ts.map +0 -1
- package/src/_types/trading.d.ts +0 -82
- package/src/_types/trading.d.ts.map +0 -1
- package/src/_types/withdrawal.d.ts +0 -37
- package/src/_types/withdrawal.d.ts.map +0 -1
- package/src/authorization.ts +0 -46
- package/src/market.ts +0 -97
- package/src/providers/hyperliquid/mappers/history.ts +0 -25
- package/src/trading.ts +0 -99
- package/src/withdrawal.ts +0 -44
package/src/asset.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export interface FundingInfo {
|
|
2
|
+
rate: string
|
|
3
|
+
nextFundingTime: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface AssetIdentity {
|
|
7
|
+
assetId: string // provider's canonical identity: "BTC", "xyz:PURR", "@142"
|
|
8
|
+
market: string // market category from /providers.markets: "hyperliquid", "xyz", "spot"
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Asset extends AssetIdentity {
|
|
12
|
+
displaySymbol: string // UI base name: "BTC", "PURR"; spot keeps full pair: "PURR/USDC"
|
|
13
|
+
displayQuote: string | null // quote asset for perps: "USDC", "USDH"; null for spot
|
|
14
|
+
displayName?: string // future: "Bitcoin", "Ethereum"
|
|
15
|
+
logoURI: string
|
|
16
|
+
szDecimals: number
|
|
17
|
+
maxLeverage: number
|
|
18
|
+
onlyIsolated: boolean
|
|
19
|
+
isMarginCollateral?: boolean
|
|
20
|
+
funding: FundingInfo
|
|
21
|
+
openInterest?: string
|
|
22
|
+
volume24h?: string
|
|
23
|
+
prevDayPrice?: string
|
|
24
|
+
markPrice: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type AssetDisplay = Pick<
|
|
28
|
+
Asset,
|
|
29
|
+
'assetId' | 'market' | 'displaySymbol' | 'displayQuote'
|
|
30
|
+
>
|
|
31
|
+
|
|
32
|
+
export interface AssetPrice {
|
|
33
|
+
assetId: string
|
|
34
|
+
price: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface AssetsResponse {
|
|
38
|
+
assets: Asset[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PricesResponse {
|
|
42
|
+
prices: AssetPrice[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Candle {
|
|
46
|
+
t: number
|
|
47
|
+
o: string
|
|
48
|
+
h: string
|
|
49
|
+
l: string
|
|
50
|
+
c: string
|
|
51
|
+
v: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface OhlcvResponse {
|
|
55
|
+
provider: string
|
|
56
|
+
assetId: string
|
|
57
|
+
interval: string
|
|
58
|
+
candles: Candle[]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type OhlcvInterval =
|
|
62
|
+
| '1m'
|
|
63
|
+
| '3m'
|
|
64
|
+
| '5m'
|
|
65
|
+
| '15m'
|
|
66
|
+
| '30m'
|
|
67
|
+
| '1h'
|
|
68
|
+
| '2h'
|
|
69
|
+
| '4h'
|
|
70
|
+
| '8h'
|
|
71
|
+
| '12h'
|
|
72
|
+
| '1d'
|
|
73
|
+
| '3d'
|
|
74
|
+
| '1w'
|
|
75
|
+
| '1M'
|
|
76
|
+
|
|
77
|
+
export interface OrderbookLevel {
|
|
78
|
+
price: string
|
|
79
|
+
size: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface OrderbookResponse {
|
|
83
|
+
provider: string
|
|
84
|
+
assetId: string
|
|
85
|
+
bids: OrderbookLevel[]
|
|
86
|
+
asks: OrderbookLevel[]
|
|
87
|
+
timestamp: number
|
|
88
|
+
}
|
package/src/enums.ts
CHANGED
|
@@ -5,6 +5,7 @@ export enum PerpsErrorCode {
|
|
|
5
5
|
ValidationError = 2002,
|
|
6
6
|
TimeoutError = 2003,
|
|
7
7
|
ThirdPartyError = 2004,
|
|
8
|
+
SDKError = 2005,
|
|
8
9
|
|
|
9
10
|
// Auth errors (2010-2019)
|
|
10
11
|
SignatureInvalid = 2010,
|
|
@@ -38,6 +39,11 @@ export enum OrderSide {
|
|
|
38
39
|
export enum OrderType {
|
|
39
40
|
MARKET = 'MARKET',
|
|
40
41
|
LIMIT = 'LIMIT',
|
|
42
|
+
STOP_MARKET = 'STOP_MARKET',
|
|
43
|
+
STOP_LIMIT = 'STOP_LIMIT',
|
|
44
|
+
TAKE_PROFIT_MARKET = 'TAKE_PROFIT_MARKET',
|
|
45
|
+
TAKE_PROFIT_LIMIT = 'TAKE_PROFIT_LIMIT',
|
|
46
|
+
TRIGGER_ONLY = 'TRIGGER_ONLY',
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
export enum PositionSide {
|
|
@@ -68,18 +74,26 @@ export enum OrderStatus {
|
|
|
68
74
|
TRIGGERED = 'TRIGGERED',
|
|
69
75
|
}
|
|
70
76
|
|
|
71
|
-
export enum
|
|
77
|
+
export enum FillStatus {
|
|
72
78
|
FILLED = 'FILLED',
|
|
73
79
|
PARTIALLY_FILLED = 'PARTIALLY_FILLED',
|
|
74
80
|
CANCELLED = 'CANCELLED',
|
|
75
81
|
REJECTED = 'REJECTED',
|
|
76
82
|
}
|
|
77
83
|
|
|
78
|
-
export enum
|
|
79
|
-
|
|
84
|
+
export enum ActionType {
|
|
85
|
+
APPROVE_AGENT = 'approveAgent',
|
|
86
|
+
APPROVE_BUILDER_FEE = 'approveBuilderFee',
|
|
87
|
+
USER_SET_ABSTRACTION = 'userSetAbstraction',
|
|
88
|
+
AGENT_SET_ABSTRACTION = 'agentSetAbstraction',
|
|
89
|
+
SEND_ASSET = 'sendAsset',
|
|
90
|
+
WITHDRAWAL = 'withdrawal',
|
|
80
91
|
PLACE_ORDER = 'placeOrder',
|
|
81
92
|
PLACE_TRIGGER_ORDER = 'placeTriggerOrder',
|
|
82
93
|
CANCEL_ORDER = 'cancelOrder',
|
|
94
|
+
MODIFY_ORDER = 'modifyOrder',
|
|
95
|
+
UPDATE_LEVERAGE = 'updateLeverage',
|
|
96
|
+
UPDATE_POSITION_MARGIN = 'updatePositionMargin',
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
export enum TriggerCondition {
|
|
@@ -97,3 +111,49 @@ export enum TriggerOrderStatus {
|
|
|
97
111
|
TRIGGERED = 'TRIGGERED',
|
|
98
112
|
CANCELLED = 'CANCELLED',
|
|
99
113
|
}
|
|
114
|
+
|
|
115
|
+
export enum ActivityType {
|
|
116
|
+
DEPOSIT = 'DEPOSIT',
|
|
117
|
+
WITHDRAWAL = 'WITHDRAWAL',
|
|
118
|
+
LIQUIDATION = 'LIQUIDATION',
|
|
119
|
+
FUNDING = 'FUNDING',
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export enum FillClassification {
|
|
123
|
+
OPENED_LONG = 'Opened Long',
|
|
124
|
+
OPENED_SHORT = 'Opened Short',
|
|
125
|
+
INCREASED_LONG = 'Increased Long',
|
|
126
|
+
INCREASED_SHORT = 'Increased Short',
|
|
127
|
+
REDUCED_LONG = 'Reduced Long',
|
|
128
|
+
REDUCED_SHORT = 'Reduced Short',
|
|
129
|
+
CLOSED_LONG = 'Closed Long',
|
|
130
|
+
CLOSED_SHORT = 'Closed Short',
|
|
131
|
+
SWITCHED_LONG = 'Switched Long',
|
|
132
|
+
SWITCHED_SHORT = 'Switched Short',
|
|
133
|
+
SPOT_BUY = 'Spot Buy',
|
|
134
|
+
SPOT_SELL = 'Spot Sell',
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export enum LiquidationClassification {
|
|
138
|
+
LIQUIDATED = 'Liquidated',
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export enum FundingClassification {
|
|
142
|
+
FUNDING = 'Funding',
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export enum TransferClassification {
|
|
146
|
+
DEPOSIT = 'Deposit',
|
|
147
|
+
WITHDRAWAL = 'Withdrawal',
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export type ActivityClassification =
|
|
151
|
+
| FillClassification
|
|
152
|
+
| LiquidationClassification
|
|
153
|
+
| FundingClassification
|
|
154
|
+
| TransferClassification
|
|
155
|
+
|
|
156
|
+
export enum PerpsSigner {
|
|
157
|
+
USER = 'USER',
|
|
158
|
+
AGENT = 'AGENT',
|
|
159
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export * from './typedData.js'
|
|
2
2
|
export * from './enums.js'
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
3
|
+
export * from './providers.js'
|
|
4
|
+
export * from './asset.js'
|
|
5
|
+
export * from './action.js'
|
|
5
6
|
export * from './account.js'
|
|
6
|
-
export * from './trading.js'
|
|
7
|
-
export * from './withdrawal.js'
|
|
8
7
|
export * from './errors.js'
|
|
9
8
|
export * from './subscriptions.js'
|
|
@@ -1,26 +1,32 @@
|
|
|
1
|
+
/** Spot asset IDs use the @pairIndex format (e.g. "@230"). */
|
|
2
|
+
export const assetIsSpot = (assetId: string): boolean => assetId.startsWith('@')
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* Calculate the asset ID for a given
|
|
5
|
+
* Calculate the asset ID for a given provider index and asset index.
|
|
3
6
|
*
|
|
4
|
-
* Main
|
|
5
|
-
* HIP-3
|
|
7
|
+
* Main provider (provider = ''): assetId = indexInProvider
|
|
8
|
+
* HIP-3 providers (provider = 'xyz'): assetId = 100_000 + perpDexIndex * 10_000 + indexInProvider
|
|
6
9
|
*/
|
|
7
10
|
export const calculateAssetId = (
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
providerIndex: number,
|
|
12
|
+
indexInProvider: number
|
|
10
13
|
): number => {
|
|
11
|
-
if (
|
|
12
|
-
return
|
|
14
|
+
if (providerIndex === 0) {
|
|
15
|
+
return indexInProvider
|
|
13
16
|
}
|
|
14
|
-
return 100_000 +
|
|
17
|
+
return 100_000 + providerIndex * 10_000 + indexInProvider
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
|
-
* Get the
|
|
21
|
+
* Get the provider index from the given list of provider names.
|
|
19
22
|
*/
|
|
20
|
-
export const
|
|
21
|
-
|
|
23
|
+
export const getProviderIndex = (
|
|
24
|
+
provider: string,
|
|
25
|
+
providerNames: string[]
|
|
26
|
+
): number => {
|
|
27
|
+
const index = providerNames.indexOf(provider)
|
|
22
28
|
if (index === -1) {
|
|
23
|
-
throw new Error(`Unknown sub-
|
|
29
|
+
throw new Error(`Unknown sub-provider: ${provider}`)
|
|
24
30
|
}
|
|
25
31
|
return index
|
|
26
32
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ActivityType } from '../../../enums.js'
|
|
2
|
+
import type {
|
|
3
|
+
ActivityItem,
|
|
4
|
+
DepositActivity,
|
|
5
|
+
WithdrawalActivity,
|
|
6
|
+
LiquidationActivity,
|
|
7
|
+
FundingActivity,
|
|
8
|
+
} from '../../../account.js'
|
|
9
|
+
import type { HlLedgerUpdate, HlFundingUpdate } from '../types.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Map a Hyperliquid non-funding ledger entry to an ActivityItem.
|
|
13
|
+
* Returns null for unsupported delta types (accountClassTransfer,
|
|
14
|
+
* internalTransfer, subAccountTransfer, spotTransfer).
|
|
15
|
+
*/
|
|
16
|
+
export const mapLedgerEntry = (
|
|
17
|
+
entry: HlLedgerUpdate,
|
|
18
|
+
providerKey: string
|
|
19
|
+
): ActivityItem | null => {
|
|
20
|
+
const { delta } = entry
|
|
21
|
+
const base = {
|
|
22
|
+
id: entry.hash,
|
|
23
|
+
provider: providerKey,
|
|
24
|
+
timestamp: new Date(entry.time).toISOString(),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
switch (delta.type) {
|
|
28
|
+
case 'deposit':
|
|
29
|
+
return {
|
|
30
|
+
...base,
|
|
31
|
+
type: ActivityType.DEPOSIT,
|
|
32
|
+
amount: delta.usdc ?? '0',
|
|
33
|
+
} satisfies DepositActivity
|
|
34
|
+
|
|
35
|
+
case 'withdraw':
|
|
36
|
+
return {
|
|
37
|
+
...base,
|
|
38
|
+
type: ActivityType.WITHDRAWAL,
|
|
39
|
+
amount: delta.usdc ?? '0',
|
|
40
|
+
fee: (delta as { fee?: string }).fee ?? '0',
|
|
41
|
+
} satisfies WithdrawalActivity
|
|
42
|
+
|
|
43
|
+
case 'liquidation': {
|
|
44
|
+
const d = delta as unknown as {
|
|
45
|
+
type: string
|
|
46
|
+
liquidatedNtlPos: string
|
|
47
|
+
accountValue: string
|
|
48
|
+
leverageType: string
|
|
49
|
+
liquidatedPositions?: { coin: string; szi: string }[]
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...base,
|
|
53
|
+
type: ActivityType.LIQUIDATION,
|
|
54
|
+
liquidatedNotionalPosition: d.liquidatedNtlPos,
|
|
55
|
+
accountValue: d.accountValue,
|
|
56
|
+
leverageType: d.leverageType,
|
|
57
|
+
liquidatedPositions: (d.liquidatedPositions ?? []).map((p) => ({
|
|
58
|
+
asset: {
|
|
59
|
+
assetId: p.coin,
|
|
60
|
+
market: '',
|
|
61
|
+
displaySymbol: p.coin,
|
|
62
|
+
displayQuote: null,
|
|
63
|
+
},
|
|
64
|
+
size: p.szi,
|
|
65
|
+
})),
|
|
66
|
+
} satisfies LiquidationActivity
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
default:
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Map a Hyperliquid funding entry to a FundingActivity.
|
|
76
|
+
*/
|
|
77
|
+
export const mapFundingActivity = (
|
|
78
|
+
entry: HlFundingUpdate,
|
|
79
|
+
providerKey: string
|
|
80
|
+
): FundingActivity => ({
|
|
81
|
+
id: entry.hash,
|
|
82
|
+
provider: providerKey,
|
|
83
|
+
timestamp: new Date(entry.time).toISOString(),
|
|
84
|
+
type: ActivityType.FUNDING,
|
|
85
|
+
asset: {
|
|
86
|
+
assetId: entry.delta.coin,
|
|
87
|
+
market: '',
|
|
88
|
+
displaySymbol: entry.delta.coin,
|
|
89
|
+
displayQuote: null,
|
|
90
|
+
},
|
|
91
|
+
amount: entry.delta.usdc,
|
|
92
|
+
positionSize: entry.delta.szi,
|
|
93
|
+
fundingRate: entry.delta.fundingRate,
|
|
94
|
+
})
|
|
@@ -1,26 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { calculateAssetId } from '../assetId.js'
|
|
1
|
+
import type { Asset } from '../../../asset.js'
|
|
3
2
|
import type { HlAssetCtx, HlUniverseItem } from '../types.js'
|
|
4
3
|
|
|
5
4
|
const NEXT_FUNDING_INTERVAL_MS = 60 * 60 * 1000 // 1 hour
|
|
6
5
|
|
|
7
|
-
export const
|
|
6
|
+
export const mapAsset = (
|
|
8
7
|
universe: HlUniverseItem,
|
|
9
|
-
assetCtx: HlAssetCtx
|
|
10
|
-
|
|
11
|
-
indexInDex: number,
|
|
12
|
-
dexKey: string
|
|
13
|
-
): Market => {
|
|
8
|
+
assetCtx: HlAssetCtx
|
|
9
|
+
): Asset => {
|
|
14
10
|
const now = Date.now()
|
|
15
11
|
const nextFundingTime =
|
|
16
12
|
Math.ceil(now / NEXT_FUNDING_INTERVAL_MS) * NEXT_FUNDING_INTERVAL_MS
|
|
17
13
|
|
|
18
14
|
return {
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
assetId: universe.name,
|
|
16
|
+
market: '',
|
|
17
|
+
displaySymbol: universe.name,
|
|
18
|
+
displayQuote: null,
|
|
21
19
|
logoURI: `https://app.hyperliquid.xyz/coins/${universe.name}.svg`,
|
|
22
|
-
assetId: calculateAssetId(dexIndex, indexInDex),
|
|
23
|
-
dex: dexKey,
|
|
24
20
|
szDecimals: universe.szDecimals,
|
|
25
21
|
maxLeverage: universe.maxLeverage,
|
|
26
22
|
onlyIsolated: universe.onlyIsolated === true,
|
|
@@ -30,6 +26,7 @@ export const mapMarket = (
|
|
|
30
26
|
},
|
|
31
27
|
openInterest: assetCtx.openInterest,
|
|
32
28
|
volume24h: assetCtx.dayNtlVlm,
|
|
29
|
+
prevDayPrice: assetCtx.prevDayPx,
|
|
33
30
|
markPrice: assetCtx.markPx,
|
|
34
31
|
}
|
|
35
32
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FillClassification,
|
|
3
|
+
FillStatus,
|
|
4
|
+
OrderSide,
|
|
5
|
+
OrderType,
|
|
6
|
+
} from '../../../enums.js'
|
|
7
|
+
import type { Fill } from '../../../account.js'
|
|
8
|
+
import type { HlUserFill } from '../types.js'
|
|
9
|
+
import { assetIsSpot } from '../assetId.js'
|
|
10
|
+
|
|
11
|
+
export function classifyFillFromPosition(
|
|
12
|
+
startPosition: string,
|
|
13
|
+
side: string,
|
|
14
|
+
sz: string
|
|
15
|
+
): FillClassification {
|
|
16
|
+
const start = parseFloat(startPosition)
|
|
17
|
+
const delta = side === 'B' ? parseFloat(sz) : -parseFloat(sz)
|
|
18
|
+
const end = start + delta
|
|
19
|
+
|
|
20
|
+
// Position was flat → opening
|
|
21
|
+
if (start === 0) {
|
|
22
|
+
return end > 0
|
|
23
|
+
? FillClassification.OPENED_LONG
|
|
24
|
+
: FillClassification.OPENED_SHORT
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Position was long
|
|
28
|
+
if (start > 0) {
|
|
29
|
+
if (end === 0) {
|
|
30
|
+
return FillClassification.CLOSED_LONG
|
|
31
|
+
}
|
|
32
|
+
if (end < 0) {
|
|
33
|
+
return FillClassification.SWITCHED_SHORT
|
|
34
|
+
}
|
|
35
|
+
if (end > start) {
|
|
36
|
+
return FillClassification.INCREASED_LONG
|
|
37
|
+
}
|
|
38
|
+
return FillClassification.REDUCED_LONG
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Position was short (start < 0)
|
|
42
|
+
if (end === 0) {
|
|
43
|
+
return FillClassification.CLOSED_SHORT
|
|
44
|
+
}
|
|
45
|
+
if (end > 0) {
|
|
46
|
+
return FillClassification.SWITCHED_LONG
|
|
47
|
+
}
|
|
48
|
+
if (end < start) {
|
|
49
|
+
return FillClassification.INCREASED_SHORT
|
|
50
|
+
}
|
|
51
|
+
return FillClassification.REDUCED_SHORT
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const mapFill = (fill: HlUserFill): Fill => ({
|
|
55
|
+
id: String(fill.tid),
|
|
56
|
+
asset: {
|
|
57
|
+
assetId: fill.coin,
|
|
58
|
+
market: '',
|
|
59
|
+
displaySymbol: fill.coin,
|
|
60
|
+
displayQuote: null,
|
|
61
|
+
},
|
|
62
|
+
side: fill.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
|
|
63
|
+
type: fill.dir?.includes('Limit') ? OrderType.LIMIT : OrderType.MARKET,
|
|
64
|
+
size: fill.sz,
|
|
65
|
+
price: fill.px,
|
|
66
|
+
status: FillStatus.FILLED,
|
|
67
|
+
filledSize: fill.sz,
|
|
68
|
+
fee: fill.fee,
|
|
69
|
+
realizedPnl: fill.closedPnl === '0' ? null : fill.closedPnl,
|
|
70
|
+
startPosition: fill.startPosition,
|
|
71
|
+
classification: assetIsSpot(fill.coin)
|
|
72
|
+
? fill.side === 'B'
|
|
73
|
+
? FillClassification.SPOT_BUY
|
|
74
|
+
: FillClassification.SPOT_SELL
|
|
75
|
+
: classifyFillFromPosition(fill.startPosition, fill.side, fill.sz),
|
|
76
|
+
createdAt: new Date(fill.time).toISOString(),
|
|
77
|
+
})
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
|
|
1
|
+
export { mapAsset } from './asset.js'
|
|
2
|
+
export {
|
|
3
|
+
isTriggerType,
|
|
4
|
+
mapOpenOrder,
|
|
5
|
+
mapOrder,
|
|
6
|
+
mapOrderType,
|
|
7
|
+
mapTriggerOrder,
|
|
8
|
+
} from './order.js'
|
|
4
9
|
export { mapPosition } from './position.js'
|
|
5
|
-
export {
|
|
10
|
+
export { classifyFillFromPosition, mapFill } from './fill.js'
|
|
11
|
+
export { mapLedgerEntry, mapFundingActivity } from './activity.js'
|
|
@@ -4,32 +4,75 @@ import {
|
|
|
4
4
|
OrderType,
|
|
5
5
|
TimeInForce,
|
|
6
6
|
} from '../../../enums.js'
|
|
7
|
-
import type { OpenOrder } from '../../../account.js'
|
|
8
|
-
import type { Order } from '../../../
|
|
7
|
+
import type { OpenOrder, TriggerOrder } from '../../../account.js'
|
|
8
|
+
import type { Order } from '../../../action.js'
|
|
9
9
|
import type { HlFrontendOpenOrder, HlOrderDetail } from '../types.js'
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/** Map a Hyperliquid orderType string to the OrderType enum. */
|
|
12
|
+
export const mapOrderType = (orderType: string): OrderType => {
|
|
13
|
+
switch (orderType) {
|
|
14
|
+
case 'Take Profit Market':
|
|
15
|
+
return OrderType.TAKE_PROFIT_MARKET
|
|
16
|
+
case 'Take Profit Limit':
|
|
17
|
+
return OrderType.TAKE_PROFIT_LIMIT
|
|
18
|
+
case 'Stop Market':
|
|
19
|
+
return OrderType.STOP_MARKET
|
|
20
|
+
case 'Stop Limit':
|
|
21
|
+
return OrderType.STOP_LIMIT
|
|
22
|
+
case 'Market':
|
|
23
|
+
return OrderType.MARKET
|
|
24
|
+
default:
|
|
25
|
+
return OrderType.LIMIT
|
|
26
|
+
}
|
|
27
|
+
}
|
|
12
28
|
|
|
13
|
-
export const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
29
|
+
export const isTriggerType = (type: OrderType): boolean =>
|
|
30
|
+
type === OrderType.TAKE_PROFIT_MARKET ||
|
|
31
|
+
type === OrderType.TAKE_PROFIT_LIMIT ||
|
|
32
|
+
type === OrderType.STOP_MARKET ||
|
|
33
|
+
type === OrderType.STOP_LIMIT
|
|
34
|
+
|
|
35
|
+
export const mapOpenOrder = (o: HlFrontendOpenOrder): OpenOrder => ({
|
|
18
36
|
id: String(o.oid),
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
37
|
+
asset: {
|
|
38
|
+
assetId: o.coin,
|
|
39
|
+
market: '',
|
|
40
|
+
displaySymbol: o.coin,
|
|
41
|
+
displayQuote: null,
|
|
42
|
+
},
|
|
22
43
|
side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
|
|
23
|
-
type: o.orderType
|
|
44
|
+
type: mapOrderType(o.orderType),
|
|
24
45
|
size: o.sz,
|
|
25
46
|
price: o.limitPx,
|
|
26
47
|
filledSize: o.origSz
|
|
27
48
|
? (parseFloat(o.origSz) - parseFloat(o.sz)).toString()
|
|
28
49
|
: '0',
|
|
29
50
|
reduceOnly: o.reduceOnly ?? false,
|
|
51
|
+
label: o.isTrigger ? o.triggerCondition : undefined,
|
|
30
52
|
createdAt: new Date(o.timestamp).toISOString(),
|
|
31
53
|
})
|
|
32
54
|
|
|
55
|
+
export const mapTriggerOrder = (o: HlFrontendOpenOrder): TriggerOrder => {
|
|
56
|
+
const type = mapOrderType(o.orderType)
|
|
57
|
+
const isLimit =
|
|
58
|
+
type === OrderType.TAKE_PROFIT_LIMIT || type === OrderType.STOP_LIMIT
|
|
59
|
+
return {
|
|
60
|
+
id: String(o.oid),
|
|
61
|
+
asset: {
|
|
62
|
+
assetId: o.coin,
|
|
63
|
+
market: '',
|
|
64
|
+
displaySymbol: o.coin,
|
|
65
|
+
displayQuote: null,
|
|
66
|
+
},
|
|
67
|
+
type,
|
|
68
|
+
size: o.sz,
|
|
69
|
+
triggerPrice: o.triggerPx,
|
|
70
|
+
...(isLimit ? { limitPrice: o.limitPx } : {}),
|
|
71
|
+
label: o.triggerCondition,
|
|
72
|
+
createdAt: new Date(o.timestamp).toISOString(),
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
33
76
|
const mapOrderStatus = (status: string): OrderStatus => {
|
|
34
77
|
switch (status) {
|
|
35
78
|
case 'open':
|
|
@@ -70,10 +113,14 @@ export const mapOrder = (detail: HlOrderDetail): Order => {
|
|
|
70
113
|
|
|
71
114
|
return {
|
|
72
115
|
orderId: String(o.oid),
|
|
73
|
-
|
|
74
|
-
|
|
116
|
+
asset: {
|
|
117
|
+
assetId: o.coin,
|
|
118
|
+
market: '',
|
|
119
|
+
displaySymbol: o.coin,
|
|
120
|
+
displayQuote: null,
|
|
121
|
+
},
|
|
75
122
|
side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
|
|
76
|
-
type: o.orderType
|
|
123
|
+
type: mapOrderType(o.orderType),
|
|
77
124
|
price: o.limitPx,
|
|
78
125
|
originalSize: o.origSz,
|
|
79
126
|
remainingSize: o.sz,
|
|
@@ -2,20 +2,17 @@ import { MarginMode, PositionSide } from '../../../enums.js'
|
|
|
2
2
|
import type { Position } from '../../../account.js'
|
|
3
3
|
import type { HlAssetPosition } from '../types.js'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export const mapPosition = (
|
|
8
|
-
ap: HlAssetPosition,
|
|
9
|
-
dexKey: string,
|
|
10
|
-
assetIdLookup: Map<string, number>
|
|
11
|
-
): Position => {
|
|
5
|
+
export const mapPosition = (ap: HlAssetPosition): Position => {
|
|
12
6
|
const pos = ap.position
|
|
13
7
|
const szi = parseFloat(pos.szi)
|
|
14
8
|
|
|
15
9
|
return {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
asset: {
|
|
11
|
+
assetId: pos.coin,
|
|
12
|
+
market: '',
|
|
13
|
+
displaySymbol: pos.coin,
|
|
14
|
+
displayQuote: null,
|
|
15
|
+
},
|
|
19
16
|
side: szi >= 0 ? PositionSide.LONG : PositionSide.SHORT,
|
|
20
17
|
size: Math.abs(szi).toString(),
|
|
21
18
|
entryPrice: pos.entryPx ?? '0',
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Look up an asset ID by symbol, throwing if the symbol is unknown.
|
|
3
|
-
* Asset ID 0 is a valid Hyperliquid asset (BTC-PERP), so a fallback
|
|
4
|
-
* to 0 would silently corrupt data.
|
|
5
|
-
*/
|
|
6
|
-
export const resolveAssetIdFromLookup = (
|
|
7
|
-
lookup: Map<string, number>,
|
|
8
|
-
symbol: string
|
|
9
|
-
): number => {
|
|
10
|
-
const id = lookup.get(symbol)
|
|
11
|
-
if (id === undefined) {
|
|
12
|
-
throw new Error(`Unknown asset symbol: ${symbol}`)
|
|
13
|
-
}
|
|
14
|
-
return id
|
|
15
|
-
}
|