@morpho-org/morpho-sdk 10.0.1 → 10.1.0-next.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 (250) hide show
  1. package/README.md +31 -10
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +5 -1
  4. package/lib/cjs/actions/blue/refinance.d.ts +1 -1
  5. package/lib/cjs/actions/blue/refinance.js +3 -8
  6. package/lib/cjs/actions/blue/refinanceValidation.d.ts +19 -0
  7. package/lib/cjs/actions/blue/refinanceValidation.js +46 -0
  8. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  9. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -7
  10. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
  11. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -20
  12. package/lib/cjs/actions/requirements/getRequirementsApproval.js +7 -21
  13. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +1 -26
  14. package/lib/cjs/actions/requirements/validateClientChainId.js +2 -31
  15. package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -1
  16. package/lib/cjs/actions/vaultV2/forceRedeem.js +20 -2
  17. package/lib/cjs/blue/abis.d.ts +1 -1
  18. package/lib/cjs/blue/abis.js +5 -1
  19. package/lib/cjs/blue/entities.d.ts +4 -4
  20. package/lib/cjs/blue/entities.js +3 -1
  21. package/lib/cjs/blue/errors.d.ts +2 -2
  22. package/lib/cjs/blue/errors.js +5 -1
  23. package/lib/cjs/blue/fetch.d.ts +2 -2
  24. package/lib/cjs/blue/fetch.js +3 -1
  25. package/lib/cjs/blue/utils.d.ts +1 -1
  26. package/lib/cjs/blue/utils.js +2 -1
  27. package/lib/cjs/entities/blue/blue.js +24 -55
  28. package/lib/cjs/entities/index.d.ts +4 -4
  29. package/lib/cjs/entities/index.js +3 -1
  30. package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +2 -5
  31. package/lib/cjs/entities/requirements/getErc2612Permit.js +9 -58
  32. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
  33. package/lib/cjs/entities/vaultV2/vaultV2.js +39 -1
  34. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -2
  35. package/lib/cjs/entities/vaultV2BlueReallocationData.js +6 -2
  36. package/lib/cjs/errors.d.ts +4 -4
  37. package/lib/cjs/errors.js +7 -5
  38. package/lib/cjs/fetch.d.ts +2 -2
  39. package/lib/cjs/fetch.js +3 -1
  40. package/lib/cjs/helpers/deadShares.js +3 -0
  41. package/lib/cjs/helpers/quoteBlueRepay.d.ts +16 -0
  42. package/lib/cjs/helpers/quoteBlueRepay.js +46 -0
  43. package/lib/cjs/helpers/tokenApproval.d.ts +14 -0
  44. package/lib/cjs/helpers/tokenApproval.js +20 -0
  45. package/lib/cjs/helpers/validate.d.ts +1 -1
  46. package/lib/cjs/helpers/validate.js +2 -2
  47. package/lib/cjs/index.d.ts +2 -2
  48. package/lib/cjs/index.js +13 -1
  49. package/lib/cjs/midnight/errors.d.ts +2 -1
  50. package/lib/cjs/midnight/errors.js +5 -4
  51. package/lib/cjs/reads/readErc2612Permit.d.ts +22 -0
  52. package/lib/cjs/reads/readErc2612Permit.js +77 -0
  53. package/lib/cjs/reads/validateClientChainId.d.ts +26 -0
  54. package/lib/cjs/reads/validateClientChainId.js +34 -0
  55. package/lib/cjs/transactionPlan/blue/authorization.d.ts +23 -0
  56. package/lib/cjs/transactionPlan/blue/authorization.js +128 -0
  57. package/lib/cjs/transactionPlan/blue/borrow.d.ts +80 -0
  58. package/lib/cjs/transactionPlan/blue/borrow.js +248 -0
  59. package/lib/cjs/transactionPlan/blue/prepare.d.ts +102 -0
  60. package/lib/cjs/transactionPlan/blue/prepare.js +191 -0
  61. package/lib/cjs/transactionPlan/blue/refinance.d.ts +93 -0
  62. package/lib/cjs/transactionPlan/blue/refinance.js +281 -0
  63. package/lib/cjs/transactionPlan/blue/refinanceValidation.d.ts +11 -0
  64. package/lib/cjs/transactionPlan/blue/refinanceValidation.js +20 -0
  65. package/lib/cjs/transactionPlan/blue/repay.d.ts +80 -0
  66. package/lib/cjs/transactionPlan/blue/repay.js +229 -0
  67. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
  68. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +379 -0
  69. package/lib/cjs/transactionPlan/blue/supply.d.ts +181 -0
  70. package/lib/cjs/transactionPlan/blue/supply.js +238 -0
  71. package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +97 -0
  72. package/lib/cjs/transactionPlan/blue/supplyCollateral.js +231 -0
  73. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
  74. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +395 -0
  75. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +136 -0
  76. package/lib/cjs/transactionPlan/blue/withdraw.js +340 -0
  77. package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
  78. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +246 -0
  79. package/lib/cjs/transactionPlan/deposit.js +8 -19
  80. package/lib/cjs/transactionPlan/error.d.ts +50 -0
  81. package/lib/cjs/transactionPlan/error.js +60 -1
  82. package/lib/cjs/transactionPlan/getTokenPermit.d.ts +17 -8
  83. package/lib/cjs/transactionPlan/getTokenPermit.js +35 -7
  84. package/lib/cjs/transactionPlan/index.d.ts +21 -1
  85. package/lib/cjs/transactionPlan/index.js +27 -5
  86. package/lib/cjs/transactionPlan/intent.d.ts +2 -2
  87. package/lib/cjs/transactionPlan/intent.js +8 -4
  88. package/lib/cjs/transactionPlan/permit.d.ts +16 -2
  89. package/lib/cjs/transactionPlan/permit.js +77 -45
  90. package/lib/cjs/transactionPlan/plan.d.ts +67 -12
  91. package/lib/cjs/transactionPlan/plan.js +284 -50
  92. package/lib/cjs/transactionPlan/requirements.d.ts +14 -5
  93. package/lib/cjs/transactionPlan/requirements.js +30 -12
  94. package/lib/cjs/transactionPlan/serde.d.ts +7 -4
  95. package/lib/cjs/transactionPlan/serde.js +80 -61
  96. package/lib/cjs/transactionPlan/types.d.ts +141 -13
  97. package/lib/cjs/transactionPlan/validateActions.js +11 -1
  98. package/lib/cjs/transactionPlan/validation.d.ts +8 -0
  99. package/lib/cjs/transactionPlan/validation.js +90 -0
  100. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -0
  101. package/lib/cjs/transactionPlan/vaultv1/deposit.js +1 -0
  102. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
  103. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +1 -0
  104. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
  105. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +1 -0
  106. package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +1 -0
  107. package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -0
  108. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -0
  109. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +1 -0
  110. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -0
  111. package/lib/cjs/transactionPlan/vaultv2/deposit.js +1 -0
  112. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
  113. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +1 -0
  114. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
  115. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +1 -0
  116. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
  117. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +1 -0
  118. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -0
  119. package/lib/cjs/transactionPlan/vaultv2/redeem.js +1 -0
  120. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -0
  121. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +1 -0
  122. package/lib/cjs/utils.d.ts +2 -2
  123. package/lib/cjs/utils.js +4 -2
  124. package/lib/cjs/version.d.ts +1 -1
  125. package/lib/cjs/version.js +1 -1
  126. package/lib/esm/abis.d.ts +1 -1
  127. package/lib/esm/abis.js +1 -1
  128. package/lib/esm/actions/blue/refinance.d.ts +1 -1
  129. package/lib/esm/actions/blue/refinance.js +4 -9
  130. package/lib/esm/actions/blue/refinanceValidation.d.ts +19 -0
  131. package/lib/esm/actions/blue/refinanceValidation.js +41 -0
  132. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  133. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -8
  134. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
  135. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -21
  136. package/lib/esm/actions/requirements/getRequirementsApproval.js +7 -21
  137. package/lib/esm/actions/requirements/validateClientChainId.d.ts +1 -26
  138. package/lib/esm/actions/requirements/validateClientChainId.js +1 -30
  139. package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -1
  140. package/lib/esm/actions/vaultV2/forceRedeem.js +20 -2
  141. package/lib/esm/blue/abis.d.ts +1 -1
  142. package/lib/esm/blue/abis.js +1 -1
  143. package/lib/esm/blue/entities.d.ts +4 -4
  144. package/lib/esm/blue/entities.js +2 -2
  145. package/lib/esm/blue/errors.d.ts +2 -2
  146. package/lib/esm/blue/errors.js +2 -2
  147. package/lib/esm/blue/fetch.d.ts +2 -2
  148. package/lib/esm/blue/fetch.js +2 -2
  149. package/lib/esm/blue/utils.d.ts +1 -1
  150. package/lib/esm/blue/utils.js +1 -1
  151. package/lib/esm/entities/blue/blue.js +26 -57
  152. package/lib/esm/entities/index.d.ts +4 -4
  153. package/lib/esm/entities/index.js +2 -2
  154. package/lib/esm/entities/requirements/getErc2612Permit.d.ts +2 -5
  155. package/lib/esm/entities/requirements/getErc2612Permit.js +9 -58
  156. package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
  157. package/lib/esm/entities/vaultV2/vaultV2.js +40 -2
  158. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -2
  159. package/lib/esm/entities/vaultV2BlueReallocationData.js +6 -2
  160. package/lib/esm/errors.d.ts +4 -4
  161. package/lib/esm/errors.js +4 -4
  162. package/lib/esm/fetch.d.ts +2 -2
  163. package/lib/esm/fetch.js +2 -2
  164. package/lib/esm/helpers/deadShares.js +4 -1
  165. package/lib/esm/helpers/quoteBlueRepay.d.ts +16 -0
  166. package/lib/esm/helpers/quoteBlueRepay.js +42 -0
  167. package/lib/esm/helpers/tokenApproval.d.ts +14 -0
  168. package/lib/esm/helpers/tokenApproval.js +16 -0
  169. package/lib/esm/helpers/validate.d.ts +1 -1
  170. package/lib/esm/helpers/validate.js +3 -3
  171. package/lib/esm/index.d.ts +2 -2
  172. package/lib/esm/index.js +1 -1
  173. package/lib/esm/midnight/errors.d.ts +2 -1
  174. package/lib/esm/midnight/errors.js +2 -1
  175. package/lib/esm/reads/readErc2612Permit.d.ts +22 -0
  176. package/lib/esm/reads/readErc2612Permit.js +73 -0
  177. package/lib/esm/reads/validateClientChainId.d.ts +26 -0
  178. package/lib/esm/reads/validateClientChainId.js +30 -0
  179. package/lib/esm/transactionPlan/blue/authorization.d.ts +23 -0
  180. package/lib/esm/transactionPlan/blue/authorization.js +122 -0
  181. package/lib/esm/transactionPlan/blue/borrow.d.ts +80 -0
  182. package/lib/esm/transactionPlan/blue/borrow.js +243 -0
  183. package/lib/esm/transactionPlan/blue/prepare.d.ts +102 -0
  184. package/lib/esm/transactionPlan/blue/prepare.js +187 -0
  185. package/lib/esm/transactionPlan/blue/refinance.d.ts +93 -0
  186. package/lib/esm/transactionPlan/blue/refinance.js +276 -0
  187. package/lib/esm/transactionPlan/blue/refinanceValidation.d.ts +11 -0
  188. package/lib/esm/transactionPlan/blue/refinanceValidation.js +17 -0
  189. package/lib/esm/transactionPlan/blue/repay.d.ts +80 -0
  190. package/lib/esm/transactionPlan/blue/repay.js +223 -0
  191. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
  192. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +373 -0
  193. package/lib/esm/transactionPlan/blue/supply.d.ts +181 -0
  194. package/lib/esm/transactionPlan/blue/supply.js +232 -0
  195. package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +97 -0
  196. package/lib/esm/transactionPlan/blue/supplyCollateral.js +225 -0
  197. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
  198. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +389 -0
  199. package/lib/esm/transactionPlan/blue/withdraw.d.ts +136 -0
  200. package/lib/esm/transactionPlan/blue/withdraw.js +334 -0
  201. package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
  202. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +240 -0
  203. package/lib/esm/transactionPlan/deposit.js +9 -20
  204. package/lib/esm/transactionPlan/error.d.ts +50 -0
  205. package/lib/esm/transactionPlan/error.js +57 -0
  206. package/lib/esm/transactionPlan/getTokenPermit.d.ts +17 -8
  207. package/lib/esm/transactionPlan/getTokenPermit.js +35 -7
  208. package/lib/esm/transactionPlan/index.d.ts +21 -1
  209. package/lib/esm/transactionPlan/index.js +11 -1
  210. package/lib/esm/transactionPlan/intent.d.ts +2 -2
  211. package/lib/esm/transactionPlan/intent.js +8 -4
  212. package/lib/esm/transactionPlan/permit.d.ts +16 -2
  213. package/lib/esm/transactionPlan/permit.js +76 -44
  214. package/lib/esm/transactionPlan/plan.d.ts +67 -12
  215. package/lib/esm/transactionPlan/plan.js +279 -46
  216. package/lib/esm/transactionPlan/requirements.d.ts +14 -5
  217. package/lib/esm/transactionPlan/requirements.js +30 -12
  218. package/lib/esm/transactionPlan/serde.d.ts +7 -4
  219. package/lib/esm/transactionPlan/serde.js +73 -54
  220. package/lib/esm/transactionPlan/types.d.ts +141 -13
  221. package/lib/esm/transactionPlan/validateActions.js +11 -1
  222. package/lib/esm/transactionPlan/validation.d.ts +8 -0
  223. package/lib/esm/transactionPlan/validation.js +88 -1
  224. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -0
  225. package/lib/esm/transactionPlan/vaultv1/deposit.js +1 -0
  226. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
  227. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +1 -0
  228. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
  229. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +1 -0
  230. package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +1 -0
  231. package/lib/esm/transactionPlan/vaultv1/redeem.js +1 -0
  232. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -0
  233. package/lib/esm/transactionPlan/vaultv1/withdraw.js +1 -0
  234. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -0
  235. package/lib/esm/transactionPlan/vaultv2/deposit.js +1 -0
  236. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
  237. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +1 -0
  238. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
  239. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +1 -0
  240. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
  241. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +1 -0
  242. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -0
  243. package/lib/esm/transactionPlan/vaultv2/redeem.js +1 -0
  244. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -0
  245. package/lib/esm/transactionPlan/vaultv2/withdraw.js +1 -0
  246. package/lib/esm/utils.d.ts +2 -2
  247. package/lib/esm/utils.js +2 -2
  248. package/lib/esm/version.d.ts +1 -1
  249. package/lib/esm/version.js +1 -1
  250. package/package.json +7 -7
@@ -0,0 +1,243 @@
1
+ import { deepFreeze, MathLib, Time } from "@morpho-org/morpho-ts";
2
+ import { blueBorrow } from "../../actions/blue/borrow.js";
3
+ import { normalizeBundlesCommonParams } from "../../actions/bundles/common.js";
4
+ import { DEFAULT_LLTV_BUFFER } from "../../helpers/index.js";
5
+ import { validatePositionHealth } from "../../helpers/validate.js";
6
+ import { IncompleteTransactionPlanError, InvalidTransactionPlanError, } from "../error.js";
7
+ import { getPlanObject, validatePlanFields } from "../validation.js";
8
+ import { prepareBlue, } from "./prepare.js";
9
+ /**
10
+ * Prepares a BlueBundlesV1 borrow against existing collateral. No token allowance is needed.
11
+ * Health is checked on the position accrued two hours beyond the later of now and last update,
12
+ * matching the existing Blue borrow handle. Snapshots are not retained in the stored plan.
13
+ * Reads Morpho authorization of BlueBundlesV1 (and its nonce when signing) from `client`.
14
+ * @param client - Viem client for `params.chainId`, used for Morpho authorization and contract code reads.
15
+ * @param params - Borrow intent and chain-tagged snapshots for the borrowing account.
16
+ * @returns A data-only plan with any Morpho authorization prerequisite before the borrow.
17
+ * @throws {ChainIdMismatchError} When the client declares another chain or its RPC reports another chain.
18
+ * @throws {MissingContractCodeError} When BlueBundlesV1 or Morpho Blue has no deployed code.
19
+ * @throws {UnsupportedBlueMarketIrmError} When positive debt requires an unsupported IRM projection.
20
+ * @throws {viem.BaseError} When a chain ID, authorization, nonce, or contract code read fails.
21
+ * @throws {BlueSnapshotChainMismatchError} When a snapshot was read from another chain.
22
+ * @throws {MarketIdMismatchError} When a snapshot describes another market.
23
+ * @throws {AccrualPositionUserMismatchError} When `positionData` belongs to another account.
24
+ * @throws {ZeroAccountError} When `account` is the zero address.
25
+ * @throws {NonIntegerInputError} When `expiresAt` or `referralFee.bps` is not a safe integer.
26
+ * @throws {NonPositiveInputError} When `expiresAt`, `borrowAssets` or a reallocation amount is not positive.
27
+ * @throws {ExpiredDeadlineError} When `expiresAt` is at or before `now`.
28
+ * @throws {NegativeInputError} When `referralFee.bps` or a reallocation penalty is negative.
29
+ * @throws {InputExceedsMaxError} When a fee or reallocation bound is exceeded, including aggregate allocator penalties above the borrow.
30
+ * @throws {InvalidCalldataOriginError} When `calldataOrigin` is empty, malformed or has an odd number of hex digits.
31
+ * @throws {ReferralFeePctExceededError} When `referralFee.bps` equals 10,000.
32
+ * @throws {ReferralFeeRecipientMissingError} When a positive referral fee has no recipient.
33
+ * @throws {InvalidVaultV2BlueReallocationShapeError} When a reallocation entry is malformed.
34
+ * @throws {ReallocationWithdrawalOnTargetMarketError} When a reallocation sources the target market.
35
+ * @throws {MissingMarketPriceError} When the market snapshot has no oracle price.
36
+ * @throws {BorrowExceedsSafeLtvError} When the resulting debt exceeds buffered LLTV.
37
+ * @see blueBorrow for the remaining reallocation encoding errors.
38
+ * @example
39
+ * ```ts
40
+ * import { prepareBlueBorrow } from "@morpho-org/morpho-sdk";
41
+ * import { fetchBlueAccrualPosition, fetchBlueMarket } from "@morpho-org/morpho-sdk/fetch";
42
+ * import type { MarketId } from "@morpho-org/blue-sdk";
43
+ * import { createPublicClient, http } from "viem";
44
+ * import { mainnet } from "viem/chains";
45
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
46
+ * const account = "0x0000000000000000000000000000000000000001";
47
+ * const marketId = "0xb8fc70e82bc5bb53e773626fcc6a23f7eefa036918d7ef216ecfb1950a94a85e" as MarketId;
48
+ * const plan = await prepareBlueBorrow(client, {
49
+ * chainId: 1, account, marketId, borrowAssets: 1_000_000n, supportSignature: true,
50
+ * marketData: { chainId: 1, market: await fetchBlueMarket(marketId, client) },
51
+ * positionData: { chainId: 1, position: await fetchBlueAccrualPosition(account, marketId, client) },
52
+ * now: (await client.getBlock()).timestamp,
53
+ * });
54
+ * // Sign the pending Morpho authorization request if any, attach it with applySignatures, then build.
55
+ * ```
56
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
57
+ */
58
+ export async function prepareBlueBorrow(client, { chainId, account, marketId, marketData, positionData, borrowAssets, reallocations, expiresAt, referralFee, supportSignature, calldataOrigin, deployless, now = Time.timestamp(), }) {
59
+ const params = {
60
+ chainId,
61
+ account,
62
+ marketId,
63
+ marketData,
64
+ positionData,
65
+ borrowAssets,
66
+ reallocations,
67
+ expiresAt,
68
+ referralFee,
69
+ supportSignature,
70
+ calldataOrigin,
71
+ deployless,
72
+ now,
73
+ };
74
+ let inputs;
75
+ const { intent, steps, signatureRequests, morphoAuthorizationRequestId } = await prepareBlue({ client, params }, (resolvedIntent) => {
76
+ const marketParams = params.marketData.market.params;
77
+ const position = params.positionData.position;
78
+ inputs = {
79
+ chainId: params.chainId,
80
+ account: params.account,
81
+ marketParams: { ...marketParams },
82
+ borrowAssets: params.borrowAssets,
83
+ authorizationNonce: 0n,
84
+ maxLtv: MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER),
85
+ reallocations: structuredClone(params.reallocations ?? []),
86
+ ...normalizeBundlesCommonParams({
87
+ deadline: resolvedIntent.deadline,
88
+ referralFeePct: resolvedIntent.referralFeePct,
89
+ referralFeeRecipient: resolvedIntent.referralFeeRecipient,
90
+ }),
91
+ };
92
+ // Validate the pure encoding, including aggregate penalties, before any RPC reads.
93
+ buildBlueBorrow({ type: "blueBorrow", inputs, signatureRequestIds: {} }, []);
94
+ // Apply the same deadline-independent two-hour health quote as the existing handle.
95
+ validatePositionHealth({
96
+ positionData: position.accrueInterest(MathLib.max(now, position.market.lastUpdate) + Time.s.from.h(2n)),
97
+ additionalCollateral: 0n,
98
+ borrowAmount: params.borrowAssets,
99
+ marketId: params.marketId,
100
+ lltv: marketParams.lltv,
101
+ });
102
+ return { id: "blueBorrow", needsAuthorization: true };
103
+ });
104
+ if (inputs === undefined) {
105
+ throw new InvalidTransactionPlanError({
106
+ path: "inputs",
107
+ reason: "expected prepared borrow inputs",
108
+ });
109
+ }
110
+ return deepFreeze(structuredClone({
111
+ version: 2,
112
+ account: inputs.account,
113
+ chainId: inputs.chainId,
114
+ expiresAt: intent.expiresAt,
115
+ ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
116
+ steps: [
117
+ ...steps,
118
+ {
119
+ id: "blueBorrow",
120
+ label: "Borrow from Blue",
121
+ action: {
122
+ type: "blueBorrow",
123
+ inputs: {
124
+ ...inputs,
125
+ authorizationNonce: signatureRequests.find((request) => request.id === morphoAuthorizationRequestId)?.args.nonce ?? 0n,
126
+ },
127
+ signatureRequestIds: morphoAuthorizationRequestId
128
+ ? { morphoAuthorization: morphoAuthorizationRequestId }
129
+ : {},
130
+ },
131
+ },
132
+ ],
133
+ signatureRequests,
134
+ }));
135
+ }
136
+ /** @internal Encodes a prepared borrow without state reads or signing. */
137
+ export function buildBlueBorrow(action, requests) {
138
+ const { chainId, account, marketParams, borrowAssets, maxLtv, reallocations, deadline, referralFeePct, referralFeeRecipient, } = action.inputs;
139
+ const id = action.signatureRequestIds.morphoAuthorization;
140
+ const request = requests.find((candidate) => candidate.id === id);
141
+ if (id !== undefined &&
142
+ (request?.kind !== "morphoAuthorization" || request.signature === undefined)) {
143
+ throw new IncompleteTransactionPlanError(id);
144
+ }
145
+ const authorizationSignature = request?.kind === "morphoAuthorization" && request.signature !== undefined
146
+ ? {
147
+ action: {
148
+ type: "authorization",
149
+ args: {
150
+ authorized: request.args.authorized,
151
+ isAuthorized: true,
152
+ deadline: request.args.deadline,
153
+ },
154
+ },
155
+ args: {
156
+ ...request.args,
157
+ isAuthorized: true,
158
+ signature: request.signature,
159
+ },
160
+ }
161
+ : undefined;
162
+ return blueBorrow({
163
+ market: { chainId, marketParams },
164
+ args: {
165
+ borrowAssets,
166
+ maxLtv,
167
+ reallocations,
168
+ deadline,
169
+ referralFeePct,
170
+ referralFeeRecipient,
171
+ userAddress: account,
172
+ authorizationSignature,
173
+ },
174
+ });
175
+ }
176
+ function validateMarket(value, path) {
177
+ const market = getPlanObject(value, path);
178
+ validatePlanFields(market, {
179
+ path,
180
+ fields: {
181
+ id: "hex",
182
+ loanToken: "address",
183
+ collateralToken: "address",
184
+ oracle: "address",
185
+ irm: "address",
186
+ lltv: "uint256 bigint",
187
+ },
188
+ });
189
+ if (typeof market.id !== "string" || market.id.length !== 66) {
190
+ throw new InvalidTransactionPlanError({
191
+ path: `${path}.id`,
192
+ reason: "expected a 32-byte market ID",
193
+ });
194
+ }
195
+ }
196
+ /** @internal Validates the stored borrow recipe, including each allocator input. */
197
+ export function validateBorrowPlanAction(action, path) {
198
+ const inputs = getPlanObject(action.inputs, `${path}.inputs`);
199
+ getPlanObject(action.signatureRequestIds, `${path}.signatureRequestIds`);
200
+ validatePlanFields(inputs, {
201
+ path: `${path}.inputs`,
202
+ fields: {
203
+ borrowAssets: "uint256 bigint",
204
+ authorizationNonce: "uint256 bigint",
205
+ maxLtv: "uint256 bigint",
206
+ deadline: "uint256 bigint",
207
+ referralFeePct: "uint256 bigint",
208
+ referralFeeRecipient: "address",
209
+ reallocations: "array",
210
+ },
211
+ });
212
+ validateMarket(inputs.marketParams, `${path}.inputs.marketParams`);
213
+ for (const [index, value] of inputs.reallocations.entries()) {
214
+ const entryPath = `${path}.inputs.reallocations[${index}]`;
215
+ const entry = getPlanObject(value, entryPath);
216
+ validatePlanFields(entry, {
217
+ path: entryPath,
218
+ fields: {
219
+ vault: "address",
220
+ assets: "uint256 bigint",
221
+ penalty: "uint256 bigint",
222
+ },
223
+ });
224
+ validatePlanFields(getPlanObject(entry.to, `${entryPath}.to`), {
225
+ path: `${entryPath}.to`,
226
+ fields: { adapter: "address" },
227
+ });
228
+ const from = getPlanObject(entry.from, `${entryPath}.from`);
229
+ if (from.type !== "market" && from.type !== "idle") {
230
+ throw new InvalidTransactionPlanError({
231
+ path: `${entryPath}.from.type`,
232
+ reason: "expected market or idle",
233
+ });
234
+ }
235
+ if (from.type === "market") {
236
+ validatePlanFields(from, {
237
+ path: `${entryPath}.from`,
238
+ fields: { adapter: "address" },
239
+ });
240
+ validateMarket(from.marketParams, `${entryPath}.from.marketParams`);
241
+ }
242
+ }
243
+ }
@@ -0,0 +1,102 @@
1
+ import type { AccrualPosition, Market, MarketId } from "@morpho-org/blue-sdk";
2
+ import type { Address, Client } from "viem";
3
+ import { type IntentParams, resolveIntent } from "../intent.js";
4
+ import type { SignatureRequest, Step } from "../types.js";
5
+ /**
6
+ * Caller-fetched Blue market with the chain it was read from.
7
+ * @example
8
+ * ```ts
9
+ * import type { BlueMarketSnapshot } from "@morpho-org/morpho-sdk";
10
+ * import type { Market } from "@morpho-org/blue-sdk";
11
+ * const snapshot = (market: Market): BlueMarketSnapshot => ({ chainId: 1, market });
12
+ * ```
13
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
14
+ */
15
+ export interface BlueMarketSnapshot {
16
+ readonly chainId: number;
17
+ readonly market: Readonly<Market>;
18
+ }
19
+ /**
20
+ * Caller-fetched Blue accrual position with the chain it was read from.
21
+ * @example
22
+ * ```ts
23
+ * import type { BluePositionSnapshot } from "@morpho-org/morpho-sdk";
24
+ * import type { AccrualPosition } from "@morpho-org/blue-sdk";
25
+ * const snapshot = (position: AccrualPosition): BluePositionSnapshot => ({ chainId: 1, position });
26
+ * ```
27
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
28
+ */
29
+ export interface BluePositionSnapshot {
30
+ readonly chainId: number;
31
+ readonly position: Readonly<AccrualPosition>;
32
+ }
33
+ interface BluePrepareParams extends IntentParams {
34
+ readonly marketId: MarketId;
35
+ readonly marketData: BlueMarketSnapshot;
36
+ readonly positionData?: BluePositionSnapshot;
37
+ readonly now?: bigint;
38
+ }
39
+ /**
40
+ * Checks that a caller-fetched Blue market snapshot belongs to the expected chain
41
+ * and market, and that the market ID matches its parameters. Does not read RPC state.
42
+ * @param marketData - Caller-fetched market snapshot.
43
+ * @param expected - Expected chain ID and market ID.
44
+ * @param expected.chainId - Chain on which the market snapshot was read.
45
+ * @param expected.marketId - Market ID to match, regardless of hex casing.
46
+ * @returns Nothing when the snapshot matches the expected chain and market.
47
+ * @throws {BlueSnapshotChainMismatchError} If the snapshot belongs to a different chain.
48
+ * @throws {MarketIdMismatchError} If the snapshot identifies a different market.
49
+ * @example
50
+ * ```ts
51
+ * import { validateBlueMarketSnapshot } from "@morpho-org/morpho-sdk";
52
+ * import { addressesRegistry } from "@morpho-org/morpho-sdk/blue/addresses";
53
+ * import { Market, MarketParams } from "@morpho-org/morpho-sdk/blue/entities";
54
+ * import { mainnet } from "viem/chains";
55
+ *
56
+ * const chainId = mainnet.id;
57
+ * const params = MarketParams.idle(addressesRegistry[chainId].usdc);
58
+ * const market = new Market({
59
+ * params,
60
+ * totalSupplyAssets: 0n,
61
+ * totalSupplyShares: 0n,
62
+ * totalBorrowAssets: 0n,
63
+ * totalBorrowShares: 0n,
64
+ * lastUpdate: 0n,
65
+ * fee: 0n,
66
+ * });
67
+ * const result = validateBlueMarketSnapshot(
68
+ * { chainId, market },
69
+ * { chainId, marketId: params.id },
70
+ * );
71
+ * // result: undefined (the snapshot matched)
72
+ * ```
73
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
74
+ */
75
+ export declare function validateBlueMarketSnapshot(marketData: BlueMarketSnapshot, { chainId, marketId, }: {
76
+ readonly chainId: number;
77
+ readonly marketId: MarketId;
78
+ }): void;
79
+ /**
80
+ * @internal Validates supplied snapshots and the registered Blue targets, then resolves only
81
+ * the prerequisites requested by the operation. The callback derives funding from the
82
+ * validated inputs before any RPC read.
83
+ * An operation that receives a morphoAuthorizationRequestId must reference it
84
+ * in its action and encode the signed authorization in the BlueBundlesV1 call.
85
+ */
86
+ export declare function prepareBlue({ client, params, }: {
87
+ readonly client: Client;
88
+ readonly params: BluePrepareParams;
89
+ }, resolve: (intent: ReturnType<typeof resolveIntent>) => {
90
+ readonly id: string;
91
+ readonly tokenPull?: {
92
+ readonly token: Address;
93
+ readonly amount: bigint;
94
+ };
95
+ readonly needsAuthorization?: boolean;
96
+ }): Promise<{
97
+ intent: import("../intent.js").Intent;
98
+ steps: Step<import("../types.js").Action>[];
99
+ signatureRequests: SignatureRequest[];
100
+ morphoAuthorizationRequestId: string | undefined;
101
+ }>;
102
+ export {};
@@ -0,0 +1,187 @@
1
+ import { blueAbi, getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
2
+ import { getChainAddress, isHexEqual, Time } from "@morpho-org/morpho-ts";
3
+ import { isAddressEqual } from "viem";
4
+ import { readContract } from "viem/actions";
5
+ import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
6
+ import { assertRegistryTargetsHaveCode } from "../../entities/requirements/assertContractCode.js";
7
+ import { validateMarketParamsId } from "../../helpers/validate.js";
8
+ import { AccrualPositionUserMismatchError, MarketIdMismatchError, } from "../../types/index.js";
9
+ import { BlueSnapshotChainMismatchError } from "../error.js";
10
+ import { resolveIntent } from "../intent.js";
11
+ import { validateTokenPermitRequest } from "../permit.js";
12
+ import { getTokenRequirements } from "../requirements.js";
13
+ import { validateMorphoAuthorizationRequest } from "./authorization.js";
14
+ /**
15
+ * Checks that a caller-fetched Blue market snapshot belongs to the expected chain
16
+ * and market, and that the market ID matches its parameters. Does not read RPC state.
17
+ * @param marketData - Caller-fetched market snapshot.
18
+ * @param expected - Expected chain ID and market ID.
19
+ * @param expected.chainId - Chain on which the market snapshot was read.
20
+ * @param expected.marketId - Market ID to match, regardless of hex casing.
21
+ * @returns Nothing when the snapshot matches the expected chain and market.
22
+ * @throws {BlueSnapshotChainMismatchError} If the snapshot belongs to a different chain.
23
+ * @throws {MarketIdMismatchError} If the snapshot identifies a different market.
24
+ * @example
25
+ * ```ts
26
+ * import { validateBlueMarketSnapshot } from "@morpho-org/morpho-sdk";
27
+ * import { addressesRegistry } from "@morpho-org/morpho-sdk/blue/addresses";
28
+ * import { Market, MarketParams } from "@morpho-org/morpho-sdk/blue/entities";
29
+ * import { mainnet } from "viem/chains";
30
+ *
31
+ * const chainId = mainnet.id;
32
+ * const params = MarketParams.idle(addressesRegistry[chainId].usdc);
33
+ * const market = new Market({
34
+ * params,
35
+ * totalSupplyAssets: 0n,
36
+ * totalSupplyShares: 0n,
37
+ * totalBorrowAssets: 0n,
38
+ * totalBorrowShares: 0n,
39
+ * lastUpdate: 0n,
40
+ * fee: 0n,
41
+ * });
42
+ * const result = validateBlueMarketSnapshot(
43
+ * { chainId, market },
44
+ * { chainId, marketId: params.id },
45
+ * );
46
+ * // result: undefined (the snapshot matched)
47
+ * ```
48
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
49
+ */
50
+ export function validateBlueMarketSnapshot(marketData, { chainId, marketId, }) {
51
+ if (marketData.chainId !== chainId) {
52
+ throw new BlueSnapshotChainMismatchError(marketData.chainId, chainId);
53
+ }
54
+ if (!isHexEqual(marketData.market.id, marketId)) {
55
+ throw new MarketIdMismatchError(marketData.market.id, marketId);
56
+ }
57
+ // Validate that the reported snapshot ID is derived from the market tuple.
58
+ validateMarketParamsId(marketData.market.params);
59
+ }
60
+ function validateSnapshots(params) {
61
+ const { chainId, account, marketId, marketData, positionData } = params;
62
+ validateBlueMarketSnapshot(marketData, { chainId, marketId });
63
+ if (positionData === undefined) {
64
+ return;
65
+ }
66
+ if (positionData.chainId !== chainId) {
67
+ throw new BlueSnapshotChainMismatchError(positionData.chainId, chainId);
68
+ }
69
+ const { position } = positionData;
70
+ if (!isHexEqual(position.marketId, marketId)) {
71
+ throw new MarketIdMismatchError(position.marketId, marketId);
72
+ }
73
+ if (!isHexEqual(position.market.id, marketId)) {
74
+ throw new MarketIdMismatchError(position.market.id, marketId);
75
+ }
76
+ // Validate the embedded position market before deriving any operation inputs from it.
77
+ validateMarketParamsId(position.market.params);
78
+ if (!isAddressEqual(position.user, account)) {
79
+ throw new AccrualPositionUserMismatchError(position.user, account);
80
+ }
81
+ }
82
+ /**
83
+ * @internal Validates supplied snapshots and the registered Blue targets, then resolves only
84
+ * the prerequisites requested by the operation. The callback derives funding from the
85
+ * validated inputs before any RPC read.
86
+ * An operation that receives a morphoAuthorizationRequestId must reference it
87
+ * in its action and encode the signed authorization in the BlueBundlesV1 call.
88
+ */
89
+ export async function prepareBlue({ client, params, }, resolve) {
90
+ // Reject mismatched snapshots before deriving operation inputs or reading chain state.
91
+ validateSnapshots(params);
92
+ const now = params.now ?? Time.timestamp();
93
+ const intent = resolveIntent(params, now);
94
+ const { id, tokenPull, needsAuthorization } = resolve(intent);
95
+ const { chainId, account } = params;
96
+ const { deadline, supportSignature, deployless } = intent;
97
+ const spender = getChainAddress(chainId, "bundles.blueBundlesV1");
98
+ const morpho = getChainAddress(chainId, "blue");
99
+ // Reject missing targets before allowance or authorization reads can fail decoding empty data.
100
+ await assertRegistryTargetsHaveCode(client, chainId, [
101
+ { role: "bundles.blueBundlesV1", address: spender },
102
+ { role: "blue", address: morpho },
103
+ ]);
104
+ const steps = [];
105
+ const signatureRequests = [];
106
+ let morphoAuthorizationRequestId;
107
+ if (tokenPull !== undefined) {
108
+ await validateClientChainId(client, chainId);
109
+ const { approvals, permit } = await getTokenRequirements(client, {
110
+ ...tokenPull,
111
+ spender,
112
+ owner: account,
113
+ chainId,
114
+ deadline,
115
+ now,
116
+ supportSignature,
117
+ deployless,
118
+ permitId: `${id}:tokenPermit`,
119
+ approvalId: (index) => `${id}:approval:${index + 1}`,
120
+ });
121
+ steps.push(...approvals);
122
+ if (permit !== undefined) {
123
+ // Pin permits to the same BlueBundlesV1 pull that selected the prerequisite.
124
+ validateTokenPermitRequest(permit, {
125
+ chainId,
126
+ account,
127
+ deadline,
128
+ bundler: "bundles.blueBundlesV1",
129
+ asset: tokenPull.token,
130
+ amount: tokenPull.amount,
131
+ exact: true,
132
+ });
133
+ signatureRequests.push(permit);
134
+ }
135
+ }
136
+ if (needsAuthorization) {
137
+ if (tokenPull === undefined) {
138
+ await validateClientChainId(client, chainId);
139
+ }
140
+ const isAuthorized = await readContract(client, {
141
+ abi: blueAbi,
142
+ address: morpho,
143
+ functionName: "isAuthorized",
144
+ args: [account, spender],
145
+ });
146
+ if (!isAuthorized) {
147
+ const authorizationId = `${id}:morphoAuthorization`;
148
+ if (supportSignature) {
149
+ const nonce = await readContract(client, {
150
+ abi: blueAbi,
151
+ address: morpho,
152
+ functionName: "nonce",
153
+ args: [account],
154
+ });
155
+ const request = {
156
+ id: authorizationId,
157
+ kind: "morphoAuthorization",
158
+ args: { authorized: spender, owner: account, nonce, deadline },
159
+ typedData: getAuthorizationTypedData({
160
+ authorizer: account,
161
+ authorized: spender,
162
+ isAuthorized: true,
163
+ nonce,
164
+ deadline,
165
+ }, chainId),
166
+ };
167
+ // Check the request against the operation's authorization inputs before returning it.
168
+ validateMorphoAuthorizationRequest(request, {
169
+ chainId,
170
+ account,
171
+ nonce,
172
+ deadline,
173
+ });
174
+ signatureRequests.push(request);
175
+ morphoAuthorizationRequestId = authorizationId;
176
+ }
177
+ else {
178
+ steps.push({
179
+ id: authorizationId,
180
+ label: "Authorize BlueBundlesV1 on Morpho",
181
+ action: { type: "morphoAuthorization", inputs: { chainId, account } },
182
+ });
183
+ }
184
+ }
185
+ }
186
+ return { intent, steps, signatureRequests, morphoAuthorizationRequestId };
187
+ }
@@ -0,0 +1,93 @@
1
+ import { type MarketId } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
+ import type { BlueRefinanceAction, ReferenceTimestampParams, Transaction, VaultV2BlueReallocation } from "../../types/index.js";
4
+ import type { IntentParams } from "../intent.js";
5
+ import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
6
+ import { type BlueMarketSnapshot, type BluePositionSnapshot } from "./prepare.js";
7
+ type RefinanceAction = Extract<Action, {
8
+ type: "blueRefinance";
9
+ }>;
10
+ interface RefinanceMarket {
11
+ readonly marketId: MarketId;
12
+ readonly marketData: BlueMarketSnapshot;
13
+ readonly positionData: BluePositionSnapshot;
14
+ }
15
+ /**
16
+ * Intent and caller-fetched snapshots for a full Blue borrow-position migration.
17
+ * @example
18
+ * ```ts
19
+ * import type { BlueRefinancePlanParams } from "@morpho-org/morpho-sdk";
20
+ * const withSignature = (params: BlueRefinancePlanParams): BlueRefinancePlanParams => ({
21
+ * ...params,
22
+ * supportSignature: true,
23
+ * });
24
+ * ```
25
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
26
+ */
27
+ export interface BlueRefinancePlanParams extends Omit<IntentParams, "deployless">, ReferenceTimestampParams {
28
+ /** Request a Morpho authorization signature instead of a direct transaction when needed. Defaults to `false`. */
29
+ readonly supportSignature?: boolean;
30
+ /** Source market and borrowing account's position. */
31
+ readonly source: RefinanceMarket;
32
+ /** Destination market and the same account's position, including an empty position. */
33
+ readonly destination: RefinanceMarket;
34
+ /** Optional ordered Vault V2 reallocations into the destination market. */
35
+ readonly reallocations?: readonly VaultV2BlueReallocation[];
36
+ }
37
+ /**
38
+ * Prepares a full Blue borrow-position migration. Fetch both market and position snapshots at the
39
+ * same block and pass that block's timestamp as `now`. Penalties and fees increase destination debt;
40
+ * the quote checks the buffered destination LLTV and the transaction enforces it again onchain.
41
+ *
42
+ * BlueBundlesV1 moves the full live debt and collateral without pulling tokens from the account.
43
+ * When Morpho authorization is missing, the plan requests a signature inline if supported,
44
+ * otherwise it puts a direct authorization step before the migration.
45
+ * @param client - Viem client used to check deployed contracts and resolve Morpho authorization.
46
+ * @param params - Chain, sending account, market and position snapshots, and optional reallocations.
47
+ * @returns A frozen plan with only the steps and signature requests required for the migration.
48
+ * @throws {BlueSnapshotChainMismatchError} when either snapshot comes from another chain.
49
+ * @throws {MarketIdMismatchError} when a snapshot or embedded market tuple does not match its market ID.
50
+ * @throws {AccrualPositionUserMismatchError} when a position belongs to another account.
51
+ * @throws {RefinanceSameMarketError} when the source and destination are the same market.
52
+ * @throws {RefinanceTokenMismatchError} when the markets have different loan or collateral tokens.
53
+ * @throws {MissingMarketPriceError} when the destination price is unavailable.
54
+ * @throws {UnsupportedBlueMarketIrmError} when debt projection requires an unsupported IRM.
55
+ * @throws {BorrowExceedsSafeLtvError} when the resulting debt exceeds the buffered destination LLTV.
56
+ * @throws {ZeroAccountError} when the sending account is the zero address.
57
+ * @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
58
+ * @throws {NonPositiveInputError} when the source has no debt shares, or expiry or reallocation assets are not positive.
59
+ * @throws {ExpiredDeadlineError} when expiry is at or before `now`.
60
+ * @throws {NegativeInputError} when a referral fee or reallocation penalty is negative.
61
+ * @throws {InputExceedsMaxError} when the fee or a reallocation value exceeds its bound.
62
+ * @throws {ReferralFeePctExceededError} when `referralFee.bps` is 10,000.
63
+ * @throws {ReferralFeeRecipientMissingError} when a positive fee has no nonzero recipient.
64
+ * @throws {InvalidTransactionPlanError} when the account, referral fee recipient, market tuples, or stored reallocation fields are malformed.
65
+ * @throws {InvalidCalldataOriginError} when calldata origin is not nonempty, whole-byte hex.
66
+ * @throws {InvalidVaultV2BlueReallocationShapeError} when a reallocation is not a Vault V2 entry.
67
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is invalid.
68
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
69
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
70
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdraws from the destination market.
71
+ * @throws {ReallocationLoanTokenMismatchError} when a reallocation uses another loan token.
72
+ * @throws {ChainIdMismatchError} when the client or RPC serves another chain.
73
+ * @throws {UnsupportedChainIdError} when the chain is not in the address registry.
74
+ * @throws {UnknownAddressError} when Morpho Blue or BlueBundlesV1 is not registered on the chain.
75
+ * @throws {MissingContractCodeError} when either registered contract has no deployed code.
76
+ * @throws {viem.BaseError} when a required RPC read fails.
77
+ * @example
78
+ * ```ts
79
+ * import { getPendingSignatureRequests, prepareBlueRefinance, type BlueRefinancePlanParams } from "@morpho-org/morpho-sdk";
80
+ * import type { Client } from "viem";
81
+ * async function prepare(client: Client, snapshots: BlueRefinancePlanParams) {
82
+ * const plan = await prepareBlueRefinance(client, { ...snapshots, supportSignature: true });
83
+ * return { plan, requests: getPendingSignatureRequests(plan) };
84
+ * }
85
+ * ```
86
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
87
+ */
88
+ export declare function prepareBlueRefinance(client: Client, params: BlueRefinancePlanParams): Promise<TransactionPlan>;
89
+ /** @internal Validates the stored migration inputs and optional authorization binding. */
90
+ export declare function validateRefinancePlanAction(action: Record<string, unknown>, path: string): void;
91
+ /** @internal Encodes the stored migration with the referenced signature inline, if required. */
92
+ export declare function buildBlueRefinance(action: RefinanceAction, requests: readonly SignatureRequest[]): Readonly<Transaction<BlueRefinanceAction>>;
93
+ export {};