@gearbox-protocol/sdk 16.0.0-next.32 → 16.0.0-next.33
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.
|
@@ -4,7 +4,11 @@ require("../../model/index.js");
|
|
|
4
4
|
const require_sdk_utils_mergeChains = require("../utils/mergeChains.js");
|
|
5
5
|
//#region src/sdk/positions/mergePositionList.ts
|
|
6
6
|
function mergePositionList(onchain, offchain, maxLagSeconds) {
|
|
7
|
-
const
|
|
7
|
+
const onchainGroups = splitPositionsByKind(onchain);
|
|
8
|
+
const offchainGroups = splitPositionsByKind(offchain);
|
|
9
|
+
return combineGroups(overlayBackendStrategyFields(require_sdk_utils_mergeChains.mergeChainList(onchainGroups.nonLiquidations, offchainGroups.nonLiquidations, maxLagSeconds), offchainGroups.nonLiquidations), require_sdk_utils_mergeChains.mergeChainList(onchainGroups.liquidations, offchainGroups.liquidations, maxLagSeconds));
|
|
10
|
+
}
|
|
11
|
+
function overlayBackendStrategyFields(merged, offchain) {
|
|
8
12
|
if (!merged || !offchain) return merged;
|
|
9
13
|
const backendById = new Map(offchain.data.filter((row) => row.kind === "strategy").map((row) => [require_model_positions.positionId(row), row]));
|
|
10
14
|
return {
|
|
@@ -19,5 +23,45 @@ function mergePositionList(onchain, offchain, maxLagSeconds) {
|
|
|
19
23
|
})
|
|
20
24
|
};
|
|
21
25
|
}
|
|
26
|
+
function splitPositionsByKind(response) {
|
|
27
|
+
if (!response) return {
|
|
28
|
+
nonLiquidations: void 0,
|
|
29
|
+
liquidations: void 0
|
|
30
|
+
};
|
|
31
|
+
const nonLiquidationRows = [];
|
|
32
|
+
const liquidationRows = [];
|
|
33
|
+
for (const row of response.data) if (row.kind === "liquidation") liquidationRows.push(row);
|
|
34
|
+
else nonLiquidationRows.push(row);
|
|
35
|
+
return {
|
|
36
|
+
nonLiquidations: {
|
|
37
|
+
...response,
|
|
38
|
+
data: nonLiquidationRows
|
|
39
|
+
},
|
|
40
|
+
liquidations: liquidationRowsToResponse(response, liquidationRows)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Liquidation rows of one source, or `undefined` when it served none, so
|
|
45
|
+
* {@link mergeChainList} cannot pick an empty backend list over on-chain rows.
|
|
46
|
+
**/
|
|
47
|
+
function liquidationRowsToResponse(source, rows) {
|
|
48
|
+
if (rows.length === 0) return;
|
|
49
|
+
const chainIds = new Set(rows.map((row) => row.chainId));
|
|
50
|
+
return {
|
|
51
|
+
data: rows,
|
|
52
|
+
meta: {
|
|
53
|
+
...source.meta,
|
|
54
|
+
chains: source.meta.chains.filter((chain) => chainIds.has(chain.chainId))
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function combineGroups(nonLiquidations, liquidations) {
|
|
59
|
+
if (!nonLiquidations) return liquidations;
|
|
60
|
+
if (!liquidations) return nonLiquidations;
|
|
61
|
+
return {
|
|
62
|
+
...nonLiquidations,
|
|
63
|
+
data: [...nonLiquidations.data, ...liquidations.data]
|
|
64
|
+
};
|
|
65
|
+
}
|
|
22
66
|
//#endregion
|
|
23
67
|
exports.mergePositionList = mergePositionList;
|
|
@@ -3,7 +3,11 @@ import "../../model/index.js";
|
|
|
3
3
|
import { mergeChainList } from "../utils/mergeChains.js";
|
|
4
4
|
//#region src/sdk/positions/mergePositionList.ts
|
|
5
5
|
function mergePositionList(onchain, offchain, maxLagSeconds) {
|
|
6
|
-
const
|
|
6
|
+
const onchainGroups = splitPositionsByKind(onchain);
|
|
7
|
+
const offchainGroups = splitPositionsByKind(offchain);
|
|
8
|
+
return combineGroups(overlayBackendStrategyFields(mergeChainList(onchainGroups.nonLiquidations, offchainGroups.nonLiquidations, maxLagSeconds), offchainGroups.nonLiquidations), mergeChainList(onchainGroups.liquidations, offchainGroups.liquidations, maxLagSeconds));
|
|
9
|
+
}
|
|
10
|
+
function overlayBackendStrategyFields(merged, offchain) {
|
|
7
11
|
if (!merged || !offchain) return merged;
|
|
8
12
|
const backendById = new Map(offchain.data.filter((row) => row.kind === "strategy").map((row) => [positionId(row), row]));
|
|
9
13
|
return {
|
|
@@ -18,5 +22,45 @@ function mergePositionList(onchain, offchain, maxLagSeconds) {
|
|
|
18
22
|
})
|
|
19
23
|
};
|
|
20
24
|
}
|
|
25
|
+
function splitPositionsByKind(response) {
|
|
26
|
+
if (!response) return {
|
|
27
|
+
nonLiquidations: void 0,
|
|
28
|
+
liquidations: void 0
|
|
29
|
+
};
|
|
30
|
+
const nonLiquidationRows = [];
|
|
31
|
+
const liquidationRows = [];
|
|
32
|
+
for (const row of response.data) if (row.kind === "liquidation") liquidationRows.push(row);
|
|
33
|
+
else nonLiquidationRows.push(row);
|
|
34
|
+
return {
|
|
35
|
+
nonLiquidations: {
|
|
36
|
+
...response,
|
|
37
|
+
data: nonLiquidationRows
|
|
38
|
+
},
|
|
39
|
+
liquidations: liquidationRowsToResponse(response, liquidationRows)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Liquidation rows of one source, or `undefined` when it served none, so
|
|
44
|
+
* {@link mergeChainList} cannot pick an empty backend list over on-chain rows.
|
|
45
|
+
**/
|
|
46
|
+
function liquidationRowsToResponse(source, rows) {
|
|
47
|
+
if (rows.length === 0) return;
|
|
48
|
+
const chainIds = new Set(rows.map((row) => row.chainId));
|
|
49
|
+
return {
|
|
50
|
+
data: rows,
|
|
51
|
+
meta: {
|
|
52
|
+
...source.meta,
|
|
53
|
+
chains: source.meta.chains.filter((chain) => chainIds.has(chain.chainId))
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function combineGroups(nonLiquidations, liquidations) {
|
|
58
|
+
if (!nonLiquidations) return liquidations;
|
|
59
|
+
if (!liquidations) return nonLiquidations;
|
|
60
|
+
return {
|
|
61
|
+
...nonLiquidations,
|
|
62
|
+
data: [...nonLiquidations.data, ...liquidations.data]
|
|
63
|
+
};
|
|
64
|
+
}
|
|
21
65
|
//#endregion
|
|
22
66
|
export { mergePositionList };
|
|
@@ -9,6 +9,11 @@ import { MergeListResult } from "../utils/types.js";
|
|
|
9
9
|
* `name` onto every strategy row the backend has — even when that chain was
|
|
10
10
|
* served from on-chain data because the backend was stale.
|
|
11
11
|
*
|
|
12
|
+
* Liquidation rows are merged independently: the backend does not serve them
|
|
13
|
+
* yet, so a fresh off-chain list must not drop on-chain liquidations. Once it
|
|
14
|
+
* does return them, they follow the same freshness rule as the rest of the
|
|
15
|
+
* list.
|
|
16
|
+
*
|
|
12
17
|
* The chain resolves `targetCollateral` from a per-account override or the
|
|
13
18
|
* credit manager's single target token. The backend's historical value is
|
|
14
19
|
* still preferred when it has the row. `name` is derived from that collateral,
|