@gearbox-protocol/sdk 14.12.0-next.65 → 14.12.0-next.67

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 (249) hide show
  1. package/dist/cjs/common-utils/index.js +2 -2
  2. package/dist/cjs/common-utils/utils/apy/calculate-safe-borrow-rate.js +3 -3
  3. package/dist/cjs/common-utils/utils/assets-math.js +7 -7
  4. package/dist/cjs/common-utils/utils/creditAccount/calc-health-factor.js +2 -2
  5. package/dist/cjs/common-utils/utils/creditAccount/debt.js +2 -2
  6. package/dist/cjs/common-utils/utils/creditAccount/quota-utils.js +5 -5
  7. package/dist/cjs/common-utils/utils/index.js +2 -2
  8. package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +2 -2
  9. package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +5 -5
  10. package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-points.js +2 -2
  11. package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +2 -2
  12. package/dist/cjs/common-utils/utils/validation/validate-open-account-pool-status.js +4 -4
  13. package/dist/cjs/dev/index.js +2 -1
  14. package/dist/cjs/dev/kycUtils.js +1 -1
  15. package/dist/cjs/dev/midasUtils.js +104 -0
  16. package/dist/cjs/model/history.js +28 -0
  17. package/dist/cjs/model/history.schema.js +84 -0
  18. package/dist/cjs/model/index.js +64 -0
  19. package/dist/cjs/model/liquidations.js +23 -0
  20. package/dist/cjs/model/liquidations.schema.js +91 -0
  21. package/dist/cjs/model/opportunities.js +55 -0
  22. package/dist/cjs/model/opportunities.schema.js +220 -0
  23. package/dist/cjs/model/package.json +1 -0
  24. package/dist/cjs/model/positions.js +1 -0
  25. package/dist/cjs/model/primitives.js +1 -0
  26. package/dist/cjs/model/primitives.schema.js +85 -0
  27. package/dist/cjs/new-sdk/AbstractNamespace.js +158 -0
  28. package/dist/cjs/new-sdk/GearboxSDK.js +122 -0
  29. package/dist/cjs/new-sdk/index.js +11 -0
  30. package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +122 -0
  31. package/dist/cjs/new-sdk/opportunities/index.js +4 -0
  32. package/dist/cjs/new-sdk/opportunities/types.js +1 -0
  33. package/dist/cjs/new-sdk/package.json +1 -0
  34. package/dist/cjs/new-sdk/types.js +22 -0
  35. package/dist/cjs/new-sdk/utils/history.js +1 -0
  36. package/dist/cjs/new-sdk/utils/index.js +2 -0
  37. package/dist/cjs/offchain/GearboxAPI.js +33 -0
  38. package/dist/cjs/offchain/index.js +8 -0
  39. package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +97 -0
  40. package/dist/cjs/offchain/opportunities/index.js +4 -0
  41. package/dist/cjs/offchain/package.json +1 -0
  42. package/dist/cjs/offchain/types.js +1 -0
  43. package/dist/cjs/preview/preview/buildDelayedPreview.js +5 -5
  44. package/dist/cjs/rewards/rewards/api.js +2 -2
  45. package/dist/cjs/rewards/rewards/extra-apy.js +2 -2
  46. package/dist/cjs/sdk/MultichainSDK.js +18 -2
  47. package/dist/cjs/sdk/OnchainSDK.js +8 -1
  48. package/dist/cjs/sdk/accounts/index.js +2 -0
  49. package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +150 -106
  50. package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
  51. package/dist/cjs/sdk/accounts/liquidations/constants.js +13 -0
  52. package/dist/cjs/sdk/accounts/liquidations/index.js +2 -0
  53. package/dist/cjs/sdk/base/MultichainConstruct.js +80 -0
  54. package/dist/cjs/sdk/base/TokensMeta.js +51 -0
  55. package/dist/cjs/sdk/base/index.js +2 -0
  56. package/dist/cjs/sdk/chain/chains.js +76 -0
  57. package/dist/cjs/sdk/chain/index.js +4 -0
  58. package/dist/cjs/sdk/core/errors.js +13 -0
  59. package/dist/cjs/sdk/core/index.js +2 -1
  60. package/dist/cjs/sdk/index.js +25 -1
  61. package/dist/cjs/sdk/market/MarketConfiguratorContract.js +14 -0
  62. package/dist/cjs/sdk/market/MarketSuite.js +163 -0
  63. package/dist/cjs/sdk/market/credit/CreditManagerV310Contract.js +26 -0
  64. package/dist/cjs/sdk/market/credit/CreditSuite.js +102 -0
  65. package/dist/cjs/sdk/market/math.js +104 -0
  66. package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
  67. package/dist/cjs/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
  68. package/dist/cjs/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
  69. package/dist/cjs/sdk/market/pool/PoolSuite.js +28 -0
  70. package/dist/cjs/sdk/market/pool/PoolV310Contract.js +27 -0
  71. package/dist/cjs/sdk/market/pool/math.js +44 -0
  72. package/dist/cjs/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
  73. package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +60 -0
  74. package/dist/cjs/sdk/opportunities/OpportunitiesService.js +103 -0
  75. package/dist/cjs/sdk/opportunities/index.js +12 -0
  76. package/dist/cjs/sdk/types/multichain.js +1 -0
  77. package/dist/cjs/{common-utils → sdk}/utils/bigint-math.js +11 -11
  78. package/dist/cjs/sdk/utils/index.js +3 -0
  79. package/dist/cjs/sdk/utils/zod.js +12 -0
  80. package/dist/esm/common-utils/index.js +1 -1
  81. package/dist/esm/common-utils/utils/apy/calculate-safe-borrow-rate.js +1 -1
  82. package/dist/esm/common-utils/utils/assets-math.js +1 -1
  83. package/dist/esm/common-utils/utils/creditAccount/calc-health-factor.js +1 -1
  84. package/dist/esm/common-utils/utils/creditAccount/debt.js +1 -1
  85. package/dist/esm/common-utils/utils/creditAccount/quota-utils.js +1 -1
  86. package/dist/esm/common-utils/utils/index.js +1 -1
  87. package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +1 -1
  88. package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +1 -1
  89. package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-points.js +1 -1
  90. package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +1 -1
  91. package/dist/esm/common-utils/utils/validation/validate-open-account-pool-status.js +1 -1
  92. package/dist/esm/dev/AccountOpener.js +1 -1
  93. package/dist/esm/dev/index.js +3 -3
  94. package/dist/esm/dev/kycUtils.js +1 -1
  95. package/dist/esm/dev/midasUtils.js +105 -2
  96. package/dist/esm/dev/withdrawalUtils.js +1 -1
  97. package/dist/esm/model/history.js +26 -0
  98. package/dist/esm/model/history.schema.js +76 -0
  99. package/dist/esm/model/index.js +9 -0
  100. package/dist/esm/model/liquidations.js +22 -0
  101. package/dist/esm/model/liquidations.schema.js +83 -0
  102. package/dist/esm/model/opportunities.js +51 -0
  103. package/dist/esm/model/opportunities.schema.js +198 -0
  104. package/dist/esm/model/package.json +1 -0
  105. package/dist/esm/model/positions.js +1 -0
  106. package/dist/esm/model/primitives.js +1 -0
  107. package/dist/esm/model/primitives.schema.js +75 -0
  108. package/dist/esm/new-sdk/AbstractNamespace.js +157 -0
  109. package/dist/esm/new-sdk/GearboxSDK.js +121 -0
  110. package/dist/esm/new-sdk/index.js +7 -0
  111. package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +121 -0
  112. package/dist/esm/new-sdk/opportunities/index.js +3 -0
  113. package/dist/esm/new-sdk/opportunities/types.js +1 -0
  114. package/dist/esm/new-sdk/package.json +1 -0
  115. package/dist/esm/new-sdk/types.js +21 -0
  116. package/dist/esm/new-sdk/utils/history.js +1 -0
  117. package/dist/esm/new-sdk/utils/index.js +2 -0
  118. package/dist/esm/offchain/GearboxAPI.js +32 -0
  119. package/dist/esm/offchain/index.js +5 -0
  120. package/dist/esm/offchain/opportunities/OffchainOpportunities.js +95 -0
  121. package/dist/esm/offchain/opportunities/index.js +2 -0
  122. package/dist/esm/offchain/package.json +1 -0
  123. package/dist/esm/offchain/types.js +1 -0
  124. package/dist/esm/preview/preview/buildDelayedPreview.js +1 -1
  125. package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
  126. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  127. package/dist/esm/rewards/rewards/api.js +1 -1
  128. package/dist/esm/rewards/rewards/extra-apy.js +1 -1
  129. package/dist/esm/sdk/MultichainSDK.js +18 -2
  130. package/dist/esm/sdk/OnchainSDK.js +8 -1
  131. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -2
  132. package/dist/esm/sdk/accounts/index.js +2 -2
  133. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +153 -109
  134. package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
  135. package/dist/esm/sdk/accounts/liquidations/constants.js +12 -1
  136. package/dist/esm/sdk/accounts/liquidations/index.js +2 -2
  137. package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
  138. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  139. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  140. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  141. package/dist/esm/sdk/base/MultichainConstruct.js +79 -0
  142. package/dist/esm/sdk/base/TokensMeta.js +53 -2
  143. package/dist/esm/sdk/base/index.js +2 -1
  144. package/dist/esm/sdk/chain/chains.js +74 -2
  145. package/dist/esm/sdk/chain/detectNetwork.js +1 -1
  146. package/dist/esm/sdk/chain/index.js +2 -2
  147. package/dist/esm/sdk/core/createAddressProvider.js +1 -1
  148. package/dist/esm/sdk/core/errors.js +13 -1
  149. package/dist/esm/sdk/core/index.js +2 -2
  150. package/dist/esm/sdk/index.js +11 -5
  151. package/dist/esm/sdk/market/MarketConfiguratorContract.js +14 -0
  152. package/dist/esm/sdk/market/MarketSuite.js +163 -0
  153. package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
  154. package/dist/esm/sdk/market/credit/CreditManagerV310Contract.js +26 -0
  155. package/dist/esm/sdk/market/credit/CreditSuite.js +102 -0
  156. package/dist/esm/sdk/market/math.js +98 -0
  157. package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
  158. package/dist/esm/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
  159. package/dist/esm/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
  160. package/dist/esm/sdk/market/pool/PoolSuite.js +28 -0
  161. package/dist/esm/sdk/market/pool/PoolV310Contract.js +28 -1
  162. package/dist/esm/sdk/market/pool/math.js +41 -0
  163. package/dist/esm/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
  164. package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
  165. package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
  166. package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +59 -0
  167. package/dist/esm/sdk/opportunities/OpportunitiesService.js +102 -0
  168. package/dist/esm/sdk/opportunities/index.js +4 -0
  169. package/dist/esm/sdk/pools/PoolService.js +1 -1
  170. package/dist/esm/sdk/types/multichain.js +1 -0
  171. package/dist/esm/{common-utils → sdk}/utils/bigint-math.js +11 -11
  172. package/dist/esm/sdk/utils/index.js +3 -2
  173. package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
  174. package/dist/esm/sdk/utils/zod.js +13 -2
  175. package/dist/types/common-utils/index.d.ts +1 -1
  176. package/dist/types/common-utils/utils/index.d.ts +1 -1
  177. package/dist/types/dev/index.d.ts +2 -2
  178. package/dist/types/dev/midasUtils.d.ts +31 -2
  179. package/dist/types/model/history.d.ts +105 -0
  180. package/dist/types/model/history.schema.d.ts +66 -0
  181. package/dist/types/model/index.d.ts +9 -0
  182. package/dist/types/model/liquidations.d.ts +230 -0
  183. package/dist/types/model/liquidations.schema.d.ts +292 -0
  184. package/dist/types/model/opportunities.d.ts +543 -0
  185. package/dist/types/model/opportunities.schema.d.ts +1066 -0
  186. package/dist/types/model/positions.d.ts +11 -0
  187. package/dist/types/model/primitives.d.ts +193 -0
  188. package/dist/types/model/primitives.schema.d.ts +81 -0
  189. package/dist/types/new-sdk/AbstractNamespace.d.ts +119 -0
  190. package/dist/types/new-sdk/GearboxSDK.d.ts +78 -0
  191. package/dist/types/new-sdk/index.d.ts +9 -0
  192. package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +69 -0
  193. package/dist/types/new-sdk/opportunities/index.d.ts +3 -0
  194. package/dist/types/new-sdk/opportunities/types.d.ts +66 -0
  195. package/dist/types/new-sdk/types.d.ts +149 -0
  196. package/dist/types/new-sdk/utils/history.d.ts +44 -0
  197. package/dist/types/new-sdk/utils/index.d.ts +2 -0
  198. package/dist/types/offchain/GearboxAPI.d.ts +31 -0
  199. package/dist/types/offchain/index.d.ts +5 -0
  200. package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +69 -0
  201. package/dist/types/offchain/opportunities/index.d.ts +2 -0
  202. package/dist/types/offchain/types.d.ts +50 -0
  203. package/dist/types/sdk/MultichainSDK.d.ts +15 -2
  204. package/dist/types/sdk/OnchainSDK.d.ts +9 -3
  205. package/dist/types/sdk/accounts/index.d.ts +3 -3
  206. package/dist/types/sdk/accounts/liquidations/LiquidationsService.d.ts +24 -16
  207. package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +26 -20
  208. package/dist/types/sdk/accounts/liquidations/constants.d.ts +12 -1
  209. package/dist/types/sdk/accounts/liquidations/index.d.ts +3 -3
  210. package/dist/types/sdk/accounts/liquidations/types.d.ts +34 -309
  211. package/dist/types/sdk/base/MultichainConstruct.d.ts +57 -0
  212. package/dist/types/sdk/base/TokensMeta.d.ts +26 -0
  213. package/dist/types/sdk/base/index.d.ts +2 -1
  214. package/dist/types/sdk/chain/chains.d.ts +68 -1
  215. package/dist/types/sdk/chain/index.d.ts +2 -2
  216. package/dist/types/sdk/core/errors.d.ts +10 -1
  217. package/dist/types/sdk/core/index.d.ts +2 -2
  218. package/dist/types/sdk/index.d.ts +15 -8
  219. package/dist/types/sdk/market/MarketConfiguratorContract.d.ts +9 -0
  220. package/dist/types/sdk/market/MarketSuite.d.ts +109 -1
  221. package/dist/types/sdk/market/credit/CreditManagerV310Contract.d.ts +14 -0
  222. package/dist/types/sdk/market/credit/CreditSuite.d.ts +47 -0
  223. package/dist/types/sdk/market/credit/types.d.ts +22 -0
  224. package/dist/types/sdk/market/index.d.ts +2 -2
  225. package/dist/types/sdk/market/math.d.ts +74 -0
  226. package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +23 -0
  227. package/dist/types/sdk/market/oracle/types.d.ts +46 -0
  228. package/dist/types/sdk/market/pool/LinearInterestRateModelContract.d.ts +12 -0
  229. package/dist/types/sdk/market/pool/PoolQuotaKeeperV310Contract.d.ts +22 -1
  230. package/dist/types/sdk/market/pool/PoolSuite.d.ts +23 -0
  231. package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +18 -0
  232. package/dist/types/sdk/market/pool/math.d.ts +57 -0
  233. package/dist/types/sdk/market/pool/types.d.ts +23 -0
  234. package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +6 -0
  235. package/dist/types/sdk/market/pricefeeds/types.d.ts +10 -0
  236. package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +40 -0
  237. package/dist/types/sdk/opportunities/OpportunitiesService.d.ts +47 -0
  238. package/dist/types/sdk/opportunities/index.d.ts +4 -0
  239. package/dist/types/sdk/types/index.d.ts +2 -1
  240. package/dist/types/sdk/types/multichain.d.ts +65 -0
  241. package/dist/types/sdk/types/state.d.ts +1 -1
  242. package/dist/types/sdk/types/transactions.d.ts +3 -0
  243. package/dist/types/{common-utils → sdk}/utils/bigint-math.d.ts +7 -11
  244. package/dist/types/sdk/utils/index.d.ts +3 -2
  245. package/dist/types/sdk/utils/zod.d.ts +6 -2
  246. package/package.json +16 -1
  247. package/dist/cjs/sdk/accounts/liquidations/helpers.js +0 -201
  248. package/dist/esm/sdk/accounts/liquidations/helpers.js +0 -192
  249. package/dist/types/sdk/accounts/liquidations/helpers.d.ts +0 -140
@@ -0,0 +1,157 @@
1
+ import { getNetworkType } from "../sdk/chain/chains.js";
2
+ import { AllSourcesFailedError } from "./types.js";
3
+ //#region src/new-sdk/AbstractNamespace.ts
4
+ /**
5
+ * Shared machinery of every {@link GearboxSDK} namespace.
6
+ *
7
+ * A namespace is a stateless router: it asks the sources its mode allows, in
8
+ * parallel, merges what came back and reports what did not. This class owns
9
+ * that routing — per-chain failure capture, backend degradation, the response
10
+ * envelope and the all-sources-failed rule — so a namespace holds nothing but
11
+ * its reads and its merge policy.
12
+ *
13
+ * The sources are whatever it was handed, and their readiness is their owner's
14
+ * business: a source that is not ready to answer fails the read it was given,
15
+ * and that failure is reported like any other.
16
+ *
17
+ * All protocol knowledge lives in the sources: `MultichainSDK` for the chain,
18
+ * `GearboxAPI` for the backend. None of it is repeated here or in a subclass.
19
+ *
20
+ * @typeParam ListRow - Row type of the namespace's list read.
21
+ **/
22
+ var AbstractNamespace = class {
23
+ logger;
24
+ #onchain;
25
+ #offchain;
26
+ constructor(name, onchain, offchain, logger) {
27
+ this.#onchain = onchain;
28
+ this.#offchain = offchain;
29
+ this.logger = logger?.child?.({ name }) ?? logger;
30
+ }
31
+ /**
32
+ * Asks both sources in parallel and combines whatever answered.
33
+ *
34
+ * The action name is only used in diagnostics and in the error raised when
35
+ * every source the read had failed.
36
+ **/
37
+ async read(action, fromChain, fromBackend, combine) {
38
+ const [onchain, offchain] = await Promise.all([this.fromChain(fromChain), this.fromBackend(fromBackend)]);
39
+ const meta = toMeta(onchain, offchain);
40
+ this.assertAnySourceSucceeded(action, meta);
41
+ return {
42
+ result: combine(onchain.value, offchain.value),
43
+ meta
44
+ };
45
+ }
46
+ /**
47
+ * Reads the namespace's list from both sources, unioned by
48
+ * {@link AbstractNamespace.mergeList}.
49
+ **/
50
+ async readList(action, fromChain, fromBackend) {
51
+ return this.read(action, fromChain, fromBackend, (onchain, offchain) => this.mergeList(onchain ?? [], offchain ?? []));
52
+ }
53
+ /**
54
+ * Reads one row from both sources and merges them under
55
+ * {@link AbstractNamespace.mergeOne}. The key names the chain, so the
56
+ * on-chain leg queries exactly one.
57
+ **/
58
+ async readOne(action, chainId, fromChain, fromBackend) {
59
+ return this.read(action, async (sdk) => {
60
+ const network = this.networkOf(chainId);
61
+ if (!network) return { chains: [] };
62
+ return this.onOneChain(action, network, () => fromChain(sdk));
63
+ }, fromBackend, (onchain, offchain) => {
64
+ if (onchain && offchain) return this.mergeOne(onchain, offchain);
65
+ return onchain ?? offchain;
66
+ });
67
+ }
68
+ /**
69
+ * Reads something only the backend can answer.
70
+ *
71
+ * The fallback stands in for a backend that answered with nothing, which is
72
+ * a different thing from a backend that failed and was dropped.
73
+ **/
74
+ async readOffchain(action, fromBackend, fallback) {
75
+ const offchain = await this.fromBackend(fromBackend);
76
+ const meta = toMeta({ chains: [] }, offchain);
77
+ this.assertAnySourceSucceeded(action, meta);
78
+ return {
79
+ result: offchain.value ?? fallback,
80
+ meta
81
+ };
82
+ }
83
+ /**
84
+ * Runs a read against the chain, or reports the chain as absent when the
85
+ * namespace has no on-chain source at all.
86
+ **/
87
+ async fromChain(run) {
88
+ return this.#onchain ? run(this.#onchain) : { chains: [] };
89
+ }
90
+ /**
91
+ * Runs a read against the backend, turning any rejection into metadata so
92
+ * that a dead backend degrades a `both`-mode read instead of failing it.
93
+ **/
94
+ async fromBackend(run) {
95
+ if (!this.#offchain) return {};
96
+ try {
97
+ const { result, meta } = await run(this.#offchain);
98
+ return {
99
+ value: meta.status === "success" ? result : void 0,
100
+ status: meta
101
+ };
102
+ } catch (error) {
103
+ this.logger?.warn(error, "offchain read failed");
104
+ return { status: {
105
+ status: "error",
106
+ error
107
+ } };
108
+ }
109
+ }
110
+ /**
111
+ * Runs a read against one chain, turning a rejection into that chain's
112
+ * metadata entry.
113
+ **/
114
+ async onOneChain(action, network, run) {
115
+ try {
116
+ return {
117
+ value: await run(),
118
+ chains: [{
119
+ network,
120
+ status: "success"
121
+ }]
122
+ };
123
+ } catch (error) {
124
+ this.logger?.warn(error, `failed to ${action} on ${network}`);
125
+ return { chains: [{
126
+ network,
127
+ status: "error",
128
+ error
129
+ }] };
130
+ }
131
+ }
132
+ /**
133
+ * A read that reached at least one source returns what it has; a read where
134
+ * every source it had failed throws, because an empty result would otherwise
135
+ * be indistinguishable from "there is nothing here".
136
+ **/
137
+ assertAnySourceSucceeded(action, meta) {
138
+ const asked = meta.chains.length > 0 || meta.offchain !== void 0;
139
+ const answered = meta.chains.some((c) => c.status === "success") || meta.offchain?.status === "success";
140
+ if (asked && !answered) throw new AllSourcesFailedError(action, meta);
141
+ }
142
+ networkOf(chainId) {
143
+ try {
144
+ return getNetworkType(chainId);
145
+ } catch {
146
+ this.logger?.debug(`chain ${chainId} is not a Gearbox network, skipping the onchain source`);
147
+ return;
148
+ }
149
+ }
150
+ };
151
+ function toMeta(onchain, offchain) {
152
+ const meta = { chains: onchain.chains };
153
+ if (offchain.status) meta.offchain = offchain.status;
154
+ return meta;
155
+ }
156
+ //#endregion
157
+ export { AbstractNamespace };
@@ -0,0 +1,121 @@
1
+ import { MultichainSDK } from "../sdk/MultichainSDK.js";
2
+ import "../sdk/index.js";
3
+ import { GearboxAPI } from "../offchain/GearboxAPI.js";
4
+ import "../offchain/index.js";
5
+ import { OpportunitiesNamespace } from "./opportunities/OpportunitiesNamespace.js";
6
+ import "./opportunities/index.js";
7
+ //#region src/new-sdk/GearboxSDK.ts
8
+ /**
9
+ * Combined entry point over both sources of Gearbox data: the chain and the
10
+ * Gearbox backend.
11
+ *
12
+ * The {@link Mode} is fixed at construction and decides which methods exist,
13
+ * not what they return, so a screen written against `both` cannot silently
14
+ * degrade to a subset when someone reconfigures the SDK:
15
+ *
16
+ * ```ts
17
+ * const sdk = new GearboxSDK({
18
+ * mode: "both",
19
+ * networks: ["Mainnet"],
20
+ * onchain: { chains: { Mainnet: { rpcURLs } } },
21
+ * offchain: { baseUrl: "https://api.gearbox.fi" },
22
+ * });
23
+ * await sdk.attach();
24
+ *
25
+ * const { result, meta } = await sdk.opportunities.list({ kind: "strategy" });
26
+ * ```
27
+ *
28
+ * Every read answers with `{ result, meta }`: `meta` says which chains and
29
+ * which backend answered, because a partial answer is the normal case for a
30
+ * multi-chain read.
31
+ *
32
+ * @typeParam M - Mode the instance was built in.
33
+ **/
34
+ var GearboxSDK = class {
35
+ /**
36
+ * Sources this instance reads from.
37
+ **/
38
+ mode;
39
+ /**
40
+ * Chains this instance covers. Authoritative even when an injected on-chain
41
+ * SDK covers a different set.
42
+ **/
43
+ networks;
44
+ /**
45
+ * Namespace for pool and strategy opportunities.
46
+ **/
47
+ opportunities;
48
+ #attachOptions;
49
+ #onchain;
50
+ #offchain;
51
+ /**
52
+ * Whether this instance built the on-chain SDK, and therefore attaches it.
53
+ **/
54
+ #ownsOnchain = false;
55
+ #attached;
56
+ constructor(options) {
57
+ const { mode, networks, onchain, offchain, attach, logger } = options;
58
+ this.mode = mode;
59
+ this.networks = [...networks];
60
+ this.#attachOptions = attach;
61
+ const needsOnchain = mode === "onchain" || mode === "both";
62
+ const needsOffchain = mode === "offchain" || mode === "both";
63
+ if (needsOnchain && !onchain) throw new Error(`GearboxSDK in ${mode} mode needs an onchain source`);
64
+ if (needsOffchain && !offchain) throw new Error(`GearboxSDK in ${mode} mode needs an offchain source`);
65
+ if (!needsOnchain && onchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the onchain source it was given`);
66
+ if (!needsOffchain && offchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the offchain source it was given`);
67
+ if (needsOnchain && onchain) if (onchain instanceof MultichainSDK) this.#onchain = onchain;
68
+ else {
69
+ this.#onchain = new MultichainSDK({
70
+ logger,
71
+ ...onchain
72
+ });
73
+ this.#ownsOnchain = true;
74
+ }
75
+ if (needsOffchain && offchain) this.#offchain = offchain instanceof GearboxAPI ? offchain : new GearboxAPI({
76
+ logger,
77
+ ...offchain
78
+ });
79
+ this.#attached = !this.#ownsOnchain;
80
+ this.opportunities = new OpportunitiesNamespace(this.#onchain, this.#offchain, logger);
81
+ }
82
+ /**
83
+ * Attaches the on-chain SDK when this instance owns one.
84
+ *
85
+ * Reads issued before this resolves still reach the chain and fail there, so
86
+ * in `both` mode they are served from the backend alone and report the chain
87
+ * as failed in their meta, rather than blocking.
88
+ *
89
+ * A no-op in `offchain` mode and when an already-attached SDK was injected.
90
+ **/
91
+ async attach() {
92
+ if (this.#attached || !this.#ownsOnchain || !this.#onchain) return;
93
+ await this.#onchain.attach(this.#attachOptions);
94
+ this.#attached = true;
95
+ }
96
+ /**
97
+ * Whether the on-chain source is ready to be read from. Always `true` in
98
+ * `offchain` mode, where there is nothing to wait for.
99
+ **/
100
+ get attached() {
101
+ return this.mode === "offchain" ? true : this.#attached;
102
+ }
103
+ /**
104
+ * The underlying on-chain SDK, for everything this facade does not expose
105
+ * yet: markets, credit accounts, transaction building.
106
+ *
107
+ * `undefined` in `offchain` mode. Otherwise it exists from construction on,
108
+ * but only answers reads once {@link attach} has resolved.
109
+ **/
110
+ get onchain() {
111
+ return this.#onchain;
112
+ }
113
+ /**
114
+ * The underlying backend client. `undefined` in `onchain` mode.
115
+ **/
116
+ get offchain() {
117
+ return this.#offchain;
118
+ }
119
+ };
120
+ //#endregion
121
+ export { GearboxSDK };
@@ -0,0 +1,7 @@
1
+ import { AllSourcesFailedError } from "./types.js";
2
+ import { AbstractNamespace } from "./AbstractNamespace.js";
3
+ import { OpportunitiesNamespace } from "./opportunities/OpportunitiesNamespace.js";
4
+ import "./opportunities/index.js";
5
+ import { GearboxSDK } from "./GearboxSDK.js";
6
+ import "./utils/index.js";
7
+ export { AbstractNamespace, AllSourcesFailedError, GearboxSDK, OpportunitiesNamespace };
@@ -0,0 +1,121 @@
1
+ import { opportunityId } from "../../model/opportunities.js";
2
+ import "../../model/index.js";
3
+ import { AbstractNamespace } from "../AbstractNamespace.js";
4
+ //#region src/new-sdk/opportunities/OpportunitiesNamespace.ts
5
+ /**
6
+ * Fields the backend owns even when the chain also fills them.
7
+ *
8
+ * Empty of on-chain conflicts today — every yield group here is off-chain only,
9
+ * so onchain-first would already yield the backend's value. It is written out
10
+ * anyway so that adding an on-chain estimate of one of them is a deliberate
11
+ * change to this list rather than a silent regression.
12
+ **/
13
+ const OFFCHAIN_OWNED_FIELDS = /* @__PURE__ */ new Set(["supplyApy", "collateralApy"]);
14
+ /**
15
+ * The `opportunities` namespace of the combined SDK.
16
+ *
17
+ * A stateless router over the two sources, see {@link AbstractNamespace} for
18
+ * the routing itself. What is specific to opportunities is the reads below and
19
+ * the merge policy at the bottom of the class.
20
+ *
21
+ * The class implements the methods of every mode; {@link GearboxSDK} exposes it
22
+ * as its mode's slice of {@link OpportunitiesByMode}, so calling a method the
23
+ * mode does not have is a compile error rather than a runtime one.
24
+ **/
25
+ var OpportunitiesNamespace = class extends AbstractNamespace {
26
+ constructor(onchain, offchain, logger) {
27
+ super("Opportunities", onchain, offchain, logger);
28
+ }
29
+ /**
30
+ * {@inheritDoc OpportunitiesBase.list}
31
+ **/
32
+ async list(filter) {
33
+ return this.readList("list opportunities", async (sdk) => {
34
+ const { result, meta } = await sdk.opportunities.list(filter);
35
+ return {
36
+ value: result,
37
+ chains: meta
38
+ };
39
+ }, (api) => api.opportunities.list(filter));
40
+ }
41
+ /**
42
+ * {@inheritDoc OpportunitiesBase.getPool}
43
+ **/
44
+ async getPool(key) {
45
+ return this.readOne("get pool opportunity", key.chainId, (sdk) => sdk.opportunities.getPool(key), (api) => api.opportunities.getPool(key));
46
+ }
47
+ /**
48
+ * {@inheritDoc OpportunitiesBase.getStrategy}
49
+ **/
50
+ async getStrategy(key) {
51
+ return this.readOne("get strategy opportunity", key.chainId, (sdk) => sdk.opportunities.getStrategy(key), (api) => api.opportunities.getStrategy(key));
52
+ }
53
+ history(key) {
54
+ return { chart: (metric, range) => this.#chart(key, metric, range) };
55
+ }
56
+ /**
57
+ * Reads one chart of one opportunity from the backend.
58
+ *
59
+ * The metric a caller may name is gated by the reader's type, so the kind of
60
+ * the key is not re-checked here.
61
+ **/
62
+ async #chart(key, metric, range) {
63
+ const { result, meta } = await this.readOffchain(`get ${metric} history`, (api) => api.opportunities.getHistory({
64
+ opportunity: key,
65
+ range,
66
+ metric
67
+ }), {
68
+ metric,
69
+ points: [],
70
+ metadata: {}
71
+ });
72
+ return {
73
+ data: result.points,
74
+ metadata: {
75
+ ...result.metadata,
76
+ source: meta
77
+ }
78
+ };
79
+ }
80
+ /**
81
+ * Merges the two versions of one opportunity.
82
+ *
83
+ * The chain wins every field it fills; the backend fills the rest and owns
84
+ * {@link OFFCHAIN_OWNED_FIELDS}.
85
+ *
86
+ * The rule is onchain-first, field-wise, and never deeper than one level: a
87
+ * group like `totalSupply` or `supplyApy` is taken whole from one source, so
88
+ * a row never mixes an on-chain token amount with a backend dollar value
89
+ * derived from a different block.
90
+ **/
91
+ mergeOne(onchain, offchain) {
92
+ const merged = { ...onchain };
93
+ for (const [field, value] of Object.entries(offchain)) {
94
+ if (value === void 0) continue;
95
+ if (OFFCHAIN_OWNED_FIELDS.has(field) || merged[field] === void 0) merged[field] = value;
96
+ }
97
+ return merged;
98
+ }
99
+ /**
100
+ * Unions the two lists by canonical opportunity id.
101
+ *
102
+ * Rows present in both are merged by {@link OpportunitiesNamespace.mergeOne}.
103
+ * Rows only the backend knows are appended: a chain the SDK does not cover,
104
+ * or a market it has not loaded, is a reason to show more rather than fewer
105
+ * opportunities.
106
+ **/
107
+ mergeList(onchain, offchain) {
108
+ const byId = /* @__PURE__ */ new Map();
109
+ for (const row of onchain) byId.set(opportunityId(row), row);
110
+ const extra = [];
111
+ for (const row of offchain) {
112
+ const id = opportunityId(row);
113
+ const existing = byId.get(id);
114
+ if (existing) byId.set(id, this.mergeOne(existing, row));
115
+ else extra.push(row);
116
+ }
117
+ return [...byId.values(), ...extra];
118
+ }
119
+ };
120
+ //#endregion
121
+ export { OpportunitiesNamespace };
@@ -0,0 +1,3 @@
1
+ import { OpportunitiesNamespace } from "./OpportunitiesNamespace.js";
2
+ import "./types.js";
3
+ export { OpportunitiesNamespace };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ {"type": "module","sideEffects":false}
@@ -0,0 +1,21 @@
1
+ //#region src/new-sdk/types.ts
2
+ /**
3
+ * Thrown when a read had sources to ask and none of them answered.
4
+ *
5
+ * A read that partially succeeded never throws: it returns what it has and
6
+ * reports the rest in {@link SourceMeta}.
7
+ **/
8
+ var AllSourcesFailedError = class extends Error {
9
+ /**
10
+ * Outcome of every source the failed read asked.
11
+ **/
12
+ meta;
13
+ constructor(action, meta) {
14
+ const reasons = [...meta.chains.filter((c) => c.status === "error").map((c) => `${c.network}: ${c.error}`), ...meta.offchain?.status === "error" ? [`offchain: ${meta.offchain.error}`] : []];
15
+ super(`cannot ${action}, every source failed (${reasons.join("; ")})`);
16
+ this.name = "AllSourcesFailedError";
17
+ this.meta = meta;
18
+ }
19
+ };
20
+ //#endregion
21
+ export { AllSourcesFailedError };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import "./history.js";
2
+ export {};
@@ -0,0 +1,32 @@
1
+ import { OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
2
+ import "./opportunities/index.js";
3
+ //#region src/offchain/GearboxAPI.ts
4
+ /**
5
+ * Client for the Gearbox backend, the off-chain source of the read model.
6
+ *
7
+ * It mirrors the namespace layout of the combined `GearboxSDK`, so the same
8
+ * call reads the same way against either source:
9
+ *
10
+ * ```ts
11
+ * const api = new GearboxAPI({ baseUrl: "https://api.gearbox.fi" });
12
+ * const { result } = await api.opportunities.list({ kind: "strategy" });
13
+ * ```
14
+ *
15
+ * The backend imports the read model from this package and its endpoints
16
+ * return those types directly, so there is no wire DTO layer here — only
17
+ * transport and schema validation.
18
+ *
19
+ * The transport is not implemented yet; see {@link OffchainOpportunities} for
20
+ * what each stubbed endpoint currently answers.
21
+ **/
22
+ var GearboxAPI = class {
23
+ /**
24
+ * Namespace for pool and strategy opportunities.
25
+ **/
26
+ opportunities;
27
+ constructor(options) {
28
+ this.opportunities = new OffchainOpportunities(options);
29
+ }
30
+ };
31
+ //#endregion
32
+ export { GearboxAPI };
@@ -0,0 +1,5 @@
1
+ import { OffchainNotImplementedError, OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
2
+ import "./opportunities/index.js";
3
+ import { GearboxAPI } from "./GearboxAPI.js";
4
+ import "./types.js";
5
+ export { GearboxAPI, OffchainNotImplementedError, OffchainOpportunities };
@@ -0,0 +1,95 @@
1
+ //#region src/offchain/opportunities/OffchainOpportunities.ts
2
+ /**
3
+ * Thrown by the endpoints that have no stub answer, so that a caller in
4
+ * `offchain` mode fails loudly instead of reading an empty detail page as a
5
+ * missing opportunity.
6
+ **/
7
+ var OffchainNotImplementedError = class extends Error {
8
+ constructor(endpoint) {
9
+ super(`GearboxAPI: ${endpoint} is not implemented yet`);
10
+ this.name = "OffchainNotImplementedError";
11
+ }
12
+ };
13
+ /**
14
+ * Backend counterpart of the `opportunities` namespace.
15
+ *
16
+ * This is a stub: the HTTP client is not written yet, so list reads answer with
17
+ * an empty list and detail reads throw. Every signature is already the final
18
+ * one, because the backend returns the read model types directly — there is no
19
+ * wire DTO and no mapper between the two.
20
+ *
21
+ * When the transport lands, each method will validate the response against the
22
+ * matching schema from `src/model` before returning it. A validation failure is
23
+ * a version-skew error and is handled exactly like a transport error: the
24
+ * combined SDK drops the backend's contribution in `both` mode and rethrows in
25
+ * `offchain` mode.
26
+ **/
27
+ var OffchainOpportunities = class {
28
+ #baseUrl;
29
+ #logger;
30
+ constructor(options) {
31
+ this.#baseUrl = options?.baseUrl;
32
+ this.#logger = options?.logger?.child?.({ name: "OffchainOpportunities" });
33
+ }
34
+ /**
35
+ * Base URL the client will call once the transport is implemented.
36
+ **/
37
+ get baseUrl() {
38
+ return this.#baseUrl;
39
+ }
40
+ /**
41
+ * All opportunities the backend knows about, optionally narrowed by
42
+ * {@link OpportunityFilter}.
43
+ *
44
+ * @returns An empty list until the backend client is implemented.
45
+ **/
46
+ async list(filter) {
47
+ this.#logger?.debug({ filter }, "offchain opportunities list is not implemented, serving empty list");
48
+ return {
49
+ result: [],
50
+ meta: { status: "success" }
51
+ };
52
+ }
53
+ /**
54
+ * Detailed view of one pool opportunity.
55
+ *
56
+ * @throws {@link OffchainNotImplementedError} until the backend client is
57
+ * implemented.
58
+ **/
59
+ async getPool(key) {
60
+ throw new OffchainNotImplementedError("opportunities.getPool");
61
+ }
62
+ /**
63
+ * Detailed view of one strategy opportunity.
64
+ *
65
+ * @throws {@link OffchainNotImplementedError} until the backend client is
66
+ * implemented.
67
+ **/
68
+ async getStrategy(key) {
69
+ throw new OffchainNotImplementedError("opportunities.getStrategy");
70
+ }
71
+ /**
72
+ * One historical series of one opportunity. History exists only here:
73
+ * rebuilding it from the chain would mean an archive read per point.
74
+ *
75
+ * The requested metric types the response, so a caller asking for one metric
76
+ * does not have to narrow the union back down. When the transport lands,
77
+ * validation is what upholds it: a response carrying a different metric than
78
+ * the one asked for is a version-skew error like any other.
79
+ *
80
+ * @returns An empty series until the backend client is implemented.
81
+ **/
82
+ async getHistory(query) {
83
+ this.#logger?.debug({ query }, "offchain opportunities history is not implemented, serving empty series");
84
+ return {
85
+ result: {
86
+ metric: query.metric,
87
+ points: [],
88
+ metadata: {}
89
+ },
90
+ meta: { status: "success" }
91
+ };
92
+ }
93
+ };
94
+ //#endregion
95
+ export { OffchainNotImplementedError, OffchainOpportunities };
@@ -0,0 +1,2 @@
1
+ import { OffchainNotImplementedError, OffchainOpportunities } from "./OffchainOpportunities.js";
2
+ export { OffchainNotImplementedError, OffchainOpportunities };
@@ -0,0 +1 @@
1
+ {"type": "module","sideEffects":false}
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { AssetsMap } from "../../sdk/utils/AssetsMap.js";
2
+ import { BigIntMath } from "../../sdk/utils/bigint-math.js";
2
3
  import "../../sdk/index.js";
3
- import { BigIntMath } from "../../common-utils/utils/bigint-math.js";
4
4
  import "../../common-utils/index.js";
5
5
  import { ERROR_UNPRICEABLE_TOKEN, PREVIEW_DUST } from "./types.js";
6
6
  import { isAddressEqual } from "viem";
@@ -1,5 +1,5 @@
1
- import { iPoolV310Abi } from "../../abi/310/generated.js";
2
1
  import { iZapperAbi } from "../../abi/iZapper.js";
2
+ import { iPoolV310Abi } from "../../abi/310/generated.js";
3
3
  import { asPreviewSimulationError } from "./errors.js";
4
4
  //#region src/preview/simulate/simulatePoolOperation.ts
5
5
  function previewRead(operation) {
@@ -1,6 +1,6 @@
1
+ import { ierc20Abi } from "../../abi/iERC20.js";
1
2
  import { iCreditFacadeV310Abi } from "../../abi/310/generated.js";
2
3
  import { AddressMap } from "../../sdk/utils/AddressMap.js";
3
- import { ierc20Abi } from "../../abi/iERC20.js";
4
4
  import "../../sdk/index.js";
5
5
  import { UnexpectedFacadeEventOrderError } from "./errors.js";
6
6
  import { getAddress, isAddressEqual, parseEventLogs } from "viem";
@@ -1,7 +1,7 @@
1
+ import { BigIntMath } from "../../sdk/utils/bigint-math.js";
1
2
  import { chains } from "../../sdk/chain/chains.js";
2
3
  import { toBigInt } from "../../sdk/utils/formatter.js";
3
4
  import "../../sdk/index.js";
4
- import { BigIntMath } from "../../common-utils/utils/bigint-math.js";
5
5
  import "../../common-utils/index.js";
6
6
  import { MerkleXYZApi } from "./merkl-api.js";
7
7
  import { getAddress } from "viem";
@@ -1,8 +1,8 @@
1
+ import { BigIntMath } from "../../sdk/utils/bigint-math.js";
1
2
  import { chains } from "../../sdk/chain/chains.js";
2
3
  import { PERCENTAGE_FACTOR } from "../../sdk/constants/math.js";
3
4
  import { toBN } from "../../sdk/utils/formatter.js";
4
5
  import "../../sdk/index.js";
5
- import { BigIntMath } from "../../common-utils/utils/bigint-math.js";
6
6
  import "../../common-utils/index.js";
7
7
  import axios from "axios";
8
8
  //#region src/rewards/rewards/extra-apy.ts