@gearbox-protocol/sdk 15.1.0-next.1 → 15.1.0-next.11

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 (436) hide show
  1. package/dist/cjs/abi/iExpirable.js +15 -0
  2. package/dist/cjs/common-utils/utils/apy/get-single-quota-borrow-rate.js +2 -0
  3. package/dist/cjs/common-utils/utils/creditAccount/calc-health-factor.js +24 -22
  4. package/dist/cjs/common-utils/utils/creditAccount/calc-quota-borrow-rate.js +2 -0
  5. package/dist/cjs/common-utils/utils/creditAccount/get-time-to-liquidation.js +8 -5
  6. package/dist/cjs/common-utils/utils/creditAccount/liquidation-price.js +21 -11
  7. package/dist/cjs/dev/AccountOpener.js +0 -1
  8. package/dist/cjs/dev/compareOpportunities.js +137 -0
  9. package/dist/cjs/dev/comparePositions.js +183 -0
  10. package/dist/cjs/dev/fieldDiff.js +241 -0
  11. package/dist/cjs/model/charts.js +147 -0
  12. package/dist/cjs/model/charts.schema.js +240 -0
  13. package/dist/cjs/model/index.js +35 -22
  14. package/dist/cjs/model/liquidations.schema.js +1 -1
  15. package/dist/cjs/model/notices.schema.js +25 -0
  16. package/dist/cjs/model/opportunities.schema.js +1 -1
  17. package/dist/cjs/model/positions.schema.js +48 -2
  18. package/dist/cjs/new-sdk/AbstractNamespace.js +34 -4
  19. package/dist/cjs/new-sdk/GearboxSDK.js +72 -3
  20. package/dist/cjs/new-sdk/execute/ExecuteApi.js +95 -0
  21. package/dist/cjs/new-sdk/execute/index.js +3 -0
  22. package/dist/cjs/new-sdk/index.js +7 -0
  23. package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +29 -6
  24. package/dist/cjs/new-sdk/positions/PositionsNamespace.js +3 -6
  25. package/dist/cjs/new-sdk/simulate/SimulateApi.js +361 -0
  26. package/dist/cjs/new-sdk/simulate/index.js +4 -0
  27. package/dist/cjs/new-sdk/utils/index.js +0 -1
  28. package/dist/cjs/offchain/AbstractOffchainNamespace.js +10 -7
  29. package/dist/cjs/offchain/GearboxAPI.js +7 -0
  30. package/dist/cjs/offchain/index.js +3 -0
  31. package/dist/cjs/offchain/notices/OffchainNotices.js +27 -0
  32. package/dist/cjs/offchain/notices/index.js +3 -0
  33. package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +4 -8
  34. package/dist/cjs/offchain/positions/OffchainPositions.js +12 -12
  35. package/dist/cjs/preview/preview/CreditAccountState.js +14 -1
  36. package/dist/cjs/preview/preview/buildDelayedPreview.js +16 -6
  37. package/dist/cjs/preview/preview/previewAdjustCreditAccount.js +9 -2
  38. package/dist/cjs/preview/preview/previewOpenCreditAccount.js +11 -3
  39. package/dist/cjs/preview/preview/previewOperation.js +1 -1
  40. package/dist/cjs/sdk/MultichainSDK.js +3 -4
  41. package/dist/cjs/sdk/OnchainSDK.js +42 -13
  42. package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +2 -8
  43. package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +2 -90
  44. package/dist/cjs/sdk/accounts/index.js +5 -2
  45. package/dist/cjs/sdk/accounts/intents/guards.js +114 -0
  46. package/dist/cjs/sdk/accounts/intents/index.js +216 -358
  47. package/dist/cjs/sdk/accounts/intents/math.js +56 -0
  48. package/dist/cjs/sdk/accounts/intents/open-strategy.js +133 -0
  49. package/dist/cjs/sdk/accounts/intents/operations.js +165 -0
  50. package/dist/cjs/sdk/accounts/intents/plan.js +373 -0
  51. package/dist/cjs/sdk/accounts/intents/realize.js +301 -0
  52. package/dist/cjs/sdk/accounts/intents/testing/delayed.js +50 -0
  53. package/dist/cjs/sdk/accounts/intents/testing/market.js +166 -0
  54. package/dist/cjs/sdk/accounts/intents/testing/sdk-mock.js +194 -35
  55. package/dist/cjs/sdk/accounts/intents/tests/add-collateral.fixtures.js +123 -0
  56. package/dist/cjs/sdk/accounts/intents/tests/adjust-leverage.fixtures.js +254 -0
  57. package/dist/cjs/sdk/accounts/intents/tests/deposit.fixtures.js +324 -0
  58. package/dist/cjs/sdk/accounts/intents/tests/finish-decrease-leverage.fixtures.js +242 -0
  59. package/dist/cjs/sdk/accounts/intents/tests/finish-withdraw.fixtures.js +383 -0
  60. package/dist/cjs/sdk/accounts/intents/tests/open-strategy.fixtures.js +133 -0
  61. package/dist/cjs/sdk/accounts/intents/tests/withdraw-asset.fixtures.js +131 -0
  62. package/dist/cjs/sdk/accounts/intents/tests/withdraw.fixtures.js +426 -0
  63. package/dist/cjs/sdk/accounts/intents/types.js +16 -0
  64. package/dist/cjs/sdk/accounts/intents/utils/adjust-state-to-snapshot.js +18 -0
  65. package/dist/cjs/sdk/accounts/intents/utils/credit-account-slice.js +45 -0
  66. package/dist/cjs/sdk/accounts/intents/utils/index.js +17 -8
  67. package/dist/cjs/sdk/accounts/intents/utils/ledger.js +94 -0
  68. package/dist/cjs/sdk/accounts/intents/utils/pick-token.js +72 -0
  69. package/dist/cjs/sdk/accounts/intents/utils/quotas-for-update.js +27 -0
  70. package/dist/cjs/sdk/accounts/intents/utils/router-path.js +86 -0
  71. package/dist/cjs/sdk/accounts/intents/view.js +38 -0
  72. package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +0 -3
  73. package/dist/cjs/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +29 -18
  74. package/dist/cjs/sdk/base/BaseContract.js +1 -1
  75. package/dist/cjs/sdk/base/TokensMeta.js +78 -27
  76. package/dist/cjs/sdk/index.js +30 -7
  77. package/dist/cjs/sdk/market/MarketRegister.js +16 -7
  78. package/dist/cjs/sdk/market/ZapperRegister.js +56 -25
  79. package/dist/cjs/sdk/market/credit/CreditManagerV310Contract.js +1 -12
  80. package/dist/cjs/sdk/market/credit/CreditSuite.js +28 -9
  81. package/dist/cjs/sdk/market/credit/index.js +3 -0
  82. package/dist/cjs/sdk/market/credit/isStrategyCollateral.js +50 -0
  83. package/dist/cjs/sdk/market/index.js +17 -0
  84. package/dist/cjs/sdk/market/math.js +65 -45
  85. package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +10 -3
  86. package/dist/cjs/sdk/market/pool/PoolV310Contract.js +15 -1
  87. package/dist/cjs/sdk/market/rwa/RWARegistry.js +22 -0
  88. package/dist/cjs/sdk/opportunities/index.js +0 -13
  89. package/dist/cjs/sdk/options.js +1 -5
  90. package/dist/cjs/sdk/pools/PoolService.js +133 -4
  91. package/dist/cjs/sdk/pools/index.js +2 -0
  92. package/dist/cjs/sdk/positions/PositionsService.js +218 -1
  93. package/dist/cjs/sdk/positions/calcBorrowRate.js +43 -0
  94. package/dist/cjs/sdk/positions/calcHealthFactor.js +52 -0
  95. package/dist/cjs/sdk/positions/calcLiquidationPrice.js +23 -0
  96. package/dist/cjs/sdk/positions/calcLiquidationPriceForTarget.js +30 -0
  97. package/dist/cjs/sdk/positions/calcTimeToLiquidationMs.js +19 -0
  98. package/dist/cjs/sdk/positions/index.js +12 -1
  99. package/dist/cjs/sdk/positions/types.js +31 -0
  100. package/dist/cjs/sdk/utils/abi-decode.js +2 -2
  101. package/dist/cjs/sdk/utils/viem/executeMulticallBatches.js +32 -0
  102. package/dist/cjs/sdk/utils/viem/index.js +2 -0
  103. package/dist/esm/abi/iExpirable.js +14 -0
  104. package/dist/esm/common-utils/utils/apy/get-single-quota-borrow-rate.js +2 -0
  105. package/dist/esm/common-utils/utils/creditAccount/calc-health-factor.js +24 -22
  106. package/dist/esm/common-utils/utils/creditAccount/calc-quota-borrow-rate.js +2 -0
  107. package/dist/esm/common-utils/utils/creditAccount/get-time-to-liquidation.js +8 -5
  108. package/dist/esm/common-utils/utils/creditAccount/liquidation-price.js +21 -11
  109. package/dist/esm/dev/AccountOpener.js +0 -1
  110. package/dist/esm/dev/compareOpportunities.js +135 -0
  111. package/dist/esm/dev/comparePositions.js +181 -0
  112. package/dist/esm/dev/fieldDiff.js +224 -0
  113. package/dist/esm/model/charts.js +140 -0
  114. package/dist/esm/model/charts.schema.js +226 -0
  115. package/dist/esm/model/index.js +9 -7
  116. package/dist/esm/model/liquidations.schema.js +1 -1
  117. package/dist/esm/model/notices.schema.js +23 -0
  118. package/dist/esm/model/opportunities.schema.js +1 -1
  119. package/dist/esm/model/positions.schema.js +46 -4
  120. package/dist/esm/new-sdk/AbstractNamespace.js +34 -4
  121. package/dist/esm/new-sdk/GearboxSDK.js +72 -4
  122. package/dist/esm/new-sdk/execute/ExecuteApi.js +94 -0
  123. package/dist/esm/new-sdk/execute/index.js +2 -0
  124. package/dist/esm/new-sdk/index.js +6 -2
  125. package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +29 -6
  126. package/dist/esm/new-sdk/positions/PositionsNamespace.js +3 -6
  127. package/dist/esm/new-sdk/simulate/SimulateApi.js +360 -0
  128. package/dist/esm/new-sdk/simulate/index.js +3 -0
  129. package/dist/esm/new-sdk/utils/index.js +0 -1
  130. package/dist/esm/offchain/AbstractOffchainNamespace.js +10 -7
  131. package/dist/esm/offchain/GearboxAPI.js +7 -0
  132. package/dist/esm/offchain/index.js +3 -1
  133. package/dist/esm/offchain/notices/OffchainNotices.js +26 -0
  134. package/dist/esm/offchain/notices/index.js +2 -0
  135. package/dist/esm/offchain/opportunities/OffchainOpportunities.js +4 -8
  136. package/dist/esm/offchain/positions/OffchainPositions.js +12 -12
  137. package/dist/esm/preview/preview/CreditAccountState.js +14 -1
  138. package/dist/esm/preview/preview/buildDelayedPreview.js +16 -6
  139. package/dist/esm/preview/preview/previewAdjustCreditAccount.js +9 -2
  140. package/dist/esm/preview/preview/previewOpenCreditAccount.js +11 -3
  141. package/dist/esm/preview/preview/previewOperation.js +1 -1
  142. package/dist/esm/sdk/MultichainSDK.js +3 -4
  143. package/dist/esm/sdk/OnchainSDK.js +42 -13
  144. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -8
  145. package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +2 -90
  146. package/dist/esm/sdk/accounts/index.js +3 -2
  147. package/dist/esm/sdk/accounts/intents/guards.js +108 -0
  148. package/dist/esm/sdk/accounts/intents/index.js +214 -358
  149. package/dist/esm/sdk/accounts/intents/math.js +51 -0
  150. package/dist/esm/sdk/accounts/intents/open-strategy.js +132 -0
  151. package/dist/esm/sdk/accounts/intents/operations.js +153 -0
  152. package/dist/esm/sdk/accounts/intents/plan.js +361 -0
  153. package/dist/esm/sdk/accounts/intents/realize.js +300 -0
  154. package/dist/esm/sdk/accounts/intents/testing/delayed.js +38 -0
  155. package/dist/esm/sdk/accounts/intents/testing/market.js +148 -0
  156. package/dist/esm/sdk/accounts/intents/testing/sdk-mock.js +194 -35
  157. package/dist/esm/sdk/accounts/intents/tests/add-collateral.fixtures.js +110 -0
  158. package/dist/esm/sdk/accounts/intents/tests/adjust-leverage.fixtures.js +234 -0
  159. package/dist/esm/sdk/accounts/intents/tests/deposit.fixtures.js +302 -0
  160. package/dist/esm/sdk/accounts/intents/tests/finish-decrease-leverage.fixtures.js +220 -0
  161. package/dist/esm/sdk/accounts/intents/tests/finish-withdraw.fixtures.js +355 -0
  162. package/dist/esm/sdk/accounts/intents/tests/open-strategy.fixtures.js +119 -0
  163. package/dist/esm/sdk/accounts/intents/tests/withdraw-asset.fixtures.js +117 -0
  164. package/dist/esm/sdk/accounts/intents/tests/withdraw.fixtures.js +402 -0
  165. package/dist/esm/sdk/accounts/intents/types.js +16 -1
  166. package/dist/esm/sdk/accounts/intents/utils/adjust-state-to-snapshot.js +17 -0
  167. package/dist/esm/sdk/accounts/intents/utils/credit-account-slice.js +43 -0
  168. package/dist/esm/sdk/accounts/intents/utils/index.js +9 -7
  169. package/dist/esm/sdk/accounts/intents/utils/ledger.js +93 -0
  170. package/dist/esm/sdk/accounts/intents/utils/pick-token.js +68 -0
  171. package/dist/esm/sdk/accounts/intents/utils/quotas-for-update.js +27 -1
  172. package/dist/esm/sdk/accounts/intents/utils/router-path.js +85 -0
  173. package/dist/esm/sdk/accounts/intents/view.js +37 -0
  174. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +0 -3
  175. package/dist/esm/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +29 -18
  176. package/dist/esm/sdk/base/BaseContract.js +1 -1
  177. package/dist/esm/sdk/base/TokensMeta.js +78 -27
  178. package/dist/esm/sdk/index.js +13 -4
  179. package/dist/esm/sdk/market/MarketRegister.js +16 -7
  180. package/dist/esm/sdk/market/ZapperRegister.js +56 -25
  181. package/dist/esm/sdk/market/credit/CreditManagerV310Contract.js +2 -13
  182. package/dist/esm/sdk/market/credit/CreditSuite.js +29 -10
  183. package/dist/esm/sdk/market/credit/index.js +2 -1
  184. package/dist/esm/sdk/market/credit/isStrategyCollateral.js +48 -0
  185. package/dist/esm/sdk/market/index.js +3 -1
  186. package/dist/esm/sdk/market/math.js +60 -41
  187. package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +10 -3
  188. package/dist/esm/sdk/market/pool/PoolV310Contract.js +16 -2
  189. package/dist/esm/sdk/market/rwa/RWARegistry.js +22 -0
  190. package/dist/esm/sdk/opportunities/index.js +1 -2
  191. package/dist/esm/sdk/options.js +1 -5
  192. package/dist/esm/sdk/pools/PoolService.js +133 -6
  193. package/dist/esm/sdk/pools/index.js +2 -2
  194. package/dist/esm/sdk/positions/PositionsService.js +218 -1
  195. package/dist/esm/sdk/positions/calcBorrowRate.js +42 -0
  196. package/dist/esm/sdk/positions/calcHealthFactor.js +51 -0
  197. package/dist/esm/sdk/positions/calcLiquidationPrice.js +22 -0
  198. package/dist/esm/sdk/positions/calcLiquidationPriceForTarget.js +29 -0
  199. package/dist/esm/sdk/positions/calcTimeToLiquidationMs.js +18 -0
  200. package/dist/esm/sdk/positions/index.js +7 -2
  201. package/dist/esm/sdk/positions/types.js +31 -1
  202. package/dist/esm/sdk/utils/abi-decode.js +2 -2
  203. package/dist/esm/sdk/utils/viem/executeMulticallBatches.js +31 -0
  204. package/dist/esm/sdk/utils/viem/index.js +2 -1
  205. package/dist/types/abi/iExpirable.d.ts +14 -0
  206. package/dist/types/common-utils/utils/apy/get-single-quota-borrow-rate.d.ts +2 -0
  207. package/dist/types/common-utils/utils/creditAccount/calc-health-factor.d.ts +3 -0
  208. package/dist/types/common-utils/utils/creditAccount/calc-quota-borrow-rate.d.ts +2 -0
  209. package/dist/types/common-utils/utils/creditAccount/get-time-to-liquidation.d.ts +6 -1
  210. package/dist/types/common-utils/utils/creditAccount/liquidation-price.d.ts +3 -0
  211. package/dist/types/dev/compareOpportunities.d.ts +105 -0
  212. package/dist/types/dev/comparePositions.d.ts +149 -0
  213. package/dist/types/dev/fieldDiff.d.ts +167 -0
  214. package/dist/types/model/charts.d.ts +349 -0
  215. package/dist/types/model/charts.schema.d.ts +364 -0
  216. package/dist/types/model/index.d.ts +8 -6
  217. package/dist/types/model/notices.d.ts +29 -0
  218. package/dist/types/model/notices.schema.d.ts +23 -0
  219. package/dist/types/model/opportunities.d.ts +9 -9
  220. package/dist/types/model/positions.d.ts +133 -7
  221. package/dist/types/model/positions.schema.d.ts +128 -1
  222. package/dist/types/new-sdk/AbstractNamespace.d.ts +15 -2
  223. package/dist/types/new-sdk/GearboxSDK.d.ts +14 -2
  224. package/dist/types/new-sdk/execute/ExecuteApi.d.ts +108 -0
  225. package/dist/types/new-sdk/execute/index.d.ts +2 -0
  226. package/dist/types/new-sdk/index.d.ts +8 -4
  227. package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +19 -7
  228. package/dist/types/new-sdk/opportunities/types.d.ts +30 -9
  229. package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +4 -5
  230. package/dist/types/new-sdk/positions/types.d.ts +9 -9
  231. package/dist/types/new-sdk/simulate/SimulateApi.d.ts +90 -0
  232. package/dist/types/new-sdk/simulate/index.d.ts +3 -0
  233. package/dist/types/new-sdk/simulate/types.d.ts +508 -0
  234. package/dist/types/new-sdk/types.d.ts +35 -1
  235. package/dist/types/new-sdk/utils/index.d.ts +1 -2
  236. package/dist/types/offchain/AbstractOffchainNamespace.d.ts +5 -21
  237. package/dist/types/offchain/GearboxAPI.d.ts +6 -0
  238. package/dist/types/offchain/index.d.ts +4 -2
  239. package/dist/types/offchain/notices/OffchainNotices.d.ts +19 -0
  240. package/dist/types/offchain/notices/index.d.ts +2 -0
  241. package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +8 -4
  242. package/dist/types/offchain/positions/OffchainPositions.d.ts +8 -6
  243. package/dist/types/preview/preview/CreditAccountState.d.ts +6 -0
  244. package/dist/types/preview/preview/buildDelayedPreview.d.ts +5 -1
  245. package/dist/types/preview/preview/types.d.ts +4 -2
  246. package/dist/types/sdk/MultichainSDK.d.ts +0 -5
  247. package/dist/types/sdk/OnchainSDK.d.ts +1 -5
  248. package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +1 -7
  249. package/dist/types/sdk/accounts/credit-account-compressor/CreditAccountCompressor.d.ts +3 -12
  250. package/dist/types/sdk/accounts/credit-account-compressor/index.d.ts +2 -2
  251. package/dist/types/sdk/accounts/credit-account-compressor/types.d.ts +1 -19
  252. package/dist/types/sdk/accounts/index.d.ts +7 -5
  253. package/dist/types/sdk/accounts/intents/guards.d.ts +72 -0
  254. package/dist/types/sdk/accounts/intents/index.d.ts +130 -15
  255. package/dist/types/sdk/accounts/intents/math.d.ts +46 -0
  256. package/dist/types/sdk/accounts/intents/open-strategy.d.ts +64 -0
  257. package/dist/types/sdk/accounts/intents/operations.d.ts +215 -0
  258. package/dist/types/sdk/accounts/intents/plan.d.ts +189 -0
  259. package/dist/types/sdk/accounts/intents/realize.d.ts +35 -0
  260. package/dist/types/sdk/accounts/intents/testing/delayed.d.ts +50 -0
  261. package/dist/types/sdk/accounts/intents/testing/expect.d.ts +17 -21
  262. package/dist/types/sdk/accounts/intents/testing/market.d.ts +91 -0
  263. package/dist/types/sdk/accounts/intents/testing/sdk-mock.d.ts +64 -16
  264. package/dist/types/sdk/accounts/intents/tests/add-collateral.fixtures.d.ts +45 -0
  265. package/dist/types/sdk/accounts/intents/tests/adjust-leverage.fixtures.d.ts +56 -0
  266. package/dist/types/sdk/accounts/intents/tests/deposit.fixtures.d.ts +64 -0
  267. package/dist/types/sdk/accounts/intents/tests/finish-decrease-leverage.fixtures.d.ts +107 -0
  268. package/dist/types/sdk/accounts/intents/tests/finish-withdraw.fixtures.d.ts +136 -0
  269. package/dist/types/sdk/accounts/intents/tests/open-strategy.fixtures.d.ts +56 -0
  270. package/dist/types/sdk/accounts/intents/tests/withdraw-asset.fixtures.d.ts +60 -0
  271. package/dist/types/sdk/accounts/intents/tests/withdraw.fixtures.d.ts +79 -0
  272. package/dist/types/sdk/accounts/intents/types.d.ts +369 -43
  273. package/dist/types/sdk/accounts/intents/utils/adjust-state-to-snapshot.d.ts +13 -0
  274. package/dist/types/sdk/accounts/intents/utils/assemble-operation-calls.d.ts +1 -1
  275. package/dist/types/sdk/accounts/intents/utils/credit-account-slice.d.ts +29 -0
  276. package/dist/types/sdk/accounts/intents/utils/index.d.ts +7 -5
  277. package/dist/types/sdk/accounts/intents/utils/ledger.d.ts +41 -0
  278. package/dist/types/sdk/accounts/intents/utils/pick-token.d.ts +55 -0
  279. package/dist/types/sdk/accounts/intents/utils/quotas-for-update.d.ts +15 -4
  280. package/dist/types/sdk/accounts/intents/utils/router-path.d.ts +70 -0
  281. package/dist/types/sdk/accounts/intents/view.d.ts +15 -0
  282. package/dist/types/sdk/accounts/types.d.ts +17 -14
  283. package/dist/types/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.d.ts +6 -0
  284. package/dist/types/sdk/accounts/withdrawal-compressor/types.d.ts +18 -0
  285. package/dist/types/sdk/base/TokensMeta.d.ts +44 -1
  286. package/dist/types/sdk/base/index.d.ts +2 -2
  287. package/dist/types/sdk/base/token-types.d.ts +12 -1
  288. package/dist/types/sdk/index.d.ts +28 -19
  289. package/dist/types/sdk/market/MarketRegister.d.ts +7 -3
  290. package/dist/types/sdk/market/ZapperRegister.d.ts +20 -2
  291. package/dist/types/sdk/market/credit/CreditManagerV310Contract.d.ts +0 -4
  292. package/dist/types/sdk/market/credit/CreditSuite.d.ts +6 -3
  293. package/dist/types/sdk/market/credit/index.d.ts +2 -1
  294. package/dist/types/sdk/market/credit/isStrategyCollateral.d.ts +74 -0
  295. package/dist/types/sdk/market/credit/types.d.ts +2 -9
  296. package/dist/types/sdk/market/index.d.ts +4 -2
  297. package/dist/types/sdk/market/math.d.ts +46 -34
  298. package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +6 -2
  299. package/dist/types/sdk/market/oracle/types.d.ts +10 -2
  300. package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +5 -0
  301. package/dist/types/sdk/market/pool/types.d.ts +5 -0
  302. package/dist/types/sdk/market/rwa/RWARegistry.d.ts +13 -0
  303. package/dist/types/sdk/market/types.d.ts +5 -1
  304. package/dist/types/sdk/opportunities/index.d.ts +1 -2
  305. package/dist/types/sdk/options.d.ts +0 -1
  306. package/dist/types/sdk/pools/PoolService.d.ts +32 -2
  307. package/dist/types/sdk/pools/index.d.ts +3 -3
  308. package/dist/types/sdk/pools/types.d.ts +109 -4
  309. package/dist/types/sdk/positions/PositionsService.d.ts +33 -2
  310. package/dist/types/sdk/positions/calcBorrowRate.d.ts +40 -0
  311. package/dist/types/sdk/positions/calcHealthFactor.d.ts +55 -0
  312. package/dist/types/sdk/positions/calcLiquidationPrice.d.ts +12 -0
  313. package/dist/types/sdk/positions/calcLiquidationPriceForTarget.d.ts +43 -0
  314. package/dist/types/sdk/positions/calcTimeToLiquidationMs.d.ts +16 -0
  315. package/dist/types/sdk/positions/index.d.ts +7 -2
  316. package/dist/types/sdk/positions/types.d.ts +58 -1
  317. package/dist/types/sdk/types/state.d.ts +6 -0
  318. package/dist/types/sdk/utils/viem/executeMulticallBatches.d.ts +39 -0
  319. package/dist/types/sdk/utils/viem/index.d.ts +3 -2
  320. package/package.json +2 -1
  321. package/dist/cjs/model/history.js +0 -53
  322. package/dist/cjs/model/history.schema.js +0 -128
  323. package/dist/cjs/sdk/accounts/intents/intents/index.js +0 -14
  324. package/dist/cjs/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.js +0 -121
  325. package/dist/cjs/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.js +0 -15
  326. package/dist/cjs/sdk/accounts/intents/intents/resume/close/close.fixtures.js +0 -108
  327. package/dist/cjs/sdk/accounts/intents/intents/resume/close/close.js +0 -51
  328. package/dist/cjs/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.js +0 -130
  329. package/dist/cjs/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.js +0 -83
  330. package/dist/cjs/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.js +0 -117
  331. package/dist/cjs/sdk/accounts/intents/intents/resume/deposit/deposit.js +0 -15
  332. package/dist/cjs/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.js +0 -95
  333. package/dist/cjs/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.js +0 -15
  334. package/dist/cjs/sdk/accounts/intents/intents/resume/index.js +0 -13
  335. package/dist/cjs/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.js +0 -138
  336. package/dist/cjs/sdk/accounts/intents/intents/resume/withdraw/withdraw.js +0 -161
  337. package/dist/cjs/sdk/accounts/intents/operations/add-collateral/index.js +0 -17
  338. package/dist/cjs/sdk/accounts/intents/operations/claim-delayed/index.js +0 -54
  339. package/dist/cjs/sdk/accounts/intents/operations/close-credit-account/index.js +0 -26
  340. package/dist/cjs/sdk/accounts/intents/operations/decrease-debt/index.js +0 -12
  341. package/dist/cjs/sdk/accounts/intents/operations/increase-debt/index.js +0 -12
  342. package/dist/cjs/sdk/accounts/intents/operations/index.js +0 -27
  343. package/dist/cjs/sdk/accounts/intents/operations/quota-update/index.js +0 -22
  344. package/dist/cjs/sdk/accounts/intents/operations/repay-credit-account/index.js +0 -22
  345. package/dist/cjs/sdk/accounts/intents/operations/start-delayed-withdrawal/index.js +0 -18
  346. package/dist/cjs/sdk/accounts/intents/operations/swap/index.js +0 -18
  347. package/dist/cjs/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +0 -27
  348. package/dist/cjs/sdk/accounts/intents/operations/withdraw-collateral/index.js +0 -43
  349. package/dist/cjs/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +0 -27
  350. package/dist/cjs/sdk/accounts/intents/quoters/close.js +0 -58
  351. package/dist/cjs/sdk/accounts/intents/quoters/index.js +0 -7
  352. package/dist/cjs/sdk/accounts/intents/quoters/swap.js +0 -79
  353. package/dist/cjs/sdk/accounts/intents/testing/expect.js +0 -167
  354. package/dist/cjs/sdk/accounts/intents/testing/resume.js +0 -172
  355. package/dist/cjs/sdk/accounts/intents/utils/simulate-adjust-state.js +0 -39
  356. package/dist/cjs/sdk/accounts/intents/utils/simulate-assets.js +0 -69
  357. package/dist/cjs/sdk/accounts/intents/utils/with-quota-update.js +0 -13
  358. package/dist/esm/model/history.js +0 -49
  359. package/dist/esm/model/history.schema.js +0 -116
  360. package/dist/esm/sdk/accounts/intents/intents/index.js +0 -8
  361. package/dist/esm/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.js +0 -107
  362. package/dist/esm/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.js +0 -14
  363. package/dist/esm/sdk/accounts/intents/intents/resume/close/close.fixtures.js +0 -94
  364. package/dist/esm/sdk/accounts/intents/intents/resume/close/close.js +0 -50
  365. package/dist/esm/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.js +0 -114
  366. package/dist/esm/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.js +0 -82
  367. package/dist/esm/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.js +0 -110
  368. package/dist/esm/sdk/accounts/intents/intents/resume/deposit/deposit.js +0 -14
  369. package/dist/esm/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.js +0 -88
  370. package/dist/esm/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.js +0 -14
  371. package/dist/esm/sdk/accounts/intents/intents/resume/index.js +0 -7
  372. package/dist/esm/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.js +0 -121
  373. package/dist/esm/sdk/accounts/intents/intents/resume/withdraw/withdraw.js +0 -160
  374. package/dist/esm/sdk/accounts/intents/operations/add-collateral/index.js +0 -16
  375. package/dist/esm/sdk/accounts/intents/operations/claim-delayed/index.js +0 -52
  376. package/dist/esm/sdk/accounts/intents/operations/close-credit-account/index.js +0 -25
  377. package/dist/esm/sdk/accounts/intents/operations/decrease-debt/index.js +0 -11
  378. package/dist/esm/sdk/accounts/intents/operations/increase-debt/index.js +0 -11
  379. package/dist/esm/sdk/accounts/intents/operations/index.js +0 -14
  380. package/dist/esm/sdk/accounts/intents/operations/quota-update/index.js +0 -21
  381. package/dist/esm/sdk/accounts/intents/operations/repay-credit-account/index.js +0 -21
  382. package/dist/esm/sdk/accounts/intents/operations/start-delayed-withdrawal/index.js +0 -17
  383. package/dist/esm/sdk/accounts/intents/operations/swap/index.js +0 -17
  384. package/dist/esm/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +0 -26
  385. package/dist/esm/sdk/accounts/intents/operations/withdraw-collateral/index.js +0 -42
  386. package/dist/esm/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +0 -26
  387. package/dist/esm/sdk/accounts/intents/quoters/close.js +0 -56
  388. package/dist/esm/sdk/accounts/intents/quoters/index.js +0 -3
  389. package/dist/esm/sdk/accounts/intents/quoters/swap.js +0 -77
  390. package/dist/esm/sdk/accounts/intents/testing/expect.js +0 -162
  391. package/dist/esm/sdk/accounts/intents/testing/resume.js +0 -157
  392. package/dist/esm/sdk/accounts/intents/utils/simulate-adjust-state.js +0 -38
  393. package/dist/esm/sdk/accounts/intents/utils/simulate-assets.js +0 -68
  394. package/dist/esm/sdk/accounts/intents/utils/with-quota-update.js +0 -12
  395. package/dist/types/model/history.d.ts +0 -153
  396. package/dist/types/model/history.schema.d.ts +0 -95
  397. package/dist/types/new-sdk/utils/history.d.ts +0 -18
  398. package/dist/types/sdk/accounts/intents/intents/index.d.ts +0 -8
  399. package/dist/types/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.d.ts +0 -13
  400. package/dist/types/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.d.ts +0 -38
  401. package/dist/types/sdk/accounts/intents/intents/resume/close/close.d.ts +0 -26
  402. package/dist/types/sdk/accounts/intents/intents/resume/close/close.fixtures.d.ts +0 -63
  403. package/dist/types/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.d.ts +0 -26
  404. package/dist/types/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.d.ts +0 -86
  405. package/dist/types/sdk/accounts/intents/intents/resume/deposit/deposit.d.ts +0 -13
  406. package/dist/types/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.d.ts +0 -48
  407. package/dist/types/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.d.ts +0 -13
  408. package/dist/types/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.d.ts +0 -46
  409. package/dist/types/sdk/accounts/intents/intents/resume/index.d.ts +0 -7
  410. package/dist/types/sdk/accounts/intents/intents/resume/withdraw/withdraw.d.ts +0 -33
  411. package/dist/types/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.d.ts +0 -68
  412. package/dist/types/sdk/accounts/intents/operations/add-collateral/index.d.ts +0 -23
  413. package/dist/types/sdk/accounts/intents/operations/claim-delayed/index.d.ts +0 -55
  414. package/dist/types/sdk/accounts/intents/operations/close-credit-account/index.d.ts +0 -22
  415. package/dist/types/sdk/accounts/intents/operations/decrease-debt/index.d.ts +0 -18
  416. package/dist/types/sdk/accounts/intents/operations/increase-debt/index.d.ts +0 -18
  417. package/dist/types/sdk/accounts/intents/operations/index.d.ts +0 -14
  418. package/dist/types/sdk/accounts/intents/operations/quota-update/index.d.ts +0 -24
  419. package/dist/types/sdk/accounts/intents/operations/repay-credit-account/index.d.ts +0 -31
  420. package/dist/types/sdk/accounts/intents/operations/start-delayed-withdrawal/index.d.ts +0 -23
  421. package/dist/types/sdk/accounts/intents/operations/swap/index.d.ts +0 -28
  422. package/dist/types/sdk/accounts/intents/operations/types.d.ts +0 -21
  423. package/dist/types/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.d.ts +0 -30
  424. package/dist/types/sdk/accounts/intents/operations/withdraw-collateral/index.d.ts +0 -36
  425. package/dist/types/sdk/accounts/intents/operations/wrap-rwa-collateral/index.d.ts +0 -30
  426. package/dist/types/sdk/accounts/intents/quoters/close.d.ts +0 -43
  427. package/dist/types/sdk/accounts/intents/quoters/index.d.ts +0 -3
  428. package/dist/types/sdk/accounts/intents/quoters/swap.d.ts +0 -43
  429. package/dist/types/sdk/accounts/intents/testing/resume.d.ts +0 -84
  430. package/dist/types/sdk/accounts/intents/utils/simulate-adjust-state.d.ts +0 -19
  431. package/dist/types/sdk/accounts/intents/utils/simulate-assets.d.ts +0 -21
  432. package/dist/types/sdk/accounts/intents/utils/with-quota-update.d.ts +0 -16
  433. /package/dist/cjs/{new-sdk/utils/history.js → model/notices.js} +0 -0
  434. /package/dist/cjs/{sdk/accounts/intents/operations → new-sdk/simulate}/types.js +0 -0
  435. /package/dist/esm/{new-sdk/utils/history.js → model/notices.js} +0 -0
  436. /package/dist/esm/{sdk/accounts/intents/operations → new-sdk/simulate}/types.js +0 -0
@@ -1,389 +1,245 @@
1
1
  import { SDKConstruct } from "../../base/SDKConstruct.js";
2
- import { assembleOperationCalls } from "./utils/assemble-operation-calls.js";
3
- import { simulateState } from "./utils/simulate-adjust-state.js";
4
- import { getOperationsWithQuotaUpdate } from "./utils/with-quota-update.js";
5
- import "./utils/index.js";
6
- import { primaryInstantOutput } from "./operations/claim-delayed/index.js";
7
- import "./operations/index.js";
8
- import { buildResumeAddCollateralOperations } from "./intents/resume/add-collateral/add-collateral.js";
9
- import { buildResumeCloseOperations } from "./intents/resume/close/close.js";
10
- import { buildResumeDecreaseLeverageOperations } from "./intents/resume/decrease-leverage/decrease-leverage.js";
11
- import { buildResumeDepositOperations } from "./intents/resume/deposit/deposit.js";
12
- import { buildResumeIncreaseLeverageOperations } from "./intents/resume/increase-leverage/increase-leverage.js";
13
- import { buildResumeWithdrawOperations } from "./intents/resume/withdraw/withdraw.js";
14
- import "./intents/resume/index.js";
15
- import { createCloseOracleQuoter, createRouterCloseQuoter } from "./quoters/close.js";
16
- import { createOracleSwapQuoter, createRouterSwapQuoter } from "./quoters/swap.js";
17
- import "./quoters/index.js";
2
+ import { IntentPreviewError } from "./types.js";
3
+ import { assertMarketOperable } from "./guards.js";
4
+ import { maxProportionalWithdrawal } from "./math.js";
5
+ import { fetchCreditAccountSlice, toCreditAccountSlice } from "./utils/credit-account-slice.js";
6
+ import { previewOpenStrategy } from "./open-strategy.js";
7
+ import { instantOutput } from "./operations.js";
8
+ import { planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planDeposit, planFinishClaimOnly, planFinishDecreaseLeverage, planFinishWithdraw, planRepay, planWithdraw, planWithdrawAsset, planWithdrawDelayed } from "./plan.js";
9
+ import { realize } from "./realize.js";
10
+ import { accountView } from "./view.js";
18
11
  //#region src/sdk/accounts/intents/index.ts
12
+ /**
13
+ * Previews of everything a wallet can do to an existing credit account.
14
+ *
15
+ * Two halves, deliberately kept apart: `plan.ts` turns an intent into a few
16
+ * steps with pure arithmetic, `realize.ts` turns the steps into router-backed
17
+ * operations. This class only picks the planner and wraps the outcome.
18
+ */
19
19
  var CreditAccountOperationsService = class extends SDKConstruct {
20
- async finishIntent(props) {
21
- const intent = props.intent;
22
- switch (intent.type) {
23
- case "ADD_COLLATERAL": return this.#finishAddCollateralIntent({
24
- ...props,
25
- intent
26
- });
27
- case "INCREASE_LEVERAGE": return this.#finishIncreaseLeverageIntent({
28
- ...props,
29
- intent
30
- });
31
- case "DEPOSIT": return this.#finishDepositIntent({
32
- ...props,
33
- intent
34
- });
35
- case "DEPOSIT_AND_INCREASE_LEVERAGE": return this.#finishDepositAndIncreaseLeverageIntent({
36
- ...props,
37
- intent
38
- });
39
- case "CLOSE_ACCOUNT": return this.#finishCloseAccountIntent({
40
- ...props,
41
- intent
42
- });
43
- case "WITHDRAW_COLLATERAL": return this.#finishWithdrawCollateralIntent({
44
- ...props,
45
- intent
46
- });
47
- case "DECREASE_LEVERAGE": return this.#finishDecreaseLeverageIntent({
48
- ...props,
49
- intent
50
- });
51
- default: throw new Error(`${props.intent.type} - not implemented`);
52
- }
20
+ /**
21
+ * Previews an operation on an existing account.
22
+ *
23
+ * @param props - Intent plus account slice, quota reserve and slippage
24
+ * @returns Operations, projected state and calldata, or `{ ok: false, reason }`
25
+ * when the intent cannot be satisfied (e.g. the account lacks the source
26
+ * balance)
27
+ */
28
+ async startIntent(props) {
29
+ return plain(await this.#preview(props, () => {
30
+ const { intent } = props;
31
+ const view = accountView(props.creditAccount, props.sdk);
32
+ switch (intent.type) {
33
+ case "ADD_COLLATERAL": return planAddCollateral(intent);
34
+ case "WITHDRAW_ASSET": return planWithdrawAsset(intent, view);
35
+ case "ADJUST_LEVERAGE": return planAdjustLeverage(intent, view);
36
+ case "DEPOSIT": return planDeposit(intent, view);
37
+ case "REPAY": return planRepay(intent, view);
38
+ case "WITHDRAW": return planWithdraw(intent, view);
39
+ default: throw new Error(`${intent.type} - not implemented`);
40
+ }
41
+ }));
53
42
  }
54
43
  /**
55
- * Shared resume flow for claim-only intents (add collateral, increase
56
- * leverage, deposit, deposit+increase): claim op + trailing changeQuota.
57
- * If any implementations change, respective handler should be implemented separately
44
+ * Largest `WITHDRAW` amount (in underlying) the account can take out while
45
+ * keeping leverage and staying inside the facade's debt band — the ceiling a
46
+ * withdraw form should offer. Taking everything out is the same intent with
47
+ * `MAX_UINT256` for an amount, and needs none of this arithmetic.
48
+ *
49
+ * @param props - Account slice and the SDK holding its market
50
+ * @returns Amount in underlying units; `0n` when nothing can leave
58
51
  */
59
- #finishClaimOnlyIntent = (buildOperations) => async (props) => {
60
- switch (props.options.kind) {
61
- case "offchain": return this.#finishClaimOnlyIntentOffchain(props, props.options, buildOperations);
62
- default: return this.#finishClaimOnlyIntentOnchain(props, props.options, buildOperations);
63
- }
64
- };
65
- #finishClaimOnlyIntentOffchain(props, options, buildOperations) {
66
- const baseOperations = buildOperations(props.creditAccount, options, this.sdk);
67
- const state = simulateState({
68
- ...props,
69
- operations: baseOperations
70
- });
71
- return {
72
- ok: true,
73
- instant: {
74
- operations: getOperationsWithQuotaUpdate({
75
- operations: baseOperations,
76
- state,
77
- creditAccount: props.creditAccount,
78
- sdk: this.sdk,
79
- options
80
- }),
81
- preview: { min: state.state },
82
- calls: []
83
- },
84
- instantError: void 0,
85
- delayedBranch: void 0,
86
- delayedError: void 0
87
- };
52
+ maxWithdraw(props) {
53
+ const view = accountView(props.creditAccount, props.sdk);
54
+ return maxProportionalWithdrawal(view, view.band);
88
55
  }
89
- async #finishClaimOnlyIntentOnchain(props, options, buildOperations) {
90
- const baseOperations = buildOperations(props.creditAccount, options, this.sdk);
91
- const state = simulateState({
92
- ...props,
93
- operations: baseOperations
94
- });
95
- const operations = getOperationsWithQuotaUpdate({
96
- operations: baseOperations,
97
- state,
98
- creditAccount: props.creditAccount,
99
- sdk: this.sdk,
100
- options
101
- });
102
- const calls = await assembleOperationCalls({
103
- ...props,
104
- operations
105
- });
106
- return {
107
- ok: true,
108
- instant: {
109
- operations,
110
- preview: { min: state.state },
111
- calls
112
- },
113
- instantError: void 0,
114
- delayedBranch: void 0,
115
- delayedError: void 0
116
- };
56
+ /**
57
+ * Debt a `REPAY` would have to cover to settle the account, in underlying
58
+ * units: principal plus the interest and fees accrued as of the read.
59
+ *
60
+ * A repayment of exactly this leaves nothing behind at the block it was read
61
+ * at, and a little behind at any later one, since interest does not stop —
62
+ * so a wallet meaning to clear the account sends this with a buffer on top,
63
+ * which the intent caps at the debt rather than spending. `REPAY` with
64
+ * `MAX_UINT256` sizes that buffer itself.
65
+ *
66
+ * @param props - Account slice and the SDK holding its market
67
+ * @returns Amount in underlying units; `0n` on an account that owes nothing
68
+ */
69
+ maxRepay(props) {
70
+ return accountView(props.creditAccount, props.sdk).debt;
117
71
  }
118
- #finishAddCollateralIntent = this.#finishClaimOnlyIntent(buildResumeAddCollateralOperations);
119
- #finishIncreaseLeverageIntent = this.#finishClaimOnlyIntent(buildResumeIncreaseLeverageOperations);
120
- #finishDepositIntent = this.#finishClaimOnlyIntent(buildResumeDepositOperations);
121
- #finishDepositAndIncreaseLeverageIntent = this.#finishClaimOnlyIntent(buildResumeDepositOperations);
122
72
  /**
123
- * Resume close: claim, then close against quoted post-claim balances.
124
- * Offchain quotes equity by oracle prices, onchain — router close path.
125
- * Router miss soft-fails to the oracle quote with `calls: []` (legacy parity).
73
+ * Previews the same operation when its source only redeems through its
74
+ * issuer: a Securitize dsToken, a Mellow share.
75
+ *
76
+ * Sits apart from {@link startIntent} because it is a different trade route,
77
+ * not a different intent: the request goes to the issuer instead of the
78
+ * router, and the proceeds — hence the repayment and the payout — arrive days
79
+ * later. Both routes of one intent are quoted together by
80
+ * {@link intentRoutes}; this is the one to call when the delayed route is the
81
+ * only one of interest.
82
+ *
83
+ * @param props - Intent plus account slice, quota reserve and slippage
84
+ * @returns The request transaction and what it recorded for the tail, or
85
+ * `{ ok: false, reason }` — `noDelayedRoute` when this route does not exist
86
+ * for the account at all
126
87
  */
127
- async #finishCloseAccountIntent({ options, ...props }) {
128
- switch (options.kind) {
129
- case "offchain": return this.#finishCloseAccountIntentOffchain({
130
- ...props,
131
- options
132
- });
133
- default: try {
134
- return await this.#finishCloseAccountIntentOnchain({
135
- ...props,
136
- options
137
- });
138
- } catch {
139
- return this.#finishCloseAccountIntentOffchain({
140
- ...props,
141
- options
142
- });
88
+ async startDelayedIntent(props) {
89
+ const { intent } = props;
90
+ const result = await this.#preview(props, () => {
91
+ const view = accountView(props.creditAccount, props.sdk);
92
+ switch (intent.type) {
93
+ case "ADJUST_LEVERAGE": return planAdjustLeverageDelayed(intent, view);
94
+ case "WITHDRAW": return planWithdrawDelayed(intent, view);
95
+ default: throw new Error(`${intent.type} - cannot be delayed`);
143
96
  }
144
- }
145
- }
146
- async #finishCloseAccountIntentOffchain({ options, ...props }) {
147
- const quoter = createCloseOracleQuoter({
148
- sdk: this.sdk,
149
- creditAccount: props.creditAccount
150
97
  });
151
- const { operations, quote } = await buildResumeCloseOperations({
152
- ...props,
153
- options
154
- }, quoter);
98
+ if (!result.ok) return result;
99
+ if (!result.delayed) throw new Error("startDelayedIntent: plan started no withdrawal");
155
100
  return {
156
- ok: true,
157
- instant: {
158
- operations,
159
- preview: { min: {
160
- kind: "close",
161
- amount: quote.amount,
162
- minAmount: quote.minAmount,
163
- underlyingBalance: quote.underlyingBalance
164
- } },
165
- calls: []
166
- },
167
- instantError: void 0,
168
- delayedBranch: void 0,
169
- delayedError: void 0
170
- };
171
- }
172
- async #finishCloseAccountIntentOnchain({ options, slippage = 0, ...props }) {
173
- const quoter = createRouterCloseQuoter({
174
- sdk: this.sdk,
175
- creditAccount: props.creditAccount,
176
- slippage
177
- });
178
- const { operations, quote } = await buildResumeCloseOperations({
179
- ...props,
180
- options
181
- }, quoter);
182
- const calls = await assembleOperationCalls({
183
- ...props,
184
- operations
185
- });
186
- return {
187
- ok: true,
188
- instant: {
189
- operations,
190
- preview: { min: {
191
- kind: "close",
192
- amount: quote.amount,
193
- minAmount: quote.minAmount,
194
- underlyingBalance: quote.underlyingBalance
195
- } },
196
- calls
197
- },
198
- instantError: void 0,
199
- delayedBranch: void 0,
200
- delayedError: void 0
101
+ ...result,
102
+ delayed: result.delayed
201
103
  };
202
104
  }
203
105
  /**
204
- * Resume withdraw: claim → (swap?) → decreaseDebt? → (unwrapRwa?) →
205
- * withdrawCollateral + trailing changeQuota. Offchain prices swaps by
206
- * oracle, onchain — router swap paths (leftover-aware).
106
+ * Previews a delayable intent both ways at once: settled by the router now,
107
+ * and started as a redemption that finishes days later.
108
+ *
109
+ * Which routes an account has depends on the intent and the token it sells,
110
+ * and a form has to know before it can offer a choice — so both are quoted
111
+ * from the same request and each is reported on its own. A route the account
112
+ * cannot take comes back `undefined` with its refusal in `refused`; only when
113
+ * neither answers is the whole result `{ ok: false }`.
114
+ *
115
+ * A route that could not be quoted at all — a pathfinder with no path out of
116
+ * the source, a read that failed — counts as missing rather than fatal, since
117
+ * the other route may be the one the caller wanted. With nothing left to
118
+ * report the failure is rethrown, as {@link startIntent} would have.
119
+ *
120
+ * @param props - A withdraw or adjust-leverage intent plus account slice,
121
+ * quota reserve and slippage
122
+ * @returns Whichever routes are viable, or `{ ok: false, reason }` when none is
207
123
  */
208
- async #finishWithdrawCollateralIntent({ options, ...props }) {
209
- switch (options.kind) {
210
- case "offchain": return this.#finishWithdrawCollateralIntentOffchain({
211
- ...props,
212
- options
213
- });
214
- default: try {
215
- return await this.#finishWithdrawCollateralIntentOnchain({
216
- ...props,
217
- options
218
- });
219
- } catch {
220
- return this.#finishWithdrawCollateralIntentOffchain({
221
- ...props,
222
- options
223
- });
224
- }
225
- }
226
- }
227
- async #finishWithdrawCollateralIntentOffchain({ options, ...props }) {
228
- const quoter = createOracleSwapQuoter({
229
- sdk: this.sdk,
230
- creditAccount: props.creditAccount
231
- });
232
- const baseOperations = await buildResumeWithdrawOperations({
233
- ...props,
234
- options
235
- }, quoter);
236
- const state = simulateState({
237
- ...props,
238
- operations: baseOperations
239
- });
240
- return {
124
+ async intentRoutes(props) {
125
+ const [instant, delayed] = await Promise.allSettled([this.startIntent(props), this.startDelayedIntent(props)]);
126
+ const instantRoute = instant.status === "fulfilled" && instant.value.ok ? instant.value : void 0;
127
+ const delayedRoute = delayed.status === "fulfilled" && delayed.value.ok ? delayed.value : void 0;
128
+ const refused = {
129
+ instant: instant.status === "fulfilled" && !instant.value.ok ? instant.value.reason : void 0,
130
+ delayed: delayed.status === "fulfilled" && !delayed.value.ok ? delayed.value.reason : void 0
131
+ };
132
+ if (instantRoute || delayedRoute) return {
241
133
  ok: true,
242
- instant: {
243
- operations: getOperationsWithQuotaUpdate({
244
- operations: baseOperations,
245
- state,
246
- creditAccount: props.creditAccount,
247
- sdk: this.sdk,
248
- options
249
- }),
250
- preview: { min: state.state },
251
- calls: []
252
- },
253
- instantError: void 0,
254
- delayedBranch: void 0,
255
- delayedError: void 0
134
+ instant: instantRoute,
135
+ delayed: delayedRoute,
136
+ refused
256
137
  };
257
- }
258
- async #finishWithdrawCollateralIntentOnchain({ options, slippage = 0, ...props }) {
259
- const quoter = createRouterSwapQuoter({
260
- sdk: this.sdk,
261
- creditAccount: props.creditAccount,
262
- slippage
263
- });
264
- const baseOperations = await buildResumeWithdrawOperations({
265
- ...props,
266
- options
267
- }, quoter);
268
- const state = simulateState({
269
- ...props,
270
- operations: baseOperations
271
- });
272
- const operations = getOperationsWithQuotaUpdate({
273
- operations: baseOperations,
274
- state,
275
- creditAccount: props.creditAccount,
276
- sdk: this.sdk,
277
- options
278
- });
279
- const calls = await assembleOperationCalls({
280
- ...props,
281
- operations
282
- });
138
+ const failed = [instant, delayed].find((s) => s.status === "rejected");
139
+ if (failed?.status === "rejected") throw failed.reason;
140
+ const reason = refused.instant ?? refused.delayed;
141
+ if (reason === void 0) throw new Error("intentRoutes: a route neither answered nor refused");
283
142
  return {
284
- ok: true,
285
- instant: {
286
- operations,
287
- preview: { min: state.state },
288
- calls
289
- },
290
- instantError: void 0,
291
- delayedBranch: void 0,
292
- delayedError: void 0
143
+ ok: false,
144
+ reason,
145
+ refused
293
146
  };
294
147
  }
295
148
  /**
296
- * Resume decrease leverage: claim → (wrapRwa | swap)? → decreaseDebt +
297
- * trailing changeQuota. Offchain prices swaps by oracle, onchain — router
298
- * swap paths. Router miss soft-fails to the oracle quote (legacy parity).
149
+ * Previews the tail of a delayed intent, once the withdrawal it started has
150
+ * matured: the claim, then whatever the intent still owes.
151
+ *
152
+ * Serves the two operations that can genuinely be interrupted by a delay — a
153
+ * withdrawal and a deleveraging. For the rest, the claim is the whole tail:
154
+ * the tokens land on the account and only their quota has to catch up.
155
+ *
156
+ * @param props - The recorded intent, the account slice as it stands now, and
157
+ * the matured claimable
158
+ * @returns Shaped exactly like {@link startIntent}'s result, so both halves of
159
+ * an operation are consumed the same way
299
160
  */
300
- async #finishDecreaseLeverageIntent({ options, ...props }) {
301
- switch (options.kind) {
302
- case "offchain": return this.#finishDecreaseLeverageIntentOffchain({
303
- ...props,
304
- options
305
- });
306
- default: try {
307
- return await this.#finishDecreaseLeverageIntentOnchain({
308
- ...props,
309
- options
310
- });
311
- } catch {
312
- return this.#finishDecreaseLeverageIntentOffchain({
313
- ...props,
314
- options
315
- });
161
+ async finishIntent(props) {
162
+ const { intent, claimable } = props;
163
+ return plain(await this.#preview(props, () => {
164
+ const view = accountView(props.creditAccount, props.sdk);
165
+ const claimed = () => {
166
+ const output = instantOutput(claimable.outputs);
167
+ if (!output) throw new IntentPreviewError("insufficientSourceBalance", "finishIntent: the claim credits nothing to spend");
168
+ return output;
169
+ };
170
+ switch (intent.type) {
171
+ case "WITHDRAW_COLLATERAL": return planFinishWithdraw(intent, claimable, claimed(), view);
172
+ case "DECREASE_LEVERAGE": return planFinishDecreaseLeverage(claimable, claimed(), view);
173
+ case "ADD_COLLATERAL":
174
+ case "INCREASE_LEVERAGE":
175
+ case "DEPOSIT":
176
+ case "DEPOSIT_AND_INCREASE_LEVERAGE": return planFinishClaimOnly(claimable);
177
+ default: throw new Error(`${intent.type} - not implemented`);
316
178
  }
317
- }
179
+ }));
318
180
  }
319
- async #finishDecreaseLeverageIntentOffchain({ options, ...props }) {
320
- const quoter = createOracleSwapQuoter({
321
- sdk: this.sdk,
322
- creditAccount: props.creditAccount
323
- });
324
- const baseOperations = await buildResumeDecreaseLeverageOperations({
325
- ...props,
326
- options
327
- }, quoter);
328
- const state = simulateState({
329
- ...props,
330
- operations: baseOperations
331
- });
332
- return {
333
- ok: true,
334
- instant: {
335
- operations: getOperationsWithQuotaUpdate({
336
- operations: baseOperations,
337
- state,
338
- creditAccount: props.creditAccount,
339
- sdk: this.sdk,
340
- options
341
- }),
342
- preview: { min: state.state },
343
- calls: []
344
- },
345
- instantError: void 0,
346
- delayedBranch: void 0,
347
- delayedError: void 0
348
- };
181
+ /**
182
+ * Previews opening a brand-new leveraged position.
183
+ *
184
+ * Sits apart from {@link startIntent} because there is no account yet: nothing
185
+ * can be projected from existing balances, and the output feeds
186
+ * `sdk.accounts.openCA` rather than a facade multicall assembled here.
187
+ *
188
+ * @param props - Credit manager, wallet collateral, target token and leverage
189
+ * @returns Debt, position size, projected balances and quotas for both the
190
+ * expected and the floor branch, or `{ ok: false, reason }` when the requested
191
+ * leverage or the resulting debt is not viable
192
+ */
193
+ async openStrategyIntent(props) {
194
+ try {
195
+ return {
196
+ ok: true,
197
+ preview: await previewOpenStrategy(props)
198
+ };
199
+ } catch (e) {
200
+ return asFailure(e);
201
+ }
349
202
  }
350
- async #finishDecreaseLeverageIntentOnchain({ options, slippage = 0, ...props }) {
351
- const quoter = createRouterSwapQuoter({
352
- sdk: this.sdk,
353
- creditAccount: props.creditAccount,
354
- slippage
355
- });
356
- const baseOperations = await buildResumeDecreaseLeverageOperations({
357
- ...props,
358
- options
359
- }, quoter);
360
- const state = simulateState({
361
- ...props,
362
- operations: baseOperations
363
- });
364
- const operations = getOperationsWithQuotaUpdate({
365
- operations: baseOperations,
366
- state,
367
- creditAccount: props.creditAccount,
368
- sdk: this.sdk,
369
- options
370
- });
371
- const calls = await assembleOperationCalls({
372
- ...props,
373
- operations
374
- });
375
- return {
376
- ok: true,
377
- instant: {
203
+ /** Plan → realise → wrap. Unviable requests become `{ ok: false }`. */
204
+ async #preview(props, plan) {
205
+ try {
206
+ assertMarketOperable(props.sdk.marketRegister.findCreditManager(props.creditAccount.creditManager));
207
+ const { operations, state, calls, delayed } = await realize(plan(), {
208
+ creditAccount: props.creditAccount,
209
+ sdk: props.sdk,
210
+ slippage: props.slippage ?? 0,
211
+ quotaReserve: props.quotaReserve
212
+ });
213
+ return {
214
+ ok: true,
378
215
  operations,
379
- preview: { min: state.state },
380
- calls
381
- },
382
- instantError: void 0,
383
- delayedBranch: void 0,
384
- delayedError: void 0
385
- };
216
+ preview: state,
217
+ calls,
218
+ delayed
219
+ };
220
+ } catch (e) {
221
+ return asFailure(e);
222
+ }
386
223
  }
387
224
  };
225
+ /** Drops the delayed half for the flows that cannot produce one. */
226
+ function plain(result) {
227
+ if (!result.ok) return result;
228
+ const { operations, preview, calls } = result;
229
+ return {
230
+ ok: true,
231
+ operations,
232
+ preview,
233
+ calls
234
+ };
235
+ }
236
+ /** Unviable requests are values; anything else is a genuine failure. */
237
+ function asFailure(e) {
238
+ if (e instanceof IntentPreviewError) return {
239
+ ok: false,
240
+ reason: e.reason
241
+ };
242
+ throw e;
243
+ }
388
244
  //#endregion
389
- export { CreditAccountOperationsService, primaryInstantOutput };
245
+ export { CreditAccountOperationsService, IntentPreviewError, fetchCreditAccountSlice, toCreditAccountSlice };
@@ -0,0 +1,51 @@
1
+ import { LEVERAGE_DECIMALS } from "../../constants/math.js";
2
+ import { IntentPreviewError } from "./types.js";
3
+ //#region src/sdk/accounts/intents/math.ts
4
+ /** Debt that realises `leverage` on `collateral`: `D = C · (L − 1)`. */
5
+ function debtForLeverage(collateral, leverage) {
6
+ return collateral * (leverage - LEVERAGE_DECIMALS) / LEVERAGE_DECIMALS;
7
+ }
8
+ /**
9
+ * Debt change that keeps leverage fixed while collateral moves by
10
+ * `collateralDelta`: `dD = D0 · dC / C0`.
11
+ *
12
+ * Computed on the ratio directly rather than by recovering `L` and reapplying
13
+ * it, so no rounding is introduced on the way.
14
+ */
15
+ function proportionalDebt(position, collateralDelta) {
16
+ if (position.collateral <= 0n) throw new IntentPreviewError("insufficientSourceBalance", "cannot preserve leverage on an account with no collateral");
17
+ return position.debt * collateralDelta / position.collateral;
18
+ }
19
+ /**
20
+ * Largest withdrawal (in underlying) that {@link proportionalDebt} can still
21
+ * pay for: the repayment it implies leaves debt at or above `minDebt`, and
22
+ * strictly less than the collateral goes — the last unit closes the account
23
+ * rather than shrinks it. `0n` when the debt already sits below the band.
24
+ *
25
+ * Solves `floor(D0 · W / C0) ≤ D0 − minDebt` for `W`.
26
+ */
27
+ function maxProportionalWithdrawal(position, band) {
28
+ const { debt, collateral } = position;
29
+ if (collateral <= 0n) return 0n;
30
+ const allButLast = collateral - 1n;
31
+ if (debt === 0n) return allButLast;
32
+ const repayable = debt - band.minDebt;
33
+ if (repayable < 0n) return 0n;
34
+ const bound = ceilDiv(collateral * (repayable + 1n), debt) - 1n;
35
+ return bound < allButLast ? bound : allButLast;
36
+ }
37
+ const ceilDiv = (a, b) => (a + b - 1n) / b;
38
+ /** Total leverage cannot drop below 1x — that would be negative debt. */
39
+ function assertLeverageAtLeastOne(leverage) {
40
+ if (leverage < 100n) throw new IntentPreviewError("leverageOutOfRange", `target leverage ${leverage} is below 1x`);
41
+ }
42
+ /**
43
+ * Rejects a debt the facade would revert on: zero is always fine (no loan at
44
+ * all), anything else has to sit inside `[minDebt, maxDebt]`.
45
+ */
46
+ function assertDebtInBand(debt, band) {
47
+ if (debt > band.maxDebt) throw new IntentPreviewError("debtOutOfRange", `debt ${debt} exceeds maxDebt ${band.maxDebt}`);
48
+ if (debt > 0n && debt < band.minDebt) throw new IntentPreviewError("debtOutOfRange", `debt ${debt} is below minDebt ${band.minDebt}`);
49
+ }
50
+ //#endregion
51
+ export { assertDebtInBand, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };