@pod-network/trade-sdk 0.9.0 → 0.11.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 +6 -4
- package/dist/client.d.ts +32 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +50 -5
- package/dist/client.js.map +1 -1
- package/dist/codec/activity.d.ts +11 -0
- package/dist/codec/activity.d.ts.map +1 -0
- package/dist/codec/activity.js +33 -0
- package/dist/codec/activity.js.map +1 -0
- package/dist/codec/decode.d.ts +14 -6
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +58 -24
- package/dist/codec/decode.js.map +1 -1
- package/dist/codec/orders-v2.d.ts +9 -3
- package/dist/codec/orders-v2.d.ts.map +1 -1
- package/dist/codec/orders-v2.js +12 -9
- package/dist/codec/orders-v2.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/sync/activity.d.ts +58 -0
- package/dist/sync/activity.d.ts.map +1 -0
- package/dist/sync/activity.js +259 -0
- package/dist/sync/activity.js.map +1 -0
- package/dist/sync/order-preview.d.ts +3 -0
- package/dist/sync/order-preview.d.ts.map +1 -1
- package/dist/sync/order-preview.js +56 -0
- package/dist/sync/order-preview.js.map +1 -1
- package/dist/sync/orders.d.ts +4 -64
- package/dist/sync/orders.d.ts.map +1 -1
- package/dist/sync/orders.js +40 -190
- package/dist/sync/orders.js.map +1 -1
- package/dist/sync/pnl-history.d.ts +140 -0
- package/dist/sync/pnl-history.d.ts.map +1 -0
- package/dist/sync/pnl-history.js +617 -0
- package/dist/sync/pnl-history.js.map +1 -0
- package/dist/sync/stream.d.ts +101 -0
- package/dist/sync/stream.d.ts.map +1 -0
- package/dist/sync/stream.js +243 -0
- package/dist/sync/stream.js.map +1 -0
- package/dist/transport/rest.d.ts +36 -4
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/transport/rest.js +43 -10
- package/dist/transport/rest.js.map +1 -1
- package/dist/transport/ws.d.ts +5 -1
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +62 -23
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +98 -16
- package/dist/types/wire.d.ts.map +1 -1
- package/dist/write/index.d.ts +3 -3
- package/dist/write/index.d.ts.map +1 -1
- package/dist/write/index.js +0 -3
- package/dist/write/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +54 -7
- package/src/codec/activity.ts +46 -0
- package/src/codec/decode.ts +65 -30
- package/src/codec/orders-v2.ts +24 -11
- package/src/index.ts +7 -1
- package/src/sync/activity.ts +266 -0
- package/src/sync/order-preview.ts +63 -0
- package/src/sync/orders.ts +41 -187
- package/src/sync/pnl-history.ts +639 -0
- package/src/sync/stream.ts +270 -0
- package/src/transport/rest.ts +62 -14
- package/src/transport/ws.ts +5 -1
- package/src/types/public.ts +62 -26
- package/src/types/wire.ts +105 -18
- package/src/write/index.ts +3 -6
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
// PnL change over a past window, folded backwards from the live snapshot.
|
|
2
|
+
//
|
|
3
|
+
// Total PnL is continuous through banking (realized ⇄ unrealized), so the
|
|
4
|
+
// change between a past point t and the reference tick needs no leg
|
|
5
|
+
// boundaries and no banked amounts:
|
|
6
|
+
// D(t) = Σ value_m(t) − Σ value_m(ref) − Σ_{events in (t, ref]} (F_b − p_b)·Δs_b
|
|
7
|
+
// value_m = (mark − F)·size spot: F = 0, mark = last clearing
|
|
8
|
+
// A spot withdrawal removes proportional cost without realizing, i.e. it is a
|
|
9
|
+
// sell at the holding's average cost.
|
|
10
|
+
//
|
|
11
|
+
// Fills page newest-first, so the walk streams: the newest points only need
|
|
12
|
+
// the newest pages, and each chunk is yielded as soon as its fills are in.
|
|
13
|
+
import { div, mul } from "../codec/fixed.js";
|
|
14
|
+
import { dec } from "../codec/units.js";
|
|
15
|
+
import { rpc } from "../transport/jsonrpc.js";
|
|
16
|
+
import { PodHttpError } from "../transport/rest.js";
|
|
17
|
+
const RESIDUAL_TOLERANCE = 10n ** 12n;
|
|
18
|
+
// Within a tick the engine runs sweeps, then deposits, then matching, then
|
|
19
|
+
// transfers and withdrawals; walking backwards undoes them in the opposite
|
|
20
|
+
// order. Cash never touches a size, so its place only has to be consistent.
|
|
21
|
+
const RANK = { withdraw: 0, cash: 1, fill: 2, deposit: 3, sweep: 4 };
|
|
22
|
+
const newestFirst = (a, b) => b.timeUs - a.timeUs || RANK[a.kind] - RANK[b.kind];
|
|
23
|
+
/** The backward walk. Feed events newest-first, ask for points newest-first;
|
|
24
|
+
* every event after a point must have been fed before the point is asked. */
|
|
25
|
+
export class PnlWalker {
|
|
26
|
+
tickAt;
|
|
27
|
+
refTimeUs;
|
|
28
|
+
accountValueRef;
|
|
29
|
+
warnings = [];
|
|
30
|
+
state = new Map();
|
|
31
|
+
pending = [];
|
|
32
|
+
acc = 0n;
|
|
33
|
+
flows = 0n;
|
|
34
|
+
refValue;
|
|
35
|
+
constructor(legs, tickAt, refTimeUs, accountValueRef) {
|
|
36
|
+
this.tickAt = tickAt;
|
|
37
|
+
this.refTimeUs = refTimeUs;
|
|
38
|
+
this.accountValueRef = accountValueRef;
|
|
39
|
+
for (const l of legs)
|
|
40
|
+
this.state.set(l.market, { kind: l.kind, size: l.size, basis: l.basis ?? 0n, basisKnown: l.basis !== undefined });
|
|
41
|
+
}
|
|
42
|
+
/** A market first seen in an old fill: flat at the reference. */
|
|
43
|
+
touch(market, kind) {
|
|
44
|
+
if (!this.state.has(market))
|
|
45
|
+
this.state.set(market, { kind, size: 0n, basis: 0n, basisKnown: true });
|
|
46
|
+
}
|
|
47
|
+
feed(events) {
|
|
48
|
+
this.pending = this.pending.concat(events).sort(newestFirst);
|
|
49
|
+
}
|
|
50
|
+
point(t) {
|
|
51
|
+
this.refValue ??= this.value(this.refTimeUs);
|
|
52
|
+
for (let e = this.pending[0]; e && e.timeUs > t; e = this.pending[0]) {
|
|
53
|
+
this.pending.shift();
|
|
54
|
+
const u = this.unapply(e);
|
|
55
|
+
this.acc += u.pnl;
|
|
56
|
+
this.flows += u.flow;
|
|
57
|
+
}
|
|
58
|
+
const pnl = this.value(t) - this.refValue - this.acc;
|
|
59
|
+
const point = { time: t / 1000, pnl };
|
|
60
|
+
// Account value is PnL plus every movement that is not PnL.
|
|
61
|
+
if (this.accountValueRef !== undefined)
|
|
62
|
+
point.accountValue = this.accountValueRef + pnl - this.flows;
|
|
63
|
+
return point;
|
|
64
|
+
}
|
|
65
|
+
value(t) {
|
|
66
|
+
let sum = 0n;
|
|
67
|
+
for (const [m, st] of this.state) {
|
|
68
|
+
if (st.size === 0n)
|
|
69
|
+
continue;
|
|
70
|
+
const tick = this.tickAt(m, t);
|
|
71
|
+
if (!tick) {
|
|
72
|
+
this.warnings.push(`${m}: no tick at or before ${t}`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
sum += mul(tick.mark - tick.funding, st.size);
|
|
76
|
+
}
|
|
77
|
+
return sum;
|
|
78
|
+
}
|
|
79
|
+
/** The PnL adjustment and the non-PnL money value carried by the event. */
|
|
80
|
+
unapply(e) {
|
|
81
|
+
if (e.kind === "cash" || e.market === undefined)
|
|
82
|
+
return { pnl: 0n, flow: e.deltaSize };
|
|
83
|
+
const st = this.state.get(e.market);
|
|
84
|
+
if (!st)
|
|
85
|
+
throw new Error(`pnlHistory: event on unknown market ${e.market}`);
|
|
86
|
+
const tick = this.tickAt(e.market, e.timeUs);
|
|
87
|
+
const F = tick?.funding ?? 0n;
|
|
88
|
+
const sizeBefore = st.size - e.deltaSize;
|
|
89
|
+
let price = e.price;
|
|
90
|
+
let flow = 0n;
|
|
91
|
+
if (st.kind === "spot") {
|
|
92
|
+
const average = st.size !== 0n && st.basisKnown ? div(st.basis, st.size) : undefined;
|
|
93
|
+
if (e.kind === "withdraw") {
|
|
94
|
+
if (average === undefined) {
|
|
95
|
+
this.warnings.push(`${e.market}: withdrawal at ${e.timeUs} with unknown cost basis, treated as neutral`);
|
|
96
|
+
price = tick?.mark ?? 0n;
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
price = average;
|
|
100
|
+
}
|
|
101
|
+
if (e.kind === "deposit")
|
|
102
|
+
price = tick?.mark ?? 0n;
|
|
103
|
+
// Tokens leave at their cost and arrive at the clearing price; either
|
|
104
|
+
// way the value that moved is what the event is priced at.
|
|
105
|
+
if (e.kind === "withdraw" || e.kind === "deposit")
|
|
106
|
+
flow = mul(price, e.deltaSize);
|
|
107
|
+
if (sizeBefore === 0n) {
|
|
108
|
+
st.basis = 0n;
|
|
109
|
+
st.basisKnown = true;
|
|
110
|
+
}
|
|
111
|
+
else if (e.deltaSize > 0n)
|
|
112
|
+
st.basis -= mul(price, e.deltaSize);
|
|
113
|
+
else if (average !== undefined)
|
|
114
|
+
st.basis = mul(average, sizeBefore);
|
|
115
|
+
else
|
|
116
|
+
st.basisKnown = false;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const crossed = st.size !== 0n && sizeBefore !== 0n && (st.size > 0n) !== (sizeBefore > 0n);
|
|
120
|
+
if (st.size === 0n || crossed)
|
|
121
|
+
st.basisKnown = false;
|
|
122
|
+
else if (st.basisKnown)
|
|
123
|
+
st.basis -= mul(F - price, e.deltaSize);
|
|
124
|
+
if (sizeBefore === 0n && st.basisKnown) {
|
|
125
|
+
const r = st.basis < 0n ? -st.basis : st.basis;
|
|
126
|
+
if (r > RESIDUAL_TOLERANCE)
|
|
127
|
+
this.warnings.push(`${e.market}: basis residual ${st.basis} at flat ${e.timeUs}`);
|
|
128
|
+
st.basis = 0n;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
st.size = sizeBefore;
|
|
132
|
+
return { pnl: mul(F - price, e.deltaSize), flow };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export function foldPnlHistory(input) {
|
|
136
|
+
const walker = new PnlWalker(input.legs, input.tickAt, input.refTimeUs, input.accountValueRef);
|
|
137
|
+
walker.feed(input.events);
|
|
138
|
+
const points = [...input.gridUs].reverse().map((t) => walker.point(t)).reverse();
|
|
139
|
+
const base = points[0]?.pnl ?? 0n;
|
|
140
|
+
return { points: points.map((p) => ({ ...p, pnl: p.pnl - base })), warnings: walker.warnings };
|
|
141
|
+
}
|
|
142
|
+
/** The REST reads, with the `ob_*` JSON-RPC twins behind them for nodes that
|
|
143
|
+
* predate the routes. ponytail: drop the fallback once every network serves
|
|
144
|
+
* /clob/solutions and /clob/fills. */
|
|
145
|
+
function reads(rest, rpcUrl, opts) {
|
|
146
|
+
const missing = (e) => e instanceof PodHttpError && e.status === 404;
|
|
147
|
+
return {
|
|
148
|
+
async fills(account, q) {
|
|
149
|
+
try {
|
|
150
|
+
return await rest.fills(account, q);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
if (!missing(e))
|
|
154
|
+
throw e;
|
|
155
|
+
return (await rpc(rpcUrl, "ob_getFills", [account, { from_ts: q.fromUs, to_ts: q.toUs, limit: q.limit }], opts)).fills;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
async solutions(q) {
|
|
159
|
+
try {
|
|
160
|
+
return await rest.solutions(q);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
if (!missing(e))
|
|
164
|
+
throw e;
|
|
165
|
+
// Older nodes count rows, not ticks: ask for enough rows to cover every market of one tick.
|
|
166
|
+
return (await rpc(rpcUrl, "ob_getSolutions", [{ orderbook_id: q.orderbook, since: q.sinceUs, until: q.untilUs, limit: q.orderbook ? q.limit : 200 }], opts)).solutions;
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/** Run `fn` over `items` with at most `n` in flight, keeping order. */
|
|
172
|
+
async function mapLimit(items, n, fn) {
|
|
173
|
+
const out = new Array(items.length);
|
|
174
|
+
let next = 0;
|
|
175
|
+
const worker = async () => {
|
|
176
|
+
for (let i = next++; i < items.length; i = next++)
|
|
177
|
+
out[i] = await fn(items[i]);
|
|
178
|
+
};
|
|
179
|
+
await Promise.all(Array.from({ length: Math.min(n, items.length) }, worker));
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
/** Immutable inputs shared by every window size and every refresh: fills as
|
|
183
|
+
* events keyed by the time ranges they cover, and ticks keyed by market and
|
|
184
|
+
* time. The fold is recomputed per request; only uncovered ranges and unseen
|
|
185
|
+
* ticks are fetched. */
|
|
186
|
+
export class PnlHistoryCache {
|
|
187
|
+
maxEvents;
|
|
188
|
+
ticks = new Map();
|
|
189
|
+
/** Times at which every market's row was fetched at once. */
|
|
190
|
+
tickTimes = new Set();
|
|
191
|
+
/** Per market, the latest time known to have no row at or before it. A
|
|
192
|
+
* market has no rows before its first tick, so one empty answer rules out
|
|
193
|
+
* every earlier point without a call. */
|
|
194
|
+
noRowBefore = new Map();
|
|
195
|
+
/** Set when a range was cached from a node without the activity feed; the
|
|
196
|
+
* next run that does get flows drops the cache so those ranges are refetched. */
|
|
197
|
+
flowsMissing = false;
|
|
198
|
+
events = [];
|
|
199
|
+
coverage = [];
|
|
200
|
+
/** About 150 bytes per event and per tick, so the default cap is on the
|
|
201
|
+
* order of 50 MB with its ticks. */
|
|
202
|
+
constructor(maxEvents = 250_000) {
|
|
203
|
+
this.maxEvents = maxEvents;
|
|
204
|
+
}
|
|
205
|
+
clear() {
|
|
206
|
+
this.ticks.clear();
|
|
207
|
+
this.tickTimes.clear();
|
|
208
|
+
this.noRowBefore.clear();
|
|
209
|
+
this.flowsMissing = false;
|
|
210
|
+
this.events = [];
|
|
211
|
+
this.coverage = [];
|
|
212
|
+
}
|
|
213
|
+
/** Whether a tick for the market at the time can exist at all. */
|
|
214
|
+
mayHaveTick(market, timeUs) {
|
|
215
|
+
return timeUs > (this.noRowBefore.get(market) ?? -1);
|
|
216
|
+
}
|
|
217
|
+
noteEmpty(market, timeUs) {
|
|
218
|
+
if (timeUs > (this.noRowBefore.get(market) ?? -1))
|
|
219
|
+
this.noRowBefore.set(market, timeUs);
|
|
220
|
+
}
|
|
221
|
+
/** Sub-ranges of [from, to) not yet covered. */
|
|
222
|
+
missing(from, to) {
|
|
223
|
+
const out = [];
|
|
224
|
+
let cursor = from;
|
|
225
|
+
for (const c of this.coverage) {
|
|
226
|
+
if (c.to <= cursor)
|
|
227
|
+
continue;
|
|
228
|
+
if (c.from >= to)
|
|
229
|
+
break;
|
|
230
|
+
if (c.from > cursor)
|
|
231
|
+
out.push({ from: cursor, to: c.from });
|
|
232
|
+
cursor = Math.max(cursor, c.to);
|
|
233
|
+
}
|
|
234
|
+
if (cursor < to)
|
|
235
|
+
out.push({ from: cursor, to });
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
add(from, to, events) {
|
|
239
|
+
this.events = this.events.concat(events).sort(newestFirst);
|
|
240
|
+
this.coverage.push({ from, to });
|
|
241
|
+
this.coverage.sort((a, b) => a.from - b.from);
|
|
242
|
+
const merged = [];
|
|
243
|
+
for (const c of this.coverage) {
|
|
244
|
+
const last = merged[merged.length - 1];
|
|
245
|
+
if (last && c.from <= last.to)
|
|
246
|
+
last.to = Math.max(last.to, c.to);
|
|
247
|
+
else
|
|
248
|
+
merged.push({ ...c });
|
|
249
|
+
}
|
|
250
|
+
this.coverage = merged;
|
|
251
|
+
if (this.events.length > this.maxEvents) {
|
|
252
|
+
// ponytail: evict the oldest events, their ticks, and coverage below them.
|
|
253
|
+
const cut = this.events[this.maxEvents - 1]?.timeUs ?? 0;
|
|
254
|
+
this.events = this.events.filter((e) => e.timeUs >= cut);
|
|
255
|
+
this.coverage = this.coverage.filter((c) => c.to > cut).map((c) => ({ from: Math.max(c.from, cut), to: c.to }));
|
|
256
|
+
for (const k of this.ticks.keys())
|
|
257
|
+
if (Number(k.slice(k.indexOf(":") + 1)) < cut)
|
|
258
|
+
this.ticks.delete(k);
|
|
259
|
+
for (const t of this.tickTimes)
|
|
260
|
+
if (t < cut)
|
|
261
|
+
this.tickTimes.delete(t);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/** Events with from ≤ time < to, newest first. */
|
|
265
|
+
slice(from, to) {
|
|
266
|
+
return this.events.filter((e) => e.timeUs >= from && e.timeUs < to);
|
|
267
|
+
}
|
|
268
|
+
/** Drop a range so it is fetched again. */
|
|
269
|
+
forget(from, to) {
|
|
270
|
+
this.events = this.events.filter((e) => e.timeUs < from || e.timeUs >= to);
|
|
271
|
+
this.coverage = this.coverage.flatMap((c) => {
|
|
272
|
+
if (c.to <= from || c.from >= to)
|
|
273
|
+
return [c];
|
|
274
|
+
const kept = [];
|
|
275
|
+
if (c.from < from)
|
|
276
|
+
kept.push({ from: c.from, to: from });
|
|
277
|
+
if (c.to > to)
|
|
278
|
+
kept.push({ from: to, to: c.to });
|
|
279
|
+
return kept;
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const MAX_POINTS = 500;
|
|
284
|
+
// The first chunks are small so the newest points draw after one window's
|
|
285
|
+
// fetch; later ones grow to keep the call count down.
|
|
286
|
+
const CHUNK_SIZES = [10, 20, 40, 50];
|
|
287
|
+
const IN_FLIGHT = 4;
|
|
288
|
+
const WIDE_ACCOUNT_MARKETS = 10;
|
|
289
|
+
const LOOKUPS_IN_FLIGHT = 24;
|
|
290
|
+
const FILLS_PAGE = 500;
|
|
291
|
+
/** Pages newest-first. A full page may cut a batch in half, so its oldest
|
|
292
|
+
* batch is dropped and refetched whole with the next page; rows are never
|
|
293
|
+
* keyed, since one order can legitimately fill several times in one batch
|
|
294
|
+
* (ADL slices). */
|
|
295
|
+
class FillsCursor {
|
|
296
|
+
reads;
|
|
297
|
+
account;
|
|
298
|
+
fromUs;
|
|
299
|
+
done = false;
|
|
300
|
+
warnings = [];
|
|
301
|
+
to;
|
|
302
|
+
constructor(reads, account, fromUs, toUs) {
|
|
303
|
+
this.reads = reads;
|
|
304
|
+
this.account = account;
|
|
305
|
+
this.fromUs = fromUs;
|
|
306
|
+
this.to = toUs;
|
|
307
|
+
}
|
|
308
|
+
async next() {
|
|
309
|
+
const fills = await this.reads.fills(this.account, { fromUs: this.fromUs, toUs: this.to, limit: FILLS_PAGE });
|
|
310
|
+
if (fills.length < FILLS_PAGE) {
|
|
311
|
+
this.done = true;
|
|
312
|
+
return fills;
|
|
313
|
+
}
|
|
314
|
+
const oldest = Math.min(...fills.map((f) => f.timestamp));
|
|
315
|
+
if (oldest + 1 === this.to) {
|
|
316
|
+
// ponytail: a single batch with more than FILLS_PAGE fills is truncated here.
|
|
317
|
+
this.warnings.push(`batch ${oldest} has more than ${FILLS_PAGE} fills; only the first page is used`);
|
|
318
|
+
this.to = oldest;
|
|
319
|
+
return fills;
|
|
320
|
+
}
|
|
321
|
+
this.to = oldest + 1;
|
|
322
|
+
return fills.filter((f) => f.timestamp !== oldest);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async function drainFills(r, account, fromUs, toUs) {
|
|
326
|
+
const cursor = new FillsCursor(r, account, fromUs, toUs);
|
|
327
|
+
const fills = [];
|
|
328
|
+
while (!cursor.done)
|
|
329
|
+
fills.push(...(await cursor.next()));
|
|
330
|
+
return { fills, warnings: cursor.warnings };
|
|
331
|
+
}
|
|
332
|
+
export async function fetchFills(rest, account, fromUs, toUs, rpcUrl = "", opts = {}) {
|
|
333
|
+
return (await drainFills(reads(rest, rpcUrl, opts), account, fromUs, toUs)).fills;
|
|
334
|
+
}
|
|
335
|
+
const FLOW_TYPES = ["backstop", "bridge_transfer", "transfer"];
|
|
336
|
+
/** Money movements in [fromUs, toUs) from the activity feed, or `undefined`
|
|
337
|
+
* when the node does not serve it. */
|
|
338
|
+
async function drainFlows(rest, account, fromUs, toUs) {
|
|
339
|
+
const out = [];
|
|
340
|
+
let cursor;
|
|
341
|
+
try {
|
|
342
|
+
do {
|
|
343
|
+
const page = await rest.activity(account, { types: [...FLOW_TYPES], from: fromUs / 1000, to: toUs / 1000, limit: 200, cursor });
|
|
344
|
+
for (const e of page.activity)
|
|
345
|
+
if (e.activityType !== "order")
|
|
346
|
+
out.push(e);
|
|
347
|
+
cursor = page.nextCursor ?? undefined;
|
|
348
|
+
} while (cursor);
|
|
349
|
+
}
|
|
350
|
+
catch (e) {
|
|
351
|
+
// Only a missing route means the node has no feed. Anything else (a 503,
|
|
352
|
+
// a timeout) must fail the run, or the range would be cached without
|
|
353
|
+
// its flows and every later account value would be wrong and silent.
|
|
354
|
+
if (e instanceof PodHttpError && e.status === 404)
|
|
355
|
+
return undefined;
|
|
356
|
+
throw e;
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
export async function* streamPnlHistory(deps, account, q) {
|
|
361
|
+
const rpcOpts = { fetch: deps.fetch };
|
|
362
|
+
const io = reads(deps.rest, deps.rpcUrl, rpcOpts);
|
|
363
|
+
// The snapshot is served from the live engine, fills and ticks from the
|
|
364
|
+
// indexer. Pair them: the engine's last executed batch is read together with
|
|
365
|
+
// the snapshot, one request sent just before it and one just after, and the
|
|
366
|
+
// read is retried when the two disagree. Sequential reads would be exact but
|
|
367
|
+
// straddle a 500 ms tick on every attempt from far away; concurrent ones
|
|
368
|
+
// land within a few ms of server time, so a boundary inside that gap is rare
|
|
369
|
+
// and the next refresh heals it.
|
|
370
|
+
const engineTick = async () => {
|
|
371
|
+
const st = await rpc(deps.rpcUrl, "pod_getSolverState", [], rpcOpts);
|
|
372
|
+
return Number(st.last_executed_batch?.deadline ?? 0);
|
|
373
|
+
};
|
|
374
|
+
const readSnapshot = async () => {
|
|
375
|
+
for (let attempt = 0;; attempt++) {
|
|
376
|
+
const [before, snap, balances, after] = await Promise.all([engineTick(), deps.rest.positions(account), deps.rest.balances(account), engineTick()]);
|
|
377
|
+
if (before === after || attempt >= 4)
|
|
378
|
+
return { nowUs: before, snap, balances };
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const [markets, snapshot, backstop, withdrawals] = await Promise.all([
|
|
382
|
+
deps.rest.markets(),
|
|
383
|
+
readSnapshot(),
|
|
384
|
+
deps.rest.backstopTransfers(account),
|
|
385
|
+
deps.rest.bridgeWithdrawals(account, { since: q.from * 1000 - 1 }),
|
|
386
|
+
]);
|
|
387
|
+
const { snap, balances } = snapshot;
|
|
388
|
+
let nowUs = snapshot.nowUs;
|
|
389
|
+
const batchUs = Math.max(500, ...markets.map((m) => m.auctionIntervalMs)) * 1000;
|
|
390
|
+
// Grid points sit on batch ticks so one range call per point serves every market.
|
|
391
|
+
const fromUs = Math.ceil((q.from * 1000) / batchUs) * batchUs;
|
|
392
|
+
if (nowUs === 0)
|
|
393
|
+
nowUs = (await deps.rest.status()).solutionNow * 1000;
|
|
394
|
+
// The indexer is normally at the engine's tick already, so the first windows
|
|
395
|
+
// are fetched while this checks; if it was behind, the newest window is
|
|
396
|
+
// fetched again once it has caught up.
|
|
397
|
+
const indexerLagged = (async () => {
|
|
398
|
+
let lagged = false;
|
|
399
|
+
for (let i = 0; i < 30 && (await deps.rest.status()).solutionNow * 1000 < nowUs; i++) {
|
|
400
|
+
lagged = true;
|
|
401
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
402
|
+
}
|
|
403
|
+
return lagged;
|
|
404
|
+
})();
|
|
405
|
+
const toUs = Math.min(q.to === undefined ? nowUs : Math.floor((q.to * 1000) / batchUs) * batchUs, nowUs);
|
|
406
|
+
if (fromUs >= toUs)
|
|
407
|
+
throw new Error("pnlHistory: from must be before to");
|
|
408
|
+
const points = Math.min(Math.max(2, Math.floor(q.points ?? MAX_POINTS)), MAX_POINTS);
|
|
409
|
+
const stepUs = Math.ceil((toUs - fromUs) / (points - 1) / batchUs) * batchUs;
|
|
410
|
+
// The series starts at `from` and ends at `to`; the points between sit on
|
|
411
|
+
// epoch multiples of the step, so a refreshed window shares them, and their
|
|
412
|
+
// ticks, with the previous one.
|
|
413
|
+
const gridUs = [fromUs];
|
|
414
|
+
for (let t = (Math.floor(fromUs / stepUs) + 1) * stepUs; t < toUs; t += stepUs)
|
|
415
|
+
gridUs.push(t);
|
|
416
|
+
gridUs.push(toUs);
|
|
417
|
+
const byId = new Map(markets.map((m) => [m.id, m]));
|
|
418
|
+
const spotByToken = new Map(markets.filter((m) => m.type === "spot").map((m) => [m.base.address, m]));
|
|
419
|
+
const kindOf = (id) => (byId.get(id)?.type === "spot" ? "spot" : "perp");
|
|
420
|
+
const legs = [];
|
|
421
|
+
for (const p of snap.positions) {
|
|
422
|
+
if (p.kind !== "perp")
|
|
423
|
+
continue;
|
|
424
|
+
const basis = p.fundingBasis !== undefined && p.costBasis !== undefined ? p.fundingBasis - p.costBasis : undefined;
|
|
425
|
+
legs.push({ market: p.orderbookId, kind: "perp", size: p.size, basis });
|
|
426
|
+
}
|
|
427
|
+
for (const h of balances.holdings)
|
|
428
|
+
legs.push({ market: h.orderbookId, kind: "spot", size: h.balance, basis: h.costBasis });
|
|
429
|
+
const cache = deps.cache ?? new PnlHistoryCache();
|
|
430
|
+
const ticks = cache.ticks;
|
|
431
|
+
const key = (m, t) => `${m}:${t}`;
|
|
432
|
+
const walker = new PnlWalker(legs, (m, t) => ticks.get(key(m, t)), nowUs, snap.accountValue);
|
|
433
|
+
const known = new Set(legs.map((l) => l.market));
|
|
434
|
+
const use = (id) => { if (!known.has(id)) {
|
|
435
|
+
known.add(id);
|
|
436
|
+
walker.touch(id, kindOf(id));
|
|
437
|
+
} };
|
|
438
|
+
const store = (row, t) => {
|
|
439
|
+
const id = row.orderbook_id;
|
|
440
|
+
const perp = kindOf(id) === "perp";
|
|
441
|
+
const window = byId.get(id)?.fundingWindowUs ?? 0;
|
|
442
|
+
ticks.set(key(id, t), {
|
|
443
|
+
mark: perp ? dec(row.mark_price) : dec(row.clearing_price),
|
|
444
|
+
funding: perp && row.funding_index !== undefined && window > 0 ? dec(row.funding_index) / BigInt(window) : 0n,
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
// A "newest row at or before t" answer is only right once the indexer has
|
|
448
|
+
// reached t, so no tick lookup goes out before the watermark check resolves.
|
|
449
|
+
// Fills do not wait: the lag retry below fetches the newest window again.
|
|
450
|
+
const solutions = async (queries) => {
|
|
451
|
+
await indexerLagged;
|
|
452
|
+
return mapLimit(queries, LOOKUPS_IN_FLIGHT, (q) => io.solutions(q));
|
|
453
|
+
};
|
|
454
|
+
/** All markets' rows at each tick, one call per tick; skipped before every
|
|
455
|
+
* known market's first tick. */
|
|
456
|
+
const fetchAllAt = async (times) => {
|
|
457
|
+
const wanted = times.filter((t) => !cache.tickTimes.has(t) && [...known].some((m) => cache.mayHaveTick(m, t)));
|
|
458
|
+
const rows = await solutions(wanted.map((t) => ({ sinceUs: t, untilUs: t + 1, limit: 1 })));
|
|
459
|
+
rows.forEach((rs, i) => { const t = wanted[i] ?? 0; cache.tickTimes.add(t); for (const r of rs)
|
|
460
|
+
store(r, t); });
|
|
461
|
+
};
|
|
462
|
+
/** Ticks for every known market at the grid points. One call per tick when
|
|
463
|
+
* the account spans many markets, else one lookup per market: the wide call
|
|
464
|
+
* returns every market's row whether needed or not. */
|
|
465
|
+
const fetchTicks = async (times) => {
|
|
466
|
+
if (known.size >= WIDE_ACCOUNT_MARKETS)
|
|
467
|
+
await fetchAllAt(times);
|
|
468
|
+
else
|
|
469
|
+
await fetchPairs(times.flatMap((t) => [...known].map((market) => ({ market, timeUs: t }))));
|
|
470
|
+
};
|
|
471
|
+
/** Newest row at or before the time, per pair; the fallback and the per-fill path. */
|
|
472
|
+
const fetchPairs = async (pairs) => {
|
|
473
|
+
const seen = new Set();
|
|
474
|
+
const wanted = pairs.filter((n) => {
|
|
475
|
+
const k = key(n.market, n.timeUs);
|
|
476
|
+
if (ticks.has(k) || seen.has(k) || !cache.mayHaveTick(n.market, n.timeUs))
|
|
477
|
+
return false;
|
|
478
|
+
seen.add(k);
|
|
479
|
+
return true;
|
|
480
|
+
});
|
|
481
|
+
const rows = await solutions(wanted.map((n) => ({ orderbook: n.market, untilUs: n.timeUs + 1, limit: 1 })));
|
|
482
|
+
rows.forEach((rs, i) => {
|
|
483
|
+
const n = wanted[i];
|
|
484
|
+
if (!n)
|
|
485
|
+
return;
|
|
486
|
+
const r = rs[0];
|
|
487
|
+
if (r)
|
|
488
|
+
store(r, n.timeUs);
|
|
489
|
+
else
|
|
490
|
+
cache.noteEmpty(n.market, n.timeUs);
|
|
491
|
+
});
|
|
492
|
+
};
|
|
493
|
+
const fixed = [];
|
|
494
|
+
for (const b of backstop.transfers) {
|
|
495
|
+
const timeUs = b.time * 1000;
|
|
496
|
+
if (!b.orderbookId || b.size === 0n || timeUs < fromUs || timeUs > nowUs)
|
|
497
|
+
continue;
|
|
498
|
+
use(b.orderbookId);
|
|
499
|
+
fixed.push({ market: b.orderbookId, timeUs, deltaSize: -b.size, price: b.markPrice, kind: "sweep" });
|
|
500
|
+
}
|
|
501
|
+
for (const w of withdrawals) {
|
|
502
|
+
if (w.error || w.timeUs < fromUs || w.timeUs > nowUs)
|
|
503
|
+
continue;
|
|
504
|
+
const m = spotByToken.get(w.token);
|
|
505
|
+
if (!m) {
|
|
506
|
+
fixed.push({ timeUs: w.timeUs, deltaSize: -w.amount, price: 0n, kind: "cash" });
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
use(m.id);
|
|
510
|
+
fixed.push({ market: m.id, timeUs: w.timeUs, deltaSize: -w.amount, price: 0n, kind: "withdraw" });
|
|
511
|
+
}
|
|
512
|
+
walker.feed(fixed);
|
|
513
|
+
// Ranges cached from a node without the activity feed hold no flows. If the
|
|
514
|
+
// feed is there now, drop them so this run fetches everything with it.
|
|
515
|
+
if (cache.flowsMissing && (await drainFlows(deps.rest, account, nowUs, nowUs + 1)) !== undefined) {
|
|
516
|
+
cache.flowsMissing = false;
|
|
517
|
+
cache.forget(0, Number.MAX_SAFE_INTEGER);
|
|
518
|
+
}
|
|
519
|
+
// Deposits, transfers and backstop cash sweeps come from the activity feed;
|
|
520
|
+
// withdrawals and sweep legs already arrived above, so those rows are skipped.
|
|
521
|
+
let flowsAvailable = true;
|
|
522
|
+
const toFlowEvents = (rows) => {
|
|
523
|
+
const out = [];
|
|
524
|
+
for (const r of rows) {
|
|
525
|
+
const timeUs = r.timeMs * 1000;
|
|
526
|
+
if (r.activityType === "backstop") {
|
|
527
|
+
if (!r.orderbookId)
|
|
528
|
+
out.push({ timeUs, deltaSize: -r.cash, price: 0n, kind: "cash" });
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (r.error || (r.activityType === "bridge_transfer" && r.amount < 0n))
|
|
532
|
+
continue;
|
|
533
|
+
const m = spotByToken.get(r.token);
|
|
534
|
+
if (!m) {
|
|
535
|
+
out.push({ timeUs, deltaSize: r.amount, price: 0n, kind: "cash" });
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
use(m.id);
|
|
539
|
+
out.push({ market: m.id, timeUs, deltaSize: r.amount, price: 0n, kind: r.amount < 0n ? "withdraw" : "deposit" });
|
|
540
|
+
}
|
|
541
|
+
return out;
|
|
542
|
+
};
|
|
543
|
+
const pairsOf = (events) => events.flatMap((e) => (e.market ? [{ market: e.market, timeUs: e.timeUs }] : []));
|
|
544
|
+
const preface = Promise.all([fetchTicks([nowUs]), fetchPairs(pairsOf(fixed))]);
|
|
545
|
+
const toEvent = (f) => {
|
|
546
|
+
const base = dec(f.base_amount);
|
|
547
|
+
return { market: f.orderbook_id, timeUs: f.timestamp, deltaSize: dec(f.initial_size) < 0n ? -base : base, price: dec(f.price), kind: "fill" };
|
|
548
|
+
};
|
|
549
|
+
// Chunks newest-first. Chunk k needs the events in (oldest_k, oldest_{k−1}],
|
|
550
|
+
// an explicit window, so the next windows load while earlier ones are folded.
|
|
551
|
+
const chunks = [];
|
|
552
|
+
const newestFirstGrid = [...gridUs].reverse();
|
|
553
|
+
for (let i = 0, n = 0; i < newestFirstGrid.length; n++) {
|
|
554
|
+
const size = CHUNK_SIZES[Math.min(n, CHUNK_SIZES.length - 1)] ?? 50;
|
|
555
|
+
chunks.push(newestFirstGrid.slice(i, i + size));
|
|
556
|
+
i += size;
|
|
557
|
+
}
|
|
558
|
+
const prep = async (k) => {
|
|
559
|
+
const pts = chunks[k] ?? [];
|
|
560
|
+
const oldest = pts[pts.length - 1] ?? 0;
|
|
561
|
+
const bound = k === 0 ? nowUs : (chunks[k - 1]?.at(-1) ?? nowUs);
|
|
562
|
+
const warnings = [];
|
|
563
|
+
await Promise.all(cache.missing(oldest + 1, bound + 1).map(async (r) => {
|
|
564
|
+
const [page, flows] = await Promise.all([drainFills(io, account, r.from, r.to), drainFlows(deps.rest, account, r.from, r.to)]);
|
|
565
|
+
if (flows === undefined) {
|
|
566
|
+
flowsAvailable = false;
|
|
567
|
+
cache.flowsMissing = true;
|
|
568
|
+
}
|
|
569
|
+
cache.add(r.from, r.to, page.fills.map(toEvent).concat(toFlowEvents(flows ?? [])));
|
|
570
|
+
warnings.push(...page.warnings);
|
|
571
|
+
}));
|
|
572
|
+
const events = cache.slice(oldest + 1, bound + 1);
|
|
573
|
+
await Promise.all([fetchTicks(pts), fetchPairs(pairsOf(events))]);
|
|
574
|
+
return { events, warnings };
|
|
575
|
+
};
|
|
576
|
+
const queue = [];
|
|
577
|
+
let nextPrep = 0;
|
|
578
|
+
for (let k = 0; k < chunks.length; k++) {
|
|
579
|
+
while (queue.length < IN_FLIGHT && nextPrep < chunks.length) {
|
|
580
|
+
const p = prep(nextPrep++);
|
|
581
|
+
p.catch(() => { });
|
|
582
|
+
queue.push(p);
|
|
583
|
+
}
|
|
584
|
+
let first = await queue.shift();
|
|
585
|
+
if (k === 0 && (await indexerLagged)) {
|
|
586
|
+
cache.forget((chunks[0]?.at(-1) ?? 0) + 1, nowUs + 1);
|
|
587
|
+
first = await prep(0);
|
|
588
|
+
}
|
|
589
|
+
const { events, warnings } = first;
|
|
590
|
+
await preface;
|
|
591
|
+
for (const e of events)
|
|
592
|
+
if (e.market)
|
|
593
|
+
use(e.market);
|
|
594
|
+
walker.feed(events);
|
|
595
|
+
const pts = chunks[k] ?? [];
|
|
596
|
+
await fetchPairs(pts.flatMap((t) => [...known].map((market) => ({ market, timeUs: t }))));
|
|
597
|
+
const out = pts.map((t) => walker.point(t)).reverse();
|
|
598
|
+
if (!flowsAvailable) {
|
|
599
|
+
for (const p of out)
|
|
600
|
+
delete p.accountValue;
|
|
601
|
+
if (k === 0)
|
|
602
|
+
warnings.push("account value unavailable: the node does not serve the activity feed");
|
|
603
|
+
}
|
|
604
|
+
yield { points: out, done: k === chunks.length - 1, warnings: [...walker.warnings.splice(0), ...warnings] };
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
export async function fetchPnlHistory(deps, account, q) {
|
|
608
|
+
let points = [];
|
|
609
|
+
const warnings = [];
|
|
610
|
+
for await (const c of streamPnlHistory(deps, account, q)) {
|
|
611
|
+
points = c.points.concat(points);
|
|
612
|
+
warnings.push(...c.warnings);
|
|
613
|
+
}
|
|
614
|
+
const base = points[0]?.pnl ?? 0n;
|
|
615
|
+
return { points: points.map((p) => ({ ...p, pnl: p.pnl - base })), warnings };
|
|
616
|
+
}
|
|
617
|
+
//# sourceMappingURL=pnl-history.js.map
|