@lifi/perps-sdk-provider-lighter 16.0.0 → 17.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  2. package/dist/cjs/LighterProvider.js +6 -31
  3. package/dist/cjs/LighterProvider.js.map +1 -1
  4. package/dist/cjs/types/account.d.ts.map +1 -1
  5. package/dist/cjs/types/activity.d.ts.map +1 -1
  6. package/dist/cjs/types/fill.d.ts +2 -0
  7. package/dist/cjs/types/fill.d.ts.map +1 -1
  8. package/dist/cjs/types/order.d.ts.map +1 -1
  9. package/dist/cjs/utils/mapFill.d.ts.map +1 -1
  10. package/dist/cjs/utils/mapFill.js +14 -5
  11. package/dist/cjs/utils/mapFill.js.map +1 -1
  12. package/dist/esm/LighterProvider.d.ts.map +1 -1
  13. package/dist/esm/LighterProvider.js +10 -35
  14. package/dist/esm/LighterProvider.js.map +1 -1
  15. package/dist/esm/types/account.d.ts +11 -0
  16. package/dist/esm/types/account.d.ts.map +1 -1
  17. package/dist/esm/types/activity.d.ts +9 -4
  18. package/dist/esm/types/activity.d.ts.map +1 -1
  19. package/dist/esm/types/fill.d.ts +9 -2
  20. package/dist/esm/types/fill.d.ts.map +1 -1
  21. package/dist/esm/types/order.d.ts +19 -5
  22. package/dist/esm/types/order.d.ts.map +1 -1
  23. package/dist/esm/utils/mapFill.d.ts.map +1 -1
  24. package/dist/esm/utils/mapFill.js +25 -5
  25. package/dist/esm/utils/mapFill.js.map +1 -1
  26. package/dist/types/LighterProvider.d.ts.map +1 -1
  27. package/dist/types/types/account.d.ts +11 -0
  28. package/dist/types/types/account.d.ts.map +1 -1
  29. package/dist/types/types/activity.d.ts +9 -4
  30. package/dist/types/types/activity.d.ts.map +1 -1
  31. package/dist/types/types/fill.d.ts +9 -2
  32. package/dist/types/types/fill.d.ts.map +1 -1
  33. package/dist/types/types/order.d.ts +19 -5
  34. package/dist/types/types/order.d.ts.map +1 -1
  35. package/dist/types/utils/mapFill.d.ts.map +1 -1
  36. package/package.json +3 -3
  37. package/src/LighterProvider.ts +20 -47
  38. package/src/types/account.ts +11 -0
  39. package/src/types/activity.ts +9 -4
  40. package/src/types/fill.ts +11 -2
  41. package/src/types/order.ts +19 -5
  42. package/src/utils/mapFill.ts +35 -5
package/src/types/fill.ts CHANGED
@@ -3,8 +3,13 @@
3
3
  /**
4
4
  * Public trade/fill row returned by Lighter's `/api/v1/trades` endpoint.
5
5
  * Size, price, and notional fields are decimal strings in market precision;
6
- * `timestamp` and `transaction_time` are Unix millisecond timestamps. The
7
- * optional fee fields are integer fee ticks and may be absent on older rows.
6
+ * `timestamp` is a Unix millisecond timestamp; `transaction_time` is a Unix
7
+ * microsecond timestamp. The optional fee fields are the side's fee *rate* as
8
+ * an integer tick on `LIGHTER_FEE_TICK_SCALE` (1e6), not the amount charged,
9
+ * and may be absent on older rows. A row can also carry
10
+ * `integrator_maker_fee` or `integrator_taker_fee`: a second optional rate for
11
+ * the same side, on the same 1e6 tick scale. The mapper adds that second rate
12
+ * to the side's own rate.
8
13
  *
9
14
  * @public
10
15
  */
@@ -28,6 +33,10 @@ export type LtTrade = {
28
33
  // on older trades) — keep optional and let the mapper emit `undefined`.
29
34
  taker_fee?: number
30
35
  maker_fee?: number
36
+ // A second fee tick that a row can carry for the same side, on the same 1e6
37
+ // tick scale as `taker_fee` / `maker_fee`.
38
+ integrator_taker_fee?: number
39
+ integrator_maker_fee?: number
31
40
  transaction_time: number
32
41
  // Per-counterparty position snapshot BEFORE the trade is applied. Signed
33
42
  // strings: positive = long, negative = short, "0" / "0.00000" = flat.
@@ -3,9 +3,10 @@
3
3
  /**
4
4
  * Order payload returned by Lighter's REST API. Amounts and prices are decimal
5
5
  * strings in market precision. `order_expiry` is an absolute Unix-millisecond
6
- * expiry; `created_at`, `updated_at`, and `transaction_time` are Unix seconds.
7
- * Enum-like side, type, time-in-force, status, and trigger fields retain
8
- * Lighter's wire strings.
6
+ * expiry; `created_at` and `updated_at` are Unix seconds. `timestamp` and
7
+ * `transaction_time` carry their unit on the member; `trigger_time` has no
8
+ * documented unit. Enum-like side, type, time-in-force, status, and trigger
9
+ * fields retain Lighter's wire strings.
9
10
  *
10
11
  * @public
11
12
  */
@@ -38,9 +39,18 @@ export type LtOrder = {
38
39
  to_trigger_order_id_1: string
39
40
  to_cancel_order_id_0: string
40
41
  block_height: number
42
+ /**
43
+ * Unit unproven. Lighter documents no unit, and every endpoint that returns
44
+ * this row is auth-gated, so no public probe can observe a value.
45
+ */
41
46
  timestamp: number
42
47
  created_at: number
43
48
  updated_at: number
49
+ /**
50
+ * Unix timestamp in microseconds. Lighter documents no unit, and its public
51
+ * trade rows report this member in microseconds while the sibling
52
+ * `created_at` and `updated_at` stay in seconds.
53
+ */
44
54
  transaction_time: number
45
55
  }
46
56
 
@@ -58,8 +68,8 @@ export interface LtOrdersResponse {
58
68
 
59
69
  /**
60
70
  * Single order-book level returned by Lighter. Amounts and prices are decimal
61
- * strings in the market's native precision; expiry and transaction time are
62
- * Unix milliseconds.
71
+ * strings in the market's native precision; `order_expiry` is an absolute
72
+ * Unix-millisecond expiry.
63
73
  *
64
74
  * @public
65
75
  */
@@ -71,6 +81,10 @@ export interface LtOrderBookOrder {
71
81
  remaining_base_amount: string
72
82
  price: string
73
83
  order_expiry: number
84
+ /**
85
+ * Unit unproven. Lighter documents no unit, and every resting order this
86
+ * endpoint returns reports the member as zero.
87
+ */
74
88
  transaction_time: number
75
89
  }
76
90
 
@@ -11,8 +11,30 @@ import {
11
11
  OrderType,
12
12
  } from '@lifi/perps-types'
13
13
  import Big from 'big.js'
14
+ import { LIGHTER_FEE_TICK_SCALE } from '../constants.js'
14
15
  import type { LtTrade } from '../types/index.js'
15
16
 
17
+ /**
18
+ * Fee charged on a fill, in the market's quote asset. Lighter publishes the
19
+ * side's fee *rate* as an integer tick on `LIGHTER_FEE_TICK_SCALE` rather than
20
+ * the amount it charged. A row can carry more than one tick for the same side,
21
+ * so `amount = notional * sum(side ticks) / LIGHTER_FEE_TICK_SCALE`.
22
+ *
23
+ * The product keeps the sign of both inputs. A rebate tick therefore maps to a
24
+ * negative amount, which `Fee.amount` permits and the Ondo mapper already emits
25
+ * when a rebate exceeds the fee, so this helper never clamps the sign. A zero
26
+ * notional charges a zero fee at every tick.
27
+ */
28
+ const tickToFeeAmount = (
29
+ notional: string,
30
+ ownTick: number,
31
+ integratorTick: number | undefined
32
+ ): string =>
33
+ new Big(notional)
34
+ .times(new Big(ownTick).plus(integratorTick ?? 0))
35
+ .div(LIGHTER_FEE_TICK_SCALE)
36
+ .toFixed()
37
+
16
38
  /**
17
39
  * Realized PnL on a position-reducing fill, derived from the pre-trade entry
18
40
  * basis. Returns `undefined` for opens/increases (nothing closes) or when the
@@ -75,9 +97,10 @@ export const mapFill = (
75
97
  const entryQuoteBefore = isMaker
76
98
  ? trade.maker_entry_quote_before
77
99
  : trade.taker_entry_quote_before
78
- const feeAmount = isMaker
79
- ? trade.maker_fee?.toString()
80
- : trade.taker_fee?.toString()
100
+ const feeTick = isMaker ? trade.maker_fee : trade.taker_fee
101
+ const integratorFeeTick = isMaker
102
+ ? trade.integrator_maker_fee
103
+ : trade.integrator_taker_fee
81
104
 
82
105
  return {
83
106
  id: trade.trade_id.toString(),
@@ -91,9 +114,16 @@ export const mapFill = (
91
114
  liquidity: isMaker ? LiquidityRole.MAKER : LiquidityRole.TAKER,
92
115
  // Lighter charges the fill fee in the market's quote asset.
93
116
  fee:
94
- feeAmount === undefined
117
+ feeTick === undefined
95
118
  ? undefined
96
- : { amount: feeAmount, asset: market.quoteAsset.displaySymbol },
119
+ : {
120
+ amount: tickToFeeAmount(
121
+ trade.usd_amount,
122
+ feeTick,
123
+ integratorFeeTick
124
+ ),
125
+ asset: market.quoteAsset.displaySymbol,
126
+ },
97
127
  realizedPnl: deriveRealizedPnl(
98
128
  startPosition,
99
129
  entryQuoteBefore,