@morpho-org/morpho-sdk 9.8.0 → 10.0.0-next.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 (181) hide show
  1. package/MIGRATION-v9-to-v10.md +114 -0
  2. package/README.md +5 -1
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +5 -1
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  7. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
  8. package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
  9. package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
  10. package/lib/cjs/actions/midnight/cancelOffers.js +107 -0
  11. package/lib/cjs/actions/midnight/index.d.ts +1 -0
  12. package/lib/cjs/actions/midnight/index.js +1 -0
  13. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  15. package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
  16. package/lib/cjs/actions/midnight/takeBorrow.js +1 -1
  17. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
  18. package/lib/cjs/actions/midnight/takeLend.js +1 -1
  19. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  21. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
  22. package/lib/cjs/actions/midnight/takeWithdraw.js +1 -1
  23. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  25. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  31. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  33. package/lib/cjs/blue/abis.d.ts +1 -1
  34. package/lib/cjs/blue/abis.js +5 -1
  35. package/lib/cjs/blue/entities.d.ts +4 -4
  36. package/lib/cjs/blue/entities.js +3 -1
  37. package/lib/cjs/blue/errors.d.ts +2 -2
  38. package/lib/cjs/blue/errors.js +5 -1
  39. package/lib/cjs/blue/fetch.d.ts +2 -2
  40. package/lib/cjs/blue/fetch.js +3 -1
  41. package/lib/cjs/blue/utils.d.ts +1 -1
  42. package/lib/cjs/blue/utils.js +2 -1
  43. package/lib/cjs/entities/blue/blue.d.ts +34 -28
  44. package/lib/cjs/entities/blue/blue.js +99 -73
  45. package/lib/cjs/entities/index.d.ts +4 -4
  46. package/lib/cjs/entities/index.js +3 -1
  47. package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
  48. package/lib/cjs/entities/midnight/deadline.js +2 -2
  49. package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
  50. package/lib/cjs/entities/midnight/midnight.js +144 -131
  51. package/lib/cjs/entities/midnight/publication.d.ts +2 -0
  52. package/lib/cjs/entities/midnight/publication.js +126 -100
  53. package/lib/cjs/entities/midnight/types.d.ts +4 -4
  54. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  55. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +17 -17
  56. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  57. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  58. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
  59. package/lib/cjs/entities/vaultV1/vaultV1.js +83 -57
  60. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +50 -24
  61. package/lib/cjs/entities/vaultV2/vaultV2.js +166 -90
  62. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +17 -2
  63. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1410 -806
  64. package/lib/cjs/errors.d.ts +4 -4
  65. package/lib/cjs/errors.js +7 -5
  66. package/lib/cjs/fetch.d.ts +2 -2
  67. package/lib/cjs/fetch.js +3 -1
  68. package/lib/cjs/helpers/deadShares.js +55 -34
  69. package/lib/cjs/helpers/metadata.d.ts +14 -2
  70. package/lib/cjs/helpers/metadata.js +39 -4
  71. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +34 -22
  72. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +7 -1
  73. package/lib/cjs/helpers/validate.js +76 -61
  74. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +154 -103
  75. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  76. package/lib/cjs/midnight/errors.d.ts +2 -1
  77. package/lib/cjs/midnight/errors.js +5 -4
  78. package/lib/cjs/transactionPlan/plan.js +6 -3
  79. package/lib/cjs/transactionPlan/serde.js +7 -7
  80. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +6 -4
  81. package/lib/cjs/transactionPlan/vaultv2/deposit.js +5 -3
  82. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  83. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +5 -3
  84. package/lib/cjs/types/action.d.ts +26 -2
  85. package/lib/cjs/types/action.js +1 -1
  86. package/lib/cjs/types/error.js +11 -5
  87. package/lib/cjs/types/metadata.d.ts +12 -2
  88. package/lib/cjs/utils.d.ts +2 -2
  89. package/lib/cjs/utils.js +4 -2
  90. package/lib/cjs/version.d.ts +1 -1
  91. package/lib/cjs/version.js +1 -1
  92. package/lib/esm/abis.d.ts +1 -1
  93. package/lib/esm/abis.js +1 -1
  94. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  95. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  96. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
  97. package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
  98. package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
  99. package/lib/esm/actions/midnight/cancelOffers.js +103 -0
  100. package/lib/esm/actions/midnight/index.d.ts +1 -0
  101. package/lib/esm/actions/midnight/index.js +1 -0
  102. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  103. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  104. package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
  105. package/lib/esm/actions/midnight/takeBorrow.js +1 -1
  106. package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
  107. package/lib/esm/actions/midnight/takeLend.js +1 -1
  108. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  109. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  110. package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
  111. package/lib/esm/actions/midnight/takeWithdraw.js +1 -1
  112. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  113. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  114. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  115. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  116. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  117. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
  118. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  119. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  120. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  121. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  122. package/lib/esm/blue/abis.d.ts +1 -1
  123. package/lib/esm/blue/abis.js +1 -1
  124. package/lib/esm/blue/entities.d.ts +4 -4
  125. package/lib/esm/blue/entities.js +2 -2
  126. package/lib/esm/blue/errors.d.ts +2 -2
  127. package/lib/esm/blue/errors.js +2 -2
  128. package/lib/esm/blue/fetch.d.ts +2 -2
  129. package/lib/esm/blue/fetch.js +2 -2
  130. package/lib/esm/blue/utils.d.ts +1 -1
  131. package/lib/esm/blue/utils.js +1 -1
  132. package/lib/esm/entities/blue/blue.d.ts +34 -28
  133. package/lib/esm/entities/blue/blue.js +99 -73
  134. package/lib/esm/entities/index.d.ts +4 -4
  135. package/lib/esm/entities/index.js +2 -2
  136. package/lib/esm/entities/midnight/deadline.d.ts +2 -2
  137. package/lib/esm/entities/midnight/deadline.js +2 -2
  138. package/lib/esm/entities/midnight/midnight.d.ts +49 -51
  139. package/lib/esm/entities/midnight/midnight.js +145 -132
  140. package/lib/esm/entities/midnight/publication.d.ts +2 -0
  141. package/lib/esm/entities/midnight/publication.js +126 -100
  142. package/lib/esm/entities/midnight/types.d.ts +4 -4
  143. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  144. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +17 -17
  145. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  146. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  147. package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
  148. package/lib/esm/entities/vaultV1/vaultV1.js +83 -57
  149. package/lib/esm/entities/vaultV2/vaultV2.d.ts +50 -24
  150. package/lib/esm/entities/vaultV2/vaultV2.js +166 -90
  151. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +17 -2
  152. package/lib/esm/entities/vaultV2BlueReallocationData.js +1410 -806
  153. package/lib/esm/errors.d.ts +4 -4
  154. package/lib/esm/errors.js +4 -4
  155. package/lib/esm/fetch.d.ts +2 -2
  156. package/lib/esm/fetch.js +2 -2
  157. package/lib/esm/helpers/deadShares.js +56 -35
  158. package/lib/esm/helpers/metadata.d.ts +14 -2
  159. package/lib/esm/helpers/metadata.js +38 -4
  160. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +34 -22
  161. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +7 -1
  162. package/lib/esm/helpers/validate.js +76 -61
  163. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +153 -102
  164. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  165. package/lib/esm/midnight/errors.d.ts +2 -1
  166. package/lib/esm/midnight/errors.js +2 -1
  167. package/lib/esm/transactionPlan/plan.js +6 -3
  168. package/lib/esm/transactionPlan/serde.js +7 -7
  169. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +6 -4
  170. package/lib/esm/transactionPlan/vaultv2/deposit.js +5 -3
  171. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  172. package/lib/esm/transactionPlan/vaultv2/withdraw.js +5 -3
  173. package/lib/esm/types/action.d.ts +26 -2
  174. package/lib/esm/types/action.js +1 -1
  175. package/lib/esm/types/error.js +11 -5
  176. package/lib/esm/types/metadata.d.ts +12 -2
  177. package/lib/esm/utils.d.ts +2 -2
  178. package/lib/esm/utils.js +2 -2
  179. package/lib/esm/version.d.ts +1 -1
  180. package/lib/esm/version.js +1 -1
  181. package/package.json +8 -7
@@ -8,6 +8,7 @@ import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBl
8
8
  import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawBuyCallback, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
9
9
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
10
10
  import { DEFAULT_LLTV_BUFFER, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
11
+ import { resolveTransactionMetadata } from "../../helpers/metadata.js";
11
12
  import { validateMarketParamsId } from "../../helpers/validate.js";
12
13
  import { BlueBuyCallbackOwnerMismatchError, BlueBuyCallbackUnauthorizedError, EmptyBlueSupplyPositionError, ExpiredDeadlineError, InputExceedsMaxError, InsufficientBlueLiquidityError, MarketIdMismatchError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, NotBlueBuyCallbackError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
13
14
  import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
@@ -38,9 +39,9 @@ export class MorphoBlue {
38
39
  throw new ExpiredDeadlineError(deadline, now);
39
40
  }
40
41
  }
41
- validateWriteCommon(params) {
42
+ validateWriteCommon(params, now) {
42
43
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
43
- this.validateDeadline(params.deadline);
44
+ this.validateDeadline(params.deadline, now);
44
45
  // Resolve at handle creation so unsupported deployments fail before any RPC prerequisite work.
45
46
  getChainAddress(this.chainId, "bundles.blueBundlesV1");
46
47
  // Validate the referral inputs eagerly; the normalized result is intentionally discarded here
@@ -72,8 +73,43 @@ export class MorphoBlue {
72
73
  getMaxLtv(marketParams = this.marketParams) {
73
74
  return MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER);
74
75
  }
75
- getBlueBundlesV1QuoteTimestamp(lastUpdate) {
76
- return MathLib.max(Time.timestamp(), lastUpdate) + Time.s.from.h(2n);
76
+ getBlueBundlesV1QuoteTimestamp(lastUpdate, now = Time.timestamp()) {
77
+ return MathLib.max(now, lastUpdate) + Time.s.from.h(2n);
78
+ }
79
+ quoteRepay({ positionData, repayAssets, repayShares, deadline, now, }) {
80
+ const saturatedRepay = repayShares === maxUint256;
81
+ const simulatedRepayShares = saturatedRepay
82
+ ? positionData.borrowShares
83
+ : repayShares;
84
+ if (repayAssets > 0n) {
85
+ validateRepayAmount({
86
+ positionData,
87
+ repayAssets,
88
+ marketId: this.marketParams.id,
89
+ });
90
+ }
91
+ else if (repayShares > 0n && !saturatedRepay) {
92
+ validateRepayShares({
93
+ positionData,
94
+ repayShares,
95
+ marketId: this.marketParams.id,
96
+ });
97
+ }
98
+ const quoteHorizon = this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate, now);
99
+ if (repayShares > 0n && deadline > quoteHorizon) {
100
+ throw new InputExceedsMaxError({
101
+ field: "deadline",
102
+ value: deadline,
103
+ max: quoteHorizon,
104
+ });
105
+ }
106
+ const accrualTimestamp = repayShares > 0n ? deadline : quoteHorizon;
107
+ const accruedPosition = positionData.accrueInterest(accrualTimestamp);
108
+ let expectedRepayAssets = repayAssets;
109
+ if (repayAssets <= 0n && repayShares > 0n) {
110
+ expectedRepayAssets = accruedPosition.market.toBorrowAssets(simulatedRepayShares, "Up");
111
+ }
112
+ return { simulatedRepayShares, accruedPosition, expectedRepayAssets };
77
113
  }
78
114
  async getTokenRequirements(params) {
79
115
  this.validateDeadline(params.deadline, params.now);
@@ -118,8 +154,11 @@ export class MorphoBlue {
118
154
  });
119
155
  }
120
156
  supply(params) {
157
+ const suppliedNow = params.now;
158
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
121
159
  const { userAddress, assets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
122
- this.validateWriteCommon(params);
160
+ const createdAt = suppliedNow ?? Time.timestamp();
161
+ this.validateWriteCommon(params, createdAt);
123
162
  if (assets <= 0n)
124
163
  throw new NonPositiveInputError("assets", assets);
125
164
  const nativeValue = validateBlueBundlesV1NativeFunding({
@@ -135,7 +174,7 @@ export class MorphoBlue {
135
174
  }
136
175
  return {
137
176
  getRequirements: (requirementsParams) => {
138
- const now = Time.timestamp();
177
+ const now = suppliedNow ?? Time.timestamp();
139
178
  this.validateDeadline(deadline, now);
140
179
  return this.assertWriteTargets().then(() => {
141
180
  if (nativeValue > 0n)
@@ -165,15 +204,18 @@ export class MorphoBlue {
165
204
  referralFeeRecipient,
166
205
  requirementSignature: token,
167
206
  },
168
- metadata: this.client.options.metadata,
207
+ metadata,
169
208
  });
170
209
  },
171
210
  };
172
211
  }
173
212
  /** {@inheritDoc BlueActions.withdraw} */
174
213
  withdraw(params) {
214
+ const suppliedNow = params.now;
215
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
175
216
  const { userAddress, positionData, deadline, referralFeePct, referralFeeRecipient, } = params;
176
- this.validateWriteCommon(params);
217
+ const createdAt = suppliedNow ?? Time.timestamp();
218
+ this.validateWriteCommon(params, createdAt);
177
219
  const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
178
220
  const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
179
221
  if (assets < 0n)
@@ -194,20 +236,13 @@ export class MorphoBlue {
194
236
  expectedMarketId: this.marketParams.id,
195
237
  expectedUser: userAddress,
196
238
  });
197
- if (shares > 0n) {
198
- validateWithdrawShares({
199
- positionData,
200
- withdrawShares: shares,
201
- marketId: this.marketParams.id,
202
- });
203
- }
204
- else {
205
- validateWithdrawAmount({
206
- positionData,
207
- withdrawAssets: assets,
208
- marketId: this.marketParams.id,
209
- });
210
- }
239
+ const validateWithdrawal = shares > 0n ? validateWithdrawShares : validateWithdrawAmount;
240
+ validateWithdrawal({
241
+ positionData,
242
+ withdrawShares: shares,
243
+ withdrawAssets: assets,
244
+ marketId: this.marketParams.id,
245
+ });
211
246
  const reallocations = [...(params.reallocations ?? [])];
212
247
  // Validate the exact PublicAllocations mapping before requirement reads.
213
248
  const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, this.marketParams);
@@ -222,7 +257,7 @@ export class MorphoBlue {
222
257
  }
223
258
  return {
224
259
  getRequirements: async () => {
225
- const now = Time.timestamp();
260
+ const now = suppliedNow ?? Time.timestamp();
226
261
  this.validateDeadline(deadline, now);
227
262
  await this.assertWriteTargets();
228
263
  return this.getAuthorizationRequirements({
@@ -247,13 +282,14 @@ export class MorphoBlue {
247
282
  referralFeeRecipient,
248
283
  authorizationSignature: authorization,
249
284
  },
250
- metadata: this.client.options.metadata,
285
+ metadata,
251
286
  });
252
287
  },
253
288
  };
254
289
  }
255
290
  /** {@inheritDoc BlueActions.withdrawBuyCallback} */
256
291
  withdrawBuyCallback(params) {
292
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
257
293
  const { userAddress, callbackAddress, positionData } = params;
258
294
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
259
295
  if (positionData == null) {
@@ -285,7 +321,7 @@ export class MorphoBlue {
285
321
  const tx = blueWithdrawBuyCallback({
286
322
  market: { chainId: this.chainId, marketParams: this.marketParams },
287
323
  args: { callbackAddress, userAddress, shares },
288
- metadata: this.client.options.metadata,
324
+ metadata,
289
325
  });
290
326
  return {
291
327
  getRequirements: async () => {
@@ -338,8 +374,10 @@ export class MorphoBlue {
338
374
  }
339
375
  /** {@inheritDoc BlueActions.supplyCollateral} */
340
376
  supplyCollateral(params) {
377
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
341
378
  const { userAddress, collateralAssets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
342
379
  const combinedAction = this.supplyCollateralBorrow({
380
+ now: params.now,
343
381
  userAddress,
344
382
  collateralAssets,
345
383
  borrowAssets: 0n,
@@ -365,16 +403,18 @@ export class MorphoBlue {
365
403
  referralFeeRecipient,
366
404
  requirementSignature: token,
367
405
  },
368
- metadata: this.client.options.metadata,
406
+ metadata,
369
407
  });
370
408
  },
371
409
  };
372
410
  }
373
411
  /** {@inheritDoc BlueActions.borrow} */
374
412
  borrow(params) {
413
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
375
414
  const { userAddress, borrowAssets, positionData, reallocations, deadline, referralFeePct, referralFeeRecipient, } = params;
376
415
  const normalizedReallocations = [...(reallocations ?? [])];
377
416
  const combinedAction = this.supplyCollateralBorrow({
417
+ now: params.now,
378
418
  userAddress,
379
419
  collateralAssets: 0n,
380
420
  borrowAssets,
@@ -400,13 +440,14 @@ export class MorphoBlue {
400
440
  referralFeeRecipient,
401
441
  authorizationSignature: authorization,
402
442
  },
403
- metadata: this.client.options.metadata,
443
+ metadata,
404
444
  });
405
445
  },
406
446
  };
407
447
  }
408
448
  /** {@inheritDoc BlueActions.repay} */
409
449
  repay(params) {
450
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
410
451
  const combinedAction = this.repayWithdrawCollateral({
411
452
  ...params,
412
453
  collateralAssets: 0n,
@@ -432,15 +473,17 @@ export class MorphoBlue {
432
473
  referralFeeRecipient,
433
474
  requirementSignature: token,
434
475
  },
435
- metadata: this.client.options.metadata,
476
+ metadata,
436
477
  });
437
478
  },
438
479
  };
439
480
  }
440
481
  /** {@inheritDoc BlueActions.withdrawCollateral} */
441
482
  withdrawCollateral(params) {
483
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
442
484
  const { userAddress, positionData, collateralAssets, deadline, referralFeePct, referralFeeRecipient, } = params;
443
485
  const combinedAction = this.repayWithdrawCollateral({
486
+ now: params.now,
444
487
  userAddress,
445
488
  positionData,
446
489
  repayAssets: 0n,
@@ -466,15 +509,18 @@ export class MorphoBlue {
466
509
  referralFeeRecipient: combinedArgs.referralFeeRecipient,
467
510
  authorizationSignature: authorization,
468
511
  },
469
- metadata: this.client.options.metadata,
512
+ metadata,
470
513
  });
471
514
  },
472
515
  };
473
516
  }
474
517
  /** {@inheritDoc BlueActions.repayWithdrawCollateral} */
475
518
  repayWithdrawCollateral(params) {
519
+ const suppliedNow = params.now;
520
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
476
521
  const { userAddress, positionData, collateralAssets, nativeAmount, deadline, referralFeeRecipient, } = params;
477
- this.validateWriteCommon(params);
522
+ const createdAt = suppliedNow ?? Time.timestamp();
523
+ this.validateWriteCommon(params, createdAt);
478
524
  // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
479
525
  // here, matching the pure action's normalization.
480
526
  const referralFeePct = params.referralFeePct ?? 0n;
@@ -504,39 +550,13 @@ export class MorphoBlue {
504
550
  expectedMarketId: this.marketParams.id,
505
551
  expectedUser: userAddress,
506
552
  });
507
- const saturatedRepay = repayShares === maxUint256;
508
- const simulatedRepayShares = saturatedRepay
509
- ? positionData.borrowShares
510
- : repayShares;
511
- if (repayAssets > 0n) {
512
- validateRepayAmount({
513
- positionData,
514
- repayAssets,
515
- marketId: this.marketParams.id,
516
- });
517
- }
518
- else if (repayShares > 0n && !saturatedRepay) {
519
- validateRepayShares({
520
- positionData,
521
- repayShares,
522
- marketId: this.marketParams.id,
523
- });
524
- }
525
- const quoteHorizon = this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate);
526
- if (repayShares > 0n && deadline > quoteHorizon) {
527
- throw new InputExceedsMaxError({
528
- field: "deadline",
529
- value: deadline,
530
- max: quoteHorizon,
531
- });
532
- }
533
- const accrualTimestamp = repayShares > 0n ? deadline : quoteHorizon;
534
- const accruedPosition = positionData.accrueInterest(accrualTimestamp);
535
- const expectedRepayAssets = repayAssets > 0n
536
- ? repayAssets
537
- : hasRepay
538
- ? accruedPosition.market.toBorrowAssets(simulatedRepayShares, "Up")
539
- : 0n;
553
+ const { simulatedRepayShares, accruedPosition, expectedRepayAssets } = this.quoteRepay({
554
+ positionData,
555
+ repayAssets,
556
+ repayShares,
557
+ deadline,
558
+ now: createdAt,
559
+ });
540
560
  const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(expectedRepayAssets, referralFeePct);
541
561
  const maxRepayAssets = expectedRepayAssets + referralFeeAssets;
542
562
  const nativeValue = validateBlueBundlesV1NativeFunding({
@@ -558,7 +578,7 @@ export class MorphoBlue {
558
578
  }
559
579
  return {
560
580
  getRequirements: async (requirementsParams) => {
561
- const now = Time.timestamp();
581
+ const now = suppliedNow ?? Time.timestamp();
562
582
  this.validateDeadline(deadline, now);
563
583
  await this.assertWriteTargets();
564
584
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
@@ -609,15 +629,18 @@ export class MorphoBlue {
609
629
  requirementSignature: token,
610
630
  authorizationSignature: authorization,
611
631
  },
612
- metadata: this.client.options.metadata,
632
+ metadata,
613
633
  });
614
634
  },
615
635
  };
616
636
  }
617
637
  /** {@inheritDoc BlueActions.supplyCollateralBorrow} */
618
638
  supplyCollateralBorrow(params) {
639
+ const suppliedNow = params.now;
640
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
619
641
  const { userAddress, collateralAssets, borrowAssets, positionData, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
620
- this.validateWriteCommon(params);
642
+ const createdAt = suppliedNow ?? Time.timestamp();
643
+ this.validateWriteCommon(params, createdAt);
621
644
  if (collateralAssets < 0n) {
622
645
  throw new NegativeInputError("collateralAssets", collateralAssets);
623
646
  }
@@ -651,7 +674,7 @@ export class MorphoBlue {
651
674
  expectedUser: userAddress,
652
675
  });
653
676
  validatePositionHealth({
654
- positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate)),
677
+ positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate, createdAt)),
655
678
  additionalCollateral: collateralAssets,
656
679
  borrowAmount: borrowAssets,
657
680
  marketId: this.marketParams.id,
@@ -666,7 +689,7 @@ export class MorphoBlue {
666
689
  });
667
690
  return {
668
691
  getRequirements: async (requirementsParams) => {
669
- const now = Time.timestamp();
692
+ const now = suppliedNow ?? Time.timestamp();
670
693
  this.validateDeadline(deadline, now);
671
694
  await this.assertWriteTargets();
672
695
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
@@ -710,15 +733,18 @@ export class MorphoBlue {
710
733
  requirementSignature: token,
711
734
  authorizationSignature: authorization,
712
735
  },
713
- metadata: this.client.options.metadata,
736
+ metadata,
714
737
  });
715
738
  },
716
739
  };
717
740
  }
718
741
  /** {@inheritDoc BlueActions.refinance} */
719
742
  refinance(params) {
743
+ const suppliedNow = params.now;
744
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
720
745
  const { userAddress, positionData, destination, deadline, referralFeeRecipient, } = params;
721
- this.validateWriteCommon(params);
746
+ const createdAt = suppliedNow ?? Time.timestamp();
747
+ this.validateWriteCommon(params, createdAt);
722
748
  validateMarketParamsId(destination.marketParams);
723
749
  // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
724
750
  // here, matching the pure action's normalization.
@@ -752,7 +778,7 @@ export class MorphoBlue {
752
778
  const reallocations = [...(params.reallocations ?? [])];
753
779
  const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, destination.marketParams);
754
780
  const penaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
755
- const accrualTimestamp = this.getBlueBundlesV1QuoteTimestamp(MathLib.max(positionData.market.lastUpdate, destination.positionData.market.lastUpdate));
781
+ const accrualTimestamp = this.getBlueBundlesV1QuoteTimestamp(MathLib.max(positionData.market.lastUpdate, destination.positionData.market.lastUpdate), createdAt);
756
782
  const accruedSourcePosition = positionData.accrueInterest(accrualTimestamp);
757
783
  const accruedDestinationPosition = destination.positionData.accrueInterest(accrualTimestamp);
758
784
  const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(accruedSourcePosition.borrowAssets, referralFeePct);
@@ -765,7 +791,7 @@ export class MorphoBlue {
765
791
  });
766
792
  return {
767
793
  getRequirements: async () => {
768
- const now = Time.timestamp();
794
+ const now = suppliedNow ?? Time.timestamp();
769
795
  this.validateDeadline(deadline, now);
770
796
  await this.assertWriteTargets();
771
797
  return this.getAuthorizationRequirements({
@@ -793,7 +819,7 @@ export class MorphoBlue {
793
819
  referralFeeRecipient,
794
820
  authorizationSignature: authorization,
795
821
  },
796
- metadata: this.client.options.metadata,
822
+ metadata,
797
823
  });
798
824
  },
799
825
  };
@@ -1,13 +1,13 @@
1
- export type { IAccrualPosition as IBlueAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket as IBlueMarket, IMarketParams as IBlueMarketParams, IPosition as IBluePosition,
1
+ export type { IAccrualPosition as IBlueAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MidnightAdapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket as IBlueMarket, IMarketParams as IBlueMarketParams, IPosition as IBluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  IPreLiquidationParams as IBluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- IPreLiquidationPosition as IBluePreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, } from "@morpho-org/blue-sdk";
6
- export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
5
+ IPreLiquidationPosition as IBluePreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MidnightAdapter, IVaultV2MidnightAdapterAccrualMarket, IVaultV2MidnightAdapterCollateralParams, IVaultV2MidnightAdapterMarket, IVaultV2MidnightAdapterMarketData, IVaultV2MidnightAdapterMarketState, IVaultV2MidnightAdapterMidnightMarket, IVaultV2MidnightAdapterPosition, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, VaultV2MidnightAdapterAccrualMarket, VaultV2MidnightAdapterMarketData, } from "@morpho-org/blue-sdk";
6
+ export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
7
7
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
8
8
  PreLiquidationParams as BluePreLiquidationParams,
9
9
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
10
- PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
10
+ PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
11
11
  export type { AnyTree as MidnightAnyTree, AnyTreeSnapshot as MidnightAnyTreeSnapshot, BuildOfferParams as MidnightBuildOfferParams, CollateralParams as MidnightCollateralParams, EcrecoverTreeCreateRequest as MidnightEcrecoverTreeCreateRequest, GroupInput as MidnightGroupInput, IAccrualPosition as IMidnightAccrualPosition, ICollateralParams as IMidnightCollateralParams, IGroup as IMidnightGroup, IMarket as IMidnightMarket, IMarketParams as IMidnightMarketParams, IOffer as IMidnightOffer, IPosition as IMidnightPosition, MarketInput as MidnightMarketInput, OfferStruct as MidnightOfferStruct, PriceRatifierV1TreeCreateRequest as MidnightPriceRatifierV1TreeCreateRequest, RateRatifierV1TreeCreateRequest as MidnightRateRatifierV1TreeCreateRequest, RatifierKind as MidnightRatifierKind, RatifierTreeInput as MidnightRatifierTreeInput, RatifierTypes as MidnightRatifierTypes, SetterTreeCreateRequest as MidnightSetterTreeCreateRequest, SettlementFeeCbps as MidnightSettlementFeeCbps, TreeCreateParams as MidnightTreeCreateParams, TreeCreateRequest as MidnightTreeCreateRequest, TreeData as MidnightTreeData, TreeDescriptor as MidnightTreeDescriptor, TreeEntry as MidnightTreeEntry, TreeInput as MidnightTreeInput, TreeLike as MidnightTreeLike, TreeMempoolValidateParams as MidnightTreeMempoolValidateParams, TreeMempoolValidateRatification as MidnightTreeMempoolValidateRatification, TreeProof as MidnightTreeProof, TreeSnapshot as MidnightTreeSnapshot, TypedRatifierTreeInput as MidnightTypedRatifierTreeInput, TypedTreeMempoolValidateParams as MidnightTypedTreeMempoolValidateParams, } from "@morpho-org/midnight-sdk";
12
12
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
13
13
  export type { BlueActions, BlueTokenRequirementsParams, } from "./blue/index.js";
@@ -1,8 +1,8 @@
1
- export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
1
+ export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  PreLiquidationParams as BluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
5
+ PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
6
6
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
7
7
  export { MorphoBlue } from "./blue/index.js";
8
8
  export { MorphoMidnight, } from "./midnight/index.js";
@@ -3,11 +3,11 @@
3
3
  *
4
4
  * Midnight bundles revert with `DeadlinePassed()` once the deadline has passed, so a deadline at
5
5
  * or before `now` builds a transaction that cannot succeed. `maxUint256` still means
6
- * "no expiry". The check uses the local clock (`Time.timestamp()`), not chain time. Transaction
6
+ * "no expiry". The check uses the supplied reference timestamp, falling back to the device clock. Transaction
7
7
  * builders stay clock-free and only check the deadline's `uint256` shape.
8
8
  *
9
9
  * @param deadline - Bundle execution deadline timestamp, in seconds.
10
- * @param now - Reference timestamp, in seconds. Defaults to `Time.timestamp()`.
10
+ * @param now - Timestamp in seconds used to validate the deadline; defaults to `Time.timestamp()`.
11
11
  * @returns Nothing when `deadline` is after `now`.
12
12
  * @throws {ExpiredDeadlineError} when `deadline` is at or before `now`.
13
13
  * @internal
@@ -5,11 +5,11 @@ import { ExpiredDeadlineError } from "../../types/index.js";
5
5
  *
6
6
  * Midnight bundles revert with `DeadlinePassed()` once the deadline has passed, so a deadline at
7
7
  * or before `now` builds a transaction that cannot succeed. `maxUint256` still means
8
- * "no expiry". The check uses the local clock (`Time.timestamp()`), not chain time. Transaction
8
+ * "no expiry". The check uses the supplied reference timestamp, falling back to the device clock. Transaction
9
9
  * builders stay clock-free and only check the deadline's `uint256` shape.
10
10
  *
11
11
  * @param deadline - Bundle execution deadline timestamp, in seconds.
12
- * @param now - Reference timestamp, in seconds. Defaults to `Time.timestamp()`.
12
+ * @param now - Timestamp in seconds used to validate the deadline; defaults to `Time.timestamp()`.
13
13
  * @returns Nothing when `deadline` is after `now`.
14
14
  * @throws {ExpiredDeadlineError} when `deadline` is at or before `now`.
15
15
  * @internal