@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
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.finalizeBlueBundlesV1Transaction = exports.selectBlueBundlesV1RequirementSignatures = exports.getBlueBundlesV1ReferralFeeAssets = exports.getBlueBundlesV1PenaltyAssets = exports.getBlueBundlesV1PublicAllocations = exports.getBlueBundlesV1SignedAuthorization = exports.getBlueBundlesV1TokenPermit = exports.validateBlueBundlesV1NativeFunding = exports.normalizeBlueBundlesV1CommonParams = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const index_js_1 = require("../../helpers/index.js");
8
+ const validate_js_1 = require("../../helpers/validate.js");
9
+ const index_js_2 = require("../../types/index.js");
10
+ const common_js_1 = require("../bundles/common.js");
11
+ const index_js_3 = require("../bundles/index.js");
12
+ const EMPTY_SIGNED_AUTHORIZATION = {
13
+ signature: { v: 0, r: viem_1.zeroHash, s: viem_1.zeroHash },
14
+ nonce: 0n,
15
+ deadline: 0n,
16
+ };
17
+ /** @internal */
18
+ const normalizeBlueBundlesV1CommonParams = (params) => {
19
+ const normalized = (0, common_js_1.normalizeBundlesCommonParams)(params);
20
+ return {
21
+ referralFeePct: normalized.referralFeePct,
22
+ referralFeeRecipient: normalized.referralFeeRecipient,
23
+ };
24
+ };
25
+ exports.normalizeBlueBundlesV1CommonParams = normalizeBlueBundlesV1CommonParams;
26
+ /** @internal */
27
+ const validateBlueBundlesV1NativeFunding = (params) => {
28
+ const nativeAmount = params.nativeAmount ?? 0n;
29
+ if (nativeAmount < 0n) {
30
+ throw new index_js_2.NegativeInputError("nativeAmount", nativeAmount);
31
+ }
32
+ if (nativeAmount === 0n)
33
+ return 0n;
34
+ (0, index_js_1.validateNativeAsset)(params.chainId, params.token);
35
+ if (nativeAmount !== params.fundedAmount) {
36
+ throw new index_js_2.NativeFundingAmountMismatchError(nativeAmount, params.fundedAmount);
37
+ }
38
+ if (params.requirementSignature != null) {
39
+ throw new index_js_2.UnexpectedRequirementSignatureError(params.requirementSignature.action.type);
40
+ }
41
+ return nativeAmount;
42
+ };
43
+ exports.validateBlueBundlesV1NativeFunding = validateBlueBundlesV1NativeFunding;
44
+ /** @internal */
45
+ const getBlueBundlesV1TokenPermit = (params) => {
46
+ return (0, index_js_3.getBundlesTokenPermit)({
47
+ userAddress: params.userAddress,
48
+ token: params.token,
49
+ spender: (0, morpho_ts_1.getChainAddress)(params.chainId, "bundles.blueBundlesV1"),
50
+ amount: params.amount,
51
+ requirementSignature: params.requirementSignature,
52
+ });
53
+ };
54
+ exports.getBlueBundlesV1TokenPermit = getBlueBundlesV1TokenPermit;
55
+ /** @internal */
56
+ const getBlueBundlesV1SignedAuthorization = (params) => {
57
+ const { authorizationSignature } = params;
58
+ if (authorizationSignature == null)
59
+ return EMPTY_SIGNED_AUTHORIZATION;
60
+ const authorized = (0, morpho_ts_1.getChainAddress)(params.chainId, "bundles.blueBundlesV1");
61
+ if (!(0, viem_1.isAddressEqual)(authorizationSignature.args.owner, params.userAddress)) {
62
+ throw new index_js_2.DepositOwnerMismatchError(params.userAddress, authorizationSignature.args.owner);
63
+ }
64
+ if (!(0, viem_1.isAddressEqual)(authorizationSignature.args.authorized, authorized)) {
65
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
66
+ field: "authorized",
67
+ expected: authorized,
68
+ actual: authorizationSignature.args.authorized,
69
+ });
70
+ }
71
+ if (!(0, viem_1.isAddressEqual)(authorizationSignature.action.args.authorized, authorized)) {
72
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
73
+ field: "authorized",
74
+ expected: authorized,
75
+ actual: authorizationSignature.action.args.authorized,
76
+ });
77
+ }
78
+ if (!authorizationSignature.args.isAuthorized) {
79
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
80
+ field: "isAuthorized",
81
+ expected: "true",
82
+ actual: String(authorizationSignature.args.isAuthorized),
83
+ });
84
+ }
85
+ if (!authorizationSignature.action.args.isAuthorized) {
86
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
87
+ field: "isAuthorized",
88
+ expected: "true",
89
+ actual: String(authorizationSignature.action.args.isAuthorized),
90
+ });
91
+ }
92
+ if (authorizationSignature.action.args.deadline !==
93
+ authorizationSignature.args.deadline) {
94
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
95
+ field: "deadline",
96
+ expected: String(authorizationSignature.args.deadline),
97
+ actual: String(authorizationSignature.action.args.deadline),
98
+ });
99
+ }
100
+ const serializedSignature = authorizationSignature.args.signature;
101
+ let parsed;
102
+ try {
103
+ parsed =
104
+ (0, viem_1.size)(serializedSignature) === 64
105
+ ? (0, viem_1.compactSignatureToSignature)((0, viem_1.parseCompactSignature)(serializedSignature))
106
+ : (0, viem_1.parseSignature)(serializedSignature);
107
+ }
108
+ catch (cause) {
109
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
110
+ field: "signature",
111
+ expected: "a 64-byte compact or 65-byte serialized ECDSA signature",
112
+ actual: serializedSignature,
113
+ cause,
114
+ });
115
+ }
116
+ const v = parsed.v ??
117
+ (parsed.yParity == null ? undefined : BigInt(parsed.yParity + 27));
118
+ if (v == null) {
119
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
120
+ field: "signature",
121
+ expected: "a signature containing v or yParity",
122
+ actual: serializedSignature,
123
+ });
124
+ }
125
+ return {
126
+ signature: { v: Number(v), r: parsed.r, s: parsed.s },
127
+ nonce: authorizationSignature.args.nonce,
128
+ deadline: authorizationSignature.args.deadline,
129
+ };
130
+ };
131
+ exports.getBlueBundlesV1SignedAuthorization = getBlueBundlesV1SignedAuthorization;
132
+ /** @internal */
133
+ const getBlueBundlesV1PublicAllocations = (reallocations, targetMarketParams) => {
134
+ (0, validate_js_1.validateVaultV2BlueReallocations)(reallocations, targetMarketParams.id);
135
+ // Hoisted out of the map: idle sources all reference the same synthetic idle market, whose
136
+ // construction eagerly hashes the market id — computing it once per call instead of per entry.
137
+ const idleMarketParams = blue_sdk_1.MarketParams.idle(targetMarketParams.loanToken);
138
+ return reallocations.map((reallocation) => {
139
+ if (reallocation.from.type === "market" &&
140
+ !(0, viem_1.isAddressEqual)(reallocation.from.marketParams.loanToken, targetMarketParams.loanToken)) {
141
+ throw new index_js_2.ReallocationLoanTokenMismatchError(targetMarketParams.loanToken, reallocation.from.marketParams.loanToken);
142
+ }
143
+ return {
144
+ vault: reallocation.vault,
145
+ adapter: reallocation.to.adapter,
146
+ marketParams: targetMarketParams,
147
+ fromIdle: reallocation.from.type === "idle",
148
+ sourceAdapter: reallocation.from.type === "market"
149
+ ? reallocation.from.adapter
150
+ : viem_1.zeroAddress,
151
+ sourceMarketParams: reallocation.from.type === "market"
152
+ ? reallocation.from.marketParams
153
+ : idleMarketParams,
154
+ assets: reallocation.assets,
155
+ penalty: reallocation.penalty,
156
+ };
157
+ });
158
+ };
159
+ exports.getBlueBundlesV1PublicAllocations = getBlueBundlesV1PublicAllocations;
160
+ /** @internal */
161
+ const getBlueBundlesV1PenaltyAssets = (reallocations) => reallocations.reduce((total, reallocation) => total +
162
+ blue_sdk_1.VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
163
+ exports.getBlueBundlesV1PenaltyAssets = getBlueBundlesV1PenaltyAssets;
164
+ /** @internal */
165
+ const getBlueBundlesV1ReferralFeeAssets = (assets, referralFeePct) => referralFeePct === 0n
166
+ ? 0n
167
+ : blue_sdk_1.MathLib.mulDivDown(assets, referralFeePct, blue_sdk_1.MathLib.WAD - referralFeePct);
168
+ exports.getBlueBundlesV1ReferralFeeAssets = getBlueBundlesV1ReferralFeeAssets;
169
+ /** @internal */
170
+ const selectBlueBundlesV1RequirementSignatures = (signatures, accepts) => {
171
+ const { permit, permit2SignatureTransfer, authorization } = (0, index_js_2.selectRequirementSignatures)(signatures, {
172
+ permit: accepts.token,
173
+ permit2SignatureTransfer: accepts.token,
174
+ authorization: accepts.authorization,
175
+ });
176
+ if (permit?.action.type === "permit2") {
177
+ throw new index_js_2.BundlesRequirementSignatureMismatchError({
178
+ field: "type",
179
+ expected: "permit or permit2SignatureTransfer",
180
+ actual: permit.action.type,
181
+ });
182
+ }
183
+ if (permit != null && permit2SignatureTransfer != null) {
184
+ throw new index_js_2.AmbiguousRequirementSignaturesError("permit", 2);
185
+ }
186
+ return {
187
+ token: permit ??
188
+ permit2SignatureTransfer,
189
+ authorization,
190
+ };
191
+ };
192
+ exports.selectBlueBundlesV1RequirementSignatures = selectBlueBundlesV1RequirementSignatures;
193
+ /** @internal */
194
+ const finalizeBlueBundlesV1Transaction = (params) => {
195
+ let transaction = {
196
+ to: (0, morpho_ts_1.getChainAddress)(params.common.chainId, "bundles.blueBundlesV1"),
197
+ value: params.value,
198
+ data: params.data,
199
+ };
200
+ if (params.common.metadata != null) {
201
+ transaction = (0, index_js_1.addTransactionMetadata)(transaction, params.common.metadata);
202
+ }
203
+ return (0, morpho_ts_1.deepFreeze)({ ...transaction, action: params.action });
204
+ };
205
+ exports.finalizeBlueBundlesV1Transaction = finalizeBlueBundlesV1Transaction;
@@ -1,135 +1,95 @@
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 BlueReallocationPlan, type BlueRefinanceAction, type Metadata, type Transaction } from "../../types/index.js";
3
+ import { type AuthorizationRequirementSignature, type BlueRefinanceAction, type Metadata, type Transaction, type VaultV2BlueReallocation } from "../../types/index.js";
4
4
  /** Parameters for {@link blueRefinance}. */
5
5
  export interface BlueRefinanceParams {
6
- source: {
6
+ /** Chain plus source and destination Morpho Blue markets. */
7
+ readonly market: {
7
8
  readonly chainId: number;
8
- readonly marketParams: MarketParams;
9
+ readonly sourceMarketParams: MarketParams;
10
+ readonly destinationMarketParams: MarketParams;
9
11
  };
10
- target: {
11
- readonly marketParams: MarketParams;
12
- };
13
- args: {
14
- /** Address whose position is refinanced from the source to the target market. */
15
- user: Address;
16
- /** Amount of collateral moved from the source market to the target market. */
17
- collateralAmount: bigint;
18
- /**
19
- * Loan assets to borrow on the target. Assets mode: the exact borrow (exclusive with
20
- * `borrowShares`). Shares mode: the positive overshoot covering accrual + slippage; omitting it
21
- * throws {@link RefinanceSharesMissingBorrowAssetsError}.
22
- */
23
- borrowAssets?: bigint;
24
- /** Source borrow shares to repay (immune to mid-tx accrual); exclusive with `borrowAssets`. */
25
- borrowShares?: bigint;
26
- /** Minimum borrow share price on the target market (in ray). */
27
- minBorrowSharePrice: bigint;
28
- /** Maximum repay share price on the source market (in ray); must be > 0 when a repay leg exists. */
29
- maxRepaySharePrice: bigint;
12
+ /** Direct BlueBundlesV1 full-position migration arguments. */
13
+ readonly args: {
30
14
  /**
31
- * Homogeneous Vault V1 or Vault V2 reallocations into the target market.
32
- * Vault V1 inputs are deprecated for high-level Blue writes; use Vault V2 for new integrations.
15
+ * Intended user used to bind an optional Morpho authorization and action metadata. This value is
16
+ * not encoded; the caller must send from the same account because BlueBundlesV1 always migrates
17
+ * `msg.sender`.
33
18
  */
34
- targetReallocations?: BlueReallocationPlan;
35
- /**
36
- * Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is
37
- * prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
38
- * standalone `setAuthorization` transaction.
39
- */
40
- authorizationSignature?: AuthorizationRequirementSignature;
19
+ readonly userAddress: Address;
20
+ /** Maximum destination LTV enforced by BlueBundlesV1. */
21
+ readonly maxLtv: bigint;
22
+ /** Optional Vault V2 reallocations into the destination market. */
23
+ readonly reallocations?: Iterable<VaultV2BlueReallocation>;
24
+ /** Final call deadline in Unix seconds. */
25
+ readonly deadline: bigint;
26
+ /** Optional WAD-scaled referral fee, strictly below 100%. */
27
+ readonly referralFeePct?: bigint;
28
+ /** Recipient required when `referralFeePct` is positive. */
29
+ readonly referralFeeRecipient?: Address;
30
+ /** Optional Morpho authorization signature for BlueBundlesV1. */
31
+ readonly authorizationSignature?: AuthorizationRequirementSignature;
41
32
  };
42
- metadata?: Metadata;
33
+ /** Optional transaction metadata suffix. */
34
+ readonly metadata?: Metadata;
43
35
  }
44
36
  /**
45
- * Prepares an atomic refinance migrating a Morpho Blue position to another market on the same
46
- * chain that shares the same loan and collateral tokens.
47
- *
48
- * Strategy: flash-collateral via the target's `onMorphoSupplyCollateral` callback. The collateral
49
- * is credited before the deferred `safeTransferFrom`, so inside the callback GA1 borrows on the
50
- * target, repays the source, then withdraws the source collateral to settle the transfer.
51
- *
52
- * Bundle shape (callback contents depend on borrow mode):
53
- *
54
- * ```text
55
- * // optional targetReallocations run first:
56
- * reallocateTo(...) | reallocate(...) | allocateFromIdle(...),
57
- *
58
- * morphoSupplyCollateral(target, collateralAmount, user, [
59
- * // omitted in collat-only mode
60
- * morphoBorrow(target, borrowAssets, 0, minBorrowSharePrice, GA1),
61
- * morphoRepay(source, assets|0, 0|shares, maxRepaySharePrice, user, []),
62
- * // shares mode only: sweep overshoot before the withdraw so same-token markets aren't drained
63
- * morphoRepay(target, maxUint256, 0, maxUint256, user, [], skipRevert=true),
64
- * // shares mode only: fallback if the repay is skipped, skim residual loan tokens to the user
65
- * erc20Transfer(loanToken, user, maxUint256, GA1, skipRevert=false),
66
- * morphoWithdrawCollateral(source, collateralAmount, GA1),
67
- * ])
68
- * ```
69
- *
70
- * Borrow modes:
71
- *
72
- * - **Assets mode** (`borrowAssets > 0n`): exact-asset borrow and repay, no GA1 dust.
73
- * - **Shares mode** (`borrowShares > 0n`, `borrowAssets` is the overshoot): the trailing
74
- * `morphoRepay(target, maxUint256, …, skipRevert=true)` sweeps the residual into the target debt,
75
- * then an `erc20Transfer` skims any residual to the user if that repay is skipped.
76
- * - **Collat-only** (both zero/omitted): only collateral is migrated; borrow/repay legs omitted.
37
+ * Encodes a direct BlueBundlesV1 full borrow-position migration transaction.
77
38
  *
78
- * Prerequisite: GA1 must be authorized on Blue — the entity's `getRequirements()` returns the
79
- * `setAuthorization` transaction when needed.
39
+ * BlueBundlesV1 reads and moves the user's complete live source debt and collateral. Source and
40
+ * destination must use identical tokens, and referral fees plus allocator penalties increase the
41
+ * destination debt. Partial and collateral-only migration are not supported.
80
42
  *
81
- * @param params.source.chainId - The chain both markets live on.
82
- * @param params.source.marketParams - Source market params (the position being closed).
83
- * @param params.target.marketParams - Target market params; must share both tokens with the source.
84
- * @param params.args.user - Position owner on both markets.
85
- * @param params.args.collateralAmount - Amount of collateral to migrate.
86
- * @param params.args.borrowAssets - Loan assets to borrow on the target; exclusive with `borrowShares`. Defaults to `0n`.
87
- * @param params.args.borrowShares - Borrow shares to repay on the source; exclusive with `borrowAssets`. Defaults to `0n`.
88
- * @param params.args.minBorrowSharePrice - Minimum borrow share price (ray) on the target.
89
- * @param params.args.maxRepaySharePrice - Maximum repay share price (ray) on the source.
90
- * @param params.args.targetReallocations - Homogeneous Vault V1 or Vault V2 reallocations into the
91
- * target, run before the supply leg. Vault V1 inputs are deprecated for high-level Blue writes;
92
- * use Vault V2 for new integrations. V1 fees add to `tx.value`; V2 penalties are paid in the
93
- * target loan token.
94
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
95
- * a `setAuthorizationWithSig` call is prepended to the bundle.
96
- * @param params.metadata - Optional analytics metadata appended to `tx.data`.
97
- * @returns A deep-frozen `Transaction<BlueRefinanceAction>`.
98
- * @remarks `borrowAssets` and `borrowShares` describe different markets (target borrow vs. source
99
- * repay); in shares mode the entity passes both. Caller-facing mutual exclusivity is enforced at the entity layer.
100
- * @throws {NonPositiveInputError} when `collateralAmount <= 0n`, a repay leg has a non-positive
101
- * `maxRepaySharePrice`, or any reallocation withdrawal amount is non-positive.
102
- * @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
103
- * @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
104
- * @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
105
- * @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
106
- * @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
107
- * @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
108
- * @throws {NegativeInputError} when `borrowAssets`, `borrowShares`, `minBorrowSharePrice`,
109
- * `maxRepaySharePrice`, a V1 fee, or a V2 penalty is negative.
110
- * @throws {RefinanceSameMarketError} when source and target market ids are equal.
111
- * @throws {RefinanceTokenMismatchError} when source and target do not share both tokens.
112
- * @throws {RefinanceSharesMissingBorrowAssetsError} when `borrowShares > 0n` but `borrowAssets` is omitted or non-positive.
113
- * @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
114
- * @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references the target market.
115
- * @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly sorted by market id.
43
+ * @param params - Migration encoding parameters.
44
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
45
+ * @param params.market.sourceMarketParams - Source market scoped by the Blue entity.
46
+ * @param params.market.destinationMarketParams - Distinct compatible destination market.
47
+ * @param params.args.userAddress - Intended user used to bind an optional Morpho authorization and
48
+ * action metadata. This value is not encoded; the caller must send from the same account because
49
+ * BlueBundlesV1 always migrates `msg.sender`.
50
+ * @param params.args.maxLtv - Maximum destination LTV.
51
+ * @param params.args.reallocations - Vault V2 reallocations into the destination.
52
+ * @param params.args.deadline - Final call deadline in Unix seconds.
53
+ * @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
54
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
55
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
56
+ * @param params.metadata - Optional transaction metadata.
57
+ * @returns A deep-frozen `Readonly<Transaction<BlueRefinanceAction>>` whose
58
+ * `to` address is BlueBundlesV1 and whose `action` records the normalized migration inputs.
59
+ * @throws {RefinanceSameMarketError} when source and destination IDs match.
60
+ * @throws {RefinanceTokenMismatchError} when their loan or collateral tokens differ.
61
+ * @throws {NegativeInputError} when `maxLtv`, a referral fee, or a reallocation penalty is negative.
62
+ * @throws {NonPositiveInputError} when the deadline or a reallocation amount is not positive.
63
+ * @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its ABI bound.
64
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
65
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
66
+ * @throws {InvalidReallocationShapeError} when a reallocation entry is not a valid Vault V2 reallocation.
67
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
68
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
69
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the destination market.
70
+ * @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
71
+ * @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
72
+ * @throws {BundlesRequirementSignatureMismatchError} when authorization cannot be bound safely.
73
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
74
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
116
75
  * @example
117
76
  * ```ts
77
+ * import { markets } from "@morpho-org/morpho-test";
118
78
  * import { blueRefinance } from "@morpho-org/morpho-sdk";
79
+ * import { zeroAddress } from "viem";
80
+ * import { mainnet } from "viem/chains";
119
81
  *
82
+ * const sourceMarketParams = markets[mainnet.id].eth_wstEth_2;
83
+ * const destinationMarketParams = markets[mainnet.id].eth_wstEth;
120
84
  * const tx = blueRefinance({
121
- * source: { chainId: 1, marketParams: sourceParams },
122
- * target: { marketParams: targetParams },
85
+ * market: { chainId: mainnet.id, sourceMarketParams, destinationMarketParams },
123
86
  * args: {
124
- * user: borrower,
125
- * collateralAmount: 1_000_000_000_000_000_000n,
126
- * borrowShares: 500_000_000_000n,
127
- * borrowAssets: 501_000_000n, // overshoot computed by entity layer
128
- * minBorrowSharePrice: 0n,
129
- * maxRepaySharePrice: 1_500_000_000_000_000_000_000_000_000n,
87
+ * userAddress: zeroAddress,
88
+ * maxLtv: 850000000000000000n,
89
+ * deadline: 1_900_000_000n,
130
90
  * },
131
91
  * });
132
92
  * // tx satisfies Readonly<Transaction<BlueRefinanceAction>>
133
93
  * ```
134
94
  */
135
- export declare const blueRefinance: ({ source: { chainId, marketParams: sourceParams }, target: { marketParams: targetParams }, args: { user, collateralAmount, borrowAssets, borrowShares, minBorrowSharePrice, maxRepaySharePrice, targetReallocations, authorizationSignature, }, metadata, }: BlueRefinanceParams) => Readonly<Transaction<BlueRefinanceAction>>;
95
+ export declare const blueRefinance: (params: BlueRefinanceParams) => Readonly<Transaction<BlueRefinanceAction>>;