@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.
- package/README.md +7 -3
- package/dist/client.d.ts +5 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -16
- package/dist/client.js.map +1 -1
- package/dist/codec/activity.d.ts +15 -10
- package/dist/codec/activity.d.ts.map +1 -1
- package/dist/codec/activity.js +166 -31
- package/dist/codec/activity.js.map +1 -1
- package/dist/codec/decode.d.ts +2 -14
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +1 -66
- package/dist/codec/decode.js.map +1 -1
- package/dist/codec/orders-v2.d.ts +16 -9
- package/dist/codec/orders-v2.d.ts.map +1 -1
- package/dist/codec/orders-v2.js +6 -16
- package/dist/codec/orders-v2.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/sync/activity.d.ts +19 -28
- package/dist/sync/activity.d.ts.map +1 -1
- package/dist/sync/activity.js +141 -167
- package/dist/sync/activity.js.map +1 -1
- package/dist/sync/orders.js +4 -4
- package/dist/sync/orders.js.map +1 -1
- package/dist/sync/pnl-history.d.ts.map +1 -1
- package/dist/sync/pnl-history.js +14 -9
- package/dist/sync/pnl-history.js.map +1 -1
- package/dist/transport/rest.d.ts +11 -12
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/transport/rest.js +11 -13
- package/dist/transport/rest.js.map +1 -1
- package/dist/transport/ws.d.ts +1 -3
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +78 -22
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +68 -63
- package/dist/types/wire.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +8 -11
- package/src/codec/activity.ts +167 -41
- package/src/codec/decode.ts +6 -74
- package/src/codec/orders-v2.ts +11 -26
- package/src/index.ts +2 -1
- package/src/sync/activity.ts +141 -167
- package/src/sync/orders.ts +4 -4
- package/src/sync/pnl-history.ts +13 -7
- package/src/transport/rest.ts +21 -21
- package/src/transport/ws.ts +1 -3
- package/src/types/public.ts +83 -20
- package/src/types/wire.ts +56 -65
package/src/types/public.ts
CHANGED
|
@@ -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<
|
|
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 =
|
|
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)
|
|
458
|
-
*
|
|
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
|
|
492
|
-
export type MoneyActivity =
|
|
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
|
|
505
|
-
*
|
|
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.
|
|
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`.
|
|
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?:
|
|
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
|
-
// `
|
|
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
|
-
|
|
441
|
+
sz: WireDecimal; // signed
|
|
463
442
|
cash: WireDecimal; // signed
|
|
464
443
|
mark: WireDecimal;
|
|
465
|
-
|
|
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
|
-
|
|
475
|
-
|
|
476
|
-
|
|
451
|
+
tok: Hex;
|
|
452
|
+
amt: WireDecimal; // signed
|
|
453
|
+
err?: string;
|
|
477
454
|
}
|
|
478
455
|
|
|
479
456
|
export interface WireTransferMoney {
|
|
480
457
|
id: Hex;
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
501
|
-
*
|
|
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
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
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 {
|