@proof-labs/trading-sdk 4.0.0 → 5.0.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 +137 -6
- package/dist/account-state.d.ts +17 -0
- package/dist/account-state.d.ts.map +1 -0
- package/dist/account-state.js +63 -0
- package/dist/account-state.js.map +1 -0
- package/dist/client.d.ts +112 -31
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +472 -188
- package/dist/client.js.map +1 -1
- package/dist/codec-adapter.d.ts.map +1 -1
- package/dist/codec-adapter.js +11 -4
- package/dist/codec-adapter.js.map +1 -1
- package/dist/crypto.d.ts +20 -0
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +39 -0
- package/dist/crypto.js.map +1 -1
- package/dist/errors.d.ts +26 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +111 -15
- package/dist/errors.js.map +1 -1
- package/dist/feed-types.d.ts +76 -0
- package/dist/feed-types.d.ts.map +1 -0
- package/dist/feed-types.js +2 -0
- package/dist/feed-types.js.map +1 -0
- package/dist/feed.d.ts +158 -0
- package/dist/feed.d.ts.map +1 -0
- package/dist/feed.js +474 -0
- package/dist/feed.js.map +1 -0
- package/dist/financial-state.d.ts +65 -0
- package/dist/financial-state.d.ts.map +1 -0
- package/dist/financial-state.js +345 -0
- package/dist/financial-state.js.map +1 -0
- package/dist/gateway-reads.d.ts +74 -0
- package/dist/gateway-reads.d.ts.map +1 -0
- package/dist/gateway-reads.js +91 -0
- package/dist/gateway-reads.js.map +1 -0
- package/dist/governance-query.d.ts +7 -13
- package/dist/governance-query.d.ts.map +1 -1
- package/dist/governance-query.js +131 -70
- package/dist/governance-query.js.map +1 -1
- package/dist/history.d.ts +17 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +150 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +13 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -3
- package/dist/index.js.map +1 -1
- package/dist/market-snapshot.d.ts +11 -0
- package/dist/market-snapshot.d.ts.map +1 -0
- package/dist/market-snapshot.js +325 -0
- package/dist/market-snapshot.js.map +1 -0
- package/dist/oracle-permissions.d.ts +51 -0
- package/dist/oracle-permissions.d.ts.map +1 -0
- package/dist/oracle-permissions.js +191 -0
- package/dist/oracle-permissions.js.map +1 -0
- package/dist/oracle-price-history.d.ts +30 -0
- package/dist/oracle-price-history.d.ts.map +1 -0
- package/dist/oracle-price-history.js +108 -0
- package/dist/oracle-price-history.js.map +1 -0
- package/dist/portfolio-history.d.ts +21 -0
- package/dist/portfolio-history.d.ts.map +1 -0
- package/dist/portfolio-history.js +49 -0
- package/dist/portfolio-history.js.map +1 -0
- package/dist/tx-result.d.ts +6 -0
- package/dist/tx-result.d.ts.map +1 -1
- package/dist/tx-result.js +42 -0
- package/dist/tx-result.js.map +1 -1
- package/dist/types.d.ts +235 -114
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +14 -5
- package/dist/types.js.map +1 -1
- package/dist/wasm/proof_trading_sdk_wasm_bg.wasm +0 -0
- package/package.json +5 -1
package/dist/feed.js
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
const CONTROL_TYPES = new Set(["subscribed", "unsubscribed", "pong", "error"]);
|
|
2
|
+
/**
|
|
3
|
+
* Lifecycle manager for the gateway's multiplexed `/ws` feed.
|
|
4
|
+
*
|
|
5
|
+
* One socket carries every channel. The class owns the full lifecycle so
|
|
6
|
+
* callers only deal with subscriptions:
|
|
7
|
+
*
|
|
8
|
+
* - **lazy connect** — the socket opens on the first `subscribe` and closes
|
|
9
|
+
* when the last subscription is disposed.
|
|
10
|
+
* - **auto-reconnect** — on an unexpected close it reconnects with
|
|
11
|
+
* exponential backoff and **replays every active subscription**, so a
|
|
12
|
+
* fresh snapshot arrives. Missed live events are not replayed.
|
|
13
|
+
* - **heartbeat** — an app-level `ping` runs on an interval; a missing
|
|
14
|
+
* `pong` forces a reconnect, catching half-open sockets the OS hasn't
|
|
15
|
+
* torn down.
|
|
16
|
+
*
|
|
17
|
+
* `subscribe` returns a disposer; the socket is reference-counted across all
|
|
18
|
+
* subscriptions. Construct via {@link ExchangeClient.feed} rather than
|
|
19
|
+
* directly so URL/auth derive from the client.
|
|
20
|
+
*/
|
|
21
|
+
export class GatewayFeed {
|
|
22
|
+
accountAuth;
|
|
23
|
+
url;
|
|
24
|
+
headers;
|
|
25
|
+
pingIntervalMs;
|
|
26
|
+
pongTimeoutMs;
|
|
27
|
+
minBackoffMs;
|
|
28
|
+
maxBackoffMs;
|
|
29
|
+
factory;
|
|
30
|
+
setTimer;
|
|
31
|
+
clearTimer;
|
|
32
|
+
onError;
|
|
33
|
+
onStateChange;
|
|
34
|
+
ws = null;
|
|
35
|
+
state = "closed";
|
|
36
|
+
nextId = 1;
|
|
37
|
+
subs = new Map();
|
|
38
|
+
/** True between `close()` and the next `subscribe` — suppresses reconnect. */
|
|
39
|
+
closedByUser = false;
|
|
40
|
+
reconnectAttempts = 0;
|
|
41
|
+
reconnectTimer = null;
|
|
42
|
+
pingTimer = null;
|
|
43
|
+
pongTimer = null;
|
|
44
|
+
awaitingPong = false;
|
|
45
|
+
constructor(opts) {
|
|
46
|
+
this.accountAuth = opts.accountAuth;
|
|
47
|
+
this.url = opts.url;
|
|
48
|
+
this.headers = opts.headers;
|
|
49
|
+
this.pingIntervalMs = opts.pingIntervalMs ?? 15_000;
|
|
50
|
+
this.pongTimeoutMs = opts.pongTimeoutMs ?? 10_000;
|
|
51
|
+
this.minBackoffMs = opts.minBackoffMs ?? 500;
|
|
52
|
+
this.maxBackoffMs = opts.maxBackoffMs ?? 10_000;
|
|
53
|
+
this.setTimer = opts.setTimeoutImpl ?? ((fn, ms) => setTimeout(fn, ms));
|
|
54
|
+
this.clearTimer =
|
|
55
|
+
opts.clearTimeoutImpl ??
|
|
56
|
+
((h) => clearTimeout(h));
|
|
57
|
+
this.onError = opts.onError;
|
|
58
|
+
this.onStateChange = opts.onStateChange;
|
|
59
|
+
this.factory = opts.webSocketFactory ?? defaultFactory(opts.WebSocketImpl);
|
|
60
|
+
}
|
|
61
|
+
/** Current connection state. */
|
|
62
|
+
get connectionState() {
|
|
63
|
+
return this.state;
|
|
64
|
+
}
|
|
65
|
+
/** Number of distinct active channel subscriptions. */
|
|
66
|
+
get subscriptionCount() {
|
|
67
|
+
return this.subs.size;
|
|
68
|
+
}
|
|
69
|
+
// -- public channel helpers ------------------------------------------------
|
|
70
|
+
subscribeOrderbook(market, onFrame) {
|
|
71
|
+
return this.subscribe("orderbook", { market }, onFrame);
|
|
72
|
+
}
|
|
73
|
+
subscribeTrades(market, onFrame) {
|
|
74
|
+
return this.subscribe("trades", { market }, onFrame);
|
|
75
|
+
}
|
|
76
|
+
/** Subscribe to `accountEvents` for `owner` (lower-hex, 20 bytes). `params`
|
|
77
|
+
* carries any signed-auth fields already serialised to wire (snake_case). */
|
|
78
|
+
subscribeAccount(owner, onFrame, authParams = {}) {
|
|
79
|
+
return this.subscribe("accountEvents", {
|
|
80
|
+
...(typeof authParams === "function" ? {} : authParams),
|
|
81
|
+
owner: owner.replace(/^0x/i, "").toLowerCase(),
|
|
82
|
+
}, onFrame, typeof authParams === "function" ? authParams : undefined);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Unsubscribe a whole channel by key, dropping **all** of its listeners —
|
|
86
|
+
* the by-key counterpart to the per-listener disposer returned by
|
|
87
|
+
* `subscribe*`. Returns true if a subscription was present. Sends the wire
|
|
88
|
+
* `unsubscribe` and closes the socket if no subscriptions remain.
|
|
89
|
+
*/
|
|
90
|
+
unsubscribeOrderbook(market) {
|
|
91
|
+
return this.unsubscribe("orderbook", { market });
|
|
92
|
+
}
|
|
93
|
+
unsubscribeTrades(market) {
|
|
94
|
+
return this.unsubscribe("trades", { market });
|
|
95
|
+
}
|
|
96
|
+
unsubscribeAccount(owner) {
|
|
97
|
+
return this.unsubscribe("accountEvents", { owner: owner.toLowerCase() });
|
|
98
|
+
}
|
|
99
|
+
/** Low-level unsubscribe by channel + key params (`market` / `owner`). */
|
|
100
|
+
unsubscribe(channel, params) {
|
|
101
|
+
const key = subKey(channel, params);
|
|
102
|
+
const record = this.subs.get(key);
|
|
103
|
+
if (!record)
|
|
104
|
+
return false;
|
|
105
|
+
this.dropRecord(key, record);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Low-level subscribe. `params` must use the gateway's wire keys
|
|
110
|
+
* (`market`, `owner`, `public_key`, …). Returns a disposer
|
|
111
|
+
* that drops this listener and unsubscribes the channel when its last
|
|
112
|
+
* listener goes away.
|
|
113
|
+
*/
|
|
114
|
+
subscribe(channel, params, onFrame, auth) {
|
|
115
|
+
// Each subscription call owns a separate reference, even for the same callback.
|
|
116
|
+
const listener = (frame) => onFrame(frame);
|
|
117
|
+
const key = subKey(channel, params);
|
|
118
|
+
this.closedByUser = false;
|
|
119
|
+
let record = this.subs.get(key);
|
|
120
|
+
if (record) {
|
|
121
|
+
record.listeners.add(listener);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
record = {
|
|
125
|
+
channel,
|
|
126
|
+
params,
|
|
127
|
+
listeners: new Set([listener]),
|
|
128
|
+
lastId: 0,
|
|
129
|
+
auth,
|
|
130
|
+
generation: 0,
|
|
131
|
+
};
|
|
132
|
+
this.subs.set(key, record);
|
|
133
|
+
this.ensureConnected();
|
|
134
|
+
if (this.state === "open")
|
|
135
|
+
this.sendSubscribe(record);
|
|
136
|
+
}
|
|
137
|
+
return () => {
|
|
138
|
+
const r = this.subs.get(key);
|
|
139
|
+
if (!r || r !== record)
|
|
140
|
+
return;
|
|
141
|
+
r.listeners.delete(listener);
|
|
142
|
+
if (r.listeners.size === 0)
|
|
143
|
+
this.dropRecord(key, r);
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** Remove a subscription record: emit the wire `unsubscribe` when open, and
|
|
147
|
+
* close the socket once the last subscription is gone. Shared by the
|
|
148
|
+
* per-listener disposer and the by-key `unsubscribe*` methods. */
|
|
149
|
+
dropRecord(key, record) {
|
|
150
|
+
this.subs.delete(key);
|
|
151
|
+
if (this.state === "open")
|
|
152
|
+
this.sendUnsubscribe(record);
|
|
153
|
+
if (this.subs.size === 0)
|
|
154
|
+
this.close();
|
|
155
|
+
}
|
|
156
|
+
/** Tear down the socket and all subscriptions. Suppresses reconnect until
|
|
157
|
+
* the next `subscribe`. Idempotent. */
|
|
158
|
+
close() {
|
|
159
|
+
this.closedByUser = true;
|
|
160
|
+
this.subs.clear();
|
|
161
|
+
this.clearReconnect();
|
|
162
|
+
this.stopHeartbeat();
|
|
163
|
+
this.teardownSocket();
|
|
164
|
+
this.setState("closed");
|
|
165
|
+
}
|
|
166
|
+
// -- connection lifecycle --------------------------------------------------
|
|
167
|
+
ensureConnected() {
|
|
168
|
+
if (this.ws ||
|
|
169
|
+
this.state === "connecting" ||
|
|
170
|
+
this.state === "reconnecting") {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.connect();
|
|
174
|
+
}
|
|
175
|
+
connect() {
|
|
176
|
+
this.setState(this.reconnectAttempts > 0 ? "reconnecting" : "connecting");
|
|
177
|
+
let ws;
|
|
178
|
+
try {
|
|
179
|
+
ws = this.factory(this.url, this.headers ? { headers: this.headers } : undefined);
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
183
|
+
this.scheduleReconnect();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.ws = ws;
|
|
187
|
+
ws.onopen = () => {
|
|
188
|
+
this.reconnectAttempts = 0;
|
|
189
|
+
this.setState("open");
|
|
190
|
+
// Replay every active subscription — a reconnect yields fresh snapshots.
|
|
191
|
+
for (const record of this.subs.values())
|
|
192
|
+
this.sendSubscribe(record);
|
|
193
|
+
this.startHeartbeat();
|
|
194
|
+
};
|
|
195
|
+
ws.onmessage = (ev) => {
|
|
196
|
+
const text = extractText(ev.data);
|
|
197
|
+
if (text === null)
|
|
198
|
+
return;
|
|
199
|
+
this.handleMessage(text);
|
|
200
|
+
};
|
|
201
|
+
ws.onerror = (ev) => {
|
|
202
|
+
// Browsers fire `error` then `close`; let `onclose` drive reconnect so
|
|
203
|
+
// we don't double-schedule. Surface a best-effort error for visibility.
|
|
204
|
+
this.onError?.(toError(ev));
|
|
205
|
+
};
|
|
206
|
+
ws.onclose = () => {
|
|
207
|
+
this.stopHeartbeat();
|
|
208
|
+
ws.onopen = ws.onclose = ws.onerror = ws.onmessage = null;
|
|
209
|
+
this.ws = null;
|
|
210
|
+
if (this.closedByUser || this.subs.size === 0) {
|
|
211
|
+
this.setState("closed");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
this.scheduleReconnect();
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
teardownSocket() {
|
|
218
|
+
const ws = this.ws;
|
|
219
|
+
if (!ws)
|
|
220
|
+
return;
|
|
221
|
+
ws.onopen = ws.onclose = ws.onerror = ws.onmessage = null;
|
|
222
|
+
this.ws = null;
|
|
223
|
+
try {
|
|
224
|
+
ws.close();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// already closing/closed
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
scheduleReconnect() {
|
|
231
|
+
this.clearReconnect();
|
|
232
|
+
const delay = Math.min(this.maxBackoffMs, this.minBackoffMs * 2 ** this.reconnectAttempts);
|
|
233
|
+
this.reconnectAttempts += 1;
|
|
234
|
+
this.setState("reconnecting");
|
|
235
|
+
this.reconnectTimer = this.setTimer(() => {
|
|
236
|
+
this.reconnectTimer = null;
|
|
237
|
+
if (this.closedByUser || this.subs.size === 0)
|
|
238
|
+
return;
|
|
239
|
+
this.connect();
|
|
240
|
+
}, delay);
|
|
241
|
+
}
|
|
242
|
+
clearReconnect() {
|
|
243
|
+
if (this.reconnectTimer != null) {
|
|
244
|
+
this.clearTimer(this.reconnectTimer);
|
|
245
|
+
this.reconnectTimer = null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// -- heartbeat -------------------------------------------------------------
|
|
249
|
+
startHeartbeat() {
|
|
250
|
+
this.stopHeartbeat();
|
|
251
|
+
if (this.pingIntervalMs <= 0)
|
|
252
|
+
return;
|
|
253
|
+
this.awaitingPong = false;
|
|
254
|
+
this.pingTimer = this.setTimer(() => this.sendPing(), this.pingIntervalMs);
|
|
255
|
+
}
|
|
256
|
+
sendPing() {
|
|
257
|
+
if (this.state !== "open" || !this.ws || this.awaitingPong)
|
|
258
|
+
return;
|
|
259
|
+
this.send({ method: "ping", id: this.nextId++ });
|
|
260
|
+
this.awaitingPong = true;
|
|
261
|
+
this.pongTimer = this.setTimer(() => {
|
|
262
|
+
if (this.awaitingPong) {
|
|
263
|
+
// Half-open socket: force a reconnect via teardown.
|
|
264
|
+
this.stopHeartbeat();
|
|
265
|
+
this.teardownSocket();
|
|
266
|
+
if (!this.closedByUser && this.subs.size > 0)
|
|
267
|
+
this.scheduleReconnect();
|
|
268
|
+
else
|
|
269
|
+
this.setState("closed");
|
|
270
|
+
}
|
|
271
|
+
}, this.pongTimeoutMs);
|
|
272
|
+
// Re-arm the next ping.
|
|
273
|
+
this.pingTimer = this.setTimer(() => this.sendPing(), this.pingIntervalMs);
|
|
274
|
+
}
|
|
275
|
+
stopHeartbeat() {
|
|
276
|
+
if (this.pingTimer != null)
|
|
277
|
+
this.clearTimer(this.pingTimer);
|
|
278
|
+
if (this.pongTimer != null)
|
|
279
|
+
this.clearTimer(this.pongTimer);
|
|
280
|
+
this.pingTimer = this.pongTimer = null;
|
|
281
|
+
this.awaitingPong = false;
|
|
282
|
+
}
|
|
283
|
+
// -- messaging -------------------------------------------------------------
|
|
284
|
+
sendSubscribe(record) {
|
|
285
|
+
const socket = this.ws;
|
|
286
|
+
const generation = ++record.generation;
|
|
287
|
+
const key = subKey(record.channel, record.params);
|
|
288
|
+
const active = () => this.ws === socket &&
|
|
289
|
+
this.state === "open" &&
|
|
290
|
+
this.subs.get(key) === record &&
|
|
291
|
+
record.generation === generation;
|
|
292
|
+
const send = (auth = {}) => {
|
|
293
|
+
if (!active())
|
|
294
|
+
return;
|
|
295
|
+
record.lastId = this.nextId++;
|
|
296
|
+
this.send({
|
|
297
|
+
method: "subscribe",
|
|
298
|
+
id: record.lastId,
|
|
299
|
+
params: {
|
|
300
|
+
...record.params,
|
|
301
|
+
...auth,
|
|
302
|
+
channel: record.channel,
|
|
303
|
+
...(record.channel === "accountEvents"
|
|
304
|
+
? { owner: record.params.owner }
|
|
305
|
+
: {}),
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
const provider = record.channel === "accountEvents"
|
|
310
|
+
? (record.auth ?? this.accountAuth)
|
|
311
|
+
: undefined;
|
|
312
|
+
if (!provider) {
|
|
313
|
+
send();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
// Never send unsigned account params while asynchronous auth is pending.
|
|
317
|
+
void Promise.resolve()
|
|
318
|
+
.then(() => provider(String(record.params.owner)))
|
|
319
|
+
.then(send)
|
|
320
|
+
.catch((err) => {
|
|
321
|
+
if (!active())
|
|
322
|
+
return;
|
|
323
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
sendUnsubscribe(record) {
|
|
327
|
+
this.send({
|
|
328
|
+
method: "unsubscribe",
|
|
329
|
+
id: this.nextId++,
|
|
330
|
+
params: { channel: record.channel, ...record.params },
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
send(msg) {
|
|
334
|
+
if (!this.ws)
|
|
335
|
+
return;
|
|
336
|
+
try {
|
|
337
|
+
this.ws.send(JSON.stringify(msg));
|
|
338
|
+
}
|
|
339
|
+
catch (err) {
|
|
340
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
handleMessage(text) {
|
|
344
|
+
let msg;
|
|
345
|
+
try {
|
|
346
|
+
msg = JSON.parse(text);
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
this.onError?.(new Error(`feed: unparseable frame: ${text.slice(0, 120)}`));
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (!msg || typeof msg !== "object" || Array.isArray(msg))
|
|
353
|
+
return;
|
|
354
|
+
const type = msg.type;
|
|
355
|
+
if (type === "pong") {
|
|
356
|
+
this.awaitingPong = false;
|
|
357
|
+
if (this.pingTimer != null)
|
|
358
|
+
this.clearTimer(this.pingTimer);
|
|
359
|
+
if (this.pingIntervalMs > 0)
|
|
360
|
+
this.pingTimer = this.setTimer(() => this.sendPing(), this.pingIntervalMs);
|
|
361
|
+
if (this.pongTimer != null) {
|
|
362
|
+
this.clearTimer(this.pongTimer);
|
|
363
|
+
this.pongTimer = null;
|
|
364
|
+
}
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (type === "error") {
|
|
368
|
+
this.dispatchError(msg);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (type && CONTROL_TYPES.has(type)) {
|
|
372
|
+
// "subscribed" / "unsubscribed" acks — no listener action needed.
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// Data frame: route by channel + key to the owning subscription.
|
|
376
|
+
const channel = msg.channel;
|
|
377
|
+
if (!channel)
|
|
378
|
+
return;
|
|
379
|
+
const key = subKey(channel, msg);
|
|
380
|
+
const record = this.subs.get(key);
|
|
381
|
+
if (!record)
|
|
382
|
+
return;
|
|
383
|
+
for (const listener of record.listeners) {
|
|
384
|
+
try {
|
|
385
|
+
listener(msg);
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
dispatchError(frame) {
|
|
393
|
+
// Route to the subscription whose last subscribe carried this id, so a
|
|
394
|
+
// rejected channel surfaces against the right caller.
|
|
395
|
+
if (typeof frame.id === "number") {
|
|
396
|
+
for (const record of this.subs.values()) {
|
|
397
|
+
if (record.lastId === frame.id) {
|
|
398
|
+
// Deliver as a frame the subscriber can pattern-match on `type`.
|
|
399
|
+
for (const listener of record.listeners) {
|
|
400
|
+
try {
|
|
401
|
+
listener(frame);
|
|
402
|
+
}
|
|
403
|
+
catch (err) {
|
|
404
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
this.onError?.(frame);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
this.onError?.(frame);
|
|
413
|
+
}
|
|
414
|
+
setState(state) {
|
|
415
|
+
if (this.state === state)
|
|
416
|
+
return;
|
|
417
|
+
this.state = state;
|
|
418
|
+
this.onStateChange?.(state);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
// ---------------------------------------------------------------------------
|
|
422
|
+
// Helpers
|
|
423
|
+
// ---------------------------------------------------------------------------
|
|
424
|
+
/** Subscription identity. Orderbook/trades key on market; accountEvents on
|
|
425
|
+
* owner. Derives the same key from an outgoing subscribe and an incoming
|
|
426
|
+
* data frame so routing is symmetric. */
|
|
427
|
+
function subKey(channel, src) {
|
|
428
|
+
if (channel === "accountEvents") {
|
|
429
|
+
return `accountEvents:${String(src.owner ?? "")
|
|
430
|
+
.replace(/^0x/i, "")
|
|
431
|
+
.toLowerCase()}`;
|
|
432
|
+
}
|
|
433
|
+
return `${channel}:${src.market ?? ""}`;
|
|
434
|
+
}
|
|
435
|
+
function defaultFactory(Impl) {
|
|
436
|
+
return (url, options) => {
|
|
437
|
+
const Ctor = Impl ??
|
|
438
|
+
globalThis
|
|
439
|
+
.WebSocket;
|
|
440
|
+
if (!Ctor) {
|
|
441
|
+
throw new Error("GatewayFeed: no WebSocket implementation. Pass WebSocketImpl (e.g. the 'ws' package) when running outside a browser.");
|
|
442
|
+
}
|
|
443
|
+
// Only forward options (headers) to an injected impl; the global browser
|
|
444
|
+
// WebSocket treats a second arg as subprotocols and would mishandle it.
|
|
445
|
+
if (Impl && options)
|
|
446
|
+
return new Impl(url, options);
|
|
447
|
+
return new Ctor(url);
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
/** Coerce a WebSocket message payload to text. Browser delivers a string;
|
|
451
|
+
* `ws` may deliver a Buffer/ArrayBuffer for the same text frame. */
|
|
452
|
+
function extractText(data) {
|
|
453
|
+
if (typeof data === "string")
|
|
454
|
+
return data;
|
|
455
|
+
if (data instanceof ArrayBuffer)
|
|
456
|
+
return new TextDecoder().decode(data);
|
|
457
|
+
if (ArrayBuffer.isView(data)) {
|
|
458
|
+
return new TextDecoder().decode(data);
|
|
459
|
+
}
|
|
460
|
+
if (data &&
|
|
461
|
+
typeof data.toString === "function") {
|
|
462
|
+
return data.toString();
|
|
463
|
+
}
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
function toError(ev) {
|
|
467
|
+
if (ev instanceof Error)
|
|
468
|
+
return ev;
|
|
469
|
+
const message = ev && typeof ev === "object" && "message" in ev
|
|
470
|
+
? String(ev.message)
|
|
471
|
+
: "feed socket error";
|
|
472
|
+
return new Error(message);
|
|
473
|
+
}
|
|
474
|
+
//# sourceMappingURL=feed.js.map
|
package/dist/feed.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feed.js","sourceRoot":"","sources":["../src/feed.ts"],"names":[],"mappings":"AAyFA,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,WAAW;IACL,WAAW,CAAuB;IAClC,GAAG,CAAS;IACZ,OAAO,CAA0B;IACjC,cAAc,CAAS;IACvB,aAAa,CAAS;IACtB,YAAY,CAAS;IACrB,YAAY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AU,EACpE,CAAC;QACD,OAAQ,IAAmC,CAAC,QAAQ,EAAE,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,OAAO,CAAC,EAAW;IAC1B,IAAI,EAAE,YAAY,KAAK;QAAE,OAAO,EAAE,CAAC;IACnC,MAAM,OAAO,GACX,EAAE,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,SAAS,IAAI,EAAE;QAC7C,CAAC,CAAC,MAAM,CAAE,EAA2B,CAAC,OAAO,CAAC;QAC9C,CAAC,CAAC,mBAAmB,CAAC;IAC1B,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type RawAccountPosition } from "./account-state.js";
|
|
2
|
+
/** Bounded selection for one finalized-store read; not historical or risk authorization. */
|
|
3
|
+
export interface FinancialStateSelection {
|
|
4
|
+
markets: number[];
|
|
5
|
+
owners: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface FinancialAccountState {
|
|
8
|
+
owner: string;
|
|
9
|
+
balance: bigint | null;
|
|
10
|
+
/** Lifetime fee/rebate counter, NOT a cash balance to add to conservation sums. */
|
|
11
|
+
feesAccrued: bigint | null;
|
|
12
|
+
feeOverride: {
|
|
13
|
+
takerBps: number;
|
|
14
|
+
makerBps: number;
|
|
15
|
+
} | null;
|
|
16
|
+
rollingVolume: {
|
|
17
|
+
lastUpdateMs: bigint;
|
|
18
|
+
volume: bigint;
|
|
19
|
+
} | null;
|
|
20
|
+
positions: RawAccountPosition[];
|
|
21
|
+
}
|
|
22
|
+
export interface FinancialMarketState {
|
|
23
|
+
market: number;
|
|
24
|
+
pool: number;
|
|
25
|
+
szDecimals: number;
|
|
26
|
+
makerFeeBps: number;
|
|
27
|
+
takerFeeBps: number;
|
|
28
|
+
feeTiers: {
|
|
29
|
+
min30dVolumeMicroUsdc: bigint;
|
|
30
|
+
makerFeeTenthBps: number;
|
|
31
|
+
takerFeeTenthBps: number;
|
|
32
|
+
}[];
|
|
33
|
+
fundingIntervalMs: bigint;
|
|
34
|
+
maxFundingRateBps: number;
|
|
35
|
+
cumulativeFunding: bigint | null;
|
|
36
|
+
fundingRateBps: bigint | null;
|
|
37
|
+
lastFundingTimeMs: bigint | null;
|
|
38
|
+
markEwma: bigint | null;
|
|
39
|
+
}
|
|
40
|
+
/** Raw stored amounts, including absence. No equity, solvency or withdrawal verdict. */
|
|
41
|
+
export interface FinancialState {
|
|
42
|
+
format: 1;
|
|
43
|
+
finalizedHeight: bigint;
|
|
44
|
+
finalizedTimeMs: bigint;
|
|
45
|
+
accounts: FinancialAccountState[];
|
|
46
|
+
markets: FinancialMarketState[];
|
|
47
|
+
feePool: bigint | null;
|
|
48
|
+
insurancePools: {
|
|
49
|
+
pool: number;
|
|
50
|
+
balance: bigint | null;
|
|
51
|
+
}[];
|
|
52
|
+
plp: {
|
|
53
|
+
owner: string;
|
|
54
|
+
/** Stored bootstrap baseline, NOT additional cash to add to the PLP account balance. */
|
|
55
|
+
bootstrapBalance: bigint;
|
|
56
|
+
minBalanceFloor: bigint;
|
|
57
|
+
enabled: boolean;
|
|
58
|
+
} | null;
|
|
59
|
+
}
|
|
60
|
+
/** Copies and canonicalizes values; duplicate selectors remain errors. */
|
|
61
|
+
export declare function canonicalFinancialSelection(selection: FinancialStateSelection): FinancialStateSelection;
|
|
62
|
+
export declare function decodeFinancialState(raw: unknown, selection: FinancialStateSelection): FinancialState;
|
|
63
|
+
/** Internal public-read transport: no direct-node fallback, retry or valuation defaults. */
|
|
64
|
+
export declare function fetchFinancialState(gatewayUrl: string, selection: FinancialStateSelection): Promise<FinancialState>;
|
|
65
|
+
//# sourceMappingURL=financial-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"financial-state.d.ts","sourceRoot":"","sources":["../src/financial-state.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,oBAAo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