@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/codec/activity.ts
CHANGED
|
@@ -1,46 +1,172 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// reference and no state to mutate, so each event is decoded straight into the
|
|
7
|
-
// entry the REST seed would have served for it — in one pass over `frame.events`,
|
|
8
|
-
// so what a consumer sees is the node's own interleaving of the tick.
|
|
1
|
+
import type { ActivityEntry, ActivityEvent, Order, OrderEventKind } from "../types/public.js";
|
|
2
|
+
import type { WireActivityEntry, WireOrderEvent } from "../types/wire.js";
|
|
3
|
+
import { decodeOrder } from "./decode.js";
|
|
4
|
+
import { applyEvent } from "./orders-v2.js";
|
|
5
|
+
import { dec, decOpt, usToMs } from "./units.js";
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
7
|
+
/** One activity row, or `undefined` for an unknown kind. */
|
|
8
|
+
export function decodeActivityEntry(e: WireActivityEntry, timeMs: number): ActivityEntry | undefined {
|
|
9
|
+
switch (e.k) {
|
|
10
|
+
case "order":
|
|
11
|
+
return { activityType: "order", timeMs, order: decodeOrder(e) };
|
|
12
|
+
case "fill":
|
|
13
|
+
return {
|
|
14
|
+
activityType: "fill",
|
|
15
|
+
timeMs,
|
|
16
|
+
orderId: e.id,
|
|
17
|
+
orderbookId: e.book,
|
|
18
|
+
base: dec(e.b),
|
|
19
|
+
quote: dec(e.q),
|
|
20
|
+
totalBase: decOpt(e.tb),
|
|
21
|
+
totalQuote: decOpt(e.tq),
|
|
22
|
+
totalFee: decOpt(e.tf),
|
|
23
|
+
fee: decOpt(e.f),
|
|
24
|
+
closedAs: e.st,
|
|
25
|
+
positionAfter: decOpt(e.pa),
|
|
26
|
+
};
|
|
27
|
+
case "modify":
|
|
28
|
+
return {
|
|
29
|
+
activityType: "modify",
|
|
30
|
+
timeMs,
|
|
31
|
+
orderId: e.id,
|
|
32
|
+
orderbookId: e.book,
|
|
33
|
+
price: dec(e.px),
|
|
34
|
+
size: dec(e.sz),
|
|
35
|
+
oldPrice: dec(e.old_px),
|
|
36
|
+
oldSize: dec(e.old_sz),
|
|
37
|
+
};
|
|
38
|
+
case "termination":
|
|
39
|
+
return {
|
|
40
|
+
activityType: "termination",
|
|
41
|
+
timeMs,
|
|
42
|
+
orderId: e.id,
|
|
43
|
+
orderbookId: e.book,
|
|
44
|
+
status: e.st,
|
|
45
|
+
};
|
|
46
|
+
case "backstop":
|
|
47
|
+
return {
|
|
48
|
+
activityType: "backstop",
|
|
49
|
+
timeMs,
|
|
50
|
+
time: timeMs,
|
|
51
|
+
orderbookId: e.book,
|
|
52
|
+
size: dec(e.sz),
|
|
53
|
+
cash: dec(e.cash),
|
|
54
|
+
markPrice: dec(e.mark),
|
|
55
|
+
equity: dec(e.eq),
|
|
56
|
+
realizedPnl: dec(e.pnl),
|
|
57
|
+
};
|
|
58
|
+
case "bridge_transfer":
|
|
59
|
+
return {
|
|
60
|
+
activityType: "bridge_transfer",
|
|
61
|
+
timeMs,
|
|
62
|
+
txHash: e.tx,
|
|
63
|
+
idx: e.idx,
|
|
64
|
+
token: e.tok,
|
|
65
|
+
amount: dec(e.amt),
|
|
66
|
+
// An empty string is the wire saying "no reason".
|
|
67
|
+
error: e.err || undefined,
|
|
68
|
+
};
|
|
69
|
+
case "transfer":
|
|
70
|
+
return {
|
|
71
|
+
activityType: "transfer",
|
|
72
|
+
timeMs,
|
|
73
|
+
transferId: e.id,
|
|
74
|
+
counterparty: e.cp,
|
|
75
|
+
token: e.tok,
|
|
76
|
+
amount: dec(e.amt),
|
|
77
|
+
error: e.err || undefined,
|
|
78
|
+
};
|
|
79
|
+
case "modify_reject":
|
|
80
|
+
return {
|
|
81
|
+
activityType: "modify_reject",
|
|
82
|
+
timeMs,
|
|
83
|
+
orderId: e.id,
|
|
84
|
+
orderbookId: e.book,
|
|
85
|
+
requestedPrice: dec(e.px),
|
|
86
|
+
requestedSize: dec(e.sz),
|
|
87
|
+
message: e.why,
|
|
88
|
+
};
|
|
89
|
+
case "cancel_reject":
|
|
90
|
+
return {
|
|
91
|
+
activityType: "cancel_reject",
|
|
92
|
+
timeMs,
|
|
93
|
+
orderId: e.id,
|
|
94
|
+
orderbookId: e.book,
|
|
95
|
+
message: e.why,
|
|
96
|
+
};
|
|
97
|
+
default:
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
23
101
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
102
|
+
function asOrderEvent(e: WireActivityEntry): WireOrderEvent | undefined {
|
|
103
|
+
switch (e.k) {
|
|
104
|
+
case "fill":
|
|
105
|
+
return { k: "fill", id: e.id, b: e.b, q: e.q, tb: e.tb, tq: e.tq, tf: e.tf, f: e.f, st: e.st, pa: e.pa };
|
|
106
|
+
case "modify":
|
|
107
|
+
return { k: "modify", id: e.id, px: e.px, sz: e.sz };
|
|
108
|
+
case "termination":
|
|
109
|
+
return { k: e.st === "expired" ? "expire" : "cancel", id: e.id, st: e.st };
|
|
110
|
+
default:
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
27
113
|
}
|
|
28
114
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Decode one batch, setting its `order` rows in `orders` and applying transitions to the
|
|
117
|
+
* orders it holds. Only a `stream` batch emits events. A REST page's `order` rows already
|
|
118
|
+
* carry their current state, so a REST batch applies nothing to an order in `pageOrders`.
|
|
119
|
+
*/
|
|
120
|
+
export function foldActivityBatch(
|
|
121
|
+
batchUs: number,
|
|
122
|
+
entries: WireActivityEntry[],
|
|
123
|
+
orders: Map<string, Order>,
|
|
124
|
+
source: "stream" | { pageOrders: ReadonlySet<string> },
|
|
125
|
+
): { rows: ActivityEntry[]; events: ActivityEvent[] } {
|
|
126
|
+
const rest = source !== "stream";
|
|
127
|
+
const batchMs = usToMs(batchUs);
|
|
128
|
+
const rows: ActivityEntry[] = [];
|
|
129
|
+
const events: ActivityEvent[] = [];
|
|
130
|
+
for (const e of entries) {
|
|
131
|
+
const row = decodeActivityEntry(e, batchMs);
|
|
132
|
+
if (!row) continue;
|
|
133
|
+
rows.push(row);
|
|
134
|
+
if (row.activityType === "order") {
|
|
135
|
+
orders.set(row.order.id, row.order);
|
|
136
|
+
if (!rest) {
|
|
137
|
+
events.push({ kind: row.order.status === "invalid" ? "reject" : "new", order: row.order, batchMs });
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (row.activityType === "backstop" || row.activityType === "bridge_transfer" || row.activityType === "transfer") {
|
|
142
|
+
if (!rest) events.push(row);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const order = orders.get(row.orderId);
|
|
146
|
+
if (!order || (rest && source.pageOrders.has(row.orderId))) continue;
|
|
147
|
+
if (rest) {
|
|
148
|
+
const event = asOrderEvent(e);
|
|
149
|
+
if (event) applyEvent(event, order, { batchMs });
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (row.activityType === "modify_reject") {
|
|
153
|
+
const { requestedPrice, requestedSize, message } = row;
|
|
154
|
+
events.push({
|
|
155
|
+
kind: "modify_reject", order, batchMs,
|
|
156
|
+
amendRejection: { requestedPrice, requestedSize, code: "unspecified", message, batchMs },
|
|
157
|
+
});
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (row.activityType === "cancel_reject") {
|
|
161
|
+
events.push({
|
|
162
|
+
kind: "cancel_reject", order, batchMs,
|
|
163
|
+
cancelRejection: { code: "unspecified", message: row.message, batchMs },
|
|
164
|
+
});
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const event = asOrderEvent(e);
|
|
168
|
+
const outcome = event && applyEvent(event, order, { batchMs });
|
|
169
|
+
if (event && outcome) events.push({ kind: event.k as OrderEventKind, order, batchMs, fill: outcome.fill });
|
|
170
|
+
}
|
|
171
|
+
return { rows, events };
|
|
46
172
|
}
|
package/src/codec/decode.ts
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
// representations are normalized into bigint + millisecond numbers.
|
|
3
3
|
|
|
4
4
|
import type {
|
|
5
|
-
|
|
6
|
-
Orderbook,
|
|
7
|
-
SpotPosition, Status, Trigger, MarketType,
|
|
5
|
+
Bar, BackstopTransfer, Balances, BridgeConfig, Market, Order,
|
|
6
|
+
Orderbook, PerpPosition, Position, PositionsSnapshot, SpotHolding,
|
|
7
|
+
SpotPosition, Status, Trigger, MarketType, OrderDirection, OrderKind,
|
|
8
8
|
OrderStatus, TriggerType, Withdrawal,
|
|
9
9
|
} from "../types/public.js";
|
|
10
10
|
import type {
|
|
11
|
-
|
|
12
|
-
WireMarketDynamics, WireMarketStatic,
|
|
11
|
+
WireBackstopTransfer, WireBalances, WireBridgeConfig, WireCandle,
|
|
12
|
+
WireMarketDynamics, WireMarketStatic, WireOrder, WireOrderbook,
|
|
13
13
|
WirePerpPosition, WirePosition, WirePositionsSnapshot, WireSpotHolding, WireSpotPosition,
|
|
14
14
|
WireStatus, WireTrigger, WireWithdrawal,
|
|
15
15
|
} from "../types/wire.js";
|
|
@@ -78,15 +78,6 @@ export function decodeCandle(w: WireCandle): Bar {
|
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
function decodePartialFill(w: WirePartialFill): PartialFill {
|
|
82
|
-
return {
|
|
83
|
-
base: dec(w.base_amount),
|
|
84
|
-
quote: dec(w.quote_amount),
|
|
85
|
-
price: dec(w.price),
|
|
86
|
-
time: usToMs(w.timestamp),
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
81
|
export function decodeOrder(w: WireOrder): Order {
|
|
91
82
|
const initialSize = dec(w.initial_size);
|
|
92
83
|
return {
|
|
@@ -112,7 +103,7 @@ export function decodeOrder(w: WireOrder): Order {
|
|
|
112
103
|
deadlineMs: usToMs(w.deadline),
|
|
113
104
|
endMs: endMsFromUs(w.end),
|
|
114
105
|
includedMs: usToMsOpt(w.included_batch),
|
|
115
|
-
|
|
106
|
+
rejectReason: w.invalid_reason,
|
|
116
107
|
reduceOnly: w.reduce_only,
|
|
117
108
|
ioc: w.ioc,
|
|
118
109
|
direction: w.direction as OrderDirection | undefined,
|
|
@@ -268,65 +259,6 @@ export function decodeBackstopTransfer(w: WireBackstopTransfer): BackstopTransfe
|
|
|
268
259
|
};
|
|
269
260
|
}
|
|
270
261
|
|
|
271
|
-
/** One money row (ADR 0057), from a seed entry or a stream event alike — the two
|
|
272
|
-
* carry the same fields under different tags. */
|
|
273
|
-
export function decodeMoneyEvent(event: WireMoneyEvent, timeMs: number): MoneyActivity {
|
|
274
|
-
switch (event.k) {
|
|
275
|
-
case "backstop":
|
|
276
|
-
return {
|
|
277
|
-
activityType: "backstop",
|
|
278
|
-
timeMs,
|
|
279
|
-
time: timeMs,
|
|
280
|
-
orderbookId: event.book,
|
|
281
|
-
size: dec(event.size),
|
|
282
|
-
cash: dec(event.cash),
|
|
283
|
-
markPrice: dec(event.mark),
|
|
284
|
-
equity: dec(event.equity),
|
|
285
|
-
realizedPnl: dec(event.pnl),
|
|
286
|
-
};
|
|
287
|
-
case "bridge_transfer":
|
|
288
|
-
return {
|
|
289
|
-
activityType: "bridge_transfer",
|
|
290
|
-
timeMs,
|
|
291
|
-
txHash: event.tx,
|
|
292
|
-
idx: event.idx,
|
|
293
|
-
token: event.token,
|
|
294
|
-
amount: dec(event.amount),
|
|
295
|
-
// `||`, as `decodeWithdrawal` does: an empty string is the wire saying
|
|
296
|
-
// "no reason", and it would otherwise read as a failure.
|
|
297
|
-
error: event.error || undefined,
|
|
298
|
-
};
|
|
299
|
-
case "transfer":
|
|
300
|
-
return {
|
|
301
|
-
activityType: "transfer",
|
|
302
|
-
timeMs,
|
|
303
|
-
transferId: event.id,
|
|
304
|
-
token: event.token,
|
|
305
|
-
amount: dec(event.amount),
|
|
306
|
-
error: event.error || undefined,
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* One activity row (ADR 0057), reusing the decoder its kind already had, or
|
|
313
|
-
* `undefined` for a kind this version does not know — the same silence the frame
|
|
314
|
-
* path keeps, rather than a row with no tag a consumer could act on.
|
|
315
|
-
*
|
|
316
|
-
* `timeMs` is the node's own sort key, which for an order is its **signed
|
|
317
|
-
* deadline** rather than the batch it landed in — `order.includedMs` is that.
|
|
318
|
-
*/
|
|
319
|
-
export function decodeActivityEntry(w: WireActivityEntry): ActivityEntry | undefined {
|
|
320
|
-
const timeMs = usToMs(w.ts);
|
|
321
|
-
switch (w.activity_type) {
|
|
322
|
-
case "order": return { activityType: "order", timeMs, order: decodeOrder(w) };
|
|
323
|
-
case "backstop": return decodeMoneyEvent({ ...w, k: "backstop" }, timeMs);
|
|
324
|
-
case "bridge_transfer": return decodeMoneyEvent({ ...w, k: "bridge_transfer" }, timeMs);
|
|
325
|
-
case "transfer": return decodeMoneyEvent({ ...w, k: "transfer" }, timeMs);
|
|
326
|
-
default: return undefined;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
262
|
export function decodeBridgeConfig(w: WireBridgeConfig): BridgeConfig {
|
|
331
263
|
return {
|
|
332
264
|
claimChainId: w.claim_chain_id,
|
package/src/codec/orders-v2.ts
CHANGED
|
@@ -11,23 +11,17 @@ import type {
|
|
|
11
11
|
PartialFill, RejectCode, TerminalStatus,
|
|
12
12
|
} from "../types/public.js";
|
|
13
13
|
import type { WireOrderEntity, WireOrderEvent, WireOrdersFrame } from "../types/wire.js";
|
|
14
|
-
import { dec, endMsFromUs, usToMs } from "./units.js";
|
|
14
|
+
import { dec, decOpt, endMsFromUs, usToMs } from "./units.js";
|
|
15
15
|
import { div } from "./fixed.js";
|
|
16
16
|
import { classifyPerpDirection } from "./direction.js";
|
|
17
17
|
|
|
18
|
-
export interface OrdersFrameContext
|
|
18
|
+
export interface OrdersFrameContext {
|
|
19
19
|
/** The account the subscription is filtered to, used when the frame omits `accts`. */
|
|
20
20
|
account: Address;
|
|
21
|
-
/**
|
|
22
|
-
* Events this codec applied nothing for, offered back in wire order — how a
|
|
23
|
-
* superset channel (`pod_activity`) folds its own kinds without a second pass
|
|
24
|
-
* that would report the tick's money after all of its order flow.
|
|
25
|
-
*/
|
|
26
|
-
foreign?(event: WireOrderEvent): X | undefined;
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
/** Per-frame facts the events need. */
|
|
30
|
-
interface FrameFacts {
|
|
24
|
+
export interface FrameFacts {
|
|
31
25
|
batchMs: number;
|
|
32
26
|
accts?: WireOrdersFrame["accts"];
|
|
33
27
|
}
|
|
@@ -43,17 +37,17 @@ interface FrameFacts {
|
|
|
43
37
|
* mid-frame: they are collected while applying but the rows are mutated in place, so a
|
|
44
38
|
* consumer never sees a half-applied order.
|
|
45
39
|
*/
|
|
46
|
-
export function applyOrdersFrame
|
|
40
|
+
export function applyOrdersFrame(
|
|
47
41
|
frame: WireOrdersFrame,
|
|
48
42
|
byId: Map<string, Order>,
|
|
49
|
-
ctx: OrdersFrameContext
|
|
50
|
-
):
|
|
43
|
+
ctx: OrdersFrameContext,
|
|
44
|
+
): OrderEvent[] {
|
|
51
45
|
// The book and the batch are frame constants: resolved once, not per entity.
|
|
52
46
|
const batchMs = usToMs(frame.batch);
|
|
53
47
|
const facts: FrameFacts = { batchMs, accts: frame.accts };
|
|
54
48
|
const created = frame.orders.map((e) => decodeEntity(e, frame, batchMs, ctx.account));
|
|
55
49
|
|
|
56
|
-
const applied:
|
|
50
|
+
const applied: OrderEvent[] = [];
|
|
57
51
|
for (const event of frame.events) {
|
|
58
52
|
// `o` indexes this frame's entities and `id` names one resting from an earlier
|
|
59
53
|
// batch — but an event kind added later (ADR 0029 §6 reserves several) may name
|
|
@@ -75,10 +69,7 @@ export function applyOrdersFrame<X = never>(
|
|
|
75
69
|
fill: outcome.fill,
|
|
76
70
|
amendRejection: outcome.amendRejection,
|
|
77
71
|
});
|
|
78
|
-
continue;
|
|
79
72
|
}
|
|
80
|
-
const foreign = ctx.foreign?.(event);
|
|
81
|
-
if (foreign !== undefined) applied.push(foreign);
|
|
82
73
|
}
|
|
83
74
|
|
|
84
75
|
// After the events, not before: an entity's own `new`/`reject` decides the
|
|
@@ -122,7 +113,6 @@ function decodeEntity(e: WireOrderEntity, frame: WireOrdersFrame, batchMs: numbe
|
|
|
122
113
|
// inclusion time is the one thing every source agrees on and the only honest
|
|
123
114
|
// key to order by.
|
|
124
115
|
includedMs: batchMs,
|
|
125
|
-
fills: [],
|
|
126
116
|
reduceOnly: e.reduce_only ?? false,
|
|
127
117
|
ioc: e.ioc ?? false,
|
|
128
118
|
triggerType: e.trigger as Order["triggerType"],
|
|
@@ -138,6 +128,8 @@ function decodeEntity(e: WireOrderEntity, frame: WireOrdersFrame, batchMs: numbe
|
|
|
138
128
|
* accumulated, so a client that missed a fill cannot drift.
|
|
139
129
|
*/
|
|
140
130
|
function applyTotals(event: WireOrderEvent, order: Order): void {
|
|
131
|
+
// Activity terminations, and activity fills stored before totals were recorded, carry none; `pod_orders_v2` always does.
|
|
132
|
+
if (event.tb === undefined) return;
|
|
141
133
|
order.filledBase = dec(event.tb);
|
|
142
134
|
order.filledQuote = dec(event.tq);
|
|
143
135
|
order.fee = dec(event.tf);
|
|
@@ -150,7 +142,7 @@ function applyTotals(event: WireOrderEvent, order: Order): void {
|
|
|
150
142
|
* Apply one event. `undefined` when the kind is not one this version knows; otherwise what
|
|
151
143
|
* the event contributed beyond the order's own state — the fill, or the refused amendment.
|
|
152
144
|
*/
|
|
153
|
-
function applyEvent(
|
|
145
|
+
export function applyEvent(
|
|
154
146
|
event: WireOrderEvent,
|
|
155
147
|
order: Order,
|
|
156
148
|
facts: FrameFacts,
|
|
@@ -186,11 +178,6 @@ function applyEvent(
|
|
|
186
178
|
const base = dec(event.b);
|
|
187
179
|
const quote = dec(event.q);
|
|
188
180
|
const fill: PartialFill = { base, quote, price: div(quote, base), time: facts.batchMs };
|
|
189
|
-
// A real fill always has size. A zero-size one is the engine flipping status: the
|
|
190
|
-
// cap-to-filled amendment fabricates it so `st: filled` reaches the indexer, which
|
|
191
|
-
// reads status from fills alone. Its totals and status count; adding a
|
|
192
|
-
// `0.0000 @ $0.00` row to the order's fill history would only invent a trade.
|
|
193
|
-
if (base !== 0n) order.fills = [...order.fills, fill];
|
|
194
181
|
// A perp fill reports the position it left. The position it started from is
|
|
195
182
|
// that minus what this fill moved it — a buy raises it, a sell lowers it — and
|
|
196
183
|
// the transition is what says whether the order opened, added to, reduced,
|
|
@@ -208,10 +195,8 @@ function applyEvent(
|
|
|
208
195
|
}
|
|
209
196
|
// Present only on the fill that closed the order.
|
|
210
197
|
if (event.st) order.status = event.st as OrderStatus;
|
|
211
|
-
// Copied rather than shared: the same object is in `order.fills`, and attaching a
|
|
212
|
-
// per-event field to it would leak the event into the order's fill history.
|
|
213
198
|
return {
|
|
214
|
-
fill: { ...fill, totalBase:
|
|
199
|
+
fill: { ...fill, totalBase: order.filledBase, closedAs: event.st as TerminalStatus | undefined, fee: decOpt(event.f) },
|
|
215
200
|
};
|
|
216
201
|
}
|
|
217
202
|
case "modify_reject":
|
package/src/index.ts
CHANGED
|
@@ -35,7 +35,7 @@ export {
|
|
|
35
35
|
export type { SeriesResource } from "./sync/candles.js";
|
|
36
36
|
export { CandleSeries } from "./sync/candles.js";
|
|
37
37
|
export { OrderHistory } from "./sync/orders.js";
|
|
38
|
-
export { ActivityHistory } from "./sync/activity.js";
|
|
38
|
+
export { ActivityHistory, type ActivityView } from "./sync/activity.js";
|
|
39
39
|
export {
|
|
40
40
|
fetchPnlHistory, streamPnlHistory, PnlHistoryCache,
|
|
41
41
|
type PnlHistory, type PnlHistoryChunk, type PnlHistoryDeps, type PnlHistoryQuery, type PnlPoint,
|
|
@@ -46,6 +46,7 @@ export { createPodDatafeed } from "./datafeed.js";
|
|
|
46
46
|
|
|
47
47
|
// Public data types
|
|
48
48
|
export * from "./types/public.js";
|
|
49
|
+
export type { WireActivityEntry, WireActivityFrame } from "./types/wire.js";
|
|
49
50
|
|
|
50
51
|
// Codec helpers
|
|
51
52
|
export {
|