@morpho-org/morpho-sdk 10.4.0 → 10.5.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 (193) hide show
  1. package/lib/cjs/actions/blue/common.js +12 -3
  2. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  3. package/lib/cjs/actions/midnight/takeBorrow.js +14 -8
  4. package/lib/cjs/actions/midnight/takeLend.js +15 -9
  5. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  6. package/lib/cjs/actions/midnight/takeWithdraw.js +14 -8
  7. package/lib/cjs/constants.d.ts +1 -1
  8. package/lib/cjs/constants.js +3 -2
  9. package/lib/cjs/entities/midnight/midnight.d.ts +106 -103
  10. package/lib/cjs/entities/midnight/midnight.js +120 -101
  11. package/lib/cjs/entities/vaultV1/vaultV1.js +0 -1
  12. package/lib/cjs/entities/vaultV2/vaultV2.js +0 -1
  13. package/lib/cjs/helpers/constant.d.ts +7 -0
  14. package/lib/cjs/helpers/constant.js +8 -1
  15. package/lib/cjs/helpers/index.d.ts +1 -1
  16. package/lib/cjs/helpers/index.js +2 -1
  17. package/lib/cjs/helpers/validate.d.ts +25 -1
  18. package/lib/cjs/helpers/validate.js +25 -1
  19. package/lib/cjs/transactionPlan/assemble.d.ts +24 -0
  20. package/lib/cjs/transactionPlan/assemble.js +25 -0
  21. package/lib/cjs/transactionPlan/blue/borrow.js +14 -64
  22. package/lib/cjs/transactionPlan/blue/refinance.js +9 -11
  23. package/lib/cjs/transactionPlan/blue/repay.js +10 -24
  24. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +49 -45
  25. package/lib/cjs/transactionPlan/blue/stored.d.ts +26 -0
  26. package/lib/cjs/transactionPlan/blue/stored.js +87 -0
  27. package/lib/cjs/transactionPlan/blue/supply.js +7 -22
  28. package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  29. package/lib/cjs/transactionPlan/blue/supplyCollateral.js +10 -15
  30. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +14 -64
  31. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +2 -3
  32. package/lib/cjs/transactionPlan/blue/withdraw.js +67 -79
  33. package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  34. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +31 -30
  35. package/lib/cjs/transactionPlan/deposit.js +4 -7
  36. package/lib/cjs/transactionPlan/intent.d.ts +1 -1
  37. package/lib/cjs/transactionPlan/midnight/makeBorrow.d.ts +1 -1
  38. package/lib/cjs/transactionPlan/midnight/makeBorrow.js +1 -1
  39. package/lib/cjs/transactionPlan/midnight/makeLend.d.ts +1 -1
  40. package/lib/cjs/transactionPlan/midnight/makeLend.js +1 -1
  41. package/lib/cjs/transactionPlan/midnight/makeRepay.d.ts +1 -1
  42. package/lib/cjs/transactionPlan/midnight/makeRepay.js +1 -1
  43. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.d.ts +1 -1
  44. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.js +1 -1
  45. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.d.ts +1 -1
  46. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.js +1 -1
  47. package/lib/cjs/transactionPlan/midnight/supplyCollateral.d.ts +1 -1
  48. package/lib/cjs/transactionPlan/midnight/supplyCollateral.js +1 -1
  49. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.d.ts +1 -1
  50. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.js +1 -1
  51. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +1 -1
  52. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.js +1 -1
  53. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.d.ts +1 -1
  54. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.js +1 -1
  55. package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +6 -2
  56. package/lib/cjs/transactionPlan/midnight/takeBorrow.js +10 -4
  57. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  58. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.js +1 -1
  59. package/lib/cjs/transactionPlan/midnight/takeWithdraw.d.ts +1 -1
  60. package/lib/cjs/transactionPlan/midnight/takeWithdraw.js +1 -1
  61. package/lib/cjs/transactionPlan/permit.d.ts +15 -2
  62. package/lib/cjs/transactionPlan/permit.js +26 -68
  63. package/lib/cjs/transactionPlan/plan.d.ts +51 -1
  64. package/lib/cjs/transactionPlan/plan.js +56 -13
  65. package/lib/cjs/transactionPlan/redeem.js +4 -7
  66. package/lib/cjs/transactionPlan/types.d.ts +4 -3
  67. package/lib/cjs/transactionPlan/vaultBinding.d.ts +29 -0
  68. package/lib/cjs/transactionPlan/vaultBinding.js +69 -0
  69. package/lib/cjs/transactionPlan/vaultRequirements.d.ts +20 -0
  70. package/lib/cjs/transactionPlan/vaultRequirements.js +37 -0
  71. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -1
  72. package/lib/cjs/transactionPlan/vaultv1/deposit.js +2 -13
  73. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  74. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +10 -41
  75. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  76. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +6 -18
  77. package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -12
  78. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  79. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +2 -13
  80. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -1
  81. package/lib/cjs/transactionPlan/vaultv2/deposit.js +2 -13
  82. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  83. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +4 -7
  84. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  85. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +12 -42
  86. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  87. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +10 -41
  88. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -1
  89. package/lib/cjs/transactionPlan/vaultv2/redeem.js +2 -13
  90. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  91. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +2 -13
  92. package/lib/cjs/transactionPlan/withdraw.js +4 -7
  93. package/lib/cjs/types/error.d.ts +27 -1
  94. package/lib/cjs/types/error.js +32 -3
  95. package/lib/cjs/version.d.ts +1 -1
  96. package/lib/cjs/version.js +1 -1
  97. package/lib/esm/actions/blue/common.js +13 -4
  98. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  99. package/lib/esm/actions/midnight/takeBorrow.js +14 -8
  100. package/lib/esm/actions/midnight/takeLend.js +15 -9
  101. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  102. package/lib/esm/actions/midnight/takeWithdraw.js +14 -8
  103. package/lib/esm/constants.d.ts +1 -1
  104. package/lib/esm/constants.js +1 -1
  105. package/lib/esm/entities/midnight/midnight.d.ts +106 -103
  106. package/lib/esm/entities/midnight/midnight.js +120 -101
  107. package/lib/esm/entities/vaultV1/vaultV1.js +0 -1
  108. package/lib/esm/entities/vaultV2/vaultV2.js +0 -1
  109. package/lib/esm/helpers/constant.d.ts +7 -0
  110. package/lib/esm/helpers/constant.js +7 -0
  111. package/lib/esm/helpers/index.d.ts +1 -1
  112. package/lib/esm/helpers/index.js +1 -1
  113. package/lib/esm/helpers/validate.d.ts +25 -1
  114. package/lib/esm/helpers/validate.js +25 -2
  115. package/lib/esm/transactionPlan/assemble.d.ts +24 -0
  116. package/lib/esm/transactionPlan/assemble.js +21 -0
  117. package/lib/esm/transactionPlan/blue/borrow.js +15 -65
  118. package/lib/esm/transactionPlan/blue/refinance.js +10 -12
  119. package/lib/esm/transactionPlan/blue/repay.js +11 -25
  120. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +50 -46
  121. package/lib/esm/transactionPlan/blue/stored.d.ts +26 -0
  122. package/lib/esm/transactionPlan/blue/stored.js +80 -0
  123. package/lib/esm/transactionPlan/blue/supply.js +7 -22
  124. package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  125. package/lib/esm/transactionPlan/blue/supplyCollateral.js +10 -15
  126. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +15 -65
  127. package/lib/esm/transactionPlan/blue/withdraw.d.ts +2 -3
  128. package/lib/esm/transactionPlan/blue/withdraw.js +70 -82
  129. package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  130. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +32 -31
  131. package/lib/esm/transactionPlan/deposit.js +5 -8
  132. package/lib/esm/transactionPlan/intent.d.ts +1 -1
  133. package/lib/esm/transactionPlan/midnight/makeBorrow.d.ts +1 -1
  134. package/lib/esm/transactionPlan/midnight/makeBorrow.js +1 -1
  135. package/lib/esm/transactionPlan/midnight/makeLend.d.ts +1 -1
  136. package/lib/esm/transactionPlan/midnight/makeLend.js +1 -1
  137. package/lib/esm/transactionPlan/midnight/makeRepay.d.ts +1 -1
  138. package/lib/esm/transactionPlan/midnight/makeRepay.js +1 -1
  139. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.d.ts +1 -1
  140. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.js +1 -1
  141. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.d.ts +1 -1
  142. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.js +1 -1
  143. package/lib/esm/transactionPlan/midnight/supplyCollateral.d.ts +1 -1
  144. package/lib/esm/transactionPlan/midnight/supplyCollateral.js +1 -1
  145. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.d.ts +1 -1
  146. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.js +1 -1
  147. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +1 -1
  148. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.js +1 -1
  149. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.d.ts +1 -1
  150. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.js +1 -1
  151. package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +6 -2
  152. package/lib/esm/transactionPlan/midnight/takeBorrow.js +11 -5
  153. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  154. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.js +1 -1
  155. package/lib/esm/transactionPlan/midnight/takeWithdraw.d.ts +1 -1
  156. package/lib/esm/transactionPlan/midnight/takeWithdraw.js +1 -1
  157. package/lib/esm/transactionPlan/permit.d.ts +15 -2
  158. package/lib/esm/transactionPlan/permit.js +25 -68
  159. package/lib/esm/transactionPlan/plan.d.ts +51 -1
  160. package/lib/esm/transactionPlan/plan.js +55 -13
  161. package/lib/esm/transactionPlan/redeem.js +5 -8
  162. package/lib/esm/transactionPlan/types.d.ts +4 -3
  163. package/lib/esm/transactionPlan/vaultBinding.d.ts +29 -0
  164. package/lib/esm/transactionPlan/vaultBinding.js +65 -0
  165. package/lib/esm/transactionPlan/vaultRequirements.d.ts +20 -0
  166. package/lib/esm/transactionPlan/vaultRequirements.js +34 -0
  167. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -1
  168. package/lib/esm/transactionPlan/vaultv1/deposit.js +3 -14
  169. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  170. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +12 -43
  171. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  172. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +9 -21
  173. package/lib/esm/transactionPlan/vaultv1/redeem.js +2 -13
  174. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  175. package/lib/esm/transactionPlan/vaultv1/withdraw.js +3 -14
  176. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -1
  177. package/lib/esm/transactionPlan/vaultv2/deposit.js +3 -14
  178. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  179. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +5 -8
  180. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  181. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +15 -45
  182. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  183. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +12 -43
  184. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -1
  185. package/lib/esm/transactionPlan/vaultv2/redeem.js +3 -14
  186. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  187. package/lib/esm/transactionPlan/vaultv2/withdraw.js +3 -14
  188. package/lib/esm/transactionPlan/withdraw.js +5 -8
  189. package/lib/esm/types/error.d.ts +27 -1
  190. package/lib/esm/types/error.js +30 -2
  191. package/lib/esm/version.d.ts +1 -1
  192. package/lib/esm/version.js +1 -1
  193. package/package.json +3 -3
@@ -1,9 +1,10 @@
1
1
  import { Token } from "@morpho-org/blue-sdk";
2
2
  import { getPermitTypedData } from "@morpho-org/blue-sdk-viem";
3
3
  import { getChainAddress } from "@morpho-org/morpho-ts";
4
- import { maxUint256 } from "viem";
4
+ import { selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../actions/bundles/common.js";
5
5
  import { IncompleteTransactionPlanError, TransactionPlanSignatureMismatchError, } from "./error.js";
6
6
  import { getPlanObject, matchesAddress, validatePlanField, validatePlanFields, } from "./validation.js";
7
+ import { getVaultPermitBinding, isVaultPermitAction, } from "./vaultBinding.js";
7
8
  /** @internal Validates the stored ERC-2612 request shape without verifying its signature. */
8
9
  export function validatePermitPlanRequest(request, path) {
9
10
  const args = getPlanObject(request.args, `${path}.args`);
@@ -76,72 +77,10 @@ export function validatePermitPlanRequest(request, path) {
76
77
  }
77
78
  /** @internal Checks a permit's signing payload against its referencing action and encoding arguments. */
78
79
  export function validateVaultPermitRequest(request, action) {
79
- if (request.kind !== "permit") {
80
+ if (request.kind !== "permit" || !isVaultPermitAction(action)) {
80
81
  throw new TransactionPlanSignatureMismatchError(request.id, "reference");
81
82
  }
82
- let spenderKey;
83
- let asset;
84
- let amount;
85
- let exact = false;
86
- switch (action.type) {
87
- case "vaultV2Deposit":
88
- spenderKey = "bundles.vaultBundlesV1";
89
- asset = action.inputs.asset;
90
- amount = action.inputs.assets;
91
- exact = true;
92
- break;
93
- case "vaultV1Deposit":
94
- spenderKey = "bundles.vaultBundlesV1";
95
- asset = action.inputs.asset;
96
- amount = action.inputs.assets;
97
- exact = true;
98
- break;
99
- case "vaultV1Withdraw":
100
- spenderKey = "bundles.vaultBundlesV1";
101
- asset = action.inputs.vault;
102
- amount = action.inputs.requiredShareAllowance;
103
- break;
104
- case "vaultV2Withdraw":
105
- spenderKey = "bundles.vaultBundlesV1";
106
- asset = action.inputs.vault;
107
- amount = action.inputs.requiredShareAllowance;
108
- break;
109
- case "vaultV2Redeem":
110
- spenderKey = "bundles.vaultBundlesV1";
111
- asset = action.inputs.vault;
112
- amount = action.inputs.shares;
113
- break;
114
- case "vaultV1Redeem":
115
- spenderKey = "bundles.vaultBundlesV1";
116
- asset = action.inputs.vault;
117
- amount = action.inputs.shares;
118
- break;
119
- case "vaultV2ForceWithdraw":
120
- spenderKey = "bundles.vaultExitBundlesV1";
121
- asset = action.inputs.vault;
122
- amount = maxUint256;
123
- exact = true;
124
- break;
125
- case "vaultV1MigrateToV2":
126
- spenderKey = "bundles.vaultBundlesV1";
127
- asset = action.inputs.sourceVault;
128
- amount = action.inputs.requiredShareAllowance;
129
- break;
130
- case "vaultV2InKindRedeem":
131
- spenderKey = "bundles.vaultExitBundlesV1";
132
- asset = action.inputs.vault;
133
- amount = maxUint256;
134
- exact = true;
135
- break;
136
- case "vaultV1InKindRedeem":
137
- spenderKey = "bundles.vaultExitBundlesV1";
138
- asset = action.inputs.vault;
139
- amount = maxUint256;
140
- exact = true;
141
- break;
142
- default:
143
- throw new TransactionPlanSignatureMismatchError(request.id, "reference");
144
- }
83
+ const { bundler, asset, restore } = getVaultPermitBinding(action);
145
84
  if (action.signatureRequestIds.tokenPermit !== request.id) {
146
85
  throw new TransactionPlanSignatureMismatchError(request.id, "reference");
147
86
  }
@@ -149,10 +88,10 @@ export function validateVaultPermitRequest(request, action) {
149
88
  chainId: action.inputs.chainId,
150
89
  account: action.inputs.account,
151
90
  deadline: action.inputs.deadline,
152
- bundler: spenderKey,
91
+ bundler,
153
92
  asset,
154
- amount,
155
- exact,
93
+ amount: restore.amount,
94
+ exact: restore.exact,
156
95
  });
157
96
  }
158
97
  /** @internal Binds the signing payload and encoding arguments to a registered bundle's token pull. */
@@ -234,5 +173,23 @@ export function getSignedTokenPermit(requests, { id, asset, owner, }) {
234
173
  },
235
174
  };
236
175
  }
176
+ export function selectVaultPermit(action, requests) {
177
+ const { chainId, account, deadline } = action.inputs;
178
+ const { bundler, asset, build } = getVaultPermitBinding(action);
179
+ const signature = getSignedTokenPermit(requests, {
180
+ id: action.signatureRequestIds.tokenPermit,
181
+ asset,
182
+ owner: account,
183
+ });
184
+ const signatures = signature ? [signature] : [];
185
+ const expected = {
186
+ spender: getChainAddress(chainId, bundler),
187
+ amount: build.amount,
188
+ deadline,
189
+ };
190
+ return build.pull === "token"
191
+ ? selectBundlesTokenRequirementSignature(signatures, expected)
192
+ : selectBundlesSharesPermitSignature(signatures, expected);
193
+ }
237
194
  /** @internal Alias for existing Vault V2 plan builders. */
238
195
  export { getSignedTokenPermit as getVaultPermit };
@@ -57,6 +57,56 @@ export declare function verifyPlanSignature(request: SignatureRequest, signature
57
57
  * ```
58
58
  */
59
59
  export declare function getPendingSignatureRequests(plan: TransactionPlan): readonly SignatureRequest[];
60
+ /**
61
+ * @internal Whether a step's transaction carries the plan's `calldataOrigin`. Operation steps do;
62
+ * approval and authorization prerequisites never do, matching the entity path. Every action type
63
+ * must be listed, so a new type does not compile until its role is chosen.
64
+ */
65
+ export declare const CALLDATA_ORIGIN_ROLE: {
66
+ readonly blueBorrow: "operation";
67
+ readonly blueRefinance: "operation";
68
+ readonly blueRepay: "operation";
69
+ readonly blueRepayWithdrawCollateral: "operation";
70
+ readonly blueSupply: "operation";
71
+ readonly blueSupplyCollateral: "operation";
72
+ readonly blueSupplyCollateralBorrow: "operation";
73
+ readonly blueWithdraw: "operation";
74
+ readonly blueWithdrawBuyCallback: "operation";
75
+ readonly blueWithdrawCollateral: "operation";
76
+ readonly erc20Approval: "prerequisite";
77
+ readonly midnightAuthorization: "prerequisite";
78
+ readonly midnightCancelOffers: "operation";
79
+ readonly midnightMakeBorrow: "operation";
80
+ readonly midnightMakeLend: "operation";
81
+ readonly midnightMakeRepay: "operation";
82
+ readonly midnightMakeWithdraw: "operation";
83
+ readonly midnightRedeem: "operation";
84
+ readonly midnightRepayWithdrawCollateral: "operation";
85
+ readonly midnightSupplyBlueMakeLend: "operation";
86
+ readonly midnightSupplyCollateral: "operation";
87
+ readonly midnightSupplyCollateralBatch: "operation";
88
+ readonly midnightSupplyCollateralMakeBorrow: "operation";
89
+ readonly midnightSupplyCollateralTakeBorrow: "operation";
90
+ readonly midnightSupplyCollateralWithNative: "operation";
91
+ readonly midnightTakeBorrow: "operation";
92
+ readonly midnightTakeLend: "operation";
93
+ readonly midnightTakeRepayWithdrawCollateral: "operation";
94
+ readonly midnightTakeWithdraw: "operation";
95
+ readonly midnightWithdrawCollateral: "operation";
96
+ readonly midnightWithdrawCollateralBatch: "operation";
97
+ readonly morphoAuthorization: "prerequisite";
98
+ readonly vaultV1Deposit: "operation";
99
+ readonly vaultV1InKindRedeem: "operation";
100
+ readonly vaultV1MigrateToV2: "operation";
101
+ readonly vaultV1Redeem: "operation";
102
+ readonly vaultV1Withdraw: "operation";
103
+ readonly vaultV2Deposit: "operation";
104
+ readonly vaultV2ForceRedeem: "operation";
105
+ readonly vaultV2ForceWithdraw: "operation";
106
+ readonly vaultV2InKindRedeem: "operation";
107
+ readonly vaultV2Redeem: "operation";
108
+ readonly vaultV2Withdraw: "operation";
109
+ };
60
110
  /**
61
111
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
62
112
  * Builds one transaction from a step, its fixed inputs and the signatures its action names.
@@ -68,7 +118,7 @@ export declare function getPendingSignatureRequests(plan: TransactionPlan): read
68
118
  * not operation amounts, market parameters or referral fees/recipients. Protect those unsigned fields.
69
119
  * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
70
120
  * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
71
- * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
121
+ * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set, except for ERC-20 approval, Morpho authorization and Midnight authorization prerequisite steps.
72
122
  * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
73
123
  * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
74
124
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
@@ -34,6 +34,7 @@ import { buildMidnightWithdrawCollateralBatch } from "./midnight/withdrawCollate
34
34
  import { validateVaultPermitRequest } from "./permit.js";
35
35
  import { validateTransactionPlanActions } from "./validateActions.js";
36
36
  import { validateTransactionPlanHeader } from "./validation.js";
37
+ import { isVaultPermitAction } from "./vaultBinding.js";
37
38
  import { buildVaultV1Deposit } from "./vaultv1/deposit.js";
38
39
  import { buildVaultV1InKindRedeem } from "./vaultv1/inKindRedeem.js";
39
40
  import { buildVaultV1MigrateToV2 } from "./vaultv1/migrateToV2.js";
@@ -244,17 +245,7 @@ function validateSignatureRequest(request, action) {
244
245
  nonce: action.inputs.authorizationNonce,
245
246
  });
246
247
  }
247
- if (request.kind === "permit" &&
248
- (action.type === "vaultV2Deposit" ||
249
- action.type === "vaultV1Deposit" ||
250
- action.type === "vaultV1Withdraw" ||
251
- action.type === "vaultV2Withdraw" ||
252
- action.type === "vaultV2Redeem" ||
253
- action.type === "vaultV1Redeem" ||
254
- action.type === "vaultV2ForceWithdraw" ||
255
- action.type === "vaultV1MigrateToV2" ||
256
- action.type === "vaultV2InKindRedeem" ||
257
- action.type === "vaultV1InKindRedeem")) {
248
+ if (request.kind === "permit" && isVaultPermitAction(action)) {
258
249
  validateVaultPermitRequest(request, action);
259
250
  return;
260
251
  }
@@ -402,6 +393,56 @@ function encodeStep(action, requests) {
402
393
  return buildVaultV2InKindRedeem(action, requests);
403
394
  }
404
395
  }
396
+ /**
397
+ * @internal Whether a step's transaction carries the plan's `calldataOrigin`. Operation steps do;
398
+ * approval and authorization prerequisites never do, matching the entity path. Every action type
399
+ * must be listed, so a new type does not compile until its role is chosen.
400
+ */
401
+ export const CALLDATA_ORIGIN_ROLE = {
402
+ blueBorrow: "operation",
403
+ blueRefinance: "operation",
404
+ blueRepay: "operation",
405
+ blueRepayWithdrawCollateral: "operation",
406
+ blueSupply: "operation",
407
+ blueSupplyCollateral: "operation",
408
+ blueSupplyCollateralBorrow: "operation",
409
+ blueWithdraw: "operation",
410
+ blueWithdrawBuyCallback: "operation",
411
+ blueWithdrawCollateral: "operation",
412
+ erc20Approval: "prerequisite",
413
+ midnightAuthorization: "prerequisite",
414
+ midnightCancelOffers: "operation",
415
+ midnightMakeBorrow: "operation",
416
+ midnightMakeLend: "operation",
417
+ midnightMakeRepay: "operation",
418
+ midnightMakeWithdraw: "operation",
419
+ midnightRedeem: "operation",
420
+ midnightRepayWithdrawCollateral: "operation",
421
+ midnightSupplyBlueMakeLend: "operation",
422
+ midnightSupplyCollateral: "operation",
423
+ midnightSupplyCollateralBatch: "operation",
424
+ midnightSupplyCollateralMakeBorrow: "operation",
425
+ midnightSupplyCollateralTakeBorrow: "operation",
426
+ midnightSupplyCollateralWithNative: "operation",
427
+ midnightTakeBorrow: "operation",
428
+ midnightTakeLend: "operation",
429
+ midnightTakeRepayWithdrawCollateral: "operation",
430
+ midnightTakeWithdraw: "operation",
431
+ midnightWithdrawCollateral: "operation",
432
+ midnightWithdrawCollateralBatch: "operation",
433
+ morphoAuthorization: "prerequisite",
434
+ vaultV1Deposit: "operation",
435
+ vaultV1InKindRedeem: "operation",
436
+ vaultV1MigrateToV2: "operation",
437
+ vaultV1Redeem: "operation",
438
+ vaultV1Withdraw: "operation",
439
+ vaultV2Deposit: "operation",
440
+ vaultV2ForceRedeem: "operation",
441
+ vaultV2ForceWithdraw: "operation",
442
+ vaultV2InKindRedeem: "operation",
443
+ vaultV2Redeem: "operation",
444
+ vaultV2Withdraw: "operation",
445
+ };
405
446
  /**
406
447
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
407
448
  * Builds one transaction from a step, its fixed inputs and the signatures its action names.
@@ -413,7 +454,7 @@ function encodeStep(action, requests) {
413
454
  * not operation amounts, market parameters or referral fees/recipients. Protect those unsigned fields.
414
455
  * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
415
456
  * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
416
- * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
457
+ * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set, except for ERC-20 approval, Morpho authorization and Midnight authorization prerequisite steps.
417
458
  * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
418
459
  * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
419
460
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
@@ -518,7 +559,8 @@ export function buildTransaction(plan, step) {
518
559
  return deepFreeze({
519
560
  to,
520
561
  value,
521
- data: plan.calldataOrigin === undefined
562
+ data: plan.calldataOrigin === undefined ||
563
+ CALLDATA_ORIGIN_ROLE[step.action.type] === "prerequisite"
522
564
  ? data
523
565
  : concatHex([data, plan.calldataOrigin]),
524
566
  action,
@@ -1,7 +1,8 @@
1
- import { deepFreeze, Time } from "@morpho-org/morpho-ts";
1
+ import { Time } from "@morpho-org/morpho-ts";
2
2
  import { normalizeBundlesCommonParams } from "../actions/bundles/common.js";
3
3
  import { validateUint256Field } from "../helpers/validate.js";
4
4
  import { NonPositiveInputError, } from "../types/index.js";
5
+ import { freezePlan, planHeader } from "./assemble.js";
5
6
  import { resolveIntent } from "./intent.js";
6
7
  import { getVaultShareSpendingRequirements } from "./shareSpending.js";
7
8
  import { validatePlanFields, validateVaultPlanAction } from "./validation.js";
@@ -26,12 +27,8 @@ export async function prepareVaultRedeem(client, params) {
26
27
  supportSignature: intent.supportSignature,
27
28
  now,
28
29
  });
29
- return deepFreeze(structuredClone({
30
- version: 2,
31
- account,
32
- chainId,
33
- expiresAt: intent.expiresAt,
34
- ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
30
+ return freezePlan({
31
+ ...planHeader({ chainId, account }, intent),
35
32
  steps: [
36
33
  ...approvals,
37
34
  {
@@ -47,7 +44,7 @@ export async function prepareVaultRedeem(client, params) {
47
44
  },
48
45
  ],
49
46
  signatureRequests,
50
- }));
47
+ }, { detach: true });
51
48
  }
52
49
  /** @internal Validates stored V1 and V2 exact-share redemption inputs. */
53
50
  export function validateRedeemPlanAction(action, path) {
@@ -9,6 +9,7 @@ import type { MidnightTakeBorrowParams } from "../actions/midnight/takeBorrow.js
9
9
  import type { MidnightTakeLendParams } from "../actions/midnight/takeLend.js";
10
10
  import type { MidnightTakeRepayWithdrawCollateralParams } from "../actions/midnight/takeRepayWithdrawCollateral.js";
11
11
  import type { MidnightTakeableOffer } from "../actions/midnight/types.js";
12
+ import type { BlueReallocationInput } from "../helpers/validate.js";
12
13
  import type { MidnightBlueSupply, MidnightCollateralTransfer } from "../types/action.js";
13
14
  import type { AuthorizationSignatureArgs, PermitAction, RequirementTypedData, VaultV1MigrateToV2AmountArgs, VaultV2BlueReallocation } from "../types/index.js";
14
15
  import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
@@ -255,12 +256,12 @@ export type Action = {
255
256
  } | {
256
257
  readonly type: "blueWithdraw";
257
258
  readonly inputs: ActionInputs & {
258
- readonly marketParams: Pick<MarketParams, "loanToken" | "collateralToken" | "oracle" | "irm" | "lltv">;
259
+ readonly marketParams: Readonly<InputMarketParams>;
259
260
  readonly withdrawAssets: bigint;
260
261
  readonly withdrawShares: bigint;
261
262
  /** Maximum WAD-scaled penalty rate per reallocation, defaulting to zero; it does not bound total proceeds lost. */
262
263
  readonly maxPenalty?: bigint;
263
- readonly reallocations: readonly VaultV2BlueReallocation[];
264
+ readonly reallocations: readonly BlueReallocationInput[];
264
265
  readonly deadline: bigint;
265
266
  readonly referralFeePct: bigint;
266
267
  readonly referralFeeRecipient: Address;
@@ -470,7 +471,7 @@ export interface TransactionPlan {
470
471
  readonly account: Address;
471
472
  /** Unix seconds. One expiry governs every step deadline and signature deadline. */
472
473
  readonly expiresAt: number;
473
- /** Appended verbatim to every built transaction's calldata. */
474
+ /** Appended verbatim to the action transaction's calldata; ERC-20 approval, Morpho authorization and Midnight authorization steps carry none. */
474
475
  readonly calldataOrigin?: Hex;
475
476
  readonly steps: readonly Step[];
476
477
  readonly signatureRequests: readonly SignatureRequest[];
@@ -0,0 +1,29 @@
1
+ import { type Address } from "viem";
2
+ import type { Action } from "./types.js";
3
+ declare const VAULT_PERMIT_ACTION_TYPES: readonly ["vaultV2Deposit", "vaultV1Deposit", "vaultV1Withdraw", "vaultV2Withdraw", "vaultV2Redeem", "vaultV1Redeem", "vaultV2ForceWithdraw", "vaultV1MigrateToV2", "vaultV2InKindRedeem", "vaultV1InKindRedeem"];
4
+ /** @internal Vault actions whose step pulls a token through a bundle and may consume a permit. */
5
+ export type VaultPermitAction = Extract<Action, {
6
+ readonly type: (typeof VAULT_PERMIT_ACTION_TYPES)[number];
7
+ }>;
8
+ /**
9
+ * @internal The bundle, signed token and amounts a vault action's token permit must match.
10
+ * `restore` binds the stored signing payload; `build` is what the bundle pull selects, which differs
11
+ * from `restore` for deposits (`assets` versus `assets ?? nativeAssets`).
12
+ */
13
+ export interface VaultPermitBinding {
14
+ readonly bundler: "bundles.vaultBundlesV1" | "bundles.vaultExitBundlesV1";
15
+ readonly asset: Address;
16
+ readonly restore: {
17
+ readonly amount: bigint | undefined;
18
+ readonly exact: boolean;
19
+ };
20
+ readonly build: {
21
+ readonly amount: bigint;
22
+ readonly pull: "token" | "shares";
23
+ };
24
+ }
25
+ /** @internal Whether the action is one of the vault actions a token permit can fund. */
26
+ export declare function isVaultPermitAction(action: Action): action is VaultPermitAction;
27
+ /** @internal Reads the permit binding from the vault action's stored inputs. */
28
+ export declare function getVaultPermitBinding(action: VaultPermitAction): VaultPermitBinding;
29
+ export {};
@@ -0,0 +1,65 @@
1
+ import { maxUint256 } from "viem";
2
+ const VAULT_PERMIT_ACTION_TYPES = [
3
+ "vaultV2Deposit",
4
+ "vaultV1Deposit",
5
+ "vaultV1Withdraw",
6
+ "vaultV2Withdraw",
7
+ "vaultV2Redeem",
8
+ "vaultV1Redeem",
9
+ "vaultV2ForceWithdraw",
10
+ "vaultV1MigrateToV2",
11
+ "vaultV2InKindRedeem",
12
+ "vaultV1InKindRedeem",
13
+ ];
14
+ /** @internal Whether the action is one of the vault actions a token permit can fund. */
15
+ export function isVaultPermitAction(action) {
16
+ return VAULT_PERMIT_ACTION_TYPES.includes(action.type);
17
+ }
18
+ /** @internal Reads the permit binding from the vault action's stored inputs. */
19
+ export function getVaultPermitBinding(action) {
20
+ switch (action.type) {
21
+ case "vaultV2Deposit":
22
+ case "vaultV1Deposit":
23
+ return {
24
+ bundler: "bundles.vaultBundlesV1",
25
+ asset: action.inputs.asset,
26
+ restore: { amount: action.inputs.assets, exact: true },
27
+ build: {
28
+ amount: action.inputs.assets ?? action.inputs.nativeAssets,
29
+ pull: "token",
30
+ },
31
+ };
32
+ case "vaultV1Withdraw":
33
+ case "vaultV2Withdraw":
34
+ return {
35
+ bundler: "bundles.vaultBundlesV1",
36
+ asset: action.inputs.vault,
37
+ restore: { amount: action.inputs.requiredShareAllowance, exact: false },
38
+ build: { amount: action.inputs.requiredShareAllowance, pull: "shares" },
39
+ };
40
+ case "vaultV2Redeem":
41
+ case "vaultV1Redeem":
42
+ return {
43
+ bundler: "bundles.vaultBundlesV1",
44
+ asset: action.inputs.vault,
45
+ restore: { amount: action.inputs.shares, exact: false },
46
+ build: { amount: action.inputs.shares, pull: "shares" },
47
+ };
48
+ case "vaultV1MigrateToV2":
49
+ return {
50
+ bundler: "bundles.vaultBundlesV1",
51
+ asset: action.inputs.sourceVault,
52
+ restore: { amount: action.inputs.requiredShareAllowance, exact: false },
53
+ build: { amount: action.inputs.requiredShareAllowance, pull: "shares" },
54
+ };
55
+ case "vaultV2ForceWithdraw":
56
+ case "vaultV2InKindRedeem":
57
+ case "vaultV1InKindRedeem":
58
+ return {
59
+ bundler: "bundles.vaultExitBundlesV1",
60
+ asset: action.inputs.vault,
61
+ restore: { amount: maxUint256, exact: true },
62
+ build: { amount: maxUint256, pull: "shares" },
63
+ };
64
+ }
65
+ }
@@ -0,0 +1,20 @@
1
+ import type { Address } from "viem";
2
+ import { type ActionRequirement } from "../types/index.js";
3
+ import type { SignatureRequest, Step } from "./types.js";
4
+ /**
5
+ * @internal Turns a vault exit's share requirements into approval steps and permit requests.
6
+ * Steps take the ID `${id}:approval` and requests `${id}:tokenPermit`; `signatureRequestIds`
7
+ * names the permit only when one is requested.
8
+ * @throws {UnsupportedRequirementSignatureError} If a requirement is a signature other than a permit.
9
+ */
10
+ export declare function vaultRequirementSteps(requirements: readonly ActionRequirement[], { id, chainId, account, }: {
11
+ readonly id: string;
12
+ readonly chainId: number;
13
+ readonly account: Address;
14
+ }): {
15
+ readonly approvals: Step[];
16
+ readonly signatureRequests: SignatureRequest[];
17
+ readonly signatureRequestIds: {
18
+ readonly tokenPermit?: string;
19
+ };
20
+ };
@@ -0,0 +1,34 @@
1
+ import { isRequirementApproval, UnsupportedRequirementSignatureError, } from "../types/index.js";
2
+ import { buildApprovalStep, buildPermitSignatureRequest, } from "./requirements.js";
3
+ /**
4
+ * @internal Turns a vault exit's share requirements into approval steps and permit requests.
5
+ * Steps take the ID `${id}:approval` and requests `${id}:tokenPermit`; `signatureRequestIds`
6
+ * names the permit only when one is requested.
7
+ * @throws {UnsupportedRequirementSignatureError} If a requirement is a signature other than a permit.
8
+ */
9
+ export function vaultRequirementSteps(requirements, { id, chainId, account, }) {
10
+ const approvals = [];
11
+ const signatureRequests = [];
12
+ const tokenPermit = `${id}:tokenPermit`;
13
+ for (const requirement of requirements) {
14
+ if (isRequirementApproval(requirement)) {
15
+ approvals.push(buildApprovalStep(requirement, {
16
+ id: `${id}:approval`,
17
+ chainId,
18
+ account,
19
+ denomination: "shares",
20
+ }));
21
+ }
22
+ else if (requirement.action.type === "permit") {
23
+ signatureRequests.push(buildPermitSignatureRequest({ action: requirement.action }, tokenPermit));
24
+ }
25
+ else {
26
+ throw new UnsupportedRequirementSignatureError(requirement.action.type);
27
+ }
28
+ }
29
+ return {
30
+ approvals,
31
+ signatureRequests,
32
+ signatureRequestIds: signatureRequests.length > 0 ? { tokenPermit } : {},
33
+ };
34
+ }
@@ -47,7 +47,7 @@ export interface VaultV1DepositPlanParams extends DepositPlanParams<AccrualVault
47
47
  * @param params.supportSignature - Optional ERC-2612 preference, defaulting to `false`; when enabled, falls back to direct approval if no permit is available.
48
48
  * @param params.now - Reference timestamp in seconds, normally the latest block's `timestamp`; uses the device clock when omitted.
49
49
  * @param params.expiresAt - Optional Unix timestamp in seconds as a safe integer, later than `now`; defaults to `now + 2h`.
50
- * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
50
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to the action transaction's calldata; approval and authorization steps carry none.
51
51
  * @param params.deployless - Optional permission to use deployless aggregation for token permit metadata reads.
52
52
  * @returns A plan with approvals, concrete signature requests and the deposit step.
53
53
  * @throws {InvalidBigIntInputError} when `now` is not a bigint.
@@ -1,8 +1,6 @@
1
- import { getChainAddress } from "@morpho-org/morpho-ts";
2
- import { selectBundlesTokenRequirementSignature } from "../../actions/bundles/common.js";
3
1
  import { vaultV1Deposit } from "../../actions/vaultV1/deposit.js";
4
2
  import { prepareVaultDeposit } from "../deposit.js";
5
- import { getVaultPermit } from "../permit.js";
3
+ import { selectVaultPermit } from "../permit.js";
6
4
  /**
7
5
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
8
6
  * Prepares a Vault V1 (MetaMorpho) deposit plan through VaultBundlesV1 from caller intent and a
@@ -21,7 +19,7 @@ import { getVaultPermit } from "../permit.js";
21
19
  * @param params.supportSignature - Optional ERC-2612 preference, defaulting to `false`; when enabled, falls back to direct approval if no permit is available.
22
20
  * @param params.now - Reference timestamp in seconds, normally the latest block's `timestamp`; uses the device clock when omitted.
23
21
  * @param params.expiresAt - Optional Unix timestamp in seconds as a safe integer, later than `now`; defaults to `now + 2h`.
24
- * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
22
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to the action transaction's calldata; approval and authorization steps carry none.
25
23
  * @param params.deployless - Optional permission to use deployless aggregation for token permit metadata reads.
26
24
  * @returns A plan with approvals, concrete signature requests and the deposit step.
27
25
  * @throws {InvalidBigIntInputError} when `now` is not a bigint.
@@ -67,16 +65,7 @@ export async function prepareVaultV1Deposit(client, params) {
67
65
  /** @internal Encodes the deposit from its self-contained inputs and resolved signature requests. */
68
66
  export function buildVaultV1Deposit(action, requests) {
69
67
  const { asset, chainId, account, assets, nativeAssets, ...args } = action.inputs;
70
- const signature = getVaultPermit(requests, {
71
- id: action.signatureRequestIds.tokenPermit,
72
- asset,
73
- owner: account,
74
- });
75
- const requirementSignature = selectBundlesTokenRequirementSignature(signature ? [signature] : [], {
76
- spender: getChainAddress(chainId, "bundles.vaultBundlesV1"),
77
- amount: assets ?? nativeAssets,
78
- deadline: action.inputs.deadline,
79
- });
68
+ const requirementSignature = selectVaultPermit(action, requests);
80
69
  return vaultV1Deposit({
81
70
  vault: { chainId, address: action.inputs.vault, asset },
82
71
  args: {
@@ -1,6 +1,6 @@
1
1
  import type { AccrualVault, MarketParams } from "@morpho-org/blue-sdk";
2
- import { type Client } from "viem";
3
- import { type ReferenceTimestampParams, type Transaction, type VaultV1InKindRedeemAction } from "../../types/index.js";
2
+ import type { Client } from "viem";
3
+ import type { ReferenceTimestampParams, Transaction, VaultV1InKindRedeemAction } from "../../types/index.js";
4
4
  import { type IntentParams } from "../intent.js";
5
5
  import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
6
6
  /**