@lifi/perps-types 0.1.1-alpha.21 → 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 +17 -4
- package/providers/lighter/package.json +5 -0
- package/src/_cjs/enums.js +10 -1
- package/src/_cjs/enums.js.map +1 -1
- package/src/_cjs/providers/hyperliquid/types.js +8 -1
- package/src/_cjs/providers/hyperliquid/types.js.map +1 -1
- package/src/_cjs/providers/lighter/apiTypes.js +3 -0
- 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/_esm/enums.js +9 -0
- package/src/_esm/enums.js.map +1 -1
- package/src/_esm/providers/hyperliquid/types.js +10 -0
- 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/_types/action.d.ts +47 -2
- package/src/_types/action.d.ts.map +1 -1
- package/src/_types/enums.d.ts +9 -1
- package/src/_types/enums.d.ts.map +1 -1
- package/src/_types/providers/hyperliquid/types.d.ts +8 -0
- 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 +2 -1
- package/src/_types/providers.d.ts.map +1 -1
- package/src/_types/typedData.d.ts +13 -5
- package/src/_types/typedData.d.ts.map +1 -1
- package/src/action.ts +66 -2
- package/src/enums.ts +9 -0
- package/src/providers/hyperliquid/types.ts +27 -0
- 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 +2 -1
- package/src/typedData.ts +15 -5
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Lighter raw API response types — shared between backend (REST) and SDK (WS)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
export type LtAccountPosition = {
|
|
6
|
+
market_id: number
|
|
7
|
+
symbol: string
|
|
8
|
+
initial_margin_fraction: string
|
|
9
|
+
open_order_count: number
|
|
10
|
+
pending_order_count: number
|
|
11
|
+
position_tied_order_count: number
|
|
12
|
+
sign: number
|
|
13
|
+
position: string
|
|
14
|
+
avg_entry_price: string
|
|
15
|
+
position_value: string
|
|
16
|
+
unrealized_pnl: string
|
|
17
|
+
realized_pnl: string
|
|
18
|
+
liquidation_price: string
|
|
19
|
+
total_funding_paid_out: string
|
|
20
|
+
margin_mode: number
|
|
21
|
+
allocated_margin: string
|
|
22
|
+
total_discount: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type LtAccountAsset = {
|
|
26
|
+
symbol: string
|
|
27
|
+
asset_id: number
|
|
28
|
+
balance: string
|
|
29
|
+
locked_balance: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type LtTrade = {
|
|
33
|
+
trade_id: number
|
|
34
|
+
tx_hash: string
|
|
35
|
+
type: string
|
|
36
|
+
market_id: number
|
|
37
|
+
size: string
|
|
38
|
+
price: string
|
|
39
|
+
usd_amount: string
|
|
40
|
+
ask_id: number
|
|
41
|
+
bid_id: number
|
|
42
|
+
ask_account_id: number
|
|
43
|
+
bid_account_id: number
|
|
44
|
+
is_maker_ask: boolean
|
|
45
|
+
block_height: number
|
|
46
|
+
timestamp: number
|
|
47
|
+
taker_fee: number
|
|
48
|
+
maker_fee: number
|
|
49
|
+
transaction_time: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type LtOrder = {
|
|
53
|
+
order_index: number
|
|
54
|
+
client_order_index: number
|
|
55
|
+
order_id: string
|
|
56
|
+
client_order_id: string
|
|
57
|
+
market_index: number
|
|
58
|
+
owner_account_index: number
|
|
59
|
+
initial_base_amount: string
|
|
60
|
+
price: string
|
|
61
|
+
nonce: number
|
|
62
|
+
remaining_base_amount: string
|
|
63
|
+
is_ask: boolean
|
|
64
|
+
filled_base_amount: string
|
|
65
|
+
filled_quote_amount: string
|
|
66
|
+
side: string
|
|
67
|
+
type: string
|
|
68
|
+
time_in_force: string
|
|
69
|
+
reduce_only: boolean
|
|
70
|
+
trigger_price: string
|
|
71
|
+
order_expiry: number
|
|
72
|
+
status: string
|
|
73
|
+
trigger_status: string
|
|
74
|
+
trigger_time: number
|
|
75
|
+
block_height: number
|
|
76
|
+
timestamp: number
|
|
77
|
+
created_at: number
|
|
78
|
+
updated_at: number
|
|
79
|
+
transaction_time: number
|
|
80
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FillClassification,
|
|
3
|
+
FillStatus,
|
|
4
|
+
OrderSide,
|
|
5
|
+
OrderType,
|
|
6
|
+
} from '../../../enums.js'
|
|
7
|
+
import type { Fill } from '../../../account.js'
|
|
8
|
+
import type { LtTrade } from '../apiTypes.js'
|
|
9
|
+
|
|
10
|
+
const classifyFill = (isBuy: boolean): FillClassification =>
|
|
11
|
+
isBuy ? FillClassification.OPENED_LONG : FillClassification.OPENED_SHORT
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Map a raw Lighter trade to the generic Fill type.
|
|
15
|
+
* @param trade - Raw trade from REST or WS
|
|
16
|
+
* @param accountIndex - The viewer's Lighter account index (to determine buy/sell side)
|
|
17
|
+
* @param symbol - Resolved symbol (market_id → symbol lookup)
|
|
18
|
+
*/
|
|
19
|
+
export const mapFill = (
|
|
20
|
+
trade: LtTrade,
|
|
21
|
+
accountIndex: number,
|
|
22
|
+
symbol: string
|
|
23
|
+
): Fill => {
|
|
24
|
+
const isBuyer = trade.bid_account_id === accountIndex
|
|
25
|
+
const isMaker =
|
|
26
|
+
(trade.is_maker_ask && !isBuyer) || (!trade.is_maker_ask && isBuyer)
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
id: trade.trade_id.toString(),
|
|
30
|
+
asset: {
|
|
31
|
+
assetId: symbol,
|
|
32
|
+
market: 'lighter',
|
|
33
|
+
displaySymbol: symbol,
|
|
34
|
+
displayQuote: 'USDC',
|
|
35
|
+
},
|
|
36
|
+
side: isBuyer ? OrderSide.BUY : OrderSide.SELL,
|
|
37
|
+
type: OrderType.LIMIT,
|
|
38
|
+
size: trade.size,
|
|
39
|
+
price: trade.price,
|
|
40
|
+
status: FillStatus.FILLED,
|
|
41
|
+
fee: isMaker ? trade.maker_fee.toString() : trade.taker_fee.toString(),
|
|
42
|
+
classification: classifyFill(isBuyer),
|
|
43
|
+
createdAt: new Date(trade.timestamp * 1000).toISOString(),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { OrderSide, OrderType } from '../../../enums.js'
|
|
2
|
+
import type { OpenOrder } from '../../../account.js'
|
|
3
|
+
import type { LtOrder } from '../apiTypes.js'
|
|
4
|
+
|
|
5
|
+
const mapOrderType = (ltType: string): OrderType => {
|
|
6
|
+
const map: Record<string, OrderType> = {
|
|
7
|
+
limit: OrderType.LIMIT,
|
|
8
|
+
market: OrderType.MARKET,
|
|
9
|
+
stop_loss: OrderType.STOP_MARKET,
|
|
10
|
+
stop_loss_limit: OrderType.STOP_LIMIT,
|
|
11
|
+
take_profit: OrderType.TAKE_PROFIT_MARKET,
|
|
12
|
+
take_profit_limit: OrderType.TAKE_PROFIT_LIMIT,
|
|
13
|
+
}
|
|
14
|
+
return map[ltType] ?? OrderType.LIMIT
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Map a raw Lighter order to the generic OpenOrder type.
|
|
19
|
+
* @param order - Raw order from REST or WS
|
|
20
|
+
* @param symbol - Resolved symbol (market_index → symbol lookup)
|
|
21
|
+
*/
|
|
22
|
+
export const mapOrder = (order: LtOrder, symbol: string): OpenOrder => ({
|
|
23
|
+
id: order.order_id,
|
|
24
|
+
asset: {
|
|
25
|
+
assetId: symbol,
|
|
26
|
+
market: 'lighter',
|
|
27
|
+
displaySymbol: symbol,
|
|
28
|
+
displayQuote: 'USDC',
|
|
29
|
+
},
|
|
30
|
+
side: order.is_ask ? OrderSide.SELL : OrderSide.BUY,
|
|
31
|
+
type: mapOrderType(order.type),
|
|
32
|
+
size: order.initial_base_amount,
|
|
33
|
+
price: order.price,
|
|
34
|
+
filledSize: order.filled_base_amount,
|
|
35
|
+
reduceOnly: order.reduce_only,
|
|
36
|
+
createdAt: new Date(order.created_at * 1000).toISOString(),
|
|
37
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { MarginMode, PositionSide } from '../../../enums.js'
|
|
2
|
+
import type { Position } from '../../../account.js'
|
|
3
|
+
import type { LtAccountPosition } from '../apiTypes.js'
|
|
4
|
+
import { LT_MARGIN_MODE_ISOLATED } from '../types.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Map a raw Lighter account position to the generic Position type.
|
|
8
|
+
* @param pos - Raw position from REST or WS
|
|
9
|
+
* @param symbol - Resolved symbol (market_id → symbol lookup, or pos.symbol)
|
|
10
|
+
*/
|
|
11
|
+
export const mapPosition = (
|
|
12
|
+
pos: LtAccountPosition,
|
|
13
|
+
symbol: string
|
|
14
|
+
): Position => {
|
|
15
|
+
const size = parseFloat(pos.position)
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
asset: {
|
|
19
|
+
assetId: symbol,
|
|
20
|
+
market: 'lighter',
|
|
21
|
+
displaySymbol: symbol,
|
|
22
|
+
displayQuote: 'USDC',
|
|
23
|
+
},
|
|
24
|
+
side: pos.sign >= 0 ? PositionSide.LONG : PositionSide.SHORT,
|
|
25
|
+
size: Math.abs(size).toString(),
|
|
26
|
+
entryPrice: pos.avg_entry_price,
|
|
27
|
+
markPrice:
|
|
28
|
+
pos.position_value === '0' || size === 0
|
|
29
|
+
? '0'
|
|
30
|
+
: (parseFloat(pos.position_value) / Math.abs(size)).toString(),
|
|
31
|
+
liquidationPrice: pos.liquidation_price,
|
|
32
|
+
unrealizedPnl: pos.unrealized_pnl,
|
|
33
|
+
leverage:
|
|
34
|
+
parseFloat(pos.initial_margin_fraction) > 0
|
|
35
|
+
? Math.round(10_000 / parseFloat(pos.initial_margin_fraction))
|
|
36
|
+
: 1,
|
|
37
|
+
marginUsed: pos.allocated_margin,
|
|
38
|
+
marginMode:
|
|
39
|
+
pos.margin_mode === LT_MARGIN_MODE_ISOLATED
|
|
40
|
+
? MarginMode.ISOLATED
|
|
41
|
+
: MarginMode.CROSS,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Lighter WASM signer parameter types
|
|
3
|
+
//
|
|
4
|
+
// These define the exact shapes that `wasmSignParams` takes for each Lighter
|
|
5
|
+
// action type. The backend populates these; the SDK passes them to the WASM
|
|
6
|
+
// signer along with SDK-managed fields (nonce, account_index, api_key_index).
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/** Backend-provided params for SignCreateOrder. */
|
|
10
|
+
export type LtCreateOrderWasmParams = {
|
|
11
|
+
market_index: number
|
|
12
|
+
client_order_index: number
|
|
13
|
+
base_amount: number
|
|
14
|
+
price: number
|
|
15
|
+
is_ask: number
|
|
16
|
+
order_type: number
|
|
17
|
+
time_in_force: number
|
|
18
|
+
reduce_only: boolean
|
|
19
|
+
trigger_price: number
|
|
20
|
+
order_expiry: number
|
|
21
|
+
integrator_account_index: number
|
|
22
|
+
integrator_taker_fee: number
|
|
23
|
+
integrator_maker_fee: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Backend-provided params for SignCancelOrder. */
|
|
27
|
+
export type LtCancelOrderWasmParams = {
|
|
28
|
+
market_index: number
|
|
29
|
+
order_index: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Backend-provided params for SignModifyOrder. */
|
|
33
|
+
export type LtModifyOrderWasmParams = {
|
|
34
|
+
market_index: number
|
|
35
|
+
order_index: number
|
|
36
|
+
base_amount: number
|
|
37
|
+
price: number
|
|
38
|
+
trigger_price: number
|
|
39
|
+
integrator_account_index: number
|
|
40
|
+
integrator_taker_fee: number
|
|
41
|
+
integrator_maker_fee: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Backend-provided params for SignUpdateLeverage. */
|
|
45
|
+
export type LtUpdateLeverageWasmParams = {
|
|
46
|
+
market_index: number
|
|
47
|
+
fraction: number
|
|
48
|
+
margin_mode: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Backend-provided params for SignUpdateMargin. */
|
|
52
|
+
export type LtUpdateMarginWasmParams = {
|
|
53
|
+
market_index: number
|
|
54
|
+
usdc_amount: number
|
|
55
|
+
direction: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Backend-provided params for SignWithdraw. */
|
|
59
|
+
export type LtWithdrawWasmParams = {
|
|
60
|
+
asset_index: number
|
|
61
|
+
route_type: number
|
|
62
|
+
amount: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Backend-provided params for SignCancelAllOrders. */
|
|
66
|
+
export type LtCancelAllOrdersWasmParams = {
|
|
67
|
+
/** 0=immediate (cancel GTC), 1=scheduled, 2=abort scheduled */
|
|
68
|
+
time_in_force: number
|
|
69
|
+
/** Unix timestamp in milliseconds. Use Date.now() for immediate cancels. */
|
|
70
|
+
timestamp_ms: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Backend-provided params for SignChangePubKey (REGISTER_API_KEY).
|
|
75
|
+
*
|
|
76
|
+
* NOTE: `new_public_key` is a placeholder here — the SDK generates the actual
|
|
77
|
+
* keypair via the WASM signer and fills this in before calling SignChangePubKey.
|
|
78
|
+
* This action also requires an L1 Ethereum signature (the user's wallet signs
|
|
79
|
+
* the ChangePubKey message), so it is a hybrid: WASM blob + EIP-191 eth_sign.
|
|
80
|
+
*/
|
|
81
|
+
export type LtChangePubKeyWasmParams = {
|
|
82
|
+
/** API key slot index to register (0-255). SDK fills in the generated public key. */
|
|
83
|
+
api_key_index: number
|
|
84
|
+
nonce: number
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// Lighter WASM signer constants
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
export const LT_ORDER_TYPE_LIMIT = 0
|
|
92
|
+
export const LT_ORDER_TYPE_MARKET = 1
|
|
93
|
+
export const LT_ORDER_TYPE_STOP_LOSS = 2
|
|
94
|
+
export const LT_ORDER_TYPE_STOP_LOSS_LIMIT = 3
|
|
95
|
+
export const LT_ORDER_TYPE_TAKE_PROFIT = 4
|
|
96
|
+
export const LT_ORDER_TYPE_TAKE_PROFIT_LIMIT = 5
|
|
97
|
+
export const LT_ORDER_TYPE_TWAP = 6
|
|
98
|
+
|
|
99
|
+
export const LT_TIME_IN_FORCE_IOC = 0
|
|
100
|
+
export const LT_TIME_IN_FORCE_GTC = 1
|
|
101
|
+
export const LT_TIME_IN_FORCE_POST_ONLY = 2
|
|
102
|
+
|
|
103
|
+
export const LT_MARGIN_MODE_CROSS = 0
|
|
104
|
+
export const LT_MARGIN_MODE_ISOLATED = 1
|
|
105
|
+
|
|
106
|
+
export const LT_MARGIN_DIRECTION_REMOVE = 0
|
|
107
|
+
export const LT_MARGIN_DIRECTION_ADD = 1
|
|
108
|
+
|
|
109
|
+
export const LT_ROUTE_PERP = 0
|
|
110
|
+
export const LT_ROUTE_SPOT = 1
|
|
111
|
+
|
|
112
|
+
export const LT_ASSET_ID_USDC = 3
|
|
113
|
+
|
|
114
|
+
export const LT_NIL_TRIGGER_PRICE = 0
|
|
115
|
+
export const LT_DEFAULT_ORDER_EXPIRY = -1
|
package/src/providers.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ActionType, PerpsSigner } from './enums.js'
|
|
1
|
+
import type { ActionType, PerpsSigner, SigningMethod } from './enums.js'
|
|
2
2
|
|
|
3
3
|
export interface ActionDescriptor {
|
|
4
4
|
type: ActionType
|
|
@@ -14,6 +14,7 @@ export interface Provider {
|
|
|
14
14
|
key: string
|
|
15
15
|
name: string
|
|
16
16
|
logoURI: string
|
|
17
|
+
signingMethod: SigningMethod
|
|
17
18
|
prepareAccountActions: ActionDescriptor[]
|
|
18
19
|
actions: ActionDescriptor[]
|
|
19
20
|
markets: ProviderMarketInfo[]
|
package/src/typedData.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import type { TypedData, SignedTypedData } from '@lifi/types'
|
|
2
1
|
import type { Address, Hex, TypedDataDomain, TypedDataParameter } from 'viem'
|
|
2
|
+
import type { HlPrimaryType } from './providers/hyperliquid/types.js'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* All known EIP-712 primaryType values across supported providers.
|
|
6
|
+
* Extend this union when adding a new EIP-712 provider.
|
|
7
|
+
*/
|
|
8
|
+
export type PerpsPrimaryType = HlPrimaryType
|
|
9
|
+
|
|
10
|
+
export type PerpsTypedData = {
|
|
11
|
+
domain: TypedDataDomain
|
|
12
|
+
types: Record<string, readonly TypedDataParameter[]>
|
|
13
|
+
primaryType: PerpsPrimaryType
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
message: Record<string, any>
|
|
6
16
|
}
|
|
7
17
|
|
|
8
|
-
export type PerpsSignedTypedData =
|
|
9
|
-
|
|
18
|
+
export type PerpsSignedTypedData = PerpsTypedData & {
|
|
19
|
+
signature: Hex
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
// Re-export viem primitives used across perps types
|