@lifi/perps-types 0.1.1-alpha.2 → 0.1.1-alpha.22
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 +40 -10
- package/providers/hyperliquid/package.json +3 -3
- package/providers/lighter/package.json +5 -0
- 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 +78 -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 +14 -0
- package/src/_cjs/providers/hyperliquid/types.js.map +1 -1
- package/src/_cjs/{authorization.js → providers/lighter/apiTypes.js} +1 -1
- package/src/_cjs/providers/lighter/apiTypes.js.map +1 -0
- package/src/_cjs/providers/lighter/index.js +20 -0
- package/src/_cjs/providers/lighter/index.js.map +1 -0
- package/src/_cjs/providers/lighter/mappers/fill.js +28 -0
- package/src/_cjs/providers/lighter/mappers/fill.js.map +1 -0
- package/src/_cjs/providers/lighter/mappers/index.js +10 -0
- package/src/_cjs/providers/lighter/mappers/index.js.map +1 -0
- package/src/_cjs/providers/lighter/mappers/order.js +33 -0
- package/src/_cjs/providers/lighter/mappers/order.js.map +1 -0
- package/src/_cjs/providers/lighter/mappers/position.js +33 -0
- package/src/_cjs/providers/lighter/mappers/position.js.map +1 -0
- package/src/_cjs/providers/lighter/types.js +23 -0
- package/src/_cjs/providers/lighter/types.js.map +1 -0
- 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 +77 -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 +22 -2
- package/src/_esm/providers/hyperliquid/types.js.map +1 -1
- package/src/_esm/providers/lighter/apiTypes.js +5 -0
- package/src/_esm/providers/lighter/apiTypes.js.map +1 -0
- package/src/_esm/providers/lighter/index.js +4 -0
- package/src/_esm/providers/lighter/index.js.map +1 -0
- package/src/_esm/providers/lighter/mappers/fill.js +30 -0
- package/src/_esm/providers/lighter/mappers/fill.js.map +1 -0
- package/src/_esm/providers/lighter/mappers/index.js +4 -0
- package/src/_esm/providers/lighter/mappers/index.js.map +1 -0
- package/src/_esm/providers/lighter/mappers/order.js +34 -0
- package/src/_esm/providers/lighter/mappers/order.js.map +1 -0
- package/src/_esm/providers/lighter/mappers/position.js +34 -0
- package/src/_esm/providers/lighter/mappers/position.js.map +1 -0
- package/src/_esm/providers/lighter/types.js +30 -0
- package/src/_esm/providers/lighter/types.js.map +1 -0
- 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 +181 -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 +62 -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 +63 -11
- package/src/_types/providers/hyperliquid/types.d.ts.map +1 -1
- package/src/_types/providers/lighter/apiTypes.d.ts +74 -0
- package/src/_types/providers/lighter/apiTypes.d.ts.map +1 -0
- package/src/_types/providers/lighter/index.d.ts +4 -0
- package/src/_types/providers/lighter/index.d.ts.map +1 -0
- package/src/_types/providers/lighter/mappers/fill.d.ts +10 -0
- package/src/_types/providers/lighter/mappers/fill.d.ts.map +1 -0
- package/src/_types/providers/lighter/mappers/index.d.ts +4 -0
- package/src/_types/providers/lighter/mappers/index.d.ts.map +1 -0
- package/src/_types/providers/lighter/mappers/order.d.ts +9 -0
- package/src/_types/providers/lighter/mappers/order.d.ts.map +1 -0
- package/src/_types/providers/lighter/mappers/position.d.ts +9 -0
- package/src/_types/providers/lighter/mappers/position.d.ts.map +1 -0
- package/src/_types/providers/lighter/types.d.ts +90 -0
- package/src/_types/providers/lighter/types.d.ts.map +1 -0
- package/src/_types/providers.d.ts +23 -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/_types/typedData.d.ts +13 -5
- package/src/_types/typedData.d.ts.map +1 -1
- package/src/account.ts +94 -18
- package/src/action.ts +246 -0
- package/src/asset.ts +88 -0
- package/src/enums.ts +72 -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 +127 -20
- package/src/providers/lighter/apiTypes.ts +80 -0
- package/src/providers/lighter/index.ts +3 -0
- package/src/providers/lighter/mappers/fill.ts +45 -0
- package/src/providers/lighter/mappers/index.ts +3 -0
- package/src/providers/lighter/mappers/order.ts +37 -0
- package/src/providers/lighter/mappers/position.ts +43 -0
- package/src/providers/lighter/types.ts +115 -0
- package/src/providers.ts +26 -0
- package/src/subscriptions.ts +22 -20
- package/src/typedData.ts +15 -5
- 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
|
@@ -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
|
-
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// ---------------------------------------------------------------------------
|
|
2
|
-
//
|
|
2
|
+
// Hyperliquid /info response types
|
|
3
3
|
// ---------------------------------------------------------------------------
|
|
4
4
|
|
|
5
5
|
// -- metaAndAssetCtxs -------------------------------------------------------
|
|
@@ -8,16 +8,19 @@ export type HlUniverseItem = {
|
|
|
8
8
|
name: string
|
|
9
9
|
szDecimals: number
|
|
10
10
|
maxLeverage: number
|
|
11
|
-
onlyIsolated
|
|
12
|
-
isDelisted
|
|
11
|
+
onlyIsolated?: boolean
|
|
12
|
+
isDelisted?: boolean
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export type HlMeta = {
|
|
15
|
+
export type HlMeta = {
|
|
16
|
+
universe: HlUniverseItem[]
|
|
17
|
+
}
|
|
16
18
|
|
|
17
19
|
export type HlAssetCtx = {
|
|
18
20
|
funding: string
|
|
19
21
|
openInterest: string
|
|
20
22
|
dayNtlVlm: string
|
|
23
|
+
prevDayPx: string
|
|
21
24
|
markPx: string
|
|
22
25
|
}
|
|
23
26
|
|
|
@@ -44,7 +47,11 @@ export type HlCandleSnapshot = HlCandle[]
|
|
|
44
47
|
|
|
45
48
|
// -- l2Book -----------------------------------------------------------------
|
|
46
49
|
|
|
47
|
-
export type HlLevel = {
|
|
50
|
+
export type HlLevel = {
|
|
51
|
+
px: string
|
|
52
|
+
sz: string
|
|
53
|
+
n: number
|
|
54
|
+
}
|
|
48
55
|
|
|
49
56
|
export type HlL2Book = {
|
|
50
57
|
levels: [HlLevel[], HlLevel[]]
|
|
@@ -61,15 +68,26 @@ export type HlPosition = {
|
|
|
61
68
|
liquidationPx: string
|
|
62
69
|
unrealizedPnl: string
|
|
63
70
|
marginUsed: string
|
|
64
|
-
leverage: {
|
|
71
|
+
leverage: {
|
|
72
|
+
type: string
|
|
73
|
+
value: number
|
|
74
|
+
}
|
|
65
75
|
}
|
|
66
76
|
|
|
67
|
-
export type HlAssetPosition = {
|
|
77
|
+
export type HlAssetPosition = {
|
|
78
|
+
position: HlPosition
|
|
79
|
+
}
|
|
68
80
|
|
|
69
81
|
export type HlClearinghouseState = {
|
|
70
82
|
assetPositions: HlAssetPosition[]
|
|
71
|
-
marginSummary: {
|
|
72
|
-
|
|
83
|
+
marginSummary: {
|
|
84
|
+
accountValue: string
|
|
85
|
+
totalMarginUsed: string
|
|
86
|
+
}
|
|
87
|
+
crossMarginSummary: {
|
|
88
|
+
accountValue: string
|
|
89
|
+
totalMarginUsed: string
|
|
90
|
+
}
|
|
73
91
|
}
|
|
74
92
|
|
|
75
93
|
// -- spotClearinghouseState -------------------------------------------------
|
|
@@ -106,13 +124,20 @@ export type HlFrontendOpenOrder = {
|
|
|
106
124
|
origSz: string
|
|
107
125
|
reduceOnly: boolean
|
|
108
126
|
timestamp: number
|
|
127
|
+
isTrigger: boolean
|
|
128
|
+
isPositionTpsl: boolean
|
|
129
|
+
triggerCondition: string
|
|
130
|
+
triggerPx: string
|
|
131
|
+
children: HlFrontendOpenOrder[]
|
|
132
|
+
tif: string | null
|
|
133
|
+
cloid: string | null
|
|
109
134
|
}
|
|
110
135
|
|
|
111
136
|
export type HlFrontendOpenOrders = HlFrontendOpenOrder[]
|
|
112
137
|
|
|
113
138
|
// -- extraAgents ------------------------------------------------------------
|
|
114
139
|
|
|
115
|
-
export type HlExtraAgents =
|
|
140
|
+
export type HlExtraAgents = Record<string, unknown>[]
|
|
116
141
|
|
|
117
142
|
// -- userFills / userFillsByTime --------------------------------------------
|
|
118
143
|
|
|
@@ -126,6 +151,7 @@ export type HlUserFill = {
|
|
|
126
151
|
fee: string
|
|
127
152
|
closedPnl: string
|
|
128
153
|
time: number
|
|
154
|
+
startPosition: string
|
|
129
155
|
}
|
|
130
156
|
|
|
131
157
|
export type HlUserFills = HlUserFill[]
|
|
@@ -134,11 +160,6 @@ export type HlUserFillsByTime = HlUserFill[]
|
|
|
134
160
|
|
|
135
161
|
// -- orderStatus ------------------------------------------------------------
|
|
136
162
|
|
|
137
|
-
export type HlOrderStatusFound = {
|
|
138
|
-
status: 'order'
|
|
139
|
-
order: HlOrderDetail
|
|
140
|
-
}
|
|
141
|
-
|
|
142
163
|
export type HlOrderDetail = {
|
|
143
164
|
order: {
|
|
144
165
|
oid: number
|
|
@@ -159,21 +180,80 @@ export type HlOrderDetail = {
|
|
|
159
180
|
statusTimestamp: number
|
|
160
181
|
}
|
|
161
182
|
|
|
183
|
+
export type HlOrderStatusFound = {
|
|
184
|
+
status: 'order'
|
|
185
|
+
order: HlOrderDetail
|
|
186
|
+
}
|
|
187
|
+
|
|
162
188
|
export type HlOrderStatusResponse =
|
|
163
189
|
| HlOrderStatusFound
|
|
164
190
|
| { status: 'unknownOid' }
|
|
165
191
|
|
|
192
|
+
// -- userNonFundingLedgerUpdates --------------------------------------------
|
|
193
|
+
|
|
194
|
+
export type HlLedgerDelta = {
|
|
195
|
+
type: string
|
|
196
|
+
usdc?: string
|
|
197
|
+
[key: string]: unknown
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export type HlLedgerUpdate = {
|
|
201
|
+
time: number
|
|
202
|
+
hash: string
|
|
203
|
+
delta: HlLedgerDelta
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export type HlUserNonFundingLedgerUpdates = HlLedgerUpdate[]
|
|
207
|
+
|
|
208
|
+
// -- userFunding ------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
export type HlFundingDelta = {
|
|
211
|
+
type: 'funding'
|
|
212
|
+
coin: string
|
|
213
|
+
usdc: string
|
|
214
|
+
szi: string
|
|
215
|
+
fundingRate: string
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export type HlFundingUpdate = {
|
|
219
|
+
time: number
|
|
220
|
+
hash: string
|
|
221
|
+
delta: HlFundingDelta
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export type HlUserFunding = HlFundingUpdate[]
|
|
225
|
+
|
|
226
|
+
// -- abstraction mode -------------------------------------------------------
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Possible values returned by the `userAbstraction` info endpoint.
|
|
230
|
+
* `null` means abstraction has never been set (standard mode).
|
|
231
|
+
*/
|
|
232
|
+
export const HlAbstractionMode = {
|
|
233
|
+
DISABLED: 'disabled',
|
|
234
|
+
UNIFIED_ACCOUNT: 'unifiedAccount',
|
|
235
|
+
PORTFOLIO_MARGIN: 'portfolioMargin',
|
|
236
|
+
DEX_ABSTRACTION: 'dexAbstraction',
|
|
237
|
+
} as const
|
|
238
|
+
|
|
239
|
+
export type HlAbstractionMode =
|
|
240
|
+
(typeof HlAbstractionMode)[keyof typeof HlAbstractionMode]
|
|
241
|
+
|
|
166
242
|
// -- perpDexs ---------------------------------------------------------------
|
|
167
243
|
|
|
168
|
-
export type HlPerpDexs =
|
|
244
|
+
export type HlPerpDexs = (null | { name: string })[]
|
|
169
245
|
|
|
170
246
|
// ---------------------------------------------------------------------------
|
|
171
|
-
// Exchange request types
|
|
247
|
+
// Exchange request / response types
|
|
172
248
|
// ---------------------------------------------------------------------------
|
|
173
249
|
|
|
174
250
|
export type HlExchangeRequest = {
|
|
175
251
|
action: Record<string, unknown>
|
|
176
|
-
signature: {
|
|
252
|
+
signature: {
|
|
253
|
+
r: string
|
|
254
|
+
s: string
|
|
255
|
+
v: number
|
|
256
|
+
}
|
|
177
257
|
nonce: number
|
|
178
258
|
vaultAddress?: string | null
|
|
179
259
|
}
|
|
@@ -185,7 +265,7 @@ export type HlExchangeResponse = {
|
|
|
185
265
|
| {
|
|
186
266
|
type: string
|
|
187
267
|
data?: {
|
|
188
|
-
statuses?:
|
|
268
|
+
statuses?: (
|
|
189
269
|
| string
|
|
190
270
|
| { filled: { totalSz: string; avgPx: string; oid: number } }
|
|
191
271
|
| { resting: { oid: number } }
|
|
@@ -193,8 +273,35 @@ export type HlExchangeResponse = {
|
|
|
193
273
|
| { waitingForTrigger: { oid: number } }
|
|
194
274
|
| { success: true }
|
|
195
275
|
| { error: string }
|
|
196
|
-
|
|
276
|
+
)[]
|
|
197
277
|
status?: unknown
|
|
198
278
|
}
|
|
199
279
|
}
|
|
200
280
|
}
|
|
281
|
+
|
|
282
|
+
// ---------------------------------------------------------------------------
|
|
283
|
+
// Hyperliquid EIP-712 primary type constants
|
|
284
|
+
// ---------------------------------------------------------------------------
|
|
285
|
+
|
|
286
|
+
export const HL_PRIMARY_TYPE_APPROVE_AGENT =
|
|
287
|
+
'HyperliquidTransaction:ApproveAgent' as const
|
|
288
|
+
export const HL_PRIMARY_TYPE_APPROVE_BUILDER_FEE =
|
|
289
|
+
'HyperliquidTransaction:ApproveBuilderFee' as const
|
|
290
|
+
export const HL_PRIMARY_TYPE_USER_SET_ABSTRACTION =
|
|
291
|
+
'HyperliquidTransaction:UserSetAbstraction' as const
|
|
292
|
+
export const HL_PRIMARY_TYPE_AGENT_SET_ABSTRACTION =
|
|
293
|
+
'HyperliquidTransaction:AgentSetAbstraction' as const
|
|
294
|
+
export const HL_PRIMARY_TYPE_WITHDRAW =
|
|
295
|
+
'HyperliquidTransaction:Withdraw' as const
|
|
296
|
+
export const HL_PRIMARY_TYPE_SEND_ASSET =
|
|
297
|
+
'HyperliquidTransaction:SendAsset' as const
|
|
298
|
+
export const HL_PRIMARY_TYPE_AGENT = 'Agent' as const
|
|
299
|
+
|
|
300
|
+
export type HlPrimaryType =
|
|
301
|
+
| typeof HL_PRIMARY_TYPE_APPROVE_AGENT
|
|
302
|
+
| typeof HL_PRIMARY_TYPE_APPROVE_BUILDER_FEE
|
|
303
|
+
| typeof HL_PRIMARY_TYPE_USER_SET_ABSTRACTION
|
|
304
|
+
| typeof HL_PRIMARY_TYPE_AGENT_SET_ABSTRACTION
|
|
305
|
+
| typeof HL_PRIMARY_TYPE_WITHDRAW
|
|
306
|
+
| typeof HL_PRIMARY_TYPE_SEND_ASSET
|
|
307
|
+
| typeof HL_PRIMARY_TYPE_AGENT
|