@pod-network/trade-sdk 0.1.0-rc.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 +113 -0
- package/dist/client.d.ts +93 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +168 -0
- package/dist/client.js.map +1 -0
- package/dist/codec/decode.d.ts +22 -0
- package/dist/codec/decode.d.ts.map +1 -0
- package/dist/codec/decode.js +251 -0
- package/dist/codec/decode.js.map +1 -0
- package/dist/codec/fixed.d.ts +11 -0
- package/dist/codec/fixed.d.ts.map +1 -0
- package/dist/codec/fixed.js +37 -0
- package/dist/codec/fixed.js.map +1 -0
- package/dist/codec/resolution.d.ts +14 -0
- package/dist/codec/resolution.d.ts.map +1 -0
- package/dist/codec/resolution.js +35 -0
- package/dist/codec/resolution.js.map +1 -0
- package/dist/codec/units.d.ts +35 -0
- package/dist/codec/units.d.ts.map +1 -0
- package/dist/codec/units.js +86 -0
- package/dist/codec/units.js.map +1 -0
- package/dist/datafeed.d.ts +4 -0
- package/dist/datafeed.d.ts.map +1 -0
- package/dist/datafeed.js +73 -0
- package/dist/datafeed.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/stores/resource.d.ts +50 -0
- package/dist/stores/resource.d.ts.map +1 -0
- package/dist/stores/resource.js +154 -0
- package/dist/stores/resource.js.map +1 -0
- package/dist/sync/account-metrics.d.ts +26 -0
- package/dist/sync/account-metrics.d.ts.map +1 -0
- package/dist/sync/account-metrics.js +41 -0
- package/dist/sync/account-metrics.js.map +1 -0
- package/dist/sync/candles.d.ts +68 -0
- package/dist/sync/candles.d.ts.map +1 -0
- package/dist/sync/candles.js +318 -0
- package/dist/sync/candles.js.map +1 -0
- package/dist/sync/order-preview.d.ts +74 -0
- package/dist/sync/order-preview.d.ts.map +1 -0
- package/dist/sync/order-preview.js +86 -0
- package/dist/sync/order-preview.js.map +1 -0
- package/dist/sync/orders.d.ts +49 -0
- package/dist/sync/orders.d.ts.map +1 -0
- package/dist/sync/orders.js +210 -0
- package/dist/sync/orders.js.map +1 -0
- package/dist/sync/positions-live.d.ts +3 -0
- package/dist/sync/positions-live.d.ts.map +1 -0
- package/dist/sync/positions-live.js +86 -0
- package/dist/sync/positions-live.js.map +1 -0
- package/dist/sync/sources.d.ts +40 -0
- package/dist/sync/sources.d.ts.map +1 -0
- package/dist/sync/sources.js +197 -0
- package/dist/sync/sources.js.map +1 -0
- package/dist/transport/rest.d.ts +83 -0
- package/dist/transport/rest.d.ts.map +1 -0
- package/dist/transport/rest.js +146 -0
- package/dist/transport/rest.js.map +1 -0
- package/dist/transport/ws.d.ts +60 -0
- package/dist/transport/ws.d.ts.map +1 -0
- package/dist/transport/ws.js +235 -0
- package/dist/transport/ws.js.map +1 -0
- package/dist/types/public.d.ts +292 -0
- package/dist/types/public.d.ts.map +1 -0
- package/dist/types/public.js +4 -0
- package/dist/types/public.js.map +1 -0
- package/dist/types/wire.d.ts +283 -0
- package/dist/types/wire.d.ts.map +1 -0
- package/dist/types/wire.js +6 -0
- package/dist/types/wire.js.map +1 -0
- package/dist/write/delegation.d.ts +54 -0
- package/dist/write/delegation.d.ts.map +1 -0
- package/dist/write/delegation.js +81 -0
- package/dist/write/delegation.js.map +1 -0
- package/dist/write/index.d.ts +261 -0
- package/dist/write/index.d.ts.map +1 -0
- package/dist/write/index.js +428 -0
- package/dist/write/index.js.map +1 -0
- package/package.json +43 -0
- package/src/client.ts +229 -0
- package/src/codec/decode.ts +277 -0
- package/src/codec/fixed.ts +41 -0
- package/src/codec/resolution.ts +39 -0
- package/src/codec/units.ts +100 -0
- package/src/datafeed.ts +94 -0
- package/src/index.ts +49 -0
- package/src/stores/resource.ts +172 -0
- package/src/sync/account-metrics.ts +71 -0
- package/src/sync/candles.ts +326 -0
- package/src/sync/order-preview.ts +152 -0
- package/src/sync/orders.ts +205 -0
- package/src/sync/positions-live.ts +102 -0
- package/src/sync/sources.ts +239 -0
- package/src/transport/rest.ts +213 -0
- package/src/transport/ws.ts +255 -0
- package/src/types/public.ts +335 -0
- package/src/types/wire.ts +301 -0
- package/src/write/delegation.ts +113 -0
- package/src/write/index.ts +642 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Pre-trade preview math — the financial logic behind an order-entry panel,
|
|
2
|
+
// kept out of the UI. Pure function over the live account snapshot + market.
|
|
3
|
+
//
|
|
4
|
+
// Mirrors the backend's cross-margin check:
|
|
5
|
+
// margin_required = notional · initial_margin_rate (rate = 1/max_leverage)
|
|
6
|
+
// available_margin = withdrawable_cash (free cross margin)
|
|
7
|
+
// max_notional = available_margin / initial_margin_rate (≈ available · max_leverage)
|
|
8
|
+
// implied_leverage = (Σ current perp notional + notional) / perps_equity
|
|
9
|
+
import { div, imRate, mul } from "../codec/fixed.js";
|
|
10
|
+
import { parseAmount, toNumber, WAD } from "../codec/units.js";
|
|
11
|
+
/**
|
|
12
|
+
* Mark price at which a perp position reaches `returnPct` return **on its
|
|
13
|
+
* initial margin** (not a raw price move). Since margin = notional / leverage,
|
|
14
|
+
* return-on-margin = price-move% × leverage, so the price move needed is
|
|
15
|
+
* `returnPct / (100·leverage)` — e.g. +20% on a 10x long needs only a +2% move.
|
|
16
|
+
* Used to turn a TP "gain %" / SL "loss %" into a trigger price.
|
|
17
|
+
*/
|
|
18
|
+
export function priceForReturn(p) {
|
|
19
|
+
if (p.leverage <= 0 || p.entryPrice <= 0n)
|
|
20
|
+
return p.entryPrice;
|
|
21
|
+
const move = p.returnPct / (100 * p.leverage); // fractional price move
|
|
22
|
+
const dir = p.side === "long" ? 1 : -1; // longs profit up, shorts profit down
|
|
23
|
+
const human = toNumber(p.entryPrice) * (1 + dir * move);
|
|
24
|
+
return human > 0 ? parseAmount(human.toFixed(8)) : 0n;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The TP and SL triggers nearest the mark for a position — i.e. the ones that
|
|
28
|
+
* would fire first. "Nearest" depends on the side: a long's TPs sit above the
|
|
29
|
+
* mark (nearest = lowest price) and its SLs below (nearest = highest); a short
|
|
30
|
+
* is mirrored. Pass the position's triggers (already filtered to its market).
|
|
31
|
+
*/
|
|
32
|
+
export function closestTriggers(side, triggers) {
|
|
33
|
+
const pick = (list, wantLowest) => list.reduce((best, t) => {
|
|
34
|
+
if (!best)
|
|
35
|
+
return t;
|
|
36
|
+
return (wantLowest ? t.triggerPrice < best.triggerPrice : t.triggerPrice > best.triggerPrice) ? t : best;
|
|
37
|
+
}, undefined);
|
|
38
|
+
const tps = triggers.filter((t) => t.triggerType === "take_profit");
|
|
39
|
+
const sls = triggers.filter((t) => t.triggerType === "stop_loss");
|
|
40
|
+
return {
|
|
41
|
+
takeProfit: pick(tps, side === "long"), // long TP nearest = lowest; short = highest
|
|
42
|
+
stopLoss: pick(sls, side === "short"), // long SL nearest = highest; short = lowest
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function previewOrder(snap, market, input) {
|
|
46
|
+
// Spot has no leverage: the order locks the full notional in cash (im = 1.0).
|
|
47
|
+
// Perps use the market's initial-margin rate (1 / max_leverage).
|
|
48
|
+
const im = market.type === "spot" ? WAD : imRate(market.maxLeverage);
|
|
49
|
+
const availableMargin = snap.withdrawableCash;
|
|
50
|
+
const marginRequired = mul(input.notional, im);
|
|
51
|
+
const maxNotional = im > 0n ? div(availableMargin, im) : 0n;
|
|
52
|
+
const magnitude = input.price > 0n ? div(input.notional, input.price) : 0n;
|
|
53
|
+
const size = input.side === "short" ? -magnitude : magnitude;
|
|
54
|
+
const currentPerpNotional = snap.positions.reduce((acc, p) => (p.kind === "perp" ? acc + p.notional : acc), 0n);
|
|
55
|
+
const impliedLeverage = snap.perpsEquity > 0n
|
|
56
|
+
? toNumber(currentPerpNotional + input.notional) / toNumber(snap.perpsEquity)
|
|
57
|
+
: 0;
|
|
58
|
+
return {
|
|
59
|
+
size,
|
|
60
|
+
availableMargin,
|
|
61
|
+
marginRequired,
|
|
62
|
+
maxNotional,
|
|
63
|
+
impliedLeverage,
|
|
64
|
+
sufficientMargin: marginRequired <= availableMargin,
|
|
65
|
+
estimatedFee: mul(input.notional, input.orderType === "limit" ? market.makerFee : market.takerFee),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Expected result of closing `size` (magnitude) of a position at `price`:
|
|
70
|
+
* long → (price − entry)·size, short → (entry − price)·size. Pass the intended
|
|
71
|
+
* exit price — the limit price, or the protective bound for a market close —
|
|
72
|
+
* and the market's fee rate (taker for a market close) for the fee estimate.
|
|
73
|
+
*/
|
|
74
|
+
export function closePreview(position, input) {
|
|
75
|
+
const size = input.size < 0n ? -input.size : input.size;
|
|
76
|
+
const move = position.side === "long"
|
|
77
|
+
? input.price - position.entryPrice
|
|
78
|
+
: position.entryPrice - input.price;
|
|
79
|
+
const notional = (input.price * size) / WAD;
|
|
80
|
+
return {
|
|
81
|
+
expectedPnl: (move * size) / WAD,
|
|
82
|
+
notional,
|
|
83
|
+
fee: mul(notional, input.feeRate ?? 0n),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=order-preview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-preview.js","sourceRoot":"","sources":["../../src/sync/order-preview.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,EAAE;AACF,4CAA4C;AAC5C,qFAAqF;AACrF,iFAAiF;AACjF,2FAA2F;AAC3F,2EAA2E;AAG3E,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAW/D;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,CAAmB;IAChD,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,CAA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1
|
+
import type { Address, MarketId, Order, OrdersQuery } from "../types/public.js";
|
|
2
|
+
import type { SeriesResource } from "./candles.js";
|
|
3
|
+
import type { SyncContext } from "./sources.js";
|
|
4
|
+
export declare class OrderHistory implements SeriesResource<Order> {
|
|
5
|
+
private readonly ctx;
|
|
6
|
+
private readonly account;
|
|
7
|
+
private readonly query;
|
|
8
|
+
/** Maps a WS order's token pair to its orderbook id (see rebuild). */
|
|
9
|
+
private readonly resolveOrderbookId?;
|
|
10
|
+
private readonly base;
|
|
11
|
+
private handle;
|
|
12
|
+
private readonly byId;
|
|
13
|
+
private nextCursor;
|
|
14
|
+
private sub;
|
|
15
|
+
private watermarkUs;
|
|
16
|
+
private alive;
|
|
17
|
+
private _hasMore;
|
|
18
|
+
private _loading;
|
|
19
|
+
private subRetries;
|
|
20
|
+
private retryTimer?;
|
|
21
|
+
constructor(ctx: SyncContext, account: Address, query?: OrdersQuery,
|
|
22
|
+
/** Maps a WS order's token pair to its orderbook id (see rebuild). */
|
|
23
|
+
resolveOrderbookId?: ((pair: {
|
|
24
|
+
base: Address;
|
|
25
|
+
quote: Address;
|
|
26
|
+
}) => MarketId | undefined) | undefined);
|
|
27
|
+
get(): Order[] | undefined;
|
|
28
|
+
subscribe(listener: () => void): () => void;
|
|
29
|
+
ready(): Promise<Order[]>;
|
|
30
|
+
get error(): Error | undefined;
|
|
31
|
+
hasMore(): boolean;
|
|
32
|
+
loading(): boolean;
|
|
33
|
+
destroy(): void;
|
|
34
|
+
setWindow(): void;
|
|
35
|
+
loadOlder(): Promise<void>;
|
|
36
|
+
private fetchFirstPage;
|
|
37
|
+
private seed;
|
|
38
|
+
/**
|
|
39
|
+
* Subscription rejected — re-seed then resubscribe. Backed off (and capped),
|
|
40
|
+
* so a server that keeps rejecting can't spin this into a tight re-seed /
|
|
41
|
+
* resubscribe loop. After a couple of failures we drop `since` (the likely
|
|
42
|
+
* culprit) and just stream live. Reset once live data flows (`onUpdates`).
|
|
43
|
+
*/
|
|
44
|
+
private onSubError;
|
|
45
|
+
private onUpdates;
|
|
46
|
+
private applyUpdate;
|
|
47
|
+
private rebuild;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=orders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orders.d.ts","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAe,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAM7F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,qBAAa,YAAa,YAAW,cAAc,CAAC,KAAK,CAAC;IActD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,sEAAsE;IACtE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC;IAjBtC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwB;IAC7C,OAAO,CAAC,MAAM,CAAsC;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4B;IACjD,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,GAAG,CAA2B;IACtC,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,UAAU,CAAC,CAAgC;gBAGhC,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,OAAO,EAChB,KAAK,GAAE,WAAgB;IACxC,sEAAsE;IACrD,kBAAkB,CAAC,GAAE,CAAC,IAAI,EAAE;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,KAAK,QAAQ,GAAG,SAAS,aAAA;IAkBzG,GAAG,IAAI,KAAK,EAAE,GAAG,SAAS;IAC1B,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAC3C,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IACzB,IAAI,KAAK,IAAI,KAAK,GAAG,SAAS,CAA4B;IAC1D,OAAO,IAAI,OAAO;IAClB,OAAO,IAAI,OAAO;IAClB,OAAO,IAAI,IAAI;IAEf,SAAS,IAAI,IAAI;IAEX,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;YAoBlB,cAAc;IAgB5B,OAAO,CAAC,IAAI;IAgBZ;;;;;OAKG;IACH,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,WAAW;IA8CnB,OAAO,CAAC,OAAO;CAgBhB"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// OrderHistory: a SeriesResource<Order> seeded from the warm first page, kept
|
|
2
|
+
// live by the pod_orders stream (bidder-filtered, `since`-resumed, all update
|
|
3
|
+
// variants incl. `modified`), and paged backwards by cursor for deep history.
|
|
4
|
+
//
|
|
5
|
+
// Reconnect: on every (re)connect we re-seed the first page (authoritative open
|
|
6
|
+
// orders) and refresh `since` from its watermark; the WS auto-resubscribes, and
|
|
7
|
+
// if `since` is too old (down too long) `onError` re-seeds and resubscribes.
|
|
8
|
+
import { dec, WAD } from "../codec/units.js";
|
|
9
|
+
import { decodeOrder } from "../codec/decode.js";
|
|
10
|
+
import { BaseResource } from "../stores/resource.js";
|
|
11
|
+
export class OrderHistory {
|
|
12
|
+
ctx;
|
|
13
|
+
account;
|
|
14
|
+
query;
|
|
15
|
+
resolveOrderbookId;
|
|
16
|
+
base;
|
|
17
|
+
handle;
|
|
18
|
+
byId = new Map();
|
|
19
|
+
nextCursor = null;
|
|
20
|
+
sub;
|
|
21
|
+
watermarkUs = 0;
|
|
22
|
+
alive = false;
|
|
23
|
+
_hasMore = true;
|
|
24
|
+
_loading = false;
|
|
25
|
+
subRetries = 0;
|
|
26
|
+
retryTimer;
|
|
27
|
+
constructor(ctx, account, query = {},
|
|
28
|
+
/** Maps a WS order's token pair to its orderbook id (see rebuild). */
|
|
29
|
+
resolveOrderbookId) {
|
|
30
|
+
this.ctx = ctx;
|
|
31
|
+
this.account = account;
|
|
32
|
+
this.query = query;
|
|
33
|
+
this.resolveOrderbookId = resolveOrderbookId;
|
|
34
|
+
this.base = new BaseResource((h) => {
|
|
35
|
+
this.handle = h;
|
|
36
|
+
this.alive = true;
|
|
37
|
+
const offOpen = this.ctx.ws.on("open", () => { if (this.alive)
|
|
38
|
+
this.seed(); });
|
|
39
|
+
this.seed(); // initial paint (REST is independent of the socket being open)
|
|
40
|
+
return () => {
|
|
41
|
+
this.alive = false;
|
|
42
|
+
offOpen();
|
|
43
|
+
if (this.retryTimer)
|
|
44
|
+
clearTimeout(this.retryTimer);
|
|
45
|
+
this.sub?.unsubscribe();
|
|
46
|
+
this.sub = undefined;
|
|
47
|
+
this.handle = undefined;
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
get() { return this.base.get(); }
|
|
52
|
+
subscribe(listener) { return this.base.subscribe(listener); }
|
|
53
|
+
ready() { return this.base.ready(); }
|
|
54
|
+
get error() { return this.base.error; }
|
|
55
|
+
hasMore() { return this._hasMore; }
|
|
56
|
+
loading() { return this._loading; }
|
|
57
|
+
destroy() { this.base.destroy(); }
|
|
58
|
+
setWindow() { }
|
|
59
|
+
async loadOlder() {
|
|
60
|
+
if (!this.handle || !this.nextCursor || this._loading)
|
|
61
|
+
return;
|
|
62
|
+
this._loading = true;
|
|
63
|
+
this.rebuild();
|
|
64
|
+
try {
|
|
65
|
+
const page = await this.ctx.rest.orders(this.account, {
|
|
66
|
+
cursor: this.nextCursor,
|
|
67
|
+
limit: this.query.limit ?? 100,
|
|
68
|
+
});
|
|
69
|
+
for (const o of page.orders)
|
|
70
|
+
if (!this.byId.has(o.id))
|
|
71
|
+
this.byId.set(o.id, o);
|
|
72
|
+
this.nextCursor = page.nextCursor;
|
|
73
|
+
this._hasMore = page.nextCursor !== null;
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
this._loading = false;
|
|
77
|
+
this.rebuild();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// --- internals ---
|
|
81
|
+
async fetchFirstPage() {
|
|
82
|
+
try {
|
|
83
|
+
const page = await this.ctx.rest.orders(this.account, { limit: this.query.limit ?? 100 });
|
|
84
|
+
if (!this.alive)
|
|
85
|
+
return false;
|
|
86
|
+
for (const o of page.orders)
|
|
87
|
+
this.byId.set(o.id, o);
|
|
88
|
+
this.nextCursor = page.nextCursor;
|
|
89
|
+
this._hasMore = page.nextCursor !== null;
|
|
90
|
+
this.watermarkUs = page.solutionNow * 1000;
|
|
91
|
+
this.rebuild();
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
if (this.byId.size === 0)
|
|
96
|
+
this.handle?.fail(e);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
seed() {
|
|
101
|
+
void this.fetchFirstPage().then((ok) => {
|
|
102
|
+
if (!ok || !this.alive)
|
|
103
|
+
return;
|
|
104
|
+
if (!this.sub) {
|
|
105
|
+
this.sub = this.ctx.ws.subscribe("pod_orders", { account: this.account, since: this.watermarkUs }, (r) => this.onUpdates(r), () => this.onSubError());
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.sub.update({ since: this.watermarkUs }); // refresh for the next reconnect
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Subscription rejected — re-seed then resubscribe. Backed off (and capped),
|
|
114
|
+
* so a server that keeps rejecting can't spin this into a tight re-seed /
|
|
115
|
+
* resubscribe loop. After a couple of failures we drop `since` (the likely
|
|
116
|
+
* culprit) and just stream live. Reset once live data flows (`onUpdates`).
|
|
117
|
+
*/
|
|
118
|
+
onSubError() {
|
|
119
|
+
if (!this.alive)
|
|
120
|
+
return;
|
|
121
|
+
this.subRetries++;
|
|
122
|
+
const delay = Math.min(30_000, 500 * 2 ** (this.subRetries - 1));
|
|
123
|
+
if (this.retryTimer)
|
|
124
|
+
clearTimeout(this.retryTimer);
|
|
125
|
+
this.retryTimer = setTimeout(() => {
|
|
126
|
+
void this.fetchFirstPage().then((ok) => {
|
|
127
|
+
if (!ok || !this.alive)
|
|
128
|
+
return;
|
|
129
|
+
this.sub?.update({ since: this.subRetries > 2 ? undefined : this.watermarkUs });
|
|
130
|
+
this.sub?.resubscribe();
|
|
131
|
+
});
|
|
132
|
+
}, delay);
|
|
133
|
+
}
|
|
134
|
+
onUpdates(result) {
|
|
135
|
+
this.subRetries = 0; // live data flowing → subscription is healthy
|
|
136
|
+
const updates = (Array.isArray(result) ? result : [result]);
|
|
137
|
+
for (const u of updates)
|
|
138
|
+
this.applyUpdate(u);
|
|
139
|
+
this.rebuild();
|
|
140
|
+
}
|
|
141
|
+
applyUpdate(u) {
|
|
142
|
+
switch (u.type) {
|
|
143
|
+
case "new":
|
|
144
|
+
case "invalid": {
|
|
145
|
+
const o = decodeOrder(u);
|
|
146
|
+
this.byId.set(o.id, o);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case "fill": {
|
|
150
|
+
const e = this.byId.get(u.order_id);
|
|
151
|
+
if (e) {
|
|
152
|
+
e.filledBase = dec(u.filled_base_amount);
|
|
153
|
+
e.filledQuote = dec(u.filled_quote_amount);
|
|
154
|
+
e.fee = dec(u.fee);
|
|
155
|
+
e.status = u.status;
|
|
156
|
+
if (u.effective_price != null)
|
|
157
|
+
e.effectivePrice = dec(u.effective_price);
|
|
158
|
+
// Keep the per-fill breakdown live too. The push carries this fill's
|
|
159
|
+
// base/quote delta but no timestamp — receipt time is close enough.
|
|
160
|
+
const base = dec(u.base_amount);
|
|
161
|
+
const quote = dec(u.quote_amount);
|
|
162
|
+
e.fills = [...e.fills, { base, quote, price: base > 0n ? (quote * WAD) / base : 0n, time: Date.now() }];
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case "modified": {
|
|
167
|
+
const e = this.byId.get(u.order_id);
|
|
168
|
+
if (e) {
|
|
169
|
+
e.price = dec(u.new_price);
|
|
170
|
+
const sign = e.initialSize < 0n ? -1n : 1n;
|
|
171
|
+
e.initialSize = sign * dec(u.new_size); // new_size is an unsigned magnitude
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case "expired": {
|
|
176
|
+
const e = this.byId.get(u.order_id);
|
|
177
|
+
if (e)
|
|
178
|
+
e.status = "expired";
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case "canceled": {
|
|
182
|
+
const e = this.byId.get(u.order_id);
|
|
183
|
+
if (e)
|
|
184
|
+
e.status = "canceled";
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
rebuild() {
|
|
190
|
+
if (!this.handle)
|
|
191
|
+
return;
|
|
192
|
+
// WS stream orders carry a token `pair` instead of orderbook_id — resolve
|
|
193
|
+
// it here so consumers can always filter by market. Retried every rebuild,
|
|
194
|
+
// so orders that streamed in before the markets list loaded still resolve.
|
|
195
|
+
if (this.resolveOrderbookId) {
|
|
196
|
+
for (const o of this.byId.values()) {
|
|
197
|
+
if (o.orderbookId === undefined && o.pair)
|
|
198
|
+
o.orderbookId = this.resolveOrderbookId(o.pair);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let arr = [...this.byId.values()];
|
|
202
|
+
if (this.query.status)
|
|
203
|
+
arr = arr.filter((o) => o.status === this.query.status);
|
|
204
|
+
if (this.query.orderbookId)
|
|
205
|
+
arr = arr.filter((o) => o.orderbookId === this.query.orderbookId);
|
|
206
|
+
arr.sort((a, b) => b.deadlineMs - a.deadlineMs || b.nonce - a.nonce);
|
|
207
|
+
this.handle.set(arr);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=orders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../src/sync/orders.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,6EAA6E;AAI7E,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAuB,MAAM,uBAAuB,CAAC;AAK1E,MAAM,OAAO,YAAY;IAcJ;IACA;IACA;IAEA;IAjBF,IAAI,CAAwB;IACrC,MAAM,CAAsC;IACnC,IAAI,GAAG,IAAI,GAAG,EAAiB,CAAC;IACzC,UAAU,GAAkB,IAAI,CAAC;IACjC,GAAG,CAA2B;IAC9B,WAAW,GAAG,CAAC,CAAC;IAChB,KAAK,GAAG,KAAK,CAAC;IACd,QAAQ,GAAG,IAAI,CAAC;IAChB,QAAQ,GAAG,KAAK,CAAC;IACjB,UAAU,GAAG,CAAC,CAAC;IACf,UAAU,CAAiC;IAEnD,YACmB,GAAgB,EAChB,OAAgB,EAChB,QAAqB,EAAE;IACxC,sEAAsE;IACrD,kBAAsF;QAJtF,QAAG,GAAH,GAAG,CAAa;QAChB,YAAO,GAAP,OAAO,CAAS;QAChB,UAAK,GAAL,KAAK,CAAkB;QAEvB,uBAAkB,GAAlB,kBAAkB,CAAoE;QAEvG,IAAI,CAAC,IAAI,GAAG,IAAI,YAAY,CAAU,CAAC,CAAC,EAAE,EAAE;YAC1C,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;YAChB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;YAClB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG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AAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC7F,CAAC;QACH,CAAC;QACD,IAAI,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QAClC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM;YAAE,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC/E,IAAI,IAAI,CAAC,KAAK,CAAC,WAAW;YAAE,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,KAAK,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC9F,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QACrE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions-live.d.ts","sourceRoot":"","sources":["../../src/sync/positions-live.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,MAAM,EAAsB,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAOxF,wBAAgB,eAAe,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,iBAAiB,CAyE7F"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Live position recompute, replicating the backend's exact account math so the
|
|
2
|
+
// mark- AND funding-driven fields stay correct between pod_positions snapshots.
|
|
3
|
+
//
|
|
4
|
+
// A snapshot is only pushed when the account is touched, but mark moves every
|
|
5
|
+
// tick and funding accrues every tick. We recompute from the live marks and the
|
|
6
|
+
// live funding accumulator (funding_index, streamed via pod_markets), holding
|
|
7
|
+
// the snapshot's frozen inputs (native cash, size, entry, entry_funding, cost
|
|
8
|
+
// basis, resting-order escrow).
|
|
9
|
+
//
|
|
10
|
+
// EXACT replication of pod — see the cross-reference notes in pod at:
|
|
11
|
+
// trading/src/perps/mod.rs `cross_budgets` (withdrawable / equity)
|
|
12
|
+
// trading/src/perps/mod.rs `funding_accrued` (funding)
|
|
13
|
+
// trading/src/perps/config.rs `into_market` (margin-rate derivation)
|
|
14
|
+
// Integer semantics match trading/src/decimal.rs:
|
|
15
|
+
// mul(a,b) = (a*b)/1e18 truncated toward zero (Decimal `*`)
|
|
16
|
+
// mulFloor(a,b) = (a*b)/1e18 rounded toward -infinity (`mul_floor`)
|
|
17
|
+
// mulDivCeil(a,b,d) = (a*b)/d, positive results rounded up (`mul_div_ceil`)
|
|
18
|
+
// im = 1e18 / max_leverage ; mm = im / 2
|
|
19
|
+
// funding_accrued = mulDivCeil(funding_index - entry_funding, size, window_us*1e18)
|
|
20
|
+
// cash_with_funding = native_cash - Σ funding_accrued
|
|
21
|
+
import { WAD } from "../codec/units.js";
|
|
22
|
+
import { imRate, mul, mulDivCeil, mulFloor } from "../codec/fixed.js";
|
|
23
|
+
const absB = (x) => (x < 0n ? -x : x);
|
|
24
|
+
const maxB = (a, b) => (a > b ? a : b);
|
|
25
|
+
export function enrichPositions(snap, markets) {
|
|
26
|
+
const byId = new Map(markets.map((m) => [m.id, m]));
|
|
27
|
+
let priceUpnl = 0n; // Σ perp price PnL at live mark (excludes funding)
|
|
28
|
+
let priceUpnlSnap = 0n; // Σ perp price PnL recorded in the snapshot
|
|
29
|
+
let fundingLiveTotal = 0n; // Σ funding accrued at the live funding_index
|
|
30
|
+
let fundingSnapTotal = 0n; // Σ funding accrued in the snapshot
|
|
31
|
+
let im = 0n; // Σ initial-margin requirement at live notional
|
|
32
|
+
let mm = 0n; // Σ maintenance-margin requirement at live notional
|
|
33
|
+
let dSpot = 0n; // change in spot mark value vs snapshot (slope = balance)
|
|
34
|
+
const positions = snap.positions.map((p) => {
|
|
35
|
+
const market = p.orderbookId ? byId.get(p.orderbookId) : undefined;
|
|
36
|
+
if (p.kind === "perp") {
|
|
37
|
+
// Server-banded mark from pod_markets. Before a market's first trade the
|
|
38
|
+
// mark is unseeded (0) — fall back to the entry price so uPnL is 0 rather
|
|
39
|
+
// than a spurious −entry·size that would wrongly zero withdrawable cash.
|
|
40
|
+
const liveMark = market?.markPrice && market.markPrice > 0n ? market.markPrice : p.markPrice;
|
|
41
|
+
const mark = liveMark > 0n ? liveMark : p.entryPrice;
|
|
42
|
+
const upnl = mulFloor(mark - p.entryPrice, p.size); // pure price drift (signed)
|
|
43
|
+
const sizeQuote = mul(absB(p.size), mark); // = notional
|
|
44
|
+
const imr = imRate(market?.maxLeverage ?? 0);
|
|
45
|
+
im += mul(sizeQuote, imr);
|
|
46
|
+
mm += mul(sizeQuote, imr / 2n);
|
|
47
|
+
priceUpnl += upnl;
|
|
48
|
+
priceUpnlSnap += p.unrealizedPnl;
|
|
49
|
+
// Live funding: mul_div_ceil(funding_index - entry_funding, size, window_us*1e18).
|
|
50
|
+
// funding_index (= market.funding) streams via pod_markets; if unavailable,
|
|
51
|
+
// hold the snapshot value (re-aligned by the periodic REST resync).
|
|
52
|
+
let funding = p.fundingAccrued;
|
|
53
|
+
if (market?.fundingIndex !== undefined && market.fundingWindowUs) {
|
|
54
|
+
funding = mulDivCeil(market.fundingIndex - p.entryFunding, p.size, BigInt(market.fundingWindowUs) * WAD);
|
|
55
|
+
}
|
|
56
|
+
fundingLiveTotal += funding;
|
|
57
|
+
fundingSnapTotal += p.fundingAccrued;
|
|
58
|
+
return { ...p, markPrice: mark, unrealizedPnl: upnl, notional: sizeQuote, fundingAccrued: funding };
|
|
59
|
+
}
|
|
60
|
+
const mark = market?.lastClearingPrice ?? market?.markPrice ?? p.markPrice;
|
|
61
|
+
dSpot += mul(p.balance, mark) - mul(p.balance, p.markPrice);
|
|
62
|
+
return { ...p, markPrice: mark, unrealizedPnl: p.unrealizedPnl + (mul(p.balance, mark) - mul(p.balance, p.markPrice)) };
|
|
63
|
+
});
|
|
64
|
+
// native_cash is frozen between snapshots; back it out of the snapshot's
|
|
65
|
+
// funding-adjusted cash, then re-apply funding at the live index.
|
|
66
|
+
const nativeCash = snap.cash + fundingSnapTotal;
|
|
67
|
+
const cashWithFunding = nativeCash - fundingLiveTotal;
|
|
68
|
+
// EXACT (perp-only, integer-faithful): equity = cash_with_funding + Σ price PnL;
|
|
69
|
+
// withdrawable = max(0, equity - Σ IM), forced to 0 below maintenance margin.
|
|
70
|
+
const perpsEquity = cashWithFunding + priceUpnl;
|
|
71
|
+
const withdrawableCash = perpsEquity < mm ? 0n : maxB(0n, perpsEquity - im);
|
|
72
|
+
// account_value moves with perp equity (incl. funding) AND spot mark; total
|
|
73
|
+
// uPnL is pure price drift only (funding is reported separately). Re-baseline
|
|
74
|
+
// off the snapshot with exact slopes, leaving the frozen resting escrow alone.
|
|
75
|
+
const dPriceUpnl = priceUpnl - priceUpnlSnap;
|
|
76
|
+
return {
|
|
77
|
+
positions,
|
|
78
|
+
cash: cashWithFunding,
|
|
79
|
+
totalRealizedPnl: snap.totalRealizedPnl,
|
|
80
|
+
perpsEquity,
|
|
81
|
+
withdrawableCash,
|
|
82
|
+
totalUnrealizedPnl: snap.totalUnrealizedPnl + dPriceUpnl + dSpot,
|
|
83
|
+
accountValue: snap.accountValue + (perpsEquity - snap.perpsEquity) + dSpot,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=positions-live.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions-live.js","sourceRoot":"","sources":["../../src/sync/positions-live.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,gFAAgF;AAChF,EAAE;AACF,8EAA8E;AAC9E,gFAAgF;AAChF,8EAA8E;AAC9E,8EAA8E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C,EAAE,EAAE,WAAW,GAAG,EAAE,CAAC,CAAC;IAE5E,4EAA4E;IAC5E,8EAA8E;IAC9E,+EAA+E;IAC/E,MAAM,UAAU,GAAG,SAAS,GAAG,aAAa,CAAC;IAC7C,OAAO;QACL,SAAS;QACT,IAAI,EAAE,eAAe;QACrB,gBAAgB,EAAE,IAAI,CAAC,gBAAgB;QACvC,WAAW;QACX,gBAAgB;QAChB,kBAAkB,EAAE,IAAI,CAAC,kBAAkB,GAAG,UAAU,GAAG,KAAK;QAChE,YAAY,EAAE,IAAI,CAAC,YAAY,GAAG,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,KAAK;KAC3E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Address, Balances, Market, MarketId, Orderbook, PositionsSnapshot, Status, Trigger } from "../types/public.js";
|
|
2
|
+
import type { PodRestClient } from "../transport/rest.js";
|
|
3
|
+
import type { PodWsClient } from "../transport/ws.js";
|
|
4
|
+
import type { ResourceSource } from "../stores/resource.js";
|
|
5
|
+
export interface SyncContext {
|
|
6
|
+
rest: PodRestClient;
|
|
7
|
+
ws: PodWsClient;
|
|
8
|
+
/** Periodic REST re-alignment interval (ms) for positions; 0 disables. */
|
|
9
|
+
positionResyncMs: number;
|
|
10
|
+
/** Periodic REST re-poll (ms) of market 24h stats; 0 disables. */
|
|
11
|
+
marketResyncMs: number;
|
|
12
|
+
/** Optional persistence for the static markets list (see marketsSource). */
|
|
13
|
+
marketsCache?: MarketsCache;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Host-supplied persistence (e.g. localStorage-backed) for the static markets
|
|
17
|
+
* list, so market-keyed UI can mount immediately on a repeat visit instead of
|
|
18
|
+
* waiting a REST round trip. Only the static `/clob/markets` config is stored —
|
|
19
|
+
* never prices/stats, which always arrive live. Implementations may throw
|
|
20
|
+
* (storage denied, quota); callers ignore failures.
|
|
21
|
+
*/
|
|
22
|
+
export interface MarketsCache {
|
|
23
|
+
get(): string | null;
|
|
24
|
+
set(value: string): void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Seed now — a REST fetch needs no socket, so don't hold first data behind the
|
|
28
|
+
* WS handshake — and re-seed on every (re)connect so state refreshes after any
|
|
29
|
+
* downtime. On a cold start the connect completes shortly after the immediate
|
|
30
|
+
* seed and triggers one extra seed; that duplicate is what guarantees a seed
|
|
31
|
+
* at/after the moment the WS subscription goes live, exactly as before.
|
|
32
|
+
*/
|
|
33
|
+
export declare function seedNowAndOnReconnect(ws: PodWsClient, fn: () => void): () => void;
|
|
34
|
+
export declare function statusSource({ rest, ws }: SyncContext): ResourceSource<Status>;
|
|
35
|
+
export declare function marketsSource({ rest, ws, marketResyncMs, marketsCache }: SyncContext): ResourceSource<Market[]>;
|
|
36
|
+
export declare function orderbookSource({ rest, ws }: SyncContext, id: MarketId, depth?: number): ResourceSource<Orderbook>;
|
|
37
|
+
export declare function positionsSource({ rest, ws, positionResyncMs }: SyncContext, account: Address): ResourceSource<PositionsSnapshot>;
|
|
38
|
+
export declare function balancesSource({ rest, ws, positionResyncMs }: SyncContext, account: Address): ResourceSource<Balances>;
|
|
39
|
+
export declare function triggersSource({ ws }: SyncContext, account: Address): ResourceSource<Trigger[]>;
|
|
40
|
+
//# sourceMappingURL=sources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.d.ts","sourceRoot":"","sources":["../../src/sync/sources.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EACV,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,EAAE,OAAO,EACnF,MAAM,oBAAoB,CAAC;AAO5B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE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