@gearbox-protocol/sdk 14.12.0-next.82 → 14.12.0-next.83

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.
@@ -1,14 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#region src/new-sdk/utils/filterResponse.ts
3
- /**
4
- * Narrows an already-read list, rows and metadata alike. `undefined` passes
5
- * through, so a read still in flight stays that way.
6
- *
7
- * @typeParam T - Row type.
8
- * @typeParam F - Filter type.
9
- * @param matches - Whether one row satisfies the filter, e.g.
10
- * `matchesOpportunityFilter`.
11
- **/
12
3
  function filterResponse(response, filter, matches) {
13
4
  if (!response) return;
14
5
  const chainIds = filter?.chainIds;
@@ -6,11 +6,6 @@ require("../errors/index.js");
6
6
  * How many seconds the backend may lag the chain and still be used.
7
7
  **/
8
8
  const DEFAULT_MAX_OFFCHAIN_LAG = 120;
9
- /**
10
- * Merges two lists chain by chain: a chain is served by the backend when it is
11
- * within `maxLagSeconds` of the chain, by the chain otherwise, and reported as
12
- * an error when neither source succeeded.
13
- **/
14
9
  function mergeChainList(onchain, offchain, maxLagSeconds = 120) {
15
10
  if (!onchain && !offchain) return;
16
11
  const fromOnchain = /* @__PURE__ */ new Map();
@@ -1,13 +1,4 @@
1
1
  //#region src/new-sdk/utils/filterResponse.ts
2
- /**
3
- * Narrows an already-read list, rows and metadata alike. `undefined` passes
4
- * through, so a read still in flight stays that way.
5
- *
6
- * @typeParam T - Row type.
7
- * @typeParam F - Filter type.
8
- * @param matches - Whether one row satisfies the filter, e.g.
9
- * `matchesOpportunityFilter`.
10
- **/
11
2
  function filterResponse(response, filter, matches) {
12
3
  if (!response) return;
13
4
  const chainIds = filter?.chainIds;
@@ -5,11 +5,6 @@ import "../errors/index.js";
5
5
  * How many seconds the backend may lag the chain and still be used.
6
6
  **/
7
7
  const DEFAULT_MAX_OFFCHAIN_LAG = 120;
8
- /**
9
- * Merges two lists chain by chain: a chain is served by the backend when it is
10
- * within `maxLagSeconds` of the chain, by the chain otherwise, and reported as
11
- * an error when neither source succeeded.
12
- **/
13
8
  function mergeChainList(onchain, offchain, maxLagSeconds = 120) {
14
9
  if (!onchain && !offchain) return;
15
10
  const fromOnchain = /* @__PURE__ */ new Map();
@@ -2,7 +2,7 @@ import { ILogger } from "../sdk/types/logger.js";
2
2
  import { DataResponse } from "../model/response.js";
3
3
  import "../model/index.js";
4
4
  import { NamespaceOptions } from "./types.js";
5
- import { SourceMerger } from "./utils/types.js";
5
+ import { EntityMerger, ListMerger } from "./utils/types.js";
6
6
  import "./utils/index.js";
7
7
  //#region src/new-sdk/AbstractNamespace.d.ts
8
8
  /**
@@ -16,7 +16,7 @@ import "./utils/index.js";
16
16
  interface MergedQuery<Onchain, Offchain, T> {
17
17
  fromChain: (source: Onchain) => Promise<DataResponse<T>>;
18
18
  fromBackend: (source: Offchain) => Promise<DataResponse<T>>;
19
- merge: SourceMerger<T>;
19
+ merge: ListMerger<T> | EntityMerger<T>;
20
20
  }
21
21
  /**
22
22
  * Base class of every {@link GearboxSDK} namespace: holds the two source
@@ -1,8 +1,8 @@
1
1
  import { GearboxSDKOptions, Mode, NamespaceOptions, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, PlainMultichainSDKOptions } from "./types.js";
2
+ import { EntityMerger, FilterResult, ListMerger, MergeListResult } from "./utils/types.js";
2
3
  import { filterResponse } from "./utils/filterResponse.js";
3
4
  import { HistoryReader } from "./utils/history.js";
4
5
  import { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne } from "./utils/mergeChains.js";
5
- import { SourceMerger } from "./utils/types.js";
6
6
  import "./utils/index.js";
7
7
  import { AbstractNamespace, MergedQuery } from "./AbstractNamespace.js";
8
8
  import { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers } from "./opportunities/types.js";
@@ -20,4 +20,4 @@ import { SourceUnavailableError } from "./errors/SourceUnavailableError.js";
20
20
  import { assertSameChains } from "./errors/assertSameChains.js";
21
21
  import { everyChainFailed } from "./errors/everyChainFailed.js";
22
22
  import "./errors/index.js";
23
- export { AbstractNamespace, AllSourcesFailedError, ChainRef, DEFAULT_MAX_OFFCHAIN_LAG, GearboxSDK, GearboxSDKOptions, HistoryReader, MergedQuery, MissingSourceError, Mode, NamespaceOptions, NoSourceServedError, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesNamespace, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers, PlainMultichainSDKOptions, PositionMergers, Positions, PositionsBase, PositionsByMode, PositionsMerged, PositionsNamespace, PositionsOffchainBranch, PositionsOffchainOnly, PositionsOnchainBranch, PositionsOnchainOnly, SourceChainMismatchError, type SourceMerger, SourceUnavailableError, assertSameChains, everyChainFailed, filterResponse, mergeChainList, mergeChainOne };
23
+ export { AbstractNamespace, AllSourcesFailedError, ChainRef, DEFAULT_MAX_OFFCHAIN_LAG, type EntityMerger, type FilterResult, GearboxSDK, GearboxSDKOptions, HistoryReader, type ListMerger, type MergeListResult, MergedQuery, MissingSourceError, Mode, NamespaceOptions, NoSourceServedError, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesNamespace, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers, PlainMultichainSDKOptions, PositionMergers, Positions, PositionsBase, PositionsByMode, PositionsMerged, PositionsNamespace, PositionsOffchainBranch, PositionsOffchainOnly, PositionsOnchainBranch, PositionsOnchainOnly, SourceChainMismatchError, SourceUnavailableError, assertSameChains, everyChainFailed, filterResponse, mergeChainList, mergeChainOne };
@@ -7,6 +7,7 @@ import "../../sdk/index.js";
7
7
  import { GearboxAPI } from "../../offchain/GearboxAPI.js";
8
8
  import "../../offchain/index.js";
9
9
  import { NamespaceOptions } from "../types.js";
10
+ import { FilterResult } from "../utils/types.js";
10
11
  import { HistoryReader } from "../utils/history.js";
11
12
  import "../utils/index.js";
12
13
  import { AbstractNamespace } from "../AbstractNamespace.js";
@@ -37,7 +38,7 @@ declare class OpportunitiesNamespace extends AbstractNamespace<MultichainSDK["op
37
38
  /**
38
39
  * {@inheritDoc OpportunitiesBase.filter}
39
40
  **/
40
- filter(response: DataResponse<Opportunity[]> | undefined, filter?: OpportunityFilter): DataResponse<Opportunity[]> | undefined;
41
+ filter<R extends DataResponse<Opportunity[]> | undefined>(response: R, filter?: OpportunityFilter): FilterResult<R, Opportunity>;
41
42
  /**
42
43
  * {@inheritDoc OpportunitiesOffchainOnly.history}
43
44
  **/
@@ -7,8 +7,8 @@ import "../../sdk/index.js";
7
7
  import { OffchainOpportunities } from "../../offchain/opportunities/OffchainOpportunities.js";
8
8
  import "../../offchain/index.js";
9
9
  import { Mode } from "../types.js";
10
+ import { EntityMerger, FilterResult, ListMerger } from "../utils/types.js";
10
11
  import { HistoryReader } from "../utils/history.js";
11
- import { SourceMerger } from "../utils/types.js";
12
12
  import "../utils/index.js";
13
13
  //#region src/new-sdk/opportunities/types.d.ts
14
14
  /**
@@ -31,9 +31,10 @@ interface OpportunitiesBase {
31
31
  getStrategy(key: StrategyOpportunityKey): Promise<DataResponse<StrategyOpportunityDetail>>;
32
32
  /**
33
33
  * Narrows an already-read list, rows and metadata alike. `undefined` passes
34
- * through, so a read still in flight stays that way.
34
+ * through, so a read still in flight stays that way, and a list already read
35
+ * narrows to a list.
35
36
  **/
36
- filter(response: DataResponse<Opportunity[]> | undefined, filter?: OpportunityFilter): DataResponse<Opportunity[]> | undefined;
37
+ filter<R extends DataResponse<Opportunity[]> | undefined>(response: R, filter?: OpportunityFilter): FilterResult<R, Opportunity>;
37
38
  /**
38
39
  * The chain on its own, for a consumer that shows each source as it arrives.
39
40
  * Throws in `offchain` mode.
@@ -66,15 +67,6 @@ interface OpportunitiesOffchainOnly {
66
67
  * Reads only the chain can answer. Empty for now.
67
68
  **/
68
69
  interface OpportunitiesOnchainOnly {}
69
- /**
70
- * How each read combines what the two sources returned: a chain is served by
71
- * the backend when it is fresh enough, and by the chain otherwise.
72
- **/
73
- interface OpportunityMergers {
74
- list: SourceMerger<Opportunity[]>;
75
- pool: SourceMerger<PoolOpportunityDetail>;
76
- strategy: SourceMerger<StrategyOpportunityDetail>;
77
- }
78
70
  /**
79
71
  * Which reads the `opportunities` namespace has in each mode. A widened mode
80
72
  * offers what every mode has, i.e. {@link OpportunitiesBase} alone.
@@ -94,9 +86,9 @@ interface OpportunitiesOffchainBranch {
94
86
  * by the backend when it is fresh enough, and by the chain otherwise.
95
87
  **/
96
88
  interface OpportunityMergers {
97
- list: SourceMerger<Opportunity[]>;
98
- pool: SourceMerger<PoolOpportunityDetail>;
99
- strategy: SourceMerger<StrategyOpportunityDetail>;
89
+ list: ListMerger<Opportunity[]>;
90
+ pool: EntityMerger<PoolOpportunityDetail>;
91
+ strategy: EntityMerger<StrategyOpportunityDetail>;
100
92
  }
101
93
  /**
102
94
  * Merging, which only exists where there are two sources to merge.
@@ -7,6 +7,7 @@ import "../../sdk/index.js";
7
7
  import { GearboxAPI } from "../../offchain/GearboxAPI.js";
8
8
  import "../../offchain/index.js";
9
9
  import { NamespaceOptions } from "../types.js";
10
+ import { FilterResult } from "../utils/types.js";
10
11
  import { HistoryReader } from "../utils/history.js";
11
12
  import "../utils/index.js";
12
13
  import { AbstractNamespace } from "../AbstractNamespace.js";
@@ -30,7 +31,7 @@ declare class PositionsNamespace extends AbstractNamespace<MultichainSDK["positi
30
31
  /**
31
32
  * {@inheritDoc PositionsBase.filter}
32
33
  **/
33
- filter(response: DataResponse<Position[]> | undefined, filter?: PositionFilter): DataResponse<Position[]> | undefined;
34
+ filter<R extends DataResponse<Position[]> | undefined>(response: R, filter?: PositionFilter): FilterResult<R, Position>;
34
35
  /**
35
36
  * {@inheritDoc PositionsOffchainOnly.history}
36
37
  **/
@@ -7,8 +7,8 @@ import "../../sdk/index.js";
7
7
  import { OffchainPositions } from "../../offchain/positions/OffchainPositions.js";
8
8
  import "../../offchain/index.js";
9
9
  import { Mode } from "../types.js";
10
+ import { FilterResult, ListMerger } from "../utils/types.js";
10
11
  import { HistoryReader } from "../utils/history.js";
11
- import { SourceMerger } from "../utils/types.js";
12
12
  import "../utils/index.js";
13
13
  import { Address } from "viem";
14
14
  //#region src/new-sdk/positions/types.d.ts
@@ -25,9 +25,10 @@ interface PositionsBase {
25
25
  list(wallet: Address, filter?: PositionFilter): Promise<DataResponse<Position[]>>;
26
26
  /**
27
27
  * Narrows an already-read list, rows and metadata alike. `undefined` passes
28
- * through, so a read still in flight stays that way.
28
+ * through, so a read still in flight stays that way, and a list already read
29
+ * narrows to a list.
29
30
  **/
30
- filter(response: DataResponse<Position[]> | undefined, filter?: PositionFilter): DataResponse<Position[]> | undefined;
31
+ filter<R extends DataResponse<Position[]> | undefined>(response: R, filter?: PositionFilter): FilterResult<R, Position>;
31
32
  /**
32
33
  * The chain on its own, for a consumer that shows each source as it arrives.
33
34
  * Throws in `offchain` mode.
@@ -62,13 +63,6 @@ interface PositionsOffchainOnly {
62
63
  * Reads only the chain can answer. Empty for now.
63
64
  **/
64
65
  interface PositionsOnchainOnly {}
65
- /**
66
- * How each read combines what the two sources returned: a chain is served by
67
- * the backend when it is fresh enough, and by the chain otherwise.
68
- **/
69
- interface PositionMergers {
70
- list: SourceMerger<Position[]>;
71
- }
72
66
  /**
73
67
  * Which reads the `positions` namespace has in each mode. A widened mode offers
74
68
  * what every mode has, i.e. {@link PositionsBase} alone.
@@ -88,7 +82,7 @@ interface PositionsOffchainBranch {
88
82
  * by the backend when it is fresh enough, and by the chain otherwise.
89
83
  **/
90
84
  interface PositionMergers {
91
- list: SourceMerger<Position[]>;
85
+ list: ListMerger<Position[]>;
92
86
  }
93
87
  /**
94
88
  * Merging, which only exists where there are two sources to merge.
@@ -1,16 +1,19 @@
1
1
  import { ChainScopedFilter } from "../../model/filters.js";
2
2
  import { DataResponse } from "../../model/response.js";
3
3
  import "../../model/index.js";
4
+ import { FilterResult } from "./types.js";
4
5
  //#region src/new-sdk/utils/filterResponse.d.ts
5
6
  /**
6
7
  * Narrows an already-read list, rows and metadata alike. `undefined` passes
7
- * through, so a read still in flight stays that way.
8
+ * through, so a read still in flight stays that way, and an envelope narrows to
9
+ * an envelope, see {@link FilterResult}.
8
10
  *
9
11
  * @typeParam T - Row type.
10
12
  * @typeParam F - Filter type.
13
+ * @typeParam R - What the response was given as.
11
14
  * @param matches - Whether one row satisfies the filter, e.g.
12
15
  * `matchesOpportunityFilter`.
13
16
  **/
14
- declare function filterResponse<T, F extends ChainScopedFilter>(response: DataResponse<T[]> | undefined, filter: F | undefined, matches: (row: T, filter?: F) => boolean): DataResponse<T[]> | undefined;
17
+ declare function filterResponse<T, F extends ChainScopedFilter, R extends DataResponse<T[]> | undefined>(response: R, filter: F | undefined, matches: (row: T, filter?: F) => boolean): FilterResult<R, T>;
15
18
  //#endregion
16
19
  export { filterResponse };
@@ -1,5 +1,5 @@
1
+ import { EntityMerger, FilterResult, ListMerger, MergeListResult } from "./types.js";
1
2
  import { filterResponse } from "./filterResponse.js";
2
3
  import { HistoryReader } from "./history.js";
3
4
  import { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne } from "./mergeChains.js";
4
- import { SourceMerger } from "./types.js";
5
- export { DEFAULT_MAX_OFFCHAIN_LAG, HistoryReader, type SourceMerger, filterResponse, mergeChainList, mergeChainOne };
5
+ export { DEFAULT_MAX_OFFCHAIN_LAG, type EntityMerger, type FilterResult, HistoryReader, type ListMerger, type MergeListResult, filterResponse, mergeChainList, mergeChainOne };
@@ -1,5 +1,6 @@
1
1
  import { ChainScoped, DataResponse } from "../../model/response.js";
2
2
  import "../../model/index.js";
3
+ import { MergeListResult } from "./types.js";
3
4
  //#region src/new-sdk/utils/mergeChains.d.ts
4
5
  /**
5
6
  * How many seconds the backend may lag the chain and still be used.
@@ -8,9 +9,10 @@ declare const DEFAULT_MAX_OFFCHAIN_LAG = 120;
8
9
  /**
9
10
  * Merges two lists chain by chain: a chain is served by the backend when it is
10
11
  * within `maxLagSeconds` of the chain, by the chain otherwise, and reported as
11
- * an error when neither source succeeded.
12
+ * an error when neither source succeeded. Answers with an envelope as soon as
13
+ * either side has arrived, see {@link MergeListResult}.
12
14
  **/
13
- declare function mergeChainList<T extends ChainScoped>(onchain: DataResponse<T[]> | undefined, offchain: DataResponse<T[]> | undefined, maxLagSeconds?: number): DataResponse<T[]> | undefined;
15
+ declare function mergeChainList<T extends ChainScoped, Onchain extends DataResponse<T[]> | undefined, Offchain extends DataResponse<T[]> | undefined>(onchain: Onchain, offchain: Offchain, maxLagSeconds?: number): MergeListResult<Onchain, Offchain, T[]>;
14
16
  /**
15
17
  * Merges two versions of one entity under the same freshness rule as
16
18
  * {@link mergeChainList}, returning the winning response whole.
@@ -2,10 +2,42 @@ import { DataResponse } from "../../model/response.js";
2
2
  import "../../model/index.js";
3
3
  //#region src/new-sdk/utils/types.d.ts
4
4
  /**
5
- * How one read combines what the two sources returned.
5
+ * Whether a merge could have been given nothing at all, i.e. both sides are
6
+ * typed as possibly absent. One definite side already answers this `false`.
7
+ **/
8
+ type BothMayBeMissing<Onchain, Offchain> = undefined extends Onchain & Offchain ? true : false;
9
+ /**
10
+ * What merging two lists answers with: an envelope unless both sides could
11
+ * still be missing, since a list merge serves whichever side did arrive.
12
+ *
13
+ * @typeParam Onchain - What the on-chain side was given as.
14
+ * @typeParam Offchain - What the backend side was given as.
15
+ * @typeParam T - Payload type of the read.
16
+ **/
17
+ type MergeListResult<Onchain, Offchain, T> = BothMayBeMissing<Onchain, Offchain> extends true ? DataResponse<T> | undefined : DataResponse<T>;
18
+ /**
19
+ * What narrowing an already-read list answers with: `undefined` only where the
20
+ * read itself was still in flight.
21
+ *
22
+ * @typeParam R - What the response was given as.
23
+ * @typeParam T - Row type.
24
+ **/
25
+ type FilterResult<R, T> = R extends undefined ? undefined : DataResponse<T[]>;
26
+ /**
27
+ * How a list read combines what the two sources returned. A side still in
28
+ * flight is `undefined`; the merge answers with an envelope as soon as either
29
+ * one has arrived, see {@link MergeListResult}.
30
+ *
31
+ * @typeParam T - Payload type of the read.
32
+ **/
33
+ type ListMerger<T> = <Onchain extends DataResponse<T> | undefined, Offchain extends DataResponse<T> | undefined>(onchain: Onchain, offchain: Offchain) => MergeListResult<Onchain, Offchain, T>;
34
+ /**
35
+ * How a single-entity read combines what the two sources returned. Unlike
36
+ * {@link ListMerger} this stays optional whatever it was given: neither source
37
+ * may have served the entity, and there is nothing to answer with then.
6
38
  *
7
39
  * @typeParam T - Payload type of the read.
8
40
  **/
9
- type SourceMerger<T> = (onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined) => DataResponse<T> | undefined;
41
+ type EntityMerger<T> = (onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined) => DataResponse<T> | undefined;
10
42
  //#endregion
11
- export { SourceMerger };
43
+ export { EntityMerger, FilterResult, ListMerger, MergeListResult };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "14.12.0-next.82",
3
+ "version": "14.12.0-next.83",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {