@pod-network/trade-sdk 0.13.0 → 0.14.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 (53) hide show
  1. package/README.md +7 -3
  2. package/dist/client.d.ts +5 -5
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +3 -16
  5. package/dist/client.js.map +1 -1
  6. package/dist/codec/activity.d.ts +15 -10
  7. package/dist/codec/activity.d.ts.map +1 -1
  8. package/dist/codec/activity.js +166 -31
  9. package/dist/codec/activity.js.map +1 -1
  10. package/dist/codec/decode.d.ts +2 -14
  11. package/dist/codec/decode.d.ts.map +1 -1
  12. package/dist/codec/decode.js +1 -66
  13. package/dist/codec/decode.js.map +1 -1
  14. package/dist/codec/orders-v2.d.ts +16 -9
  15. package/dist/codec/orders-v2.d.ts.map +1 -1
  16. package/dist/codec/orders-v2.js +6 -16
  17. package/dist/codec/orders-v2.js.map +1 -1
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/sync/activity.d.ts +19 -28
  22. package/dist/sync/activity.d.ts.map +1 -1
  23. package/dist/sync/activity.js +141 -167
  24. package/dist/sync/activity.js.map +1 -1
  25. package/dist/sync/orders.js +4 -4
  26. package/dist/sync/orders.js.map +1 -1
  27. package/dist/sync/pnl-history.d.ts.map +1 -1
  28. package/dist/sync/pnl-history.js +14 -9
  29. package/dist/sync/pnl-history.js.map +1 -1
  30. package/dist/transport/rest.d.ts +11 -12
  31. package/dist/transport/rest.d.ts.map +1 -1
  32. package/dist/transport/rest.js +11 -13
  33. package/dist/transport/rest.js.map +1 -1
  34. package/dist/transport/ws.d.ts +1 -3
  35. package/dist/transport/ws.d.ts.map +1 -1
  36. package/dist/transport/ws.js.map +1 -1
  37. package/dist/types/public.d.ts +78 -22
  38. package/dist/types/public.d.ts.map +1 -1
  39. package/dist/types/wire.d.ts +68 -63
  40. package/dist/types/wire.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/client.ts +8 -11
  43. package/src/codec/activity.ts +167 -41
  44. package/src/codec/decode.ts +6 -74
  45. package/src/codec/orders-v2.ts +11 -26
  46. package/src/index.ts +2 -1
  47. package/src/sync/activity.ts +141 -167
  48. package/src/sync/orders.ts +4 -4
  49. package/src/sync/pnl-history.ts +13 -7
  50. package/src/transport/rest.ts +21 -21
  51. package/src/transport/ws.ts +1 -3
  52. package/src/types/public.ts +83 -20
  53. package/src/types/wire.ts +56 -65
@@ -20,7 +20,7 @@ export type OrderSide = "buy" | "sell";
20
20
  export type OrderType = "limit" | "market";
21
21
  export type OrderStatus =
22
22
  | "pending" | "active" | "filled" | "expired"
23
- | "canceled" | "margin_canceled" | "invalid" | "post_only_refused";
23
+ | "canceled" | "margin_canceled" | "self_trade_canceled" | "invalid" | "post_only_refused";
24
24
  export type OrderKind =
25
25
  | "user_signed" | "liquidation" | "triggered" | "adl" | "adl_counterparty"
26
26
  /** A backstop transfer merged into the order feed as a synthetic terminal row. */
@@ -76,20 +76,21 @@ export interface AmendRejection {
76
76
  * Absent when the stream names a single account.
77
77
  */
78
78
  requestedBy?: Address;
79
- /** The batch the refusal landed in (ms). */
80
79
  batchMs: number;
81
80
  }
82
81
 
83
82
  /** What the engine did to an order, as `pod_orders_v2` reports it (ADR 0029 §3). */
84
83
  export type OrderEventKind =
85
- | "new" | "reject" | "fill" | "cancel" | "expire" | "modify" | "modify_reject";
84
+ | "new" | "reject" | "fill" | "cancel" | "expire" | "modify" | "modify_reject" | "cancel_reject";
86
85
 
87
86
  /**
88
87
  * The statuses an order can *close* with. Narrower than `OrderStatus`, which also
89
88
  * covers the states a live order passes through — so a `switch` over this one can be
90
89
  * exhaustive.
91
90
  */
92
- export type TerminalStatus = Extract<OrderStatus, "filled" | "canceled" | "margin_canceled" | "expired" | "post_only_refused">;
91
+ export type TerminalStatus = Extract<
92
+ OrderStatus, "filled" | "canceled" | "margin_canceled" | "self_trade_canceled" | "expired" | "post_only_refused"
93
+ >;
93
94
 
94
95
  /** One fill, plus the status it closed the order with when it did. */
95
96
  export interface OrderEventFill extends PartialFill {
@@ -110,6 +111,8 @@ export interface OrderEventFill extends PartialFill {
110
111
  * 0029 §6 reserves a `why` for that), so do not try to re-derive it from `status`.
111
112
  */
112
113
  closedAs?: TerminalStatus;
114
+ /** This fill's fee; absent on activity rows stored before it was recorded. */
115
+ fee?: bigint;
113
116
  }
114
117
 
115
118
  /**
@@ -149,6 +152,16 @@ export interface OrderEvent {
149
152
  * from REST.
150
153
  */
151
154
  amendRejection?: AmendRejection;
155
+ /** `cancel_reject` only. */
156
+ cancelRejection?: CancelRejection;
157
+ }
158
+
159
+ /** A cancel the engine refused; the order is untouched. */
160
+ export interface CancelRejection {
161
+ code: RejectCode;
162
+ message?: string;
163
+ /** The batch the refusal landed in (ms). */
164
+ batchMs: number;
152
165
  }
153
166
 
154
167
  export interface TokenInfo {
@@ -277,7 +290,6 @@ export interface Order {
277
290
  endMs?: number;
278
291
  /** Batch-inclusion time (ms) — when the order entered the book. */
279
292
  includedMs?: number;
280
- fills: PartialFill[];
281
293
  /** Why the engine rejected the order; set only on `status: "invalid"`. */
282
294
  rejectReason?: string;
283
295
  // perpetual-only
@@ -451,21 +463,53 @@ export interface CandleQuery {
451
463
  limit?: number;
452
464
  }
453
465
 
454
- export type ActivityType = "order" | "backstop" | "bridge_transfer" | "transfer";
466
+ export type ActivityType =
467
+ | "order" | "fill" | "modify" | "termination" | "backstop" | "bridge_transfer" | "transfer"
468
+ | "modify_reject" | "cancel_reject";
455
469
 
456
470
  /**
457
- * One row of an account's activity (ADR 0057): an order at its placement tick,
458
- * or money that moved.
459
- *
460
- * `timeMs` is what the row is ordered by. Fills, cancels and amendments are not
461
- * rows — they are events on the stream, and the order an `order` row carries
462
- * already reflects them.
463
- *
464
- * Money amounts are signed from the account's side — negative left, positive
465
- * arrived — and say nothing about the other end.
471
+ * One row of an account's activity (ADR 0057). Money amounts are signed from the
472
+ * account's side: negative left, positive arrived.
466
473
  */
467
474
  export type ActivityEntry =
475
+ /** The placement, carrying the order as every later row seen so far left it. */
468
476
  | { activityType: "order"; timeMs: number; order: Order }
477
+ | {
478
+ activityType: "fill";
479
+ timeMs: number;
480
+ orderId: Hash;
481
+ orderbookId: MarketId;
482
+ /** This fill alone. */
483
+ base: bigint;
484
+ quote: bigint;
485
+ /** Life-to-date totals; absent only on rows stored before they were recorded. */
486
+ totalBase?: bigint;
487
+ totalQuote?: bigint;
488
+ totalFee?: bigint;
489
+ /** This fill's fee; absent on rows stored before it was recorded. */
490
+ fee?: bigint;
491
+ closedAs?: TerminalStatus;
492
+ /** Perp only; signed. */
493
+ positionAfter?: bigint;
494
+ }
495
+ | {
496
+ activityType: "modify";
497
+ timeMs: number;
498
+ orderId: Hash;
499
+ orderbookId: MarketId;
500
+ price: bigint;
501
+ /** Unsigned magnitude. */
502
+ size: bigint;
503
+ oldPrice: bigint;
504
+ oldSize: bigint;
505
+ }
506
+ | {
507
+ activityType: "termination";
508
+ timeMs: number;
509
+ orderId: Hash;
510
+ orderbookId: MarketId;
511
+ status: TerminalStatus;
512
+ }
469
513
  | (BackstopTransfer & { activityType: "backstop"; timeMs: number })
470
514
  | {
471
515
  activityType: "bridge_transfer";
@@ -483,13 +527,33 @@ export type ActivityEntry =
483
527
  activityType: "transfer";
484
528
  timeMs: number;
485
529
  transferId: Hash;
530
+ /** The receiver when `amount` is negative, the sender otherwise; the account itself on a self-transfer. */
531
+ counterparty: Address;
486
532
  token: Address;
487
533
  amount: bigint;
488
534
  error?: string;
535
+ }
536
+ /** An amendment the engine refused; `message` is the engine's reason. */
537
+ | {
538
+ activityType: "modify_reject";
539
+ timeMs: number;
540
+ orderId: Hash;
541
+ orderbookId: MarketId;
542
+ requestedPrice: bigint;
543
+ requestedSize: bigint;
544
+ message: string;
545
+ }
546
+ /** A cancel the engine refused; `message` is the engine's reason. */
547
+ | {
548
+ activityType: "cancel_reject";
549
+ timeMs: number;
550
+ orderId: Hash;
551
+ orderbookId: MarketId;
552
+ message: string;
489
553
  };
490
554
 
491
- /** The activity rows that are not orders. */
492
- export type MoneyActivity = Exclude<ActivityEntry, { activityType: "order" }>;
555
+ /** The activity rows that move money. */
556
+ export type MoneyActivity = Extract<ActivityEntry, { activityType: "backstop" | "bridge_transfer" | "transfer" }>;
493
557
 
494
558
  /**
495
559
  * What the stream reported in one tick: the order transitions, and the money
@@ -501,14 +565,13 @@ export type MoneyActivity = Exclude<ActivityEntry, { activityType: "order" }>;
501
565
  export type ActivityEvent = OrderEvent | MoneyActivity;
502
566
 
503
567
  /**
504
- * `from` and `to` are milliseconds; `limit` is a row count, not a span of time.
505
- * `types` names the kinds to keep — absent or empty keeps every kind.
568
+ * `from` and `to` are milliseconds, both inclusive. `types` names the kinds to
569
+ * keep — absent or empty keeps every kind.
506
570
  */
507
571
  export interface ActivityQuery {
508
572
  types?: ActivityType[];
509
573
  from?: number;
510
574
  to?: number;
511
- limit?: number;
512
575
  }
513
576
 
514
577
  export interface OrdersQuery {
package/src/types/wire.ts CHANGED
@@ -91,13 +91,6 @@ export interface WireCandleTick {
91
91
  volume: WireDecimal;
92
92
  }
93
93
 
94
- export interface WirePartialFill {
95
- base_amount: WireDecimal;
96
- quote_amount: WireDecimal;
97
- timestamp: number; // micros
98
- price: WireDecimal;
99
- }
100
-
101
94
  export interface WireOrder {
102
95
  orderbook_id?: Hex;
103
96
  /** The order endpoints say "perpetual" where `/clob/markets` says "perp". */
@@ -118,13 +111,13 @@ export interface WireOrder {
118
111
  end: number | string; // micros
119
112
  included_batch?: number | string; // micros; batch-inclusion time (REST order history)
120
113
  effective_price?: WireDecimal | null;
121
- fills?: WirePartialFill[];
122
114
  side?: "buy" | "sell";
123
115
  reduce_only?: boolean;
124
116
  ioc?: boolean;
125
117
  direction?: string;
126
118
  grouping?: WireDecimal;
127
119
  trigger_type?: string | null;
120
+ invalid_reason?: string;
128
121
  }
129
122
 
130
123
  export interface WireOrdersPage {
@@ -269,8 +262,7 @@ export interface WireBackstopPage {
269
262
  // means something specific — noted per field.
270
263
 
271
264
  export interface WireOrdersFrame {
272
- /** The orderbook these actions happened on; constant for the frame. Absent on
273
- * `pod_activity`, which is one frame per tick and names the book per entity. */
265
+ /** The orderbook these actions happened on; constant for the frame. */
274
266
  book?: Hex;
275
267
  /** Deadline (micros) of the batch the actions **landed in**. Half of the resume cursor; `book` is the other half. */
276
268
  batch: number;
@@ -289,8 +281,6 @@ export interface WireOrderEntity {
289
281
  id: Hex;
290
282
  /** Creating transaction, or its parent `submitBatch` envelope. */
291
283
  tx: Hex;
292
- /** The order's book. Sent here by `pod_activity`, which covers a whole
293
- * account; `pod_orders_v2` names it once on the frame instead. */
294
284
  book?: Hex;
295
285
  /** Index into the frame's `accts`; present iff `accts` is. */
296
286
  a?: number;
@@ -357,13 +347,15 @@ export interface WireOrderEvent {
357
347
  tb?: WireDecimal;
358
348
  /** Total quote filled so far; travels with `tb`. */
359
349
  tq?: WireDecimal;
360
- /** Total fee charged so far; travels with `tb`. There is no per-fill counterpart. */
350
+ /** Total fee charged so far; travels with `tb`. */
361
351
  tf?: WireDecimal;
352
+ /** `fill` only: this fill's fee. */
353
+ f?: WireDecimal;
362
354
  /**
363
355
  * The terminal status an event closed the order with. On `fill`, only on the fill that
364
356
  * closed it. On `cancel`, only when the engine removed the order rather than the owner.
365
357
  */
366
- st?: "filled" | "canceled" | "margin_canceled" | "expired" | "post_only_refused";
358
+ st?: WireTerminalStatus;
367
359
  /**
368
360
  * `fill` on a perp market: the owner's position after this fill, signed and
369
361
  * 1e18-scaled. Omitted on spot, which has no position.
@@ -441,80 +433,79 @@ export interface WireWithdrawalDetail {
441
433
 
442
434
  // --- account activity (ADR 0057) ---
443
435
  //
444
- // `GET /clob/activity/{account}` serves the seed and `pod_activity` streams it.
445
- // The seed is a union tagged by `activity_type`: the order variant flattens the
446
- // orders route's `WireOrder`, and every money variant carries exactly the fields
447
- // its stream event does, so the two decode through one decoder. The channel is
448
- // `pod_orders_v2`'s frame for a whole account — one frame per tick, with the
449
- // money the tick moved as extra event kinds.
450
-
451
- export interface WireActivityPage {
452
- activity: WireActivityEntry[];
453
- next_cursor: string | null;
454
- solution_now: number; // micros
455
- }
436
+ // REST pages and `pod_activity` frames are `{ "<batch micros>": [entry, ...] }`; absent fields are absent, never null.
456
437
 
457
- /** What each money kind carries. Shared because the seed row and the stream
458
- * event differ only in how they are tagged. */
459
438
  export interface WireBackstopMoney {
460
439
  /** Absent on the terminal cash sweep, which belongs to no market. */
461
440
  book?: Hex;
462
- size: WireDecimal; // signed
441
+ sz: WireDecimal; // signed
463
442
  cash: WireDecimal; // signed
464
443
  mark: WireDecimal;
465
- equity: WireDecimal; // signed
444
+ eq: WireDecimal; // signed
466
445
  pnl: WireDecimal; // signed
467
446
  }
468
447
 
469
448
  export interface WireBridgeMoney {
470
449
  tx: Hex;
471
- /** Position within the transaction: one tx can bridge several amounts, so the
472
- * hash alone does not identify the row. */
473
450
  idx: number;
474
- token: Hex;
475
- amount: WireDecimal; // signed
476
- error?: string;
451
+ tok: Hex;
452
+ amt: WireDecimal; // signed
453
+ err?: string;
477
454
  }
478
455
 
479
456
  export interface WireTransferMoney {
480
457
  id: Hex;
481
- token: Hex;
482
- amount: WireDecimal; // signed
483
- error?: string;
458
+ /** The receiver when `amt` is negative, the sender otherwise. */
459
+ cp: Hex;
460
+ tok: Hex;
461
+ amt: WireDecimal; // signed
462
+ err?: string;
484
463
  }
485
464
 
486
- /**
487
- * One activity row. `ts` is what the node sorted the page by — for an order that
488
- * is its **signed deadline**, not the batch it landed in.
489
- *
490
- * Money amounts are signed from the account's side: negative left, positive
491
- * arrived, for the bridge and for a transfer alike.
492
- */
493
- export type WireActivityEntry =
494
- | ({ activity_type: "order"; ts: number } & WireOrder)
495
- | ({ activity_type: "backstop"; ts: number } & WireBackstopMoney)
496
- | ({ activity_type: "bridge_transfer"; ts: number } & WireBridgeMoney)
497
- | ({ activity_type: "transfer"; ts: number } & WireTransferMoney);
465
+ type WireTerminalStatus =
466
+ | "filled" | "canceled" | "margin_canceled" | "self_trade_canceled" | "expired" | "post_only_refused";
498
467
 
499
468
  /**
500
- * One tick of an account's activity. No `book` — a tick covers every book the
501
- * account traded on, so each entity names its own — and no `accts`, since the
502
- * channel takes exactly one account.
469
+ * An unknown `k` from a newer node is dropped by the decoder; it is not in this union
470
+ * because an open member would defeat narrowing on the known ones.
503
471
  */
504
- export interface WireActivityFrame {
505
- /** Deadline (micros) of the batch. One frame per tick, so this alone is the
506
- * resume cursor: a frame is already delivered exactly when `batch <= since`. */
507
- batch: number;
508
- orders: WireOrderEntity[];
509
- events: (WireOrderEvent | WireMoneyEvent)[];
510
- }
511
-
512
- /** Money the tick moved, discriminated by `k` — disjoint from the order kinds,
513
- * which is what lets the two share one untagged union on the wire. */
514
- export type WireMoneyEvent =
472
+ export type WireActivityEntry =
473
+ | ({ k: "order" } & WireOrder)
474
+ | {
475
+ k: "fill";
476
+ id: Hex;
477
+ book: Hex;
478
+ b: WireDecimal;
479
+ q: WireDecimal;
480
+ tb?: WireDecimal;
481
+ tq?: WireDecimal;
482
+ tf?: WireDecimal;
483
+ f?: WireDecimal;
484
+ st?: WireTerminalStatus;
485
+ pa?: WireDecimal;
486
+ }
487
+ | { k: "modify"; id: Hex; book: Hex; px: WireDecimal; sz: WireDecimal; old_px: WireDecimal; old_sz: WireDecimal }
488
+ | {
489
+ k: "termination";
490
+ id: Hex;
491
+ book: Hex;
492
+ st: Exclude<WireTerminalStatus, "filled">;
493
+ }
515
494
  | ({ k: "backstop" } & WireBackstopMoney)
516
495
  | ({ k: "bridge_transfer" } & WireBridgeMoney)
517
- | ({ k: "transfer" } & WireTransferMoney);
496
+ | ({ k: "transfer" } & WireTransferMoney)
497
+ | { k: "modify_reject"; id: Hex; book: Hex; px: WireDecimal; sz: WireDecimal; why: string }
498
+ | { k: "cancel_reject"; id: Hex; book: Hex; why: string };
499
+
500
+ /** Entries by batch (micros, decimal string key); the stream sends one key per message. */
501
+ export type WireActivityFrame = Record<string, WireActivityEntry[]>;
502
+
503
+ export interface WireActivityPage {
504
+ activity: WireActivityFrame;
505
+ /** Pass back as `to` for the next, older page; null when the window is exhausted. */
506
+ next: string | null;
507
+ solution_now: number; // micros
508
+ }
518
509
 
519
510
  /** One `/clob/fills/{account}` row: a fill of one order in one batch. */
520
511
  export interface WireFillRow {