@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders-v2.d.ts","sourceRoot":"","sources":["../../src/codec/orders-v2.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,OAAO,
|
|
1
|
+
{"version":3,"file":"orders-v2.d.ts","sourceRoot":"","sources":["../../src/codec/orders-v2.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,UAAU,EAAE,cAAc,EAE3D,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAmB,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAKzF,MAAM,WAAW,kBAAkB;IACjC,sFAAsF;IACtF,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,uCAAuC;AACvC,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC;CAClC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,eAAe,EACtB,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,EACxB,GAAG,EAAE,kBAAkB,GACtB,UAAU,EAAE,CAqCd;AA4DD;;;GAGG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,UAAU,GAChB;IAAE,IAAI,CAAC,EAAE,cAAc,CAAC;IAAC,cAAc,CAAC,EAAE,cAAc,CAAA;CAAE,GAAG,SAAS,CAwExE"}
|
package/dist/codec/orders-v2.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// materialize this batch's entities, then let the events say what happened to
|
|
6
6
|
// them and to orders already held. Status lives only in the events, which is what
|
|
7
7
|
// lets the server add transition kinds without changing the entity.
|
|
8
|
-
import { dec, endMsFromUs, usToMs } from "./units.js";
|
|
8
|
+
import { dec, decOpt, endMsFromUs, usToMs } from "./units.js";
|
|
9
9
|
import { div } from "./fixed.js";
|
|
10
10
|
import { classifyPerpDirection } from "./direction.js";
|
|
11
11
|
/**
|
|
@@ -46,11 +46,7 @@ export function applyOrdersFrame(frame, byId, ctx) {
|
|
|
46
46
|
fill: outcome.fill,
|
|
47
47
|
amendRejection: outcome.amendRejection,
|
|
48
48
|
});
|
|
49
|
-
continue;
|
|
50
49
|
}
|
|
51
|
-
const foreign = ctx.foreign?.(event);
|
|
52
|
-
if (foreign !== undefined)
|
|
53
|
-
applied.push(foreign);
|
|
54
50
|
}
|
|
55
51
|
// After the events, not before: an entity's own `new`/`reject` decides the
|
|
56
52
|
// status it lands with. Entities left untouched by every event this version
|
|
@@ -93,7 +89,6 @@ function decodeEntity(e, frame, batchMs, account) {
|
|
|
93
89
|
// inclusion time is the one thing every source agrees on and the only honest
|
|
94
90
|
// key to order by.
|
|
95
91
|
includedMs: batchMs,
|
|
96
|
-
fills: [],
|
|
97
92
|
reduceOnly: e.reduce_only ?? false,
|
|
98
93
|
ioc: e.ioc ?? false,
|
|
99
94
|
triggerType: e.trigger,
|
|
@@ -108,6 +103,9 @@ function decodeEntity(e, frame, batchMs, account) {
|
|
|
108
103
|
* accumulated, so a client that missed a fill cannot drift.
|
|
109
104
|
*/
|
|
110
105
|
function applyTotals(event, order) {
|
|
106
|
+
// Activity terminations, and activity fills stored before totals were recorded, carry none; `pod_orders_v2` always does.
|
|
107
|
+
if (event.tb === undefined)
|
|
108
|
+
return;
|
|
111
109
|
order.filledBase = dec(event.tb);
|
|
112
110
|
order.filledQuote = dec(event.tq);
|
|
113
111
|
order.fee = dec(event.tf);
|
|
@@ -119,7 +117,7 @@ function applyTotals(event, order) {
|
|
|
119
117
|
* Apply one event. `undefined` when the kind is not one this version knows; otherwise what
|
|
120
118
|
* the event contributed beyond the order's own state — the fill, or the refused amendment.
|
|
121
119
|
*/
|
|
122
|
-
function applyEvent(event, order, facts) {
|
|
120
|
+
export function applyEvent(event, order, facts) {
|
|
123
121
|
switch (event.k) {
|
|
124
122
|
case "new":
|
|
125
123
|
order.status = "active";
|
|
@@ -151,12 +149,6 @@ function applyEvent(event, order, facts) {
|
|
|
151
149
|
const base = dec(event.b);
|
|
152
150
|
const quote = dec(event.q);
|
|
153
151
|
const fill = { base, quote, price: div(quote, base), time: facts.batchMs };
|
|
154
|
-
// A real fill always has size. A zero-size one is the engine flipping status: the
|
|
155
|
-
// cap-to-filled amendment fabricates it so `st: filled` reaches the indexer, which
|
|
156
|
-
// reads status from fills alone. Its totals and status count; adding a
|
|
157
|
-
// `0.0000 @ $0.00` row to the order's fill history would only invent a trade.
|
|
158
|
-
if (base !== 0n)
|
|
159
|
-
order.fills = [...order.fills, fill];
|
|
160
152
|
// A perp fill reports the position it left. The position it started from is
|
|
161
153
|
// that minus what this fill moved it — a buy raises it, a sell lowers it — and
|
|
162
154
|
// the transition is what says whether the order opened, added to, reduced,
|
|
@@ -175,10 +167,8 @@ function applyEvent(event, order, facts) {
|
|
|
175
167
|
// Present only on the fill that closed the order.
|
|
176
168
|
if (event.st)
|
|
177
169
|
order.status = event.st;
|
|
178
|
-
// Copied rather than shared: the same object is in `order.fills`, and attaching a
|
|
179
|
-
// per-event field to it would leak the event into the order's fill history.
|
|
180
170
|
return {
|
|
181
|
-
fill: { ...fill, totalBase:
|
|
171
|
+
fill: { ...fill, totalBase: order.filledBase, closedAs: event.st, fee: decOpt(event.f) },
|
|
182
172
|
};
|
|
183
173
|
}
|
|
184
174
|
case "modify_reject":
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders-v2.js","sourceRoot":"","sources":["../../src/codec/orders-v2.ts"],"names":[],"mappings":"AAAA,+DAA+D;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KAAK,CAAC,EAAgC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;aACvH,CAAC;QACJ,CAAC;QACD,KAAK,eAAe;YAClB,qFAAqF;YACrF,mFAAmF;YACnF,+EAA+E;YAC/E,kFAAkF;YAClF,kEAAkE;YAClE,OAAO;gBACL,cAAc,EAAE;oBACd,cAAc,EAAE,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC;oBACjC,aAAa,EAAE,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC;oBAChC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,IAAI,aAAa,CAAe;oBACjD,OAAO,EAAE,KAAK,CAAC,GAAG;oBAClB,WAAW,EAAE,KAAK,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS;oBACzE,OAAO,EAAE,KAAK,CAAC,OAAO;iBACvB;aACF,CAAC;QACJ,uEAAuE;QACvE,wDAAwD;IAC1D,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -13,10 +13,11 @@ export { previewOrder, type OrderPreview, type OrderPreviewInput, closePreview,
|
|
|
13
13
|
export type { SeriesResource } from "./sync/candles.js";
|
|
14
14
|
export { CandleSeries } from "./sync/candles.js";
|
|
15
15
|
export { OrderHistory } from "./sync/orders.js";
|
|
16
|
-
export { ActivityHistory } from "./sync/activity.js";
|
|
16
|
+
export { ActivityHistory, type ActivityView } from "./sync/activity.js";
|
|
17
17
|
export { fetchPnlHistory, streamPnlHistory, PnlHistoryCache, type PnlHistory, type PnlHistoryChunk, type PnlHistoryDeps, type PnlHistoryQuery, type PnlPoint, } from "./sync/pnl-history.js";
|
|
18
18
|
export { createPodDatafeed } from "./datafeed.js";
|
|
19
19
|
export * from "./types/public.js";
|
|
20
|
+
export type { WireActivityEntry, WireActivityFrame } from "./types/wire.js";
|
|
20
21
|
export { formatAmount, formatPrice, decimalsForTick, alignSize, toNumber, parseAmount, dec, WAD, WAD_DECIMALS, } from "./codec/units.js";
|
|
21
22
|
export { RESOLUTION_SECONDS, RESOLUTION_PAGE_BUCKETS, RESOLUTIONS, isResolution, } from "./codec/resolution.js";
|
|
22
23
|
export { withdrawStep, quantizeWithdrawAmount, toClaimAmount, toPodAmount, maxWithdrawable, checkWithdrawAmount, type WithdrawRejection, bridgeTokenFor, } from "./codec/bridge.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,YAAY,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGtD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAClE,YAAY,EACV,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAC5E,WAAW,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAE,iBAAiB,EAChF,iBAAiB,GAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAC5E,YAAY,EACV,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,EACzE,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAG3B,YAAY,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EACL,cAAc,EAAE,KAAK,cAAc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AAmB,EAAE,KAAK,iBAAiB,EAAE,cAAc,GAC7E,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,gBAAgB,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EACxD,KAAK,WAAW,EAAE,KAAK,YAAY,EAAE,KAAK,mBAAmB,EAC7D,KAAK,YAAY,EAAE,KAAK,kBAAkB,GAC3C,MAAM,sBAAsB,CAAC;AAG9B,YAAY,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAE5E,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAI7C,0EAA0E;AAC1E,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAMlE,OAAO,EAAE,WAAW,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAQ5E,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EACL,cAAc,EACd,kBAAkB,GACnB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,eAAe,EACf,YAAY,GACb,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAE5E,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAI7C,0EAA0E;AAC1E,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAA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ts
CHANGED
|
@@ -1,34 +1,27 @@
|
|
|
1
1
|
import type { ActivityEntry, ActivityEvent, ActivityQuery, Address } from "../types/public.js";
|
|
2
2
|
import type { SeriesResource } from "./candles.js";
|
|
3
3
|
import type { SyncContext } from "./sources.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*/
|
|
8
|
-
export declare
|
|
9
|
-
export declare class ActivityHistory implements SeriesResource<ActivityEntry> {
|
|
4
|
+
export interface ActivityView extends SeriesResource<ActivityEntry> {
|
|
5
|
+
onEvent(listener: (events: ActivityEvent[]) => void): () => void;
|
|
6
|
+
}
|
|
7
|
+
/** One account's whole feed: the one subscription, batch map and paging state its views share. */
|
|
8
|
+
export declare class ActivityHistory implements ActivityView {
|
|
10
9
|
private readonly ctx;
|
|
11
10
|
private readonly account;
|
|
12
11
|
private readonly base;
|
|
13
12
|
private handle;
|
|
13
|
+
/** Every batch's rows, from either source; what dedupes one against the other. */
|
|
14
|
+
private readonly batches;
|
|
14
15
|
private readonly orders;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* it: the node times an order by its signed deadline, which no frame carries, so
|
|
18
|
-
* neither a later frame nor a re-seed must be able to retime a row already placed.
|
|
19
|
-
*/
|
|
20
|
-
private readonly orderTimeMs;
|
|
21
|
-
/** The money rows, by {@link moneyKey}. */
|
|
22
|
-
private readonly money;
|
|
23
|
-
private nextCursor;
|
|
24
|
-
private painted;
|
|
16
|
+
private rows;
|
|
17
|
+
private nextUs;
|
|
25
18
|
private seedGen;
|
|
26
19
|
private _hasMore;
|
|
27
20
|
private _loading;
|
|
28
21
|
private readonly eventListeners;
|
|
29
22
|
private readonly stream;
|
|
30
|
-
private readonly
|
|
31
|
-
constructor(ctx: SyncContext, account: Address
|
|
23
|
+
private readonly views;
|
|
24
|
+
constructor(ctx: SyncContext, account: Address);
|
|
32
25
|
get(): ActivityEntry[] | undefined;
|
|
33
26
|
subscribe(listener: () => void): () => void;
|
|
34
27
|
ready(): Promise<ActivityEntry[]>;
|
|
@@ -36,23 +29,21 @@ export declare class ActivityHistory implements SeriesResource<ActivityEntry> {
|
|
|
36
29
|
hasMore(): boolean;
|
|
37
30
|
loading(): boolean;
|
|
38
31
|
destroy(): void;
|
|
32
|
+
/** The rows of `types` whose `timeMs` is within inclusive `from`/`to`. */
|
|
33
|
+
view(query: ActivityQuery): ActivityView;
|
|
39
34
|
/**
|
|
40
35
|
* Observe what the stream reported, a tick at a time: the order transitions and
|
|
41
|
-
* the money that moved. Nothing is emitted for
|
|
36
|
+
* the money that moved. Nothing is emitted for REST pages.
|
|
42
37
|
*/
|
|
43
38
|
onEvent(listener: (events: ActivityEvent[]) => void): () => void;
|
|
44
39
|
setWindow(): void;
|
|
45
40
|
loadOlder(): Promise<void>;
|
|
46
|
-
private
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
/** One frame: everything that happened to the account in one auction batch. */
|
|
41
|
+
private seed;
|
|
42
|
+
/** Oldest batch first, so transitions to held orders land in order; a batch already held is skipped. */
|
|
43
|
+
private store;
|
|
50
44
|
private onFrame;
|
|
51
|
-
/**
|
|
52
|
-
|
|
53
|
-
* kinds and the window server-side; the stream filters neither.
|
|
54
|
-
*/
|
|
55
|
-
private keep;
|
|
45
|
+
/** Rows newest batch first, wire order within a batch. */
|
|
46
|
+
private restack;
|
|
56
47
|
private rebuild;
|
|
57
48
|
}
|
|
58
49
|
//# sourceMappingURL=activity.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.d.ts","sourceRoot":"","sources":["../../src/sync/activity.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"activity.d.ts","sourceRoot":"","sources":["../../src/sync/activity.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,aAAa,EAAE,aAAa,EAAE,aAAa,EAAgB,OAAO,EACnE,MAAM,oBAAoB,CAAC;AAO5B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO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EAAE,CAAC,MAAM,EAAE,aAAa,EAAE,KAAK,IAAI,GAAG,MAAM,IAAI;IAOhE,SAAS,IAAI,IAAI;IAEX,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;YAgBlB,IAAI;IAqClB,wGAAwG;IACxG,OAAO,CAAC,KAAK;IAQb,OAAO,CAAC,OAAO;IAyBf,0DAA0D;IAC1D,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,OAAO;CAGhB"}
|
package/dist/sync/activity.js
CHANGED
|
@@ -1,79 +1,33 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//
|
|
5
|
-
// The cursor is the batch alone. `pod_activity` sends one frame per tick,
|
|
6
|
-
// covering every book, so a frame is already delivered exactly when its batch is
|
|
7
|
-
// at or below `since` — there is no `sinceBook` to resume inside a tick with.
|
|
8
|
-
import { applyActivityFrame, isMoney } from "../codec/activity.js";
|
|
9
|
-
import { msToUs, usToMs } from "../codec/units.js";
|
|
1
|
+
// `pod_activity` sends one batch per tick, so the batch alone is the resume cursor (ADR 0057).
|
|
2
|
+
import { foldActivityBatch } from "../codec/activity.js";
|
|
3
|
+
import { msToUs } from "../codec/units.js";
|
|
10
4
|
import { BaseResource } from "../stores/resource.js";
|
|
11
|
-
import {
|
|
12
|
-
/**
|
|
13
|
-
* No types is no filter, and an empty list says the same thing. One form, so REST,
|
|
14
|
-
* `keep()` and the client's memo key cannot read the same query three ways.
|
|
15
|
-
*/
|
|
16
|
-
export function normalizeActivityQuery(query = {}) {
|
|
17
|
-
return query.types?.length ? query : { ...query, types: undefined };
|
|
18
|
-
}
|
|
19
|
-
/** The node's page order within a tick: `(timestamp, ordinal, key)` descending. */
|
|
20
|
-
const ORDINAL = { order: 1, backstop: 2, bridge_transfer: 3, transfer: 4 };
|
|
21
|
-
/**
|
|
22
|
-
* A money row's identity, read off the row itself — so the same row keys the same
|
|
23
|
-
* whichever page it arrives on and however many of its kind share its tick. A
|
|
24
|
-
* backstop leg has no id of its own, but a sweep touches each market once.
|
|
25
|
-
*/
|
|
26
|
-
function moneyKey(entry) {
|
|
27
|
-
switch (entry.activityType) {
|
|
28
|
-
case "transfer": return `transfer:${entry.transferId}`;
|
|
29
|
-
case "bridge_transfer": return `bridge:${entry.txHash}:${entry.idx}`;
|
|
30
|
-
case "backstop": return `backstop:${entry.timeMs}:${entry.orderbookId ?? "cash"}`;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const cmp = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
34
|
-
/**
|
|
35
|
-
* Separate two rows the tick and the ordinal cannot. Insertion order would do it
|
|
36
|
-
* for one source alone, but a tick that arrives as a frame and a tick that arrives
|
|
37
|
-
* as a page are built in different orders, and a reload must not reshuffle the list.
|
|
38
|
-
*/
|
|
39
|
-
function tiebreak(a, b) {
|
|
40
|
-
if (a.activityType === "order" && b.activityType === "order") {
|
|
41
|
-
return b.order.nonce - a.order.nonce || cmp(a.order.id, b.order.id);
|
|
42
|
-
}
|
|
43
|
-
// Equal ordinals, so neither is an order.
|
|
44
|
-
return cmp(moneyKey(a), moneyKey(b));
|
|
45
|
-
}
|
|
5
|
+
import { ResumableStream } from "./stream.js";
|
|
6
|
+
/** One account's whole feed: the one subscription, batch map and paging state its views share. */
|
|
46
7
|
export class ActivityHistory {
|
|
47
8
|
ctx;
|
|
48
9
|
account;
|
|
49
10
|
base;
|
|
50
11
|
handle;
|
|
12
|
+
/** Every batch's rows, from either source; what dedupes one against the other. */
|
|
13
|
+
batches = new Map();
|
|
51
14
|
orders = new Map();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
* it: the node times an order by its signed deadline, which no frame carries, so
|
|
55
|
-
* neither a later frame nor a re-seed must be able to retime a row already placed.
|
|
56
|
-
*/
|
|
57
|
-
orderTimeMs = new Map();
|
|
58
|
-
/** The money rows, by {@link moneyKey}. */
|
|
59
|
-
money = new Map();
|
|
60
|
-
nextCursor = null;
|
|
61
|
-
painted = false;
|
|
15
|
+
rows = [];
|
|
16
|
+
nextUs = null;
|
|
62
17
|
seedGen = 0;
|
|
63
18
|
_hasMore = true;
|
|
64
19
|
_loading = false;
|
|
65
20
|
eventListeners = new Set();
|
|
66
21
|
stream;
|
|
67
|
-
|
|
68
|
-
constructor(ctx, account
|
|
22
|
+
views = new Map();
|
|
23
|
+
constructor(ctx, account) {
|
|
69
24
|
this.ctx = ctx;
|
|
70
25
|
this.account = account;
|
|
71
|
-
this.query = normalizeActivityQuery(query);
|
|
72
26
|
this.stream = new ResumableStream({
|
|
73
27
|
ws: ctx.ws,
|
|
74
28
|
channel: "pod_activity",
|
|
75
29
|
params: { account },
|
|
76
|
-
reseed: () => this.
|
|
30
|
+
reseed: () => this.seed(),
|
|
77
31
|
onFrame: (r) => this.onFrame(r),
|
|
78
32
|
});
|
|
79
33
|
this.base = new BaseResource((h) => {
|
|
@@ -92,65 +46,75 @@ export class ActivityHistory {
|
|
|
92
46
|
hasMore() { return this._hasMore; }
|
|
93
47
|
loading() { return this._loading; }
|
|
94
48
|
destroy() { this.base.destroy(); }
|
|
49
|
+
/** The rows of `types` whose `timeMs` is within inclusive `from`/`to`. */
|
|
50
|
+
view(query) {
|
|
51
|
+
// An empty `types` is no filter; sorted, since key order is an argument's accident.
|
|
52
|
+
const q = query.types?.length ? query : { ...query, types: undefined };
|
|
53
|
+
const key = JSON.stringify(q, Object.keys(q).sort());
|
|
54
|
+
let view = this.views.get(key);
|
|
55
|
+
if (!view)
|
|
56
|
+
this.views.set(key, view = new FilteredActivity(this, q));
|
|
57
|
+
return view;
|
|
58
|
+
}
|
|
95
59
|
/**
|
|
96
60
|
* Observe what the stream reported, a tick at a time: the order transitions and
|
|
97
|
-
* the money that moved. Nothing is emitted for
|
|
61
|
+
* the money that moved. Nothing is emitted for REST pages.
|
|
98
62
|
*/
|
|
99
63
|
onEvent(listener) {
|
|
100
64
|
this.eventListeners.add(listener);
|
|
101
|
-
// Listening starts the stream, like subscribing does
|
|
102
|
-
// ref-counted from `subscribe`/`ready` alone.
|
|
65
|
+
// Listening starts the stream, like subscribing does.
|
|
103
66
|
const release = this.base.subscribe(() => { });
|
|
104
67
|
return () => { this.eventListeners.delete(listener); release(); };
|
|
105
68
|
}
|
|
106
69
|
setWindow() { }
|
|
107
70
|
async loadOlder() {
|
|
108
|
-
if (!this.handle ||
|
|
71
|
+
if (!this.handle || this.nextUs === null || this._loading)
|
|
109
72
|
return;
|
|
110
73
|
this._loading = true;
|
|
111
74
|
this.rebuild();
|
|
112
75
|
try {
|
|
113
|
-
const page = await this.ctx.rest.activity(this.account, {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
this.nextCursor = page.nextCursor;
|
|
119
|
-
this._hasMore = page.nextCursor !== null;
|
|
76
|
+
const page = await this.ctx.rest.activity(this.account, { toUs: this.nextUs });
|
|
77
|
+
this.store([page]);
|
|
78
|
+
this.nextUs = page.nextUs;
|
|
79
|
+
this._hasMore = page.nextUs !== null;
|
|
80
|
+
this.restack();
|
|
120
81
|
}
|
|
121
82
|
finally {
|
|
122
83
|
this._loading = false;
|
|
123
84
|
this.rebuild();
|
|
124
85
|
}
|
|
125
86
|
}
|
|
126
|
-
|
|
127
|
-
restQuery() {
|
|
128
|
-
return { ...this.query, limit: this.query.limit ?? 100 };
|
|
129
|
-
}
|
|
130
|
-
async fetchFirstPage() {
|
|
87
|
+
async seed() {
|
|
131
88
|
const gen = ++this.seedGen;
|
|
132
|
-
const
|
|
89
|
+
const since = this.stream.cursor.since;
|
|
90
|
+
const stale = () => !this.stream.running || gen !== this.seedGen;
|
|
133
91
|
try {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
// Paging is the consumer's position in history, not the seed's: a reconnect
|
|
145
|
-
// re-paints the first page, and taking its cursor again would hand back pages
|
|
146
|
-
// `loadOlder` has already walked past.
|
|
147
|
-
if (!this.painted) {
|
|
148
|
-
this.painted = true;
|
|
149
|
-
this.nextCursor = page.nextCursor;
|
|
150
|
-
this._hasMore = page.nextCursor !== null;
|
|
92
|
+
if (since === undefined) {
|
|
93
|
+
const page = await this.ctx.rest.activity(this.account);
|
|
94
|
+
if (stale())
|
|
95
|
+
return false;
|
|
96
|
+
this.store([page]);
|
|
97
|
+
this.nextUs = page.nextUs;
|
|
98
|
+
this._hasMore = page.nextUs !== null;
|
|
99
|
+
this.restack();
|
|
100
|
+
this.stream.raise({ since: msToUs(page.solutionNow) });
|
|
101
|
+
return true;
|
|
151
102
|
}
|
|
152
|
-
|
|
153
|
-
|
|
103
|
+
// A reconnect: fill the gap above the cursor, and keep the consumer's paging position.
|
|
104
|
+
const fromUs = since + 1;
|
|
105
|
+
let toUs;
|
|
106
|
+
const pages = [];
|
|
107
|
+
do {
|
|
108
|
+
const page = await this.ctx.rest.activity(this.account, { fromUs, toUs });
|
|
109
|
+
if (stale())
|
|
110
|
+
return false;
|
|
111
|
+
pages.push(page);
|
|
112
|
+
toUs = page.nextUs ?? undefined;
|
|
113
|
+
} while (toUs !== undefined);
|
|
114
|
+
this.store(pages);
|
|
115
|
+
this.restack();
|
|
116
|
+
for (const page of pages)
|
|
117
|
+
this.stream.raise({ since: msToUs(page.solutionNow) });
|
|
154
118
|
return true;
|
|
155
119
|
}
|
|
156
120
|
catch (e) {
|
|
@@ -161,52 +125,37 @@ export class ActivityHistory {
|
|
|
161
125
|
throw e;
|
|
162
126
|
}
|
|
163
127
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// Only the source that first reported the order gets to time it. A re-seed
|
|
171
|
-
// carries the node's row time for an order the stream already placed by its
|
|
172
|
-
// batch, and taking it would move the row on every reconnect.
|
|
173
|
-
if (!this.orderTimeMs.has(entry.order.id))
|
|
174
|
-
this.orderTimeMs.set(entry.order.id, entry.timeMs);
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
const key = moneyKey(entry);
|
|
178
|
-
if (!this.money.has(key))
|
|
179
|
-
this.money.set(key, entry);
|
|
128
|
+
/** Oldest batch first, so transitions to held orders land in order; a batch already held is skipped. */
|
|
129
|
+
store(pages) {
|
|
130
|
+
const fresh = pages.flatMap((p) => p.batches).filter(([batchUs]) => !this.batches.has(batchUs));
|
|
131
|
+
const pageOrders = new Set(fresh.flatMap(([, entries]) => entries.flatMap((e) => (e.k === "order" ? [e.order_id] : []))));
|
|
132
|
+
for (const [batchUs, entries] of fresh.sort((a, b) => a[0] - b[0])) {
|
|
133
|
+
this.batches.set(batchUs, foldActivityBatch(batchUs, entries, this.orders, { pageOrders }).rows);
|
|
180
134
|
}
|
|
181
135
|
}
|
|
182
|
-
/** One frame: everything that happened to the account in one auction batch. */
|
|
183
136
|
onFrame(result) {
|
|
184
|
-
|
|
185
|
-
if (!frame || !Array.isArray(frame.orders) || !Array.isArray(frame.events))
|
|
137
|
+
if (!result || typeof result !== "object")
|
|
186
138
|
return;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const at = { since: frame.batch };
|
|
190
|
-
if (this.stream.delivered(at))
|
|
139
|
+
const keys = Object.keys(result);
|
|
140
|
+
if (keys.length !== 1)
|
|
191
141
|
return;
|
|
192
|
-
const
|
|
193
|
-
|
|
142
|
+
const key = keys[0];
|
|
143
|
+
const entries = result[key];
|
|
144
|
+
const batchUs = Number(key);
|
|
145
|
+
if (!Array.isArray(entries) || !Number.isSafeInteger(batchUs))
|
|
146
|
+
return;
|
|
147
|
+
// Folding is not idempotent (events would be reported twice) and a resume replays from the cursor.
|
|
148
|
+
const at = { since: batchUs };
|
|
149
|
+
if (this.stream.delivered(at))
|
|
194
150
|
return;
|
|
195
|
-
const entries = [];
|
|
196
|
-
const events = applyActivityFrame(kept, { orders: this.orders, entries }, {
|
|
197
|
-
account: this.account,
|
|
198
|
-
});
|
|
199
|
-
// An order the stream is the first to report is timed by its batch; one the
|
|
200
|
-
// seed already placed keeps the time the node gave it.
|
|
201
|
-
const batchMs = usToMs(kept.batch);
|
|
202
|
-
for (const o of kept.orders)
|
|
203
|
-
if (!this.orderTimeMs.has(o.id))
|
|
204
|
-
this.orderTimeMs.set(o.id, batchMs);
|
|
205
|
-
this.absorb(entries, false);
|
|
206
151
|
this.stream.advance(at);
|
|
152
|
+
if (!entries.length || this.batches.has(batchUs))
|
|
153
|
+
return;
|
|
154
|
+
const { rows, events } = foldActivityBatch(batchUs, entries, this.orders, "stream");
|
|
155
|
+
this.batches.set(batchUs, rows);
|
|
156
|
+
this.rows = [...rows, ...this.rows];
|
|
157
|
+
// Listeners run after rebuild() so one that reads the resource sees what the event produced.
|
|
207
158
|
this.rebuild();
|
|
208
|
-
// Strictly after `rebuild()`: a listener that reads the resource in response to an
|
|
209
|
-
// event must see the state that event produced.
|
|
210
159
|
if (events.length) {
|
|
211
160
|
for (const listener of this.eventListeners) {
|
|
212
161
|
try {
|
|
@@ -216,44 +165,69 @@ export class ActivityHistory {
|
|
|
216
165
|
}
|
|
217
166
|
}
|
|
218
167
|
}
|
|
219
|
-
/**
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
keep(frame) {
|
|
224
|
-
const { types, from, to } = this.query;
|
|
225
|
-
if (from !== undefined && frame.batch < msToUs(from))
|
|
226
|
-
return undefined;
|
|
227
|
-
if (to !== undefined && frame.batch >= msToUs(to))
|
|
228
|
-
return undefined;
|
|
229
|
-
if (!types)
|
|
230
|
-
return frame;
|
|
231
|
-
const want = new Set(types);
|
|
232
|
-
return {
|
|
233
|
-
...frame,
|
|
234
|
-
// The order events index `orders`, so the two are kept or dropped together.
|
|
235
|
-
orders: want.has("order") ? frame.orders : [],
|
|
236
|
-
events: frame.events.filter((e) => want.has(isMoney(e.k) ? e.k : "order")),
|
|
237
|
-
};
|
|
168
|
+
/** Rows newest batch first, wire order within a batch. */
|
|
169
|
+
restack() {
|
|
170
|
+
this.rows = [...this.batches.keys()].sort((a, b) => b - a).flatMap((b) => this.batches.get(b));
|
|
171
|
+
this.rebuild();
|
|
238
172
|
}
|
|
239
173
|
rebuild() {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
174
|
+
this.handle?.set(this.rows.slice());
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const EVENT_TYPE = {
|
|
178
|
+
new: "order", reject: "order", modify_reject: "modify_reject", cancel_reject: "cancel_reject",
|
|
179
|
+
fill: "fill", modify: "modify", cancel: "termination", expire: "termination",
|
|
180
|
+
};
|
|
181
|
+
class FilteredActivity {
|
|
182
|
+
parent;
|
|
183
|
+
query;
|
|
184
|
+
want;
|
|
185
|
+
held;
|
|
186
|
+
shown;
|
|
187
|
+
constructor(parent, query) {
|
|
188
|
+
this.parent = parent;
|
|
189
|
+
this.query = query;
|
|
190
|
+
this.want = query.types && new Set(query.types);
|
|
191
|
+
}
|
|
192
|
+
get() {
|
|
193
|
+
const rows = this.parent.get();
|
|
194
|
+
if (rows !== this.held) {
|
|
195
|
+
this.held = rows;
|
|
196
|
+
this.shown = rows?.filter((r) => this.keeps(r));
|
|
250
197
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
198
|
+
return this.shown;
|
|
199
|
+
}
|
|
200
|
+
subscribe(listener) { return this.parent.subscribe(listener); }
|
|
201
|
+
async ready() { return (await this.parent.ready()).filter((r) => this.keeps(r)); }
|
|
202
|
+
get error() { return this.parent.error; }
|
|
203
|
+
loading() { return this.parent.loading(); }
|
|
204
|
+
setWindow() { }
|
|
205
|
+
hasMore() {
|
|
206
|
+
const oldest = this.parent.get()?.at(-1)?.timeMs;
|
|
207
|
+
const floor = this.query.from;
|
|
208
|
+
return this.parent.hasMore() && (floor === undefined || oldest === undefined || oldest > floor);
|
|
209
|
+
}
|
|
210
|
+
async loadOlder() {
|
|
211
|
+
if (this.hasMore())
|
|
212
|
+
await this.parent.loadOlder();
|
|
213
|
+
}
|
|
214
|
+
onEvent(listener) {
|
|
215
|
+
const want = this.want;
|
|
216
|
+
if (!want)
|
|
217
|
+
return this.parent.onEvent(listener);
|
|
218
|
+
return this.parent.onEvent((events) => {
|
|
219
|
+
const kept = events.filter((e) => want.has("activityType" in e ? e.activityType : EVENT_TYPE[e.kind]));
|
|
220
|
+
if (kept.length)
|
|
221
|
+
listener(kept);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
keeps(r) {
|
|
225
|
+
const { from, to } = this.query;
|
|
226
|
+
if (from !== undefined && r.timeMs < from)
|
|
227
|
+
return false;
|
|
228
|
+
if (to !== undefined && r.timeMs > to)
|
|
229
|
+
return false;
|
|
230
|
+
return !this.want || this.want.has(r.activityType);
|
|
257
231
|
}
|
|
258
232
|
}
|
|
259
233
|
//# sourceMappingURL=activity.js.map
|