@gearbox-protocol/sdk 14.12.0-next.81 → 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.
Files changed (200) hide show
  1. package/dist/cjs/model/filters.js +0 -4
  2. package/dist/cjs/model/filters.schema.js +10 -1
  3. package/dist/cjs/model/index.js +3 -0
  4. package/dist/cjs/model/liquidations.js +1 -1
  5. package/dist/cjs/model/liquidations.schema.js +1 -1
  6. package/dist/cjs/model/opportunities.js +1 -1
  7. package/dist/cjs/model/opportunities.schema.js +6 -13
  8. package/dist/cjs/model/positions.js +1 -1
  9. package/dist/cjs/model/positions.schema.js +33 -1
  10. package/dist/cjs/model/response.schema.js +9 -6
  11. package/dist/cjs/new-sdk/AbstractNamespace.js +46 -128
  12. package/dist/cjs/new-sdk/GearboxSDK.js +55 -42
  13. package/dist/cjs/new-sdk/errors/AllSourcesFailedError.js +20 -0
  14. package/dist/cjs/new-sdk/errors/MissingSourceError.js +25 -0
  15. package/dist/cjs/new-sdk/errors/NoSourceServedError.js +20 -0
  16. package/dist/cjs/new-sdk/errors/SourceChainMismatchError.js +34 -0
  17. package/dist/cjs/new-sdk/errors/SourceUnavailableError.js +25 -0
  18. package/dist/cjs/new-sdk/errors/assertSameChains.js +12 -0
  19. package/dist/cjs/new-sdk/errors/everyChainFailed.js +11 -0
  20. package/dist/cjs/new-sdk/errors/index.js +15 -0
  21. package/dist/cjs/new-sdk/index.js +23 -3
  22. package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +37 -89
  23. package/dist/cjs/new-sdk/positions/PositionsNamespace.js +27 -94
  24. package/dist/cjs/new-sdk/types.js +0 -21
  25. package/dist/cjs/new-sdk/utils/filterResponse.js +12 -0
  26. package/dist/cjs/new-sdk/utils/index.js +6 -0
  27. package/dist/cjs/new-sdk/utils/mergeChains.js +76 -0
  28. package/dist/cjs/new-sdk/utils/types.js +1 -0
  29. package/dist/cjs/offchain/AbstractOffchainNamespace.js +42 -65
  30. package/dist/cjs/offchain/GearboxAPI.js +12 -8
  31. package/dist/cjs/offchain/errors/OffchainInvalidJsonError.js +19 -0
  32. package/dist/cjs/offchain/errors/OffchainNotConfiguredError.js +20 -0
  33. package/dist/cjs/offchain/errors/OffchainNotImplementedError.js +19 -0
  34. package/dist/cjs/offchain/errors/OffchainRequestFailedError.js +24 -0
  35. package/dist/cjs/offchain/errors/OffchainStatusError.js +20 -0
  36. package/dist/cjs/offchain/errors/OffchainTransportError.js +31 -0
  37. package/dist/cjs/offchain/errors/OffchainValidationError.js +26 -0
  38. package/dist/cjs/offchain/errors/backendMessage.js +28 -0
  39. package/dist/cjs/offchain/errors/errorCause.js +12 -0
  40. package/dist/cjs/offchain/errors/index.js +21 -0
  41. package/dist/cjs/offchain/errors/readResponseBody.js +15 -0
  42. package/dist/cjs/offchain/index.js +22 -5
  43. package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +6 -3
  44. package/dist/cjs/offchain/positions/OffchainPositions.js +15 -21
  45. package/dist/cjs/offchain/types.js +0 -75
  46. package/dist/cjs/sdk/MultichainSDK.js +1 -1
  47. package/dist/cjs/sdk/OnchainSDK.js +1 -1
  48. package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
  49. package/dist/cjs/sdk/base/MultichainConstruct.js +73 -34
  50. package/dist/cjs/sdk/chain/chains.js +11 -0
  51. package/dist/cjs/sdk/chain/index.js +1 -0
  52. package/dist/cjs/sdk/core/index.js +1 -1
  53. package/dist/cjs/sdk/index.js +2 -1
  54. package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
  55. package/dist/cjs/sdk/opportunities/OpportunitiesService.js +1 -4
  56. package/dist/cjs/sdk/positions/MultichainPositionsService.js +6 -29
  57. package/dist/cjs/sdk/positions/PositionsService.js +1 -2
  58. package/dist/esm/dev/AccountOpener.js +1 -1
  59. package/dist/esm/dev/withdrawalUtils.js +1 -1
  60. package/dist/esm/model/filters.js +0 -4
  61. package/dist/esm/model/filters.schema.js +10 -2
  62. package/dist/esm/model/index.js +3 -3
  63. package/dist/esm/model/liquidations.js +1 -1
  64. package/dist/esm/model/liquidations.schema.js +1 -1
  65. package/dist/esm/model/opportunities.js +1 -1
  66. package/dist/esm/model/opportunities.schema.js +4 -11
  67. package/dist/esm/model/positions.js +1 -1
  68. package/dist/esm/model/positions.schema.js +33 -3
  69. package/dist/esm/model/response.schema.js +9 -6
  70. package/dist/esm/new-sdk/AbstractNamespace.js +46 -128
  71. package/dist/esm/new-sdk/GearboxSDK.js +55 -42
  72. package/dist/esm/new-sdk/errors/AllSourcesFailedError.js +19 -0
  73. package/dist/esm/new-sdk/errors/MissingSourceError.js +24 -0
  74. package/dist/esm/new-sdk/errors/NoSourceServedError.js +19 -0
  75. package/dist/esm/new-sdk/errors/SourceChainMismatchError.js +33 -0
  76. package/dist/esm/new-sdk/errors/SourceUnavailableError.js +24 -0
  77. package/dist/esm/new-sdk/errors/assertSameChains.js +11 -0
  78. package/dist/esm/new-sdk/errors/everyChainFailed.js +10 -0
  79. package/dist/esm/new-sdk/errors/index.js +8 -0
  80. package/dist/esm/new-sdk/index.js +13 -3
  81. package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +38 -90
  82. package/dist/esm/new-sdk/positions/PositionsNamespace.js +28 -95
  83. package/dist/esm/new-sdk/types.js +1 -21
  84. package/dist/esm/new-sdk/utils/filterResponse.js +11 -0
  85. package/dist/esm/new-sdk/utils/index.js +3 -1
  86. package/dist/esm/new-sdk/utils/mergeChains.js +73 -0
  87. package/dist/esm/new-sdk/utils/types.js +1 -0
  88. package/dist/esm/offchain/AbstractOffchainNamespace.js +41 -64
  89. package/dist/esm/offchain/GearboxAPI.js +12 -8
  90. package/dist/esm/offchain/errors/OffchainInvalidJsonError.js +18 -0
  91. package/dist/esm/offchain/errors/OffchainNotConfiguredError.js +19 -0
  92. package/dist/esm/offchain/errors/OffchainNotImplementedError.js +18 -0
  93. package/dist/esm/offchain/errors/OffchainRequestFailedError.js +23 -0
  94. package/dist/esm/offchain/errors/OffchainStatusError.js +19 -0
  95. package/dist/esm/offchain/errors/OffchainTransportError.js +30 -0
  96. package/dist/esm/offchain/errors/OffchainValidationError.js +25 -0
  97. package/dist/esm/offchain/errors/backendMessage.js +27 -0
  98. package/dist/esm/offchain/errors/errorCause.js +11 -0
  99. package/dist/esm/offchain/errors/index.js +11 -0
  100. package/dist/esm/offchain/errors/readResponseBody.js +14 -0
  101. package/dist/esm/offchain/index.js +13 -2
  102. package/dist/esm/offchain/opportunities/OffchainOpportunities.js +6 -3
  103. package/dist/esm/offchain/positions/OffchainPositions.js +15 -21
  104. package/dist/esm/offchain/types.js +1 -72
  105. package/dist/esm/plugins/adapters/contracts/ERC4626AdapterContract.js +1 -1
  106. package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
  107. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  108. package/dist/esm/sdk/MultichainSDK.js +1 -1
  109. package/dist/esm/sdk/OnchainSDK.js +1 -1
  110. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -2
  111. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +1 -1
  112. package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
  113. package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
  114. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  115. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  116. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  117. package/dist/esm/sdk/base/MultichainConstruct.js +73 -34
  118. package/dist/esm/sdk/base/TokensMeta.js +2 -2
  119. package/dist/esm/sdk/chain/chains.js +11 -1
  120. package/dist/esm/sdk/chain/detectNetwork.js +1 -1
  121. package/dist/esm/sdk/chain/index.js +2 -2
  122. package/dist/esm/sdk/core/createAddressProvider.js +1 -1
  123. package/dist/esm/sdk/core/index.js +1 -1
  124. package/dist/esm/sdk/index.js +3 -3
  125. package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
  126. package/dist/esm/sdk/market/pool/PoolV310Contract.js +1 -1
  127. package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
  128. package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
  129. package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
  130. package/dist/esm/sdk/opportunities/OpportunitiesService.js +1 -4
  131. package/dist/esm/sdk/pools/PoolService.js +1 -1
  132. package/dist/esm/sdk/positions/MultichainPositionsService.js +6 -29
  133. package/dist/esm/sdk/positions/PositionsService.js +1 -2
  134. package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
  135. package/dist/types/model/filters.d.ts +9 -3
  136. package/dist/types/model/filters.schema.d.ts +7 -1
  137. package/dist/types/model/index.d.ts +5 -5
  138. package/dist/types/model/liquidations.d.ts +2 -6
  139. package/dist/types/model/liquidations.schema.d.ts +1 -1
  140. package/dist/types/model/opportunities.d.ts +3 -7
  141. package/dist/types/model/opportunities.schema.d.ts +2 -2
  142. package/dist/types/model/positions.d.ts +2 -6
  143. package/dist/types/model/positions.schema.d.ts +32 -2
  144. package/dist/types/model/response.d.ts +7 -6
  145. package/dist/types/model/response.schema.d.ts +41 -32
  146. package/dist/types/new-sdk/AbstractNamespace.d.ts +33 -98
  147. package/dist/types/new-sdk/GearboxSDK.d.ts +10 -25
  148. package/dist/types/new-sdk/errors/AllSourcesFailedError.d.ts +11 -0
  149. package/dist/types/new-sdk/errors/MissingSourceError.d.ts +23 -0
  150. package/dist/types/new-sdk/errors/NoSourceServedError.d.ts +17 -0
  151. package/dist/types/new-sdk/errors/SourceChainMismatchError.d.ts +35 -0
  152. package/dist/types/new-sdk/errors/SourceUnavailableError.d.ts +22 -0
  153. package/dist/types/new-sdk/errors/assertSameChains.d.ts +11 -0
  154. package/dist/types/new-sdk/errors/everyChainFailed.d.ts +10 -0
  155. package/dist/types/new-sdk/errors/index.d.ts +8 -0
  156. package/dist/types/new-sdk/index.d.ts +17 -6
  157. package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +20 -40
  158. package/dist/types/new-sdk/opportunities/index.d.ts +2 -2
  159. package/dist/types/new-sdk/opportunities/types.d.ts +72 -26
  160. package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +18 -38
  161. package/dist/types/new-sdk/positions/index.d.ts +2 -2
  162. package/dist/types/new-sdk/positions/types.d.ts +68 -24
  163. package/dist/types/new-sdk/types.d.ts +24 -77
  164. package/dist/types/new-sdk/utils/filterResponse.d.ts +19 -0
  165. package/dist/types/new-sdk/utils/history.d.ts +6 -36
  166. package/dist/types/new-sdk/utils/index.d.ts +5 -2
  167. package/dist/types/new-sdk/utils/mergeChains.d.ts +22 -0
  168. package/dist/types/new-sdk/utils/types.d.ts +43 -0
  169. package/dist/types/offchain/AbstractOffchainNamespace.d.ts +21 -28
  170. package/dist/types/offchain/GearboxAPI.d.ts +13 -9
  171. package/dist/types/offchain/errors/OffchainInvalidJsonError.d.ts +16 -0
  172. package/dist/types/offchain/errors/OffchainNotConfiguredError.d.ts +16 -0
  173. package/dist/types/offchain/errors/OffchainNotImplementedError.d.ts +15 -0
  174. package/dist/types/offchain/errors/OffchainRequestFailedError.d.ts +12 -0
  175. package/dist/types/offchain/errors/OffchainStatusError.d.ts +16 -0
  176. package/dist/types/offchain/errors/OffchainTransportError.d.ts +43 -0
  177. package/dist/types/offchain/errors/OffchainValidationError.d.ts +21 -0
  178. package/dist/types/offchain/errors/backendMessage.d.ts +9 -0
  179. package/dist/types/offchain/errors/errorCause.d.ts +22 -0
  180. package/dist/types/offchain/errors/index.d.ts +11 -0
  181. package/dist/types/offchain/errors/readResponseBody.d.ts +8 -0
  182. package/dist/types/offchain/index.d.ts +13 -2
  183. package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +9 -8
  184. package/dist/types/offchain/positions/OffchainPositions.d.ts +9 -15
  185. package/dist/types/offchain/types.d.ts +7 -88
  186. package/dist/types/sdk/accounts/index.d.ts +2 -2
  187. package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +15 -14
  188. package/dist/types/sdk/accounts/liquidations/index.d.ts +2 -2
  189. package/dist/types/sdk/accounts/liquidations/types.d.ts +9 -15
  190. package/dist/types/sdk/base/MultichainConstruct.d.ts +63 -14
  191. package/dist/types/sdk/base/index.d.ts +2 -2
  192. package/dist/types/sdk/chain/chains.d.ts +10 -2
  193. package/dist/types/sdk/chain/index.d.ts +2 -2
  194. package/dist/types/sdk/index.d.ts +5 -5
  195. package/dist/types/sdk/market/MarketSuite.d.ts +1 -1
  196. package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +13 -16
  197. package/dist/types/sdk/positions/MultichainPositionsService.d.ts +2 -12
  198. package/dist/types/sdk/types/index.d.ts +2 -2
  199. package/dist/types/sdk/types/multichain.d.ts +8 -43
  200. package/package.json +1 -1
@@ -0,0 +1,19 @@
1
+ import { ChainScopedFilter } from "../../model/filters.js";
2
+ import { DataResponse } from "../../model/response.js";
3
+ import "../../model/index.js";
4
+ import { FilterResult } from "./types.js";
5
+ //#region src/new-sdk/utils/filterResponse.d.ts
6
+ /**
7
+ * Narrows an already-read list, rows and metadata alike. `undefined` passes
8
+ * through, so a read still in flight stays that way, and an envelope narrows to
9
+ * an envelope, see {@link FilterResult}.
10
+ *
11
+ * @typeParam T - Row type.
12
+ * @typeParam F - Filter type.
13
+ * @typeParam R - What the response was given as.
14
+ * @param matches - Whether one row satisfies the filter, e.g.
15
+ * `matchesOpportunityFilter`.
16
+ **/
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>;
18
+ //#endregion
19
+ export { filterResponse };
@@ -1,48 +1,18 @@
1
- import { HistoryChartMetadata, HistoryPoint, HistoryRange } from "../../model/history.js";
1
+ import { HistoryRange, HistorySeries } from "../../model/history.js";
2
+ import { DataResponse } from "../../model/response.js";
2
3
  import "../../model/index.js";
3
- import { SourceMeta } from "../types.js";
4
4
  //#region src/new-sdk/utils/history.d.ts
5
- /**
6
- * Everything that annotates a chart: what the backend said about the series,
7
- * plus which sources produced it.
8
- **/
9
- interface ChartMetadata extends HistoryChartMetadata {
10
- /**
11
- * Which sources answered, see {@link SourceMeta}. A chart is backend-only,
12
- * so this reports the backend alone.
13
- **/
14
- source: SourceMeta;
15
- }
16
- /**
17
- * One chart: the points to draw and everything that annotates them.
18
- **/
19
- interface Chart {
20
- /**
21
- * Samples, oldest first.
22
- **/
23
- data: HistoryPoint[];
24
- /**
25
- * Annotations of the series, see {@link ChartMetadata}.
26
- **/
27
- metadata: ChartMetadata;
28
- }
29
5
  /**
30
6
  * Reads the charts of one subject, one metric and one range at a time.
31
7
  *
32
- * The constraint is `string` rather than {@link HistoryMetric}, matching
33
- * {@link HistorySeries}: opportunities and positions carry their own metric
34
- * unions, and those are free to drift apart.
35
- *
36
8
  * @typeParam Metric - Metrics the subject has.
37
9
  **/
38
10
  interface HistoryReader<Metric extends string> {
39
11
  /**
40
- * Historical chart of one metric over one window.
41
- *
42
- * A metric the subject does not have is a compile error rather than an empty
43
- * chart.
12
+ * Historical chart of one metric over one window. A metric the subject does
13
+ * not have is a compile error.
44
14
  **/
45
- chart(metric: Metric, range: HistoryRange): Promise<Chart>;
15
+ chart(metric: Metric, range: HistoryRange): Promise<DataResponse<HistorySeries<Metric>>>;
46
16
  }
47
17
  //#endregion
48
- export { Chart, ChartMetadata, HistoryReader };
18
+ export { HistoryReader };
@@ -1,2 +1,5 @@
1
- import { Chart, ChartMetadata, HistoryReader } from "./history.js";
2
- export { Chart, ChartMetadata, HistoryReader };
1
+ import { EntityMerger, FilterResult, ListMerger, MergeListResult } from "./types.js";
2
+ import { filterResponse } from "./filterResponse.js";
3
+ import { HistoryReader } from "./history.js";
4
+ import { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne } from "./mergeChains.js";
5
+ export { DEFAULT_MAX_OFFCHAIN_LAG, type EntityMerger, type FilterResult, HistoryReader, type ListMerger, type MergeListResult, filterResponse, mergeChainList, mergeChainOne };
@@ -0,0 +1,22 @@
1
+ import { ChainScoped, DataResponse } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ import { MergeListResult } from "./types.js";
4
+ //#region src/new-sdk/utils/mergeChains.d.ts
5
+ /**
6
+ * How many seconds the backend may lag the chain and still be used.
7
+ **/
8
+ declare 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. Answers with an envelope as soon as
13
+ * either side has arrived, see {@link MergeListResult}.
14
+ **/
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[]>;
16
+ /**
17
+ * Merges two versions of one entity under the same freshness rule as
18
+ * {@link mergeChainList}, returning the winning response whole.
19
+ **/
20
+ declare function mergeChainOne<T>(onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined, maxLagSeconds?: number): DataResponse<T> | undefined;
21
+ //#endregion
22
+ export { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne };
@@ -0,0 +1,43 @@
1
+ import { DataResponse } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/utils/types.d.ts
4
+ /**
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.
38
+ *
39
+ * @typeParam T - Payload type of the read.
40
+ **/
41
+ type EntityMerger<T> = (onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined) => DataResponse<T> | undefined;
42
+ //#endregion
43
+ export { EntityMerger, FilterResult, ListMerger, MergeListResult };
@@ -1,6 +1,9 @@
1
1
  import { ILogger } from "../sdk/types/logger.js";
2
+ import { ChainId } from "../model/primitives.js";
3
+ import { ChainScopedFilter } from "../model/filters.js";
2
4
  import { HistoryMetric, HistoryRange, HistorySeries } from "../model/history.js";
3
- import { GearboxAPIOptions, OffchainResult } from "./types.js";
5
+ import { DataResponse } from "../model/response.js";
6
+ import { GearboxAPIOptions } from "./types.js";
4
7
  import { z } from "zod/v4";
5
8
  //#region src/offchain/AbstractOffchainNamespace.d.ts
6
9
  /**
@@ -23,8 +26,7 @@ interface OffchainGetRequest<S extends z.ZodType> {
23
26
  **/
24
27
  query?: OffchainQuery;
25
28
  /**
26
- * Schema the payload is decoded with. Decoding is what turns the response
27
- * into read model values, so it is required rather than optional.
29
+ * Schema the payload is decoded with.
28
30
  **/
29
31
  schema: S;
30
32
  }
@@ -39,49 +41,40 @@ interface OffchainHistoryRequest<M extends HistoryMetric> {
39
41
  **/
40
42
  path: string;
41
43
  /**
42
- * Metric the response must carry, see
43
- * {@link AbstractOffchainNamespace.readHistory}.
44
+ * Metric the response must carry.
44
45
  **/
45
46
  metric: M;
46
- /**
47
- * Window to cover.
48
- **/
49
47
  range: HistoryRange;
50
48
  }
51
49
  /**
52
- * Common logic of every {@link GearboxAPI} namespace.
53
- *
54
- * A namespace is a set of routes: it knows which path answers which read and
55
- * which schema describes the payload. Everything under that — where the
56
- * backend is, how long a request may take, how a failure is reported and how a
57
- * response becomes read model values — lives here, so a namespace holds
50
+ * Base class of every {@link GearboxAPI} namespace: issues the requests,
51
+ * decodes the responses and reports the failures, so that a namespace holds
58
52
  * nothing but its routes.
59
53
  **/
60
54
  declare abstract class AbstractOffchainNamespace {
61
55
  #private;
62
56
  protected readonly logger?: ILogger;
63
- protected constructor(name: string, options?: GearboxAPIOptions);
57
+ protected constructor(name: string, options: GearboxAPIOptions);
64
58
  /**
65
59
  * Base URL every read of this namespace is issued against.
66
60
  **/
67
61
  get baseUrl(): string | undefined;
68
62
  /**
69
- * Reads one endpoint and decodes its payload.
70
- *
71
- * A 2xx body is a {@link DataResponse}. Timeouts, non-2xx, and schema skew
72
- * throw — they are not a backend envelope, and this layer is what wraps
73
- * them, because the request may never have reached the backend.
63
+ * Chains one read covers: the configured ones, narrowed by the filter when it
64
+ * names any. A filter naming a chain this client does not cover narrows
65
+ * nothing.
66
+ **/
67
+ protected scopedChainIds(filter?: ChainScopedFilter): ChainId[];
68
+ /**
69
+ * Reads one endpoint and decodes its payload. Timeouts, non-2xx answers and
70
+ * schema skew throw, since none of them is a {@link DataResponse}.
74
71
  **/
75
- protected get<S extends z.ZodType>(request: OffchainGetRequest<S>): Promise<OffchainResult<z.output<S>>>;
72
+ protected get<S extends z.ZodType>(request: OffchainGetRequest<S>): Promise<DataResponse<z.output<S>>>;
76
73
  /**
77
- * Reads one historical series.
78
- *
79
- * The requested metric is pinned in the schema, which is what upholds the
80
- * `HistorySeries<M>` a caller gets back: a response carrying a different
81
- * metric is version skew and fails validation like any other mismatch,
82
- * rather than being cast into the requested shape.
74
+ * Reads one historical series. A response carrying a metric other than the
75
+ * requested one fails validation.
83
76
  **/
84
- protected readHistory<M extends HistoryMetric>(request: OffchainHistoryRequest<M>): Promise<OffchainResult<HistorySeries<M>>>;
77
+ protected readHistory<M extends HistoryMetric>(request: OffchainHistoryRequest<M>): Promise<DataResponse<HistorySeries<M>>>;
85
78
  }
86
79
  //#endregion
87
80
  export { AbstractOffchainNamespace, OffchainGetRequest, OffchainHistoryRequest, OffchainQuery };
@@ -1,3 +1,4 @@
1
+ import { ChainId } from "../model/primitives.js";
1
2
  import { GearboxAPIOptions } from "./types.js";
2
3
  import { OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
3
4
  import "./opportunities/index.js";
@@ -11,18 +12,21 @@ import "./positions/index.js";
11
12
  * call reads the same way against either source:
12
13
  *
13
14
  * ```ts
14
- * const api = new GearboxAPI({ baseUrl: "https://api.gearbox.fi" });
15
- * const { result } = await api.opportunities.list({ kind: "strategy" });
15
+ * const api = new GearboxAPI({
16
+ * baseUrl: "https://api.gearbox.fi",
17
+ * chainIds: [1, 42161],
18
+ * });
19
+ * const { data } = await api.opportunities.list({ kind: "strategy" });
16
20
  * ```
17
21
  *
18
- * The backend imports the read model from this package and its endpoints
19
- * return those types directly, so there is no wire DTO layer here — only
20
- * transport and schema validation.
21
- *
22
- * The transport is not implemented yet; see {@link OffchainOpportunities} and
23
- * {@link OffchainPositions} for what each stubbed endpoint currently answers.
22
+ * Every read is scoped to {@link GearboxAPIOptions.chainIds}, so a client only
23
+ * ever sees the chains it was built for even though the backend knows more.
24
24
  **/
25
25
  declare class GearboxAPI {
26
+ /**
27
+ * Chains every read of this client is scoped to.
28
+ **/
29
+ readonly chainIds: readonly ChainId[];
26
30
  /**
27
31
  * Namespace for pool and strategy opportunities.
28
32
  **/
@@ -31,7 +35,7 @@ declare class GearboxAPI {
31
35
  * Namespace for the positions a wallet holds.
32
36
  **/
33
37
  readonly positions: OffchainPositions;
34
- constructor(options?: GearboxAPIOptions);
38
+ constructor(options: GearboxAPIOptions);
35
39
  }
36
40
  //#endregion
37
41
  export { GearboxAPI };
@@ -0,0 +1,16 @@
1
+ import { OffchainTransportError } from "./OffchainTransportError.js";
2
+ //#region src/offchain/errors/OffchainInvalidJsonError.d.ts
3
+ /**
4
+ * Thrown when the backend answered in the 2xx range with a body that is not
5
+ * JSON, so there is nothing to validate against the read model.
6
+ **/
7
+ declare class OffchainInvalidJsonError extends OffchainTransportError {
8
+ name: string;
9
+ /**
10
+ * Status the backend answered with.
11
+ **/
12
+ readonly status: number;
13
+ constructor(url: string, status: number, cause: unknown);
14
+ }
15
+ //#endregion
16
+ export { OffchainInvalidJsonError };
@@ -0,0 +1,16 @@
1
+ import { BaseError } from "viem";
2
+ //#region src/offchain/errors/OffchainNotConfiguredError.d.ts
3
+ /**
4
+ * Thrown when a read is issued against a client that was never told where the
5
+ * backend is.
6
+ **/
7
+ declare class OffchainNotConfiguredError extends BaseError {
8
+ name: string;
9
+ /**
10
+ * Path that was read, below the base URL that is missing.
11
+ **/
12
+ readonly path: string;
13
+ constructor(path: string);
14
+ }
15
+ //#endregion
16
+ export { OffchainNotConfiguredError };
@@ -0,0 +1,15 @@
1
+ import { BaseError } from "viem";
2
+ //#region src/offchain/errors/OffchainNotImplementedError.d.ts
3
+ /**
4
+ * Thrown by the endpoints that are not implemented yet.
5
+ **/
6
+ declare class OffchainNotImplementedError extends BaseError {
7
+ name: string;
8
+ /**
9
+ * Endpoint that was asked for.
10
+ **/
11
+ readonly endpoint: string;
12
+ constructor(endpoint: string);
13
+ }
14
+ //#endregion
15
+ export { OffchainNotImplementedError };
@@ -0,0 +1,12 @@
1
+ import { OffchainTransportError } from "./OffchainTransportError.js";
2
+ //#region src/offchain/errors/OffchainRequestFailedError.d.ts
3
+ /**
4
+ * Thrown when a request to the backend never completed: the network was down,
5
+ * or it took longer than {@link GearboxAPIOptions.timeout}.
6
+ **/
7
+ declare class OffchainRequestFailedError extends OffchainTransportError {
8
+ name: string;
9
+ constructor(url: string, cause: unknown);
10
+ }
11
+ //#endregion
12
+ export { OffchainRequestFailedError };
@@ -0,0 +1,16 @@
1
+ import { OffchainTransportError } from "./OffchainTransportError.js";
2
+ //#region src/offchain/errors/OffchainStatusError.d.ts
3
+ /**
4
+ * Thrown when the backend answered with a status outside the 2xx range. What it
5
+ * said about it, when it said anything, is the `details` of the error.
6
+ **/
7
+ declare class OffchainStatusError extends OffchainTransportError {
8
+ name: string;
9
+ /**
10
+ * Status the backend answered with.
11
+ **/
12
+ readonly status: number;
13
+ constructor(url: string, status: number, body?: string);
14
+ }
15
+ //#endregion
16
+ export { OffchainStatusError };
@@ -0,0 +1,43 @@
1
+ import { BaseError } from "viem";
2
+ //#region src/offchain/errors/OffchainTransportError.d.ts
3
+ /**
4
+ * What every transport failure of a backend read names, see
5
+ * {@link OffchainTransportError}.
6
+ **/
7
+ interface OffchainTransportErrorParams {
8
+ /**
9
+ * URL that was requested, query included.
10
+ **/
11
+ url: string;
12
+ /**
13
+ * Status the backend answered with, absent when the request never completed.
14
+ **/
15
+ status?: number;
16
+ /**
17
+ * Value that was caught, normalised by {@link errorCause}.
18
+ **/
19
+ cause?: unknown;
20
+ /**
21
+ * What the backend itself said. Used only when there is no {@link cause},
22
+ * whose message viem reports instead.
23
+ **/
24
+ details?: string;
25
+ }
26
+ /**
27
+ * Thrown when the backend could not be reached, or answered with something
28
+ * other than a JSON body in the 2xx range. Its subclasses say which of the
29
+ * three it was; catch this one to catch all of them.
30
+ **/
31
+ declare abstract class OffchainTransportError extends BaseError {
32
+ /**
33
+ * URL that was requested, query included.
34
+ **/
35
+ readonly url: string;
36
+ /**
37
+ * Status the backend answered with, absent when the request never completed.
38
+ **/
39
+ readonly status?: number;
40
+ protected constructor(shortMessage: string, params: OffchainTransportErrorParams);
41
+ }
42
+ //#endregion
43
+ export { OffchainTransportError, OffchainTransportErrorParams };
@@ -0,0 +1,21 @@
1
+ import { BaseError } from "viem";
2
+ import { z } from "zod/v4";
3
+ //#region src/offchain/errors/OffchainValidationError.d.ts
4
+ /**
5
+ * Thrown when the backend answered, but with a payload the read model does not
6
+ * describe, i.e. the two are of different versions.
7
+ **/
8
+ declare class OffchainValidationError extends BaseError {
9
+ name: string;
10
+ /**
11
+ * URL whose payload failed to validate.
12
+ **/
13
+ readonly url: string;
14
+ /**
15
+ * The zod failure itself, e.g. for `z.treeifyError(error.zodError)`.
16
+ **/
17
+ readonly zodError: z.core.$ZodError;
18
+ constructor(url: string, zodError: z.core.$ZodError);
19
+ }
20
+ //#endregion
21
+ export { OffchainValidationError };
@@ -0,0 +1,9 @@
1
+ //#region src/offchain/errors/backendMessage.d.ts
2
+ /**
3
+ * Why a non-2xx response failed, in the backend's own words: the `message` of a
4
+ * Gearbox backend error body, the whole body when it is not one, and nothing
5
+ * when it said nothing at all.
6
+ **/
7
+ declare function backendMessage(body: string | undefined): string | undefined;
8
+ //#endregion
9
+ export { backendMessage };
@@ -0,0 +1,22 @@
1
+ //#region src/offchain/errors/errorCause.d.ts
2
+ /**
3
+ * The `cause` / `details` pair viem's `BaseError` accepts, see
4
+ * {@link errorCause}.
5
+ **/
6
+ interface ErrorCause {
7
+ /**
8
+ * The caught value when it was an `Error`, so `walk()` reaches it.
9
+ **/
10
+ cause?: Error;
11
+ /**
12
+ * The caught value stringified, when it was not an `Error`.
13
+ **/
14
+ details?: string;
15
+ }
16
+ /**
17
+ * Turns a caught `unknown` into what viem's `BaseError` takes: it types `cause`
18
+ * as an `Error`, so anything else can only be reported as `details`.
19
+ **/
20
+ declare function errorCause(error: unknown): ErrorCause;
21
+ //#endregion
22
+ export { ErrorCause, errorCause };
@@ -0,0 +1,11 @@
1
+ import { backendMessage } from "./backendMessage.js";
2
+ import { ErrorCause, errorCause } from "./errorCause.js";
3
+ import { OffchainTransportError, OffchainTransportErrorParams } from "./OffchainTransportError.js";
4
+ import { OffchainInvalidJsonError } from "./OffchainInvalidJsonError.js";
5
+ import { OffchainNotConfiguredError } from "./OffchainNotConfiguredError.js";
6
+ import { OffchainNotImplementedError } from "./OffchainNotImplementedError.js";
7
+ import { OffchainRequestFailedError } from "./OffchainRequestFailedError.js";
8
+ import { OffchainStatusError } from "./OffchainStatusError.js";
9
+ import { OffchainValidationError } from "./OffchainValidationError.js";
10
+ import { readResponseBody } from "./readResponseBody.js";
11
+ export { ErrorCause, OffchainInvalidJsonError, OffchainNotConfiguredError, OffchainNotImplementedError, OffchainRequestFailedError, OffchainStatusError, OffchainTransportError, OffchainTransportErrorParams, OffchainValidationError, backendMessage, errorCause, readResponseBody };
@@ -0,0 +1,8 @@
1
+ //#region src/offchain/errors/readResponseBody.d.ts
2
+ /**
3
+ * Body of a failed response as text, or nothing when it cannot be read: a
4
+ * failure explaining a failure is not itself worth throwing.
5
+ **/
6
+ declare function readResponseBody(response: Response): Promise<string | undefined>;
7
+ //#endregion
8
+ export { readResponseBody };
@@ -1,8 +1,19 @@
1
- import { GearboxAPIOptions, OffchainNotConfiguredError, OffchainNotImplementedError, OffchainResult, OffchainSourceMeta, OffchainTransportError, OffchainValidationError } from "./types.js";
1
+ import { GearboxAPIOptions } from "./types.js";
2
2
  import { AbstractOffchainNamespace, OffchainGetRequest, OffchainHistoryRequest, OffchainQuery } from "./AbstractOffchainNamespace.js";
3
+ import { backendMessage } from "./errors/backendMessage.js";
4
+ import { ErrorCause, errorCause } from "./errors/errorCause.js";
5
+ import { OffchainTransportError, OffchainTransportErrorParams } from "./errors/OffchainTransportError.js";
6
+ import { OffchainInvalidJsonError } from "./errors/OffchainInvalidJsonError.js";
7
+ import { OffchainNotConfiguredError } from "./errors/OffchainNotConfiguredError.js";
8
+ import { OffchainNotImplementedError } from "./errors/OffchainNotImplementedError.js";
9
+ import { OffchainRequestFailedError } from "./errors/OffchainRequestFailedError.js";
10
+ import { OffchainStatusError } from "./errors/OffchainStatusError.js";
11
+ import { OffchainValidationError } from "./errors/OffchainValidationError.js";
12
+ import { readResponseBody } from "./errors/readResponseBody.js";
13
+ import "./errors/index.js";
3
14
  import { OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
4
15
  import "./opportunities/index.js";
5
16
  import { OffchainPositions } from "./positions/OffchainPositions.js";
6
17
  import "./positions/index.js";
7
18
  import { GearboxAPI } from "./GearboxAPI.js";
8
- export { AbstractOffchainNamespace, GearboxAPI, GearboxAPIOptions, OffchainGetRequest, OffchainHistoryRequest, OffchainNotConfiguredError, OffchainNotImplementedError, OffchainOpportunities, OffchainPositions, OffchainQuery, OffchainResult, OffchainSourceMeta, OffchainTransportError, OffchainValidationError };
19
+ export { AbstractOffchainNamespace, ErrorCause, GearboxAPI, GearboxAPIOptions, OffchainGetRequest, OffchainHistoryRequest, OffchainInvalidJsonError, OffchainNotConfiguredError, OffchainNotImplementedError, OffchainOpportunities, OffchainPositions, OffchainQuery, OffchainRequestFailedError, OffchainStatusError, OffchainTransportError, OffchainTransportErrorParams, OffchainValidationError, backendMessage, errorCause, readResponseBody };
@@ -1,6 +1,7 @@
1
1
  import { Opportunity, OpportunityFilter, PoolOpportunityDetail, PoolOpportunityKey, StrategyOpportunityDetail, StrategyOpportunityKey } from "../../model/opportunities.js";
2
2
  import { HistoryMetric, HistorySeries, OpportunityHistoryQuery } from "../../model/history.js";
3
- import { GearboxAPIOptions, OffchainResult } from "../types.js";
3
+ import { DataResponse } from "../../model/response.js";
4
+ import { GearboxAPIOptions } from "../types.js";
4
5
  import { AbstractOffchainNamespace } from "../AbstractOffchainNamespace.js";
5
6
  //#region src/offchain/opportunities/OffchainOpportunities.d.ts
6
7
  /**
@@ -8,24 +9,24 @@ import { AbstractOffchainNamespace } from "../AbstractOffchainNamespace.js";
8
9
  **/
9
10
  declare class OffchainOpportunities extends AbstractOffchainNamespace {
10
11
  #private;
11
- constructor(options?: GearboxAPIOptions);
12
+ constructor(options: GearboxAPIOptions);
12
13
  /**
13
- * All opportunities the backend knows about, optionally narrowed by
14
- * {@link OpportunityFilter}.
14
+ * Opportunities of the chains this client covers, optionally narrowed further
15
+ * by {@link OpportunityFilter}.
15
16
  **/
16
- list(filter?: OpportunityFilter): Promise<OffchainResult<Opportunity[]>>;
17
+ list(filter?: OpportunityFilter): Promise<DataResponse<Opportunity[]>>;
17
18
  /**
18
19
  * Detailed view of one pool opportunity.
19
20
  **/
20
- getPool(key: PoolOpportunityKey): Promise<OffchainResult<PoolOpportunityDetail>>;
21
+ getPool(key: PoolOpportunityKey): Promise<DataResponse<PoolOpportunityDetail>>;
21
22
  /**
22
23
  * Detailed view of one strategy opportunity.
23
24
  **/
24
- getStrategy(key: StrategyOpportunityKey): Promise<OffchainResult<StrategyOpportunityDetail>>;
25
+ getStrategy(key: StrategyOpportunityKey): Promise<DataResponse<StrategyOpportunityDetail>>;
25
26
  /**
26
27
  * One historical series of one opportunity
27
28
  **/
28
- getHistory<M extends HistoryMetric>(query: OpportunityHistoryQuery<M>): Promise<OffchainResult<HistorySeries<M>>>;
29
+ getHistory<M extends HistoryMetric>(query: OpportunityHistoryQuery<M>): Promise<DataResponse<HistorySeries<M>>>;
29
30
  }
30
31
  //#endregion
31
32
  export { OffchainOpportunities };
@@ -1,32 +1,26 @@
1
- import { Position, PositionFilter } from "../../model/positions.js";
1
+ import { Position } from "../../model/positions.js";
2
2
  import { HistorySeries, PositionHistoryMetric, PositionHistoryQuery } from "../../model/history.js";
3
- import { GearboxAPIOptions, OffchainResult } from "../types.js";
3
+ import { DataResponse } from "../../model/response.js";
4
+ import { ListPositionsPropsBase } from "../../sdk/positions/types.js";
5
+ import { GearboxAPIOptions } from "../types.js";
4
6
  import { AbstractOffchainNamespace } from "../AbstractOffchainNamespace.js";
5
- import { Address } from "viem";
6
7
  //#region src/offchain/positions/OffchainPositions.d.ts
7
8
  /**
8
9
  * Backend counterpart of the `positions` namespace.
9
10
  **/
10
11
  declare class OffchainPositions extends AbstractOffchainNamespace {
11
- constructor(options?: GearboxAPIOptions);
12
+ #private;
13
+ constructor(options: GearboxAPIOptions);
12
14
  /**
13
15
  * Everything a wallet holds, optionally narrowed by {@link PositionFilter}.
14
- *
15
- * @returns An empty list until the backend client is implemented.
16
16
  **/
17
- list(wallet: Address, filter?: PositionFilter): Promise<OffchainResult<Position[]>>;
17
+ list(props: ListPositionsPropsBase): Promise<DataResponse<Position[]>>;
18
18
  /**
19
- * One historical series of one position. History exists only here: rebuilding
20
- * it from the chain would mean an archive read per point.
21
- *
22
- * The requested metric types the response, so a caller asking for one metric
23
- * does not have to narrow the union back down. When the transport lands,
24
- * validation is what upholds it: a response carrying a different metric than
25
- * the one asked for is a version-skew error like any other.
19
+ * One historical series of one position.
26
20
  *
27
21
  * @returns An empty series until the backend client is implemented.
28
22
  **/
29
- getHistory<M extends PositionHistoryMetric>(query: PositionHistoryQuery<M>): Promise<OffchainResult<HistorySeries<M>>>;
23
+ getHistory<M extends PositionHistoryMetric>(query: PositionHistoryQuery<M>): Promise<DataResponse<HistorySeries<M>>>;
30
24
  }
31
25
  //#endregion
32
26
  export { OffchainPositions };