@gearbox-protocol/sdk 14.12.0-next.80 → 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 (221) 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 +11 -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.js +1 -0
  11. package/dist/cjs/model/response.schema.js +64 -0
  12. package/dist/cjs/new-sdk/AbstractNamespace.js +46 -128
  13. package/dist/cjs/new-sdk/GearboxSDK.js +55 -42
  14. package/dist/cjs/new-sdk/errors/AllSourcesFailedError.js +20 -0
  15. package/dist/cjs/new-sdk/errors/MissingSourceError.js +25 -0
  16. package/dist/cjs/new-sdk/errors/NoSourceServedError.js +20 -0
  17. package/dist/cjs/new-sdk/errors/SourceChainMismatchError.js +34 -0
  18. package/dist/cjs/new-sdk/errors/SourceUnavailableError.js +25 -0
  19. package/dist/cjs/new-sdk/errors/assertSameChains.js +12 -0
  20. package/dist/cjs/new-sdk/errors/everyChainFailed.js +11 -0
  21. package/dist/cjs/new-sdk/errors/index.js +15 -0
  22. package/dist/cjs/new-sdk/index.js +23 -3
  23. package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +37 -89
  24. package/dist/cjs/new-sdk/positions/PositionsNamespace.js +27 -94
  25. package/dist/cjs/new-sdk/types.js +0 -21
  26. package/dist/cjs/new-sdk/utils/filterResponse.js +21 -0
  27. package/dist/cjs/new-sdk/utils/index.js +6 -0
  28. package/dist/cjs/new-sdk/utils/mergeChains.js +81 -0
  29. package/dist/cjs/new-sdk/utils/types.js +1 -0
  30. package/dist/cjs/offchain/AbstractOffchainNamespace.js +42 -65
  31. package/dist/cjs/offchain/GearboxAPI.js +12 -8
  32. package/dist/cjs/offchain/errors/OffchainInvalidJsonError.js +19 -0
  33. package/dist/cjs/offchain/errors/OffchainNotConfiguredError.js +20 -0
  34. package/dist/cjs/offchain/errors/OffchainNotImplementedError.js +19 -0
  35. package/dist/cjs/offchain/errors/OffchainRequestFailedError.js +24 -0
  36. package/dist/cjs/offchain/errors/OffchainStatusError.js +20 -0
  37. package/dist/cjs/offchain/errors/OffchainTransportError.js +31 -0
  38. package/dist/cjs/offchain/errors/OffchainValidationError.js +26 -0
  39. package/dist/cjs/offchain/errors/backendMessage.js +28 -0
  40. package/dist/cjs/offchain/errors/errorCause.js +12 -0
  41. package/dist/cjs/offchain/errors/index.js +21 -0
  42. package/dist/cjs/offchain/errors/readResponseBody.js +15 -0
  43. package/dist/cjs/offchain/index.js +22 -5
  44. package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +6 -3
  45. package/dist/cjs/offchain/positions/OffchainPositions.js +15 -21
  46. package/dist/cjs/offchain/types.js +0 -75
  47. package/dist/cjs/sdk/MultichainSDK.js +1 -1
  48. package/dist/cjs/sdk/OnchainSDK.js +1 -1
  49. package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +6 -6
  50. package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +26 -20
  51. package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
  52. package/dist/cjs/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +7 -5
  53. package/dist/cjs/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +3 -2
  54. package/dist/cjs/sdk/base/MultichainConstruct.js +73 -34
  55. package/dist/cjs/sdk/chain/chains.js +11 -0
  56. package/dist/cjs/sdk/chain/index.js +1 -0
  57. package/dist/cjs/sdk/core/index.js +1 -1
  58. package/dist/cjs/sdk/index.js +2 -1
  59. package/dist/cjs/sdk/market/rwa/RWARegistry.js +4 -2
  60. package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
  61. package/dist/cjs/sdk/opportunities/OpportunitiesService.js +1 -4
  62. package/dist/cjs/sdk/pools/PoolService.js +2 -1
  63. package/dist/cjs/sdk/positions/MultichainPositionsService.js +6 -29
  64. package/dist/cjs/sdk/positions/PositionsService.js +12 -6
  65. package/dist/esm/dev/AccountOpener.js +1 -1
  66. package/dist/esm/dev/withdrawalUtils.js +1 -1
  67. package/dist/esm/model/filters.js +0 -4
  68. package/dist/esm/model/filters.schema.js +10 -2
  69. package/dist/esm/model/index.js +5 -3
  70. package/dist/esm/model/liquidations.js +1 -1
  71. package/dist/esm/model/liquidations.schema.js +1 -1
  72. package/dist/esm/model/opportunities.js +1 -1
  73. package/dist/esm/model/opportunities.schema.js +4 -11
  74. package/dist/esm/model/positions.js +1 -1
  75. package/dist/esm/model/positions.schema.js +33 -3
  76. package/dist/esm/model/response.js +1 -0
  77. package/dist/esm/model/response.schema.js +58 -0
  78. package/dist/esm/new-sdk/AbstractNamespace.js +46 -128
  79. package/dist/esm/new-sdk/GearboxSDK.js +55 -42
  80. package/dist/esm/new-sdk/errors/AllSourcesFailedError.js +19 -0
  81. package/dist/esm/new-sdk/errors/MissingSourceError.js +24 -0
  82. package/dist/esm/new-sdk/errors/NoSourceServedError.js +19 -0
  83. package/dist/esm/new-sdk/errors/SourceChainMismatchError.js +33 -0
  84. package/dist/esm/new-sdk/errors/SourceUnavailableError.js +24 -0
  85. package/dist/esm/new-sdk/errors/assertSameChains.js +11 -0
  86. package/dist/esm/new-sdk/errors/everyChainFailed.js +10 -0
  87. package/dist/esm/new-sdk/errors/index.js +8 -0
  88. package/dist/esm/new-sdk/index.js +13 -3
  89. package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +38 -90
  90. package/dist/esm/new-sdk/positions/PositionsNamespace.js +28 -95
  91. package/dist/esm/new-sdk/types.js +1 -21
  92. package/dist/esm/new-sdk/utils/filterResponse.js +20 -0
  93. package/dist/esm/new-sdk/utils/index.js +3 -1
  94. package/dist/esm/new-sdk/utils/mergeChains.js +78 -0
  95. package/dist/esm/new-sdk/utils/types.js +1 -0
  96. package/dist/esm/offchain/AbstractOffchainNamespace.js +41 -64
  97. package/dist/esm/offchain/GearboxAPI.js +12 -8
  98. package/dist/esm/offchain/errors/OffchainInvalidJsonError.js +18 -0
  99. package/dist/esm/offchain/errors/OffchainNotConfiguredError.js +19 -0
  100. package/dist/esm/offchain/errors/OffchainNotImplementedError.js +18 -0
  101. package/dist/esm/offchain/errors/OffchainRequestFailedError.js +23 -0
  102. package/dist/esm/offchain/errors/OffchainStatusError.js +19 -0
  103. package/dist/esm/offchain/errors/OffchainTransportError.js +30 -0
  104. package/dist/esm/offchain/errors/OffchainValidationError.js +25 -0
  105. package/dist/esm/offchain/errors/backendMessage.js +27 -0
  106. package/dist/esm/offchain/errors/errorCause.js +11 -0
  107. package/dist/esm/offchain/errors/index.js +11 -0
  108. package/dist/esm/offchain/errors/readResponseBody.js +14 -0
  109. package/dist/esm/offchain/index.js +13 -2
  110. package/dist/esm/offchain/opportunities/OffchainOpportunities.js +6 -3
  111. package/dist/esm/offchain/positions/OffchainPositions.js +15 -21
  112. package/dist/esm/offchain/types.js +1 -72
  113. package/dist/esm/plugins/adapters/contracts/ERC4626AdapterContract.js +1 -1
  114. package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
  115. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  116. package/dist/esm/sdk/MultichainSDK.js +1 -1
  117. package/dist/esm/sdk/OnchainSDK.js +1 -1
  118. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -2
  119. package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +6 -6
  120. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +27 -21
  121. package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
  122. package/dist/esm/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +7 -5
  123. package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
  124. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  125. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  126. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +4 -3
  127. package/dist/esm/sdk/base/MultichainConstruct.js +73 -34
  128. package/dist/esm/sdk/base/TokensMeta.js +2 -2
  129. package/dist/esm/sdk/chain/chains.js +11 -1
  130. package/dist/esm/sdk/chain/detectNetwork.js +1 -1
  131. package/dist/esm/sdk/chain/index.js +2 -2
  132. package/dist/esm/sdk/core/createAddressProvider.js +1 -1
  133. package/dist/esm/sdk/core/index.js +1 -1
  134. package/dist/esm/sdk/index.js +3 -3
  135. package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
  136. package/dist/esm/sdk/market/pool/PoolV310Contract.js +1 -1
  137. package/dist/esm/sdk/market/rwa/RWARegistry.js +4 -2
  138. package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
  139. package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
  140. package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
  141. package/dist/esm/sdk/opportunities/OpportunitiesService.js +1 -4
  142. package/dist/esm/sdk/pools/PoolService.js +3 -2
  143. package/dist/esm/sdk/positions/MultichainPositionsService.js +6 -29
  144. package/dist/esm/sdk/positions/PositionsService.js +12 -6
  145. package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
  146. package/dist/types/model/filters.d.ts +9 -3
  147. package/dist/types/model/filters.schema.d.ts +7 -1
  148. package/dist/types/model/index.d.ts +7 -5
  149. package/dist/types/model/liquidations.d.ts +2 -6
  150. package/dist/types/model/liquidations.schema.d.ts +1 -1
  151. package/dist/types/model/opportunities.d.ts +3 -7
  152. package/dist/types/model/opportunities.schema.d.ts +2 -2
  153. package/dist/types/model/positions.d.ts +2 -6
  154. package/dist/types/model/positions.schema.d.ts +32 -2
  155. package/dist/types/model/response.d.ts +105 -0
  156. package/dist/types/model/response.schema.d.ts +98 -0
  157. package/dist/types/new-sdk/AbstractNamespace.d.ts +33 -98
  158. package/dist/types/new-sdk/GearboxSDK.d.ts +10 -25
  159. package/dist/types/new-sdk/errors/AllSourcesFailedError.d.ts +11 -0
  160. package/dist/types/new-sdk/errors/MissingSourceError.d.ts +23 -0
  161. package/dist/types/new-sdk/errors/NoSourceServedError.d.ts +17 -0
  162. package/dist/types/new-sdk/errors/SourceChainMismatchError.d.ts +35 -0
  163. package/dist/types/new-sdk/errors/SourceUnavailableError.d.ts +22 -0
  164. package/dist/types/new-sdk/errors/assertSameChains.d.ts +11 -0
  165. package/dist/types/new-sdk/errors/everyChainFailed.d.ts +10 -0
  166. package/dist/types/new-sdk/errors/index.d.ts +8 -0
  167. package/dist/types/new-sdk/index.d.ts +17 -6
  168. package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +19 -40
  169. package/dist/types/new-sdk/opportunities/index.d.ts +2 -2
  170. package/dist/types/new-sdk/opportunities/types.d.ts +80 -26
  171. package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +17 -38
  172. package/dist/types/new-sdk/positions/index.d.ts +2 -2
  173. package/dist/types/new-sdk/positions/types.d.ts +74 -24
  174. package/dist/types/new-sdk/types.d.ts +24 -77
  175. package/dist/types/new-sdk/utils/filterResponse.d.ts +16 -0
  176. package/dist/types/new-sdk/utils/history.d.ts +6 -36
  177. package/dist/types/new-sdk/utils/index.d.ts +5 -2
  178. package/dist/types/new-sdk/utils/mergeChains.d.ts +20 -0
  179. package/dist/types/new-sdk/utils/types.d.ts +11 -0
  180. package/dist/types/offchain/AbstractOffchainNamespace.d.ts +21 -28
  181. package/dist/types/offchain/GearboxAPI.d.ts +13 -9
  182. package/dist/types/offchain/errors/OffchainInvalidJsonError.d.ts +16 -0
  183. package/dist/types/offchain/errors/OffchainNotConfiguredError.d.ts +16 -0
  184. package/dist/types/offchain/errors/OffchainNotImplementedError.d.ts +15 -0
  185. package/dist/types/offchain/errors/OffchainRequestFailedError.d.ts +12 -0
  186. package/dist/types/offchain/errors/OffchainStatusError.d.ts +16 -0
  187. package/dist/types/offchain/errors/OffchainTransportError.d.ts +43 -0
  188. package/dist/types/offchain/errors/OffchainValidationError.d.ts +21 -0
  189. package/dist/types/offchain/errors/backendMessage.d.ts +9 -0
  190. package/dist/types/offchain/errors/errorCause.d.ts +22 -0
  191. package/dist/types/offchain/errors/index.d.ts +11 -0
  192. package/dist/types/offchain/errors/readResponseBody.d.ts +8 -0
  193. package/dist/types/offchain/index.d.ts +13 -2
  194. package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +9 -8
  195. package/dist/types/offchain/positions/OffchainPositions.d.ts +9 -15
  196. package/dist/types/offchain/types.d.ts +7 -88
  197. package/dist/types/sdk/accounts/credit-account-compressor/types.d.ts +4 -0
  198. package/dist/types/sdk/accounts/index.d.ts +3 -3
  199. package/dist/types/sdk/accounts/liquidations/LiquidationsService.d.ts +2 -2
  200. package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +15 -14
  201. package/dist/types/sdk/accounts/liquidations/index.d.ts +2 -2
  202. package/dist/types/sdk/accounts/liquidations/types.d.ts +32 -16
  203. package/dist/types/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.d.ts +4 -4
  204. package/dist/types/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.d.ts +2 -2
  205. package/dist/types/sdk/accounts/withdrawal-compressor/index.d.ts +2 -2
  206. package/dist/types/sdk/accounts/withdrawal-compressor/types.d.ts +26 -4
  207. package/dist/types/sdk/base/MultichainConstruct.d.ts +63 -14
  208. package/dist/types/sdk/base/index.d.ts +2 -2
  209. package/dist/types/sdk/chain/chains.d.ts +10 -2
  210. package/dist/types/sdk/chain/index.d.ts +2 -2
  211. package/dist/types/sdk/index.d.ts +7 -7
  212. package/dist/types/sdk/market/MarketSuite.d.ts +1 -1
  213. package/dist/types/sdk/market/rwa/RWARegistry.d.ts +2 -1
  214. package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +13 -16
  215. package/dist/types/sdk/pools/types.d.ts +4 -0
  216. package/dist/types/sdk/positions/MultichainPositionsService.d.ts +2 -12
  217. package/dist/types/sdk/positions/index.d.ts +2 -2
  218. package/dist/types/sdk/positions/types.d.ts +14 -5
  219. package/dist/types/sdk/types/index.d.ts +2 -2
  220. package/dist/types/sdk/types/multichain.d.ts +16 -36
  221. package/package.json +1 -1
@@ -1,119 +1,54 @@
1
1
  import { ILogger } from "../sdk/types/logger.js";
2
- import { ChainId } from "../model/primitives.js";
3
- import { NetworkType } from "../sdk/chain/chains.js";
4
- import { MultichainNetworkMeta } from "../sdk/types/multichain.js";
2
+ import { DataResponse } from "../model/response.js";
5
3
  import "../model/index.js";
6
- import { MultichainSDK } from "../sdk/MultichainSDK.js";
7
- import "../sdk/index.js";
8
- import { OffchainResult } from "../offchain/types.js";
9
- import { GearboxAPI } from "../offchain/GearboxAPI.js";
10
- import "../offchain/index.js";
11
- import { OffchainSourceStatus, ReadResult, SourceMeta } from "./types.js";
4
+ import { NamespaceOptions } from "./types.js";
5
+ import { SourceMerger } from "./utils/types.js";
6
+ import "./utils/index.js";
12
7
  //#region src/new-sdk/AbstractNamespace.d.ts
13
8
  /**
14
- * What the on-chain source contributed, after per-chain failures have been
15
- * turned into metadata.
9
+ * One read of a combined namespace: the same query against each source, plus
10
+ * the policy that turns the two answers into one.
16
11
  *
17
- * @internal
12
+ * @typeParam Onchain - On-chain source namespace.
13
+ * @typeParam Offchain - Backend source namespace.
14
+ * @typeParam T - Payload type of the read.
18
15
  **/
19
- interface OnchainContribution<T> {
20
- value?: T;
21
- chains: MultichainNetworkMeta[];
16
+ interface MergedQuery<Onchain, Offchain, T> {
17
+ fromChain: (source: Onchain) => Promise<DataResponse<T>>;
18
+ fromBackend: (source: Offchain) => Promise<DataResponse<T>>;
19
+ merge: SourceMerger<T>;
22
20
  }
23
21
  /**
24
- * What the off-chain source contributed, after a rejection has been turned into
25
- * metadata.
22
+ * Base class of every {@link GearboxSDK} namespace: holds the two source
23
+ * namespaces, exposes each on its own, and runs merged reads over both.
26
24
  *
27
- * @internal
25
+ * @typeParam Onchain - On-chain source namespace, e.g.
26
+ * `MultichainOpportunitiesService`.
27
+ * @typeParam Offchain - Backend source namespace, e.g. `OffchainOpportunities`.
28
28
  **/
29
- interface OffchainContribution<T> {
30
- value?: T;
31
- status?: OffchainSourceStatus;
32
- }
33
- /**
34
- * How a read turns the two contributions into one payload. Either side is
35
- * absent when its source was not asked, or was asked and failed.
36
- *
37
- * @internal
38
- **/
39
- type CombineSources<T> = (onchain: T | undefined, offchain: T | undefined) => T;
40
- /**
41
- * Shared machinery of every {@link GearboxSDK} namespace.
42
- *
43
- * A namespace is a stateless router: it asks the sources its mode allows, in
44
- * parallel, merges what came back and reports what did not. This class owns
45
- * that routing — per-chain failure capture, backend degradation, the response
46
- * envelope and the all-sources-failed rule — so a namespace holds nothing but
47
- * its reads and its merge policy.
48
- *
49
- * The sources are whatever it was handed, and their readiness is their owner's
50
- * business: a source that is not ready to answer fails the read it was given,
51
- * and that failure is reported like any other.
52
- *
53
- * All protocol knowledge lives in the sources: `MultichainSDK` for the chain,
54
- * `GearboxAPI` for the backend. None of it is repeated here or in a subclass.
55
- *
56
- * @typeParam ListRow - Row type of the namespace's list read.
57
- **/
58
- declare abstract class AbstractNamespace<ListRow extends object> {
29
+ declare abstract class AbstractNamespace<Onchain, Offchain> {
59
30
  #private;
60
- protected readonly logger?: ILogger;
61
- protected constructor(name: string, onchain: MultichainSDK | undefined, offchain: GearboxAPI | undefined, logger?: ILogger);
62
- /**
63
- * Per-field source policy for one row of this namespace.
64
- **/
65
- protected abstract mergeOne<T extends object>(onchain: T, offchain: T): T;
66
- /**
67
- * Union policy for this namespace's list read.
68
- **/
69
- protected abstract mergeList(onchain: ListRow[], offchain: ListRow[]): ListRow[];
70
- /**
71
- * Asks both sources in parallel and combines whatever answered.
72
- *
73
- * The action name is only used in diagnostics and in the error raised when
74
- * every source the read had failed.
75
- **/
76
- protected read<T>(action: string, fromChain: (sdk: MultichainSDK) => Promise<OnchainContribution<T>>, fromBackend: (api: GearboxAPI) => Promise<OffchainResult<T>>, combine: CombineSources<T>): Promise<ReadResult<T>>;
77
- /**
78
- * Reads the namespace's list from both sources, unioned by
79
- * {@link AbstractNamespace.mergeList}.
80
- **/
81
- protected readList(action: string, fromChain: (sdk: MultichainSDK) => Promise<OnchainContribution<ListRow[]>>, fromBackend: (api: GearboxAPI) => Promise<OffchainResult<ListRow[]>>): Promise<ReadResult<ListRow[]>>;
82
- /**
83
- * Reads one row from both sources and merges them under
84
- * {@link AbstractNamespace.mergeOne}. The key names the chain, so the
85
- * on-chain leg queries exactly one.
86
- **/
87
- protected readOne<T extends object>(action: string, chainId: ChainId, fromChain: (sdk: MultichainSDK) => Promise<T>, fromBackend: (api: GearboxAPI) => Promise<OffchainResult<T>>): Promise<ReadResult<T>>;
88
31
  /**
89
- * Reads something only the backend can answer.
90
- *
91
- * The fallback stands in for a backend that answered with nothing, which is
92
- * a different thing from a backend that failed and was dropped.
32
+ * How far the backend may lag the chain and still serve a chain.
93
33
  **/
94
- protected readOffchain<T>(action: string, fromBackend: (api: GearboxAPI) => Promise<OffchainResult<T>>, fallback: T): Promise<ReadResult<T>>;
95
- /**
96
- * Runs a read against the chain, or reports the chain as absent when the
97
- * namespace has no on-chain source at all.
98
- **/
99
- protected fromChain<T>(run: (sdk: MultichainSDK) => Promise<OnchainContribution<T>>): Promise<OnchainContribution<T>>;
34
+ protected readonly maxOffchainLagSeconds: number;
35
+ protected readonly logger?: ILogger;
36
+ protected constructor(name: string, onchain: Onchain | undefined, offchain: Offchain | undefined, options: NamespaceOptions);
100
37
  /**
101
- * Runs a read against the backend, turning any rejection into metadata so
102
- * that a dead backend degrades a `both`-mode read instead of failing it.
38
+ * The on-chain source namespace on its own. Throws
39
+ * {@link SourceUnavailableError} in `offchain` mode.
103
40
  **/
104
- protected fromBackend<T>(run: (api: GearboxAPI) => Promise<OffchainResult<T>>): Promise<OffchainContribution<T>>;
41
+ get onchain(): Onchain;
105
42
  /**
106
- * Runs a read against one chain, turning a rejection into that chain's
107
- * metadata entry.
43
+ * The backend source namespace on its own. Throws
44
+ * {@link SourceUnavailableError} in `onchain` mode.
108
45
  **/
109
- protected onOneChain<T>(action: string, network: NetworkType, run: () => Promise<T>): Promise<OnchainContribution<T>>;
46
+ get offchain(): Offchain;
110
47
  /**
111
- * A read that reached at least one source returns what it has; a read where
112
- * every source it had failed throws, because an empty result would otherwise
113
- * be indistinguishable from "there is nothing here".
48
+ * Asks both sources at once and merges what came back, failing only when no
49
+ * chain was served at all.
114
50
  **/
115
- protected assertAnySourceSucceeded(action: string, meta: SourceMeta): void;
116
- protected networkOf(chainId: ChainId): NetworkType | undefined;
51
+ protected merged<T>(action: string, query: MergedQuery<Onchain, Offchain, T>): Promise<DataResponse<T>>;
117
52
  }
118
53
  //#endregion
119
- export { AbstractNamespace, CombineSources, OffchainContribution, OnchainContribution };
54
+ export { AbstractNamespace, MergedQuery };
@@ -7,12 +7,9 @@ import { Positions } from "./positions/types.js";
7
7
  import "./positions/index.js";
8
8
  //#region src/new-sdk/GearboxSDK.d.ts
9
9
  /**
10
- * Combined entry point over both sources of Gearbox data: the chain and the
11
- * Gearbox backend.
12
- *
13
- * The {@link Mode} is fixed at construction and decides which methods exist,
14
- * not what they return, so a screen written against `both` cannot silently
15
- * degrade to a subset when someone reconfigures the SDK:
10
+ * Entry point over both sources of Gearbox data: the chain and the Gearbox
11
+ * backend. The {@link Mode} is fixed at construction and decides which methods
12
+ * exist.
16
13
  *
17
14
  * ```ts
18
15
  * const sdk = new GearboxSDK({
@@ -23,13 +20,9 @@ import "./positions/index.js";
23
20
  * });
24
21
  * await sdk.attach();
25
22
  *
26
- * const { result, meta } = await sdk.opportunities.list({ kind: "strategy" });
23
+ * const { data, meta } = await sdk.opportunities.list({ kind: "strategy" });
27
24
  * ```
28
25
  *
29
- * Every read answers with `{ result, meta }`: `meta` says which chains and
30
- * which backend answered, because a partial answer is the normal case for a
31
- * multi-chain read.
32
- *
33
26
  * @typeParam M - Mode the instance was built in.
34
27
  **/
35
28
  declare class GearboxSDK<const M extends Mode = Mode> {
@@ -39,8 +32,7 @@ declare class GearboxSDK<const M extends Mode = Mode> {
39
32
  **/
40
33
  readonly mode: M;
41
34
  /**
42
- * Chains this instance covers. Authoritative even when an injected on-chain
43
- * SDK covers a different set.
35
+ * Chains this instance covers, which every read is scoped to.
44
36
  **/
45
37
  readonly networks: readonly NetworkType[];
46
38
  /**
@@ -53,26 +45,19 @@ declare class GearboxSDK<const M extends Mode = Mode> {
53
45
  readonly positions: Positions<M>;
54
46
  constructor(options: GearboxSDKOptions<M>);
55
47
  /**
56
- * Attaches the on-chain SDK when this instance owns one.
57
- *
58
- * Reads issued before this resolves still reach the chain and fail there, so
59
- * in `both` mode they are served from the backend alone and report the chain
60
- * as failed in their meta, rather than blocking.
61
- *
62
- * A no-op in `offchain` mode and when an already-attached SDK was injected.
48
+ * Attaches the on-chain SDK when this instance owns one; a no-op in `offchain`
49
+ * mode and when an already-attached SDK was injected.
63
50
  **/
64
51
  attach(): Promise<void>;
65
52
  /**
66
53
  * Whether the on-chain source is ready to be read from. Always `true` in
67
- * `offchain` mode, where there is nothing to wait for.
54
+ * `offchain` mode.
68
55
  **/
69
56
  get attached(): boolean;
70
57
  /**
71
58
  * The underlying on-chain SDK, for everything this facade does not expose
72
- * yet: markets, credit accounts, transaction building.
73
- *
74
- * `undefined` in `offchain` mode. Otherwise it exists from construction on,
75
- * but only answers reads once {@link attach} has resolved.
59
+ * yet: markets, credit accounts, transaction building. `undefined` in
60
+ * `offchain` mode, and only answers reads once {@link attach} has resolved.
76
61
  **/
77
62
  get onchain(): OnchainByMode[M];
78
63
  /**
@@ -0,0 +1,11 @@
1
+ import { ResponseMetadata } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/errors/AllSourcesFailedError.d.ts
4
+ /**
5
+ * Thrown when a read had sources to ask and none of them served a single chain.
6
+ **/
7
+ declare class AllSourcesFailedError extends AggregateError {
8
+ constructor(action: string, sourceErrors: unknown[], meta?: ResponseMetadata);
9
+ }
10
+ //#endregion
11
+ export { AllSourcesFailedError };
@@ -0,0 +1,23 @@
1
+ import { DataSource } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ import { Mode } from "../types.js";
4
+ import { BaseError } from "viem";
5
+ //#region src/new-sdk/errors/MissingSourceError.d.ts
6
+ /**
7
+ * Thrown at construction when a {@link GearboxSDK} was not given a source its
8
+ * {@link Mode} reads from.
9
+ **/
10
+ declare class MissingSourceError extends BaseError {
11
+ name: string;
12
+ /**
13
+ * Mode the SDK was built in.
14
+ **/
15
+ readonly mode: Mode;
16
+ /**
17
+ * Source that mode needs and did not get.
18
+ **/
19
+ readonly source: DataSource;
20
+ constructor(mode: Mode, source: DataSource);
21
+ }
22
+ //#endregion
23
+ export { MissingSourceError };
@@ -0,0 +1,17 @@
1
+ import { ChainId } from "../../model/primitives.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/errors/NoSourceServedError.d.ts
4
+ /**
5
+ * The failure of a chain no source served. It is reported in that chain's
6
+ * metadata entry rather than thrown: the other chains of the same read may
7
+ * still have been served.
8
+ **/
9
+ declare class NoSourceServedError extends AggregateError {
10
+ /**
11
+ * Chain no source served.
12
+ **/
13
+ readonly chainId: ChainId;
14
+ constructor(chainId: ChainId, reasons: unknown[]);
15
+ }
16
+ //#endregion
17
+ export { NoSourceServedError };
@@ -0,0 +1,35 @@
1
+ import { ChainId } from "../../model/primitives.js";
2
+ import { NetworkType } from "../../sdk/chain/chains.js";
3
+ import { DataSource } from "../../model/response.js";
4
+ import "../../model/index.js";
5
+ import "../../sdk/index.js";
6
+ import { BaseError } from "viem";
7
+ //#region src/new-sdk/errors/SourceChainMismatchError.d.ts
8
+ /**
9
+ * How a source names the chains it covers: by label or by id, depending on which
10
+ * of the two it was configured with.
11
+ **/
12
+ type ChainRef = NetworkType | ChainId;
13
+ /**
14
+ * Thrown at construction when a source covers anything other than exactly the
15
+ * chains a {@link GearboxSDK} was built for, see
16
+ * {@link GearboxSDKOptions.networks}.
17
+ **/
18
+ declare class SourceChainMismatchError extends BaseError {
19
+ name: string;
20
+ /**
21
+ * Source whose coverage does not match.
22
+ **/
23
+ readonly source: DataSource;
24
+ /**
25
+ * Chains the SDK covers.
26
+ **/
27
+ readonly expected: readonly ChainRef[];
28
+ /**
29
+ * Chains the source covers.
30
+ **/
31
+ readonly covered: readonly ChainRef[];
32
+ constructor(source: DataSource, expected: readonly ChainRef[], covered: readonly ChainRef[]);
33
+ }
34
+ //#endregion
35
+ export { ChainRef, SourceChainMismatchError };
@@ -0,0 +1,22 @@
1
+ import { DataSource } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ import { BaseError } from "viem";
4
+ //#region src/new-sdk/errors/SourceUnavailableError.d.ts
5
+ /**
6
+ * Thrown when a source branch of a namespace is read in a mode that has no such
7
+ * source, e.g. `sdk.opportunities.onchain` in `offchain` mode.
8
+ **/
9
+ declare class SourceUnavailableError extends BaseError {
10
+ name: string;
11
+ /**
12
+ * Namespace whose branch was read.
13
+ **/
14
+ readonly namespace: string;
15
+ /**
16
+ * Source this mode does not read.
17
+ **/
18
+ readonly source: DataSource;
19
+ constructor(namespace: string, source: DataSource);
20
+ }
21
+ //#endregion
22
+ export { SourceUnavailableError };
@@ -0,0 +1,11 @@
1
+ import { DataSource } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ import { ChainRef } from "./SourceChainMismatchError.js";
4
+ //#region src/new-sdk/errors/assertSameChains.d.ts
5
+ /**
6
+ * Rejects a source covering anything other than exactly the chains the SDK was
7
+ * built for. A source is not narrowed to them: it has to name them.
8
+ **/
9
+ declare function assertSameChains<T extends ChainRef>(source: DataSource, expected: readonly T[], covered: readonly T[]): void;
10
+ //#endregion
11
+ export { assertSameChains };
@@ -0,0 +1,10 @@
1
+ import { ResponseMetadata } from "../../model/response.js";
2
+ import "../../model/index.js";
3
+ //#region src/new-sdk/errors/everyChainFailed.d.ts
4
+ /**
5
+ * Whether a merged envelope covered chains and lost every one of them, which is
6
+ * what makes a read a failure rather than a partial result.
7
+ **/
8
+ declare function everyChainFailed(meta: ResponseMetadata): boolean;
9
+ //#endregion
10
+ export { everyChainFailed };
@@ -0,0 +1,8 @@
1
+ import { AllSourcesFailedError } from "./AllSourcesFailedError.js";
2
+ import { MissingSourceError } from "./MissingSourceError.js";
3
+ import { NoSourceServedError } from "./NoSourceServedError.js";
4
+ import { ChainRef, SourceChainMismatchError } from "./SourceChainMismatchError.js";
5
+ import { SourceUnavailableError } from "./SourceUnavailableError.js";
6
+ import { assertSameChains } from "./assertSameChains.js";
7
+ import { everyChainFailed } from "./everyChainFailed.js";
8
+ export { AllSourcesFailedError, ChainRef, MissingSourceError, NoSourceServedError, SourceChainMismatchError, SourceUnavailableError, assertSameChains, everyChainFailed };
@@ -1,12 +1,23 @@
1
- import { AllSourcesFailedError, GearboxSDKOptions, Mode, OffchainByMode, OffchainSource, OffchainSourceStatus, OnchainByMode, OnchainSource, PlainMultichainSDKOptions, ReadResult, SourceMeta } from "./types.js";
2
- import { AbstractNamespace, CombineSources, OffchainContribution, OnchainContribution } from "./AbstractNamespace.js";
3
- import { Chart, ChartMetadata, HistoryReader } from "./utils/history.js";
1
+ import { GearboxSDKOptions, Mode, NamespaceOptions, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, PlainMultichainSDKOptions } from "./types.js";
2
+ import { filterResponse } from "./utils/filterResponse.js";
3
+ import { HistoryReader } from "./utils/history.js";
4
+ import { DEFAULT_MAX_OFFCHAIN_LAG, mergeChainList, mergeChainOne } from "./utils/mergeChains.js";
5
+ import { SourceMerger } from "./utils/types.js";
4
6
  import "./utils/index.js";
5
- import { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesOffchainOnly, OpportunitiesOnchainOnly } from "./opportunities/types.js";
7
+ import { AbstractNamespace, MergedQuery } from "./AbstractNamespace.js";
8
+ import { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers } from "./opportunities/types.js";
6
9
  import { OpportunitiesNamespace } from "./opportunities/OpportunitiesNamespace.js";
7
10
  import "./opportunities/index.js";
8
- import { Positions, PositionsBase, PositionsByMode, PositionsOffchainOnly, PositionsOnchainOnly } from "./positions/types.js";
11
+ import { PositionMergers, Positions, PositionsBase, PositionsByMode, PositionsMerged, PositionsOffchainBranch, PositionsOffchainOnly, PositionsOnchainBranch, PositionsOnchainOnly } from "./positions/types.js";
9
12
  import { PositionsNamespace } from "./positions/PositionsNamespace.js";
10
13
  import "./positions/index.js";
11
14
  import { GearboxSDK } from "./GearboxSDK.js";
12
- export { AbstractNamespace, AllSourcesFailedError, Chart, ChartMetadata, CombineSources, GearboxSDK, GearboxSDKOptions, HistoryReader, Mode, OffchainByMode, OffchainContribution, OffchainSource, OffchainSourceStatus, OnchainByMode, OnchainContribution, OnchainSource, Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesNamespace, OpportunitiesOffchainOnly, OpportunitiesOnchainOnly, PlainMultichainSDKOptions, Positions, PositionsBase, PositionsByMode, PositionsNamespace, PositionsOffchainOnly, PositionsOnchainOnly, ReadResult, SourceMeta };
15
+ import { AllSourcesFailedError } from "./errors/AllSourcesFailedError.js";
16
+ import { MissingSourceError } from "./errors/MissingSourceError.js";
17
+ import { NoSourceServedError } from "./errors/NoSourceServedError.js";
18
+ import { ChainRef, SourceChainMismatchError } from "./errors/SourceChainMismatchError.js";
19
+ import { SourceUnavailableError } from "./errors/SourceUnavailableError.js";
20
+ import { assertSameChains } from "./errors/assertSameChains.js";
21
+ import { everyChainFailed } from "./errors/everyChainFailed.js";
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 };
@@ -1,69 +1,48 @@
1
- import { ILogger } from "../../sdk/types/logger.js";
2
1
  import { Opportunity, OpportunityFilter, PoolOpportunityDetail, PoolOpportunityKey, PoolOpportunityRef, StrategyOpportunityDetail, StrategyOpportunityKey, StrategyOpportunityRef } from "../../model/opportunities.js";
3
2
  import { PoolHistoryMetric, StrategyHistoryMetric } from "../../model/history.js";
3
+ import { DataResponse } from "../../model/response.js";
4
4
  import "../../model/index.js";
5
5
  import { MultichainSDK } from "../../sdk/MultichainSDK.js";
6
6
  import "../../sdk/index.js";
7
7
  import { GearboxAPI } from "../../offchain/GearboxAPI.js";
8
8
  import "../../offchain/index.js";
9
- import { ReadResult } from "../types.js";
10
- import { AbstractNamespace } from "../AbstractNamespace.js";
9
+ import { NamespaceOptions } from "../types.js";
11
10
  import { HistoryReader } from "../utils/history.js";
12
11
  import "../utils/index.js";
13
- import { OpportunitiesBase, OpportunitiesOffchainOnly } from "./types.js";
12
+ import { AbstractNamespace } from "../AbstractNamespace.js";
13
+ import { OpportunitiesBase, OpportunitiesOffchainOnly, OpportunityMergers } from "./types.js";
14
14
  //#region src/new-sdk/opportunities/OpportunitiesNamespace.d.ts
15
15
  /**
16
- * The `opportunities` namespace of the combined SDK.
17
- *
18
- * A stateless router over the two sources, see {@link AbstractNamespace} for
19
- * the routing itself. What is specific to opportunities is the reads below and
20
- * the merge policy at the bottom of the class.
21
- *
22
- * The class implements the methods of every mode; {@link GearboxSDK} exposes it
23
- * as its mode's slice of {@link OpportunitiesByMode}, so calling a method the
24
- * mode does not have is a compile error rather than a runtime one.
16
+ * The `opportunities` namespace of a {@link GearboxSDK}, see
17
+ * {@link OpportunitiesByMode} for what each mode offers.
25
18
  **/
26
- declare class OpportunitiesNamespace extends AbstractNamespace<Opportunity> implements OpportunitiesBase, OpportunitiesOffchainOnly {
27
- #private;
28
- constructor(onchain: MultichainSDK | undefined, offchain: GearboxAPI | undefined, logger?: ILogger);
19
+ declare class OpportunitiesNamespace extends AbstractNamespace<MultichainSDK["opportunities"], GearboxAPI["opportunities"]> implements OpportunitiesBase, OpportunitiesOffchainOnly {
20
+ /**
21
+ * {@inheritDoc OpportunitiesBase.merge}
22
+ **/
23
+ readonly merge: OpportunityMergers;
24
+ constructor(onchain: MultichainSDK | undefined, offchain: GearboxAPI | undefined, options: NamespaceOptions);
29
25
  /**
30
26
  * {@inheritDoc OpportunitiesBase.list}
31
27
  **/
32
- list(filter?: OpportunityFilter): Promise<ReadResult<Opportunity[]>>;
28
+ list(filter?: OpportunityFilter): Promise<DataResponse<Opportunity[]>>;
33
29
  /**
34
30
  * {@inheritDoc OpportunitiesBase.getPool}
35
31
  **/
36
- getPool(key: PoolOpportunityKey): Promise<ReadResult<PoolOpportunityDetail>>;
32
+ getPool(key: PoolOpportunityKey): Promise<DataResponse<PoolOpportunityDetail>>;
37
33
  /**
38
34
  * {@inheritDoc OpportunitiesBase.getStrategy}
39
35
  **/
40
- getStrategy(key: StrategyOpportunityKey): Promise<ReadResult<StrategyOpportunityDetail>>;
36
+ getStrategy(key: StrategyOpportunityKey): Promise<DataResponse<StrategyOpportunityDetail>>;
37
+ /**
38
+ * {@inheritDoc OpportunitiesBase.filter}
39
+ **/
40
+ filter(response: DataResponse<Opportunity[]> | undefined, filter?: OpportunityFilter): DataResponse<Opportunity[]> | undefined;
41
41
  /**
42
42
  * {@inheritDoc OpportunitiesOffchainOnly.history}
43
43
  **/
44
44
  history(key: PoolOpportunityRef): HistoryReader<PoolHistoryMetric>;
45
45
  history(key: StrategyOpportunityRef): HistoryReader<StrategyHistoryMetric>;
46
- /**
47
- * Merges the two versions of one opportunity.
48
- *
49
- * The chain wins every field it fills; the backend fills the rest and owns
50
- * {@link OFFCHAIN_OWNED_FIELDS}.
51
- *
52
- * The rule is onchain-first, field-wise, and never deeper than one level: a
53
- * group like `totalSupply` or `supplyApy` is taken whole from one source, so
54
- * a row never mixes an on-chain token amount with a backend dollar value
55
- * derived from a different block.
56
- **/
57
- protected mergeOne<T extends object>(onchain: T, offchain: T): T;
58
- /**
59
- * Unions the two lists by canonical opportunity id.
60
- *
61
- * Rows present in both are merged by {@link OpportunitiesNamespace.mergeOne}.
62
- * Rows only the backend knows are appended: a chain the SDK does not cover,
63
- * or a market it has not loaded, is a reason to show more rather than fewer
64
- * opportunities.
65
- **/
66
- protected mergeList(onchain: Opportunity[], offchain: Opportunity[]): Opportunity[];
67
46
  }
68
47
  //#endregion
69
48
  export { OpportunitiesNamespace };
@@ -1,3 +1,3 @@
1
- import { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesOffchainOnly, OpportunitiesOnchainOnly } from "./types.js";
1
+ import { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers } from "./types.js";
2
2
  import { OpportunitiesNamespace } from "./OpportunitiesNamespace.js";
3
- export { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesNamespace, OpportunitiesOffchainOnly, OpportunitiesOnchainOnly };
3
+ export { Opportunities, OpportunitiesBase, OpportunitiesByMode, OpportunitiesMerged, OpportunitiesNamespace, OpportunitiesOffchainBranch, OpportunitiesOffchainOnly, OpportunitiesOnchainBranch, OpportunitiesOnchainOnly, OpportunityMergers };