@lifi/perps-sdk-provider-ondo 36.0.0 → 36.2.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 +34 -9
- package/dist/cjs/OndoProvider.d.ts.map +1 -1
- package/dist/cjs/OndoProvider.js +7 -4
- package/dist/cjs/OndoProvider.js.map +1 -1
- package/dist/cjs/auth/OndoTokenStore.d.ts +1 -1
- package/dist/cjs/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/cjs/auth/OndoTokenStore.js +9 -2
- package/dist/cjs/auth/OndoTokenStore.js.map +1 -1
- package/dist/cjs/utils/buildFee.d.ts +4 -0
- package/dist/cjs/utils/buildFee.d.ts.map +1 -0
- package/dist/cjs/utils/buildFee.js +22 -0
- package/dist/cjs/utils/buildFee.js.map +1 -0
- package/dist/cjs/utils/fillLeverages.d.ts +4 -0
- package/dist/cjs/utils/fillLeverages.d.ts.map +1 -0
- package/dist/cjs/utils/fillLeverages.js +39 -0
- package/dist/cjs/utils/fillLeverages.js.map +1 -0
- package/dist/cjs/utils/mapFill.d.ts.map +1 -1
- package/dist/cjs/utils/mapFill.js +4 -7
- package/dist/cjs/utils/mapFill.js.map +1 -1
- package/dist/cjs/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/cjs/utils/mapPortfolioHistory.js +18 -3
- package/dist/cjs/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/cjs/websocket/OndoWsProvider.d.ts +8 -1
- package/dist/cjs/websocket/OndoWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/OndoWsProvider.js +122 -9
- package/dist/cjs/websocket/OndoWsProvider.js.map +1 -1
- package/dist/esm/OndoProvider.d.ts.map +1 -1
- package/dist/esm/OndoProvider.js +8 -5
- package/dist/esm/OndoProvider.js.map +1 -1
- package/dist/esm/auth/OndoTokenStore.d.ts +1 -1
- package/dist/esm/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/esm/auth/OndoTokenStore.js +9 -2
- package/dist/esm/auth/OndoTokenStore.js.map +1 -1
- package/dist/esm/utils/buildFee.d.ts +8 -0
- package/dist/esm/utils/buildFee.d.ts.map +1 -0
- package/dist/esm/utils/buildFee.js +22 -0
- package/dist/esm/utils/buildFee.js.map +1 -0
- package/dist/esm/utils/fillLeverages.d.ts +12 -0
- package/dist/esm/utils/fillLeverages.d.ts.map +1 -0
- package/dist/esm/utils/fillLeverages.js +43 -0
- package/dist/esm/utils/fillLeverages.js.map +1 -0
- package/dist/esm/utils/mapFill.d.ts +2 -1
- package/dist/esm/utils/mapFill.d.ts.map +1 -1
- package/dist/esm/utils/mapFill.js +7 -10
- package/dist/esm/utils/mapFill.js.map +1 -1
- package/dist/esm/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/esm/utils/mapPortfolioHistory.js +19 -4
- package/dist/esm/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/esm/websocket/OndoWsProvider.d.ts +18 -2
- package/dist/esm/websocket/OndoWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/OndoWsProvider.js +136 -11
- package/dist/esm/websocket/OndoWsProvider.js.map +1 -1
- package/dist/types/OndoProvider.d.ts.map +1 -1
- package/dist/types/auth/OndoTokenStore.d.ts +1 -1
- package/dist/types/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/types/utils/buildFee.d.ts +8 -0
- package/dist/types/utils/buildFee.d.ts.map +1 -0
- package/dist/types/utils/fillLeverages.d.ts +12 -0
- package/dist/types/utils/fillLeverages.d.ts.map +1 -0
- package/dist/types/utils/mapFill.d.ts +2 -1
- package/dist/types/utils/mapFill.d.ts.map +1 -1
- package/dist/types/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/types/websocket/OndoWsProvider.d.ts +18 -2
- package/dist/types/websocket/OndoWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/OndoProvider.ts +15 -4
- package/src/auth/OndoTokenStore.ts +13 -2
- package/src/utils/buildFee.ts +35 -0
- package/src/utils/fillLeverages.ts +67 -0
- package/src/utils/mapFill.ts +27 -30
- package/src/utils/mapPortfolioHistory.ts +29 -4
- package/src/websocket/OndoWsProvider.ts +192 -12
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DecimalString, Fill } from '@lifi/perps-types';
|
|
2
|
+
import type { OndoApiClient } from './apiClient.js';
|
|
3
|
+
/**
|
|
4
|
+
* Current leverage setting of each market that a fill with a `closedSize`
|
|
5
|
+
* trades, keyed by `Market.id`. One `/v1/perps/leverage` read covers every
|
|
6
|
+
* market. A failed read gives an empty map, and a market without a positive
|
|
7
|
+
* leverage row stays out, so the ROE of its fills stays absent.
|
|
8
|
+
*
|
|
9
|
+
* @throws The abort reason when `signal` aborts.
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchFillLeverages: (client: OndoApiClient, authToken: string, fills: readonly Fill[], signal?: AbortSignal) => Promise<Map<string, DecimalString>>;
|
|
12
|
+
//# sourceMappingURL=fillLeverages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fillLeverages.d.ts","sourceRoot":"","sources":["../../../src/utils/fillLeverages.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AAG5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAInD;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAC7B,QAAQ,aAAa,EACrB,WAAW,MAAM,EACjB,OAAO,SAAS,IAAI,EAAE,EACtB,SAAS,WAAW,KACnB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAyCpC,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createWarnOnce, isDecimalString, isDecimalStringGreaterThan, } from '@lifi/perps-sdk';
|
|
2
|
+
import { ONDO_PROVIDER_KEY } from '../constants.js';
|
|
3
|
+
const warnLeverageReadOnce = createWarnOnce();
|
|
4
|
+
/**
|
|
5
|
+
* Current leverage setting of each market that a fill with a `closedSize`
|
|
6
|
+
* trades, keyed by `Market.id`. One `/v1/perps/leverage` read covers every
|
|
7
|
+
* market. A failed read gives an empty map, and a market without a positive
|
|
8
|
+
* leverage row stays out, so the ROE of its fills stays absent.
|
|
9
|
+
*
|
|
10
|
+
* @throws The abort reason when `signal` aborts.
|
|
11
|
+
*/
|
|
12
|
+
export const fetchFillLeverages = async (client, authToken, fills, signal) => {
|
|
13
|
+
const marketIds = new Set(fills.flatMap((fill) => fill.closedSize === undefined ? [] : [fill.market.id]));
|
|
14
|
+
if (marketIds.size === 0) {
|
|
15
|
+
return new Map();
|
|
16
|
+
}
|
|
17
|
+
let rows;
|
|
18
|
+
try {
|
|
19
|
+
rows = await client.get('/v1/perps/leverage', {
|
|
20
|
+
authToken,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
signal?.throwIfAborted();
|
|
25
|
+
warnLeverageReadOnce('leverage', `[${ONDO_PROVIDER_KEY}] leverage read failed; fills carry no returnOnEquity: ${String(error)}`);
|
|
26
|
+
return new Map();
|
|
27
|
+
}
|
|
28
|
+
const leverages = new Map();
|
|
29
|
+
for (const row of rows) {
|
|
30
|
+
if (marketIds.has(row.market) &&
|
|
31
|
+
isDecimalString(row.leverage) &&
|
|
32
|
+
isDecimalStringGreaterThan(row.leverage, '0')) {
|
|
33
|
+
leverages.set(row.market, row.leverage);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (const marketId of marketIds) {
|
|
37
|
+
if (!leverages.has(marketId)) {
|
|
38
|
+
warnLeverageReadOnce(marketId, `[${ONDO_PROVIDER_KEY}] no positive leverage for market '${marketId}'; its fills carry no returnOnEquity`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return leverages;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=fillLeverages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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,QAAQ,CAAC;YAC7B,0BAA0B,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,EAC7C,CAAC;YACD,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAA;QACzC,CAAC;IACH,CAAC;IACD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC7B,oBAAoB,CAClB,QAAQ,EACR,IAAI,iBAAiB,sCAAsC,QAAQ,sCAAsC,CAC1G,CAAA;QACH,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA"}
|
|
@@ -4,7 +4,8 @@ import type { OndoFill } from '../types/wire.js';
|
|
|
4
4
|
* Map a raw Ondo fill to the generic {@link Fill}. The fee is netted against
|
|
5
5
|
* Ondo's `feeRebate`; when the wire `direction` is absent the classification
|
|
6
6
|
* is the bare fill side. Size and price are the raw venue strings. A fill
|
|
7
|
-
* with an invalid time gives `undefined`.
|
|
7
|
+
* with an invalid time gives `undefined`. A closing fill gets `closedSize`
|
|
8
|
+
* and `entryPrice`, but no `returnOnEquity`: the fill carries no leverage.
|
|
8
9
|
*
|
|
9
10
|
* @param market - Backend-resolved market identity for `fill.market`.
|
|
10
11
|
* @public
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapFill.d.ts","sourceRoot":"","sources":["../../../src/utils/mapFill.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAE5D,OAAO,KAAK,EAAE,QAAQ,EAAqB,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"mapFill.d.ts","sourceRoot":"","sources":["../../../src/utils/mapFill.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAE5D,OAAO,KAAK,EAAE,QAAQ,EAAqB,MAAM,kBAAkB,CAAA;AAcnE;;;;;;;;;GASG;AACH,eAAO,MAAM,OAAO,GAClB,MAAM,QAAQ,EACd,QAAQ,aAAa,KACpB,IAAI,GAAG,SAiCT,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildFill } from '@lifi/perps-sdk';
|
|
2
2
|
import { FillClassification, LiquidityRole, OrderSide } from '@lifi/perps-types';
|
|
3
|
+
import { buildFee } from './buildFee.js';
|
|
3
4
|
import { rowTimestampToIsoStringOrWarn } from './venueValues.js';
|
|
4
5
|
const DIRECTION_CLASSIFICATIONS = {
|
|
5
6
|
openLong: FillClassification.OPENED_LONG,
|
|
@@ -9,13 +10,12 @@ const DIRECTION_CLASSIFICATIONS = {
|
|
|
9
10
|
flipLongToShort: FillClassification.SWITCHED_SHORT,
|
|
10
11
|
flipShortToLong: FillClassification.SWITCHED_LONG,
|
|
11
12
|
};
|
|
12
|
-
/** Ondo's `fee` net of `feeRebate`, or `undefined` when either is malformed. */
|
|
13
|
-
const netFeeAmount = (fill) => safeSubtractDecimalString(fill.fee, fill.feeRebate ?? '0');
|
|
14
13
|
/**
|
|
15
14
|
* Map a raw Ondo fill to the generic {@link Fill}. The fee is netted against
|
|
16
15
|
* Ondo's `feeRebate`; when the wire `direction` is absent the classification
|
|
17
16
|
* is the bare fill side. Size and price are the raw venue strings. A fill
|
|
18
|
-
* with an invalid time gives `undefined`.
|
|
17
|
+
* with an invalid time gives `undefined`. A closing fill gets `closedSize`
|
|
18
|
+
* and `entryPrice`, but no `returnOnEquity`: the fill carries no leverage.
|
|
19
19
|
*
|
|
20
20
|
* @param market - Backend-resolved market identity for `fill.market`.
|
|
21
21
|
* @public
|
|
@@ -25,8 +25,7 @@ export const mapFill = (fill, market) => {
|
|
|
25
25
|
if (createdAt === undefined) {
|
|
26
26
|
return undefined;
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
28
|
+
return buildFill({
|
|
30
29
|
id: fill.id,
|
|
31
30
|
orderId: fill.orderId,
|
|
32
31
|
clientOrderId: fill.clientOrderId,
|
|
@@ -36,9 +35,7 @@ export const mapFill = (fill, market) => {
|
|
|
36
35
|
price: fill.price,
|
|
37
36
|
liquidity: fill.isMaker ? LiquidityRole.MAKER : LiquidityRole.TAKER,
|
|
38
37
|
// Ondo charges the fill fee in the market's quote asset.
|
|
39
|
-
fee:
|
|
40
|
-
? undefined
|
|
41
|
-
: { amount: feeAmount, asset: market.quoteAsset.displaySymbol },
|
|
38
|
+
fee: buildFee(fill, market.quoteAsset.displaySymbol),
|
|
42
39
|
realizedPnl: fill.pnl,
|
|
43
40
|
classification: fill.direction !== undefined
|
|
44
41
|
? DIRECTION_CLASSIFICATIONS[fill.direction]
|
|
@@ -46,6 +43,6 @@ export const mapFill = (fill, market) => {
|
|
|
46
43
|
? FillClassification.BUY
|
|
47
44
|
: FillClassification.SELL,
|
|
48
45
|
createdAt,
|
|
49
|
-
};
|
|
46
|
+
}, undefined);
|
|
50
47
|
};
|
|
51
48
|
//# sourceMappingURL=mapFill.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapFill.js","sourceRoot":"","sources":["../../../src/utils/mapFill.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"mapFill.js","sourceRoot":"","sources":["../../../src/utils/mapFill.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEhF,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAA;AAEhE,MAAM,yBAAyB,GAC7B;IACE,QAAQ,EAAE,kBAAkB,CAAC,WAAW;IACxC,S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,CAAC,GAAG;gBACxB,CAAC,CAAC,kBAAkB,CAAC,IAAI;QAC/B,SAAS;KACV,EACD,SAAS,CACV,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapPortfolioHistory.d.ts","sourceRoot":"","sources":["../../../src/utils/mapPortfolioHistory.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mapPortfolioHistory.d.ts","sourceRoot":"","sources":["../../../src/utils/mapPortfolioHistory.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAEV,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,KAAK,EACV,uBAAuB,EACvB,oBAAoB,EACrB,MAAM,kBAAkB,CAAA;AAoDzB;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,GAC9B,OAAO,qBAAqB,EAC5B,OAAO,uBAAuB,EAAE,EAChC,SAAS,oBAAoB,KAC5B,wBAUF,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { warnSkippedVenueRow } from '@lifi/perps-sdk';
|
|
1
|
+
import { isDecimalString, safeSubtractDecimalString, warnSkippedVenueRow, } from '@lifi/perps-sdk';
|
|
2
2
|
import { ONDO_PROVIDER_KEY } from '../constants.js';
|
|
3
3
|
const summaryVolume = (summary, range) => {
|
|
4
4
|
switch (range) {
|
|
@@ -13,7 +13,7 @@ const summaryVolume = (summary, range) => {
|
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
15
|
const POINT_ROW = 'portfolio point';
|
|
16
|
-
const mapPoint = (point) => {
|
|
16
|
+
const mapPoint = (point, baseline) => {
|
|
17
17
|
const timestamp = Date.parse(point.time);
|
|
18
18
|
if (Number.isNaN(timestamp)) {
|
|
19
19
|
warnSkippedVenueRow(ONDO_PROVIDER_KEY, POINT_ROW, 'time', point.time, {
|
|
@@ -21,8 +21,21 @@ const mapPoint = (point) => {
|
|
|
21
21
|
});
|
|
22
22
|
return undefined;
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
const netFlow = getCumulativeNetFlow(point);
|
|
25
|
+
const netTransfers = baseline === undefined || netFlow === undefined
|
|
26
|
+
? undefined
|
|
27
|
+
: safeSubtractDecimalString(netFlow, baseline);
|
|
28
|
+
return {
|
|
29
|
+
timestamp,
|
|
30
|
+
accountValue: point.marginBalance,
|
|
31
|
+
pnl: point.totalPnL,
|
|
32
|
+
...(netTransfers === undefined ? {} : { netTransfers }),
|
|
33
|
+
};
|
|
25
34
|
};
|
|
35
|
+
const getCumulativeNetFlow = (point) => isDecimalString(point.allTimeDeposits) &&
|
|
36
|
+
isDecimalString(point.allTimeWithdrawals)
|
|
37
|
+
? safeSubtractDecimalString(point.allTimeDeposits, point.allTimeWithdrawals)
|
|
38
|
+
: undefined;
|
|
26
39
|
/**
|
|
27
40
|
* Join the `/v1/portfolio/summary/graph` series with the `/v1/portfolio/summary`
|
|
28
41
|
* totals for `range`. Ondo reports no 24-hour volume, so `volume` is absent
|
|
@@ -31,7 +44,9 @@ const mapPoint = (point) => {
|
|
|
31
44
|
* with an invalid time is skipped.
|
|
32
45
|
*/
|
|
33
46
|
export const mapPortfolioHistory = (range, graph, summary) => {
|
|
34
|
-
const
|
|
47
|
+
const first = graph.find((point) => !Number.isNaN(Date.parse(point.time)));
|
|
48
|
+
const baseline = first === undefined ? undefined : getCumulativeNetFlow(first);
|
|
49
|
+
const points = graph.flatMap((point) => mapPoint(point, baseline) ?? []);
|
|
35
50
|
return {
|
|
36
51
|
range,
|
|
37
52
|
points,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapPortfolioHistory.js","sourceRoot":"","sources":["../../../src/utils/mapPortfolioHistory.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"mapPortfolioHistory.js","sourceRoot":"","sources":["../../../src/utils/mapPortfolioHistory.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,yBAAyB,E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ACxD,CAAA;AACH,CAAC,CAAA;AAED,MAAM,oBAAoB,GAAG,CAC3B,KAA8B,EACV,EAAE,CACtB,eAAe,CAAC,KAAK,CAAC,eAAe,CAAC;IACtC,eAAe,CAAC,KAAK,CAAC,kBAAkB,CAAC;IACvC,CAAC,CAAC,yBAAyB,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,kBAAkB,CAAC;IAC5E,CAAC,CAAC,SAAS,CAAA;AAEf;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,KAA4B,EAC5B,KAAgC,EAChC,OAA6B,EACH,EAAE;IAC5B,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC1E,MAAM,QAAQ,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAA;IAC9E,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAA;IACxE,OAAO;QACL,KAAK;QACL,MAAM;QACN,MAAM,EAAE,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC;QACrC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG;KAC7B,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type PerpsSDKClient, type ProviderGetQuoteParams, type QuoteListener, type StorageAdapter, WsProviderBase, type WsProviderFactory } from '@lifi/perps-sdk';
|
|
2
2
|
import { type Subscription } from '@lifi/perps-types';
|
|
3
|
-
import type
|
|
3
|
+
import { type Address } from 'viem';
|
|
4
4
|
interface SubState {
|
|
5
5
|
/** Subscribe payload minus `op` — re-sent verbatim on every (re)open. */
|
|
6
6
|
frame: {
|
|
@@ -60,6 +60,7 @@ export declare class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
60
60
|
* out of order. `'latest'` coalesces the backlog to one trailing refresh.
|
|
61
61
|
*/
|
|
62
62
|
private readonly accountSummaryChain;
|
|
63
|
+
private readonly fillChain;
|
|
63
64
|
/** Complete (mark-price-bearing) contexts, keyed by venue market symbol. */
|
|
64
65
|
private contexts;
|
|
65
66
|
/** Funding seen before the market's first mark price. */
|
|
@@ -67,6 +68,8 @@ export declare class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
67
68
|
/** The single address this connection's login is bound to. */
|
|
68
69
|
private accountAddress;
|
|
69
70
|
private loginPromise;
|
|
71
|
+
private loginAttempt;
|
|
72
|
+
private readonly rejectedTokens;
|
|
70
73
|
private authGeneration;
|
|
71
74
|
private authBindingRefs;
|
|
72
75
|
private closeGeneration;
|
|
@@ -87,8 +90,18 @@ export declare class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
87
90
|
* false — leaving the binding intact — while any is still live.
|
|
88
91
|
*/
|
|
89
92
|
private releaseBindingIfIdle;
|
|
90
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* Release a channel's binding reservation, including one still opening.
|
|
95
|
+
* The last release cycles the connection only when a login was sent on it,
|
|
96
|
+
* because the venue holds that login until the socket closes.
|
|
97
|
+
*/
|
|
91
98
|
private releaseBinding;
|
|
99
|
+
private authChannelKeys;
|
|
100
|
+
/**
|
|
101
|
+
* The venue login is per connection, so one credential failure stops every
|
|
102
|
+
* private channel of the address.
|
|
103
|
+
*/
|
|
104
|
+
protected resolveChannelKeys(key: string, state: SubState): string[];
|
|
92
105
|
/** Register a wire sub, ref-counting the shared authenticated channels. */
|
|
93
106
|
private acquireWire;
|
|
94
107
|
/** Mirror of {@link acquireWire}: drop the wire sub on its last release. */
|
|
@@ -129,6 +142,9 @@ export declare class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
129
142
|
private readBalance;
|
|
130
143
|
/** Emit the current account summary from the venue balance figures. */
|
|
131
144
|
private emitAccountSummary;
|
|
145
|
+
/** Detach the pending login attempt, if any, and stop its acknowledgement timer. */
|
|
146
|
+
private takeLoginAttempt;
|
|
147
|
+
private rejectLogin;
|
|
132
148
|
protected handleMessage(raw: string): void;
|
|
133
149
|
/** Valid levels of one book side, best first (`-1` bids, `1` asks). */
|
|
134
150
|
private toBookLevels;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OndoWsProvider.d.ts","sourceRoot":"","sources":["../../../src/websocket/OndoWsProvider.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"OndoWsProvider.d.ts","sourceRoot":"","sources":["../../../src/websocket/OndoWsProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EASL,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC3B,KAAK,aAAa,EAGlB,KAAK,cAAc,EAInB,cAAc,EACd,KAAK,iBAAiB,EAGvB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAOL,KAAK,YAAY,EAClB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,KAAK,OAAO,EAAa,MAAM,MAAM,CAAA;AAsE9C,UAAU,QAAQ;IAChB,yEAAyE;IACzE,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IACnE,SAAS,EAAE,OAAO,CAAA;IAClB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,6EAA6E;IAC7E,OAAO,CAAC,EAAE,cAAc,CAAA;CACz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CAAA"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { DecodeChain, getMarketRegistry, isDecimalString, localStorageAdapter, numberToDecimalString, PerpsError, ReconnectingWebSocket, resolveSubscribeQuote, safeCompareDecimalStrings, toMarketDisplay, toPerpsMarketDisplay, WsProviderBase, wsLog, } from '@lifi/perps-sdk';
|
|
1
|
+
import { buildFill, DecodeChain, getMarketRegistry, isDecimalString, localStorageAdapter, numberToDecimalString, PerpsError, ReconnectingWebSocket, resolveSubscribeQuote, safeCompareDecimalStrings, toMarketDisplay, toPerpsMarketDisplay, WsProviderBase, wsLog, } from '@lifi/perps-sdk';
|
|
2
2
|
import { PerpsErrorCode, } from '@lifi/perps-types';
|
|
3
|
+
import { isAddress } from 'viem';
|
|
3
4
|
import { OndoTokenStore } from '../auth/OndoTokenStore.js';
|
|
4
5
|
import { ondoSessionRejectedError } from '../auth/sessionToken.js';
|
|
5
6
|
import { DEFAULT_ONDO_API_URL, DEFAULT_ONDO_WS_URL, ONDO_BASE_FEE_TIER, ONDO_PROVIDER_KEY, } from '../constants.js';
|
|
7
|
+
import { fetchFillLeverages } from '../utils/fillLeverages.js';
|
|
6
8
|
import { isOpenPosition, mapFill, mapOrderUpdates, mapPosition, OndoApiClient, OndoSessionExpiredError, } from '../utils/index.js';
|
|
7
9
|
import { intervalFromBarSpan, mapInterval } from '../utils/ohlcvInterval.js';
|
|
8
10
|
// Public channels (per market, demuxed from the frame's `market` field):
|
|
@@ -29,6 +31,8 @@ const ONDO_AUTH_FANOUT = [
|
|
|
29
31
|
...Object.keys(ONDO_AUTH_CHANNEL),
|
|
30
32
|
'accountSummary',
|
|
31
33
|
];
|
|
34
|
+
/** Bound on the venue's answer to a `login` op. An unmapped error leaves no other exit. */
|
|
35
|
+
const ONDO_LOGIN_ACK_TIMEOUT_MS = 10_000;
|
|
32
36
|
const isAuthChannel = (channel) => channel in ONDO_AUTH_CHANNEL;
|
|
33
37
|
const BOOK_LEVEL_ROW = 'order book level';
|
|
34
38
|
/**
|
|
@@ -64,6 +68,8 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
64
68
|
* out of order. `'latest'` coalesces the backlog to one trailing refresh.
|
|
65
69
|
*/
|
|
66
70
|
accountSummaryChain = new DecodeChain('latest', (err) => wsLog.handlerFailure(this.providerKey, err));
|
|
71
|
+
// A closing fill waits for a leverage read, so fills frames emit in order.
|
|
72
|
+
fillChain = new DecodeChain('every', (err) => wsLog.handlerFailure(this.providerKey, err));
|
|
67
73
|
/** Complete (mark-price-bearing) contexts, keyed by venue market symbol. */
|
|
68
74
|
contexts = {};
|
|
69
75
|
/** Funding seen before the market's first mark price. */
|
|
@@ -71,6 +77,8 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
71
77
|
/** The single address this connection's login is bound to. */
|
|
72
78
|
accountAddress;
|
|
73
79
|
loginPromise;
|
|
80
|
+
loginAttempt;
|
|
81
|
+
rejectedTokens = new Set();
|
|
74
82
|
authGeneration = 0;
|
|
75
83
|
authBindingRefs = 0;
|
|
76
84
|
closeGeneration = 0;
|
|
@@ -83,6 +91,7 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
83
91
|
// The venue's login lives and dies with the connection, so a drop must
|
|
84
92
|
// forget both the in-flight login and the address it bound.
|
|
85
93
|
this.rws.on('close', () => {
|
|
94
|
+
this.takeLoginAttempt()?.reject(new Error('Ondo WebSocket closed during login'));
|
|
86
95
|
this.loginPromise = undefined;
|
|
87
96
|
this.accountAddress = undefined;
|
|
88
97
|
});
|
|
@@ -242,23 +251,43 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
242
251
|
if (bound === undefined) {
|
|
243
252
|
return true;
|
|
244
253
|
}
|
|
245
|
-
for (const
|
|
246
|
-
if (!this.closeChannelIfIdle(
|
|
254
|
+
for (const key of this.authChannelKeys(bound)) {
|
|
255
|
+
if (!this.closeChannelIfIdle(key)) {
|
|
247
256
|
return false;
|
|
248
257
|
}
|
|
249
258
|
}
|
|
250
259
|
return true;
|
|
251
260
|
}
|
|
252
|
-
/**
|
|
261
|
+
/**
|
|
262
|
+
* Release a channel's binding reservation, including one still opening.
|
|
263
|
+
* The last release cycles the connection only when a login was sent on it,
|
|
264
|
+
* because the venue holds that login until the socket closes.
|
|
265
|
+
*/
|
|
253
266
|
releaseBinding() {
|
|
254
267
|
this.authBindingRefs -= 1;
|
|
255
268
|
if (this.authBindingRefs > 0) {
|
|
256
269
|
return;
|
|
257
270
|
}
|
|
258
271
|
this.accountAddress = undefined;
|
|
272
|
+
const login = this.loginPromise;
|
|
259
273
|
this.loginPromise = undefined;
|
|
260
274
|
this.authGeneration += 1;
|
|
261
|
-
|
|
275
|
+
if (login !== undefined) {
|
|
276
|
+
this.rws.reconnect();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
authChannelKeys(address) {
|
|
280
|
+
return ONDO_AUTH_FANOUT.map((channel) => `${channel}:${address}`);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* The venue login is per connection, so one credential failure stops every
|
|
284
|
+
* private channel of the address.
|
|
285
|
+
*/
|
|
286
|
+
resolveChannelKeys(key, state) {
|
|
287
|
+
if (!state.needsAuth || state.address === undefined) {
|
|
288
|
+
return super.resolveChannelKeys(key, state);
|
|
289
|
+
}
|
|
290
|
+
return this.authChannelKeys(state.address.toLowerCase());
|
|
262
291
|
}
|
|
263
292
|
/** Register a wire sub, ref-counting the shared authenticated channels. */
|
|
264
293
|
async acquireWire(key, state, shared) {
|
|
@@ -318,12 +347,14 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
318
347
|
this.contexts = {};
|
|
319
348
|
this.pendingFunding.clear();
|
|
320
349
|
this.loginPromise = undefined;
|
|
350
|
+
this.takeLoginAttempt()?.reject(new Error('Ondo WebSocket closed during login'));
|
|
321
351
|
this.accountAddress = undefined;
|
|
322
352
|
this.authGeneration += 1;
|
|
323
353
|
this.closeGeneration += 1;
|
|
324
354
|
this.authWireRefs.clear();
|
|
325
355
|
this.authBindingRefs = 0;
|
|
326
356
|
this.clearAccountSummary();
|
|
357
|
+
this.fillChain.reset();
|
|
327
358
|
}
|
|
328
359
|
/** Wire subs for `sub`, keyed uniquely for the base's replay registry. */
|
|
329
360
|
resolveChannel(sub) {
|
|
@@ -442,6 +473,9 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
442
473
|
if (token === null) {
|
|
443
474
|
throw new PerpsError(PerpsErrorCode.SetupRequired, `No Ondo session for ${address}. Run the SIWE login before subscribing to account channels.`);
|
|
444
475
|
}
|
|
476
|
+
if (this.rejectedTokens.has(token.token)) {
|
|
477
|
+
throw ondoSessionRejectedError('WebSocket subscription', new OndoSessionExpiredError('Ondo rejected the session token'));
|
|
478
|
+
}
|
|
445
479
|
return token;
|
|
446
480
|
}
|
|
447
481
|
/**
|
|
@@ -452,9 +486,28 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
452
486
|
ensureLogin(address) {
|
|
453
487
|
if (this.loginPromise === undefined) {
|
|
454
488
|
const login = this.requireSession(address).then((token) => {
|
|
455
|
-
if (this.loginPromise
|
|
456
|
-
|
|
489
|
+
if (this.loginPromise !== login) {
|
|
490
|
+
return;
|
|
457
491
|
}
|
|
492
|
+
return new Promise((resolve, reject) => {
|
|
493
|
+
this.rws.send(JSON.stringify({ op: 'login', args: { token: token.token } }));
|
|
494
|
+
const attempt = {
|
|
495
|
+
address,
|
|
496
|
+
token: token.token,
|
|
497
|
+
accepted: false,
|
|
498
|
+
timer: setTimeout(() => {
|
|
499
|
+
if (this.loginAttempt !== attempt) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
this.rejectLogin(new PerpsError(PerpsErrorCode.TimeoutError, 'Ondo did not acknowledge the WebSocket login.'), false);
|
|
503
|
+
// The venue may still hold a half-open login on this socket.
|
|
504
|
+
this.rws.reconnect();
|
|
505
|
+
}, ONDO_LOGIN_ACK_TIMEOUT_MS),
|
|
506
|
+
resolve,
|
|
507
|
+
reject,
|
|
508
|
+
};
|
|
509
|
+
this.loginAttempt = attempt;
|
|
510
|
+
});
|
|
458
511
|
});
|
|
459
512
|
this.loginPromise = login;
|
|
460
513
|
login.catch(() => {
|
|
@@ -465,8 +518,9 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
465
518
|
}
|
|
466
519
|
return this.loginPromise;
|
|
467
520
|
}
|
|
468
|
-
restClient() {
|
|
521
|
+
restClient(signal) {
|
|
469
522
|
return new OndoApiClient(this.apiUrl, {
|
|
523
|
+
signal,
|
|
470
524
|
fetchImpl: this.client?.config.fetch,
|
|
471
525
|
});
|
|
472
526
|
}
|
|
@@ -523,7 +577,7 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
523
577
|
if (!(err instanceof OndoSessionExpiredError)) {
|
|
524
578
|
throw err;
|
|
525
579
|
}
|
|
526
|
-
await this.tokenStore.remove(address);
|
|
580
|
+
await this.tokenStore.remove(address, token.token);
|
|
527
581
|
throw ondoSessionRejectedError('account summary', err);
|
|
528
582
|
}
|
|
529
583
|
return {
|
|
@@ -551,6 +605,45 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
551
605
|
},
|
|
552
606
|
});
|
|
553
607
|
}
|
|
608
|
+
/** Detach the pending login attempt, if any, and stop its acknowledgement timer. */
|
|
609
|
+
takeLoginAttempt() {
|
|
610
|
+
const attempt = this.loginAttempt;
|
|
611
|
+
if (attempt !== undefined) {
|
|
612
|
+
clearTimeout(attempt.timer);
|
|
613
|
+
this.loginAttempt = undefined;
|
|
614
|
+
}
|
|
615
|
+
return attempt;
|
|
616
|
+
}
|
|
617
|
+
rejectLogin(error, unauthorized) {
|
|
618
|
+
const attempt = this.takeLoginAttempt();
|
|
619
|
+
if (attempt === undefined) {
|
|
620
|
+
wsLog.serverError(this.providerKey, `Login error without a pending login: ${error.message}`);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
this.loginPromise = undefined;
|
|
624
|
+
const address = attempt.address.toLowerCase();
|
|
625
|
+
if (unauthorized) {
|
|
626
|
+
this.rejectedTokens.add(attempt.token);
|
|
627
|
+
void this.tokenStore
|
|
628
|
+
.remove(attempt.address, attempt.token)
|
|
629
|
+
.catch((cause) => wsLog.handlerFailure(this.providerKey, cause));
|
|
630
|
+
}
|
|
631
|
+
for (const [key, state] of this.wireSubs) {
|
|
632
|
+
if (state.needsAuth) {
|
|
633
|
+
this.unregisterSub(key);
|
|
634
|
+
this.rws.send(JSON.stringify({ op: 'unsubscribe', ...state.frame }));
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
this.authWireRefs.clear();
|
|
638
|
+
this.authBindingRefs = 0;
|
|
639
|
+
this.authGeneration += 1;
|
|
640
|
+
this.accountAddress = undefined;
|
|
641
|
+
this.clearAccountSummary();
|
|
642
|
+
attempt.reject(error);
|
|
643
|
+
for (const key of this.authChannelKeys(address)) {
|
|
644
|
+
this.rejectChannel(key, error);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
554
647
|
handleMessage(raw) {
|
|
555
648
|
let msg;
|
|
556
649
|
try {
|
|
@@ -560,8 +653,27 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
560
653
|
wsLog.parseFailure(this.providerKey, raw);
|
|
561
654
|
return;
|
|
562
655
|
}
|
|
656
|
+
if (msg.type === 'loggedIn') {
|
|
657
|
+
if (this.loginAttempt !== undefined) {
|
|
658
|
+
clearTimeout(this.loginAttempt.timer);
|
|
659
|
+
this.loginAttempt.accepted = true;
|
|
660
|
+
this.loginAttempt.resolve();
|
|
661
|
+
}
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
563
664
|
if (msg.type === 'error') {
|
|
564
|
-
|
|
665
|
+
if (msg.msg?.startsWith('error parsing jwt: ')) {
|
|
666
|
+
this.rejectLogin(ondoSessionRejectedError('WebSocket subscription', new OndoSessionExpiredError('Ondo rejected the session token')), true);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (this.loginAttempt !== undefined &&
|
|
670
|
+
!this.loginAttempt.accepted &&
|
|
671
|
+
(msg.msg === 'already logged in on this connection' ||
|
|
672
|
+
msg.msg === 'account already has too many connections')) {
|
|
673
|
+
this.rejectLogin(new PerpsError(PerpsErrorCode.ThirdPartyError, msg.msg), false);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
wsLog.serverError(this.providerKey, 'Venue rejected a WebSocket request');
|
|
565
677
|
return;
|
|
566
678
|
}
|
|
567
679
|
if (msg.type !== 'update') {
|
|
@@ -751,7 +863,20 @@ export class OndoWsProvider extends WsProviderBase {
|
|
|
751
863
|
mapped.push(mappedFill);
|
|
752
864
|
}
|
|
753
865
|
}
|
|
754
|
-
this.
|
|
866
|
+
this.fillChain.push(async () => {
|
|
867
|
+
const leverages = isAddress(address, { strict: false })
|
|
868
|
+
? await this.readFrameEnrichment('fill leverage', async (signal) => {
|
|
869
|
+
const token = await this.tokenStore.get(address);
|
|
870
|
+
return token === null
|
|
871
|
+
? undefined
|
|
872
|
+
: fetchFillLeverages(this.restClient(signal), token.token, mapped, signal);
|
|
873
|
+
})
|
|
874
|
+
: undefined;
|
|
875
|
+
this.emit(`fills:${address}`, {
|
|
876
|
+
channel: 'fills',
|
|
877
|
+
data: mapped.map((fill) => buildFill(fill, leverages?.get(fill.market.id))),
|
|
878
|
+
});
|
|
879
|
+
});
|
|
755
880
|
if (this.accountSummary !== undefined) {
|
|
756
881
|
const generation = this.accountSummaryGeneration;
|
|
757
882
|
this.accountSummaryChain.push(() => this.refreshAccountSummary(address, generation));
|