@pod-network/trade-sdk 0.1.1-rc.0 → 0.3.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/dist/client.d.ts.map +1 -1
- package/dist/client.js +1 -12
- package/dist/client.js.map +1 -1
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +7 -6
- package/dist/codec/decode.js.map +1 -1
- package/dist/codec/direction.d.ts +10 -0
- package/dist/codec/direction.d.ts.map +1 -0
- package/dist/codec/direction.js +46 -0
- package/dist/codec/direction.js.map +1 -0
- package/dist/codec/orders-v2.d.ts +19 -0
- package/dist/codec/orders-v2.d.ts.map +1 -0
- package/dist/codec/orders-v2.js +200 -0
- package/dist/codec/orders-v2.js.map +1 -0
- package/dist/codec/units.d.ts +10 -0
- package/dist/codec/units.d.ts.map +1 -1
- package/dist/codec/units.js +16 -0
- package/dist/codec/units.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/sync/orders.d.ts +71 -16
- package/dist/sync/orders.d.ts.map +1 -1
- package/dist/sync/orders.js +202 -83
- package/dist/sync/orders.js.map +1 -1
- package/dist/transport/ws.d.ts +63 -1
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js +82 -2
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +125 -9
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +104 -40
- package/dist/types/wire.d.ts.map +1 -1
- package/dist/write/index.d.ts +31 -1
- package/dist/write/index.d.ts.map +1 -1
- package/dist/write/index.js +34 -1
- package/dist/write/index.js.map +1 -1
- package/package.json +6 -3
- package/src/client.ts +1 -14
- package/src/codec/decode.ts +7 -6
- package/src/codec/direction.ts +36 -0
- package/src/codec/orders-v2.ts +222 -0
- package/src/codec/units.ts +16 -0
- package/src/index.ts +1 -1
- package/src/sync/orders.ts +203 -83
- package/src/transport/ws.ts +111 -3
- package/src/types/public.ts +139 -7
- package/src/types/wire.ts +114 -28
- package/src/write/index.ts +65 -1
package/dist/sync/orders.d.ts
CHANGED
|
@@ -1,29 +1,53 @@
|
|
|
1
|
-
import type { Address,
|
|
1
|
+
import type { Address, Order, OrderEvent, OrdersQuery } from "../types/public.js";
|
|
2
|
+
import { type SubParams } from "../transport/ws.js";
|
|
2
3
|
import type { SeriesResource } from "./candles.js";
|
|
3
4
|
import type { SyncContext } from "./sources.js";
|
|
5
|
+
/**
|
|
6
|
+
* Order two positions in the stream, the way the server does.
|
|
7
|
+
*
|
|
8
|
+
* A batch is delivered as one frame per book, so a position is the pair
|
|
9
|
+
* `(batch, book)` — and an absent book means the whole batch, which sorts *above*
|
|
10
|
+
* every book in it. This mirrors `already_delivered` in
|
|
11
|
+
* `node/src/rpc/orders_v2.rs`: `(frame_batch, frame_book) <= (since,
|
|
12
|
+
* since_book.unwrap_or(0xff…))`. Getting the absent case wrong turns "all of batch
|
|
13
|
+
* N" into "up to book B of batch N", which asks the server to re-send the rest.
|
|
14
|
+
*
|
|
15
|
+
* Book ids are fixed-width lowercase hex, so comparing them as strings is
|
|
16
|
+
* comparing their bytes.
|
|
17
|
+
*/
|
|
18
|
+
export declare function compareCursor(a: SubParams, b: SubParams): number;
|
|
4
19
|
export declare class OrderHistory implements SeriesResource<Order> {
|
|
5
20
|
private readonly ctx;
|
|
6
21
|
private readonly account;
|
|
7
22
|
private readonly query;
|
|
8
|
-
/** Maps a WS order's token pair to its orderbook id (see rebuild). */
|
|
9
|
-
private readonly resolveOrderbookId?;
|
|
10
23
|
private readonly base;
|
|
11
24
|
private handle;
|
|
12
25
|
private readonly byId;
|
|
13
26
|
private nextCursor;
|
|
14
27
|
private sub;
|
|
15
|
-
private watermarkUs;
|
|
16
28
|
private alive;
|
|
17
29
|
private _hasMore;
|
|
18
30
|
private _loading;
|
|
19
31
|
private subRetries;
|
|
32
|
+
private fastResumes;
|
|
20
33
|
private retryTimer?;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
34
|
+
private readonly eventListeners;
|
|
35
|
+
/**
|
|
36
|
+
* Where the stream is: the last frame accepted, as the `(batch, book)` pair the
|
|
37
|
+
* channel resumes from. Replaced whole rather than patched half at a time —
|
|
38
|
+
* `sinceBook` names a book *within* `since`, so the two are one fact and a
|
|
39
|
+
* mismatched pair asks the server to skip books we never saw.
|
|
40
|
+
*/
|
|
41
|
+
private cursor;
|
|
42
|
+
/**
|
|
43
|
+
* Push the cursor to the transport, both halves.
|
|
44
|
+
*
|
|
45
|
+
* `update` merges, so sending `{ since }` alone would leave whatever `sinceBook` was
|
|
46
|
+
* there before — a book from an older batch beside a newer `since`, which asks the
|
|
47
|
+
* server to skip less than it should and re-send the difference.
|
|
48
|
+
*/
|
|
49
|
+
private pushCursor;
|
|
50
|
+
constructor(ctx: SyncContext, account: Address, query?: OrdersQuery);
|
|
27
51
|
get(): Order[] | undefined;
|
|
28
52
|
subscribe(listener: () => void): () => void;
|
|
29
53
|
ready(): Promise<Order[]>;
|
|
@@ -31,19 +55,50 @@ export declare class OrderHistory implements SeriesResource<Order> {
|
|
|
31
55
|
hasMore(): boolean;
|
|
32
56
|
loading(): boolean;
|
|
33
57
|
destroy(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Observe the transitions the stream reports, as they arrive — a frame at a time, in
|
|
60
|
+
* the order the engine caused them.
|
|
61
|
+
*
|
|
62
|
+
* The snapshot (`get`/`subscribe`) is what to render; this is what *happened*, which
|
|
63
|
+
* a snapshot cannot express: two fills in one batch are one state change, and a
|
|
64
|
+
* refused amendment is none at all. Nothing is emitted for the REST seed, so a
|
|
65
|
+
* consumer never has to filter out a backlog of history as if it were live.
|
|
66
|
+
*/
|
|
67
|
+
onEvent(listener: (events: OrderEvent[]) => void): () => void;
|
|
34
68
|
setWindow(): void;
|
|
35
69
|
loadOlder(): Promise<void>;
|
|
36
70
|
private fetchFirstPage;
|
|
37
71
|
private seed;
|
|
38
72
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
73
|
+
* The subscription is not running: `eth_subscribe` was rejected, or the server
|
|
74
|
+
* closed it.
|
|
75
|
+
*
|
|
76
|
+
* A resumable close needs neither a re-seed nor a delay: the server reports where
|
|
77
|
+
* it stopped, so resubscribing from that delivers exactly the frames we never got.
|
|
78
|
+
* That fast path is only taken while the socket is actually open — `resubscribe()`
|
|
79
|
+
* is a no-op otherwise, which would spend the budget without an attempt and leave
|
|
80
|
+
* nothing scheduled, and `-32021` (node shutting down) arrives exactly as the
|
|
81
|
+
* socket goes away.
|
|
82
|
+
*
|
|
83
|
+
* Everything else (a rejection, a server bug, a close with the socket already
|
|
84
|
+
* gone, or closes that keep coming) takes the slow path: backed off and capped, so
|
|
85
|
+
* a server that keeps refusing cannot spin this into a tight re-seed loop, and
|
|
86
|
+
* eventually the cursor is dropped (the likely culprit) to just stream live. Both
|
|
87
|
+
* counters reset once live data flows (`onFrame`).
|
|
43
88
|
*/
|
|
44
89
|
private onSubError;
|
|
45
|
-
|
|
46
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Back off, re-seed over REST, then resubscribe — and keep trying.
|
|
92
|
+
*
|
|
93
|
+
* The re-seed can fail too (the same node is usually behind both the stream and the
|
|
94
|
+
* indexer), and a failure has to re-arm here: not resubscribing means no further
|
|
95
|
+
* close or rejection arrives, so nothing else would ever schedule another attempt and
|
|
96
|
+
* the stream would stay down with no error surfaced. Capped, so a node that keeps
|
|
97
|
+
* refusing cannot spin this.
|
|
98
|
+
*/
|
|
99
|
+
private scheduleReseed;
|
|
100
|
+
/** One frame: everything that happened to one book in one auction batch. */
|
|
101
|
+
private onFrame;
|
|
47
102
|
private rebuild;
|
|
48
103
|
}
|
|
49
104
|
//# sourceMappingURL=orders.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders.d.ts","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"orders.d.ts","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,OAAO,EAAY,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAI5F,OAAO,EAA8B,KAAK,SAAS,EAAqB,MAAM,oBAAoB,CAAC;AACnG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAmBhD;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAOhE;AAED,qBAAa,YAAa,YAAW,cAAc,CAAC,KAAK,CAAC;IAiCtD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK;IAlCxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwB;IAC7C,OAAO,CAAC,MAAM,CAAsC;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4B;IACjD,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,GAAG,CAA2B;IACtC,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,UAAU,CAAC,CAAgC;IACnD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6C;IAC5E;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAiD;IAE/D;;;;;;OAMG;IACH,OAAO,CAAC,UAAU;gBAKC,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,OAAO,EAChB,KAAK,GAAE,WAAgB;IAkB1C,GAAG,IAAI,KAAK,EAAE,GAAG,SAAS;IAC1B,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAC3C,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,KAAK,IAAI,KAAK,GAAG,SAAS,CAA4B;IAC1D,OAAO,IAAI,OAAO;IAClB,OAAO,IAAI,OAAO;IAClB,OAAO,IAAI,IAAI;IAEf;;;;;;;;OAQG;IACH,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,IAAI,GAAG,MAAM,IAAI;IAW7D,SAAS,IAAI,IAAI;IAEX,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;YAoBlB,cAAc;IA8B5B,OAAO,CAAC,IAAI;IAgBZ;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,UAAU;IA8BlB;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;IAgBtB,4EAA4E;IAC5E,OAAO,CAAC,OAAO;IA6Bf,OAAO,CAAC,OAAO;CAehB"}
|
package/dist/sync/orders.js
CHANGED
|
@@ -1,36 +1,89 @@
|
|
|
1
1
|
// OrderHistory: a SeriesResource<Order> seeded from the warm first page, kept
|
|
2
|
-
// live by the
|
|
3
|
-
//
|
|
2
|
+
// live by the pod_orders_v2 stream (bidder-filtered, resumed from a (batch, book)
|
|
3
|
+
// cursor), and paged backwards by cursor for deep history.
|
|
4
4
|
//
|
|
5
5
|
// Reconnect: on every (re)connect we re-seed the first page (authoritative open
|
|
6
|
-
// orders) and refresh
|
|
7
|
-
// if
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
// orders) and refresh the cursor from its watermark; the WS auto-resubscribes,
|
|
7
|
+
// and if the cursor is too old (down too long) `onError` re-seeds and
|
|
8
|
+
// resubscribes. A server-initiated close is not the same as a rejection: it
|
|
9
|
+
// reports where delivery stopped, so resuming from one is a bare `resubscribe()`
|
|
10
|
+
// with no re-seed.
|
|
11
|
+
import { applyOrdersFrame } from "../codec/orders-v2.js";
|
|
10
12
|
import { BaseResource } from "../stores/resource.js";
|
|
13
|
+
import { PodSubscriptionClosedError } from "../transport/ws.js";
|
|
14
|
+
/**
|
|
15
|
+
* Consecutive server closes we resume from before falling back to the re-seed
|
|
16
|
+
* path. A lagged close is recoverable by resubscribing, so the fast path is the
|
|
17
|
+
* right default — but a stream that keeps closing is one we are not keeping up
|
|
18
|
+
* with, and re-seeding beats replaying a growing backlog on every attempt.
|
|
19
|
+
*/
|
|
20
|
+
const FAST_RESUMES_BEFORE_RESEED = 3;
|
|
21
|
+
/**
|
|
22
|
+
* Re-seed attempts that keep the cursor before we conclude the cursor is what the
|
|
23
|
+
* server is rejecting, drop it, and settle for streaming live.
|
|
24
|
+
*/
|
|
25
|
+
const RETRIES_BEFORE_DROPPING_CURSOR = 2;
|
|
26
|
+
/** The largest book id, which is what an absent `sinceBook` means. */
|
|
27
|
+
const WHOLE_BATCH = `0x${"ff".repeat(32)}`;
|
|
28
|
+
/**
|
|
29
|
+
* Order two positions in the stream, the way the server does.
|
|
30
|
+
*
|
|
31
|
+
* A batch is delivered as one frame per book, so a position is the pair
|
|
32
|
+
* `(batch, book)` — and an absent book means the whole batch, which sorts *above*
|
|
33
|
+
* every book in it. This mirrors `already_delivered` in
|
|
34
|
+
* `node/src/rpc/orders_v2.rs`: `(frame_batch, frame_book) <= (since,
|
|
35
|
+
* since_book.unwrap_or(0xff…))`. Getting the absent case wrong turns "all of batch
|
|
36
|
+
* N" into "up to book B of batch N", which asks the server to re-send the rest.
|
|
37
|
+
*
|
|
38
|
+
* Book ids are fixed-width lowercase hex, so comparing them as strings is
|
|
39
|
+
* comparing their bytes.
|
|
40
|
+
*/
|
|
41
|
+
export function compareCursor(a, b) {
|
|
42
|
+
const aBatch = a.since ?? 0;
|
|
43
|
+
const bBatch = b.since ?? 0;
|
|
44
|
+
if (aBatch !== bBatch)
|
|
45
|
+
return aBatch < bBatch ? -1 : 1;
|
|
46
|
+
const aBook = a.sinceBook ?? WHOLE_BATCH;
|
|
47
|
+
const bBook = b.sinceBook ?? WHOLE_BATCH;
|
|
48
|
+
return aBook === bBook ? 0 : aBook < bBook ? -1 : 1;
|
|
49
|
+
}
|
|
11
50
|
export class OrderHistory {
|
|
12
51
|
ctx;
|
|
13
52
|
account;
|
|
14
53
|
query;
|
|
15
|
-
resolveOrderbookId;
|
|
16
54
|
base;
|
|
17
55
|
handle;
|
|
18
56
|
byId = new Map();
|
|
19
57
|
nextCursor = null;
|
|
20
58
|
sub;
|
|
21
|
-
watermarkUs = 0;
|
|
22
59
|
alive = false;
|
|
23
60
|
_hasMore = true;
|
|
24
61
|
_loading = false;
|
|
25
62
|
subRetries = 0;
|
|
63
|
+
fastResumes = 0;
|
|
26
64
|
retryTimer;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
|
|
65
|
+
eventListeners = new Set();
|
|
66
|
+
/**
|
|
67
|
+
* Where the stream is: the last frame accepted, as the `(batch, book)` pair the
|
|
68
|
+
* channel resumes from. Replaced whole rather than patched half at a time —
|
|
69
|
+
* `sinceBook` names a book *within* `since`, so the two are one fact and a
|
|
70
|
+
* mismatched pair asks the server to skip books we never saw.
|
|
71
|
+
*/
|
|
72
|
+
cursor = { since: 0, sinceBook: undefined };
|
|
73
|
+
/**
|
|
74
|
+
* Push the cursor to the transport, both halves.
|
|
75
|
+
*
|
|
76
|
+
* `update` merges, so sending `{ since }` alone would leave whatever `sinceBook` was
|
|
77
|
+
* there before — a book from an older batch beside a newer `since`, which asks the
|
|
78
|
+
* server to skip less than it should and re-send the difference.
|
|
79
|
+
*/
|
|
80
|
+
pushCursor() {
|
|
81
|
+
this.sub?.update({ since: this.cursor.since, sinceBook: this.cursor.sinceBook });
|
|
82
|
+
}
|
|
83
|
+
constructor(ctx, account, query = {}) {
|
|
30
84
|
this.ctx = ctx;
|
|
31
85
|
this.account = account;
|
|
32
86
|
this.query = query;
|
|
33
|
-
this.resolveOrderbookId = resolveOrderbookId;
|
|
34
87
|
this.base = new BaseResource((h) => {
|
|
35
88
|
this.handle = h;
|
|
36
89
|
this.alive = true;
|
|
@@ -55,6 +108,25 @@ export class OrderHistory {
|
|
|
55
108
|
hasMore() { return this._hasMore; }
|
|
56
109
|
loading() { return this._loading; }
|
|
57
110
|
destroy() { this.base.destroy(); }
|
|
111
|
+
/**
|
|
112
|
+
* Observe the transitions the stream reports, as they arrive — a frame at a time, in
|
|
113
|
+
* the order the engine caused them.
|
|
114
|
+
*
|
|
115
|
+
* The snapshot (`get`/`subscribe`) is what to render; this is what *happened*, which
|
|
116
|
+
* a snapshot cannot express: two fills in one batch are one state change, and a
|
|
117
|
+
* refused amendment is none at all. Nothing is emitted for the REST seed, so a
|
|
118
|
+
* consumer never has to filter out a backlog of history as if it were live.
|
|
119
|
+
*/
|
|
120
|
+
onEvent(listener) {
|
|
121
|
+
this.eventListeners.add(listener);
|
|
122
|
+
// Listening starts the stream, like subscribing does. The resource is ref-counted
|
|
123
|
+
// from `subscribe`/`ready` alone, so without holding one an event-only consumer
|
|
124
|
+
// would wait forever — and would fall silent the moment the last snapshot
|
|
125
|
+
// subscriber left, since teardown drops the websocket subscription and leaves the
|
|
126
|
+
// listeners registered.
|
|
127
|
+
const release = this.base.subscribe(() => { });
|
|
128
|
+
return () => { this.eventListeners.delete(listener); release(); };
|
|
129
|
+
}
|
|
58
130
|
setWindow() { }
|
|
59
131
|
async loadOlder() {
|
|
60
132
|
if (!this.handle || !this.nextCursor || this._loading)
|
|
@@ -79,15 +151,30 @@ export class OrderHistory {
|
|
|
79
151
|
}
|
|
80
152
|
// --- internals ---
|
|
81
153
|
async fetchFirstPage() {
|
|
154
|
+
const before = this.cursor;
|
|
82
155
|
try {
|
|
83
156
|
const page = await this.ctx.rest.orders(this.account, { limit: this.query.limit ?? 100 });
|
|
84
157
|
if (!this.alive)
|
|
85
158
|
return false;
|
|
86
|
-
|
|
159
|
+
// The transport resubscribes synchronously right after the `open` event that
|
|
160
|
+
// starts this fetch, so replayed frames can land while it is still in flight —
|
|
161
|
+
// and the indexer trails the stream by design. Overwriting a row the stream has
|
|
162
|
+
// already advanced would revert it, permanently: the cursor below refuses to
|
|
163
|
+
// rewind and `onFrame` drops a re-delivery, so nothing would repair it.
|
|
164
|
+
const streamMovedOn = compareCursor(this.cursor, before) > 0;
|
|
165
|
+
for (const o of page.orders) {
|
|
166
|
+
if (streamMovedOn && this.byId.has(o.id))
|
|
167
|
+
continue;
|
|
87
168
|
this.byId.set(o.id, o);
|
|
169
|
+
}
|
|
88
170
|
this.nextCursor = page.nextCursor;
|
|
89
171
|
this._hasMore = page.nextCursor !== null;
|
|
90
|
-
|
|
172
|
+
// Only ever forward, and a page settles whole batches, so its watermark
|
|
173
|
+
// carries no book — which makes it *ahead* of a stream position in the same
|
|
174
|
+
// batch, not equal to it.
|
|
175
|
+
const settled = { since: page.solutionNow * 1000, sinceBook: undefined };
|
|
176
|
+
if (compareCursor(settled, this.cursor) > 0)
|
|
177
|
+
this.cursor = settled;
|
|
91
178
|
this.rebuild();
|
|
92
179
|
return true;
|
|
93
180
|
}
|
|
@@ -102,108 +189,140 @@ export class OrderHistory {
|
|
|
102
189
|
if (!ok || !this.alive)
|
|
103
190
|
return;
|
|
104
191
|
if (!this.sub) {
|
|
105
|
-
this.sub = this.ctx.ws.subscribe("
|
|
192
|
+
this.sub = this.ctx.ws.subscribe("pod_orders_v2", { account: this.account, ...this.cursor }, (r) => this.onFrame(r), (e) => this.onSubError(e));
|
|
106
193
|
}
|
|
107
194
|
else {
|
|
108
|
-
this.
|
|
195
|
+
this.pushCursor(); // refresh for the next reconnect
|
|
109
196
|
}
|
|
110
197
|
});
|
|
111
198
|
}
|
|
112
199
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
200
|
+
* The subscription is not running: `eth_subscribe` was rejected, or the server
|
|
201
|
+
* closed it.
|
|
202
|
+
*
|
|
203
|
+
* A resumable close needs neither a re-seed nor a delay: the server reports where
|
|
204
|
+
* it stopped, so resubscribing from that delivers exactly the frames we never got.
|
|
205
|
+
* That fast path is only taken while the socket is actually open — `resubscribe()`
|
|
206
|
+
* is a no-op otherwise, which would spend the budget without an attempt and leave
|
|
207
|
+
* nothing scheduled, and `-32021` (node shutting down) arrives exactly as the
|
|
208
|
+
* socket goes away.
|
|
209
|
+
*
|
|
210
|
+
* Everything else (a rejection, a server bug, a close with the socket already
|
|
211
|
+
* gone, or closes that keep coming) takes the slow path: backed off and capped, so
|
|
212
|
+
* a server that keeps refusing cannot spin this into a tight re-seed loop, and
|
|
213
|
+
* eventually the cursor is dropped (the likely culprit) to just stream live. Both
|
|
214
|
+
* counters reset once live data flows (`onFrame`).
|
|
117
215
|
*/
|
|
118
|
-
onSubError() {
|
|
216
|
+
onSubError(err) {
|
|
119
217
|
if (!this.alive)
|
|
120
218
|
return;
|
|
219
|
+
const canFastResume = err instanceof PodSubscriptionClosedError && err.resumable
|
|
220
|
+
&& this.fastResumes < FAST_RESUMES_BEFORE_RESEED && this.ctx.ws.state === "open";
|
|
221
|
+
if (canFastResume) {
|
|
222
|
+
this.fastResumes++;
|
|
223
|
+
// Adopt the server's watermark only when it is ahead of ours: it knows which
|
|
224
|
+
// frames it handed over, but a re-seed may already have carried us past it,
|
|
225
|
+
// and rewinding would re-deliver frames we have applied.
|
|
226
|
+
const reported = { since: err.resumeSince, sinceBook: err.resumeSinceBook };
|
|
227
|
+
if (err.resumeSince !== undefined && compareCursor(reported, this.cursor) > 0)
|
|
228
|
+
this.cursor = reported;
|
|
229
|
+
// The transport rewrote `sub.params` from the close before this ran, so without
|
|
230
|
+
// pushing our own decision back the wire resumes from the server's point
|
|
231
|
+
// regardless and the guard above protects nothing.
|
|
232
|
+
this.pushCursor();
|
|
233
|
+
this.sub?.resubscribe();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
// The slow path, and the one that answers "what if we fell behind the server's
|
|
237
|
+
// replay buffer": `eth_subscribe` rejects a `since` older than what the buffer
|
|
238
|
+
// retains, and the prescribed recovery is to backfill over REST and resubscribe.
|
|
239
|
+
// That is what this is. `fetchFirstPage` also *replaces the cursor* with the
|
|
240
|
+
// page's watermark, so the position that was too old is gone by the first retry
|
|
241
|
+
// and the resubscribe is accepted with no gap — the server replays from the page
|
|
242
|
+
// forward. Dropping the cursor below is the backstop for when even that fresh
|
|
243
|
+
// watermark is refused (the indexer further behind than the buffer retains):
|
|
244
|
+
// live-only resubscribe, trading the unreplayable window for a working stream.
|
|
245
|
+
this.scheduleReseed();
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Back off, re-seed over REST, then resubscribe — and keep trying.
|
|
249
|
+
*
|
|
250
|
+
* The re-seed can fail too (the same node is usually behind both the stream and the
|
|
251
|
+
* indexer), and a failure has to re-arm here: not resubscribing means no further
|
|
252
|
+
* close or rejection arrives, so nothing else would ever schedule another attempt and
|
|
253
|
+
* the stream would stay down with no error surfaced. Capped, so a node that keeps
|
|
254
|
+
* refusing cannot spin this.
|
|
255
|
+
*/
|
|
256
|
+
scheduleReseed() {
|
|
121
257
|
this.subRetries++;
|
|
122
258
|
const delay = Math.min(30_000, 500 * 2 ** (this.subRetries - 1));
|
|
123
259
|
if (this.retryTimer)
|
|
124
260
|
clearTimeout(this.retryTimer);
|
|
125
261
|
this.retryTimer = setTimeout(() => {
|
|
126
262
|
void this.fetchFirstPage().then((ok) => {
|
|
127
|
-
if (!
|
|
263
|
+
if (!this.alive)
|
|
264
|
+
return;
|
|
265
|
+
if (!ok) {
|
|
266
|
+
this.scheduleReseed();
|
|
128
267
|
return;
|
|
129
|
-
|
|
268
|
+
}
|
|
269
|
+
const tooOld = this.subRetries > RETRIES_BEFORE_DROPPING_CURSOR;
|
|
270
|
+
if (tooOld)
|
|
271
|
+
this.sub?.update({ since: undefined, sinceBook: undefined });
|
|
272
|
+
else
|
|
273
|
+
this.pushCursor();
|
|
130
274
|
this.sub?.resubscribe();
|
|
131
275
|
});
|
|
132
276
|
}, delay);
|
|
133
277
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
278
|
+
/** One frame: everything that happened to one book in one auction batch. */
|
|
279
|
+
onFrame(result) {
|
|
280
|
+
// Live data flowing → the subscription is healthy on both paths.
|
|
281
|
+
this.subRetries = 0;
|
|
282
|
+
this.fastResumes = 0;
|
|
283
|
+
const frame = result;
|
|
284
|
+
if (!frame || !Array.isArray(frame.orders) || !Array.isArray(frame.events))
|
|
285
|
+
return;
|
|
286
|
+
// Drop what we already hold. Applying a frame is not idempotent — a fill event
|
|
287
|
+
// appends to `order.fills`, and re-creating an entity resets its totals — and
|
|
288
|
+
// re-delivery is designed in: a resumed subscription replays from a cursor, and
|
|
289
|
+
// the replay boundary is a whole batch. Same predicate as the server's
|
|
290
|
+
// `already_delivered`, so client and server agree on what "already sent" means.
|
|
291
|
+
const at = { since: frame.batch, sinceBook: frame.book };
|
|
292
|
+
if (compareCursor(at, this.cursor) <= 0)
|
|
293
|
+
return;
|
|
294
|
+
const events = applyOrdersFrame(frame, this.byId, { account: this.account });
|
|
295
|
+
// A socket-level reconnect resubscribes from whatever is stored here.
|
|
296
|
+
this.cursor = at;
|
|
297
|
+
this.sub?.update(at);
|
|
139
298
|
this.rebuild();
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
this.byId.set(o.id, o);
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
case "fill": {
|
|
150
|
-
const e = this.byId.get(u.order_id);
|
|
151
|
-
if (e) {
|
|
152
|
-
e.filledBase = dec(u.filled_base_amount);
|
|
153
|
-
e.filledQuote = dec(u.filled_quote_amount);
|
|
154
|
-
e.fee = dec(u.fee);
|
|
155
|
-
e.status = u.status;
|
|
156
|
-
if (u.effective_price != null)
|
|
157
|
-
e.effectivePrice = dec(u.effective_price);
|
|
158
|
-
// Keep the per-fill breakdown live too. The push carries this fill's
|
|
159
|
-
// base/quote delta but no timestamp — receipt time is close enough.
|
|
160
|
-
const base = dec(u.base_amount);
|
|
161
|
-
const quote = dec(u.quote_amount);
|
|
162
|
-
e.fills = [...e.fills, { base, quote, price: base > 0n ? (quote * WAD) / base : 0n, time: Date.now() }];
|
|
163
|
-
}
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
case "modified": {
|
|
167
|
-
const e = this.byId.get(u.order_id);
|
|
168
|
-
if (e) {
|
|
169
|
-
e.price = dec(u.new_price);
|
|
170
|
-
const sign = e.initialSize < 0n ? -1n : 1n;
|
|
171
|
-
e.initialSize = sign * dec(u.new_size); // new_size is an unsigned magnitude
|
|
299
|
+
// Strictly after `rebuild()`: a listener that reads the resource in response to an
|
|
300
|
+
// event must see the state that event produced, not the state before it.
|
|
301
|
+
if (events.length) {
|
|
302
|
+
for (const listener of this.eventListeners) {
|
|
303
|
+
try {
|
|
304
|
+
listener(events);
|
|
172
305
|
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
case "expired": {
|
|
176
|
-
const e = this.byId.get(u.order_id);
|
|
177
|
-
if (e)
|
|
178
|
-
e.status = "expired";
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
case "canceled": {
|
|
182
|
-
const e = this.byId.get(u.order_id);
|
|
183
|
-
if (e)
|
|
184
|
-
e.status = "canceled";
|
|
185
|
-
break;
|
|
306
|
+
catch { /* a listener's failure is not the stream's */ }
|
|
186
307
|
}
|
|
187
308
|
}
|
|
188
309
|
}
|
|
189
310
|
rebuild() {
|
|
190
311
|
if (!this.handle)
|
|
191
312
|
return;
|
|
192
|
-
// WS stream orders carry a token `pair` instead of orderbook_id — resolve
|
|
193
|
-
// it here so consumers can always filter by market. Retried every rebuild,
|
|
194
|
-
// so orders that streamed in before the markets list loaded still resolve.
|
|
195
|
-
if (this.resolveOrderbookId) {
|
|
196
|
-
for (const o of this.byId.values()) {
|
|
197
|
-
if (o.orderbookId === undefined && o.pair)
|
|
198
|
-
o.orderbookId = this.resolveOrderbookId(o.pair);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
313
|
let arr = [...this.byId.values()];
|
|
202
314
|
if (this.query.status)
|
|
203
315
|
arr = arr.filter((o) => o.status === this.query.status);
|
|
204
316
|
if (this.query.orderbookId)
|
|
205
317
|
arr = arr.filter((o) => o.orderbookId === this.query.orderbookId);
|
|
206
|
-
|
|
318
|
+
// Newest first by when the order became real. Inclusion time is the key both
|
|
319
|
+
// sources report — REST sends it alongside the signed deadline, and a frame's
|
|
320
|
+
// batch *is* it — so a REST row and a streamed row sort against each other. An
|
|
321
|
+
// order with none has not been in a batch yet, so it is newer than every order
|
|
322
|
+
// that has; the signed deadline is deliberately not a fallback, since it is a
|
|
323
|
+
// future time and would sort on a different clock.
|
|
324
|
+
const at = (o) => o.includedMs ?? Number.MAX_SAFE_INTEGER;
|
|
325
|
+
arr.sort((a, b) => at(b) - at(a) || b.nonce - a.nonce);
|
|
207
326
|
this.handle.set(arr);
|
|
208
327
|
}
|
|
209
328
|
}
|
package/dist/sync/orders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,
|
|
1
|
+
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,kFAAkF;AAClF,2DAA2D;AAC3D,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,sEAAsE;AACtE,4EAA4E;AAC5E,iFAAiF;AACjF,mBAAmB;AAInB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAuB,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,0BAA0B,EAAqC,MAAM,oBAAoB,CAAC;AAInG;;;;;GAKG;AACH,MA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IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC9F,6EAA6E;QAC7E,8EAA8E;QAC9E,+EAA+E;QAC/E,+EAA+E;QAC/E,8EAA8E;QAC9E,mDAAmD;QACnD,MAAM,EAAE,GAAG,CAAC,CAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,IAAI,MAAM,CAAC,gBAAgB,CAAC;QACjE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QACvD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CACF"}
|
package/dist/transport/ws.d.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
import type { Address, MarketId } from "../types/public.js";
|
|
2
|
-
export type Channel = "pod_orderbook" | "
|
|
2
|
+
export type Channel = "pod_orderbook" | "pod_orders_v2" | "pod_candles" | "pod_markets" | "pod_positions" | "pod_triggers";
|
|
3
3
|
export interface SubParams {
|
|
4
4
|
clobIds?: MarketId[];
|
|
5
5
|
account?: Address;
|
|
6
6
|
depth?: number;
|
|
7
7
|
since?: number;
|
|
8
|
+
/**
|
|
9
|
+
* `pod_orders_v2` only: the `book` of the last frame accepted at `since`,
|
|
10
|
+
* completing the resume cursor.
|
|
11
|
+
*
|
|
12
|
+
* One batch settles many orderbooks and is delivered as one frame each, so a
|
|
13
|
+
* client can hold part of a batch, and `since` alone cannot express that
|
|
14
|
+
* position. With this set, replay skips every book at or below it within
|
|
15
|
+
* `since`, then delivers later batches whole. Omitted means the whole of
|
|
16
|
+
* `since` arrived — what `since` means on every other channel.
|
|
17
|
+
*/
|
|
18
|
+
sinceBook?: MarketId;
|
|
8
19
|
}
|
|
9
20
|
export interface Subscription {
|
|
10
21
|
unsubscribe(): void;
|
|
@@ -24,6 +35,56 @@ export interface WsClientOptions {
|
|
|
24
35
|
maxDelayMs?: number;
|
|
25
36
|
idleTimeoutMs?: number;
|
|
26
37
|
}
|
|
38
|
+
/** What the server sends in a close notification's `params.error`. */
|
|
39
|
+
interface WireCloseError {
|
|
40
|
+
code?: unknown;
|
|
41
|
+
message?: unknown;
|
|
42
|
+
data?: {
|
|
43
|
+
resumable?: unknown;
|
|
44
|
+
resume_since?: unknown;
|
|
45
|
+
resume_since_book?: unknown;
|
|
46
|
+
missed?: unknown;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The server ended a subscription on its own initiative.
|
|
51
|
+
*
|
|
52
|
+
* Delivered to `subscribe`'s `onError`. The subscription is gone server-side, and
|
|
53
|
+
* the connection stays open — so nothing else will nudge you, and no further
|
|
54
|
+
* updates arrive on it. On a resumable close the client has already advanced the
|
|
55
|
+
* subscription's `since` to `resumeSince`, so restarting it is one call:
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* const sub = ws.subscribe("pod_orders_v2", { account }, onFrame, (err) => {
|
|
59
|
+
* if (err instanceof PodSubscriptionClosedError && err.resumable) sub.resubscribe();
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* `resumable` is false for a server bug (`-32023`) and for any close this version
|
|
64
|
+
* cannot parse: resubscribing immediately would most likely reproduce it, so back
|
|
65
|
+
* off or report it instead.
|
|
66
|
+
*/
|
|
67
|
+
export declare class PodSubscriptionClosedError extends Error {
|
|
68
|
+
/** -32020 lagged, -32021 node shutting down, -32022 send timeout, -32023 server bug. */
|
|
69
|
+
readonly code: number;
|
|
70
|
+
/** Whether resubscribing recovers the stream. */
|
|
71
|
+
readonly resumable: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Solution time (micros) the stream is square on. Absent when the subscription
|
|
74
|
+
* had not yet taken a whole tick.
|
|
75
|
+
*/
|
|
76
|
+
readonly resumeSince?: number;
|
|
77
|
+
/**
|
|
78
|
+
* `pod_orders_v2` only: the last book of `resumeSince` that was delivered, when
|
|
79
|
+
* that batch was only partly delivered. Absent means the batch landed whole.
|
|
80
|
+
*/
|
|
81
|
+
readonly resumeSinceBook?: MarketId;
|
|
82
|
+
/** `-32020` only: how many ticks the broadcast dropped. */
|
|
83
|
+
readonly missed?: number;
|
|
84
|
+
/** The raw `params.error`, for fields this version does not map. */
|
|
85
|
+
readonly raw: unknown;
|
|
86
|
+
constructor(raw: WireCloseError);
|
|
87
|
+
}
|
|
27
88
|
export declare class PodWsClient {
|
|
28
89
|
private readonly url;
|
|
29
90
|
private readonly WS;
|
|
@@ -57,4 +118,5 @@ export declare class PodWsClient {
|
|
|
57
118
|
private removeSub;
|
|
58
119
|
private onMessage;
|
|
59
120
|
}
|
|
121
|
+
export {};
|
|
60
122
|
//# sourceMappingURL=ws.d.ts.map
|