@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,20 +1,23 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_sdk_chain_chains = require("../sdk/chain/chains.js");
2
3
  const require_sdk_MultichainSDK = require("../sdk/MultichainSDK.js");
3
4
  require("../sdk/index.js");
5
+ const require_new_sdk_errors_assertSameChains = require("./errors/assertSameChains.js");
6
+ const require_new_sdk_errors_MissingSourceError = require("./errors/MissingSourceError.js");
7
+ require("./errors/index.js");
4
8
  const require_offchain_GearboxAPI = require("../offchain/GearboxAPI.js");
5
9
  require("../offchain/index.js");
10
+ require("./utils/mergeChains.js");
11
+ require("./utils/index.js");
6
12
  const require_new_sdk_opportunities_OpportunitiesNamespace = require("./opportunities/OpportunitiesNamespace.js");
7
13
  require("./opportunities/index.js");
8
14
  const require_new_sdk_positions_PositionsNamespace = require("./positions/PositionsNamespace.js");
9
15
  require("./positions/index.js");
10
16
  //#region src/new-sdk/GearboxSDK.ts
11
17
  /**
12
- * Combined entry point over both sources of Gearbox data: the chain and the
13
- * Gearbox backend.
14
- *
15
- * The {@link Mode} is fixed at construction and decides which methods exist,
16
- * not what they return, so a screen written against `both` cannot silently
17
- * degrade to a subset when someone reconfigures the SDK:
18
+ * Entry point over both sources of Gearbox data: the chain and the Gearbox
19
+ * backend. The {@link Mode} is fixed at construction and decides which methods
20
+ * exist.
18
21
  *
19
22
  * ```ts
20
23
  * const sdk = new GearboxSDK({
@@ -25,13 +28,9 @@ require("./positions/index.js");
25
28
  * });
26
29
  * await sdk.attach();
27
30
  *
28
- * const { result, meta } = await sdk.opportunities.list({ kind: "strategy" });
31
+ * const { data, meta } = await sdk.opportunities.list({ kind: "strategy" });
29
32
  * ```
30
33
  *
31
- * Every read answers with `{ result, meta }`: `meta` says which chains and
32
- * which backend answered, because a partial answer is the normal case for a
33
- * multi-chain read.
34
- *
35
34
  * @typeParam M - Mode the instance was built in.
36
35
  **/
37
36
  var GearboxSDK = class {
@@ -40,8 +39,7 @@ var GearboxSDK = class {
40
39
  **/
41
40
  mode;
42
41
  /**
43
- * Chains this instance covers. Authoritative even when an injected on-chain
44
- * SDK covers a different set.
42
+ * Chains this instance covers, which every read is scoped to.
45
43
  **/
46
44
  networks;
47
45
  /**
@@ -55,46 +53,51 @@ var GearboxSDK = class {
55
53
  #attachOptions;
56
54
  #onchain;
57
55
  #offchain;
58
- /**
59
- * Whether this instance built the on-chain SDK, and therefore attaches it.
60
- **/
61
56
  #ownsOnchain = false;
62
57
  #attached;
63
58
  constructor(options) {
64
- const { mode, networks, onchain, offchain, attach, logger } = options;
59
+ const { mode, networks, onchain, offchain, attach, logger, maxOffchainLagSeconds = 120 } = options;
65
60
  this.mode = mode;
66
61
  this.networks = [...networks];
67
62
  this.#attachOptions = attach;
68
63
  const needsOnchain = mode === "onchain" || mode === "both";
69
64
  const needsOffchain = mode === "offchain" || mode === "both";
70
- if (needsOnchain && !onchain) throw new Error(`GearboxSDK in ${mode} mode needs an onchain source`);
71
- if (needsOffchain && !offchain) throw new Error(`GearboxSDK in ${mode} mode needs an offchain source`);
65
+ if (needsOnchain && !onchain) throw new require_new_sdk_errors_MissingSourceError.MissingSourceError(mode, "onchain");
66
+ if (needsOffchain && !offchain) throw new require_new_sdk_errors_MissingSourceError.MissingSourceError(mode, "offchain");
72
67
  if (!needsOnchain && onchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the onchain source it was given`);
73
68
  if (!needsOffchain && offchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the offchain source it was given`);
74
- if (needsOnchain && onchain) if (onchain instanceof require_sdk_MultichainSDK.MultichainSDK) this.#onchain = onchain;
75
- else {
76
- this.#onchain = new require_sdk_MultichainSDK.MultichainSDK({
77
- logger,
78
- ...onchain
79
- });
80
- this.#ownsOnchain = true;
69
+ const chainIds = require_sdk_chain_chains.toChainIds(networks);
70
+ if (needsOnchain && onchain) {
71
+ if (onchain instanceof require_sdk_MultichainSDK.MultichainSDK) this.#onchain = onchain;
72
+ else {
73
+ this.#onchain = new require_sdk_MultichainSDK.MultichainSDK({
74
+ logger,
75
+ ...onchain,
76
+ chains: chainsOf(onchain.chains, networks)
77
+ });
78
+ this.#ownsOnchain = true;
79
+ }
80
+ require_new_sdk_errors_assertSameChains.assertSameChains("onchain", networks, [...this.#onchain.chains.keys()]);
81
81
  }
82
- if (needsOffchain && offchain) this.#offchain = offchain instanceof require_offchain_GearboxAPI.GearboxAPI ? offchain : new require_offchain_GearboxAPI.GearboxAPI({
82
+ if (needsOffchain && offchain) if (offchain instanceof require_offchain_GearboxAPI.GearboxAPI) {
83
+ require_new_sdk_errors_assertSameChains.assertSameChains("offchain", chainIds, offchain.chainIds);
84
+ this.#offchain = offchain;
85
+ } else this.#offchain = new require_offchain_GearboxAPI.GearboxAPI({
83
86
  logger,
84
- ...offchain
87
+ ...offchain,
88
+ chainIds
85
89
  });
86
90
  this.#attached = !this.#ownsOnchain;
87
- this.opportunities = new require_new_sdk_opportunities_OpportunitiesNamespace.OpportunitiesNamespace(this.#onchain, this.#offchain, logger);
88
- this.positions = new require_new_sdk_positions_PositionsNamespace.PositionsNamespace(this.#onchain, this.#offchain, logger);
91
+ const namespaceOptions = {
92
+ maxOffchainLagSeconds,
93
+ logger
94
+ };
95
+ this.opportunities = new require_new_sdk_opportunities_OpportunitiesNamespace.OpportunitiesNamespace(this.#onchain, this.#offchain, namespaceOptions);
96
+ this.positions = new require_new_sdk_positions_PositionsNamespace.PositionsNamespace(this.#onchain, this.#offchain, namespaceOptions);
89
97
  }
90
98
  /**
91
- * Attaches the on-chain SDK when this instance owns one.
92
- *
93
- * Reads issued before this resolves still reach the chain and fail there, so
94
- * in `both` mode they are served from the backend alone and report the chain
95
- * as failed in their meta, rather than blocking.
96
- *
97
- * A no-op in `offchain` mode and when an already-attached SDK was injected.
99
+ * Attaches the on-chain SDK when this instance owns one; a no-op in `offchain`
100
+ * mode and when an already-attached SDK was injected.
98
101
  **/
99
102
  async attach() {
100
103
  if (this.#attached || !this.#ownsOnchain || !this.#onchain) return;
@@ -103,17 +106,15 @@ var GearboxSDK = class {
103
106
  }
104
107
  /**
105
108
  * Whether the on-chain source is ready to be read from. Always `true` in
106
- * `offchain` mode, where there is nothing to wait for.
109
+ * `offchain` mode.
107
110
  **/
108
111
  get attached() {
109
112
  return this.mode === "offchain" ? true : this.#attached;
110
113
  }
111
114
  /**
112
115
  * The underlying on-chain SDK, for everything this facade does not expose
113
- * yet: markets, credit accounts, transaction building.
114
- *
115
- * `undefined` in `offchain` mode. Otherwise it exists from construction on,
116
- * but only answers reads once {@link attach} has resolved.
116
+ * yet: markets, credit accounts, transaction building. `undefined` in
117
+ * `offchain` mode, and only answers reads once {@link attach} has resolved.
117
118
  **/
118
119
  get onchain() {
119
120
  return this.#onchain;
@@ -125,5 +126,17 @@ var GearboxSDK = class {
125
126
  return this.#offchain;
126
127
  }
127
128
  };
129
+ /**
130
+ * Per-chain configuration of exactly the networks the SDK covers.
131
+ **/
132
+ function chainsOf(configured, networks) {
133
+ const chains = {};
134
+ for (const network of networks) {
135
+ const chain = configured[network];
136
+ if (!chain) throw new Error(`GearboxSDK covers ${network}, but its onchain source has no configuration for it`);
137
+ chains[network] = chain;
138
+ }
139
+ return chains;
140
+ }
128
141
  //#endregion
129
142
  exports.GearboxSDK = GearboxSDK;
@@ -0,0 +1,20 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/new-sdk/errors/AllSourcesFailedError.ts
3
+ /**
4
+ * Thrown when a read had sources to ask and none of them served a single chain.
5
+ **/
6
+ var AllSourcesFailedError = class extends AggregateError {
7
+ constructor(action, sourceErrors, meta) {
8
+ const causes = [...sourceErrors, ...chainErrors(meta)].filter((reason) => reason !== void 0);
9
+ super(causes, `cannot ${action}, every source failed (${causes.join("; ")})`);
10
+ this.name = "AllSourcesFailedError";
11
+ }
12
+ };
13
+ /**
14
+ * Why each chain a merged envelope did report failed.
15
+ **/
16
+ function chainErrors(meta) {
17
+ return (meta?.chains ?? []).map((chain) => chain.status === "error" ? chain.error : void 0);
18
+ }
19
+ //#endregion
20
+ exports.AllSourcesFailedError = AllSourcesFailedError;
@@ -0,0 +1,25 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let viem = require("viem");
3
+ //#region src/new-sdk/errors/MissingSourceError.ts
4
+ /**
5
+ * Thrown at construction when a {@link GearboxSDK} was not given a source its
6
+ * {@link Mode} reads from.
7
+ **/
8
+ var MissingSourceError = class extends viem.BaseError {
9
+ name = "MissingSourceError";
10
+ /**
11
+ * Mode the SDK was built in.
12
+ **/
13
+ mode;
14
+ /**
15
+ * Source that mode needs and did not get.
16
+ **/
17
+ source;
18
+ constructor(mode, source) {
19
+ super(`A GearboxSDK in ${mode} mode needs an ${source} source.`);
20
+ this.mode = mode;
21
+ this.source = source;
22
+ }
23
+ };
24
+ //#endregion
25
+ exports.MissingSourceError = MissingSourceError;
@@ -0,0 +1,20 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/new-sdk/errors/NoSourceServedError.ts
3
+ /**
4
+ * The failure of a chain no source served. It is reported in that chain's
5
+ * metadata entry rather than thrown: the other chains of the same read may
6
+ * still have been served.
7
+ **/
8
+ var NoSourceServedError = class extends AggregateError {
9
+ /**
10
+ * Chain no source served.
11
+ **/
12
+ chainId;
13
+ constructor(chainId, reasons) {
14
+ super(reasons, `no source could serve chain ${chainId}`);
15
+ this.name = "NoSourceServedError";
16
+ this.chainId = chainId;
17
+ }
18
+ };
19
+ //#endregion
20
+ exports.NoSourceServedError = NoSourceServedError;
@@ -0,0 +1,34 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let viem = require("viem");
3
+ //#region src/new-sdk/errors/SourceChainMismatchError.ts
4
+ /**
5
+ * Thrown at construction when a source covers anything other than exactly the
6
+ * chains a {@link GearboxSDK} was built for, see
7
+ * {@link GearboxSDKOptions.networks}.
8
+ **/
9
+ var SourceChainMismatchError = class extends viem.BaseError {
10
+ name = "SourceChainMismatchError";
11
+ /**
12
+ * Source whose coverage does not match.
13
+ **/
14
+ source;
15
+ /**
16
+ * Chains the SDK covers.
17
+ **/
18
+ expected;
19
+ /**
20
+ * Chains the source covers.
21
+ **/
22
+ covered;
23
+ constructor(source, expected, covered) {
24
+ super(`The ${source} source does not cover the chains this GearboxSDK was built for.`, { metaMessages: [`Expected: ${named(expected)}`, `Covered: ${named(covered)}`] });
25
+ this.source = source;
26
+ this.expected = [...expected];
27
+ this.covered = [...covered];
28
+ }
29
+ };
30
+ function named(chains) {
31
+ return chains.join(", ") || "no chain";
32
+ }
33
+ //#endregion
34
+ exports.SourceChainMismatchError = SourceChainMismatchError;
@@ -0,0 +1,25 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let viem = require("viem");
3
+ //#region src/new-sdk/errors/SourceUnavailableError.ts
4
+ /**
5
+ * Thrown when a source branch of a namespace is read in a mode that has no such
6
+ * source, e.g. `sdk.opportunities.onchain` in `offchain` mode.
7
+ **/
8
+ var SourceUnavailableError = class extends viem.BaseError {
9
+ name = "SourceUnavailableError";
10
+ /**
11
+ * Namespace whose branch was read.
12
+ **/
13
+ namespace;
14
+ /**
15
+ * Source this mode does not read.
16
+ **/
17
+ source;
18
+ constructor(namespace, source) {
19
+ super(`The ${namespace} namespace has no ${source} source in this mode.`);
20
+ this.namespace = namespace;
21
+ this.source = source;
22
+ }
23
+ };
24
+ //#endregion
25
+ exports.SourceUnavailableError = SourceUnavailableError;
@@ -0,0 +1,12 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_new_sdk_errors_SourceChainMismatchError = require("./SourceChainMismatchError.js");
3
+ //#region src/new-sdk/errors/assertSameChains.ts
4
+ /**
5
+ * Rejects a source covering anything other than exactly the chains the SDK was
6
+ * built for. A source is not narrowed to them: it has to name them.
7
+ **/
8
+ function assertSameChains(source, expected, covered) {
9
+ if (covered.length !== expected.length || covered.some((chain) => !expected.includes(chain))) throw new require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError(source, expected, covered);
10
+ }
11
+ //#endregion
12
+ exports.assertSameChains = assertSameChains;
@@ -0,0 +1,11 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/new-sdk/errors/everyChainFailed.ts
3
+ /**
4
+ * Whether a merged envelope covered chains and lost every one of them, which is
5
+ * what makes a read a failure rather than a partial result.
6
+ **/
7
+ function everyChainFailed(meta) {
8
+ return meta.chains.length > 0 && meta.chains.every((chain) => chain.status === "error");
9
+ }
10
+ //#endregion
11
+ exports.everyChainFailed = everyChainFailed;
@@ -0,0 +1,15 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_new_sdk_errors_AllSourcesFailedError = require("./AllSourcesFailedError.js");
3
+ const require_new_sdk_errors_SourceChainMismatchError = require("./SourceChainMismatchError.js");
4
+ const require_new_sdk_errors_assertSameChains = require("./assertSameChains.js");
5
+ const require_new_sdk_errors_everyChainFailed = require("./everyChainFailed.js");
6
+ const require_new_sdk_errors_MissingSourceError = require("./MissingSourceError.js");
7
+ const require_new_sdk_errors_NoSourceServedError = require("./NoSourceServedError.js");
8
+ const require_new_sdk_errors_SourceUnavailableError = require("./SourceUnavailableError.js");
9
+ exports.AllSourcesFailedError = require_new_sdk_errors_AllSourcesFailedError.AllSourcesFailedError;
10
+ exports.MissingSourceError = require_new_sdk_errors_MissingSourceError.MissingSourceError;
11
+ exports.NoSourceServedError = require_new_sdk_errors_NoSourceServedError.NoSourceServedError;
12
+ exports.SourceChainMismatchError = require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError;
13
+ exports.SourceUnavailableError = require_new_sdk_errors_SourceUnavailableError.SourceUnavailableError;
14
+ exports.assertSameChains = require_new_sdk_errors_assertSameChains.assertSameChains;
15
+ exports.everyChainFailed = require_new_sdk_errors_everyChainFailed.everyChainFailed;
@@ -1,14 +1,34 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_new_sdk_types = require("./types.js");
2
+ const require_new_sdk_errors_AllSourcesFailedError = require("./errors/AllSourcesFailedError.js");
3
+ const require_new_sdk_errors_SourceChainMismatchError = require("./errors/SourceChainMismatchError.js");
4
+ const require_new_sdk_errors_assertSameChains = require("./errors/assertSameChains.js");
5
+ const require_new_sdk_errors_everyChainFailed = require("./errors/everyChainFailed.js");
6
+ const require_new_sdk_errors_MissingSourceError = require("./errors/MissingSourceError.js");
7
+ const require_new_sdk_errors_NoSourceServedError = require("./errors/NoSourceServedError.js");
8
+ const require_new_sdk_errors_SourceUnavailableError = require("./errors/SourceUnavailableError.js");
9
+ require("./errors/index.js");
3
10
  const require_new_sdk_AbstractNamespace = require("./AbstractNamespace.js");
11
+ const require_new_sdk_utils_filterResponse = require("./utils/filterResponse.js");
12
+ const require_new_sdk_utils_mergeChains = require("./utils/mergeChains.js");
13
+ require("./utils/index.js");
4
14
  const require_new_sdk_opportunities_OpportunitiesNamespace = require("./opportunities/OpportunitiesNamespace.js");
5
15
  require("./opportunities/index.js");
6
16
  const require_new_sdk_positions_PositionsNamespace = require("./positions/PositionsNamespace.js");
7
17
  require("./positions/index.js");
8
18
  const require_new_sdk_GearboxSDK = require("./GearboxSDK.js");
9
- require("./utils/index.js");
19
+ require("./types.js");
10
20
  exports.AbstractNamespace = require_new_sdk_AbstractNamespace.AbstractNamespace;
11
- exports.AllSourcesFailedError = require_new_sdk_types.AllSourcesFailedError;
21
+ exports.AllSourcesFailedError = require_new_sdk_errors_AllSourcesFailedError.AllSourcesFailedError;
22
+ exports.DEFAULT_MAX_OFFCHAIN_LAG = require_new_sdk_utils_mergeChains.DEFAULT_MAX_OFFCHAIN_LAG;
12
23
  exports.GearboxSDK = require_new_sdk_GearboxSDK.GearboxSDK;
24
+ exports.MissingSourceError = require_new_sdk_errors_MissingSourceError.MissingSourceError;
25
+ exports.NoSourceServedError = require_new_sdk_errors_NoSourceServedError.NoSourceServedError;
13
26
  exports.OpportunitiesNamespace = require_new_sdk_opportunities_OpportunitiesNamespace.OpportunitiesNamespace;
14
27
  exports.PositionsNamespace = require_new_sdk_positions_PositionsNamespace.PositionsNamespace;
28
+ exports.SourceChainMismatchError = require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError;
29
+ exports.SourceUnavailableError = require_new_sdk_errors_SourceUnavailableError.SourceUnavailableError;
30
+ exports.assertSameChains = require_new_sdk_errors_assertSameChains.assertSameChains;
31
+ exports.everyChainFailed = require_new_sdk_errors_everyChainFailed.everyChainFailed;
32
+ exports.filterResponse = require_new_sdk_utils_filterResponse.filterResponse;
33
+ exports.mergeChainList = require_new_sdk_utils_mergeChains.mergeChainList;
34
+ exports.mergeChainOne = require_new_sdk_utils_mergeChains.mergeChainOne;
@@ -2,120 +2,68 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_model_opportunities = require("../../model/opportunities.js");
3
3
  require("../../model/index.js");
4
4
  const require_new_sdk_AbstractNamespace = require("../AbstractNamespace.js");
5
+ const require_new_sdk_utils_filterResponse = require("../utils/filterResponse.js");
6
+ const require_new_sdk_utils_mergeChains = require("../utils/mergeChains.js");
7
+ require("../utils/index.js");
5
8
  //#region src/new-sdk/opportunities/OpportunitiesNamespace.ts
6
9
  /**
7
- * Fields the backend owns even when the chain also fills them.
8
- *
9
- * Empty of on-chain conflicts today — every yield group here is off-chain only,
10
- * so onchain-first would already yield the backend's value. It is written out
11
- * anyway so that adding an on-chain estimate of one of them is a deliberate
12
- * change to this list rather than a silent regression.
13
- **/
14
- const OFFCHAIN_OWNED_FIELDS = /* @__PURE__ */ new Set(["supplyApy", "collateralApy"]);
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.
10
+ * The `opportunities` namespace of a {@link GearboxSDK}, see
11
+ * {@link OpportunitiesByMode} for what each mode offers.
25
12
  **/
26
13
  var OpportunitiesNamespace = class extends require_new_sdk_AbstractNamespace.AbstractNamespace {
27
- constructor(onchain, offchain, logger) {
28
- super("Opportunities", onchain, offchain, logger);
14
+ /**
15
+ * {@inheritDoc OpportunitiesBase.merge}
16
+ **/
17
+ merge = {
18
+ list: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainList(onchain, offchain, this.maxOffchainLagSeconds),
19
+ pool: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainOne(onchain, offchain, this.maxOffchainLagSeconds),
20
+ strategy: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainOne(onchain, offchain, this.maxOffchainLagSeconds)
21
+ };
22
+ constructor(onchain, offchain, options) {
23
+ super("Opportunities", onchain?.opportunities, offchain?.opportunities, options);
29
24
  }
30
25
  /**
31
26
  * {@inheritDoc OpportunitiesBase.list}
32
27
  **/
33
28
  async list(filter) {
34
- return this.readList("list opportunities", async (sdk) => {
35
- const { result, meta } = await sdk.opportunities.list(filter);
36
- return {
37
- value: result,
38
- chains: meta
39
- };
40
- }, (api) => api.opportunities.list(filter));
29
+ return this.merged("list opportunities", {
30
+ fromChain: (source) => source.list(filter),
31
+ fromBackend: (source) => source.list(filter),
32
+ merge: this.merge.list
33
+ });
41
34
  }
42
35
  /**
43
36
  * {@inheritDoc OpportunitiesBase.getPool}
44
37
  **/
45
38
  async getPool(key) {
46
- return this.readOne("get pool opportunity", key.chainId, (sdk) => sdk.opportunities.getPool(key), (api) => api.opportunities.getPool(key));
39
+ return this.merged("get pool opportunity", {
40
+ fromChain: (source) => source.getPool(key),
41
+ fromBackend: (source) => source.getPool(key),
42
+ merge: this.merge.pool
43
+ });
47
44
  }
48
45
  /**
49
46
  * {@inheritDoc OpportunitiesBase.getStrategy}
50
47
  **/
51
48
  async getStrategy(key) {
52
- return this.readOne("get strategy opportunity", key.chainId, (sdk) => sdk.opportunities.getStrategy(key), (api) => api.opportunities.getStrategy(key));
53
- }
54
- history(key) {
55
- return { chart: (metric, range) => this.#chart(key, metric, range) };
49
+ return this.merged("get strategy opportunity", {
50
+ fromChain: (source) => source.getStrategy(key),
51
+ fromBackend: (source) => source.getStrategy(key),
52
+ merge: this.merge.strategy
53
+ });
56
54
  }
57
55
  /**
58
- * Reads one chart of one opportunity from the backend.
59
- *
60
- * The metric a caller may name is gated by the reader's type, so the kind of
61
- * the key is not re-checked here.
56
+ * {@inheritDoc OpportunitiesBase.filter}
62
57
  **/
63
- async #chart(key, metric, range) {
64
- const { result, meta } = await this.readOffchain(`get ${metric} history`, (api) => api.opportunities.getHistory({
58
+ filter(response, filter) {
59
+ return require_new_sdk_utils_filterResponse.filterResponse(response, filter, require_model_opportunities.matchesOpportunityFilter);
60
+ }
61
+ history(key) {
62
+ return { chart: (metric, range) => this.offchain.getHistory({
65
63
  opportunity: key,
66
64
  range,
67
65
  metric
68
- }), {
69
- metric,
70
- points: [],
71
- metadata: {}
72
- });
73
- return {
74
- data: result.points,
75
- metadata: {
76
- ...result.metadata,
77
- source: meta
78
- }
79
- };
80
- }
81
- /**
82
- * Merges the two versions of one opportunity.
83
- *
84
- * The chain wins every field it fills; the backend fills the rest and owns
85
- * {@link OFFCHAIN_OWNED_FIELDS}.
86
- *
87
- * The rule is onchain-first, field-wise, and never deeper than one level: a
88
- * group like `totalSupply` or `supplyApy` is taken whole from one source, so
89
- * a row never mixes an on-chain token amount with a backend dollar value
90
- * derived from a different block.
91
- **/
92
- mergeOne(onchain, offchain) {
93
- const merged = { ...onchain };
94
- for (const [field, value] of Object.entries(offchain)) {
95
- if (value === void 0) continue;
96
- if (OFFCHAIN_OWNED_FIELDS.has(field) || merged[field] === void 0) merged[field] = value;
97
- }
98
- return merged;
99
- }
100
- /**
101
- * Unions the two lists by canonical opportunity id.
102
- *
103
- * Rows present in both are merged by {@link OpportunitiesNamespace.mergeOne}.
104
- * Rows only the backend knows are appended: a chain the SDK does not cover,
105
- * or a market it has not loaded, is a reason to show more rather than fewer
106
- * opportunities.
107
- **/
108
- mergeList(onchain, offchain) {
109
- const byId = /* @__PURE__ */ new Map();
110
- for (const row of onchain) byId.set(require_model_opportunities.opportunityId(row), row);
111
- const extra = [];
112
- for (const row of offchain) {
113
- const id = require_model_opportunities.opportunityId(row);
114
- const existing = byId.get(id);
115
- if (existing) byId.set(id, this.mergeOne(existing, row));
116
- else extra.push(row);
117
- }
118
- return [...byId.values(), ...extra];
66
+ }) };
119
67
  }
120
68
  };
121
69
  //#endregion