@pod-network/trade-sdk 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +18 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +30 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- 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/pnl-history.d.ts +121 -0
- package/dist/sync/pnl-history.d.ts.map +1 -0
- package/dist/sync/pnl-history.js +427 -0
- package/dist/sync/pnl-history.js.map +1 -0
- package/dist/transport/jsonrpc.d.ts +5 -0
- package/dist/transport/jsonrpc.d.ts.map +1 -1
- package/dist/transport/jsonrpc.js +20 -0
- package/dist/transport/jsonrpc.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +32 -0
- package/src/index.ts +4 -0
- package/src/sync/order-preview.ts +63 -0
- package/src/sync/pnl-history.ts +470 -0
- package/src/transport/jsonrpc.ts +23 -0
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// available_margin = withdrawable_cash (free cross margin)
|
|
7
7
|
// max_notional = available_margin / initial_margin_rate (≈ available · max_leverage)
|
|
8
8
|
// implied_leverage = (Σ current perp notional + notional) / perps_equity
|
|
9
|
+
// liquidation = mark where equity falls to Σ maintenance margin (im/2)
|
|
9
10
|
|
|
10
11
|
import type { Market, PerpPosition, PositionsSnapshot, Trigger } from "../types/public.js";
|
|
11
12
|
import { div, imRate, mul } from "../codec/fixed.js";
|
|
@@ -87,6 +88,67 @@ export interface OrderPreview {
|
|
|
87
88
|
sufficientMargin: boolean;
|
|
88
89
|
/** Estimated fee = notional · fee rate (taker for market, maker for limit). */
|
|
89
90
|
estimatedFee: bigint;
|
|
91
|
+
/** Mark at which the account is liquidated once this order fills; undefined
|
|
92
|
+
* when this market's mark alone can never trigger one. */
|
|
93
|
+
liquidationPrice?: bigint;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const absB = (x: bigint) => (x < 0n ? -x : x);
|
|
97
|
+
const maxB = (a: bigint, b: bigint) => (a > b ? a : b);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Mark at which the account is liquidated once an order for signed `size` fills
|
|
101
|
+
* at `price`. Margin is cross, so this is an account-level number in one
|
|
102
|
+
* market's mark: the engine liquidates at `perps_equity < Σ maintenance margin`,
|
|
103
|
+
* and both sides are linear in this market's mark X (others held still) —
|
|
104
|
+
*
|
|
105
|
+
* equity(X) = equity_now + (X − mark)·size_existing + (X − price)·size
|
|
106
|
+
* mm(X) = mm_other + |size_total|·X·mm_rate
|
|
107
|
+
* root = (mm_other − C) / (size_total − |size_total|·mm_rate),
|
|
108
|
+
* C = equity_now − mark·size_existing − price·size
|
|
109
|
+
*
|
|
110
|
+
* Undefined when no such mark exists (spot, flat after the fill, or a root at or
|
|
111
|
+
* below zero), or when `maintenanceMargin` is absent while perps elsewhere need
|
|
112
|
+
* it — one `market` cannot supply their rates, and `enrichPositions` sets the
|
|
113
|
+
* field. Prices the fill only: fees and funding shift the real number.
|
|
114
|
+
*/
|
|
115
|
+
function liquidationPriceAfter(
|
|
116
|
+
snap: PositionsSnapshot,
|
|
117
|
+
market: Market,
|
|
118
|
+
size: bigint,
|
|
119
|
+
price: bigint,
|
|
120
|
+
): bigint | undefined {
|
|
121
|
+
if (market.type !== "perp") return undefined;
|
|
122
|
+
const mmRate = imRate(market.maxLeverage) / 2n; // mm = im/2, as in enrichPositions
|
|
123
|
+
if (mmRate <= 0n) return undefined;
|
|
124
|
+
|
|
125
|
+
// Split the book: this market's perps move with X, every other one is constant.
|
|
126
|
+
let sizeExisting = 0n;
|
|
127
|
+
let markedExisting = 0n; // Σ mark·size here — the uPnL already in equity_now
|
|
128
|
+
let mmHere = 0n;
|
|
129
|
+
let mmElsewhere = false;
|
|
130
|
+
for (const p of snap.positions) {
|
|
131
|
+
if (p.kind !== "perp" || p.size === 0n) continue;
|
|
132
|
+
if (p.orderbookId === market.id) {
|
|
133
|
+
sizeExisting += p.size;
|
|
134
|
+
markedExisting += mul(p.markPrice, p.size);
|
|
135
|
+
mmHere += mul(mul(absB(p.size), p.markPrice), mmRate);
|
|
136
|
+
} else {
|
|
137
|
+
mmElsewhere = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (snap.maintenanceMargin === undefined && mmElsewhere) return undefined;
|
|
141
|
+
const mmOther = snap.maintenanceMargin === undefined
|
|
142
|
+
? 0n
|
|
143
|
+
: maxB(0n, snap.maintenanceMargin - mmHere);
|
|
144
|
+
|
|
145
|
+
const sizeTotal = sizeExisting + size;
|
|
146
|
+
const denom = sizeTotal - mul(absB(sizeTotal), mmRate);
|
|
147
|
+
if (denom === 0n) return undefined; // flat after the fill
|
|
148
|
+
|
|
149
|
+
const c = snap.perpsEquity - markedExisting - mul(price, size);
|
|
150
|
+
const liq = div(mmOther - c, denom);
|
|
151
|
+
return liq > 0n ? liq : undefined;
|
|
90
152
|
}
|
|
91
153
|
|
|
92
154
|
export function previewOrder(
|
|
@@ -124,6 +186,7 @@ export function previewOrder(
|
|
|
124
186
|
impliedLeverage,
|
|
125
187
|
sufficientMargin: marginRequired <= availableMargin,
|
|
126
188
|
estimatedFee: mul(notional, input.orderType === "limit" ? market.makerFee : market.takerFee),
|
|
189
|
+
liquidationPrice: liquidationPriceAfter(snap, market, size, input.price),
|
|
127
190
|
};
|
|
128
191
|
}
|
|
129
192
|
|
|
@@ -0,0 +1,470 @@
|
|
|
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
|
+
|
|
14
|
+
import type { Address, MarketId } from "../types/public.js";
|
|
15
|
+
import { div, mul } from "../codec/fixed.js";
|
|
16
|
+
import { dec } from "../codec/units.js";
|
|
17
|
+
import { rpc, rpcBatch, type RpcOptions } from "../transport/jsonrpc.js";
|
|
18
|
+
import type { PodRestClient } from "../transport/rest.js";
|
|
19
|
+
|
|
20
|
+
export interface PnlPoint { time: number; pnl: bigint }
|
|
21
|
+
export interface PnlHistory { points: PnlPoint[]; warnings: string[] }
|
|
22
|
+
export interface PnlHistoryQuery {
|
|
23
|
+
/** ms */
|
|
24
|
+
from: number;
|
|
25
|
+
/** ms; defaults to the engine's current tick */
|
|
26
|
+
to?: number;
|
|
27
|
+
/** Sample count, at most 500 (the default). The step is the window divided
|
|
28
|
+
* by it, rounded up to a whole batch interval. */
|
|
29
|
+
points?: number;
|
|
30
|
+
}
|
|
31
|
+
/** One step of the backward walk. Chunks arrive newest-first; within a chunk
|
|
32
|
+
* points are ascending. `pnl` is relative to the reference tick (0 at the
|
|
33
|
+
* newest point), so a chart anchored at `from` shifts by the last chunk's
|
|
34
|
+
* first point once `done`. */
|
|
35
|
+
export interface PnlHistoryChunk { points: PnlPoint[]; done: boolean; warnings: string[] }
|
|
36
|
+
|
|
37
|
+
export interface PnlLeg {
|
|
38
|
+
market: MarketId;
|
|
39
|
+
kind: "perp" | "spot";
|
|
40
|
+
size: bigint;
|
|
41
|
+
/** perp: fundingBasis − costBasis; spot: costBasis. Absent when the node
|
|
42
|
+
* does not serve the bases; the residual check is then skipped. */
|
|
43
|
+
basis?: bigint;
|
|
44
|
+
}
|
|
45
|
+
export interface PnlEvent {
|
|
46
|
+
market: MarketId;
|
|
47
|
+
timeUs: number;
|
|
48
|
+
deltaSize: bigint;
|
|
49
|
+
/** Fill price; unused for withdrawals. */
|
|
50
|
+
price: bigint;
|
|
51
|
+
kind: "fill" | "withdraw" | "sweep";
|
|
52
|
+
}
|
|
53
|
+
export interface PnlTick { mark: bigint; funding: bigint }
|
|
54
|
+
export interface PnlFoldInput {
|
|
55
|
+
refTimeUs: number;
|
|
56
|
+
/** One per market touched by legs or events. */
|
|
57
|
+
legs: PnlLeg[];
|
|
58
|
+
events: PnlEvent[];
|
|
59
|
+
/** Ascending, all ≤ refTimeUs. */
|
|
60
|
+
gridUs: number[];
|
|
61
|
+
/** Newest tick at or before `timeUs`. */
|
|
62
|
+
tickAt: (market: MarketId, timeUs: number) => PnlTick | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const RESIDUAL_TOLERANCE = 10n ** 12n;
|
|
66
|
+
// Within a tick the engine runs sweeps, then matching, then withdrawals;
|
|
67
|
+
// walking backwards undoes them in the opposite order.
|
|
68
|
+
const RANK = { withdraw: 0, fill: 1, sweep: 2 } as const;
|
|
69
|
+
const newestFirst = (a: PnlEvent, b: PnlEvent) => b.timeUs - a.timeUs || RANK[a.kind] - RANK[b.kind];
|
|
70
|
+
|
|
71
|
+
interface LegState { kind: "perp" | "spot"; size: bigint; basis: bigint; basisKnown: boolean }
|
|
72
|
+
|
|
73
|
+
/** The backward walk. Feed events newest-first, ask for points newest-first;
|
|
74
|
+
* every event after a point must have been fed before the point is asked. */
|
|
75
|
+
export class PnlWalker {
|
|
76
|
+
readonly warnings: string[] = [];
|
|
77
|
+
private readonly state = new Map<MarketId, LegState>();
|
|
78
|
+
private pending: PnlEvent[] = [];
|
|
79
|
+
private acc = 0n;
|
|
80
|
+
private refValue?: bigint;
|
|
81
|
+
|
|
82
|
+
constructor(
|
|
83
|
+
legs: PnlLeg[],
|
|
84
|
+
private readonly tickAt: (market: MarketId, timeUs: number) => PnlTick | undefined,
|
|
85
|
+
private readonly refTimeUs: number,
|
|
86
|
+
) {
|
|
87
|
+
for (const l of legs) this.state.set(l.market, { kind: l.kind, size: l.size, basis: l.basis ?? 0n, basisKnown: l.basis !== undefined });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A market first seen in an old fill: flat at the reference. */
|
|
91
|
+
touch(market: MarketId, kind: "perp" | "spot"): void {
|
|
92
|
+
if (!this.state.has(market)) this.state.set(market, { kind, size: 0n, basis: 0n, basisKnown: true });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
feed(events: PnlEvent[]): void {
|
|
96
|
+
this.pending = this.pending.concat(events).sort(newestFirst);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
point(t: number): PnlPoint {
|
|
100
|
+
this.refValue ??= this.value(this.refTimeUs);
|
|
101
|
+
for (let e = this.pending[0]; e && e.timeUs > t; e = this.pending[0]) {
|
|
102
|
+
this.pending.shift();
|
|
103
|
+
this.acc += this.unapply(e);
|
|
104
|
+
}
|
|
105
|
+
return { time: t / 1000, pnl: this.value(t) - this.refValue - this.acc };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private value(t: number): bigint {
|
|
109
|
+
let sum = 0n;
|
|
110
|
+
for (const [m, st] of this.state) {
|
|
111
|
+
if (st.size === 0n) continue;
|
|
112
|
+
const tick = this.tickAt(m, t);
|
|
113
|
+
if (!tick) { this.warnings.push(`${m}: no tick at or before ${t}`); continue; }
|
|
114
|
+
sum += mul(tick.mark - tick.funding, st.size);
|
|
115
|
+
}
|
|
116
|
+
return sum;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private unapply(e: PnlEvent): bigint {
|
|
120
|
+
const st = this.state.get(e.market);
|
|
121
|
+
if (!st) throw new Error(`pnlHistory: event on unknown market ${e.market}`);
|
|
122
|
+
const tick = this.tickAt(e.market, e.timeUs);
|
|
123
|
+
const F = tick?.funding ?? 0n;
|
|
124
|
+
const sizeBefore = st.size - e.deltaSize;
|
|
125
|
+
let price = e.price;
|
|
126
|
+
|
|
127
|
+
if (st.kind === "spot") {
|
|
128
|
+
const average = st.size !== 0n && st.basisKnown ? div(st.basis, st.size) : undefined;
|
|
129
|
+
if (e.kind === "withdraw") {
|
|
130
|
+
if (average === undefined) {
|
|
131
|
+
this.warnings.push(`${e.market}: withdrawal at ${e.timeUs} with unknown cost basis, treated as neutral`);
|
|
132
|
+
price = tick?.mark ?? 0n;
|
|
133
|
+
} else price = average;
|
|
134
|
+
}
|
|
135
|
+
if (sizeBefore === 0n) { st.basis = 0n; st.basisKnown = true; }
|
|
136
|
+
else if (e.deltaSize > 0n) st.basis -= mul(e.price, e.deltaSize);
|
|
137
|
+
else if (average !== undefined) st.basis = mul(average, sizeBefore);
|
|
138
|
+
else st.basisKnown = false;
|
|
139
|
+
} else {
|
|
140
|
+
const crossed = st.size !== 0n && sizeBefore !== 0n && (st.size > 0n) !== (sizeBefore > 0n);
|
|
141
|
+
if (st.size === 0n || crossed) st.basisKnown = false;
|
|
142
|
+
else if (st.basisKnown) st.basis -= mul(F - price, e.deltaSize);
|
|
143
|
+
if (sizeBefore === 0n && st.basisKnown) {
|
|
144
|
+
const r = st.basis < 0n ? -st.basis : st.basis;
|
|
145
|
+
if (r > RESIDUAL_TOLERANCE) this.warnings.push(`${e.market}: basis residual ${st.basis} at flat ${e.timeUs}`);
|
|
146
|
+
st.basis = 0n;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
st.size = sizeBefore;
|
|
151
|
+
return mul(F - price, e.deltaSize);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function foldPnlHistory(input: PnlFoldInput): PnlHistory {
|
|
156
|
+
const walker = new PnlWalker(input.legs, input.tickAt, input.refTimeUs);
|
|
157
|
+
walker.feed(input.events);
|
|
158
|
+
const points = [...input.gridUs].reverse().map((t) => walker.point(t)).reverse();
|
|
159
|
+
const base = points[0]?.pnl ?? 0n;
|
|
160
|
+
return { points: points.map((p) => ({ time: p.time, pnl: p.pnl - base })), warnings: walker.warnings };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// --- fetch ---
|
|
164
|
+
|
|
165
|
+
interface WireFill {
|
|
166
|
+
orderbook_id: string;
|
|
167
|
+
order_id: string;
|
|
168
|
+
initial_size: string;
|
|
169
|
+
base_amount: string;
|
|
170
|
+
timestamp: number;
|
|
171
|
+
price: string;
|
|
172
|
+
}
|
|
173
|
+
interface WireSolution {
|
|
174
|
+
orderbook_id: string;
|
|
175
|
+
timestamp: number;
|
|
176
|
+
clearing_price?: string;
|
|
177
|
+
mark_price: string;
|
|
178
|
+
funding_index?: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Immutable inputs shared by every window size and every refresh: fills as
|
|
182
|
+
* events keyed by the time ranges they cover, and ticks keyed by market and
|
|
183
|
+
* time. The fold is recomputed per request; only uncovered ranges and unseen
|
|
184
|
+
* ticks are fetched. */
|
|
185
|
+
export class PnlHistoryCache {
|
|
186
|
+
readonly ticks = new Map<string, PnlTick>();
|
|
187
|
+
/** Times at which every market's row was fetched at once. */
|
|
188
|
+
readonly tickTimes = new Set<number>();
|
|
189
|
+
private events: PnlEvent[] = [];
|
|
190
|
+
private coverage: { from: number; to: number }[] = [];
|
|
191
|
+
|
|
192
|
+
/** About 150 bytes per event and per tick, so the default cap is on the
|
|
193
|
+
* order of 50 MB with its ticks. */
|
|
194
|
+
constructor(readonly maxEvents = 250_000) {}
|
|
195
|
+
|
|
196
|
+
clear(): void {
|
|
197
|
+
this.ticks.clear();
|
|
198
|
+
this.tickTimes.clear();
|
|
199
|
+
this.events = [];
|
|
200
|
+
this.coverage = [];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Sub-ranges of [from, to) not yet covered. */
|
|
204
|
+
missing(from: number, to: number): { from: number; to: number }[] {
|
|
205
|
+
const out: { from: number; to: number }[] = [];
|
|
206
|
+
let cursor = from;
|
|
207
|
+
for (const c of this.coverage) {
|
|
208
|
+
if (c.to <= cursor) continue;
|
|
209
|
+
if (c.from >= to) break;
|
|
210
|
+
if (c.from > cursor) out.push({ from: cursor, to: c.from });
|
|
211
|
+
cursor = Math.max(cursor, c.to);
|
|
212
|
+
}
|
|
213
|
+
if (cursor < to) out.push({ from: cursor, to });
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
add(from: number, to: number, events: PnlEvent[]): void {
|
|
218
|
+
this.events = this.events.concat(events).sort(newestFirst);
|
|
219
|
+
this.coverage.push({ from, to });
|
|
220
|
+
this.coverage.sort((a, b) => a.from - b.from);
|
|
221
|
+
const merged: { from: number; to: number }[] = [];
|
|
222
|
+
for (const c of this.coverage) {
|
|
223
|
+
const last = merged[merged.length - 1];
|
|
224
|
+
if (last && c.from <= last.to) last.to = Math.max(last.to, c.to);
|
|
225
|
+
else merged.push({ ...c });
|
|
226
|
+
}
|
|
227
|
+
this.coverage = merged;
|
|
228
|
+
if (this.events.length > this.maxEvents) {
|
|
229
|
+
// ponytail: evict the oldest events, their ticks, and coverage below them.
|
|
230
|
+
const cut = this.events[this.maxEvents - 1]?.timeUs ?? 0;
|
|
231
|
+
this.events = this.events.filter((e) => e.timeUs >= cut);
|
|
232
|
+
this.coverage = this.coverage.filter((c) => c.to > cut).map((c) => ({ from: Math.max(c.from, cut), to: c.to }));
|
|
233
|
+
for (const k of this.ticks.keys()) if (Number(k.slice(k.indexOf(":") + 1)) < cut) this.ticks.delete(k);
|
|
234
|
+
for (const t of this.tickTimes) if (t < cut) this.tickTimes.delete(t);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Events with from ≤ time < to, newest first. */
|
|
239
|
+
slice(from: number, to: number): PnlEvent[] {
|
|
240
|
+
return this.events.filter((e) => e.timeUs >= from && e.timeUs < to);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface PnlHistoryDeps { rest: PodRestClient; rpcUrl: string; fetch?: typeof fetch; cache?: PnlHistoryCache }
|
|
245
|
+
|
|
246
|
+
const MAX_POINTS = 500;
|
|
247
|
+
const CHUNK_POINTS = 50;
|
|
248
|
+
const IN_FLIGHT = 4;
|
|
249
|
+
const WIDE_ACCOUNT_MARKETS = 10;
|
|
250
|
+
const RPC_BATCH = 200;
|
|
251
|
+
const FILLS_PAGE = 500;
|
|
252
|
+
|
|
253
|
+
/** Pages newest-first. A full page may cut a batch in half, so its oldest
|
|
254
|
+
* batch is dropped and refetched whole with the next page; rows are never
|
|
255
|
+
* keyed, since one order can legitimately fill several times in one batch
|
|
256
|
+
* (ADL slices). */
|
|
257
|
+
class FillsCursor {
|
|
258
|
+
done = false;
|
|
259
|
+
readonly warnings: string[] = [];
|
|
260
|
+
private to: number;
|
|
261
|
+
constructor(private readonly rpcUrl: string, private readonly account: Address, private readonly fromUs: number, toUs: number, private readonly opts: RpcOptions) {
|
|
262
|
+
this.to = toUs;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async next(): Promise<WireFill[]> {
|
|
266
|
+
const page = await rpc<{ fills: WireFill[] }>(this.rpcUrl, "ob_getFills", [this.account, { from_ts: this.fromUs, to_ts: this.to, limit: FILLS_PAGE }], this.opts);
|
|
267
|
+
if (page.fills.length < FILLS_PAGE) {
|
|
268
|
+
this.done = true;
|
|
269
|
+
return page.fills;
|
|
270
|
+
}
|
|
271
|
+
const oldest = Math.min(...page.fills.map((f) => f.timestamp));
|
|
272
|
+
if (oldest + 1 === this.to) {
|
|
273
|
+
// ponytail: a single batch with more than FILLS_PAGE fills is truncated here.
|
|
274
|
+
this.warnings.push(`batch ${oldest} has more than ${FILLS_PAGE} fills; only the first page is used`);
|
|
275
|
+
this.to = oldest;
|
|
276
|
+
return page.fills;
|
|
277
|
+
}
|
|
278
|
+
this.to = oldest + 1;
|
|
279
|
+
return page.fills.filter((f) => f.timestamp !== oldest);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function drainFills(rpcUrl: string, account: Address, fromUs: number, toUs: number, opts: RpcOptions): Promise<{ fills: WireFill[]; warnings: string[] }> {
|
|
284
|
+
const cursor = new FillsCursor(rpcUrl, account, fromUs, toUs, opts);
|
|
285
|
+
const fills: WireFill[] = [];
|
|
286
|
+
while (!cursor.done) fills.push(...(await cursor.next()));
|
|
287
|
+
return { fills, warnings: cursor.warnings };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export async function fetchFills(rpcUrl: string, account: Address, fromUs: number, toUs: number, opts: RpcOptions): Promise<WireFill[]> {
|
|
291
|
+
return (await drainFills(rpcUrl, account, fromUs, toUs, opts)).fills;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export async function* streamPnlHistory(
|
|
295
|
+
deps: PnlHistoryDeps,
|
|
296
|
+
account: Address,
|
|
297
|
+
q: PnlHistoryQuery,
|
|
298
|
+
): AsyncGenerator<PnlHistoryChunk> {
|
|
299
|
+
const rpcOpts: RpcOptions = { fetch: deps.fetch };
|
|
300
|
+
|
|
301
|
+
// The snapshot is served from the live engine, fills and ticks from the
|
|
302
|
+
// indexer. Pair them: the engine's last executed batch is read together with
|
|
303
|
+
// the snapshot, one request sent just before it and one just after, and the
|
|
304
|
+
// read is retried when the two disagree. Sequential reads would be exact but
|
|
305
|
+
// straddle a 500 ms tick on every attempt from far away; concurrent ones
|
|
306
|
+
// land within a few ms of server time, so a boundary inside that gap is rare
|
|
307
|
+
// and the next refresh heals it.
|
|
308
|
+
const engineTick = async (): Promise<number> => {
|
|
309
|
+
const st = await rpc<{ last_executed_batch?: { deadline: string | number } | null }>(deps.rpcUrl, "pod_getSolverState", [], rpcOpts);
|
|
310
|
+
return Number(st.last_executed_batch?.deadline ?? 0);
|
|
311
|
+
};
|
|
312
|
+
const readSnapshot = async () => {
|
|
313
|
+
for (let attempt = 0; ; attempt++) {
|
|
314
|
+
const [before, snap, balances, after] = await Promise.all([engineTick(), deps.rest.positions(account), deps.rest.balances(account), engineTick()]);
|
|
315
|
+
if (before === after || attempt >= 4) return { nowUs: before, snap, balances };
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const [markets, snapshot] = await Promise.all([deps.rest.markets(), readSnapshot()]);
|
|
319
|
+
const { snap, balances } = snapshot;
|
|
320
|
+
let nowUs = snapshot.nowUs;
|
|
321
|
+
const batchUs = Math.max(500, ...markets.map((m) => m.auctionIntervalMs)) * 1000;
|
|
322
|
+
// Grid points sit on batch ticks so one range call per point serves every market.
|
|
323
|
+
const fromUs = Math.ceil((q.from * 1000) / batchUs) * batchUs;
|
|
324
|
+
|
|
325
|
+
const indexerCaughtUp = async () => {
|
|
326
|
+
if (nowUs === 0) { nowUs = (await deps.rest.status()).solutionNow * 1000; return; }
|
|
327
|
+
for (let i = 0; i < 30 && (await deps.rest.status()).solutionNow * 1000 < nowUs; i++) {
|
|
328
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
const [backstop, withdrawals] = await Promise.all([
|
|
332
|
+
deps.rest.backstopTransfers(account),
|
|
333
|
+
deps.rest.bridgeWithdrawals(account, { since: fromUs - 1 }),
|
|
334
|
+
indexerCaughtUp(),
|
|
335
|
+
]);
|
|
336
|
+
const toUs = Math.min(q.to === undefined ? nowUs : Math.floor((q.to * 1000) / batchUs) * batchUs, nowUs);
|
|
337
|
+
if (fromUs >= toUs) throw new Error("pnlHistory: from must be before to");
|
|
338
|
+
|
|
339
|
+
const points = Math.min(Math.max(2, Math.floor(q.points ?? MAX_POINTS)), MAX_POINTS);
|
|
340
|
+
const stepUs = Math.ceil((toUs - fromUs) / (points - 1) / batchUs) * batchUs;
|
|
341
|
+
// The series starts at `from` and ends at `to`; the points between sit on
|
|
342
|
+
// epoch multiples of the step, so a refreshed window shares them, and their
|
|
343
|
+
// ticks, with the previous one.
|
|
344
|
+
const gridUs: number[] = [fromUs];
|
|
345
|
+
for (let t = (Math.floor(fromUs / stepUs) + 1) * stepUs; t < toUs; t += stepUs) gridUs.push(t);
|
|
346
|
+
gridUs.push(toUs);
|
|
347
|
+
|
|
348
|
+
const byId = new Map(markets.map((m) => [m.id, m]));
|
|
349
|
+
const spotByToken = new Map(markets.filter((m) => m.type === "spot").map((m) => [m.base.address, m]));
|
|
350
|
+
const kindOf = (id: MarketId): "perp" | "spot" => (byId.get(id)?.type === "spot" ? "spot" : "perp");
|
|
351
|
+
const legs: PnlLeg[] = [];
|
|
352
|
+
for (const p of snap.positions) {
|
|
353
|
+
if (p.kind !== "perp") continue;
|
|
354
|
+
const basis = p.fundingBasis !== undefined && p.costBasis !== undefined ? p.fundingBasis - p.costBasis : undefined;
|
|
355
|
+
legs.push({ market: p.orderbookId, kind: "perp", size: p.size, basis });
|
|
356
|
+
}
|
|
357
|
+
for (const h of balances.holdings) legs.push({ market: h.orderbookId, kind: "spot", size: h.balance, basis: h.costBasis });
|
|
358
|
+
|
|
359
|
+
const cache = deps.cache ?? new PnlHistoryCache();
|
|
360
|
+
const ticks = cache.ticks;
|
|
361
|
+
const key = (m: MarketId, t: number) => `${m}:${t}`;
|
|
362
|
+
const walker = new PnlWalker(legs, (m, t) => ticks.get(key(m, t)), nowUs);
|
|
363
|
+
const known = new Set<MarketId>(legs.map((l) => l.market));
|
|
364
|
+
const use = (id: MarketId) => { if (!known.has(id)) { known.add(id); walker.touch(id, kindOf(id)); } };
|
|
365
|
+
|
|
366
|
+
const store = (row: WireSolution, t: number) => {
|
|
367
|
+
const id = row.orderbook_id as MarketId;
|
|
368
|
+
const perp = kindOf(id) === "perp";
|
|
369
|
+
const window = byId.get(id)?.fundingWindowUs ?? 0;
|
|
370
|
+
ticks.set(key(id, t), {
|
|
371
|
+
mark: perp ? dec(row.mark_price) : dec(row.clearing_price),
|
|
372
|
+
funding: perp && row.funding_index !== undefined && window > 0 ? dec(row.funding_index) / BigInt(window) : 0n,
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
// Every batch of 200 calls goes out at once; the node runs them concurrently.
|
|
376
|
+
const solutions = async (params: object[]): Promise<WireSolution[][]> => {
|
|
377
|
+
const batches: object[][] = [];
|
|
378
|
+
for (let i = 0; i < params.length; i += RPC_BATCH) batches.push(params.slice(i, i + RPC_BATCH));
|
|
379
|
+
const res = await Promise.all(batches.map((b) => rpcBatch<{ solutions: WireSolution[] }>(deps.rpcUrl, b.map((p) => ({ method: "ob_getSolutions", params: [p] })), rpcOpts)));
|
|
380
|
+
return res.flat().map((r) => r.solutions);
|
|
381
|
+
};
|
|
382
|
+
/** All markets' rows at each tick, one call per tick. */
|
|
383
|
+
const fetchAllAt = async (times: number[]) => {
|
|
384
|
+
const wanted = times.filter((t) => !cache.tickTimes.has(t));
|
|
385
|
+
const rows = await solutions(wanted.map((t) => ({ since: t, until: t + 1, limit: RPC_BATCH })));
|
|
386
|
+
rows.forEach((rs, i) => { const t = wanted[i] ?? 0; cache.tickTimes.add(t); for (const r of rs) store(r, t); });
|
|
387
|
+
};
|
|
388
|
+
/** Ticks for every known market at the grid points. One call per tick when
|
|
389
|
+
* the account spans many markets, else one lookup per market: the wide call
|
|
390
|
+
* returns every market's row whether needed or not. */
|
|
391
|
+
const fetchTicks = async (times: number[]) => {
|
|
392
|
+
if (known.size >= WIDE_ACCOUNT_MARKETS) await fetchAllAt(times);
|
|
393
|
+
else await fetchPairs(times.flatMap((t) => [...known].map((market) => ({ market, timeUs: t }))));
|
|
394
|
+
};
|
|
395
|
+
/** Newest row at or before the time, per pair; the fallback and the per-fill path. */
|
|
396
|
+
const fetchPairs = async (pairs: { market: MarketId; timeUs: number }[]) => {
|
|
397
|
+
const wanted = pairs.filter((n, i, all) => !ticks.has(key(n.market, n.timeUs)) && all.findIndex((o) => o.market === n.market && o.timeUs === n.timeUs) === i);
|
|
398
|
+
const rows = await solutions(wanted.map((n) => ({ orderbook_id: n.market, until: n.timeUs + 1, limit: 1 })));
|
|
399
|
+
rows.forEach((rs, i) => { const r = rs[0]; const n = wanted[i]; if (r && n) store(r, n.timeUs); });
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const fixed: PnlEvent[] = [];
|
|
403
|
+
for (const b of backstop.transfers) {
|
|
404
|
+
const timeUs = b.time * 1000;
|
|
405
|
+
if (!b.orderbookId || b.size === 0n || timeUs < fromUs || timeUs > nowUs) continue;
|
|
406
|
+
use(b.orderbookId);
|
|
407
|
+
fixed.push({ market: b.orderbookId, timeUs, deltaSize: -b.size, price: b.markPrice, kind: "sweep" });
|
|
408
|
+
}
|
|
409
|
+
for (const w of withdrawals) {
|
|
410
|
+
if (w.error || w.timeUs < fromUs || w.timeUs > nowUs) continue;
|
|
411
|
+
const m = spotByToken.get(w.token);
|
|
412
|
+
if (!m) continue;
|
|
413
|
+
use(m.id);
|
|
414
|
+
fixed.push({ market: m.id, timeUs: w.timeUs, deltaSize: -w.amount, price: 0n, kind: "withdraw" });
|
|
415
|
+
}
|
|
416
|
+
walker.feed(fixed);
|
|
417
|
+
const preface = Promise.all([fetchTicks([nowUs]), fetchPairs(fixed.map((e) => ({ market: e.market, timeUs: e.timeUs })))]);
|
|
418
|
+
|
|
419
|
+
const toEvent = (f: WireFill): PnlEvent => {
|
|
420
|
+
const base = dec(f.base_amount);
|
|
421
|
+
return { market: f.orderbook_id as MarketId, timeUs: f.timestamp, deltaSize: dec(f.initial_size) < 0n ? -base : base, price: dec(f.price), kind: "fill" };
|
|
422
|
+
};
|
|
423
|
+
// Chunks newest-first. Chunk k needs the events in (oldest_k, oldest_{k−1}],
|
|
424
|
+
// an explicit window, so the next windows load while earlier ones are folded.
|
|
425
|
+
const chunks: number[][] = [];
|
|
426
|
+
const newestFirstGrid = [...gridUs].reverse();
|
|
427
|
+
for (let i = 0; i < newestFirstGrid.length; i += CHUNK_POINTS) chunks.push(newestFirstGrid.slice(i, i + CHUNK_POINTS));
|
|
428
|
+
const prep = async (k: number): Promise<{ events: PnlEvent[]; warnings: string[] }> => {
|
|
429
|
+
const pts = chunks[k] ?? [];
|
|
430
|
+
const oldest = pts[pts.length - 1] ?? 0;
|
|
431
|
+
const bound = k === 0 ? nowUs : (chunks[k - 1]?.at(-1) ?? nowUs);
|
|
432
|
+
const warnings: string[] = [];
|
|
433
|
+
await Promise.all(cache.missing(oldest + 1, bound + 1).map(async (r) => {
|
|
434
|
+
const page = await drainFills(deps.rpcUrl, account, r.from, r.to, rpcOpts);
|
|
435
|
+
cache.add(r.from, r.to, page.fills.map(toEvent));
|
|
436
|
+
warnings.push(...page.warnings);
|
|
437
|
+
}));
|
|
438
|
+
const events = cache.slice(oldest + 1, bound + 1);
|
|
439
|
+
await Promise.all([fetchTicks(pts), fetchPairs(events.map((e) => ({ market: e.market, timeUs: e.timeUs })))]);
|
|
440
|
+
return { events, warnings };
|
|
441
|
+
};
|
|
442
|
+
const queue: Promise<{ events: PnlEvent[]; warnings: string[] }>[] = [];
|
|
443
|
+
let nextPrep = 0;
|
|
444
|
+
for (let k = 0; k < chunks.length; k++) {
|
|
445
|
+
while (queue.length < IN_FLIGHT && nextPrep < chunks.length) {
|
|
446
|
+
const p = prep(nextPrep++);
|
|
447
|
+
p.catch(() => {});
|
|
448
|
+
queue.push(p);
|
|
449
|
+
}
|
|
450
|
+
const { events, warnings } = await (queue.shift() as Promise<{ events: PnlEvent[]; warnings: string[] }>);
|
|
451
|
+
await preface;
|
|
452
|
+
for (const e of events) use(e.market);
|
|
453
|
+
walker.feed(events);
|
|
454
|
+
const pts = chunks[k] ?? [];
|
|
455
|
+
await fetchPairs(pts.flatMap((t) => [...known].map((market) => ({ market, timeUs: t }))));
|
|
456
|
+
const out = pts.map((t) => walker.point(t)).reverse();
|
|
457
|
+
yield { points: out, done: k === chunks.length - 1, warnings: [...walker.warnings.splice(0), ...warnings] };
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export async function fetchPnlHistory(deps: PnlHistoryDeps, account: Address, q: PnlHistoryQuery): Promise<PnlHistory> {
|
|
462
|
+
let points: PnlPoint[] = [];
|
|
463
|
+
const warnings: string[] = [];
|
|
464
|
+
for await (const c of streamPnlHistory(deps, account, q)) {
|
|
465
|
+
points = c.points.concat(points);
|
|
466
|
+
warnings.push(...c.warnings);
|
|
467
|
+
}
|
|
468
|
+
const base = points[0]?.pnl ?? 0n;
|
|
469
|
+
return { points: points.map((p) => ({ time: p.time, pnl: p.pnl - base })), warnings };
|
|
470
|
+
}
|
package/src/transport/jsonrpc.ts
CHANGED
|
@@ -52,3 +52,26 @@ export async function tryRpc<T>(
|
|
|
52
52
|
return undefined;
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
+
|
|
56
|
+
/** One HTTP round trip carrying many calls; results come back in call order. */
|
|
57
|
+
export async function rpcBatch<T>(
|
|
58
|
+
rpcUrl: string,
|
|
59
|
+
calls: { method: string; params: unknown[] }[],
|
|
60
|
+
opts?: RpcOptions,
|
|
61
|
+
): Promise<T[]> {
|
|
62
|
+
const doFetch = opts?.fetch ?? fetch;
|
|
63
|
+
const res = await doFetch(rpcUrl, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: { "content-type": "application/json" },
|
|
66
|
+
body: JSON.stringify(calls.map((c, id) => ({ jsonrpc: "2.0", id, ...c }))),
|
|
67
|
+
signal: opts?.signal,
|
|
68
|
+
});
|
|
69
|
+
const json = (await res.json()) as { id: number; result?: T; error?: { code?: number; message?: string } }[];
|
|
70
|
+
if (!Array.isArray(json)) throw new PodRpcError(undefined, "batch response is not an array");
|
|
71
|
+
const out = new Array<T>(calls.length);
|
|
72
|
+
for (const r of json) {
|
|
73
|
+
if (r.error) throw new PodRpcError(r.error.code, r.error.message ?? `${calls[r.id]?.method} failed`);
|
|
74
|
+
out[r.id] = r.result as T;
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|