@gearbox-protocol/sdk 17.0.0-next.3 → 17.0.0

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 (370) hide show
  1. package/dist/cjs/dev/abi.js +14 -15
  2. package/dist/cjs/dev/claimFromFaucet.js +4 -8
  3. package/dist/cjs/dev/createAnvilClient.js +35 -17
  4. package/dist/cjs/dev/index.js +7 -2
  5. package/dist/cjs/dev/kycUtils.js +66 -58
  6. package/dist/cjs/dev/midasUtils.js +113 -16
  7. package/dist/cjs/dev/securitizeUtils.js +82 -42
  8. package/dist/cjs/dev/withdrawalUtils.js +3 -6
  9. package/dist/cjs/model/errors/index.js +2 -0
  10. package/dist/cjs/model/errors/operation-errors.js +19 -1
  11. package/dist/cjs/model/index.js +4 -2
  12. package/dist/cjs/model/rwa.js +4 -4
  13. package/dist/cjs/model/rwa.schema.js +4 -4
  14. package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +1 -1
  15. package/dist/cjs/onchain/OnchainSDK.js +3 -3
  16. package/dist/cjs/onchain/accounts/CreditAccountsServiceV310.js +2 -17
  17. package/dist/cjs/onchain/accounts/intents/borrow.js +140 -0
  18. package/dist/cjs/onchain/accounts/intents/collateral-valuation.js +41 -0
  19. package/dist/cjs/onchain/accounts/intents/guards.js +19 -7
  20. package/dist/cjs/onchain/accounts/intents/index.js +107 -20
  21. package/dist/cjs/onchain/accounts/intents/leverage-band.js +2 -12
  22. package/dist/cjs/onchain/accounts/intents/maxBorrow.js +89 -0
  23. package/dist/cjs/onchain/accounts/intents/maxWithdrawCollateral.js +15 -41
  24. package/dist/cjs/onchain/accounts/intents/open-strategy.js +8 -46
  25. package/dist/cjs/onchain/accounts/intents/operations.js +0 -1
  26. package/dist/cjs/onchain/accounts/intents/realize.js +61 -4
  27. package/dist/cjs/onchain/accounts/intents/tail.js +1 -1
  28. package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +1 -0
  29. package/dist/cjs/onchain/accounts/intents/utils/common.js +19 -0
  30. package/dist/cjs/onchain/accounts/intents/utils/credit-account-slice.js +21 -0
  31. package/dist/cjs/onchain/accounts/intents/utils/index.js +2 -0
  32. package/dist/cjs/onchain/accounts/intents/utils/price-impact.js +12 -9
  33. package/dist/cjs/onchain/accounts/intents/utils/router-path.js +4 -5
  34. package/dist/cjs/onchain/accounts/intents/withdraw-limits.js +97 -0
  35. package/dist/cjs/onchain/accounts/liquidations/LiquidationsService.js +1 -1
  36. package/dist/cjs/onchain/constants/address-provider.js +0 -2
  37. package/dist/cjs/onchain/constants/index.js +0 -1
  38. package/dist/cjs/onchain/core/createAddressProvider.js +2 -5
  39. package/dist/cjs/onchain/index.js +60 -43
  40. package/dist/cjs/onchain/market/adapters/abi/conctructorAbi.js +2 -2
  41. package/dist/cjs/onchain/market/adapters/contracts/AbstractAdapter.js +6 -0
  42. package/dist/cjs/onchain/market/adapters/contracts/MidasGatewayAdapterContract.js +49 -5
  43. package/dist/cjs/onchain/market/credit/CreditSuite.js +33 -12
  44. package/dist/cjs/onchain/market/index.js +50 -38
  45. package/dist/cjs/onchain/market/oracle/PriceOracleBaseContract.js +3 -3
  46. package/dist/cjs/onchain/market/pricefeeds/AbstractPriceFeed.js +0 -5
  47. package/dist/cjs/onchain/market/pricefeeds/PriceFeedRegistry.js +127 -0
  48. package/dist/cjs/onchain/market/pricefeeds/{BalancerStablePriceFeed.js → feeds/BalancerStablePriceFeed.js} +4 -4
  49. package/dist/cjs/onchain/market/pricefeeds/{BalancerWeightedPriceFeed.js → feeds/BalancerWeightedPriceFeed.js} +4 -4
  50. package/dist/cjs/onchain/market/pricefeeds/{BoundedPriceFeed.js → feeds/BoundedPriceFeed.js} +4 -4
  51. package/dist/cjs/onchain/market/pricefeeds/{CompositePriceFeed.js → feeds/CompositePriceFeed.js} +4 -4
  52. package/dist/cjs/onchain/market/pricefeeds/{ConstantPriceFeed.js → feeds/ConstantPriceFeed.js} +3 -3
  53. package/dist/cjs/onchain/market/pricefeeds/{CurveCryptoPriceFeed.js → feeds/CurveCryptoPriceFeed.js} +4 -4
  54. package/dist/cjs/onchain/market/pricefeeds/{CurveStablePriceFeed.js → feeds/CurveStablePriceFeed.js} +4 -4
  55. package/dist/cjs/onchain/market/pricefeeds/{CurveUSDPriceFeed.js → feeds/CurveUSDPriceFeed.js} +4 -4
  56. package/dist/cjs/onchain/market/pricefeeds/{Erc4626PriceFeed.js → feeds/Erc4626PriceFeed.js} +4 -4
  57. package/dist/cjs/onchain/market/pricefeeds/{ExternalPriceFeed.js → feeds/ExternalPriceFeed.js} +4 -4
  58. package/dist/cjs/onchain/market/pricefeeds/{MellowLRTPriceFeed.js → feeds/MellowLRTPriceFeed.js} +4 -4
  59. package/dist/cjs/onchain/market/pricefeeds/{PendleTWAPPTPriceFeed.js → feeds/PendleTWAPPTPriceFeed.js} +4 -4
  60. package/dist/cjs/onchain/market/pricefeeds/{PythPriceFeed.js → feeds/PythPriceFeed.js} +3 -3
  61. package/dist/cjs/onchain/market/pricefeeds/{RedstonePriceFeed.js → feeds/RedstonePriceFeed.js} +6 -6
  62. package/dist/cjs/onchain/market/pricefeeds/{WstETHPriceFeed.js → feeds/WstETHPriceFeed.js} +4 -4
  63. package/dist/cjs/onchain/market/pricefeeds/{YearnPriceFeed.js → feeds/YearnPriceFeed.js} +4 -4
  64. package/dist/cjs/onchain/market/pricefeeds/{ZeroPriceFeed.js → feeds/ZeroPriceFeed.js} +4 -4
  65. package/dist/cjs/onchain/market/pricefeeds/feeds/index.js +35 -0
  66. package/dist/cjs/onchain/market/pricefeeds/index.js +51 -39
  67. package/dist/cjs/onchain/market/pricefeeds/updates/UpdatablePriceFeedRegistry.js +189 -0
  68. package/dist/cjs/onchain/market/pricefeeds/{getRawPriceUpdates.js → updates/getRawPriceUpdates.js} +2 -2
  69. package/dist/cjs/onchain/market/pricefeeds/updates/index.js +8 -0
  70. package/dist/cjs/onchain/market/pricefeeds/{isUpdatablePriceFeed.js → updates/isUpdatablePriceFeed.js} +1 -1
  71. package/dist/cjs/onchain/market/pricefeeds/updates/updatableDependencies.js +14 -0
  72. package/dist/cjs/onchain/market/rwa/RWARegistry.js +4 -7
  73. package/dist/cjs/onchain/market/rwa/index.js +1 -0
  74. package/dist/cjs/onchain/market/rwa/midas/MidasDegenNFT.js +45 -5
  75. package/dist/cjs/onchain/market/rwa/securitize/SecuritizeDegenNFT.js +56 -12
  76. package/dist/cjs/onchain/market/rwa/securitize/SecuritizeRWAFactory.js +0 -37
  77. package/dist/cjs/onchain/opportunities/MultichainOpportunitiesService.js +8 -2
  78. package/dist/cjs/onchain/opportunities/OpportunitiesService.js +16 -4
  79. package/dist/cjs/onchain/positions/calcHealthFactor.js +3 -3
  80. package/dist/cjs/onchain/preview/index.js +2 -0
  81. package/dist/cjs/onchain/preview/parse/parseRWAFactoryOperationCalldata.js +2 -2
  82. package/dist/cjs/onchain/preview/preview/index.js +2 -0
  83. package/dist/cjs/onchain/preview/preview/midasGreenlistsAccount.js +17 -0
  84. package/dist/cjs/onchain/preview/preview/previewOpenStrategyPosition.js +13 -16
  85. package/dist/cjs/onchain/preview/preview/previewOperation.js +19 -11
  86. package/dist/cjs/onchain/preview/preview/replayMulticall.js +4 -3
  87. package/dist/cjs/onchain/validation/bundles/checkCollateralFunding.js +3 -3
  88. package/dist/cjs/onchain/validation/bundles/checkCreditOperation.js +12 -10
  89. package/dist/cjs/onchain/validation/bundles/checkIncreaseDebt.js +28 -0
  90. package/dist/cjs/onchain/validation/bundles/{checkQuotasAsked.js → checkIncreaseQuota.js} +3 -3
  91. package/dist/cjs/onchain/validation/bundles/checkMidasAccountGreenlist.js +44 -0
  92. package/dist/cjs/onchain/validation/bundles/checkRWAOpenRequirements.js +9 -9
  93. package/dist/cjs/onchain/validation/bundles/checkRWAOpening.js +18 -8
  94. package/dist/cjs/onchain/validation/bundles/index.js +6 -4
  95. package/dist/cjs/onchain/validation/checkOperation.js +0 -1
  96. package/dist/cjs/onchain/validation/checkSimulation.js +1 -1
  97. package/dist/cjs/onchain/validation/checks/checkBorrowLimit.js +1 -1
  98. package/dist/cjs/onchain/validation/checks/checkDebtLimits.js +2 -5
  99. package/dist/cjs/onchain/validation/checks/checkPoolLiquidity.js +1 -1
  100. package/dist/cjs/onchain/validation/checks/checkReservePriceLimited.js +30 -0
  101. package/dist/cjs/onchain/validation/checks/index.js +2 -0
  102. package/dist/cjs/onchain/validation/index.js +8 -4
  103. package/dist/cjs/permissionless/utils/price-update/get-price-update-tx.js +2 -2
  104. package/dist/cjs/permissionless/utils/price-update/get-updatable-feeds.js +2 -2
  105. package/dist/cjs/sdk/execute/ExecuteApi.js +67 -7
  106. package/dist/cjs/sdk/opportunities/OpportunitiesNamespace.js +9 -3
  107. package/dist/cjs/sdk/prepare/PrepareApi.js +89 -14
  108. package/dist/esm/dev/abi.js +14 -15
  109. package/dist/esm/dev/claimFromFaucet.js +4 -8
  110. package/dist/esm/dev/createAnvilClient.js +36 -19
  111. package/dist/esm/dev/index.js +4 -4
  112. package/dist/esm/dev/kycUtils.js +66 -59
  113. package/dist/esm/dev/midasUtils.js +112 -18
  114. package/dist/esm/dev/securitizeUtils.js +84 -44
  115. package/dist/esm/dev/withdrawalUtils.js +3 -6
  116. package/dist/esm/model/errors/index.js +2 -2
  117. package/dist/esm/model/errors/operation-errors.js +18 -2
  118. package/dist/esm/model/index.js +4 -4
  119. package/dist/esm/model/rwa.js +4 -4
  120. package/dist/esm/model/rwa.schema.js +5 -5
  121. package/dist/esm/offchain/opportunities/OffchainOpportunities.js +1 -1
  122. package/dist/esm/onchain/OnchainSDK.js +3 -3
  123. package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -17
  124. package/dist/esm/onchain/accounts/intents/borrow.js +138 -0
  125. package/dist/esm/onchain/accounts/intents/collateral-valuation.js +40 -0
  126. package/dist/esm/onchain/accounts/intents/guards.js +19 -7
  127. package/dist/esm/onchain/accounts/intents/index.js +107 -20
  128. package/dist/esm/onchain/accounts/intents/leverage-band.js +2 -12
  129. package/dist/esm/onchain/accounts/intents/maxBorrow.js +87 -0
  130. package/dist/esm/onchain/accounts/intents/maxWithdrawCollateral.js +15 -41
  131. package/dist/esm/onchain/accounts/intents/open-strategy.js +8 -46
  132. package/dist/esm/onchain/accounts/intents/operations.js +0 -1
  133. package/dist/esm/onchain/accounts/intents/realize.js +61 -4
  134. package/dist/esm/onchain/accounts/intents/tail.js +1 -1
  135. package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +1 -0
  136. package/dist/esm/onchain/accounts/intents/utils/common.js +19 -1
  137. package/dist/esm/onchain/accounts/intents/utils/credit-account-slice.js +21 -1
  138. package/dist/esm/onchain/accounts/intents/utils/index.js +3 -3
  139. package/dist/esm/onchain/accounts/intents/utils/price-impact.js +12 -9
  140. package/dist/esm/onchain/accounts/intents/utils/router-path.js +4 -5
  141. package/dist/esm/onchain/accounts/intents/withdraw-limits.js +95 -0
  142. package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
  143. package/dist/esm/onchain/constants/address-provider.js +1 -2
  144. package/dist/esm/onchain/constants/index.js +2 -2
  145. package/dist/esm/onchain/core/createAddressProvider.js +2 -5
  146. package/dist/esm/onchain/index.js +32 -24
  147. package/dist/esm/onchain/market/adapters/abi/conctructorAbi.js +2 -2
  148. package/dist/esm/onchain/market/adapters/contracts/AbstractAdapter.js +6 -0
  149. package/dist/esm/onchain/market/adapters/contracts/MidasGatewayAdapterContract.js +50 -6
  150. package/dist/esm/onchain/market/credit/CreditSuite.js +33 -12
  151. package/dist/esm/onchain/market/index.js +26 -21
  152. package/dist/esm/onchain/market/oracle/PriceOracleBaseContract.js +1 -1
  153. package/dist/esm/onchain/market/pricefeeds/AbstractPriceFeed.js +0 -5
  154. package/dist/esm/onchain/market/pricefeeds/PriceFeedRegistry.js +126 -0
  155. package/dist/esm/onchain/market/pricefeeds/{BalancerStablePriceFeed.js → feeds/BalancerStablePriceFeed.js} +4 -4
  156. package/dist/esm/onchain/market/pricefeeds/{BalancerWeightedPriceFeed.js → feeds/BalancerWeightedPriceFeed.js} +4 -4
  157. package/dist/esm/onchain/market/pricefeeds/{BoundedPriceFeed.js → feeds/BoundedPriceFeed.js} +4 -4
  158. package/dist/esm/onchain/market/pricefeeds/{CompositePriceFeed.js → feeds/CompositePriceFeed.js} +4 -4
  159. package/dist/esm/onchain/market/pricefeeds/{ConstantPriceFeed.js → feeds/ConstantPriceFeed.js} +3 -3
  160. package/dist/esm/onchain/market/pricefeeds/{CurveCryptoPriceFeed.js → feeds/CurveCryptoPriceFeed.js} +4 -4
  161. package/dist/esm/onchain/market/pricefeeds/{CurveStablePriceFeed.js → feeds/CurveStablePriceFeed.js} +4 -4
  162. package/dist/esm/onchain/market/pricefeeds/{CurveUSDPriceFeed.js → feeds/CurveUSDPriceFeed.js} +4 -4
  163. package/dist/esm/onchain/market/pricefeeds/{Erc4626PriceFeed.js → feeds/Erc4626PriceFeed.js} +4 -4
  164. package/dist/esm/onchain/market/pricefeeds/{ExternalPriceFeed.js → feeds/ExternalPriceFeed.js} +4 -4
  165. package/dist/esm/onchain/market/pricefeeds/{MellowLRTPriceFeed.js → feeds/MellowLRTPriceFeed.js} +4 -4
  166. package/dist/esm/onchain/market/pricefeeds/{PendleTWAPPTPriceFeed.js → feeds/PendleTWAPPTPriceFeed.js} +4 -4
  167. package/dist/esm/onchain/market/pricefeeds/{PythPriceFeed.js → feeds/PythPriceFeed.js} +3 -3
  168. package/dist/esm/onchain/market/pricefeeds/{RedstonePriceFeed.js → feeds/RedstonePriceFeed.js} +6 -6
  169. package/dist/esm/onchain/market/pricefeeds/{WstETHPriceFeed.js → feeds/WstETHPriceFeed.js} +4 -4
  170. package/dist/esm/onchain/market/pricefeeds/{YearnPriceFeed.js → feeds/YearnPriceFeed.js} +4 -4
  171. package/dist/esm/onchain/market/pricefeeds/{ZeroPriceFeed.js → feeds/ZeroPriceFeed.js} +4 -4
  172. package/dist/esm/onchain/market/pricefeeds/feeds/index.js +18 -0
  173. package/dist/esm/onchain/market/pricefeeds/index.js +27 -21
  174. package/dist/esm/onchain/market/pricefeeds/updates/UpdatablePriceFeedRegistry.js +188 -0
  175. package/dist/esm/onchain/market/pricefeeds/{getRawPriceUpdates.js → updates/getRawPriceUpdates.js} +2 -2
  176. package/dist/esm/onchain/market/pricefeeds/updates/index.js +5 -1
  177. package/dist/esm/onchain/market/pricefeeds/{isUpdatablePriceFeed.js → updates/isUpdatablePriceFeed.js} +1 -1
  178. package/dist/esm/onchain/market/pricefeeds/updates/updatableDependencies.js +13 -0
  179. package/dist/esm/onchain/market/rwa/RWARegistry.js +4 -8
  180. package/dist/esm/onchain/market/rwa/index.js +2 -2
  181. package/dist/esm/onchain/market/rwa/midas/MidasDegenNFT.js +45 -5
  182. package/dist/esm/onchain/market/rwa/securitize/SecuritizeDegenNFT.js +55 -10
  183. package/dist/esm/onchain/market/rwa/securitize/SecuritizeRWAFactory.js +0 -37
  184. package/dist/esm/onchain/opportunities/MultichainOpportunitiesService.js +8 -2
  185. package/dist/esm/onchain/opportunities/OpportunitiesService.js +16 -4
  186. package/dist/esm/onchain/positions/calcHealthFactor.js +3 -3
  187. package/dist/esm/onchain/preview/index.js +2 -1
  188. package/dist/esm/onchain/preview/parse/parseRWAFactoryOperationCalldata.js +2 -2
  189. package/dist/esm/onchain/preview/preview/index.js +2 -1
  190. package/dist/esm/onchain/preview/preview/midasGreenlistsAccount.js +16 -0
  191. package/dist/esm/onchain/preview/preview/previewOpenStrategyPosition.js +14 -17
  192. package/dist/esm/onchain/preview/preview/previewOperation.js +19 -11
  193. package/dist/esm/onchain/preview/preview/replayMulticall.js +4 -3
  194. package/dist/esm/onchain/validation/bundles/checkCollateralFunding.js +3 -3
  195. package/dist/esm/onchain/validation/bundles/checkCreditOperation.js +12 -10
  196. package/dist/esm/onchain/validation/bundles/checkIncreaseDebt.js +27 -0
  197. package/dist/esm/onchain/validation/bundles/{checkQuotasAsked.js → checkIncreaseQuota.js} +3 -3
  198. package/dist/esm/onchain/validation/bundles/checkMidasAccountGreenlist.js +43 -0
  199. package/dist/esm/onchain/validation/bundles/checkRWAOpenRequirements.js +9 -9
  200. package/dist/esm/onchain/validation/bundles/checkRWAOpening.js +18 -8
  201. package/dist/esm/onchain/validation/bundles/index.js +4 -3
  202. package/dist/esm/onchain/validation/checkOperation.js +0 -1
  203. package/dist/esm/onchain/validation/checkSimulation.js +1 -1
  204. package/dist/esm/onchain/validation/checks/checkBorrowLimit.js +1 -1
  205. package/dist/esm/onchain/validation/checks/checkDebtLimits.js +2 -5
  206. package/dist/esm/onchain/validation/checks/checkPoolLiquidity.js +1 -1
  207. package/dist/esm/onchain/validation/checks/checkReservePriceLimited.js +29 -0
  208. package/dist/esm/onchain/validation/checks/index.js +2 -1
  209. package/dist/esm/onchain/validation/index.js +5 -3
  210. package/dist/esm/permissionless/utils/price-update/get-price-update-tx.js +1 -1
  211. package/dist/esm/permissionless/utils/price-update/get-updatable-feeds.js +1 -1
  212. package/dist/esm/sdk/execute/ExecuteApi.js +67 -7
  213. package/dist/esm/sdk/opportunities/OpportunitiesNamespace.js +9 -3
  214. package/dist/esm/sdk/prepare/PrepareApi.js +94 -19
  215. package/dist/types/dev/abi.d.ts +13 -15
  216. package/dist/types/dev/claimFromFaucet.d.ts +0 -1
  217. package/dist/types/dev/createAnvilClient.d.ts +10 -3
  218. package/dist/types/dev/index.d.ts +4 -4
  219. package/dist/types/dev/kycUtils.d.ts +15 -24
  220. package/dist/types/dev/midasUtils.d.ts +35 -4
  221. package/dist/types/dev/securitizeUtils.d.ts +3 -9
  222. package/dist/types/model/errors/index.d.ts +2 -2
  223. package/dist/types/model/errors/operation-errors.d.ts +65 -11
  224. package/dist/types/model/index.d.ts +5 -5
  225. package/dist/types/model/opportunities.d.ts +6 -7
  226. package/dist/types/model/previews.d.ts +25 -19
  227. package/dist/types/model/rwa.d.ts +47 -27
  228. package/dist/types/model/rwa.schema.d.ts +3 -3
  229. package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +1 -2
  230. package/dist/types/offchain/opportunities/types.d.ts +1 -4
  231. package/dist/types/onchain/OnchainSDK.d.ts +2 -2
  232. package/dist/types/onchain/accounts/index.d.ts +5 -4
  233. package/dist/types/onchain/accounts/intents/borrow.d.ts +141 -0
  234. package/dist/types/onchain/accounts/intents/collateral-valuation.d.ts +42 -0
  235. package/dist/types/onchain/accounts/intents/guards.d.ts +18 -2
  236. package/dist/types/onchain/accounts/intents/index.d.ts +103 -15
  237. package/dist/types/onchain/accounts/intents/maxBorrow.d.ts +54 -0
  238. package/dist/types/onchain/accounts/intents/maxWithdrawCollateral.d.ts +3 -8
  239. package/dist/types/onchain/accounts/intents/open-strategy.d.ts +3 -17
  240. package/dist/types/onchain/accounts/intents/testing/sdk-mock.d.ts +2 -0
  241. package/dist/types/onchain/accounts/intents/tests/open-strategy.fixtures.d.ts +2 -2
  242. package/dist/types/onchain/accounts/intents/types.d.ts +51 -6
  243. package/dist/types/onchain/accounts/intents/utils/common.d.ts +15 -1
  244. package/dist/types/onchain/accounts/intents/utils/credit-account-slice.d.ts +14 -1
  245. package/dist/types/onchain/accounts/intents/utils/index.d.ts +3 -3
  246. package/dist/types/onchain/accounts/intents/utils/price-impact.d.ts +4 -1
  247. package/dist/types/onchain/accounts/intents/utils/router-path.d.ts +2 -5
  248. package/dist/types/onchain/accounts/intents/withdraw-limits.d.ts +80 -0
  249. package/dist/types/onchain/accounts/types.d.ts +4 -2
  250. package/dist/types/onchain/constants/address-provider.d.ts +1 -2
  251. package/dist/types/onchain/constants/index.d.ts +2 -2
  252. package/dist/types/onchain/index.d.ts +40 -31
  253. package/dist/types/onchain/market/adapters/contracts/AbstractAdapter.d.ts +6 -0
  254. package/dist/types/onchain/market/adapters/contracts/MidasGatewayAdapterContract.d.ts +20 -4
  255. package/dist/types/onchain/market/adapters/types.d.ts +8 -0
  256. package/dist/types/onchain/market/credit/CreditSuite.d.ts +13 -3
  257. package/dist/types/onchain/market/credit/types.d.ts +2 -2
  258. package/dist/types/onchain/market/index.d.ts +29 -24
  259. package/dist/types/onchain/market/oracle/PriceOracleBaseContract.d.ts +2 -1
  260. package/dist/types/onchain/market/oracle/types.d.ts +2 -1
  261. package/dist/types/onchain/market/pool/types.d.ts +1 -1
  262. package/dist/types/onchain/market/pricefeeds/AbstractPriceFeed.d.ts +1 -2
  263. package/dist/types/onchain/market/pricefeeds/PriceFeedRegistry.d.ts +60 -0
  264. package/dist/types/onchain/market/pricefeeds/feeds/BalancerStablePriceFeed.d.ts +12 -0
  265. package/dist/types/onchain/market/pricefeeds/{BalancerWeightedPriceFeed.d.ts → feeds/BalancerWeightedPriceFeed.d.ts} +7 -7
  266. package/dist/types/onchain/market/pricefeeds/feeds/BoundedPriceFeed.d.ts +15 -0
  267. package/dist/types/onchain/market/pricefeeds/{CompositePriceFeed.d.ts → feeds/CompositePriceFeed.d.ts} +6 -6
  268. package/dist/types/onchain/market/pricefeeds/{ConstantPriceFeed.d.ts → feeds/ConstantPriceFeed.d.ts} +6 -6
  269. package/dist/types/onchain/market/pricefeeds/feeds/CurveCryptoPriceFeed.d.ts +12 -0
  270. package/dist/types/onchain/market/pricefeeds/feeds/CurveStablePriceFeed.d.ts +12 -0
  271. package/dist/types/onchain/market/pricefeeds/feeds/CurveUSDPriceFeed.d.ts +12 -0
  272. package/dist/types/onchain/market/pricefeeds/feeds/Erc4626PriceFeed.d.ts +12 -0
  273. package/dist/types/onchain/market/pricefeeds/feeds/ExternalPriceFeed.d.ts +11 -0
  274. package/dist/types/onchain/market/pricefeeds/feeds/MellowLRTPriceFeed.d.ts +12 -0
  275. package/dist/types/onchain/market/pricefeeds/{PendleTWAPPTPriceFeed.d.ts → feeds/PendleTWAPPTPriceFeed.d.ts} +3 -3
  276. package/dist/types/onchain/market/pricefeeds/{PythPriceFeed.d.ts → feeds/PythPriceFeed.d.ts} +3 -3
  277. package/dist/types/onchain/market/pricefeeds/{RedstonePriceFeed.d.ts → feeds/RedstonePriceFeed.d.ts} +9 -9
  278. package/dist/types/onchain/market/pricefeeds/feeds/WstETHPriceFeed.d.ts +12 -0
  279. package/dist/types/onchain/market/pricefeeds/feeds/YearnPriceFeed.d.ts +12 -0
  280. package/dist/types/onchain/market/pricefeeds/feeds/ZeroPriceFeed.d.ts +11 -0
  281. package/dist/types/onchain/market/pricefeeds/feeds/index.d.ts +18 -0
  282. package/dist/types/onchain/market/pricefeeds/index.d.ts +28 -22
  283. package/dist/types/onchain/market/pricefeeds/types.d.ts +1 -31
  284. package/dist/types/onchain/market/pricefeeds/updates/RedstoneUpdater.d.ts +1 -1
  285. package/dist/types/onchain/market/pricefeeds/updates/UpdatablePriceFeedRegistry.d.ts +106 -0
  286. package/dist/types/onchain/market/pricefeeds/updates/getRawPriceUpdates.d.ts +6 -0
  287. package/dist/types/onchain/market/pricefeeds/updates/index.d.ts +7 -3
  288. package/dist/types/onchain/market/pricefeeds/updates/isUpdatablePriceFeed.d.ts +6 -0
  289. package/dist/types/onchain/market/pricefeeds/updates/types.d.ts +56 -2
  290. package/dist/types/onchain/market/pricefeeds/updates/updatableDependencies.d.ts +10 -0
  291. package/dist/types/onchain/market/rwa/RWARegistry.d.ts +2 -1
  292. package/dist/types/onchain/market/rwa/index.d.ts +3 -3
  293. package/dist/types/onchain/market/rwa/midas/MidasDegenNFT.d.ts +19 -4
  294. package/dist/types/onchain/market/rwa/securitize/SecuritizeDegenNFT.d.ts +24 -4
  295. package/dist/types/onchain/market/rwa/securitize/SecuritizeRWAFactory.d.ts +2 -14
  296. package/dist/types/onchain/market/rwa/types.d.ts +39 -37
  297. package/dist/types/onchain/opportunities/MultichainOpportunitiesService.d.ts +5 -1
  298. package/dist/types/onchain/opportunities/OpportunitiesService.d.ts +10 -3
  299. package/dist/types/onchain/opportunities/types.d.ts +6 -1
  300. package/dist/types/onchain/preview/index.d.ts +2 -1
  301. package/dist/types/onchain/preview/preview/index.d.ts +2 -1
  302. package/dist/types/onchain/preview/preview/midasGreenlistsAccount.d.ts +13 -0
  303. package/dist/types/onchain/preview/preview/previewOpenStrategyPosition.d.ts +5 -3
  304. package/dist/types/onchain/preview/preview/replayMulticall.d.ts +8 -7
  305. package/dist/types/onchain/validation/bundles/checkCreditOperation.d.ts +5 -4
  306. package/dist/types/onchain/validation/bundles/{checkDraw.d.ts → checkIncreaseDebt.d.ts} +7 -7
  307. package/dist/types/onchain/validation/bundles/{checkQuotasAsked.d.ts → checkIncreaseQuota.d.ts} +3 -3
  308. package/dist/types/onchain/validation/bundles/checkMidasAccountGreenlist.d.ts +22 -0
  309. package/dist/types/onchain/validation/bundles/checkRWAOpenRequirements.d.ts +2 -2
  310. package/dist/types/onchain/validation/bundles/checkRWAOpening.d.ts +9 -7
  311. package/dist/types/onchain/validation/bundles/index.d.ts +5 -4
  312. package/dist/types/onchain/validation/checks/checkBorrowLimit.d.ts +1 -1
  313. package/dist/types/onchain/validation/checks/checkDebtLimits.d.ts +3 -3
  314. package/dist/types/onchain/validation/checks/checkPoolLiquidity.d.ts +1 -1
  315. package/dist/types/onchain/validation/checks/checkReservePriceLimited.d.ts +31 -0
  316. package/dist/types/onchain/validation/checks/index.d.ts +2 -1
  317. package/dist/types/onchain/validation/index.d.ts +6 -4
  318. package/dist/types/onchain/validation/raise.d.ts +2 -2
  319. package/dist/types/sdk/execute/index.d.ts +2 -2
  320. package/dist/types/sdk/execute/types.d.ts +48 -6
  321. package/dist/types/sdk/index.d.ts +5 -4
  322. package/dist/types/sdk/opportunities/OpportunitiesNamespace.d.ts +5 -1
  323. package/dist/types/sdk/opportunities/types.d.ts +9 -3
  324. package/dist/types/sdk/prepare/PrepareApi.d.ts +16 -4
  325. package/dist/types/sdk/prepare/index.d.ts +4 -3
  326. package/dist/types/sdk/prepare/types.d.ts +200 -66
  327. package/package.json +1 -1
  328. package/dist/cjs/onchain/accounts/utils/midasUtils.js +0 -70
  329. package/dist/cjs/onchain/market/pricefeeds/PriceFeedsRegister.js +0 -301
  330. package/dist/cjs/onchain/validation/bundles/checkDraw.js +0 -28
  331. package/dist/cjs/plugins/degen-distributors/DegenDistributorsPlugin.js +0 -61
  332. package/dist/cjs/plugins/degen-distributors/index.js +0 -4
  333. package/dist/cjs/plugins/degen-distributors/package.json +0 -1
  334. package/dist/cjs/plugins/degen-distributors/types.js +0 -1
  335. package/dist/cjs/plugins/delayed-withdrawal/DelayedWithdrawalPlugin.js +0 -114
  336. package/dist/cjs/plugins/delayed-withdrawal/index.js +0 -4
  337. package/dist/cjs/plugins/delayed-withdrawal/package.json +0 -1
  338. package/dist/cjs/plugins/delayed-withdrawal/types.js +0 -1
  339. package/dist/esm/onchain/accounts/utils/midasUtils.js +0 -69
  340. package/dist/esm/onchain/market/pricefeeds/PriceFeedsRegister.js +0 -300
  341. package/dist/esm/onchain/validation/bundles/checkDraw.js +0 -27
  342. package/dist/esm/plugins/degen-distributors/DegenDistributorsPlugin.js +0 -60
  343. package/dist/esm/plugins/degen-distributors/index.js +0 -3
  344. package/dist/esm/plugins/degen-distributors/package.json +0 -1
  345. package/dist/esm/plugins/degen-distributors/types.js +0 -1
  346. package/dist/esm/plugins/delayed-withdrawal/DelayedWithdrawalPlugin.js +0 -113
  347. package/dist/esm/plugins/delayed-withdrawal/index.js +0 -3
  348. package/dist/esm/plugins/delayed-withdrawal/package.json +0 -1
  349. package/dist/esm/plugins/delayed-withdrawal/types.js +0 -1
  350. package/dist/types/onchain/accounts/utils/midasUtils.d.ts +0 -38
  351. package/dist/types/onchain/market/pricefeeds/BalancerStablePriceFeed.d.ts +0 -12
  352. package/dist/types/onchain/market/pricefeeds/BoundedPriceFeed.d.ts +0 -15
  353. package/dist/types/onchain/market/pricefeeds/CurveCryptoPriceFeed.d.ts +0 -12
  354. package/dist/types/onchain/market/pricefeeds/CurveStablePriceFeed.d.ts +0 -12
  355. package/dist/types/onchain/market/pricefeeds/CurveUSDPriceFeed.d.ts +0 -12
  356. package/dist/types/onchain/market/pricefeeds/Erc4626PriceFeed.d.ts +0 -12
  357. package/dist/types/onchain/market/pricefeeds/ExternalPriceFeed.d.ts +0 -11
  358. package/dist/types/onchain/market/pricefeeds/MellowLRTPriceFeed.d.ts +0 -12
  359. package/dist/types/onchain/market/pricefeeds/PriceFeedsRegister.d.ts +0 -180
  360. package/dist/types/onchain/market/pricefeeds/WstETHPriceFeed.d.ts +0 -12
  361. package/dist/types/onchain/market/pricefeeds/YearnPriceFeed.d.ts +0 -12
  362. package/dist/types/onchain/market/pricefeeds/ZeroPriceFeed.d.ts +0 -11
  363. package/dist/types/onchain/market/pricefeeds/getRawPriceUpdates.d.ts +0 -5
  364. package/dist/types/onchain/market/pricefeeds/isUpdatablePriceFeed.d.ts +0 -5
  365. package/dist/types/plugins/degen-distributors/DegenDistributorsPlugin.d.ts +0 -25
  366. package/dist/types/plugins/degen-distributors/index.d.ts +0 -3
  367. package/dist/types/plugins/degen-distributors/types.d.ts +0 -8
  368. package/dist/types/plugins/delayed-withdrawal/DelayedWithdrawalPlugin.d.ts +0 -25
  369. package/dist/types/plugins/delayed-withdrawal/index.d.ts +0 -3
  370. package/dist/types/plugins/delayed-withdrawal/types.d.ts +0 -10
@@ -7,6 +7,7 @@ const require_onchain_validation_checks_checkCreditManagerPaused = require("../.
7
7
  const require_onchain_validation_checks_checkForbiddenToken = require("../../validation/checks/checkForbiddenToken.js");
8
8
  const require_onchain_validation_checks_checkMarketExpired = require("../../validation/checks/checkMarketExpired.js");
9
9
  const require_onchain_validation_checks_checkQuotaLimit = require("../../validation/checks/checkQuotaLimit.js");
10
+ const require_onchain_validation_checks_checkReservePriceLimited = require("../../validation/checks/checkReservePriceLimited.js");
10
11
  const require_onchain_validation_raise = require("../../validation/raise.js");
11
12
  require("../../validation/index.js");
12
13
  const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
@@ -44,13 +45,13 @@ function assertMarketOperable(suite) {
44
45
  }
45
46
  /** The pool has to be able to lend what the plan means to draw. */
46
47
  function assertCanBorrow(sdk, suite, amount) {
47
- const { value, limit } = suite.maxBorrowAmount();
48
+ const maxBorrowAmount = suite.maxBorrowAmount();
48
49
  require_onchain_validation_raise.raise(require_onchain_validation_checks_checkBorrowLimit.checkBorrowLimit({
49
50
  requested: amount,
50
- available: value,
51
- limit,
51
+ available: maxBorrowAmount.amount.value,
52
+ limit: maxBorrowAmount.limit,
52
53
  underlying: require_onchain_validation_helpers_token.toToken(sdk, suite.market.pool.underlying)
53
- }), `borrow: ${amount} exceeds what the pool can lend now (${value})`);
54
+ }), `borrow: ${amount} exceeds what the pool can lend now (${maxBorrowAmount.amount.value})`);
54
55
  }
55
56
  /**
56
57
  * What the account is allowed to end up holding more of than it started with.
@@ -112,13 +113,24 @@ function assertGrowthAllowed(args) {
112
113
  * whose reserve feed the SDK cannot read keeps its main price, so a plan can
113
114
  * still be refused on-chain after passing here.
114
115
  */
115
- function assertCollateralised(healthFactorBps, safePrices) {
116
+ function assertCollateralised(healthFactorBps, safePrices, atSafePrices) {
116
117
  const healthFactorThreshold = require_onchain_validation_helpers_health_factor_limits.MIN_HEALTH_FACTOR_FACADE;
117
- require_onchain_validation_raise.raise(require_onchain_validation_checks_checkCollateralised.checkCollateralised({
118
+ const errors = require_onchain_validation_checks_checkCollateralised.checkCollateralised({
118
119
  healthFactor: healthFactorBps,
119
120
  healthFactorThreshold,
120
121
  safePrices
121
- }), `the account would end at a health factor of ${healthFactorBps}, below ${healthFactorThreshold}`);
122
+ });
123
+ if (errors.length === 0) return;
124
+ if (safePrices && atSafePrices) {
125
+ const { atMainPrices, withdrawable } = atSafePrices();
126
+ require_onchain_validation_raise.raise(require_onchain_validation_checks_checkReservePriceLimited.checkReservePriceLimited({
127
+ healthFactor: healthFactorBps,
128
+ atMainPrices,
129
+ healthFactorThreshold,
130
+ withdrawable
131
+ }), `the account covers its debt at ${atMainPrices} on the main feed and only ${healthFactorBps} at the reserve one, below ${healthFactorThreshold}`);
132
+ }
133
+ require_onchain_validation_raise.raise(errors, `the account would end at a health factor of ${healthFactorBps}, below ${healthFactorThreshold}`);
122
134
  }
123
135
  /**
124
136
  * A quota can only be raised as far as the market still has room for: past the
@@ -8,14 +8,16 @@ const require_onchain_validation_raise = require("../../validation/raise.js");
8
8
  require("../../validation/index.js");
9
9
  const require_onchain_accounts_intents_utils_pick_token = require("./utils/pick-token.js");
10
10
  const require_onchain_accounts_intents_guards = require("./guards.js");
11
+ const require_onchain_accounts_intents_utils_credit_account_slice = require("./utils/credit-account-slice.js");
12
+ const require_onchain_accounts_intents_borrow = require("./borrow.js");
11
13
  const require_onchain_accounts_intents_leverage_band = require("./leverage-band.js");
12
- const require_onchain_accounts_intents_math = require("./math.js");
14
+ const require_onchain_accounts_intents_maxBorrow = require("./maxBorrow.js");
13
15
  const require_onchain_accounts_intents_maxWithdrawCollateral = require("./maxWithdrawCollateral.js");
14
- const require_onchain_accounts_intents_utils_credit_account_slice = require("./utils/credit-account-slice.js");
15
16
  const require_onchain_accounts_intents_open_strategy = require("./open-strategy.js");
16
17
  const require_onchain_accounts_intents_plan = require("./plan.js");
17
- const require_onchain_accounts_intents_realize = require("./realize.js");
18
18
  const require_onchain_accounts_intents_view = require("./view.js");
19
+ const require_onchain_accounts_intents_withdraw_limits = require("./withdraw-limits.js");
20
+ const require_onchain_accounts_intents_realize = require("./realize.js");
19
21
  const require_onchain_accounts_intents_tail = require("./tail.js");
20
22
  //#region src/onchain/accounts/intents/index.ts
21
23
  /**
@@ -51,26 +53,29 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
51
53
  }
52
54
  /**
53
55
  * Both ends of what a `WITHDRAW` can take out, in underlying: the largest
54
- * partial withdrawal that keeps leverage and stays inside the facade's
55
- * `debtLimits`, and the net value an exit hands over. They are reported together
56
- * because a withdraw form needs both — the range it may offer, and the one
57
- * amount past it that is allowed — and because the distance between them is
58
- * the account's own, not a constant a caller could assume.
56
+ * partial withdrawal that keeps leverage, and the net value an exit hands
57
+ * over. They are reported together because a withdraw form needs both — the
58
+ * range it may offer, and the one amount past it that is allowed — and
59
+ * because the distance between them is the account's own, not a constant a
60
+ * caller could assume.
59
61
  *
60
- * Takes no target health factor, unlike {@link maxWithdrawCollateral}: a
61
- * proportional withdrawal leaves the factor where it found it, and the
62
- * facade's `minDebt` is what bounds it.
62
+ * Two rules bound the partial end and both are reported: the facade's
63
+ * `debtLimits` as `partial`, and the safe-price collateral check on top of
64
+ * it as `safePartial`. The second is the one to offer — see
65
+ * {@link WithdrawCeilings}.
63
66
  *
64
- * @param props - Account slice and the SDK holding its market
65
- * @returns The two ceilings, see {@link WithdrawCeilings} for the gap between
66
- * them
67
+ * Takes no target health factor, unlike {@link maxWithdrawCollateral}. A
68
+ * proportional withdrawal leaves the factor where it found it, so there is
69
+ * no room to choose: what these answer to is the facade's own threshold,
70
+ * which is also what {@link startIntent} refuses against.
71
+ *
72
+ * @param props - Account slice, the SDK holding its market, and optionally
73
+ * the collateral the withdrawal would be funded from
74
+ * @returns The three limits, see {@link WithdrawCeilings} for the gap
75
+ * between them
67
76
  */
68
77
  maxWithdraw(props) {
69
- const view = require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk);
70
- return {
71
- partial: require_onchain_accounts_intents_math.maxProportionalWithdrawal(view, view.debtLimits),
72
- exit: view.collateral > 0n ? view.collateral : 0n
73
- };
78
+ return require_onchain_accounts_intents_withdraw_limits.withdrawLimits(props);
74
79
  }
75
80
  /**
76
81
  * Debt a `REPAY` would have to cover to settle the account, in underlying
@@ -130,6 +135,34 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
130
135
  });
131
136
  }
132
137
  /**
138
+ * Largest loan a given collateral supports at `targetHF`, in the payout
139
+ * token's units — the ceiling a borrow form should offer.
140
+ *
141
+ * Reads no account, like {@link leverageBand}: the borrow opens one. The
142
+ * collateral is valued the way the transaction will be judged, at safe
143
+ * prices and under the quota the borrow buys, and the answer is then held to
144
+ * what the market will lend.
145
+ *
146
+ * A ceiling, not a verdict: the facade's `minDebt` is a floor and is not
147
+ * applied here, so collateral too small for this market still answers with
148
+ * what it carries and {@link borrowIntent} is the one that refuses the loan.
149
+ *
150
+ * The default is {@link MIN_HF_LIMITED}, the threshold a form holds an
151
+ * account to.
152
+ *
153
+ * @param props - The manager, the SDK holding its market, the collateral put
154
+ * up, the token to be paid in, and optionally the health factor to land at
155
+ * @returns Amount in the payout token's units; `0n` where no loan of this
156
+ * shape can be funded at any size
157
+ */
158
+ maxBorrow(props) {
159
+ const { targetHF = require_onchain_validation_helpers_health_factor_limits.MIN_HF_LIMITED, ...rest } = props;
160
+ return require_onchain_accounts_intents_maxBorrow.maxBorrow({
161
+ ...rest,
162
+ targetHF: targetHF + 2n
163
+ });
164
+ }
165
+ /**
133
166
  * Previews the same operation when its source only redeems through its
134
167
  * issuer: a Securitize dsToken, a Mellow share.
135
168
  *
@@ -180,7 +213,10 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
180
213
  });
181
214
  return {
182
215
  ...result,
183
- state: tail.state,
216
+ state: {
217
+ ...tail.state,
218
+ executionCost: result.state.executionCost
219
+ },
184
220
  delayed
185
221
  };
186
222
  } catch (e) {
@@ -272,6 +308,26 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
272
308
  };
273
309
  }
274
310
  /**
311
+ * Previews opening an account that holds nothing.
312
+ *
313
+ * Nothing is put up, drawn or routed, so there is no state to build and no
314
+ * guard to run beyond the market's own: a paused or expired facade takes no
315
+ * multicall, and an opening is a multicall like any other. Answers the same
316
+ * envelope its two neighbours do so a caller branches on `ok` throughout.
317
+ *
318
+ * @param props - The SDK holding the market, and the manager to open in
319
+ * @returns `{ ok: true }`, or `{ ok: false, error }` when the market takes
320
+ * no transaction right now
321
+ */
322
+ async openEmptyAccountIntent(props) {
323
+ try {
324
+ require_onchain_accounts_intents_guards.assertMarketOperable(props.sdk.marketRegister.findCreditManager(props.creditManager));
325
+ return { ok: true };
326
+ } catch (e) {
327
+ return asSDKError(e);
328
+ }
329
+ }
330
+ /**
275
331
  * Previews opening a brand-new leveraged position.
276
332
  *
277
333
  * Sits apart from {@link startIntent} because there is no account yet: nothing
@@ -293,6 +349,37 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
293
349
  return asSDKError(e);
294
350
  }
295
351
  }
352
+ /**
353
+ * Previews taking a loan against collateral, on an account this same
354
+ * transaction opens.
355
+ *
356
+ * Sits beside {@link openStrategyIntent} rather than under
357
+ * {@link startIntent} for the same reason: there is no account yet, and the
358
+ * output feeds `sdk.accounts.openCA`. What sets it apart from an opening is
359
+ * where the loan goes — out to the wallet rather than into a position — so
360
+ * the debt is named outright instead of following from a leverage, and the
361
+ * collateral is the only thing the account is left holding.
362
+ *
363
+ * `creditAccount` draws the loan on one the wallet already holds instead of
364
+ * opening another, as an opening takes one.
365
+ *
366
+ * @param props - Credit manager, the collateral the wallet puts up and the
367
+ * payout it asks for
368
+ * @returns Debt, the payout's two branches and the projection the account
369
+ * lands in, or `{ ok: false, error }` when the loan is not viable — a debt
370
+ * outside the facade's limits, collateral that cannot carry it, a payout the
371
+ * router has no path to
372
+ */
373
+ async borrowIntent(props) {
374
+ try {
375
+ return {
376
+ ok: true,
377
+ state: await require_onchain_accounts_intents_borrow.buildBorrowState(props)
378
+ };
379
+ } catch (e) {
380
+ return asSDKError(e);
381
+ }
382
+ }
296
383
  /** Plan → realise → wrap. Unviable requests become `{ ok: false }`. */
297
384
  async #preview(props, plan) {
298
385
  try {
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
3
3
  const require_onchain_constants_math = require("../../constants/math.js");
4
+ const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
4
5
  //#region src/onchain/accounts/intents/leverage-band.ts
5
6
  /**
6
7
  * The leverages this market will actually fund for a position of this size.
@@ -30,7 +31,7 @@ const require_onchain_constants_math = require("../../constants/math.js");
30
31
  * ```
31
32
  **/
32
33
  function calcLeverageBand({ sdk, creditManager, collateral, targetHF }) {
33
- const found = resolve(sdk, creditManager);
34
+ const found = require_onchain_accounts_intents_utils_common.resolveCreditManager(sdk, creditManager);
34
35
  if (!found) return;
35
36
  const { suite, market } = found;
36
37
  const target = suite.strategyTargetCollateral;
@@ -55,16 +56,5 @@ function calcLeverageBand({ sdk, creditManager, collateral, targetHF }) {
55
56
  max
56
57
  };
57
58
  }
58
- /** The manager's suite and market, or nothing while they cannot be resolved. */
59
- function resolve(sdk, creditManager) {
60
- try {
61
- return {
62
- suite: sdk.marketRegister.findCreditManager(creditManager),
63
- market: sdk.marketRegister.findByCreditManager(creditManager)
64
- };
65
- } catch {
66
- return;
67
- }
68
- }
69
59
  //#endregion
70
60
  exports.calcLeverageBand = calcLeverageBand;
@@ -0,0 +1,89 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
3
+ require("../../constants/math.js");
4
+ require("../../utils/index.js");
5
+ const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
6
+ const require_onchain_accounts_intents_utils_credit_account_slice = require("./utils/credit-account-slice.js");
7
+ require("./utils/index.js");
8
+ const require_onchain_accounts_intents_borrow = require("./borrow.js");
9
+ const require_onchain_accounts_intents_collateral_valuation = require("./collateral-valuation.js");
10
+ //#region src/onchain/accounts/intents/maxBorrow.ts
11
+ /**
12
+ * Largest loan this collateral supports at `targetHF` — the ceiling a borrow
13
+ * form should offer, in the payout token's units.
14
+ *
15
+ * The inverse of a borrow rather than a search for one: the loan leaves the
16
+ * account entirely, so the collateral is the whole of what backs the debt, and
17
+ * the health factor is one division away from the amount. Solving it the other
18
+ * way round costs a division too, and no iteration.
19
+ *
20
+ * Collateral is valued the way the transaction will be judged — at safe
21
+ * prices, under its liquidation threshold, capped by the quota the borrow
22
+ * buys for it, all of which is {@link collateralValuation}'s business. The ceiling
23
+ * is then held to what the market will actually lend: the pool's free
24
+ * liquidity, the manager's own allowance and the facade's `maxDebt`, whichever
25
+ * binds first.
26
+ *
27
+ * The facade's `minDebt` is deliberately not applied. It is a floor, and a
28
+ * ceiling answered as `0n` because the collateral is too small for this market
29
+ * would tell a form nothing about what it is holding — the number a user needs
30
+ * to see is the one they are short of. Collateral that carries something
31
+ * therefore answers with it, whether or not the market would lend that little;
32
+ * a loan under the floor is refused by `borrow` itself, with `debtOutOfRange`
33
+ * naming both ends.
34
+ *
35
+ * Nothing is fetched or simulated — the account does not exist yet and every
36
+ * input is loaded market state, so a form can call this on each keystroke.
37
+ *
38
+ * @param props - {@link MaxBorrowProps}
39
+ * @returns Amount in the payout token's units; `0n` where no loan of this
40
+ * shape exists at any size — a collateral that backs nothing at safe prices, a
41
+ * market with nothing left to lend, and a manager the SDK does not hold yet
42
+ **/
43
+ function maxBorrow(props) {
44
+ const { sdk, creditManager, collateralAmount, targetHF, quotaReserve } = props;
45
+ const found = require_onchain_accounts_intents_utils_common.resolveCreditManager(sdk, creditManager);
46
+ if (!found) return 0n;
47
+ const { suite, market } = found;
48
+ const { priceOracle } = market;
49
+ const underlying = market.pool.underlying.toLowerCase();
50
+ const collateralToken = props.collateralToken.toLowerCase();
51
+ const borrowToken = props.borrowToken.toLowerCase();
52
+ const rwaAsset = sdk.tokensMeta.rwaUnderlyings.get(underlying)?.asset?.toLowerCase();
53
+ if (require_onchain_accounts_intents_utils_common.eq(collateralToken, borrowToken)) return 0n;
54
+ if (rwaAsset && require_onchain_accounts_intents_utils_common.eq(borrowToken, underlying)) return 0n;
55
+ if (collateralAmount <= 10n || targetHF <= 0n) return 0n;
56
+ const assets = [{
57
+ token: collateralToken,
58
+ balance: collateralAmount
59
+ }];
60
+ const quotas = require_onchain_accounts_intents_borrow.borrowCollateralQuota({
61
+ sdk,
62
+ creditManager,
63
+ assets,
64
+ quotaReserve
65
+ });
66
+ const holding = {
67
+ token: collateralToken,
68
+ balance: collateralAmount,
69
+ quota: quotas.find((q) => require_onchain_accounts_intents_utils_common.eq(q.token, collateralToken))?.balance ?? 0n,
70
+ mask: 0n,
71
+ success: true
72
+ };
73
+ const valuation = require_onchain_accounts_intents_collateral_valuation.collateralValuation({
74
+ ...require_onchain_accounts_intents_utils_credit_account_slice.unopenedAccountSlice({
75
+ creditManager,
76
+ creditFacade: suite.creditFacade.address,
77
+ underlying
78
+ }),
79
+ tokens: [holding]
80
+ }, sdk);
81
+ const weighted = valuation.checkedUsd(holding) * valuation.lt(collateralToken);
82
+ const backed = quotas.some((q) => require_onchain_accounts_intents_utils_common.eq(q.token, collateralToken)) ? require_onchain_utils_bigint_math.BigIntMath.min(valuation.quotaValue(holding), weighted) : weighted;
83
+ if (backed <= 0n) return 0n;
84
+ const ceiling = require_onchain_utils_bigint_math.BigIntMath.min(priceOracle.safeConvertFromUSD(underlying, backed / targetHF).value, suite.maxBorrowAmount().amount.value);
85
+ const unwrapsPayout = !!rwaAsset && require_onchain_accounts_intents_utils_common.eq(borrowToken, rwaAsset);
86
+ return require_onchain_accounts_intents_utils_common.eq(borrowToken, underlying) ? ceiling : unwrapsPayout ? require_onchain_accounts_intents_utils_common.toTargetDecimals(ceiling, underlying, borrowToken, sdk) : priceOracle.safeConvert(underlying, borrowToken, ceiling).value;
87
+ }
88
+ //#endregion
89
+ exports.maxBorrow = maxBorrow;
@@ -1,22 +1,18 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
3
- const require_onchain_constants_math = require("../../constants/math.js");
3
+ require("../../constants/math.js");
4
4
  require("../../utils/index.js");
5
5
  const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
6
+ const require_onchain_accounts_intents_collateral_valuation = require("./collateral-valuation.js");
6
7
  //#region src/onchain/accounts/intents/maxWithdrawCollateral.ts
7
8
  /**
8
9
  * Largest amount of one collateral the account can withdraw while its health
9
10
  * factor stays at or above `targetHF`.
10
11
  *
11
12
  * This is the collateral check solved for one balance, and it counts what that
12
- * check counts: a holding backed by a quota contributes the lesser of the
13
- * quota and its threshold-weighted value, an unquoted one — the underlying —
14
- * its weighted value alone, and dust or a disabled balance nothing at all.
15
- * Collateral is valued at the protocol safe price (`min` of the two feeds,
16
- * 0 when there is no reserve), the way the facade values a call that hands
17
- * funds over; the underlying is exempt and is valued at the main feed, as
18
- * `CreditManagerV3._safeConvertToUSD` does. The debt is valued at the main
19
- * feed, as the check does. Zero debt frees the whole balance.
13
+ * check counts — see {@link collateralValuation} for it, safe prices included.
14
+ * The debt is valued at the main feed, as the check does. Zero debt frees the
15
+ * whole balance.
20
16
  *
21
17
  * Rounding always favours the account, so the answer clears the check rather
22
18
  * than landing a wei short of it.
@@ -27,49 +23,27 @@ const require_onchain_accounts_intents_utils_common = require("./utils/common.js
27
23
  **/
28
24
  function maxWithdrawCollateral(props) {
29
25
  const { creditAccount, sdk, token, targetHF } = props;
30
- const { market, creditManager } = sdk.marketRegister.findCreditManager(creditAccount.creditManager);
31
- const { priceOracle } = market;
32
- const { pqk } = market.pool;
33
- const underlying = market.pool.underlying;
34
26
  const target = creditAccount.tokens.find((t) => require_onchain_accounts_intents_utils_common.eq(t.token, token));
35
27
  if (!target || target.balance <= 10n) return 0n;
36
28
  if (creditAccount.totalDebt === 0n) return target.balance;
37
- const masked = creditAccount.enabledTokensMask !== 0n;
38
- const counts = (t) => t.balance > 10n && (!masked || (t.mask & creditAccount.enabledTokensMask) !== 0n);
39
- /** What a holding backs, in the check's units: USD × PERCENTAGE_FACTOR. */
40
- const weigh = (t) => {
41
- const lt = BigInt(creditManager.liquidationThresholds.get(t.token) ?? 0);
42
- const weighted = (require_onchain_accounts_intents_utils_common.eq(t.token, underlying) ? usd(priceOracle, t.token, t.balance) ?? 0n : priceOracle.safeConvertMinUSD(t.token, t.balance).value) * lt;
43
- if (t.quota === 0n) return weighted;
44
- return require_onchain_utils_bigint_math.BigIntMath.min(quotaUsd(t) * require_onchain_constants_math.PERCENTAGE_FACTOR, weighted);
45
- };
46
- /** A quota is underlying-denominated, and a closed market backs nothing. */
47
- const quotaUsd = (t) => pqk.hasActiveQuota(t.token) ? usd(priceOracle, underlying, t.quota) ?? 0n : 0n;
48
- let otherMoney = 0n;
29
+ const valuation = require_onchain_accounts_intents_collateral_valuation.collateralValuation(creditAccount, sdk);
30
+ let otherValue = 0n;
49
31
  for (const t of creditAccount.tokens) {
50
- if (require_onchain_accounts_intents_utils_common.eq(t.token, token) || !counts(t)) continue;
51
- otherMoney += weigh(t);
32
+ if (require_onchain_accounts_intents_utils_common.eq(t.token, token) || !valuation.counts(t)) continue;
33
+ otherValue += valuation.weigh(t);
52
34
  }
53
- const borrowed = usd(priceOracle, underlying, creditAccount.totalDebt);
35
+ const borrowed = valuation.mainUsd(valuation.underlying, creditAccount.totalDebt);
54
36
  if (borrowed === void 0 || borrowed <= 0n) return 0n;
55
37
  const required = borrowed * targetHF;
56
- if (required <= otherMoney) return target.balance;
57
- const shortfall = required - otherMoney;
58
- if (target.quota > 0n && quotaUsd(target) * 10000n < shortfall) return 0n;
59
- const targetLt = BigInt(creditManager.liquidationThresholds.get(target.token) ?? 0);
60
- const targetUsd = require_onchain_accounts_intents_utils_common.eq(target.token, underlying) ? usd(priceOracle, target.token, target.balance) ?? 0n : priceOracle.safeConvertMinUSD(target.token, target.balance).value;
38
+ if (required <= otherValue) return target.balance;
39
+ const shortfall = required - otherValue;
40
+ if (target.quota > 0n && valuation.quotaValue(target) < shortfall) return 0n;
41
+ const targetLt = valuation.lt(target.token);
42
+ const targetUsd = valuation.checkedUsd(target);
61
43
  if (targetLt === 0n || targetUsd === 0n) return 0n;
62
44
  const keptUsd = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(shortfall, targetLt);
63
45
  const kept = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(target.balance * keptUsd, targetUsd);
64
46
  return kept >= target.balance ? 0n : target.balance - kept;
65
47
  }
66
- /** USD value at the main feed, or `undefined` when the token has no price. */
67
- function usd(oracle, token, amount) {
68
- try {
69
- return oracle.convertToUSD(token, amount);
70
- } catch {
71
- return;
72
- }
73
- }
74
48
  //#endregion
75
49
  exports.maxWithdrawCollateral = maxWithdrawCollateral;
@@ -4,17 +4,13 @@ require("../../../model/index.js");
4
4
  const require_onchain_validation_raise = require("../../validation/raise.js");
5
5
  const require_onchain_accounts_intents_guards = require("./guards.js");
6
6
  const require_onchain_accounts_intents_math = require("./math.js");
7
+ const require_onchain_accounts_intents_utils_credit_account_slice = require("./utils/credit-account-slice.js");
7
8
  const require_onchain_accounts_intents_utils_price_impact = require("./utils/price-impact.js");
8
9
  const require_onchain_accounts_intents_utils_quotas_for_update = require("./utils/quotas-for-update.js");
9
10
  const require_onchain_accounts_intents_utils_router_path = require("./utils/router-path.js");
10
11
  require("./utils/index.js");
11
12
  //#region src/onchain/accounts/intents/open-strategy.ts
12
13
  /**
13
- * Stand-in account address, used when the opening creates its own account:
14
- * nothing exists on chain until the tx lands.
15
- */
16
- const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
17
- /**
18
14
  * Builds the state opening a leveraged position out of wallet collateral would
19
15
  * reach.
20
16
  *
@@ -27,7 +23,6 @@ const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
27
23
  * `sdk.accounts.openCA`.
28
24
  */
29
25
  async function buildOpenStrategyState(props) {
30
- if (props.empty) return emptyOpenState(props);
31
26
  const { sdk, creditManager, collateral, targetToken, leverage, slippage = 0, quotaReserve, leftoverBalances = [], creditAccount: existing } = props;
32
27
  require_onchain_accounts_intents_math.assertLeverageAtLeastOne(leverage);
33
28
  const suite = sdk.marketRegister.findCreditManager(creditManager);
@@ -38,16 +33,11 @@ async function buildOpenStrategyState(props) {
38
33
  const margin = collateral.reduce((acc, a) => acc + convert(a.token, underlying, a.balance), 0n);
39
34
  if (margin <= 0n) throw new require_onchain_validation_raise.IntentPreviewError(require_model_errors_operation_errors.insufficientBalance(), "openStrategy: collateral is worth nothing in underlying");
40
35
  const debt = require_onchain_accounts_intents_math.debtForLeverage(margin, leverage);
41
- const account = existing ?? {
42
- creditAccount: NO_ACCOUNT,
43
- creditManager: creditManager.toLowerCase(),
44
- creditFacade: suite.creditFacade.address.toLowerCase(),
45
- underlying,
46
- enabledTokensMask: 0n,
47
- totalDebtUSD: 0n,
48
- totalDebt: 0n,
49
- tokens: []
50
- };
36
+ const account = existing ?? require_onchain_accounts_intents_utils_credit_account_slice.unopenedAccountSlice({
37
+ creditManager,
38
+ creditFacade: suite.creditFacade.address,
39
+ underlying
40
+ });
51
41
  require_onchain_accounts_intents_math.assertDebtLimits(sdk, debt, suite.creditFacade, underlying);
52
42
  require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, debt);
53
43
  const paths = require_onchain_accounts_intents_utils_router_path.createRouterPaths({
@@ -97,7 +87,8 @@ async function buildOpenStrategyState(props) {
97
87
  const priceImpact = await require_onchain_accounts_intents_utils_price_impact.collectPriceImpact(leg.probe ? [leg.probe] : [], {
98
88
  totalValue: margin + debt,
99
89
  netValue: margin,
100
- toUnderlying: (from, amount) => convert(from, underlying, amount)
90
+ toUnderlying: (from, amount) => convert(from, underlying, amount),
91
+ toUnderlyingAmount: market.toUnderlyingAmount
101
92
  });
102
93
  return {
103
94
  ...projection,
@@ -111,35 +102,6 @@ async function buildOpenStrategyState(props) {
111
102
  creditAccount: existing?.creditAccount
112
103
  };
113
104
  }
114
- /**
115
- * The opening that holds an account and nothing else.
116
- *
117
- * Taken before the walk rather than threaded through it: the router has no
118
- * guard for an empty basket and would still make its `eth_call`, and every
119
- * assertion below reads amounts that are not there.
120
- */
121
- async function emptyOpenState(props) {
122
- const { sdk, creditManager } = props;
123
- require_onchain_accounts_intents_guards.assertMarketOperable(sdk.marketRegister.findCreditManager(creditManager));
124
- const snapshot = {
125
- creditManager,
126
- assets: [],
127
- quotas: [],
128
- totalDebt: 0n,
129
- totalValue: 0n
130
- };
131
- const { assets: _assets, quotas: _quotas, ...projection } = sdk.positions.projection(snapshot, { availableLiquidityChange: 0n });
132
- return {
133
- ...projection,
134
- currentPrice: sdk.positions.currentPrice(snapshot),
135
- priceImpact: void 0,
136
- averageAssets: [],
137
- minAssets: [],
138
- averageQuota: [],
139
- minQuota: [],
140
- calls: []
141
- };
142
- }
143
105
  /** Collateral plus the borrowed underlying, folded into one balance per token. */
144
106
  function mergeExpectedBalances(collateral, underlying, debt) {
145
107
  const merged = /* @__PURE__ */ new Map();
@@ -55,7 +55,6 @@ function buildSwapOperation(input) {
55
55
  * underlying comes out.
56
56
  */
57
57
  function buildCloseSwapOperation(input) {
58
- if (input.calls.length === 0) throw new Error("swap: missing router calls");
59
58
  return {
60
59
  type: "swap",
61
60
  from: input.from,