@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,68 +1,63 @@
1
- import { getChainAddresses } from "@morpho-org/blue-sdk";
2
- import { blueAbi } from "@morpho-org/blue-sdk-viem";
3
1
  import { deepFreeze } from "@morpho-org/morpho-ts";
4
- import { encodeFunctionData } from "viem";
5
- import { addTransactionMetadata } from "../../helpers/index.js";
6
- import { NonPositiveInputError, } from "../../types/index.js";
2
+ import { blueRepayWithdrawCollateral } from "./repayWithdrawCollateral.js";
7
3
  /**
8
- * Prepares a withdraw-collateral transaction for a Morpho Blue market.
4
+ * Encodes a pure collateral withdrawal through BlueBundlesV1.
9
5
  *
10
- * Direct call to `Morpho.withdrawCollateral` — no bundler needed. Collateral flows out of Morpho,
11
- * so there is no inflation-attack surface requiring the bundler.
6
+ * Delegates to {@link blueRepayWithdrawCollateral} with a zero repay leg. BlueBundlesV1 enforces
7
+ * the supplied post-operation LTV, and no standalone Morpho or Bundler3 call is encoded.
12
8
  *
13
- * The caller (`msg.sender`) must be `onBehalf` or be authorized by them on Morpho.
14
- *
15
- * @param params.market.chainId - The chain the market lives on.
16
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
17
- * @param params.args.amount - Amount of collateral to withdraw.
18
- * @param params.args.onBehalf - Address whose Morpho position the collateral is withdrawn from.
19
- * @param params.args.receiver - Address that receives the withdrawn collateral.
20
- * @param params.metadata - Optional analytics metadata attached to the transaction.
21
- * @returns A deep-frozen `Transaction<BlueWithdrawCollateralAction>` with `to`, `value`,
22
- * `data`, and the typed `action` discriminator the simulation layer consumes.
23
- * @throws {NonPositiveInputError} when `amount <= 0n`.
9
+ * @param params - Collateral-withdrawal encoding parameters.
10
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
11
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
12
+ * @param params.args.userAddress - User whose collateral position is reduced.
13
+ * @param params.args.collateralAssets - Collateral assets withdrawn.
14
+ * @param params.args.maxLtv - Maximum post-operation LTV.
15
+ * @param params.args.deadline - Final call deadline in Unix seconds.
16
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
17
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
18
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
19
+ * @param params.metadata - Optional transaction metadata.
20
+ * @returns A deep-frozen `Readonly<Transaction<BlueWithdrawCollateralAction>>` whose calldata
21
+ * invokes the combined BlueBundlesV1 entrypoint with a zero repay leg.
22
+ * @throws {NegativeInputError} when collateral, LTV, or fee is negative.
23
+ * @throws {NonPositiveInputError} when collateral or deadline is not positive.
24
+ * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
25
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
26
+ * @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
27
+ * @throws {BundlesRequirementSignatureMismatchError} when authorization cannot be bound safely.
28
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
29
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
24
30
  * @example
25
31
  * ```ts
32
+ * import { markets } from "@morpho-org/morpho-test";
26
33
  * import { blueWithdrawCollateral } from "@morpho-org/morpho-sdk";
34
+ * import { zeroAddress } from "viem";
35
+ * import { mainnet } from "viem/chains";
27
36
  *
28
37
  * const tx = blueWithdrawCollateral({
29
- * market: { chainId: 1, marketParams },
38
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
30
39
  * args: {
31
- * amount: 1_000_000_000_000_000_000n,
32
- * onBehalf: borrower,
33
- * receiver: borrower,
40
+ * userAddress: zeroAddress,
41
+ * collateralAssets: 1_000_000_000_000_000_000n,
42
+ * maxLtv: 850000000000000000n,
43
+ * deadline: 1_900_000_000n,
34
44
  * },
35
45
  * });
36
46
  * // tx satisfies Readonly<Transaction<BlueWithdrawCollateralAction>>
37
47
  * ```
38
48
  */
39
- export const blueWithdrawCollateral = ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }) => {
40
- if (amount <= 0n) {
41
- throw new NonPositiveInputError("amount", amount);
42
- }
43
- const { morpho } = getChainAddresses(chainId);
44
- let tx = {
45
- to: morpho,
46
- data: encodeFunctionData({
47
- abi: blueAbi,
48
- functionName: "withdrawCollateral",
49
- args: [marketParams, amount, onBehalf, receiver],
50
- }),
51
- value: 0n,
52
- };
53
- if (metadata) {
54
- tx = addTransactionMetadata(tx, metadata);
55
- }
56
- return deepFreeze({
57
- ...tx,
58
- action: {
59
- type: "blueWithdrawCollateral",
60
- args: {
61
- market: marketParams.id,
62
- amount,
63
- onBehalf,
64
- receiver,
65
- },
49
+ export const blueWithdrawCollateral = (params) => {
50
+ const transaction = blueRepayWithdrawCollateral({
51
+ ...params,
52
+ args: {
53
+ ...params.args,
54
+ repayAssets: 0n,
55
+ repayShares: 0n,
56
+ maxRepayAssets: 0n,
66
57
  },
67
58
  });
59
+ return deepFreeze({
60
+ ...transaction,
61
+ action: { ...transaction.action, type: "blueWithdrawCollateral" },
62
+ });
68
63
  };
@@ -0,0 +1,200 @@
1
+ import { type Address, type Hex } from "viem";
2
+ import { type BaseAction, type BundlesFundingArgs, type BundlesTokenRequirementSignature, type Erc2612RequirementSignature, type Metadata, type Permit2SignatureTransferAction, type PermitAction, type PermitRequirementSignature, type RequirementSignature, type TokenRequirementSignature, type Transaction } from "../../types/index.js";
3
+ /** Numeric permit kinds consumed by `TokenLib.pullToken`. */
4
+ export declare const BundlesPermitKind: {
5
+ readonly None: 0;
6
+ readonly ERC2612: 1;
7
+ readonly Permit2: 2;
8
+ };
9
+ /** A numeric permit kind consumed by `TokenLib.pullToken`. */
10
+ export type BundlesPermitKind = (typeof BundlesPermitKind)[keyof typeof BundlesPermitKind];
11
+ /** ABI-ready token permit consumed by fixed bundles deposit and funding entrypoints. */
12
+ export interface BundlesTokenPermit {
13
+ /** TokenLib permit dispatch kind. */
14
+ readonly kind: BundlesPermitKind;
15
+ /** ABI-encoded permit payload, or empty bytes for classic allowance funding. */
16
+ readonly data: Hex;
17
+ }
18
+ /** ABI-ready ERC-2612 share permit consumed by vault bundles. */
19
+ export interface BundleSharesPermit {
20
+ /** Exact vault-share allowance authorized by the permit. */
21
+ readonly value: bigint;
22
+ /** Vault permit nonce signed by the owner. */
23
+ readonly nonce: bigint;
24
+ /** Permit signature deadline. */
25
+ readonly deadline: bigint;
26
+ /** ECDSA recovery identifier, or zero for the empty sentinel. */
27
+ readonly v: number;
28
+ /** ECDSA signature `r`, or zero for the empty sentinel. */
29
+ readonly r: Hex;
30
+ /** ECDSA signature `s`, or zero for the empty sentinel. */
31
+ readonly s: Hex;
32
+ }
33
+ /** Compatibility alias for the canonical {@link BundleSharesPermit} tuple. */
34
+ export type BundlesSharesPermit = BundleSharesPermit;
35
+ /** Common deadline and referral fields shared by fixed bundles calls. */
36
+ export interface BundlesCommonParams {
37
+ /** Contract execution deadline. */
38
+ readonly deadline: bigint;
39
+ /** Optional WAD-scaled referral fee percentage. */
40
+ readonly referralFeePct?: bigint;
41
+ /** Recipient required when `referralFeePct` is positive. */
42
+ readonly referralFeeRecipient?: Address;
43
+ }
44
+ /** Normalized deadline and referral fields encoded by fixed bundles calls. */
45
+ export interface NormalizedBundlesCommonParams {
46
+ /** Validated contract execution deadline. */
47
+ readonly deadline: bigint;
48
+ /** Validated WAD-scaled referral fee percentage. */
49
+ readonly referralFeePct: bigint;
50
+ /** Referral recipient, or the zero address when fees are disabled. */
51
+ readonly referralFeeRecipient: Address;
52
+ }
53
+ /**
54
+ * Validates and normalizes a bundles deadline and referral fee configuration.
55
+ *
56
+ * @internal
57
+ *
58
+ * @param params - Common bundles parameters.
59
+ * @returns Validated values with explicit zero defaults.
60
+ * @throws {NonPositiveInputError} when `deadline` is not positive.
61
+ * @throws {InputExceedsMaxError} when `deadline` exceeds uint256.
62
+ * @throws {NegativeInputError} when `referralFeePct` is negative.
63
+ * @throws {ReferralFeePctExceededError} when `referralFeePct >= WAD`.
64
+ * @throws {ReferralFeeRecipientMissingError} when a positive fee has no recipient.
65
+ */
66
+ export declare const normalizeBundlesCommonParams: (params: BundlesCommonParams) => NormalizedBundlesCommonParams;
67
+ /**
68
+ * Resolves mutually exclusive ERC-20/native funding into the entrypoint amount and transaction value.
69
+ *
70
+ * @param params - Exclusive bundles funding.
71
+ * @param params.amount - ERC-20 funding in the token's smallest unit; mutually exclusive with `nativeAmount`.
72
+ * @param params.nativeAmount - Native funding in wei; mutually exclusive with `amount`.
73
+ * @returns The gross ABI asset amount and native transaction value.
74
+ * @throws {MixedBundlesFundingError} when both funding keys are present.
75
+ * @throws {NegativeInputError} when the selected amount is negative.
76
+ * @throws {NonPositiveInputError} when the selected amount is not positive.
77
+ * @example
78
+ * ```ts
79
+ * import { resolveBundlesFunding } from "@morpho-org/morpho-sdk";
80
+ *
81
+ * const funding = resolveBundlesFunding({ amount: 1_000_000n });
82
+ * // funding is { assets: 1_000_000n, value: 0n }
83
+ * ```
84
+ */
85
+ export declare const resolveBundlesFunding: (params: BundlesFundingArgs) => {
86
+ readonly assets: bigint;
87
+ readonly value: bigint;
88
+ };
89
+ /**
90
+ * Converts an optional signed token requirement into the ABI-ready TokenLib permit tuple.
91
+ *
92
+ * @param params - Expected funding values and optional signed requirement.
93
+ * @param params.userAddress - Account funding the operation; must match the signature owner.
94
+ * @param params.token - ERC-20 funding token; must match the signed asset.
95
+ * @param params.spender - Fixed bundles contract authorized to pull the token.
96
+ * @param params.amount - Exact pull amount in the token's smallest unit; must match both signed amount fields.
97
+ * @param params.requirementSignature - Optional signed ERC-2612 or Permit2 SignatureTransfer requirement; omit for allowance funding.
98
+ * @returns An empty, ERC-2612, or Permit2 SignatureTransfer token permit.
99
+ * @throws {UnexpectedRequirementSignatureError} when Permit2 AllowanceTransfer is supplied.
100
+ * @throws {DepositOwnerMismatchError} when the signature owner differs from `userAddress`.
101
+ * @throws {DepositAssetMismatchError} when the signed token differs from `token`.
102
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
103
+ * @throws {DepositSpenderMismatchError} when the signed spender differs from `spender`.
104
+ * @throws {BundlesRequirementSignatureMismatchError} when signature metadata is malformed.
105
+ * @example
106
+ * ```ts
107
+ * import { getBundlesTokenPermit } from "@morpho-org/morpho-sdk";
108
+ * import { zeroAddress } from "viem";
109
+ *
110
+ * const permit = getBundlesTokenPermit({
111
+ * userAddress: zeroAddress,
112
+ * token: zeroAddress,
113
+ * spender: zeroAddress,
114
+ * amount: 1n,
115
+ * });
116
+ * // permit is { kind: 0, data: "0x" }
117
+ * ```
118
+ */
119
+ export declare const getBundlesTokenPermit: (params: {
120
+ readonly userAddress: Address;
121
+ readonly token: Address;
122
+ readonly spender: Address;
123
+ readonly amount: bigint;
124
+ readonly requirementSignature?: TokenRequirementSignature;
125
+ }) => BundlesTokenPermit;
126
+ /** @internal Normalizes a compact or serialized ECDSA signature for bundles permit tuples. */
127
+ export declare const normalizeBundlesSignature: (serializedSignature: Hex) => {
128
+ readonly v: number;
129
+ readonly r: Hex;
130
+ readonly s: Hex;
131
+ };
132
+ /**
133
+ * Converts an optional vault-share ERC-2612 requirement into the ABI-ready Permit tuple.
134
+ *
135
+ * @param params - Vault token, empty-sentinel deadline, and optional signed requirement.
136
+ * @param params.vault - Vault share token authorized by the permit.
137
+ * @param params.deadline - Unix timestamp in seconds used by the empty-permit sentinel.
138
+ * @param params.owner - Optional expected signature owner.
139
+ * @param params.spender - Optional expected fixed bundles contract authorized to pull shares.
140
+ * @param params.amount - Optional exact share allowance in the vault share token's smallest unit.
141
+ * @param params.requirementSignature - Optional signed ERC-2612 share requirement; omit for the empty-permit sentinel.
142
+ * @returns The signed share permit or the contract's empty-permit sentinel.
143
+ * @throws {BundlesPermitMismatchError} when the requirement kind, token, or signature is invalid.
144
+ * @example
145
+ * ```ts
146
+ * import { getBundlesSharesPermit } from "@morpho-org/morpho-sdk";
147
+ * import { zeroAddress } from "viem";
148
+ *
149
+ * const permit = getBundlesSharesPermit({
150
+ * vault: zeroAddress,
151
+ * deadline: 1_900_000_000n,
152
+ * });
153
+ * // permit.value === 0n
154
+ * ```
155
+ */
156
+ export declare const getBundlesSharesPermit: (params: {
157
+ readonly vault: Address;
158
+ readonly deadline: bigint;
159
+ readonly owner?: Address;
160
+ readonly spender?: Address;
161
+ readonly amount?: bigint;
162
+ readonly requirementSignature?: PermitRequirementSignature;
163
+ }) => BundleSharesPermit;
164
+ /**
165
+ * Selects the one token signature accepted by a bundles-funded call.
166
+ *
167
+ * @internal
168
+ *
169
+ * @param signatures - Signatures passed to `buildTx`.
170
+ * @param expectedRequirement - Signature requirement captured by this prepared operation.
171
+ * @returns The selected ERC-2612 or Permit2 SignatureTransfer signature.
172
+ * @throws {UnexpectedRequirementSignatureError} when a non-token signature is supplied.
173
+ * @throws {AmbiguousRequirementSignaturesError} when both token signature kinds are supplied.
174
+ */
175
+ export declare const selectBundlesTokenRequirementSignature: (signatures: readonly RequirementSignature[] | undefined, expectedRequirement?: PermitAction | Permit2SignatureTransferAction) => BundlesTokenRequirementSignature | undefined;
176
+ /**
177
+ * Selects the exact ERC-2612 vault-share permit produced for one prepared exit.
178
+ *
179
+ * @internal
180
+ *
181
+ * @param signatures - Signatures passed to the prepared operation's `buildTx` function.
182
+ * @param prepared - Exact share cap and permit requirement captured by this prepared operation.
183
+ * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
184
+ * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612, was built for another
185
+ * share cap, or is supplied before this operation resolves its share cap.
186
+ */
187
+ export declare const selectBundlesSharesRequirementSignature: (signatures: readonly RequirementSignature[] | undefined, prepared: {
188
+ readonly requiredShareAllowance: bigint | undefined;
189
+ readonly expectedRequirement?: PermitAction;
190
+ }) => Erc2612RequirementSignature | undefined;
191
+ /** @internal Returns the exact referral fee deducted from a fixed gross asset amount. */
192
+ export declare const getBundlesReferralFeeAssets: (assets: bigint, referralFeePct: bigint) => bigint;
193
+ /** @internal Finalizes a direct VaultBundlesV1 transaction with metadata and immutable action data. */
194
+ export declare const finalizeVaultBundlesV1Transaction: <TAction extends BaseAction>(params: {
195
+ readonly chainId: number;
196
+ readonly value: bigint;
197
+ readonly data: Hex;
198
+ readonly action: TAction;
199
+ readonly metadata?: Metadata;
200
+ }) => Readonly<Transaction<TAction>>;