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

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 +21 -0
  26. package/dist/cjs/new-sdk/utils/index.js +6 -0
  27. package/dist/cjs/new-sdk/utils/mergeChains.js +81 -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 +20 -0
  85. package/dist/esm/new-sdk/utils/index.js +3 -1
  86. package/dist/esm/new-sdk/utils/mergeChains.js +78 -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 +19 -40
  158. package/dist/types/new-sdk/opportunities/index.d.ts +2 -2
  159. package/dist/types/new-sdk/opportunities/types.d.ts +80 -26
  160. package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +17 -38
  161. package/dist/types/new-sdk/positions/index.d.ts +2 -2
  162. package/dist/types/new-sdk/positions/types.d.ts +74 -24
  163. package/dist/types/new-sdk/types.d.ts +24 -77
  164. package/dist/types/new-sdk/utils/filterResponse.d.ts +16 -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 +20 -0
  168. package/dist/types/new-sdk/utils/types.d.ts +11 -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
@@ -1,6 +1,5 @@
1
1
  import { ILogger } from "../sdk/types/logger.js";
2
2
  import { NetworkType } from "../sdk/chain/chains.js";
3
- import { MultichainNetworkMeta } from "../sdk/types/multichain.js";
4
3
  import { MultichainAttachOptions, MultichainSDK, MultichainSDKOptions } from "../sdk/MultichainSDK.js";
5
4
  import "../sdk/index.js";
6
5
  import { GearboxAPIOptions } from "../offchain/types.js";
@@ -8,89 +7,25 @@ import { GearboxAPI } from "../offchain/GearboxAPI.js";
8
7
  import "../offchain/index.js";
9
8
  //#region src/new-sdk/types.d.ts
10
9
  /**
11
- * Which sources a {@link GearboxSDK} reads from. Fixed at construction: a
12
- * running instance never changes mode, and the mode decides which methods
13
- * exist at all rather than what they return.
10
+ * Which sources a {@link GearboxSDK} reads from, and therefore which of its
11
+ * methods exist.
14
12
  **/
15
13
  type Mode = "onchain" | "offchain" | "both";
16
14
  /**
17
- * Outcome of the off-chain read behind a response.
18
- **/
19
- interface OffchainSourceStatus {
20
- /**
21
- * Whether the backend served the request.
22
- **/
23
- status: "success" | "error";
24
- /**
25
- * Rejection reason. Only set when {@link status} is `"error"`.
26
- **/
27
- error?: unknown;
28
- }
29
- /**
30
- * Which sources produced a response and which of them failed.
31
- *
32
- * Meta is always present, in every mode. A partial answer is the normal case
33
- * for a multi-chain read, so a caller that ignores this block is choosing to
34
- * treat "one chain is down" as "these opportunities do not exist".
35
- **/
36
- interface SourceMeta {
37
- /**
38
- * Outcome per queried chain. Empty when the read touched no chain, either
39
- * because the SDK is off-chain or because a filter selected no chain.
40
- **/
41
- chains: MultichainNetworkMeta[];
42
- /**
43
- * Outcome of the off-chain read, absent when the SDK has no backend source.
44
- **/
45
- offchain?: OffchainSourceStatus;
46
- }
47
- /**
48
- * What every {@link GearboxSDK} read returns.
49
- *
50
- * @typeParam T - Payload type.
51
- **/
52
- interface ReadResult<T> {
53
- /**
54
- * Requested payload, built from every source that answered.
55
- **/
56
- result: T;
57
- /**
58
- * Which sources produced it, see {@link SourceMeta}.
59
- **/
60
- meta: SourceMeta;
61
- }
62
- /**
63
- * Thrown when a read had sources to ask and none of them answered.
64
- *
65
- * A read that partially succeeded never throws: it returns what it has and
66
- * reports the rest in {@link SourceMeta}.
67
- **/
68
- declare class AllSourcesFailedError extends Error {
69
- /**
70
- * Outcome of every source the failed read asked.
71
- **/
72
- readonly meta: SourceMeta;
73
- constructor(action: string, meta: SourceMeta);
74
- }
75
- /**
76
- * On-chain source of a {@link GearboxSDK}: either an already-attached SDK to
77
- * reuse, or the options to build one with.
78
- *
79
- * An injected instance is never re-attached — its owner decides when it syncs.
15
+ * On-chain source of a {@link GearboxSDK}: either an SDK to reuse, which its
16
+ * owner keeps attaching itself, or the options to build one with.
80
17
  **/
81
18
  type OnchainSource = MultichainSDK | PlainMultichainSDKOptions;
82
19
  /**
83
20
  * {@link MultichainSDKOptions} without plugin typing.
84
- *
85
- * The facade exposes the on-chain SDK as a plain `MultichainSDK`; a consumer
86
- * that injects a plugin-typed instance keeps its own typed reference to it.
87
21
  **/
88
22
  type PlainMultichainSDKOptions = MultichainSDKOptions<{}>;
89
23
  /**
90
24
  * Off-chain source of a {@link GearboxSDK}: either a client to reuse or the
91
- * options to build one with.
25
+ * options to build one with. The chains come from
26
+ * {@link GearboxSDKOptions.networks} either way.
92
27
  **/
93
- type OffchainSource = GearboxAPI | GearboxAPIOptions;
28
+ type OffchainSource = GearboxAPI | Omit<GearboxAPIOptions, "chainIds">;
94
29
  /**
95
30
  * Options for creating a {@link GearboxSDK}.
96
31
  *
@@ -102,9 +37,8 @@ interface GearboxSDKOptions<M extends Mode = Mode> {
102
37
  **/
103
38
  mode: M;
104
39
  /**
105
- * Chains the SDK covers. This list is authoritative: when {@link onchain} is
106
- * an already-attached instance covering a different set of chains, the
107
- * mismatch is logged and this list still decides what is read.
40
+ * Chains the SDK covers, which every read of every source is scoped to. An
41
+ * injected source covering a different set is rejected at construction.
108
42
  **/
109
43
  networks: NetworkType[];
110
44
  /**
@@ -115,14 +49,27 @@ interface GearboxSDKOptions<M extends Mode = Mode> {
115
49
  * Off-chain source. Required in `offchain` and `both` mode.
116
50
  **/
117
51
  offchain?: OffchainSource;
52
+ /**
53
+ * How many seconds the backend may lag the chain and still serve a chain in
54
+ * `both` mode, see {@link DEFAULT_MAX_OFFCHAIN_LAG}.
55
+ **/
56
+ maxOffchainLagSeconds?: number;
118
57
  /**
119
58
  * Options passed to {@link MultichainSDK.attach}, used only when the SDK
120
59
  * builds the on-chain source itself.
121
60
  **/
122
61
  attach?: MultichainAttachOptions;
62
+ logger?: ILogger;
63
+ }
64
+ /**
65
+ * What a {@link GearboxSDK} hands every namespace it builds.
66
+ **/
67
+ interface NamespaceOptions {
123
68
  /**
124
- * Logger for source selection and degradation diagnostics.
69
+ * How far the backend may lag the chain, see
70
+ * {@link GearboxSDKOptions.maxOffchainLagSeconds}.
125
71
  **/
72
+ maxOffchainLagSeconds: number;
126
73
  logger?: ILogger;
127
74
  }
128
75
  /**
@@ -146,4 +93,4 @@ interface OffchainByMode {
146
93
  both: GearboxAPI;
147
94
  }
148
95
  //#endregion
149
- export { AllSourcesFailedError, GearboxSDKOptions, Mode, OffchainByMode, OffchainSource, OffchainSourceStatus, OnchainByMode, OnchainSource, PlainMultichainSDKOptions, ReadResult, SourceMeta };
96
+ export { GearboxSDKOptions, Mode, NamespaceOptions, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, PlainMultichainSDKOptions };
@@ -0,0 +1,16 @@
1
+ import { ChainScopedFilter } from "../../model/filters.js";
2
+ import { DataResponse } from "../../model/response.js";
3
+ import "../../model/index.js";
4
+ //#region src/new-sdk/utils/filterResponse.d.ts
5
+ /**
6
+ * Narrows an already-read list, rows and metadata alike. `undefined` passes
7
+ * through, so a read still in flight stays that way.
8
+ *
9
+ * @typeParam T - Row type.
10
+ * @typeParam F - Filter type.
11
+ * @param matches - Whether one row satisfies the filter, e.g.
12
+ * `matchesOpportunityFilter`.
13
+ **/
14
+ declare function filterResponse<T, F extends ChainScopedFilter>(response: DataResponse<T[]> | undefined, filter: F | undefined, matches: (row: T, filter?: F) => boolean): DataResponse<T[]> | undefined;
15
+ //#endregion
16
+ 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 { filterResponse } from "./filterResponse.js";
2
+ import { HistoryReader } from "./history.js";
3
+ 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 };
@@ -0,0 +1,20 @@
1
+ import { ChainScoped, DataResponse } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/utils/mergeChains.d.ts
4
+ /**
5
+ * How many seconds the backend may lag the chain and still be used.
6
+ **/
7
+ declare 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
+ declare function mergeChainList<T extends ChainScoped>(onchain: DataResponse<T[]> | undefined, offchain: DataResponse<T[]> | undefined, maxLagSeconds?: number): DataResponse<T[]> | undefined;
14
+ /**
15
+ * Merges two versions of one entity under the same freshness rule as
16
+ * {@link mergeChainList}, returning the winning response whole.
17
+ **/
18
+ declare function mergeChainOne<T>(onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined, maxLagSeconds?: number): DataResponse<T> | undefined;
19
+ //#endregion
20
+ export { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne };
@@ -0,0 +1,11 @@
1
+ import { DataResponse } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/utils/types.d.ts
4
+ /**
5
+ * How one read combines what the two sources returned.
6
+ *
7
+ * @typeParam T - Payload type of the read.
8
+ **/
9
+ type SourceMerger<T> = (onchain: DataResponse<T> | undefined, offchain: DataResponse<T> | undefined) => DataResponse<T> | undefined;
10
+ //#endregion
11
+ export { SourceMerger };
@@ -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 };