@morpho-org/morpho-sdk 6.6.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/MIGRATION-v6-to-v7.md +291 -0
  2. package/README.md +26 -12
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +3 -2
  5. package/lib/cjs/actions/bundles/common.d.ts +9 -5
  6. package/lib/cjs/actions/bundles/common.js +22 -13
  7. package/lib/cjs/actions/midnight/authorization.d.ts +3 -2
  8. package/lib/cjs/actions/midnight/authorization.js +3 -2
  9. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +153 -0
  10. package/lib/cjs/actions/midnight/cancelAndMake.js +292 -0
  11. package/lib/cjs/actions/midnight/index.d.ts +3 -3
  12. package/lib/cjs/actions/midnight/index.js +3 -3
  13. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
  14. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +105 -69
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +128 -71
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +40 -28
  18. package/lib/cjs/actions/midnight/takeBorrow.js +95 -54
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +51 -27
  20. package/lib/cjs/actions/midnight/takeLend.js +111 -54
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +201 -0
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +69 -0
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +153 -0
  25. package/lib/cjs/actions/midnight/types.d.ts +7 -26
  26. package/lib/cjs/actions/midnight/types.js +0 -13
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -2
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +5 -5
  31. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +2 -2
  32. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
  33. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
  34. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +24 -13
  35. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
  36. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
  37. package/lib/cjs/actions/requirements/midnight/index.d.ts +0 -1
  38. package/lib/cjs/actions/requirements/midnight/index.js +0 -1
  39. package/lib/cjs/constants.d.ts +1 -1
  40. package/lib/cjs/constants.js +2 -1
  41. package/lib/cjs/entities/index.d.ts +2 -2
  42. package/lib/cjs/entities/midnight/midnight.d.ts +582 -108
  43. package/lib/cjs/entities/midnight/midnight.js +981 -509
  44. package/lib/cjs/entities/midnight/types.d.ts +142 -60
  45. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +2 -1
  46. package/lib/cjs/entities/vaultV1/vaultV1.js +1 -0
  47. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +2 -1
  48. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -0
  49. package/lib/cjs/helpers/callback.d.ts +54 -0
  50. package/lib/cjs/helpers/callback.js +73 -0
  51. package/lib/cjs/helpers/constant.d.ts +2 -0
  52. package/lib/cjs/helpers/constant.js +3 -1
  53. package/lib/cjs/helpers/index.d.ts +3 -2
  54. package/lib/cjs/helpers/index.js +6 -1
  55. package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +3 -3
  56. package/lib/cjs/helpers/signAndVerifyTypedData.js +3 -3
  57. package/lib/cjs/helpers/validate.d.ts +43 -2
  58. package/lib/cjs/helpers/validate.js +60 -3
  59. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
  60. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +3 -7
  61. package/lib/cjs/helpers/validateRequirementSpender.d.ts +3 -3
  62. package/lib/cjs/helpers/validateRequirementSpender.js +4 -5
  63. package/lib/cjs/midnight/abis.d.ts +1 -1
  64. package/lib/cjs/midnight/abis.js +3 -2
  65. package/lib/cjs/midnight/types.d.ts +1 -1
  66. package/lib/cjs/types/action.d.ts +117 -89
  67. package/lib/cjs/types/action.js +1 -19
  68. package/lib/cjs/types/error.d.ts +126 -40
  69. package/lib/cjs/types/error.js +154 -49
  70. package/lib/esm/abis.d.ts +1 -1
  71. package/lib/esm/abis.js +1 -1
  72. package/lib/esm/actions/bundles/common.d.ts +9 -5
  73. package/lib/esm/actions/bundles/common.js +22 -13
  74. package/lib/esm/actions/midnight/authorization.d.ts +3 -2
  75. package/lib/esm/actions/midnight/authorization.js +3 -2
  76. package/lib/esm/actions/midnight/cancelAndMake.d.ts +153 -0
  77. package/lib/esm/actions/midnight/cancelAndMake.js +288 -0
  78. package/lib/esm/actions/midnight/index.d.ts +3 -3
  79. package/lib/esm/actions/midnight/index.js +3 -3
  80. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
  81. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +108 -72
  82. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
  83. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +131 -74
  84. package/lib/esm/actions/midnight/takeBorrow.d.ts +40 -28
  85. package/lib/esm/actions/midnight/takeBorrow.js +98 -57
  86. package/lib/esm/actions/midnight/takeLend.d.ts +51 -27
  87. package/lib/esm/actions/midnight/takeLend.js +114 -57
  88. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
  89. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +197 -0
  90. package/lib/esm/actions/midnight/takeWithdraw.d.ts +69 -0
  91. package/lib/esm/actions/midnight/takeWithdraw.js +149 -0
  92. package/lib/esm/actions/midnight/types.d.ts +7 -26
  93. package/lib/esm/actions/midnight/types.js +1 -12
  94. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  95. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +3 -2
  96. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
  97. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -5
  98. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +2 -2
  99. package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -2
  100. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
  101. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +25 -14
  102. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
  103. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
  104. package/lib/esm/actions/requirements/midnight/index.d.ts +0 -1
  105. package/lib/esm/actions/requirements/midnight/index.js +0 -1
  106. package/lib/esm/constants.d.ts +1 -1
  107. package/lib/esm/constants.js +1 -1
  108. package/lib/esm/entities/index.d.ts +2 -2
  109. package/lib/esm/entities/midnight/midnight.d.ts +582 -108
  110. package/lib/esm/entities/midnight/midnight.js +987 -515
  111. package/lib/esm/entities/midnight/types.d.ts +142 -60
  112. package/lib/esm/entities/vaultV1/vaultV1.d.ts +2 -1
  113. package/lib/esm/entities/vaultV1/vaultV1.js +1 -0
  114. package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -1
  115. package/lib/esm/entities/vaultV2/vaultV2.js +1 -0
  116. package/lib/esm/helpers/callback.d.ts +54 -0
  117. package/lib/esm/helpers/callback.js +68 -0
  118. package/lib/esm/helpers/constant.d.ts +2 -0
  119. package/lib/esm/helpers/constant.js +2 -0
  120. package/lib/esm/helpers/index.d.ts +3 -2
  121. package/lib/esm/helpers/index.js +3 -2
  122. package/lib/esm/helpers/signAndVerifyTypedData.d.ts +3 -3
  123. package/lib/esm/helpers/signAndVerifyTypedData.js +3 -3
  124. package/lib/esm/helpers/validate.d.ts +43 -2
  125. package/lib/esm/helpers/validate.js +58 -2
  126. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
  127. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +3 -7
  128. package/lib/esm/helpers/validateRequirementSpender.d.ts +3 -3
  129. package/lib/esm/helpers/validateRequirementSpender.js +4 -5
  130. package/lib/esm/midnight/abis.d.ts +1 -1
  131. package/lib/esm/midnight/abis.js +1 -1
  132. package/lib/esm/midnight/types.d.ts +1 -1
  133. package/lib/esm/types/action.d.ts +117 -89
  134. package/lib/esm/types/action.js +1 -18
  135. package/lib/esm/types/error.d.ts +126 -40
  136. package/lib/esm/types/error.js +135 -38
  137. package/package.json +8 -7
  138. package/lib/cjs/actions/midnight/cancelOffer.d.ts +0 -38
  139. package/lib/cjs/actions/midnight/cancelOffer.js +0 -73
  140. package/lib/cjs/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
  141. package/lib/cjs/actions/midnight/mempoolSubmitOffers.js +0 -72
  142. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
  143. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.js +0 -59
  144. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
  145. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -70
  146. package/lib/esm/actions/midnight/cancelOffer.d.ts +0 -38
  147. package/lib/esm/actions/midnight/cancelOffer.js +0 -69
  148. package/lib/esm/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
  149. package/lib/esm/actions/midnight/mempoolSubmitOffers.js +0 -68
  150. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
  151. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.js +0 -55
  152. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
  153. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -66
@@ -1,60 +1,95 @@
1
- import type { AccrualPosition, Market, MarketInput, MidnightFetchParams, Tree, TreeInput, TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
1
+ import type { InputMarketParams as BlueMarketParams } from "@morpho-org/blue-sdk";
2
+ import type { AccrualPosition, Market, MidnightFetchParams, PriceRatifierV1TreeCreateRequest, RateRatifierV1TreeCreateRequest, Tree, TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
2
3
  import type { Address, Hex } from "viem";
3
- import type { MidnightTakeableOffer } from "../../actions/midnight/types.js";
4
- import type { ActionOutput, BaseAction, MempoolSubmitOffersAction, MidnightOfferRootSignature } from "../../types/action.js";
4
+ import type { MidnightMakeBorrowCollateral } from "../../actions/midnight/cancelAndMake.js";
5
+ import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "../../actions/midnight/types.js";
6
+ import type { ActionOutput, BaseAction, MidnightBuyTarget, MidnightCancelAndMakeAction, MidnightCollateralTransfer, MidnightGroupCancellation, MidnightRepay, MidnightSellTarget } from "../../types/action.js";
5
7
  /** Optional Midnight API validation controls for make-offer flows. */
6
- export type OfferValidationParams = Omit<TreeMempoolValidateParams, "chainId">;
8
+ export type OfferValidationParams = Omit<TreeMempoolValidateParams, "chainId" | "ratification">;
9
+ /**
10
+ * Offers accepted by Midnight maker flows: a PriceRatifierV1 or RateRatifierV1 tree, or
11
+ * the `Tree.create` request that builds one.
12
+ */
13
+ export type MidnightMakerTreeInput = PriceRatifierV1TreeCreateRequest | RateRatifierV1TreeCreateRequest | Tree<"priceV1"> | Tree<"rateV1">;
7
14
  /** Parameters for building and validating Midnight offer data. */
8
15
  export interface GetOffersDataParams {
9
16
  readonly accountAddress: Address;
10
- readonly offers: TreeInput;
17
+ readonly offers: MidnightMakerTreeInput;
11
18
  readonly validation?: OfferValidationParams;
12
19
  }
13
- interface OffersDataBase {
20
+ /** Prepared Midnight maker-offer data with the payload derived from its tree. */
21
+ export interface OffersData {
14
22
  readonly accountAddress: Address;
15
23
  readonly groups: readonly Hex[];
16
- readonly tree: Tree;
24
+ readonly tree: Tree<"priceV1"> | Tree<"rateV1">;
25
+ readonly ratifierType: "priceV1" | "rateV1";
17
26
  readonly ratifier: Address;
27
+ /** Encoded offer payload for `tree.root`. */
28
+ readonly payload: Hex;
18
29
  }
19
- /** Prepared Midnight maker-offer data discriminated by its ratifier execution path. */
20
- export type OffersData = (OffersDataBase & {
21
- readonly ratifierType: "ecrecover";
22
- readonly setterPayload?: never;
23
- }) | (OffersDataBase & {
24
- readonly ratifierType: "setter";
25
- readonly setterPayload: Hex;
26
- });
27
- /** Parameters shared by Midnight maker-offer flows. */
30
+ /** Parameters shared by Midnight maker flows. */
28
31
  export interface MakeOffersParams {
32
+ /** Maker expected on every offer. Must send the transaction: V2 acts on `msg.sender`. */
29
33
  readonly accountAddress: Address;
30
- readonly offers: TreeInput;
34
+ readonly offers: MidnightMakerTreeInput;
35
+ /** Previous offer groups to cancel atomically. Omit or pass `[]` for a new publication. */
36
+ readonly cancellations?: readonly MidnightGroupCancellation[];
37
+ /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
38
+ readonly deadline: bigint;
31
39
  readonly validation?: OfferValidationParams;
32
40
  }
33
- /** Parameters for the Midnight make-lend maker flow. */
41
+ /** Parameters for a Midnight lend maker flow. */
34
42
  export interface MakeLendParams extends MakeOffersParams {
35
43
  readonly loanToken: Address;
36
44
  /** New group loan reserve. For grouped OCA offers, pass the group reserve once instead of summing every leg. */
37
45
  readonly loanAssets: bigint;
38
- /** Existing loan assets reserved across the maker's other open groups, including consumed amounts when available. */
46
+ /** Existing loan assets reserved across the maker's other open groups, including consumed amounts when available. Leave out groups listed in `cancellations`; this transaction cancels them. */
39
47
  readonly reservedLoanAssets?: bigint;
40
48
  }
41
- /** Parameters for the Midnight supply-collateral-and-make-borrow maker flow. */
49
+ /** Parameters for the Midnight Blue-funded lend maker flow. */
50
+ export interface SupplyBlueMakeLendParams extends MakeOffersParams {
51
+ /**
52
+ * Morpho Blue market the parked assets are supplied to; its loan token must match every offer.
53
+ * The contract does not check supply share-price slippage: use only markets protected against
54
+ * supply-share-price inflation. Markets with no supply shares or parked amounts that mint zero
55
+ * shares are rejected.
56
+ */
57
+ readonly blueMarket: BlueMarketParams;
58
+ /** Loan assets pulled from the maker and supplied to `blueMarket` for the maker's callback. */
59
+ readonly assetsToPark: bigint;
60
+ /** Salt selecting the maker's `BlueBuyCallback`; the same salt reuses the same callback. */
61
+ readonly callbackSalt: Hex;
62
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
63
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
64
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
65
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
66
+ readonly nativeAmount?: bigint;
67
+ }
68
+ /** Parameters for the Midnight collateral-supplying borrow maker flow. */
42
69
  export interface SupplyCollateralMakeBorrowParams extends MakeOffersParams {
43
- readonly market: MarketInput;
44
- /** Collateral supplied before offer submission and the new group collateral reserve counted once for grouped offers. */
45
- readonly collateralAssets: bigint;
46
- /** Existing collateral assets reserved across the maker's other open groups, including consumed amounts when available. */
47
- readonly reservedCollateralAssets?: bigint;
48
- readonly collateralIndex?: bigint;
70
+ /** Required collateral supplied before activation; every offer must target `collateral.market`. */
71
+ readonly collateral: MidnightMakeBorrowCollateral;
72
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
73
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
74
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
75
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
76
+ readonly nativeAmount?: bigint;
77
+ }
78
+ /** Options for Midnight `getRequirements()`. */
79
+ export interface MidnightRequirementsParams {
80
+ /**
81
+ * ERC-20 allowance to set when an approval is needed. Defaults to `maxUint256` (capped per token by
82
+ * `MAX_TOKEN_APPROVALS`). Applies to every token the flow approves and must cover the amount each
83
+ * of them pulls. Flows that need no token approval ignore it.
84
+ */
85
+ readonly approvalAmount?: bigint;
49
86
  }
50
- /** Signatures accepted by Midnight action-output transaction builders. */
51
- export type MidnightActionSignatures = MidnightOfferRootSignature | readonly MidnightOfferRootSignature[];
52
87
  /**
53
88
  * Lazy Midnight entity result with async requirements and sync transaction building.
54
89
  *
55
- * Call `getRequirements()` first to collect approvals, authorizations, or
56
- * signatures. Once those are handled, pass any requirement signatures to
57
- * `buildTx`; the transaction builder itself performs no fetching or signing.
90
+ * Call `getRequirements()` first and send the approval and authorization
91
+ * transactions it returns. `buildTx()` takes no signatures and performs no
92
+ * fetching or signing.
58
93
  *
59
94
  * @example
60
95
  * ```ts
@@ -72,24 +107,19 @@ export type MidnightActionSignatures = MidnightOfferRootSignature | readonly Mid
72
107
  * const tx = output.buildTx();
73
108
  * ```
74
109
  */
75
- export type MidnightActionOutput<TAction extends BaseAction, TSignatures = undefined> = ActionOutput<TAction, TSignatures, undefined>;
110
+ export type MidnightActionOutput<TAction extends BaseAction, TSignatures = undefined> = ActionOutput<TAction, TSignatures, MidnightRequirementsParams>;
76
111
  /**
77
- * Output returned by maker-offer flows after offer-tree preparation.
112
+ * Output returned by Midnight maker flows after offer-tree preparation.
78
113
  *
79
- * Use `groups` and `root` for review UI, call `getRequirements()` to collect
80
- * ratifier approval or signature requirements, then call `buildTx(signatures)`
81
- * to publish the encoded payload to the Midnight mempool.
114
+ * Use `groups` and `root` for review UI, send the approval and authorization
115
+ * requirements from `getRequirements()`, then send `buildTx()` from the maker.
82
116
  *
83
117
  * @example
84
118
  * ```ts
85
- * const output = await midnight.makeLend(params);
119
+ * const output = await midnight.makeBorrow(params);
86
120
  * console.log(output.root, output.groups);
87
- * const requirements = await output.getRequirements();
88
- * const signatures = [];
89
- * for (const requirement of requirements) {
90
- * if ("sign" in requirement) {
91
- * signatures.push(await requirement.sign(walletClient, maker));
92
- * } else {
121
+ * for (const requirement of await output.getRequirements()) {
122
+ * if (!("sign" in requirement)) {
93
123
  * await walletClient.sendTransaction({
94
124
  * to: requirement.to,
95
125
  * data: requirement.data,
@@ -97,13 +127,13 @@ export type MidnightActionOutput<TAction extends BaseAction, TSignatures = undef
97
127
  * });
98
128
  * }
99
129
  * }
100
- * const tx = output.buildTx(signatures);
130
+ * await walletClient.sendTransaction(output.buildTx());
101
131
  * ```
102
132
  */
103
- export interface MakeOffersOutput extends MidnightActionOutput<MempoolSubmitOffersAction, MidnightActionSignatures> {
133
+ export interface MakeOffersOutput extends MidnightActionOutput<MidnightCancelAndMakeAction> {
104
134
  readonly groups: readonly Hex[];
105
135
  readonly root: Hex;
106
- readonly ratifierType: "ecrecover" | "setter";
136
+ readonly ratifierType: "priceV1" | "rateV1";
107
137
  }
108
138
  /** Parameters shared by Midnight market action flows. */
109
139
  export interface MarketActionParams {
@@ -111,25 +141,70 @@ export interface MarketActionParams {
111
141
  readonly marketData: Market;
112
142
  }
113
143
  /** Parameters for the Midnight take-lend taker flow. */
114
- export interface TakeLendParams extends MarketActionParams {
115
- readonly assets: bigint;
116
- readonly minUnits: bigint;
144
+ export interface TakeLendParams extends MarketActionParams, MidnightReferralFeeParams {
145
+ /** Loan assets paid with a unit floor, or units bought with a loan-asset cap. */
146
+ readonly target: MidnightBuyTarget;
147
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
148
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
149
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
150
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
151
+ readonly nativeAmount?: bigint;
117
152
  readonly takeableOffers: readonly MidnightTakeableOffer[];
118
- /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
153
+ /** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
154
+ readonly maxContinuousFee: bigint;
155
+ /** Bounded bundle execution deadline timestamp. */
119
156
  readonly deadline: bigint;
120
157
  }
121
158
  /** Parameters for the Midnight take-borrow taker flow. */
122
- export interface TakeBorrowParams extends MarketActionParams {
123
- readonly loanAssets: bigint;
124
- readonly maxUnits: bigint;
159
+ export interface TakeBorrowParams extends MarketActionParams, MidnightReferralFeeParams {
160
+ /** Loan assets received with a unit cap, or units sold with a loan-asset floor. */
161
+ readonly target: MidnightSellTarget;
162
+ /** Recipient of the borrowed loan assets. Defaults to `accountAddress`. */
163
+ readonly receiver?: Address;
125
164
  readonly takeableOffers: readonly MidnightTakeableOffer[];
126
- /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
165
+ /** Bounded bundle execution deadline timestamp. */
166
+ readonly deadline: bigint;
167
+ }
168
+ /** Parameters for the Midnight credit-withdrawal taker flow. */
169
+ export interface TakeWithdrawParams extends MarketActionParams, MidnightReferralFeeParams {
170
+ /** Loan assets received with a credit-unit cap, or credit units sold with a loan-asset floor. */
171
+ readonly target: MidnightSellTarget;
172
+ /** Recipient of the withdrawn loan assets. Defaults to `accountAddress`. */
173
+ readonly receiver?: Address;
174
+ readonly takeableOffers: readonly MidnightTakeableOffer[];
175
+ /** Bounded bundle execution deadline timestamp. */
127
176
  readonly deadline: bigint;
128
177
  }
129
178
  /** Parameters for the Midnight supply-collateral-and-take-borrow taker flow. */
130
179
  export interface SupplyCollateralTakeBorrowParams extends TakeBorrowParams {
131
- readonly collateralAssets: bigint;
132
- readonly collateralIndex?: bigint;
180
+ /** Collateral supplied before borrowing; must not be empty. */
181
+ readonly collateralSupplies: readonly MidnightCollateralTransfer[];
182
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
183
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
184
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
185
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
186
+ readonly nativeAmount?: bigint;
187
+ }
188
+ /** Parameters for the Midnight offer-based repay-and-withdraw-collateral taker flow. */
189
+ export interface TakeRepayWithdrawCollateralParams extends MarketActionParams, MidnightReferralFeeParams {
190
+ /** Loan assets paid with a unit floor, or debt units repaid with a loan-asset cap. `units: maxUint256` targets the whole debt; `assets` and `maxBuyerAssets` bound the loan assets pulled and must be below `maxUint256`. */
191
+ readonly target: MidnightBuyTarget;
192
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
193
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
194
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
195
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
196
+ readonly nativeAmount?: bigint;
197
+ readonly takeableOffers: readonly MidnightTakeableOffer[];
198
+ /** Repays the rest of the target directly to Midnight when offers do not fill it. */
199
+ readonly repayEnabled: boolean;
200
+ /** Collateral withdrawn after repaying. Defaults to none. */
201
+ readonly collateralWithdrawals?: readonly MidnightCollateralTransfer[];
202
+ /** Recipient of withdrawn collateral. Defaults to `accountAddress`. */
203
+ readonly collateralReceiver?: Address;
204
+ /** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
205
+ readonly maxContinuousFee: bigint;
206
+ /** Bounded bundle execution deadline timestamp. */
207
+ readonly deadline: bigint;
133
208
  }
134
209
  /** Parameters for the Midnight supply-collateral flow. */
135
210
  export interface SupplyCollateralParams extends MarketActionParams {
@@ -148,9 +223,17 @@ export interface RedeemParams {
148
223
  }
149
224
  /** Parameters for the Midnight repay-and-withdraw-collateral flow. */
150
225
  export interface RepayWithdrawCollateralParams extends MarketActionParams {
151
- readonly repayAssets: bigint;
152
- readonly withdrawCollateralAssets: bigint;
153
- readonly collateralIndex?: bigint;
226
+ /** `{ type: "assets", assets }` repays exactly `assets` (`0n` only withdraws); `{ type: "full", maxBuyerAssets }` repays the whole debt at execution with a finite asset cap. */
227
+ readonly repay: MidnightRepay;
228
+ /** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
229
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
230
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
231
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
232
+ readonly nativeAmount?: bigint;
233
+ /** `assets: maxUint256` withdraws the whole balance of that collateral. Defaults to none. */
234
+ readonly collateralWithdrawals?: readonly MidnightCollateralTransfer[];
235
+ /** Defaults to `accountAddress`. */
236
+ readonly collateralReceiver?: Address;
154
237
  /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
155
238
  readonly deadline: bigint;
156
239
  }
@@ -161,4 +244,3 @@ export interface GetPositionDataParams {
161
244
  /** Optional fetch controls. Pass an externally fetched block number to coordinate this snapshot with other reads. */
162
245
  readonly parameters?: MidnightFetchParams;
163
246
  }
164
- export {};
@@ -144,7 +144,8 @@ export interface VaultV1Actions {
144
144
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
145
145
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
146
146
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
147
- * its spender, amount or deadline differ from this operation's.
147
+ * its spender or deadline differ from this operation's, or its amount is outside
148
+ * `[cap, cap / (1 - slippageTolerance)]`.
148
149
  * @throws {viem.BaseError} when an allowance or nonce read or transaction encoding fails.
149
150
  * @example
150
151
  * ```ts
@@ -178,6 +178,7 @@ export class MorphoVaultV1 {
178
178
  const permit = selectBundlesSharesPermitSignature(signatures, {
179
179
  spender,
180
180
  amount: requiredShareAllowance,
181
+ maxAmount: maxShareAllowance,
181
182
  deadline,
182
183
  });
183
184
  return vaultV1Withdraw({
@@ -150,7 +150,8 @@ export interface VaultV2Actions {
150
150
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
151
151
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
152
152
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
153
- * its spender, amount or deadline differ from this operation's.
153
+ * its spender or deadline differ from this operation's, or its amount is outside
154
+ * `[cap, cap / (1 - slippageTolerance)]`.
154
155
  * @throws {viem.BaseError} when an allowance or nonce read or transaction encoding fails.
155
156
  * @example
156
157
  * ```ts
@@ -181,6 +181,7 @@ export class MorphoVaultV2 {
181
181
  const permit = selectBundlesSharesPermitSignature(signatures, {
182
182
  spender,
183
183
  amount: requiredShareAllowance,
184
+ maxAmount: maxShareAllowance,
184
185
  deadline,
185
186
  });
186
187
  return vaultV2Withdraw({
@@ -0,0 +1,54 @@
1
+ import { type InputMarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address, type Hex } from "viem";
3
+ /**
4
+ * Computes the CREATE2 address of `owner`'s `BlueBuyCallback` for `salt`, as deployed or
5
+ * returned by the chain's `midnightBlueBuyCallbackFactory.createBlueBuyCallback(owner, salt)`.
6
+ *
7
+ * Use it to fill each offer's `callback` before calling `supplyBlueMakeLend`.
8
+ *
9
+ * @param params.chainId - Chain whose `midnightBlueBuyCallbackFactory`, `midnight` and `blue` are used.
10
+ * @param params.owner - Callback owner, the maker.
11
+ * @param params.salt - Callback salt.
12
+ * @returns The callback address, whether or not it is deployed yet.
13
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
14
+ * @throws {UnknownAddressError} when the chain has no `midnightBlueBuyCallbackFactory`, `midnight` or `blue` deployment.
15
+ * @example
16
+ * ```ts
17
+ * import { getMidnightBlueBuyCallbackAddress } from "@morpho-org/morpho-sdk";
18
+ * import { ChainId } from "@morpho-org/morpho-ts";
19
+ * import { keccak256, toHex } from "viem";
20
+ * import type { Address } from "viem";
21
+ *
22
+ * const callbackFor = (maker: Address) =>
23
+ * getMidnightBlueBuyCallbackAddress({
24
+ * chainId: ChainId.EthMainnet,
25
+ * owner: maker,
26
+ * salt: keccak256(toHex("my-strategy")),
27
+ * });
28
+ * // `callbackFor(maker)` returns the maker's BlueBuyCallback address, deployed or not.
29
+ * ```
30
+ */
31
+ export declare const getMidnightBlueBuyCallbackAddress: ({ chainId, owner, salt, }: {
32
+ readonly chainId: number;
33
+ readonly owner: Address;
34
+ readonly salt: Hex;
35
+ }) => Address;
36
+ /**
37
+ * Returns the `callbackData` every `supplyBlueMakeLend` offer must carry: `abi.encode(blueMarket)`.
38
+ *
39
+ * @param blueMarket - Morpho Blue market encoded as the callback data.
40
+ * @returns The ABI-encoded Morpho Blue market parameters.
41
+ * @example
42
+ * ```ts
43
+ * import { getMidnightBlueBuyCallbackData } from "@morpho-org/morpho-sdk";
44
+ * import type { InputMarketParams as MarketParams } from "@morpho-org/blue-sdk";
45
+ * import type { Hex } from "viem";
46
+ *
47
+ * function getCallbackData(blueMarket: MarketParams) {
48
+ * const callbackData = getMidnightBlueBuyCallbackData(blueMarket);
49
+ * // callbackData satisfies Hex
50
+ * return callbackData;
51
+ * }
52
+ * ```
53
+ */
54
+ export declare const getMidnightBlueBuyCallbackData: (blueMarket: InputMarketParams) => Hex;
@@ -0,0 +1,68 @@
1
+ import { marketParamsAbi } from "@morpho-org/blue-sdk";
2
+ import { getChainAddress } from "@morpho-org/morpho-ts";
3
+ import { concatHex, encodeAbiParameters, getContractAddress, } from "viem";
4
+ /**
5
+ * `BlueBuyCallback` creation bytecode from `morpho-org/midnight` commit
6
+ * `4603969e`, embedded in the `BlueBuyCallbackFactory` deployed on Ethereum, Base, Robinhood and Arc.
7
+ */
8
+ const blueBuyCallbackBytecode = "0x60e0806040523461012657606081611611803803809161001f828561012a565b8339810103126101265761003281610161565b9061004b604061004460208401610161565b9201610161565b908260805260a0528060c05260018060a01b031690813b15610126576040516207767560ed1b81526001600160a01b03909116600482015260016024820152905f908290604490829084905af1801561011b5761010b575b60405161149b9081610176823960805181818160b4015281816102fc0152818161078201528181610dfd0152611004015260a0518181816102210152610dc3015260c051818181610103015281816102650152818161067c015281816108660152610f5e0152f35b5f6101159161012a565b5f6100a3565b6040513d5f823e3d90fd5b5f80fd5b601f909101601f19168101906001600160401b0382119082101761014d57604052565b634e487b7160e01b5f52604160045260245ffd5b51906001600160a01b03821682036101265756fe6080806040526004361015610012575f80fd5b5f905f3560e01c908163117803e314610fe7575080631c0bb54d14610d585780631e686318146107ef5780638069218f1461043d578063affed0e014610420578063bc25cf7714610289578063c011ff6414610245578063dc400c12146102015763eecea00014610081575f80fd5b346101a15760403660031901126101a15761009a61107c565b9060243591821515908184036101fd576001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001633036101ee576040516332f256cf60e11b81523060048201526001600160a01b038281166024830152939485947f000000000000000000000000000000000000000000000000000000000000000016939091602081604481885afa9081156101e35786916101b4575b5015150361014a57505050f35b823b156101af576040516207767560ed1b81526001600160a01b03929092166004830152151560248201529082908290604490829084905af180156101a4576101905750f35b8161019a91611044565b6101a15780f35b80fd5b6040513d84823e3d90fd5b505050fd5b6101d6915060203d6020116101dc575b6101ce8183611044565b810190611358565b5f61013d565b503d6101c4565b6040513d88823e3d90fd5b6330cd747160e01b8352600483fd5b8280fd5b50346101a157806003193601126101a15760206040516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152f35b50346101a157806003193601126101a15760206040516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152f35b50346101a15760203660031901126101a1576102a361107c565b6040516370a0823160e01b81523060048201526001600160a01b03821691602082602481865afa9182156104155784926103dd575b50803b156103ce5760405163a9059cbb60e01b602082019081526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001660248301526044820184905285928392909190839061034881606481015b03601f198101835282611044565b51925af1610354611370565b90156103c65780519081159182156103ac575b50501561039d576040519081527f2ae72b44f59d038340fca5739135a1d51fc5ab720bb02d983e4c5ff4119ca7b860203392a380f35b630bc11c3f60e21b8352600483fd5b6103bf9250602080918301019101611358565b5f80610367565b602081519101fd5b633c11a9c560e21b8452600484fd5b9091506020813d60201161040d575b816103f960209383611044565b810103126104095751905f6102d8565b5f80fd5b3d91506103ec565b6040513d86823e3d90fd5b50346101a157806003193601126101a15760209054604051908152f35b503461040957366003190161010081126104095760a0136104095760405161046481611028565b61046c61107c565b8152602435906001600160a01b038216820361040957602081019182526044359182151583036104095760408201928352606082019160643583526084356080820190808252606060a3193601126104095742116107e05783515f54905f1982146107cc57600182015f55036107bd5760405160208101917f81d0284fb0e2cde18d0553b06189d6f7613c96a01bb5b5e7828eade6a0dcac9183526001600160a01b0384511660408301526001600160a01b038551166060830152865115156080830152855160a08301525160c082015260c0815261054c60e082611044565b51902060405160208101907f47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218825246604082015230606082015260608152610595608082611044565b5190209060405190602082019261190160f01b845260228301526042820152604281526105c3606282611044565b51902060a4359060ff82168092036104095760805f916020936040519182528482015260c435604082015260e435606082015282805260015afa1561073b576001600160a01b035f511680151591826107a9575b5081610776575b5015610767576106af91516040519081527fbae999fc435f6200224444d8535f1936f9abd69060fcaec680f690f1810ae36a60203392a280516040516332f256cf60e11b81523060048201526001600160a01b0391821660248201527f0000000000000000000000000000000000000000000000000000000000000000909116939092602090849081906044820190565b0381875afa92831561073b575f93610746575b5051151580921515036106d3578380f35b516001600160a01b031690823b15610409576040516207767560ed1b81526001600160a01b0392909216600483015215156024820152905f908290604490829084905af1801561073b57610728575b80808380f35b61073491505f90611044565b5f5f610722565b6040513d5f823e3d90fd5b61076091935060203d6020116101dc576101ce8183611044565b915f6106c2565b638baa579f60e01b5f5260045ffd5b90506001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016145f61061e565b516001600160a01b0316811491505f610617565b633ab3447f60e11b5f5260045ffd5b634e487b7160e01b5f52601160045260245ffd5b630f05f5bf60e01b5f5260045ffd5b346104095760803660031901126104095760243567ffffffffffffffff8111610409576108209036906004016110a6565b50610829611066565b5060643567ffffffffffffffff81116104095761084d61085c91369060040161122a565b60208082518301019101611284565b6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169060a0812060405190632e3071cd60e11b8252600482015260c081602481865afa90811561073b575f91610ca8575b50608081016108ce6001600160801b038251164261134b565b9081151580610c92575b80610c7c575b610a82575b50506001600160801b03815116906001600160801b03604081602084015116920151169060a08420604051906349e2903160e11b82526004820152306024820152606081604481895afa90811561073b575f916109f9575b505160018401928385116107cc57620f424083018093116107cc576109766001600160a01b039361097160209661097c956113cc565b611470565b9461134b565b935116936024604051809681936370a0823160e01b835260048301525afa91821561073b575f926109c4575b6020935081811090821802186040519181811090821802188152f35b91506020833d6020116109f1575b816109df60209383611044565b810103126104095760209251916109a8565b3d91506109d2565b90506060813d606011610a7a575b81610a1460609383611044565b8101031261040957604051906060820182811067ffffffffffffffff821117610a6657610a5b91604091825280518452610a5060208201611337565b602085015201611337565b60408201528661093b565b634e487b7160e01b5f52604160045260245ffd5b3d9150610a07565b6060840151604051638c00bf6b60e01b8152906001600160a01b0316610aab60048301876112e6565b6001600160801b0385511660a483015260208501936001600160801b0385511660c48401526001600160801b0360408701948186511660e48601528160608901511661010486015251166101248401526020836101648160a08a01956001600160801b038751166101448301525afa92831561073b575f93610c46575b50610b8b6001600160801b0393610b85610b4f670de0b6b3a76400009487895116936113cc565b610b80671bc16d674ec80000610b6583806113cc565b046729a2241af62c0000610b7984836113cc565b04926113bf565b6113bf565b906113cc565b049282610ba3610b9a866113df565b8284511661139f565b16905281610bbc610bb3856113df565b8288511661139f565b16855251169081610bce575b506108e3565b670de0b6b3a764000091610be1916113cc565b04610bf6816001600160801b0385511661134b565b6001600160801b0383511691620f424083018093116107cc57600182018092116107cc57610c36610c3b926109716001600160801b0395610b9a946113cc565b6113df565b169052838080610bc8565b92506020833d602011610c74575b81610c6160209383611044565b8101031261040957915191610b8b610b28565b3d9150610c54565b506001600160a01b0360608501511615156108de565b506001600160801b0360408401511615156108d8565b905060c0813d60c011610d50575b81610cc360c09383611044565b81010312610409576040519060c0820182811067ffffffffffffffff821117610a6657610d459160a091604052610cf981611337565b8452610d0760208201611337565b6020850152610d1860408201611337565b6040850152610d2960608201611337565b6060850152610d3a60808201611337565b608085015201611337565b60a0820152836108b5565b3d9150610cb6565b346104095760e03660031901126104095760243567ffffffffffffffff811161040957610d899036906004016110a6565b60443560a435916001600160a01b0383168093036104095760c43567ffffffffffffffff811161040957610dc190369060040161122a565b7f0000000000000000000000000000000000000000000000000000000000000000936001600160a01b0385163303610fd8576001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001603610fc95780602080610e3593518301019101611284565b60406001600160a01b038251169201916001600160a01b0383511603610fba5782610f17575b505160405163095ea7b360e01b602082019081526001600160a01b03948516602483015260448201939093525f938493921691908390610e9e816064810161033a565b51925af1610eaa611370565b90156103c6578051908115918215610efd575b505015610eee5760206040517f7f87788ea698181ea4d28d1576d0ba4fc92c0dbe5bf75b43692af2ce91dbaea28152f35b631f55ddd960e21b5f5260045ffd5b610f109250602080918301019101611358565b8180610ebd565b604051635c2bea4960e01b815290610f339060048301906112e6565b8260a48201525f60c48201523060e482015230610104820152604081610124815f6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000165af1801561073b5715610e5b57604090819392933d8311610fb3575b610fa48183611044565b81010312610409579083610e5b565b503d610f9a565b63effb039160e01b5f5260045ffd5b631ec6e83760e21b5f5260045ffd5b631e6a35c760e01b5f5260045ffd5b34610409575f366003190112610409576020906001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152f35b60a0810190811067ffffffffffffffff821117610a6657604052565b90601f8019910116810190811067ffffffffffffffff821117610a6657604052565b604435906001600160a01b038216820361040957565b600435906001600160a01b038216820361040957565b35906001600160a01b038216820361040957565b919091610100818403126104095760405190610100820182811067ffffffffffffffff821117610a66576040528193813583526110e560208301611092565b60208401526110f660408301611092565b6040840152606082013567ffffffffffffffff811161040957820181601f820112156104095780359067ffffffffffffffff8211610a66576040519261114260208460051b0185611044565b82845260208085019360071b8301019181831161040957602001925b8284106111a6575050505060e080926111a19260608601526080810135608086015260a081013560a086015261119660c08201611092565b60c086015201611092565b910152565b6080848303126104095760405190608082019082821067ffffffffffffffff831117610a66576080926020926040526111de87611092565b81528287013583820152604087013560408201526111fe60608801611092565b606082015281520193019261115e565b67ffffffffffffffff8111610a6657601f01601f191660200190565b81601f82011215610409578035906112418261120e565b9261124f6040519485611044565b8284526020838301011161040957815f926020809301838601378301015290565b51906001600160a01b038216820361040957565b908160a09103126104095760806040519161129e83611028565b6112a781611270565b83526112b560208201611270565b60208401526112c660408201611270565b60408401526112d760608201611270565b60608401520151608082015290565b608080916001600160a01b0381511684526001600160a01b0360208201511660208501526001600160a01b0360408201511660408501526001600160a01b0360608201511660608501520151910152565b51906001600160801b038216820361040957565b919082039182116107cc57565b90816020910312610409575180151581036104095790565b3d1561139a573d906113818261120e565b9161138f6040519384611044565b82523d5f602084013e565b606090565b906001600160801b03809116911601906001600160801b0382116107cc57565b919082018092116107cc57565b818102929181159184041417156107cc57565b6040516113ed604082611044565b6014815260208101907f6d61782075696e7431323820657863656564656400000000000000000000000082526001600160801b0383116114355750506001600160801b031690565b60449060405192839162461bcd60e51b8352602060048401525180918160248501528484015e5f828201840152601f01601f19168101030190fd5b811561147a570490565b634e487b7160e01b5f52601260045260245ffdfea164736f6c6343000822000a";
9
+ /**
10
+ * Computes the CREATE2 address of `owner`'s `BlueBuyCallback` for `salt`, as deployed or
11
+ * returned by the chain's `midnightBlueBuyCallbackFactory.createBlueBuyCallback(owner, salt)`.
12
+ *
13
+ * Use it to fill each offer's `callback` before calling `supplyBlueMakeLend`.
14
+ *
15
+ * @param params.chainId - Chain whose `midnightBlueBuyCallbackFactory`, `midnight` and `blue` are used.
16
+ * @param params.owner - Callback owner, the maker.
17
+ * @param params.salt - Callback salt.
18
+ * @returns The callback address, whether or not it is deployed yet.
19
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
20
+ * @throws {UnknownAddressError} when the chain has no `midnightBlueBuyCallbackFactory`, `midnight` or `blue` deployment.
21
+ * @example
22
+ * ```ts
23
+ * import { getMidnightBlueBuyCallbackAddress } from "@morpho-org/morpho-sdk";
24
+ * import { ChainId } from "@morpho-org/morpho-ts";
25
+ * import { keccak256, toHex } from "viem";
26
+ * import type { Address } from "viem";
27
+ *
28
+ * const callbackFor = (maker: Address) =>
29
+ * getMidnightBlueBuyCallbackAddress({
30
+ * chainId: ChainId.EthMainnet,
31
+ * owner: maker,
32
+ * salt: keccak256(toHex("my-strategy")),
33
+ * });
34
+ * // `callbackFor(maker)` returns the maker's BlueBuyCallback address, deployed or not.
35
+ * ```
36
+ */
37
+ export const getMidnightBlueBuyCallbackAddress = ({ chainId, owner, salt, }) => getContractAddress({
38
+ opcode: "CREATE2",
39
+ from: getChainAddress(chainId, "midnightBlueBuyCallbackFactory"),
40
+ salt,
41
+ bytecode: concatHex([
42
+ blueBuyCallbackBytecode,
43
+ encodeAbiParameters([{ type: "address" }, { type: "address" }, { type: "address" }], [
44
+ owner,
45
+ getChainAddress(chainId, "midnight"),
46
+ getChainAddress(chainId, "blue"),
47
+ ]),
48
+ ]),
49
+ });
50
+ /**
51
+ * Returns the `callbackData` every `supplyBlueMakeLend` offer must carry: `abi.encode(blueMarket)`.
52
+ *
53
+ * @param blueMarket - Morpho Blue market encoded as the callback data.
54
+ * @returns The ABI-encoded Morpho Blue market parameters.
55
+ * @example
56
+ * ```ts
57
+ * import { getMidnightBlueBuyCallbackData } from "@morpho-org/morpho-sdk";
58
+ * import type { InputMarketParams as MarketParams } from "@morpho-org/blue-sdk";
59
+ * import type { Hex } from "viem";
60
+ *
61
+ * function getCallbackData(blueMarket: MarketParams) {
62
+ * const callbackData = getMidnightBlueBuyCallbackData(blueMarket);
63
+ * // callbackData satisfies Hex
64
+ * return callbackData;
65
+ * }
66
+ * ```
67
+ */
68
+ export const getMidnightBlueBuyCallbackData = (blueMarket) => encodeAbiParameters([marketParamsAbi], [blueMarket]);
@@ -1,4 +1,6 @@
1
1
  import { type Address } from "viem";
2
+ /** Maximum time between now and a Midnight taker-flow deadline, in seconds. */
3
+ export declare const MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS = 3600n;
2
4
  /** Maximum slippage tolerance: 10% */
3
5
  export declare const MAX_SLIPPAGE_TOLERANCE: bigint;
4
6
  /** Default maximum Vault V2 reallocation penalty: zero. */
@@ -1,6 +1,8 @@
1
1
  import { ChainId, MathLib } from "@morpho-org/blue-sdk";
2
2
  import { Time } from "@morpho-org/morpho-ts";
3
3
  import { maxUint96 } from "viem";
4
+ /** Maximum time between now and a Midnight taker-flow deadline, in seconds. */
5
+ export const MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS = 3600n;
4
6
  /** Maximum slippage tolerance: 10% */
5
7
  export const MAX_SLIPPAGE_TOLERANCE = MathLib.WAD / 10n;
6
8
  /** Default maximum Vault V2 reallocation penalty: zero. */
@@ -1,11 +1,12 @@
1
- export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
1
+ export { getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData, } from "./callback.js";
2
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, } from "./constant.js";
2
3
  export { addTransactionMetadata } from "./metadata.js";
3
4
  export { type PreviewVaultV2ForceWithdrawParams, previewVaultV2ForceWithdraw, type VaultV2ForceWithdrawPreview, } from "./previewVaultV2ForceWithdraw.js";
4
5
  export { type PreviewVaultV2InKindRedeemParams, previewVaultV2InKindRedeem, type VaultV2InKindRedeemMarketPreview, } from "./previewVaultV2InKindRedeem.js";
5
6
  export { grossFromNetAssets } from "./referralFee.js";
6
7
  export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
7
8
  export { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, } from "./slippage.js";
8
- export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
9
+ export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateMidnightNativeAmount, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
9
10
  export { validateOfferSides } from "./validateOfferSides.js";
10
11
  export { type RequirementSpenderKey, validateRequirementSpender, } from "./validateRequirementSpender.js";
11
12
  export { computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, resolveVaultV2ForceWithdrawEligibility, type VaultV2ForceWithdrawEligibility, type VaultV2ForceWithdrawPlan, } from "./vaultV2ForceWithdrawPlan.js";
@@ -1,11 +1,12 @@
1
- export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
1
+ export { getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData, } from "./callback.js";
2
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, } from "./constant.js";
2
3
  export { addTransactionMetadata } from "./metadata.js";
3
4
  export { previewVaultV2ForceWithdraw, } from "./previewVaultV2ForceWithdraw.js";
4
5
  export { previewVaultV2InKindRedeem, } from "./previewVaultV2InKindRedeem.js";
5
6
  export { grossFromNetAssets } from "./referralFee.js";
6
7
  export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
7
8
  export { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, } from "./slippage.js";
8
- export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
9
+ export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateMidnightNativeAmount, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
9
10
  export { validateOfferSides } from "./validateOfferSides.js";
10
11
  export { validateRequirementSpender, } from "./validateRequirementSpender.js";
11
12
  export { computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, resolveVaultV2ForceWithdrawEligibility, } from "./vaultV2ForceWithdrawPlan.js";
@@ -4,7 +4,7 @@ import type { Address, Hex, TypedDataDefinition, WalletClient } from "viem";
4
4
  * `userAddress`, and returns the signature.
5
5
  *
6
6
  * Use inside requirement `sign(...)` callbacks before converting a signature
7
- * into protocol calldata or mempool payload bytes. The verification step keeps
7
+ * into protocol calldata. The verification step keeps
8
8
  * wallet/account mismatches from reaching transaction builders.
9
9
  *
10
10
  * @param params - Signing and verification parameters.
@@ -22,8 +22,8 @@ import type { Address, Hex, TypedDataDefinition, WalletClient } from "viem";
22
22
  *
23
23
  * const signature = await signAndVerifyTypedData({
24
24
  * client: walletClient,
25
- * userAddress: maker,
26
- * typedData: offerRootTypedData,
25
+ * userAddress: user,
26
+ * typedData: permitTypedData,
27
27
  * });
28
28
  * ```
29
29
  */
@@ -7,7 +7,7 @@ import { validateChainId, validateUserAddress } from "./validate.js";
7
7
  * `userAddress`, and returns the signature.
8
8
  *
9
9
  * Use inside requirement `sign(...)` callbacks before converting a signature
10
- * into protocol calldata or mempool payload bytes. The verification step keeps
10
+ * into protocol calldata. The verification step keeps
11
11
  * wallet/account mismatches from reaching transaction builders.
12
12
  *
13
13
  * @param params - Signing and verification parameters.
@@ -25,8 +25,8 @@ import { validateChainId, validateUserAddress } from "./validate.js";
25
25
  *
26
26
  * const signature = await signAndVerifyTypedData({
27
27
  * client: walletClient,
28
- * userAddress: maker,
29
- * typedData: offerRootTypedData,
28
+ * userAddress: user,
29
+ * typedData: permitTypedData,
30
30
  * });
31
31
  * ```
32
32
  */
@@ -70,8 +70,7 @@ export declare const validateMidnightMarketChainId: (market: MidnightMarketInput
70
70
  * the provided user address.
71
71
  *
72
72
  * Used at `sign()` time by the shared `signAndVerifyTypedData` helper (which
73
- * backs the permit, Permit2, Blue authorization, and Midnight offer-root
74
- * signature flows) and by `encodeVaultSharesPermit`, to enforce builder =
73
+ * backs the permit, Permit2, and Blue authorization signature flows) and by `encodeVaultSharesPermit`, to enforce builder =
75
74
  * signer: the signing flow is
76
75
  * the only path where an account/address mismatch is a real security concern
77
76
  * (rather than just an integrator footgun).
@@ -163,6 +162,48 @@ export declare const validateChainId: (clientChainId: number | undefined, expect
163
162
  * @throws {NativeAmountOnNonWNativeAssetError} if the asset is not wNative.
164
163
  */
165
164
  export declare const validateNativeAsset: (chainId: number, asset: Address) => void;
165
+ /**
166
+ * Validates native ETH funding against the wNative tokens pulled by a MidnightBundlesV2 action.
167
+ *
168
+ * @param params.chainId - Chain used to resolve wNative.
169
+ * @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
170
+ * chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
171
+ * maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
172
+ * the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
173
+ * for the full pulled amount.
174
+ * @param params.pulls - Token pulls performed by the bundle action.
175
+ * @returns The transaction `value` and the `wrappedNative` contract argument.
176
+ * @throws {NegativeInputError} when `nativeAmount` is negative.
177
+ * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
178
+ * @throws {NativeAmountOnNonWNativeAssetError} when pulls are non-empty and none use wNative.
179
+ * @throws {InputExceedsMaxError} when `nativeAmount` exceeds the maximum wNative pull, including
180
+ * when the call pulls none.
181
+ * @example
182
+ * ```ts
183
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
184
+ * import { validateMidnightNativeAmount } from "@morpho-org/morpho-sdk";
185
+ *
186
+ * const wNative = getChainAddress(8453, "wNative");
187
+ * const funding = validateMidnightNativeAmount({
188
+ * chainId: 8453,
189
+ * nativeAmount: 1_000n,
190
+ * pulls: [{ token: wNative, assets: 2_000n }],
191
+ * });
192
+ * funding.value; // 1_000n
193
+ * funding.wrappedNative; // wNative
194
+ * ```
195
+ */
196
+ export declare const validateMidnightNativeAmount: (params: {
197
+ readonly chainId: number;
198
+ readonly nativeAmount?: bigint;
199
+ readonly pulls: readonly {
200
+ readonly token: Address;
201
+ readonly assets: bigint;
202
+ }[];
203
+ }) => {
204
+ value: bigint;
205
+ wrappedNative: Address;
206
+ };
166
207
  /** @internal Validates native funding against a vault's wrapped-native asset. */
167
208
  export declare const validateNativeVaultAsset: (chainId: number, vaultAsset: Address) => void;
168
209
  /**