@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1

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 (243) hide show
  1. package/MIGRATION-v5-to-v6.md +404 -0
  2. package/README.md +127 -69
  3. package/lib/cjs/abis.d.ts +210 -0
  4. package/lib/cjs/abis.js +142 -2
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -61
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +86 -0
  8. package/lib/cjs/actions/blue/common.js +205 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -112
  10. package/lib/cjs/actions/blue/refinance.js +97 -199
  11. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  12. package/lib/cjs/actions/blue/repay.js +56 -118
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  15. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  16. package/lib/cjs/actions/blue/supply.js +94 -70
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  18. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
  22. package/lib/cjs/actions/blue/withdraw.js +101 -97
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  25. package/lib/cjs/actions/bundles/common.d.ts +200 -0
  26. package/lib/cjs/actions/bundles/common.js +469 -0
  27. package/lib/cjs/actions/bundles/index.d.ts +3 -0
  28. package/lib/cjs/actions/bundles/index.js +23 -0
  29. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  30. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
  31. package/lib/cjs/actions/index.d.ts +2 -1
  32. package/lib/cjs/actions/index.js +1 -0
  33. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  34. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  35. package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
  36. package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
  37. package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
  38. package/lib/cjs/actions/midnight/takeLend.js +2 -2
  39. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  40. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  41. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  42. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  43. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  44. package/lib/cjs/actions/requirements/blue/index.js +1 -0
  45. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  46. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  48. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
  49. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  50. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
  51. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  52. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
  53. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  54. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  55. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  56. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  57. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  58. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
  59. package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
  60. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  61. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  62. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  63. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
  64. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  65. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  66. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  67. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  68. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  69. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  70. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  71. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  72. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  73. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  74. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  75. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  76. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  77. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  78. package/lib/cjs/bundler/actions.d.ts +4 -4
  79. package/lib/cjs/bundler/actions.js +4 -4
  80. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  81. package/lib/cjs/client/morphoViemExtension.js +2 -1
  82. package/lib/cjs/entities/blue/blue.d.ts +806 -374
  83. package/lib/cjs/entities/blue/blue.js +449 -696
  84. package/lib/cjs/entities/index.d.ts +2 -0
  85. package/lib/cjs/entities/index.js +15 -0
  86. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  87. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  88. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
  89. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  90. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  91. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  92. package/lib/cjs/entities/requirements/index.js +17 -0
  93. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
  94. package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
  95. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
  96. package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
  97. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
  98. package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
  99. package/lib/cjs/helpers/index.d.ts +2 -1
  100. package/lib/cjs/helpers/index.js +5 -1
  101. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  102. package/lib/cjs/helpers/referralFee.js +35 -0
  103. package/lib/cjs/helpers/slippage.d.ts +90 -1
  104. package/lib/cjs/helpers/slippage.js +123 -0
  105. package/lib/cjs/helpers/validate.d.ts +16 -23
  106. package/lib/cjs/helpers/validate.js +41 -48
  107. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  108. package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
  109. package/lib/cjs/index.d.ts +1 -0
  110. package/lib/cjs/index.js +1 -0
  111. package/lib/cjs/types/action.d.ts +303 -210
  112. package/lib/cjs/types/action.js +29 -2
  113. package/lib/cjs/types/client.d.ts +9 -0
  114. package/lib/cjs/types/error.d.ts +193 -18
  115. package/lib/cjs/types/error.js +267 -24
  116. package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
  117. package/lib/esm/abis.d.ts +210 -0
  118. package/lib/esm/abis.js +140 -0
  119. package/lib/esm/actions/blue/borrow.d.ts +62 -61
  120. package/lib/esm/actions/blue/borrow.js +48 -80
  121. package/lib/esm/actions/blue/common.d.ts +86 -0
  122. package/lib/esm/actions/blue/common.js +193 -0
  123. package/lib/esm/actions/blue/refinance.d.ts +72 -112
  124. package/lib/esm/actions/blue/refinance.js +98 -200
  125. package/lib/esm/actions/blue/repay.d.ts +72 -68
  126. package/lib/esm/actions/blue/repay.js +56 -118
  127. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  128. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  129. package/lib/esm/actions/blue/supply.d.ts +62 -45
  130. package/lib/esm/actions/blue/supply.js +94 -70
  131. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  132. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  133. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  134. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
  135. package/lib/esm/actions/blue/withdraw.d.ts +68 -79
  136. package/lib/esm/actions/blue/withdraw.js +100 -96
  137. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  138. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  139. package/lib/esm/actions/bundles/common.d.ts +200 -0
  140. package/lib/esm/actions/bundles/common.js +457 -0
  141. package/lib/esm/actions/bundles/index.d.ts +3 -0
  142. package/lib/esm/actions/bundles/index.js +2 -0
  143. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  144. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  145. package/lib/esm/actions/index.d.ts +2 -1
  146. package/lib/esm/actions/index.js +1 -0
  147. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  148. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  149. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  150. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  151. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  152. package/lib/esm/actions/midnight/takeLend.js +2 -2
  153. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  154. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  155. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  156. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  157. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  158. package/lib/esm/actions/requirements/blue/index.js +1 -0
  159. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  160. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  161. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  162. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
  163. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  164. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
  165. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  166. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
  167. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  168. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  169. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  170. package/lib/esm/actions/requirements/encode/index.js +1 -0
  171. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  172. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
  173. package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
  174. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  175. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  176. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  177. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
  178. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  179. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  180. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  181. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  182. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  183. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  184. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  185. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  186. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  187. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  188. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  189. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  190. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  191. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  192. package/lib/esm/bundler/actions.d.ts +4 -4
  193. package/lib/esm/bundler/actions.js +4 -4
  194. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  195. package/lib/esm/client/morphoViemExtension.js +2 -1
  196. package/lib/esm/entities/blue/blue.d.ts +806 -374
  197. package/lib/esm/entities/blue/blue.js +455 -702
  198. package/lib/esm/entities/index.d.ts +2 -0
  199. package/lib/esm/entities/index.js +1 -0
  200. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  201. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
  203. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  204. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  205. package/lib/esm/entities/requirements/index.d.ts +1 -0
  206. package/lib/esm/entities/requirements/index.js +1 -0
  207. package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
  208. package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
  209. package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
  210. package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
  211. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
  212. package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
  213. package/lib/esm/helpers/index.d.ts +2 -1
  214. package/lib/esm/helpers/index.js +2 -1
  215. package/lib/esm/helpers/referralFee.d.ts +21 -0
  216. package/lib/esm/helpers/referralFee.js +31 -0
  217. package/lib/esm/helpers/slippage.d.ts +90 -1
  218. package/lib/esm/helpers/slippage.js +122 -2
  219. package/lib/esm/helpers/validate.d.ts +16 -23
  220. package/lib/esm/helpers/validate.js +40 -48
  221. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  222. package/lib/esm/helpers/validateRequirementSpender.js +3 -0
  223. package/lib/esm/index.d.ts +1 -0
  224. package/lib/esm/index.js +1 -0
  225. package/lib/esm/types/action.d.ts +303 -210
  226. package/lib/esm/types/action.js +28 -2
  227. package/lib/esm/types/client.d.ts +9 -0
  228. package/lib/esm/types/error.d.ts +193 -18
  229. package/lib/esm/types/error.js +252 -21
  230. package/lib/esm/types/sharedLiquidity.d.ts +0 -7
  231. package/package.json +6 -5
  232. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  233. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  234. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  235. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  236. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  237. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  238. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  239. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  240. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  241. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  242. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  243. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,137 +1,75 @@
1
- import { getChainAddresses } from "@morpho-org/blue-sdk";
2
1
  import { deepFreeze } from "@morpho-org/morpho-ts";
3
2
  import { maxUint256 } from "viem";
4
- import { BundlerAction } from "../../bundler/index.js";
5
- import { addTransactionMetadata } from "../../helpers/index.js";
6
- import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
7
- import { resolveRepayFunding } from "./resolveRepayFunding.js";
3
+ import { blueRepayWithdrawCollateral } from "./repayWithdrawCollateral.js";
8
4
  /**
9
- * Prepares a repay transaction for a Morpho Blue market.
5
+ * Encodes a pure repayment through BlueBundlesV1.
10
6
  *
11
- * Routed through bundler3 via `GeneralAdapter1`. All amount arithmetic is done upstream (see
12
- * `MorphoBlue.repay`); this builder just assembles the bundle from the pre-resolved
13
- * {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional native wrapping
14
- * (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()` before the
15
- * repay; the loan token must be the chain's wNative):
7
+ * Delegates to {@link blueRepayWithdrawCollateral} with a zero collateral-withdrawal leg and an
8
+ * unrestricted LTV cap. BlueBundlesV1 refunds unused `maxRepayAssets` funding.
16
9
  *
17
- * - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
18
- * additive like `blueSupply`), pulling `amount` ERC-20 and wrapping `nativeAmount`. No residual.
19
- * - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
20
- * ERC-20 (already net of native) and wrapping `nativeAmount`. Residual loan tokens are skimmed
21
- * back to `receiver` after the call.
22
- *
23
- * Uses `maxSharePrice` to protect against share price manipulation between construction and execution.
24
- *
25
- * @param params.market.chainId - The chain the market lives on.
26
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
27
- * @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
28
- * @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
29
- * @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
30
- * total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
31
- * @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
32
- * Requires the loan token to be the chain's wNative.
33
- * @param params.args.onBehalf - Address whose Morpho debt is being repaid.
34
- * @param params.args.receiver - Address that receives residual loan tokens in shares mode.
35
- * @param params.args.maxSharePrice - Maximum acceptable repay share price (in ray). Slippage
36
- * protection.
37
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
38
- * loan-token transfer.
39
- * @param params.metadata - Optional analytics metadata attached to the bundle.
40
- * @returns A deep-frozen `Transaction<BlueRepayAction>` with `to`, `value` (= `nativeAmount`),
41
- * `data`, and the typed `action` discriminator the simulation layer consumes.
42
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or the total funding is zero.
43
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
44
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
45
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
46
- * `transferAmount !== amount + nativeAmount`.
47
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
48
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
49
- * the chain's wNative.
50
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
51
- * is provided and the signed asset differs from `marketParams.loanToken`.
52
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
53
- * is provided and the signed amount differs from the ERC-20 amount pulled.
54
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
55
- * signature is missing its expiration.
10
+ * @param params - Repay encoding parameters.
11
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
12
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
13
+ * @param params.args.userAddress - User whose debt is repaid.
14
+ * @param params.args.repayAssets - Exact assets repaid, exclusive with shares.
15
+ * @param params.args.repayShares - Exact shares or `maxUint256` for full repay.
16
+ * @param params.args.maxRepayAssets - Gross repayment and fee funding cap.
17
+ * @param params.args.nativeAmount - Exclusive native repay funding.
18
+ * @param params.args.deadline - Final call deadline in Unix seconds.
19
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
20
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
21
+ * @param params.args.requirementSignature - Optional loan-token signature.
22
+ * @param params.metadata - Optional transaction metadata.
23
+ * @returns A deep-frozen `Readonly<Transaction<BlueRepayAction>>` whose calldata invokes the
24
+ * combined BlueBundlesV1 entrypoint with a zero collateral-withdrawal leg.
25
+ * @throws {NegativeInputError} when an amount or fee is negative.
26
+ * @throws {MutuallyExclusiveRepayAmountsError} when assets and shares are both nonzero.
27
+ * @throws {NonPositiveInputError} when repayment, funding, or deadline is not positive.
28
+ * @throws {MaxRepayAssetsBelowRepayAssetsError} when funding cannot cover repayment and fees.
29
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
30
+ * @throws {ChainWNativeMissingError} when native funding is requested without a registered wNative.
31
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another loan token.
32
+ * @throws {UnexpectedRequirementSignatureError} when native funding also supplies a token signature.
33
+ * @throws {DepositOwnerMismatchError} when the signed owner differs from `userAddress`.
34
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the loan token.
35
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `maxRepayAssets`.
36
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
37
+ * @throws {BundlesRequirementSignatureMismatchError} when a signature cannot be encoded safely.
38
+ * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
39
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
40
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
41
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
56
42
  * @example
57
43
  * ```ts
44
+ * import { markets } from "@morpho-org/morpho-test";
58
45
  * import { blueRepay } from "@morpho-org/morpho-sdk";
46
+ * import { zeroAddress } from "viem";
47
+ * import { mainnet } from "viem/chains";
59
48
  *
60
- * // Repay 0.5 loan-asset units, 0.2 of them funded by wrapping native ETH.
61
49
  * const tx = blueRepay({
62
- * market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
50
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
63
51
  * args: {
64
- * amount: 300_000_000_000_000_000n, // ERC-20 part
65
- * nativeAmount: 200_000_000_000_000_000n, // wrapped ETH part
66
- * transferAmount: 500_000_000_000_000_000n, // total repaid = amount + nativeAmount
67
- * onBehalf: borrower,
68
- * receiver: borrower,
69
- * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
52
+ * userAddress: zeroAddress,
53
+ * repayAssets: 1_000_000n,
54
+ * repayShares: 0n,
55
+ * maxRepayAssets: 1_000_000n,
56
+ * deadline: 1_900_000_000n,
70
57
  * },
71
58
  * });
72
- * // tx.value === 200_000_000_000_000_000n
59
+ * // tx satisfies Readonly<Transaction<BlueRepayAction>>
73
60
  * ```
74
61
  */
75
- export const blueRepay = ({ market: { chainId, marketParams }, args, metadata, }) => {
76
- const { amount = 0n, shares = 0n, nativeAmount = 0n, transferAmount, onBehalf, receiver, maxSharePrice, requirementSignature, } = args;
77
- const { isSharesMode, repayAssets, repayShares, erc20Amount } = resolveRepayFunding({ amount, shares, nativeAmount, transferAmount, maxSharePrice }, marketParams.id);
78
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
79
- const actions = buildAssetFundingActions({
80
- chainId,
81
- asset: marketParams.loanToken,
82
- erc20Amount,
83
- nativeAmount,
84
- requirementSignature,
85
- });
86
- actions.push({
87
- type: "morphoRepay",
88
- args: [
89
- marketParams,
90
- repayAssets,
91
- repayShares,
92
- maxSharePrice,
93
- onBehalf,
94
- [],
95
- false,
96
- ],
62
+ export const blueRepay = (params) => {
63
+ const transaction = blueRepayWithdrawCollateral({
64
+ ...params,
65
+ args: {
66
+ ...params.args,
67
+ collateralAssets: 0n,
68
+ maxLtv: maxUint256,
69
+ },
97
70
  });
98
- // Skim residual loan tokens back to the payer when repaying by shares.
99
- // In shares mode, transferAmount is an upper-bound estimate; morphoRepay
100
- // consumes only the exact amount needed, leaving a residual in the adapter.
101
- if (isSharesMode) {
102
- actions.push({
103
- type: "erc20Transfer",
104
- args: [
105
- marketParams.loanToken,
106
- receiver,
107
- maxUint256,
108
- generalAdapter1,
109
- false,
110
- ],
111
- });
112
- }
113
- let tx = {
114
- ...BundlerAction.encodeBundle(chainId, actions),
115
- value: nativeAmount,
116
- };
117
- if (metadata) {
118
- tx = addTransactionMetadata(tx, metadata);
119
- }
120
71
  return deepFreeze({
121
- ...tx,
122
- action: {
123
- type: "blueRepay",
124
- args: {
125
- market: marketParams.id,
126
- assets: repayAssets,
127
- shares: repayShares,
128
- // Total loan tokens routed to the adapter: ERC-20 pulled + native wrapped.
129
- transferAmount: erc20Amount + nativeAmount,
130
- onBehalf,
131
- receiver,
132
- maxSharePrice,
133
- nativeAmount: nativeAmount > 0n ? nativeAmount : undefined,
134
- },
135
- },
72
+ ...transaction,
73
+ action: { ...transaction.action, type: "blueRepay" },
136
74
  });
137
75
  };
@@ -1,111 +1,106 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import { type Address } from "viem";
3
- import { type AuthorizationRequirementSignature, type BlueRepayWithdrawCollateralAction, type Metadata, type PermitRequirementSignature, type RepayActionAmountArgs, type Transaction } from "../../types/index.js";
3
+ import { type AuthorizationRequirementSignature, type BlueRepayWithdrawCollateralAction, type BundlesTokenRequirementSignature, type Metadata, type Transaction } from "../../types/index.js";
4
4
  /** Parameters for {@link blueRepayWithdrawCollateral}. */
5
5
  export interface BlueRepayWithdrawCollateralParams {
6
- market: {
6
+ /** Chain and scoped Morpho Blue market. */
7
+ readonly market: {
7
8
  readonly chainId: number;
8
9
  readonly marketParams: MarketParams;
9
10
  };
10
- args: RepayActionAmountArgs & {
11
- /** Amount of collateral to withdraw. */
12
- withdrawAmount: bigint;
13
- /** Address whose debt is being repaid. */
14
- onBehalf: Address;
15
- /** Receives withdrawn collateral and residual loan tokens in shares mode. */
16
- receiver: Address;
17
- /** Maximum repay share price (in ray). Protects against share price manipulation. */
18
- maxSharePrice: bigint;
19
- /** Optional pre-signed permit/permit2 approval for the loan-token transfer. */
20
- requirementSignature?: PermitRequirementSignature;
21
- /**
22
- * Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is
23
- * prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
24
- * standalone `setAuthorization` transaction.
25
- */
26
- authorizationSignature?: AuthorizationRequirementSignature;
11
+ /** Direct BlueBundlesV1 combined-operation arguments. */
12
+ readonly args: {
13
+ /** User whose debt and collateral position is changed. */
14
+ readonly userAddress: Address;
15
+ /** Exact assets repaid, exclusive with `repayShares`. */
16
+ readonly repayAssets: bigint;
17
+ /** Exact shares repaid; use `maxUint256` for a saturated full repay. */
18
+ readonly repayShares: bigint;
19
+ /** Maximum gross loan-token funding, including referral fees. */
20
+ readonly maxRepayAssets: bigint;
21
+ /** Collateral assets withdrawn; zero selects a pure repay. */
22
+ readonly collateralAssets: bigint;
23
+ /** Maximum post-operation LTV enforced by BlueBundlesV1. */
24
+ readonly maxLtv: bigint;
25
+ /** Full native repay funding; must equal `maxRepayAssets`. */
26
+ readonly nativeAmount?: bigint;
27
+ /** Final call deadline in Unix seconds. */
28
+ readonly deadline: bigint;
29
+ /** Optional WAD-scaled referral fee, strictly below 100%. */
30
+ readonly referralFeePct?: bigint;
31
+ /** Recipient required when `referralFeePct` is positive. */
32
+ readonly referralFeeRecipient?: Address;
33
+ /** Optional loan-token ERC-2612 or Permit2 SignatureTransfer result. */
34
+ readonly requirementSignature?: BundlesTokenRequirementSignature;
35
+ /** Optional Morpho authorization signature for BlueBundlesV1. */
36
+ readonly authorizationSignature?: AuthorizationRequirementSignature;
27
37
  };
28
- metadata?: Metadata;
38
+ /** Optional transaction metadata suffix. */
39
+ readonly metadata?: Metadata;
29
40
  }
30
41
  /**
31
- * Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
42
+ * Encodes a direct BlueBundlesV1 repay and/or collateral-withdrawal transaction.
32
43
  *
33
- * Routed through bundler3. The bundle order is critical:
44
+ * `maxRepayAssets` funds the live repayment plus fee and BlueBundlesV1 refunds the unused amount.
45
+ * A saturated `repayShares = maxUint256` closes the live remaining debt. This route has no
46
+ * Bundler3 share-price bound or `slippageTolerance` input.
34
47
  *
35
- * 1. ERC-20 transfer of the loan token to `GeneralAdapter1`.
36
- * 2. `morphoRepay` — reduces debt **first**.
37
- * 3. `morphoWithdrawCollateral` — then withdraws collateral.
38
- *
39
- * If the order were reversed, Morpho would revert because the position would be insolvent at the
40
- * time of the withdraw. All amount arithmetic is done upstream (see
41
- * `MorphoBlue.repayWithdrawCollateral`); this builder just assembles the bundle from the
42
- * pre-resolved {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional
43
- * native wrapping (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()`
44
- * before the repay; the loan token must be the chain's wNative):
45
- *
46
- * - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
47
- * additive like `blueSupply`), pulling `amount` ERC-20.
48
- * - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
49
- * ERC-20 (already net of native); residual loan tokens are skimmed back to `receiver`.
50
- *
51
- * Prerequisites: ERC-20 approval for the loan token to `GeneralAdapter1` (for the repay) **and**
52
- * `GeneralAdapter1` must be authorized on Morpho (for the withdraw). Passing an
53
- * `authorizationSignature` prepends the authorization in-bundle instead.
54
- *
55
- * @param params.market.chainId - The chain the market lives on.
56
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
57
- * @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
58
- * @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
59
- * @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
60
- * total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
61
- * @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
62
- * Requires the loan token to be the chain's wNative.
63
- * @param params.args.withdrawAmount - Amount of collateral to withdraw after the repay leg
64
- * completes.
65
- * @param params.args.onBehalf - Address whose Morpho debt is being repaid.
66
- * @param params.args.receiver - Address that receives the withdrawn collateral and any residual
67
- * loan tokens in shares mode.
68
- * @param params.args.maxSharePrice - Maximum acceptable repay share price (in ray). Slippage
69
- * protection.
70
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
71
- * loan-token transfer.
72
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
73
- * a `setAuthorizationWithSig` call is prepended to the bundle.
74
- * @param params.metadata - Optional analytics metadata attached to the bundle.
75
- * @returns A deep-frozen `Transaction<BlueRepayWithdrawCollateralAction>` with `to`,
76
- * `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes.
77
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n`, the total funding is zero, or
78
- * `withdrawAmount <= 0n`.
79
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
80
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
81
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
82
- * `transferAmount !== amount + nativeAmount`.
83
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
84
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
85
- * the chain's wNative.
86
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
87
- * is provided and the signed asset differs from `marketParams.loanToken`.
88
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
89
- * is provided and the signed amount differs from the ERC-20 amount pulled.
90
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
91
- * signature is missing its expiration.
48
+ * @param params - Combined-operation encoding parameters.
49
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
50
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
51
+ * @param params.args.userAddress - User whose position is changed.
52
+ * @param params.args.repayAssets - Exact assets repaid, exclusive with shares.
53
+ * @param params.args.repayShares - Exact shares or `maxUint256` for full repay.
54
+ * @param params.args.maxRepayAssets - Gross repayment and fee funding cap.
55
+ * @param params.args.collateralAssets - Collateral withdrawn, or zero.
56
+ * @param params.args.maxLtv - Maximum post-operation LTV.
57
+ * @param params.args.nativeAmount - Exclusive native repay funding.
58
+ * @param params.args.deadline - Final call deadline in Unix seconds.
59
+ * @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
60
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
61
+ * @param params.args.requirementSignature - Optional loan-token signature.
62
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
63
+ * @param params.metadata - Optional transaction metadata.
64
+ * @returns A deep-frozen `Readonly<Transaction<BlueRepayWithdrawCollateralAction>>`
65
+ * whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
66
+ * @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
67
+ * @throws {MutuallyExclusiveRepayAmountsError} when assets and shares are both nonzero.
68
+ * @throws {NonPositiveInputError} when both operation legs are zero, repay funding is absent, or the deadline is invalid.
69
+ * @throws {MaxRepayAssetsBelowRepayAssetsError} when exact-asset funding cannot cover the repayment and referral fee.
70
+ * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
71
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
72
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
73
+ * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
74
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
75
+ * @throws {UnexpectedRequirementSignatureError} when a signature is supplied for an inactive leg.
76
+ * @throws {DepositOwnerMismatchError} when a signed owner differs from `userAddress`.
77
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the loan token.
78
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `maxRepayAssets`.
79
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
80
+ * @throws {BundlesRequirementSignatureMismatchError} when a signature cannot be bound safely.
81
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
82
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
92
83
  * @example
93
84
  * ```ts
85
+ * import { markets } from "@morpho-org/morpho-test";
94
86
  * import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
87
+ * import { maxUint256, zeroAddress } from "viem";
88
+ * import { mainnet } from "viem/chains";
95
89
  *
90
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
96
91
  * const tx = blueRepayWithdrawCollateral({
97
- * market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
92
+ * market: { chainId: mainnet.id, marketParams },
98
93
  * args: {
99
- * shares: 500_000_000_000_000_000_000_000n,
100
- * transferAmount: 310_000_000_000_000_000n, // ERC-20 pulled (net of native)
101
- * nativeAmount: 200_000_000_000_000_000n, // 0.2 funded by wrapping native ETH
102
- * withdrawAmount: 1_000_000_000_000_000_000n,
103
- * onBehalf: borrower,
104
- * receiver: borrower,
105
- * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
94
+ * userAddress: zeroAddress,
95
+ * repayAssets: 1_000_000n,
96
+ * repayShares: 0n,
97
+ * maxRepayAssets: 1_000_000n,
98
+ * collateralAssets: 0n,
99
+ * maxLtv: maxUint256,
100
+ * deadline: 1_900_000_000n,
106
101
  * },
107
102
  * });
108
- * // tx.value === 200_000_000_000_000_000n
103
+ * // tx satisfies Readonly<Transaction<BlueRepayWithdrawCollateralAction>>
109
104
  * ```
110
105
  */
111
- export declare const blueRepayWithdrawCollateral: ({ market: { chainId, marketParams }, args, metadata, }: BlueRepayWithdrawCollateralParams) => Readonly<Transaction<BlueRepayWithdrawCollateralAction>>;
106
+ export declare const blueRepayWithdrawCollateral: (params: BlueRepayWithdrawCollateralParams) => Readonly<Transaction<BlueRepayWithdrawCollateralAction>>;