@pod-network/trade-sdk 0.2.0 → 0.4.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 +94 -2
- package/dist/client.d.ts +19 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +25 -13
- package/dist/client.js.map +1 -1
- package/dist/codec/bridge.d.ts +82 -0
- package/dist/codec/bridge.d.ts.map +1 -0
- package/dist/codec/bridge.js +114 -0
- package/dist/codec/bridge.js.map +1 -0
- package/dist/codec/decode.d.ts +15 -2
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +46 -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 +25 -1
- package/dist/codec/units.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- 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/sync/sources.d.ts +5 -1
- package/dist/sync/sources.d.ts.map +1 -1
- package/dist/sync/sources.js +16 -0
- package/dist/sync/sources.js.map +1 -1
- package/dist/sync/withdrawals.d.ts +5 -0
- package/dist/sync/withdrawals.d.ts.map +1 -0
- package/dist/sync/withdrawals.js +172 -0
- package/dist/sync/withdrawals.js.map +1 -0
- package/dist/transport/claim.d.ts +160 -0
- package/dist/transport/claim.d.ts.map +1 -0
- package/dist/transport/claim.js +341 -0
- package/dist/transport/claim.js.map +1 -0
- package/dist/transport/jsonrpc.d.ts +16 -0
- package/dist/transport/jsonrpc.d.ts.map +1 -0
- package/dist/transport/jsonrpc.js +42 -0
- package/dist/transport/jsonrpc.js.map +1 -0
- package/dist/transport/rest.d.ts +15 -1
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/transport/rest.js +25 -1
- package/dist/transport/rest.js.map +1 -1
- package/dist/transport/ws.d.ts +22 -2
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js +14 -2
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +212 -9
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +147 -41
- package/dist/types/wire.d.ts.map +1 -1
- package/dist/write/delegation.d.ts.map +1 -1
- package/dist/write/delegation.js +2 -12
- package/dist/write/delegation.js.map +1 -1
- package/dist/write/index.d.ts +26 -1
- package/dist/write/index.d.ts.map +1 -1
- package/dist/write/index.js +21 -14
- package/dist/write/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +33 -18
- package/src/codec/bridge.ts +138 -0
- package/src/codec/decode.ts +54 -10
- package/src/codec/direction.ts +36 -0
- package/src/codec/orders-v2.ts +222 -0
- package/src/codec/units.ts +24 -1
- package/src/index.ts +16 -0
- package/src/sync/orders.ts +203 -83
- package/src/sync/sources.ts +16 -1
- package/src/sync/withdrawals.ts +177 -0
- package/src/transport/claim.ts +434 -0
- package/src/transport/jsonrpc.ts +54 -0
- package/src/transport/rest.ts +35 -7
- package/src/transport/ws.ts +30 -4
- package/src/types/public.ts +233 -7
- package/src/types/wire.ts +161 -29
- package/src/write/delegation.ts +2 -12
- package/src/write/index.ts +59 -13
package/src/sync/orders.ts
CHANGED
|
@@ -1,39 +1,97 @@
|
|
|
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
|
|
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.
|
|
8
11
|
|
|
9
|
-
import type { Address, MarketId, Order,
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
12
|
-
import { decodeOrder } from "../codec/decode.js";
|
|
12
|
+
import type { Address, MarketId, Order, OrderEvent, OrdersQuery } from "../types/public.js";
|
|
13
|
+
import type { WireOrdersFrame } from "../types/wire.js";
|
|
14
|
+
import { applyOrdersFrame } from "../codec/orders-v2.js";
|
|
13
15
|
import { BaseResource, type ResourceHandle } from "../stores/resource.js";
|
|
14
|
-
import type
|
|
16
|
+
import { PodSubscriptionClosedError, type SubParams, type Subscription } from "../transport/ws.js";
|
|
15
17
|
import type { SeriesResource } from "./candles.js";
|
|
16
18
|
import type { SyncContext } from "./sources.js";
|
|
17
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Consecutive server closes we resume from before falling back to the re-seed
|
|
22
|
+
* path. A lagged close is recoverable by resubscribing, so the fast path is the
|
|
23
|
+
* right default — but a stream that keeps closing is one we are not keeping up
|
|
24
|
+
* with, and re-seeding beats replaying a growing backlog on every attempt.
|
|
25
|
+
*/
|
|
26
|
+
const FAST_RESUMES_BEFORE_RESEED = 3;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Re-seed attempts that keep the cursor before we conclude the cursor is what the
|
|
30
|
+
* server is rejecting, drop it, and settle for streaming live.
|
|
31
|
+
*/
|
|
32
|
+
const RETRIES_BEFORE_DROPPING_CURSOR = 2;
|
|
33
|
+
|
|
34
|
+
/** The largest book id, which is what an absent `sinceBook` means. */
|
|
35
|
+
const WHOLE_BATCH = `0x${"ff".repeat(32)}` as MarketId;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Order two positions in the stream, the way the server does.
|
|
39
|
+
*
|
|
40
|
+
* A batch is delivered as one frame per book, so a position is the pair
|
|
41
|
+
* `(batch, book)` — and an absent book means the whole batch, which sorts *above*
|
|
42
|
+
* every book in it. This mirrors `already_delivered` in
|
|
43
|
+
* `node/src/rpc/orders_v2.rs`: `(frame_batch, frame_book) <= (since,
|
|
44
|
+
* since_book.unwrap_or(0xff…))`. Getting the absent case wrong turns "all of batch
|
|
45
|
+
* N" into "up to book B of batch N", which asks the server to re-send the rest.
|
|
46
|
+
*
|
|
47
|
+
* Book ids are fixed-width lowercase hex, so comparing them as strings is
|
|
48
|
+
* comparing their bytes.
|
|
49
|
+
*/
|
|
50
|
+
export function compareCursor(a: SubParams, b: SubParams): number {
|
|
51
|
+
const aBatch = a.since ?? 0;
|
|
52
|
+
const bBatch = b.since ?? 0;
|
|
53
|
+
if (aBatch !== bBatch) return aBatch < bBatch ? -1 : 1;
|
|
54
|
+
const aBook = a.sinceBook ?? WHOLE_BATCH;
|
|
55
|
+
const bBook = b.sinceBook ?? WHOLE_BATCH;
|
|
56
|
+
return aBook === bBook ? 0 : aBook < bBook ? -1 : 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
18
59
|
export class OrderHistory implements SeriesResource<Order> {
|
|
19
60
|
private readonly base: BaseResource<Order[]>;
|
|
20
61
|
private handle: ResourceHandle<Order[]> | undefined;
|
|
21
62
|
private readonly byId = new Map<string, Order>();
|
|
22
63
|
private nextCursor: string | null = null;
|
|
23
64
|
private sub: Subscription | undefined;
|
|
24
|
-
private watermarkUs = 0;
|
|
25
65
|
private alive = false;
|
|
26
66
|
private _hasMore = true;
|
|
27
67
|
private _loading = false;
|
|
28
68
|
private subRetries = 0;
|
|
69
|
+
private fastResumes = 0;
|
|
29
70
|
private retryTimer?: ReturnType<typeof setTimeout>;
|
|
71
|
+
private readonly eventListeners = new Set<(events: OrderEvent[]) => void>();
|
|
72
|
+
/**
|
|
73
|
+
* Where the stream is: the last frame accepted, as the `(batch, book)` pair the
|
|
74
|
+
* channel resumes from. Replaced whole rather than patched half at a time —
|
|
75
|
+
* `sinceBook` names a book *within* `since`, so the two are one fact and a
|
|
76
|
+
* mismatched pair asks the server to skip books we never saw.
|
|
77
|
+
*/
|
|
78
|
+
private cursor: SubParams = { since: 0, sinceBook: undefined };
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Push the cursor to the transport, both halves.
|
|
82
|
+
*
|
|
83
|
+
* `update` merges, so sending `{ since }` alone would leave whatever `sinceBook` was
|
|
84
|
+
* there before — a book from an older batch beside a newer `since`, which asks the
|
|
85
|
+
* server to skip less than it should and re-send the difference.
|
|
86
|
+
*/
|
|
87
|
+
private pushCursor(): void {
|
|
88
|
+
this.sub?.update({ since: this.cursor.since, sinceBook: this.cursor.sinceBook });
|
|
89
|
+
}
|
|
30
90
|
|
|
31
91
|
constructor(
|
|
32
92
|
private readonly ctx: SyncContext,
|
|
33
93
|
private readonly account: Address,
|
|
34
94
|
private readonly query: OrdersQuery = {},
|
|
35
|
-
/** Maps a WS order's token pair to its orderbook id (see rebuild). */
|
|
36
|
-
private readonly resolveOrderbookId?: (pair: { base: Address; quote: Address }) => MarketId | undefined,
|
|
37
95
|
) {
|
|
38
96
|
this.base = new BaseResource<Order[]>((h) => {
|
|
39
97
|
this.handle = h;
|
|
@@ -59,6 +117,26 @@ export class OrderHistory implements SeriesResource<Order> {
|
|
|
59
117
|
loading(): boolean { return this._loading; }
|
|
60
118
|
destroy(): void { this.base.destroy(); }
|
|
61
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Observe the transitions the stream reports, as they arrive — a frame at a time, in
|
|
122
|
+
* the order the engine caused them.
|
|
123
|
+
*
|
|
124
|
+
* The snapshot (`get`/`subscribe`) is what to render; this is what *happened*, which
|
|
125
|
+
* a snapshot cannot express: two fills in one batch are one state change, and a
|
|
126
|
+
* refused amendment is none at all. Nothing is emitted for the REST seed, so a
|
|
127
|
+
* consumer never has to filter out a backlog of history as if it were live.
|
|
128
|
+
*/
|
|
129
|
+
onEvent(listener: (events: OrderEvent[]) => void): () => void {
|
|
130
|
+
this.eventListeners.add(listener);
|
|
131
|
+
// Listening starts the stream, like subscribing does. The resource is ref-counted
|
|
132
|
+
// from `subscribe`/`ready` alone, so without holding one an event-only consumer
|
|
133
|
+
// would wait forever — and would fall silent the moment the last snapshot
|
|
134
|
+
// subscriber left, since teardown drops the websocket subscription and leaves the
|
|
135
|
+
// listeners registered.
|
|
136
|
+
const release = this.base.subscribe(() => {});
|
|
137
|
+
return () => { this.eventListeners.delete(listener); release(); };
|
|
138
|
+
}
|
|
139
|
+
|
|
62
140
|
setWindow(): void { /* order history pages by cursor, not by time window */ }
|
|
63
141
|
|
|
64
142
|
async loadOlder(): Promise<void> {
|
|
@@ -82,13 +160,27 @@ export class OrderHistory implements SeriesResource<Order> {
|
|
|
82
160
|
// --- internals ---
|
|
83
161
|
|
|
84
162
|
private async fetchFirstPage(): Promise<boolean> {
|
|
163
|
+
const before = this.cursor;
|
|
85
164
|
try {
|
|
86
165
|
const page = await this.ctx.rest.orders(this.account, { limit: this.query.limit ?? 100 });
|
|
87
166
|
if (!this.alive) return false;
|
|
88
|
-
|
|
167
|
+
// The transport resubscribes synchronously right after the `open` event that
|
|
168
|
+
// starts this fetch, so replayed frames can land while it is still in flight —
|
|
169
|
+
// and the indexer trails the stream by design. Overwriting a row the stream has
|
|
170
|
+
// already advanced would revert it, permanently: the cursor below refuses to
|
|
171
|
+
// rewind and `onFrame` drops a re-delivery, so nothing would repair it.
|
|
172
|
+
const streamMovedOn = compareCursor(this.cursor, before) > 0;
|
|
173
|
+
for (const o of page.orders) {
|
|
174
|
+
if (streamMovedOn && this.byId.has(o.id)) continue;
|
|
175
|
+
this.byId.set(o.id, o);
|
|
176
|
+
}
|
|
89
177
|
this.nextCursor = page.nextCursor;
|
|
90
178
|
this._hasMore = page.nextCursor !== null;
|
|
91
|
-
|
|
179
|
+
// Only ever forward, and a page settles whole batches, so its watermark
|
|
180
|
+
// carries no book — which makes it *ahead* of a stream position in the same
|
|
181
|
+
// batch, not equal to it.
|
|
182
|
+
const settled: SubParams = { since: page.solutionNow * 1000, sinceBook: undefined };
|
|
183
|
+
if (compareCursor(settled, this.cursor) > 0) this.cursor = settled;
|
|
92
184
|
this.rebuild();
|
|
93
185
|
return true;
|
|
94
186
|
} catch (e) {
|
|
@@ -102,104 +194,132 @@ export class OrderHistory implements SeriesResource<Order> {
|
|
|
102
194
|
if (!ok || !this.alive) return;
|
|
103
195
|
if (!this.sub) {
|
|
104
196
|
this.sub = this.ctx.ws.subscribe(
|
|
105
|
-
"
|
|
106
|
-
{ account: this.account,
|
|
107
|
-
(r) => this.
|
|
108
|
-
() => this.onSubError(),
|
|
197
|
+
"pod_orders_v2",
|
|
198
|
+
{ account: this.account, ...this.cursor },
|
|
199
|
+
(r) => this.onFrame(r),
|
|
200
|
+
(e) => this.onSubError(e),
|
|
109
201
|
);
|
|
110
202
|
} else {
|
|
111
|
-
this.
|
|
203
|
+
this.pushCursor(); // refresh for the next reconnect
|
|
112
204
|
}
|
|
113
205
|
});
|
|
114
206
|
}
|
|
115
207
|
|
|
116
208
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
209
|
+
* The subscription is not running: `eth_subscribe` was rejected, or the server
|
|
210
|
+
* closed it.
|
|
211
|
+
*
|
|
212
|
+
* A resumable close needs neither a re-seed nor a delay: the server reports where
|
|
213
|
+
* it stopped, so resubscribing from that delivers exactly the frames we never got.
|
|
214
|
+
* That fast path is only taken while the socket is actually open — `resubscribe()`
|
|
215
|
+
* is a no-op otherwise, which would spend the budget without an attempt and leave
|
|
216
|
+
* nothing scheduled, and `-32021` (node shutting down) arrives exactly as the
|
|
217
|
+
* socket goes away.
|
|
218
|
+
*
|
|
219
|
+
* Everything else (a rejection, a server bug, a close with the socket already
|
|
220
|
+
* gone, or closes that keep coming) takes the slow path: backed off and capped, so
|
|
221
|
+
* a server that keeps refusing cannot spin this into a tight re-seed loop, and
|
|
222
|
+
* eventually the cursor is dropped (the likely culprit) to just stream live. Both
|
|
223
|
+
* counters reset once live data flows (`onFrame`).
|
|
121
224
|
*/
|
|
122
|
-
private onSubError(): void {
|
|
225
|
+
private onSubError(err: unknown): void {
|
|
123
226
|
if (!this.alive) return;
|
|
227
|
+
const canFastResume = err instanceof PodSubscriptionClosedError && err.resumable
|
|
228
|
+
&& this.fastResumes < FAST_RESUMES_BEFORE_RESEED && this.ctx.ws.state === "open";
|
|
229
|
+
if (canFastResume) {
|
|
230
|
+
this.fastResumes++;
|
|
231
|
+
// Adopt the server's watermark only when it is ahead of ours: it knows which
|
|
232
|
+
// frames it handed over, but a re-seed may already have carried us past it,
|
|
233
|
+
// and rewinding would re-deliver frames we have applied.
|
|
234
|
+
const reported: SubParams = { since: err.resumeSince, sinceBook: err.resumeSinceBook };
|
|
235
|
+
if (err.resumeSince !== undefined && compareCursor(reported, this.cursor) > 0) this.cursor = reported;
|
|
236
|
+
// The transport rewrote `sub.params` from the close before this ran, so without
|
|
237
|
+
// pushing our own decision back the wire resumes from the server's point
|
|
238
|
+
// regardless and the guard above protects nothing.
|
|
239
|
+
this.pushCursor();
|
|
240
|
+
this.sub?.resubscribe();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// The slow path, and the one that answers "what if we fell behind the server's
|
|
244
|
+
// replay buffer": `eth_subscribe` rejects a `since` older than what the buffer
|
|
245
|
+
// retains, and the prescribed recovery is to backfill over REST and resubscribe.
|
|
246
|
+
// That is what this is. `fetchFirstPage` also *replaces the cursor* with the
|
|
247
|
+
// page's watermark, so the position that was too old is gone by the first retry
|
|
248
|
+
// and the resubscribe is accepted with no gap — the server replays from the page
|
|
249
|
+
// forward. Dropping the cursor below is the backstop for when even that fresh
|
|
250
|
+
// watermark is refused (the indexer further behind than the buffer retains):
|
|
251
|
+
// live-only resubscribe, trading the unreplayable window for a working stream.
|
|
252
|
+
this.scheduleReseed();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Back off, re-seed over REST, then resubscribe — and keep trying.
|
|
257
|
+
*
|
|
258
|
+
* The re-seed can fail too (the same node is usually behind both the stream and the
|
|
259
|
+
* indexer), and a failure has to re-arm here: not resubscribing means no further
|
|
260
|
+
* close or rejection arrives, so nothing else would ever schedule another attempt and
|
|
261
|
+
* the stream would stay down with no error surfaced. Capped, so a node that keeps
|
|
262
|
+
* refusing cannot spin this.
|
|
263
|
+
*/
|
|
264
|
+
private scheduleReseed(): void {
|
|
124
265
|
this.subRetries++;
|
|
125
266
|
const delay = Math.min(30_000, 500 * 2 ** (this.subRetries - 1));
|
|
126
267
|
if (this.retryTimer) clearTimeout(this.retryTimer);
|
|
127
268
|
this.retryTimer = setTimeout(() => {
|
|
128
269
|
void this.fetchFirstPage().then((ok) => {
|
|
129
|
-
if (!
|
|
130
|
-
|
|
270
|
+
if (!this.alive) return;
|
|
271
|
+
if (!ok) { this.scheduleReseed(); return; }
|
|
272
|
+
const tooOld = this.subRetries > RETRIES_BEFORE_DROPPING_CURSOR;
|
|
273
|
+
if (tooOld) this.sub?.update({ since: undefined, sinceBook: undefined });
|
|
274
|
+
else this.pushCursor();
|
|
131
275
|
this.sub?.resubscribe();
|
|
132
276
|
});
|
|
133
277
|
}, delay);
|
|
134
278
|
}
|
|
135
279
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
|
|
280
|
+
/** One frame: everything that happened to one book in one auction batch. */
|
|
281
|
+
private onFrame(result: unknown): void {
|
|
282
|
+
// Live data flowing → the subscription is healthy on both paths.
|
|
283
|
+
this.subRetries = 0;
|
|
284
|
+
this.fastResumes = 0;
|
|
285
|
+
const frame = result as WireOrdersFrame;
|
|
286
|
+
if (!frame || !Array.isArray(frame.orders) || !Array.isArray(frame.events)) return;
|
|
142
287
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const base = dec(u.base_amount);
|
|
162
|
-
const quote = dec(u.quote_amount);
|
|
163
|
-
e.fills = [...e.fills, { base, quote, price: base > 0n ? (quote * WAD) / base : 0n, time: Date.now() }];
|
|
164
|
-
}
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
case "modified": {
|
|
168
|
-
const e = this.byId.get(u.order_id);
|
|
169
|
-
if (e) {
|
|
170
|
-
e.price = dec(u.new_price);
|
|
171
|
-
const sign = e.initialSize < 0n ? -1n : 1n;
|
|
172
|
-
e.initialSize = sign * dec(u.new_size); // new_size is an unsigned magnitude
|
|
173
|
-
}
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
case "expired": {
|
|
177
|
-
const e = this.byId.get(u.order_id);
|
|
178
|
-
if (e) e.status = "expired";
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
case "canceled": {
|
|
182
|
-
const e = this.byId.get(u.order_id);
|
|
183
|
-
if (e) e.status = "canceled";
|
|
184
|
-
break;
|
|
288
|
+
// Drop what we already hold. Applying a frame is not idempotent — a fill event
|
|
289
|
+
// appends to `order.fills`, and re-creating an entity resets its totals — and
|
|
290
|
+
// re-delivery is designed in: a resumed subscription replays from a cursor, and
|
|
291
|
+
// the replay boundary is a whole batch. Same predicate as the server's
|
|
292
|
+
// `already_delivered`, so client and server agree on what "already sent" means.
|
|
293
|
+
const at: SubParams = { since: frame.batch, sinceBook: frame.book };
|
|
294
|
+
if (compareCursor(at, this.cursor) <= 0) return;
|
|
295
|
+
|
|
296
|
+
const events = applyOrdersFrame(frame, this.byId, { account: this.account });
|
|
297
|
+
// A socket-level reconnect resubscribes from whatever is stored here.
|
|
298
|
+
this.cursor = at;
|
|
299
|
+
this.sub?.update(at);
|
|
300
|
+
this.rebuild();
|
|
301
|
+
// Strictly after `rebuild()`: a listener that reads the resource in response to an
|
|
302
|
+
// event must see the state that event produced, not the state before it.
|
|
303
|
+
if (events.length) {
|
|
304
|
+
for (const listener of this.eventListeners) {
|
|
305
|
+
try { listener(events); } catch { /* a listener's failure is not the stream's */ }
|
|
185
306
|
}
|
|
186
307
|
}
|
|
187
308
|
}
|
|
188
309
|
|
|
189
310
|
private rebuild(): void {
|
|
190
311
|
if (!this.handle) return;
|
|
191
|
-
// WS stream orders carry a token `pair` instead of orderbook_id — resolve
|
|
192
|
-
// it here so consumers can always filter by market. Retried every rebuild,
|
|
193
|
-
// so orders that streamed in before the markets list loaded still resolve.
|
|
194
|
-
if (this.resolveOrderbookId) {
|
|
195
|
-
for (const o of this.byId.values()) {
|
|
196
|
-
if (o.orderbookId === undefined && o.pair) o.orderbookId = this.resolveOrderbookId(o.pair);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
312
|
let arr = [...this.byId.values()];
|
|
200
313
|
if (this.query.status) arr = arr.filter((o) => o.status === this.query.status);
|
|
201
314
|
if (this.query.orderbookId) arr = arr.filter((o) => o.orderbookId === this.query.orderbookId);
|
|
202
|
-
|
|
315
|
+
// Newest first by when the order became real. Inclusion time is the key both
|
|
316
|
+
// sources report — REST sends it alongside the signed deadline, and a frame's
|
|
317
|
+
// batch *is* it — so a REST row and a streamed row sort against each other. An
|
|
318
|
+
// order with none has not been in a batch yet, so it is newer than every order
|
|
319
|
+
// that has; the signed deadline is deliberately not a fallback, since it is a
|
|
320
|
+
// future time and would sort on a different clock.
|
|
321
|
+
const at = (o: Order) => o.includedMs ?? Number.MAX_SAFE_INTEGER;
|
|
322
|
+
arr.sort((a, b) => at(b) - at(a) || b.nonce - a.nonce);
|
|
203
323
|
this.handle.set(arr);
|
|
204
324
|
}
|
|
205
325
|
}
|
package/src/sync/sources.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// REST re-seed via `onError` when `since` is too old (down too long).
|
|
15
15
|
|
|
16
16
|
import type {
|
|
17
|
-
Address, Balances, Market, MarketId, Orderbook, PositionsSnapshot, Status, Trigger,
|
|
17
|
+
Address, Balances, BridgeConfig, Market, MarketId, Orderbook, PositionsSnapshot, Status, Trigger,
|
|
18
18
|
} from "../types/public.js";
|
|
19
19
|
import type {
|
|
20
20
|
WireMarketDynamics, WireOrderbook, WirePositionsPush, WireTriggersPush,
|
|
@@ -84,6 +84,21 @@ export function statusSource({ rest, ws }: SyncContext): ResourceSource<Status>
|
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
/** Static bridge config (ADR 0033 §5). Re-seeded on reconnect because an
|
|
88
|
+
* operator can change the token set or the withdraw limits under a long-lived
|
|
89
|
+
* tab, and a stale window would offer amounts the node now refuses. */
|
|
90
|
+
export function bridgeConfigSource({ rest, ws }: SyncContext): ResourceSource<BridgeConfig> {
|
|
91
|
+
return (h) => {
|
|
92
|
+
let alive = true;
|
|
93
|
+
const off = seedNowAndOnReconnect(ws, () => {
|
|
94
|
+
rest.bridgeConfig()
|
|
95
|
+
.then((c) => { if (alive) h.set(c); })
|
|
96
|
+
.catch((e) => { if (!h.current()) h.fail(e as Error); });
|
|
97
|
+
});
|
|
98
|
+
return () => { alive = false; off(); };
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
87
102
|
export function marketsSource(
|
|
88
103
|
{ rest, ws, marketResyncMs, marketsCache }: SyncContext,
|
|
89
104
|
): ResourceSource<Market[]> {
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Terminal withdrawal outcomes for one account (ADR 0033 §6): seeded and
|
|
2
|
+
// gap-filled over `GET /v1/bridge/withdrawals/{account}`, kept live by the
|
|
3
|
+
// `pod_withdrawals` subscription.
|
|
4
|
+
//
|
|
5
|
+
// The two surfaces serve the identical shape, so a backfilled outcome is
|
|
6
|
+
// indistinguishable from one that arrived live — which is what lets a reconnect
|
|
7
|
+
// be a plain "fetch the gap" rather than a separate code path.
|
|
8
|
+
//
|
|
9
|
+
// Outcomes are terminal and immutable: an id appears once and never changes, so
|
|
10
|
+
// merging is last-write-wins on the id with no reconciliation.
|
|
11
|
+
|
|
12
|
+
import type { Address, Withdrawal } from "../types/public.js";
|
|
13
|
+
import type { WireWithdrawal } from "../types/wire.js";
|
|
14
|
+
import { decodeWithdrawal } from "../codec/decode.js";
|
|
15
|
+
import type { ResourceSource } from "../stores/resource.js";
|
|
16
|
+
import { seedNowAndOnReconnect, type SyncContext } from "./sources.js";
|
|
17
|
+
|
|
18
|
+
/** Page size for the backfill. The server caps at 1000 and defaults to 500. */
|
|
19
|
+
const PAGE_LIMIT = 500;
|
|
20
|
+
|
|
21
|
+
/** Pages to walk in one backfill before giving up, so a bad cursor can't spin
|
|
22
|
+
* forever. 50 pages × 500 covers 25k outcomes — far past any real account. */
|
|
23
|
+
const MAX_PAGES = 50;
|
|
24
|
+
|
|
25
|
+
/** Resubscribe attempts after a rejection before leaving the channel alone.
|
|
26
|
+
* Reached only when the rejection is permanent — see the `onError` comment. */
|
|
27
|
+
const MAX_SUB_RETRIES = 5;
|
|
28
|
+
|
|
29
|
+
export function withdrawalsSource(
|
|
30
|
+
{ rest, ws }: SyncContext,
|
|
31
|
+
account: Address,
|
|
32
|
+
): ResourceSource<Withdrawal[]> {
|
|
33
|
+
return (h) => {
|
|
34
|
+
let alive = true;
|
|
35
|
+
const byId = new Map<string, Withdrawal>();
|
|
36
|
+
// Cursor into the outcome log: the newest tick fully absorbed, plus the last
|
|
37
|
+
// id seen inside it. Both are needed because one tick can span a page — the
|
|
38
|
+
// deadline alone can only re-serve the tick's earlier rows or skip its later
|
|
39
|
+
// ones.
|
|
40
|
+
let sinceUs = 0;
|
|
41
|
+
let sinceId: Withdrawal["id"] | undefined;
|
|
42
|
+
let retries = 0;
|
|
43
|
+
let retryTimer: ReturnType<typeof setTimeout> | undefined;
|
|
44
|
+
|
|
45
|
+
const publish = () => {
|
|
46
|
+
// Newest first: a user reads their most recent withdrawal at the top, and
|
|
47
|
+
// the id tiebreak keeps a same-tick pair from reordering between renders.
|
|
48
|
+
h.set([...byId.values()].sort((a, b) => b.timeUs - a.timeUs || b.id.localeCompare(a.id)));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const absorb = (items: Withdrawal[]) => {
|
|
52
|
+
for (const w of items) {
|
|
53
|
+
byId.set(w.id, w);
|
|
54
|
+
// Never move the cursor backwards: an out-of-order arrival would
|
|
55
|
+
// otherwise re-serve ticks we already hold.
|
|
56
|
+
if (w.timeUs >= sinceUs) { sinceUs = w.timeUs; sinceId = w.id; }
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Walk forward from the cursor until the log is exhausted.
|
|
62
|
+
*
|
|
63
|
+
* Serialised on `inflight` because two callers race by construction: the
|
|
64
|
+
* immediate seed and the socket's `open` overlap on a cold start, and a
|
|
65
|
+
* reconnect can fire while a retry's backfill is still paging. Both share
|
|
66
|
+
* the cursor, so concurrent loops would re-request each other's pages.
|
|
67
|
+
*/
|
|
68
|
+
let inflight: Promise<void> | undefined;
|
|
69
|
+
const backfill = (): Promise<void> => (inflight ??= run().finally(() => { inflight = undefined; }));
|
|
70
|
+
|
|
71
|
+
const run = async () => {
|
|
72
|
+
try {
|
|
73
|
+
// Page against a cursor local to this run. The shared one is also moved
|
|
74
|
+
// by live pushes, and a push landing mid-backfill would jump the next
|
|
75
|
+
// page past the rest of an older tick — rows the forward-only cursor
|
|
76
|
+
// could then never come back for.
|
|
77
|
+
let pageSince = sinceUs;
|
|
78
|
+
let pageSinceId = sinceId;
|
|
79
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
80
|
+
const rows = await rest.bridgeWithdrawals(account, {
|
|
81
|
+
since: pageSince,
|
|
82
|
+
sinceId: pageSinceId,
|
|
83
|
+
limit: PAGE_LIMIT,
|
|
84
|
+
});
|
|
85
|
+
if (!alive) return;
|
|
86
|
+
if (rows.length === 0) break;
|
|
87
|
+
absorb(rows);
|
|
88
|
+
// Rows come back ascending, so the last one is this page's high-water
|
|
89
|
+
// mark — regardless of what the stream did to the shared cursor.
|
|
90
|
+
// Non-empty by the check above.
|
|
91
|
+
const last = rows[rows.length - 1]!;
|
|
92
|
+
pageSince = last.timeUs;
|
|
93
|
+
pageSinceId = last.id;
|
|
94
|
+
if (rows.length < PAGE_LIMIT) break;
|
|
95
|
+
}
|
|
96
|
+
// Once, after paging: publishing per page re-copies and re-sorts
|
|
97
|
+
// everything accumulated so far for a list the user sees for
|
|
98
|
+
// milliseconds — O(pages²) on exactly the accounts paging exists for.
|
|
99
|
+
//
|
|
100
|
+
// Unconditional, including when nothing was found. An account with no
|
|
101
|
+
// withdrawals must still reach a defined `[]`, or every consumer stays
|
|
102
|
+
// on "not loaded yet" and treats the first live outcome as its initial
|
|
103
|
+
// snapshot — silently swallowing a user's first-ever withdrawal.
|
|
104
|
+
publish();
|
|
105
|
+
} catch (e) {
|
|
106
|
+
// Publish on the failure path too, for exactly the reason the success
|
|
107
|
+
// path publishes unconditionally: a consumer left on "not loaded yet"
|
|
108
|
+
// treats the first live outcome as its initial snapshot and swallows it.
|
|
109
|
+
// A failed backfill is a reason to have no history — not a reason to
|
|
110
|
+
// misread the next thing that happens. `fail` records the error beside
|
|
111
|
+
// the value rather than replacing it, so a caller can read both.
|
|
112
|
+
publish();
|
|
113
|
+
// Only surface the failure when there is nothing to show; otherwise the
|
|
114
|
+
// stream is still feeding a usable list and a toast would be noise.
|
|
115
|
+
if (byId.size === 0) h.fail(e as Error);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const sub = ws.subscribe(
|
|
120
|
+
"pod_withdrawals",
|
|
121
|
+
// Live-only until a cursor exists. This is a replay channel, so the server
|
|
122
|
+
// accepts `since` only when its buffer still reaches back that far — and
|
|
123
|
+
// `since: 0` never does, so sending it would fail the subscribe every time
|
|
124
|
+
// and burn the retry budget below on a self-inflicted rejection. (The
|
|
125
|
+
// `since: 0` idiom belongs to the *state* channels, which answer it with a
|
|
126
|
+
// snapshot.) REST backfill covers history either way.
|
|
127
|
+
{ account },
|
|
128
|
+
(result) => {
|
|
129
|
+
retries = 0;
|
|
130
|
+
// A frame means the subscription is healthy, so a retry armed by an
|
|
131
|
+
// earlier rejection has nothing left to fix. Letting it fire would call
|
|
132
|
+
// `resubscribe()` on a live subscription, which drops the server's id
|
|
133
|
+
// without an `eth_unsubscribe` and leaks one subscription per occurrence.
|
|
134
|
+
if (retryTimer) { clearTimeout(retryTimer); retryTimer = undefined; }
|
|
135
|
+
// One message per tick carrying that tick's outcomes for this account.
|
|
136
|
+
absorb((result as WireWithdrawal[]).map(decodeWithdrawal));
|
|
137
|
+
publish();
|
|
138
|
+
sub.update({ since: sinceUs }); // keep the resume point fresh for a reconnect
|
|
139
|
+
},
|
|
140
|
+
() => {
|
|
141
|
+
// Rejected — usually a `since` older than the replay buffer. Back off,
|
|
142
|
+
// re-seed over REST (which has no such horizon), and resubscribe; after
|
|
143
|
+
// a couple of failures drop `since` and settle for live-only.
|
|
144
|
+
//
|
|
145
|
+
// Bounded, because the other reason for a rejection is a node that
|
|
146
|
+
// doesn't serve this channel at all — an older build, since the app
|
|
147
|
+
// ships ahead of the fleet. Retrying that forever is a request every
|
|
148
|
+
// 30s for the lifetime of the tab, so give up and leave the REST-seeded
|
|
149
|
+
// list in place rather than polling a node that will never answer.
|
|
150
|
+
if (!alive || retries >= MAX_SUB_RETRIES) return;
|
|
151
|
+
retries++;
|
|
152
|
+
if (retryTimer) clearTimeout(retryTimer);
|
|
153
|
+
retryTimer = setTimeout(() => {
|
|
154
|
+
void backfill().then(() => {
|
|
155
|
+
if (!alive) return;
|
|
156
|
+
// Live-only until a real cursor exists (see the subscribe above), and
|
|
157
|
+
// dropped again after a couple of failures in case the replay buffer
|
|
158
|
+
// has outrun it.
|
|
159
|
+
sub.update({ since: retries > 2 || sinceUs === 0 ? undefined : sinceUs });
|
|
160
|
+
sub.resubscribe();
|
|
161
|
+
});
|
|
162
|
+
}, Math.min(30_000, 500 * 2 ** (retries - 1)));
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
// Seed immediately — REST needs no socket, so history paints without waiting
|
|
167
|
+
// on the handshake — and fill the gap again on every reconnect.
|
|
168
|
+
const offOpen = seedNowAndOnReconnect(ws, () => { if (alive) void backfill(); });
|
|
169
|
+
|
|
170
|
+
return () => {
|
|
171
|
+
alive = false;
|
|
172
|
+
offOpen();
|
|
173
|
+
if (retryTimer) clearTimeout(retryTimer);
|
|
174
|
+
sub.unsubscribe();
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
}
|