@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,141 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildBlueReallocationActions = exports.buildVaultV2BlueReallocationActions = exports.buildVaultV1ReallocationActions = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
- const validate_js_1 = require("../../helpers/validate.js");
6
- /**
7
- * Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
8
- *
9
- * Preserves the supplied reallocation order and aggregates each allocator call's native fee.
10
- *
11
- * @param params.reallocations - Validated Vault V1 reallocations to execute.
12
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
13
- * @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
14
- * @internal
15
- */
16
- const buildVaultV1ReallocationActions = ({ reallocations, targetMarketParams, }) => {
17
- let fee = 0n;
18
- const actions = [];
19
- for (const reallocation of reallocations) {
20
- actions.push({
21
- type: "reallocateTo",
22
- args: [
23
- reallocation.vault,
24
- reallocation.fee,
25
- reallocation.withdrawals.map((withdrawal) => ({
26
- marketParams: withdrawal.marketParams,
27
- amount: withdrawal.amount,
28
- })),
29
- targetMarketParams,
30
- false,
31
- ],
32
- });
33
- fee += reallocation.fee;
34
- }
35
- return { actions, fee, penaltyAssets: 0n };
36
- };
37
- exports.buildVaultV1ReallocationActions = buildVaultV1ReallocationActions;
38
- /**
39
- * Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
40
- *
41
- * Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
42
- * allocator action per validated Vault V2 reallocation.
43
- *
44
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
45
- * @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
46
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
47
- * @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
48
- * transaction initiator.
49
- * @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
50
- * @internal
51
- */
52
- const buildVaultV2BlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource = "initiator", }) => {
53
- const actions = [];
54
- const penaltyAssets = reallocations.reduce((total, reallocation) => total +
55
- blue_sdk_1.VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
56
- if (penaltyAssets > 0n) {
57
- const { bundler3: { bundler3, generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
58
- actions.push(penaltyFundingSource === "generalAdapter1"
59
- ? {
60
- type: "erc20Transfer",
61
- args: [
62
- targetMarketParams.loanToken,
63
- bundler3,
64
- penaltyAssets,
65
- generalAdapter1,
66
- false,
67
- ],
68
- }
69
- : {
70
- type: "erc20TransferFrom",
71
- args: [
72
- targetMarketParams.loanToken,
73
- penaltyAssets,
74
- bundler3,
75
- false,
76
- ],
77
- });
78
- }
79
- for (const reallocation of reallocations) {
80
- actions.push(reallocation.from.type === "market"
81
- ? {
82
- type: "vaultV2BluePublicAllocatorReallocate",
83
- args: [
84
- reallocation.vault,
85
- reallocation.from.adapter,
86
- reallocation.from.marketParams,
87
- reallocation.to.adapter,
88
- targetMarketParams,
89
- reallocation.assets,
90
- reallocation.penalty,
91
- false,
92
- ],
93
- }
94
- : {
95
- type: "vaultV2BluePublicAllocatorAllocateFromIdle",
96
- args: [
97
- reallocation.vault,
98
- reallocation.to.adapter,
99
- targetMarketParams,
100
- reallocation.assets,
101
- reallocation.penalty,
102
- false,
103
- ],
104
- });
105
- }
106
- return { actions, fee: 0n, penaltyAssets };
107
- };
108
- exports.buildVaultV2BlueReallocationActions = buildVaultV2BlueReallocationActions;
109
- /**
110
- * Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
111
- *
112
- * Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
113
- * native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
114
- *
115
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
116
- * @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
117
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
118
- * @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
119
- * transaction initiator.
120
- * @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
121
- * @internal
122
- */
123
- const buildBlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }) => {
124
- const reallocationPlan = (0, validate_js_1.validateAndNormalizeReallocations)({
125
- reallocations,
126
- targetMarketId: targetMarketParams.id,
127
- chainId,
128
- });
129
- return reallocationPlan.type === "vaultV1"
130
- ? (0, exports.buildVaultV1ReallocationActions)({
131
- reallocations: reallocationPlan.reallocations,
132
- targetMarketParams,
133
- })
134
- : (0, exports.buildVaultV2BlueReallocationActions)({
135
- chainId,
136
- reallocations: reallocationPlan.reallocations,
137
- targetMarketParams,
138
- penaltyFundingSource,
139
- });
140
- };
141
- exports.buildBlueReallocationActions = buildBlueReallocationActions;
@@ -1,66 +0,0 @@
1
- import type { MarketId } from "@morpho-org/blue-sdk";
2
- /**
3
- * Pre-resolved repay amounts (plus `maxSharePrice`) validated by {@link resolveRepayFunding}.
4
- *
5
- * These are the fields the entity layer (`MorphoBlue.repay` / `repayWithdrawCollateral`) resolves
6
- * from live market/position state before either repay builder assembles the bundle.
7
- *
8
- * @internal
9
- */
10
- export interface RepayFundingInput {
11
- /** (assets mode) ERC-20 loan tokens pulled from the payer. */
12
- readonly amount: bigint;
13
- /** (shares mode) Borrow shares to repay. Discriminates the mode when `> 0n`. */
14
- readonly shares: bigint;
15
- /** Native ETH wrapped into wNative to help fund the repay. */
16
- readonly nativeAmount: bigint;
17
- /** Loan tokens routed into `GeneralAdapter1` (see `RepayActionAmountArgs`). */
18
- readonly transferAmount: bigint;
19
- /** Maximum acceptable repay share price (in ray). */
20
- readonly maxSharePrice: bigint;
21
- }
22
- /**
23
- * Derived repay funding values consumed by the repay builders.
24
- *
25
- * @internal
26
- */
27
- export interface RepayFunding {
28
- /** `true` when repaying an exact share count (full close); `false` in assets mode. */
29
- readonly isSharesMode: boolean;
30
- /** Assets passed to `morphoRepay` (`transferAmount` in assets mode, `0n` in shares mode). */
31
- readonly repayAssets: bigint;
32
- /** Shares passed to `morphoRepay` (`shares` in shares mode, `0n` in assets mode). */
33
- readonly repayShares: bigint;
34
- /** ERC-20 loan tokens pulled into `GeneralAdapter1` (net of any wrapped native). */
35
- readonly erc20Amount: bigint;
36
- }
37
- /**
38
- * Resolves and validates the repay funding envelope shared by `blueRepay` and
39
- * `blueRepayWithdrawCollateral`.
40
- *
41
- * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
42
- * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
43
- * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
44
- * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
45
- * tokens nor under-funds the repay:
46
- *
47
- * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
48
- * like `blueSupply`); ERC-20 pulled = `amount`.
49
- * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
50
- * net of native).
51
- *
52
- * Single source of truth for the funding math both repay builders depend on — a correctness fix here
53
- * applies to both paths instead of risking a silent divergence between two copies.
54
- *
55
- * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
56
- * @param marketId - Market id used to tag the thrown errors.
57
- * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
58
- * amounts the repay builders assemble the bundle from.
59
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
60
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
61
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
62
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
63
- * `transferAmount !== amount + nativeAmount`.
64
- * @internal
65
- */
66
- export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveRepayFunding = void 0;
4
- const index_js_1 = require("../../types/index.js");
5
- /**
6
- * Resolves and validates the repay funding envelope shared by `blueRepay` and
7
- * `blueRepayWithdrawCollateral`.
8
- *
9
- * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
10
- * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
11
- * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
12
- * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
13
- * tokens nor under-funds the repay:
14
- *
15
- * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
16
- * like `blueSupply`); ERC-20 pulled = `amount`.
17
- * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
18
- * net of native).
19
- *
20
- * Single source of truth for the funding math both repay builders depend on — a correctness fix here
21
- * applies to both paths instead of risking a silent divergence between two copies.
22
- *
23
- * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
24
- * @param marketId - Market id used to tag the thrown errors.
25
- * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
26
- * amounts the repay builders assemble the bundle from.
27
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
28
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
29
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
30
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
31
- * `transferAmount !== amount + nativeAmount`.
32
- * @internal
33
- */
34
- const resolveRepayFunding = ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }, marketId) => {
35
- if (maxSharePrice <= 0n) {
36
- throw new index_js_1.NonPositiveInputError("maxSharePrice", maxSharePrice);
37
- }
38
- if (nativeAmount < 0n) {
39
- throw new index_js_1.NegativeInputError("nativeAmount", nativeAmount);
40
- }
41
- if (amount < 0n) {
42
- throw new index_js_1.NegativeInputError("amount", amount);
43
- }
44
- if (shares < 0n) {
45
- throw new index_js_1.NegativeInputError("shares", shares);
46
- }
47
- if (amount > 0n && shares > 0n) {
48
- throw new index_js_1.MutuallyExclusiveRepayAmountsError(marketId);
49
- }
50
- // Shares mode repays an exact share count and pulls `transferAmount` ERC-20
51
- // (already net of native), skimming the residual. Assets mode repays
52
- // `transferAmount` (= amount + native, additive) and pulls `amount`.
53
- const isSharesMode = shares > 0n;
54
- const repayAssets = isSharesMode ? 0n : transferAmount;
55
- const repayShares = shares;
56
- const erc20Amount = isSharesMode ? transferAmount : amount;
57
- // Validate the pre-resolved funding. Assets mode is additive like `blueSupply`:
58
- // the ERC-20 pulled (`amount`) plus the wrapped `nativeAmount` must equal the
59
- // assets repaid, and the total must be positive — so the adapter neither strands
60
- // over-pulled tokens nor under-funds the repay. Shares mode pulls `transferAmount`
61
- // ERC-20 (0 on a fully-native repay) and wraps `nativeAmount`; the bundle must
62
- // fund the repay with a non-negative transfer and a positive total.
63
- if (isSharesMode) {
64
- if (transferAmount < 0n) {
65
- throw new index_js_1.NegativeInputError("transferAmount", transferAmount);
66
- }
67
- if (transferAmount + nativeAmount <= 0n) {
68
- throw new index_js_1.NonPositiveInputError("transferAmount + nativeAmount", transferAmount + nativeAmount);
69
- }
70
- }
71
- else {
72
- if (transferAmount !== amount + nativeAmount) {
73
- throw new index_js_1.TransferAmountNotEqualToAssetsError({
74
- transferAmount,
75
- assets: amount + nativeAmount,
76
- market: marketId,
77
- });
78
- }
79
- if (repayAssets <= 0n) {
80
- throw new index_js_1.NonPositiveInputError("amount + nativeAmount", repayAssets);
81
- }
82
- }
83
- return { isSharesMode, repayAssets, repayShares, erc20Amount };
84
- };
85
- exports.resolveRepayFunding = resolveRepayFunding;
@@ -1,47 +0,0 @@
1
- import type { Address } from "viem";
2
- import type { Action } from "../../bundler/index.js";
3
- import type { PermitRequirementSignature } from "../../types/index.js";
4
- /** Parameters for {@link buildAssetFundingActions}. */
5
- export interface BuildAssetFundingActionsParams {
6
- /** Chain the operation runs on — resolves adapter addresses and validates wNative. */
7
- readonly chainId: number;
8
- /** Asset wrapped and/or pulled into `GeneralAdapter1` (loan token, collateral token, …). */
9
- readonly asset: Address;
10
- /** ERC-20 amount of `asset` to pull from the payer (`0n` on a fully-native funding). */
11
- readonly erc20Amount: bigint;
12
- /** Native ETH to wrap into wNative before the pull (`0n` when not wrapping). */
13
- readonly nativeAmount: bigint;
14
- /** Optional pre-signed permit/permit2 approval for the ERC-20 pull. */
15
- readonly requirementSignature?: PermitRequirementSignature;
16
- }
17
- /**
18
- * Builds the shared asset-funding actions — native wrapping followed by the ERC-20 pull — that move
19
- * `asset` into `GeneralAdapter1` before a bundled Morpho operation (`blueSupply`,
20
- * `blueSupplyCollateral`, `blueSupplyCollateralBorrow`, `blueRepay`, `blueRepayWithdrawCollateral`).
21
- *
22
- * Encode-only and synchronous: returns a fresh `Action[]` (never mutates its inputs, mirroring
23
- * `buildVaultV2BlueReallocationActions`) and reads no on-chain state. When `nativeAmount > 0n`, validates
24
- * `asset` is the chain's wNative and emits `nativeTransfer → wrapNative`. When `erc20Amount > 0n`,
25
- * pulls the ERC-20 via a signed permit/permit2 (`requirementSignature`) or a plain
26
- * `erc20TransferFrom` — `getTokenRequirementActions` emits the latter itself when no signature is
27
- * supplied. A fully-native funding emits no ERC-20 action; funding nothing returns `[]`.
28
- *
29
- * Single source of truth for the native-wrap + ERC-20-pull composition every bundled Blue funding
30
- * path depends on — a fix here applies to all of them instead of risking a silent divergence
31
- * between copies.
32
- *
33
- * @param params - See {@link BuildAssetFundingActionsParams}.
34
- * @returns The `nativeTransfer`/`wrapNative`/pull actions, in bundle order (empty when nothing is
35
- * funded).
36
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
37
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but `asset` is not the
38
- * chain's wNative.
39
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
40
- * provided and the signed asset differs from `asset`.
41
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
42
- * provided and the signed amount differs from `erc20Amount`.
43
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
44
- * signature is missing its expiration.
45
- * @internal
46
- */
47
- export declare const buildAssetFundingActions: ({ chainId, asset, erc20Amount, nativeAmount, requirementSignature, }: BuildAssetFundingActionsParams) => Action[];
@@ -1,59 +0,0 @@
1
- import { getChainAddresses } from "@morpho-org/blue-sdk";
2
- import { validateNativeAsset } from "../../helpers/index.js";
3
- import { getTokenRequirementActions } from "../signatures/getTokenRequirementActions.js";
4
- /**
5
- * Builds the shared asset-funding actions — native wrapping followed by the ERC-20 pull — that move
6
- * `asset` into `GeneralAdapter1` before a bundled Morpho operation (`blueSupply`,
7
- * `blueSupplyCollateral`, `blueSupplyCollateralBorrow`, `blueRepay`, `blueRepayWithdrawCollateral`).
8
- *
9
- * Encode-only and synchronous: returns a fresh `Action[]` (never mutates its inputs, mirroring
10
- * `buildVaultV2BlueReallocationActions`) and reads no on-chain state. When `nativeAmount > 0n`, validates
11
- * `asset` is the chain's wNative and emits `nativeTransfer → wrapNative`. When `erc20Amount > 0n`,
12
- * pulls the ERC-20 via a signed permit/permit2 (`requirementSignature`) or a plain
13
- * `erc20TransferFrom` — `getTokenRequirementActions` emits the latter itself when no signature is
14
- * supplied. A fully-native funding emits no ERC-20 action; funding nothing returns `[]`.
15
- *
16
- * Single source of truth for the native-wrap + ERC-20-pull composition every bundled Blue funding
17
- * path depends on — a fix here applies to all of them instead of risking a silent divergence
18
- * between copies.
19
- *
20
- * @param params - See {@link BuildAssetFundingActionsParams}.
21
- * @returns The `nativeTransfer`/`wrapNative`/pull actions, in bundle order (empty when nothing is
22
- * funded).
23
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
24
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but `asset` is not the
25
- * chain's wNative.
26
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
27
- * provided and the signed asset differs from `asset`.
28
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
29
- * provided and the signed amount differs from `erc20Amount`.
30
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
31
- * signature is missing its expiration.
32
- * @internal
33
- */
34
- export const buildAssetFundingActions = ({ chainId, asset, erc20Amount, nativeAmount, requirementSignature, }) => {
35
- const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
36
- const actions = [];
37
- // Wrap native into wNative before pulling the ERC-20 remainder.
38
- if (nativeAmount > 0n) {
39
- validateNativeAsset(chainId, asset);
40
- actions.push({
41
- type: "nativeTransfer",
42
- args: [bundler3, generalAdapter1, nativeAmount, false],
43
- }, {
44
- type: "wrapNative",
45
- args: [nativeAmount, generalAdapter1, false],
46
- });
47
- }
48
- // Pull the ERC-20 portion (0 on a fully native funding). getTokenRequirementActions emits a plain
49
- // erc20TransferFrom itself when no permit/permit2 signature is supplied.
50
- if (erc20Amount > 0n) {
51
- actions.push(...getTokenRequirementActions({
52
- asset,
53
- amount: erc20Amount,
54
- recipient: generalAdapter1,
55
- requirementSignature,
56
- }));
57
- }
58
- return actions;
59
- };
@@ -1,69 +0,0 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
2
- import type { Action } from "../../bundler/index.js";
3
- import type { BlueReallocationPlan, VaultV1Reallocation, VaultV2BlueReallocation } from "../../types/index.js";
4
- /**
5
- * Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
6
- *
7
- * Preserves the supplied reallocation order and aggregates each allocator call's native fee.
8
- *
9
- * @param params.reallocations - Validated Vault V1 reallocations to execute.
10
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
11
- * @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
12
- * @internal
13
- */
14
- export declare const buildVaultV1ReallocationActions: ({ reallocations, targetMarketParams, }: {
15
- readonly reallocations: readonly VaultV1Reallocation[];
16
- readonly targetMarketParams: MarketParams;
17
- }) => {
18
- actions: Action[];
19
- fee: bigint;
20
- penaltyAssets: bigint;
21
- };
22
- /**
23
- * Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
24
- *
25
- * Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
26
- * allocator action per validated Vault V2 reallocation.
27
- *
28
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
29
- * @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
30
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
31
- * @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
32
- * transaction initiator.
33
- * @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
34
- * @internal
35
- */
36
- export declare const buildVaultV2BlueReallocationActions: ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }: {
37
- readonly chainId: number;
38
- readonly reallocations: readonly VaultV2BlueReallocation[];
39
- readonly targetMarketParams: MarketParams;
40
- readonly penaltyFundingSource?: "initiator" | "generalAdapter1";
41
- }) => {
42
- actions: Action[];
43
- fee: bigint;
44
- penaltyAssets: bigint;
45
- };
46
- /**
47
- * Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
48
- *
49
- * Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
50
- * native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
51
- *
52
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
53
- * @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
54
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
55
- * @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
56
- * transaction initiator.
57
- * @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
58
- * @internal
59
- */
60
- export declare const buildBlueReallocationActions: ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }: {
61
- readonly chainId: number;
62
- readonly reallocations: BlueReallocationPlan | undefined;
63
- readonly targetMarketParams: MarketParams;
64
- readonly penaltyFundingSource?: "initiator" | "generalAdapter1";
65
- }) => {
66
- actions: Action[];
67
- fee: bigint;
68
- penaltyAssets: bigint;
69
- };
@@ -1,135 +0,0 @@
1
- import { getChainAddresses, VaultV2BluePublicAllocatorConfigUtils, } from "@morpho-org/blue-sdk";
2
- import { validateAndNormalizeReallocations } from "../../helpers/validate.js";
3
- /**
4
- * Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
5
- *
6
- * Preserves the supplied reallocation order and aggregates each allocator call's native fee.
7
- *
8
- * @param params.reallocations - Validated Vault V1 reallocations to execute.
9
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
10
- * @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
11
- * @internal
12
- */
13
- export const buildVaultV1ReallocationActions = ({ reallocations, targetMarketParams, }) => {
14
- let fee = 0n;
15
- const actions = [];
16
- for (const reallocation of reallocations) {
17
- actions.push({
18
- type: "reallocateTo",
19
- args: [
20
- reallocation.vault,
21
- reallocation.fee,
22
- reallocation.withdrawals.map((withdrawal) => ({
23
- marketParams: withdrawal.marketParams,
24
- amount: withdrawal.amount,
25
- })),
26
- targetMarketParams,
27
- false,
28
- ],
29
- });
30
- fee += reallocation.fee;
31
- }
32
- return { actions, fee, penaltyAssets: 0n };
33
- };
34
- /**
35
- * Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
36
- *
37
- * Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
38
- * allocator action per validated Vault V2 reallocation.
39
- *
40
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
41
- * @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
42
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
43
- * @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
44
- * transaction initiator.
45
- * @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
46
- * @internal
47
- */
48
- export const buildVaultV2BlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource = "initiator", }) => {
49
- const actions = [];
50
- const penaltyAssets = reallocations.reduce((total, reallocation) => total +
51
- VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
52
- if (penaltyAssets > 0n) {
53
- const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
54
- actions.push(penaltyFundingSource === "generalAdapter1"
55
- ? {
56
- type: "erc20Transfer",
57
- args: [
58
- targetMarketParams.loanToken,
59
- bundler3,
60
- penaltyAssets,
61
- generalAdapter1,
62
- false,
63
- ],
64
- }
65
- : {
66
- type: "erc20TransferFrom",
67
- args: [
68
- targetMarketParams.loanToken,
69
- penaltyAssets,
70
- bundler3,
71
- false,
72
- ],
73
- });
74
- }
75
- for (const reallocation of reallocations) {
76
- actions.push(reallocation.from.type === "market"
77
- ? {
78
- type: "vaultV2BluePublicAllocatorReallocate",
79
- args: [
80
- reallocation.vault,
81
- reallocation.from.adapter,
82
- reallocation.from.marketParams,
83
- reallocation.to.adapter,
84
- targetMarketParams,
85
- reallocation.assets,
86
- reallocation.penalty,
87
- false,
88
- ],
89
- }
90
- : {
91
- type: "vaultV2BluePublicAllocatorAllocateFromIdle",
92
- args: [
93
- reallocation.vault,
94
- reallocation.to.adapter,
95
- targetMarketParams,
96
- reallocation.assets,
97
- reallocation.penalty,
98
- false,
99
- ],
100
- });
101
- }
102
- return { actions, fee: 0n, penaltyAssets };
103
- };
104
- /**
105
- * Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
106
- *
107
- * Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
108
- * native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
109
- *
110
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
111
- * @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
112
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
113
- * @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
114
- * transaction initiator.
115
- * @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
116
- * @internal
117
- */
118
- export const buildBlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }) => {
119
- const reallocationPlan = validateAndNormalizeReallocations({
120
- reallocations,
121
- targetMarketId: targetMarketParams.id,
122
- chainId,
123
- });
124
- return reallocationPlan.type === "vaultV1"
125
- ? buildVaultV1ReallocationActions({
126
- reallocations: reallocationPlan.reallocations,
127
- targetMarketParams,
128
- })
129
- : buildVaultV2BlueReallocationActions({
130
- chainId,
131
- reallocations: reallocationPlan.reallocations,
132
- targetMarketParams,
133
- penaltyFundingSource,
134
- });
135
- };
@@ -1,66 +0,0 @@
1
- import type { MarketId } from "@morpho-org/blue-sdk";
2
- /**
3
- * Pre-resolved repay amounts (plus `maxSharePrice`) validated by {@link resolveRepayFunding}.
4
- *
5
- * These are the fields the entity layer (`MorphoBlue.repay` / `repayWithdrawCollateral`) resolves
6
- * from live market/position state before either repay builder assembles the bundle.
7
- *
8
- * @internal
9
- */
10
- export interface RepayFundingInput {
11
- /** (assets mode) ERC-20 loan tokens pulled from the payer. */
12
- readonly amount: bigint;
13
- /** (shares mode) Borrow shares to repay. Discriminates the mode when `> 0n`. */
14
- readonly shares: bigint;
15
- /** Native ETH wrapped into wNative to help fund the repay. */
16
- readonly nativeAmount: bigint;
17
- /** Loan tokens routed into `GeneralAdapter1` (see `RepayActionAmountArgs`). */
18
- readonly transferAmount: bigint;
19
- /** Maximum acceptable repay share price (in ray). */
20
- readonly maxSharePrice: bigint;
21
- }
22
- /**
23
- * Derived repay funding values consumed by the repay builders.
24
- *
25
- * @internal
26
- */
27
- export interface RepayFunding {
28
- /** `true` when repaying an exact share count (full close); `false` in assets mode. */
29
- readonly isSharesMode: boolean;
30
- /** Assets passed to `morphoRepay` (`transferAmount` in assets mode, `0n` in shares mode). */
31
- readonly repayAssets: bigint;
32
- /** Shares passed to `morphoRepay` (`shares` in shares mode, `0n` in assets mode). */
33
- readonly repayShares: bigint;
34
- /** ERC-20 loan tokens pulled into `GeneralAdapter1` (net of any wrapped native). */
35
- readonly erc20Amount: bigint;
36
- }
37
- /**
38
- * Resolves and validates the repay funding envelope shared by `blueRepay` and
39
- * `blueRepayWithdrawCollateral`.
40
- *
41
- * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
42
- * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
43
- * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
44
- * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
45
- * tokens nor under-funds the repay:
46
- *
47
- * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
48
- * like `blueSupply`); ERC-20 pulled = `amount`.
49
- * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
50
- * net of native).
51
- *
52
- * Single source of truth for the funding math both repay builders depend on — a correctness fix here
53
- * applies to both paths instead of risking a silent divergence between two copies.
54
- *
55
- * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
56
- * @param marketId - Market id used to tag the thrown errors.
57
- * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
58
- * amounts the repay builders assemble the bundle from.
59
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
60
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
61
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
62
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
63
- * `transferAmount !== amount + nativeAmount`.
64
- * @internal
65
- */
66
- export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;