@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/MIGRATION-v5-to-v6.md +404 -0
  2. package/README.md +127 -69
  3. package/lib/cjs/abis.d.ts +210 -0
  4. package/lib/cjs/abis.js +142 -2
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -61
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +86 -0
  8. package/lib/cjs/actions/blue/common.js +205 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -112
  10. package/lib/cjs/actions/blue/refinance.js +97 -199
  11. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  12. package/lib/cjs/actions/blue/repay.js +56 -118
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  15. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  16. package/lib/cjs/actions/blue/supply.js +94 -70
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  18. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
  22. package/lib/cjs/actions/blue/withdraw.js +101 -97
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  25. package/lib/cjs/actions/bundles/common.d.ts +200 -0
  26. package/lib/cjs/actions/bundles/common.js +469 -0
  27. package/lib/cjs/actions/bundles/index.d.ts +3 -0
  28. package/lib/cjs/actions/bundles/index.js +23 -0
  29. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  30. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
  31. package/lib/cjs/actions/index.d.ts +2 -1
  32. package/lib/cjs/actions/index.js +1 -0
  33. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  34. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  35. package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
  36. package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
  37. package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
  38. package/lib/cjs/actions/midnight/takeLend.js +2 -2
  39. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  40. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  41. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  42. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  43. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  44. package/lib/cjs/actions/requirements/blue/index.js +1 -0
  45. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  46. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  48. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
  49. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  50. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
  51. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  52. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
  53. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  54. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  55. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  56. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  57. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  58. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
  59. package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
  60. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  61. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  62. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  63. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
  64. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  65. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  66. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  67. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  68. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  69. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  70. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  71. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  72. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  73. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  74. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  75. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  76. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  77. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  78. package/lib/cjs/bundler/actions.d.ts +4 -4
  79. package/lib/cjs/bundler/actions.js +4 -4
  80. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  81. package/lib/cjs/client/morphoViemExtension.js +2 -1
  82. package/lib/cjs/entities/blue/blue.d.ts +806 -374
  83. package/lib/cjs/entities/blue/blue.js +449 -696
  84. package/lib/cjs/entities/index.d.ts +2 -0
  85. package/lib/cjs/entities/index.js +15 -0
  86. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  87. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  88. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
  89. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  90. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  91. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  92. package/lib/cjs/entities/requirements/index.js +17 -0
  93. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
  94. package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
  95. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
  96. package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
  97. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
  98. package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
  99. package/lib/cjs/helpers/index.d.ts +2 -1
  100. package/lib/cjs/helpers/index.js +5 -1
  101. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  102. package/lib/cjs/helpers/referralFee.js +35 -0
  103. package/lib/cjs/helpers/slippage.d.ts +90 -1
  104. package/lib/cjs/helpers/slippage.js +123 -0
  105. package/lib/cjs/helpers/validate.d.ts +16 -23
  106. package/lib/cjs/helpers/validate.js +41 -48
  107. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  108. package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
  109. package/lib/cjs/index.d.ts +1 -0
  110. package/lib/cjs/index.js +1 -0
  111. package/lib/cjs/types/action.d.ts +303 -210
  112. package/lib/cjs/types/action.js +29 -2
  113. package/lib/cjs/types/client.d.ts +9 -0
  114. package/lib/cjs/types/error.d.ts +193 -18
  115. package/lib/cjs/types/error.js +267 -24
  116. package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
  117. package/lib/esm/abis.d.ts +210 -0
  118. package/lib/esm/abis.js +140 -0
  119. package/lib/esm/actions/blue/borrow.d.ts +62 -61
  120. package/lib/esm/actions/blue/borrow.js +48 -80
  121. package/lib/esm/actions/blue/common.d.ts +86 -0
  122. package/lib/esm/actions/blue/common.js +193 -0
  123. package/lib/esm/actions/blue/refinance.d.ts +72 -112
  124. package/lib/esm/actions/blue/refinance.js +98 -200
  125. package/lib/esm/actions/blue/repay.d.ts +72 -68
  126. package/lib/esm/actions/blue/repay.js +56 -118
  127. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  128. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  129. package/lib/esm/actions/blue/supply.d.ts +62 -45
  130. package/lib/esm/actions/blue/supply.js +94 -70
  131. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  132. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  133. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  134. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
  135. package/lib/esm/actions/blue/withdraw.d.ts +68 -79
  136. package/lib/esm/actions/blue/withdraw.js +100 -96
  137. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  138. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  139. package/lib/esm/actions/bundles/common.d.ts +200 -0
  140. package/lib/esm/actions/bundles/common.js +457 -0
  141. package/lib/esm/actions/bundles/index.d.ts +3 -0
  142. package/lib/esm/actions/bundles/index.js +2 -0
  143. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  144. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  145. package/lib/esm/actions/index.d.ts +2 -1
  146. package/lib/esm/actions/index.js +1 -0
  147. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  148. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  149. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  150. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  151. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  152. package/lib/esm/actions/midnight/takeLend.js +2 -2
  153. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  154. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  155. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  156. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  157. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  158. package/lib/esm/actions/requirements/blue/index.js +1 -0
  159. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  160. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  161. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  162. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
  163. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  164. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
  165. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  166. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
  167. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  168. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  169. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  170. package/lib/esm/actions/requirements/encode/index.js +1 -0
  171. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  172. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
  173. package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
  174. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  175. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  176. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  177. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
  178. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  179. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  180. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  181. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  182. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  183. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  184. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  185. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  186. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  187. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  188. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  189. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  190. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  191. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  192. package/lib/esm/bundler/actions.d.ts +4 -4
  193. package/lib/esm/bundler/actions.js +4 -4
  194. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  195. package/lib/esm/client/morphoViemExtension.js +2 -1
  196. package/lib/esm/entities/blue/blue.d.ts +806 -374
  197. package/lib/esm/entities/blue/blue.js +455 -702
  198. package/lib/esm/entities/index.d.ts +2 -0
  199. package/lib/esm/entities/index.js +1 -0
  200. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  201. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
  203. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  204. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  205. package/lib/esm/entities/requirements/index.d.ts +1 -0
  206. package/lib/esm/entities/requirements/index.js +1 -0
  207. package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
  208. package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
  209. package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
  210. package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
  211. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
  212. package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
  213. package/lib/esm/helpers/index.d.ts +2 -1
  214. package/lib/esm/helpers/index.js +2 -1
  215. package/lib/esm/helpers/referralFee.d.ts +21 -0
  216. package/lib/esm/helpers/referralFee.js +31 -0
  217. package/lib/esm/helpers/slippage.d.ts +90 -1
  218. package/lib/esm/helpers/slippage.js +122 -2
  219. package/lib/esm/helpers/validate.d.ts +16 -23
  220. package/lib/esm/helpers/validate.js +40 -48
  221. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  222. package/lib/esm/helpers/validateRequirementSpender.js +3 -0
  223. package/lib/esm/index.d.ts +1 -0
  224. package/lib/esm/index.js +1 -0
  225. package/lib/esm/types/action.d.ts +303 -210
  226. package/lib/esm/types/action.js +28 -2
  227. package/lib/esm/types/client.d.ts +9 -0
  228. package/lib/esm/types/error.d.ts +193 -18
  229. package/lib/esm/types/error.js +252 -21
  230. package/lib/esm/types/sharedLiquidity.d.ts +0 -7
  231. package/package.json +6 -5
  232. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  233. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  234. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  235. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  236. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  237. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  238. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  239. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  240. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  241. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  242. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  243. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,4 +1,5 @@
1
1
  export * from "./blue/index.js";
2
+ export * from "./bundles/index.js";
2
3
  export * from "./midnight/index.js";
3
4
  export * from "./requirements/index.js";
4
5
  export * from "./signatures/index.js";
@@ -40,8 +40,8 @@ export interface MidnightSupplyCollateralTakeBorrowParams extends MidnightTakeBo
40
40
  * chainId: 8453,
41
41
  * market: marketData.params,
42
42
  * collateralAssets: 2_000_000n,
43
- * loanAssets: BigInt(quote.data.availableAssets),
44
- * maxUnits: BigInt(quote.data.availableUnits),
43
+ * loanAssets: 1_000_000n,
44
+ * maxUnits: 1_100_000n,
45
45
  * taker: borrower,
46
46
  * takeableOffers: quote.data.takeableOffers,
47
47
  * deadline: maxUint256,
@@ -41,8 +41,8 @@ import { PermitKind } from "./types.js";
41
41
  * chainId: 8453,
42
42
  * market: marketData.params,
43
43
  * collateralAssets: 2_000_000n,
44
- * loanAssets: BigInt(quote.data.availableAssets),
45
- * maxUnits: BigInt(quote.data.availableUnits),
44
+ * loanAssets: 1_000_000n,
45
+ * maxUnits: 1_100_000n,
46
46
  * taker: borrower,
47
47
  * takeableOffers: quote.data.takeableOffers,
48
48
  * deadline: maxUint256,
@@ -46,8 +46,8 @@ export interface MidnightTakeBorrowParams {
46
46
  * const tx = midnightTakeBorrow({
47
47
  * chainId: 8453,
48
48
  * market: marketData.params,
49
- * loanAssets: BigInt(quote.data.availableAssets),
50
- * maxUnits: BigInt(quote.data.availableUnits),
49
+ * loanAssets: 1_000_000n,
50
+ * maxUnits: 1_100_000n,
51
51
  * taker: borrower,
52
52
  * takeableOffers: quote.data.takeableOffers,
53
53
  * deadline: maxUint256,
@@ -37,8 +37,8 @@ import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js
37
37
  * const tx = midnightTakeBorrow({
38
38
  * chainId: 8453,
39
39
  * market: marketData.params,
40
- * loanAssets: BigInt(quote.data.availableAssets),
41
- * maxUnits: BigInt(quote.data.availableUnits),
40
+ * loanAssets: 1_000_000n,
41
+ * maxUnits: 1_100_000n,
42
42
  * taker: borrower,
43
43
  * takeableOffers: quote.data.takeableOffers,
44
44
  * deadline: maxUint256,
@@ -46,8 +46,8 @@ export interface MidnightTakeLendParams {
46
46
  * const tx = midnightTakeLend({
47
47
  * chainId: 8453,
48
48
  * market: marketData.params,
49
- * assets: BigInt(quote.data.availableAssets),
50
- * minUnits: BigInt(quote.data.availableUnits),
49
+ * assets: 1_000_000n,
50
+ * minUnits: 900_000n,
51
51
  * taker: lender,
52
52
  * takeableOffers: quote.data.takeableOffers,
53
53
  * deadline: maxUint256,
@@ -38,8 +38,8 @@ import { PermitKind } from "./types.js";
38
38
  * const tx = midnightTakeLend({
39
39
  * chainId: 8453,
40
40
  * market: marketData.params,
41
- * assets: BigInt(quote.data.availableAssets),
42
- * minUnits: BigInt(quote.data.availableUnits),
41
+ * assets: 1_000_000n,
42
+ * minUnits: 900_000n,
43
43
  * taker: lender,
44
44
  * takeableOffers: quote.data.takeableOffers,
45
45
  * deadline: maxUint256,
@@ -2,48 +2,60 @@ import type { Client } from "viem";
2
2
  import { type Address } from "viem";
3
3
  import { type AuthorizationRequirementSignature, type BlueAuthorizationAction, type Requirement, type Transaction } from "../../../types/index.js";
4
4
  /**
5
- * Resolves whether `GeneralAdapter1` needs Blue authorization for the given user, and returns
5
+ * Resolves whether a supported operator needs Blue authorization for the given user, and returns
6
6
  * the requirement to satisfy it when it does.
7
7
  *
8
- * Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
9
- * any bundled Blue path that operates on behalf of the user (`borrow`, `withdraw`,
10
- * `supplyCollateralBorrow`, `repayWithdrawCollateral`, `refinance`).
8
+ * Reads `Morpho.isAuthorized(userAddress, authorized)` on the target chain.
11
9
  *
12
10
  * - When `supportSignature` is falsy (default), returns the
13
- * `setAuthorization(generalAdapter1, true)` transaction the user submits before the bundle.
11
+ * `setAuthorization(authorized, true)` transaction the user submits before the operation.
14
12
  * - When `supportSignature` is `true`, reads the user's Morpho `nonce` and returns a signable
15
- * `Requirement`; the signed authorization is folded into the bundle via
16
- * `setAuthorizationWithSig`, removing the standalone transaction.
13
+ * `Requirement`. The selected route consumes the result: Bundler3 emits
14
+ * `setAuthorizationWithSig`, while BlueBundlesV1 embeds its signed-authorization struct.
17
15
  *
18
16
  * @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
19
- * @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
20
- * @param params.userAddress - The user that must authorize `GeneralAdapter1`.
17
+ * @param params.chainId - Target chain id used to resolve Morpho and the default GeneralAdapter1.
18
+ * @param params.userAddress - The user granting authorization.
19
+ * @param params.authorized - Operator to authorize. Defaults to GeneralAdapter1; direct Blue
20
+ * writes pass the registered BlueBundlesV1 deployment. Must be the chain's registered
21
+ * GeneralAdapter1 or BlueBundlesV1 operator.
22
+ * @param params.deadline - Optional signature deadline forwarded to the authorization encoder.
21
23
  * @param params.supportSignature - When `true`, return a signable `Requirement` instead of a
22
- * transaction so authorization can be bundled via `setAuthorizationWithSig`.
24
+ * transaction so the destination route can consume the signed authorization.
23
25
  * @returns A deep-frozen `Transaction<BlueAuthorizationAction>`, a signable authorization
24
26
  * `Requirement` (when `supportSignature` is `true`), or `null` when authorization is already in
25
27
  * place.
26
28
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
29
+ * @throws {UnsupportedAuthorizationOperatorError} when `authorized` is neither the chain's
30
+ * GeneralAdapter1 nor its BlueBundlesV1 operator.
31
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
32
+ * @throws {viem.BaseError} when an authorization or nonce RPC read fails.
27
33
  * @example
28
34
  * ```ts
29
- * import { createPublicClient, http } from "viem";
35
+ * import { createPublicClient, http, zeroAddress } from "viem";
30
36
  * import { mainnet } from "viem/chains";
31
37
  * import { getBlueAuthorizationRequirement } from "@morpho-org/morpho-sdk";
38
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
32
39
  *
33
40
  * const client = createPublicClient({ chain: mainnet, transport: http() });
41
+ * const blueBundlesV1 = getChainAddress(mainnet.id, "bundles.blueBundlesV1");
34
42
  * const requirement = await getBlueAuthorizationRequirement({
35
43
  * viemClient: client,
36
- * chainId: 1,
37
- * userAddress: borrower,
44
+ * chainId: mainnet.id,
45
+ * userAddress: zeroAddress,
38
46
  * supportSignature: true,
47
+ * authorized: blueBundlesV1,
48
+ * deadline: 1_900_000_000n,
39
49
  * });
40
50
  * // requirement is null when already authorized, a Requirement when supportSignature is true,
41
51
  * // otherwise Readonly<Transaction<BlueAuthorizationAction>>
42
52
  * ```
43
53
  */
44
54
  export declare const getBlueAuthorizationRequirement: (params: {
45
- viemClient: Client;
46
- chainId: number;
47
- userAddress: Address;
48
- supportSignature?: boolean;
55
+ readonly viemClient: Client;
56
+ readonly chainId: number;
57
+ readonly userAddress: Address;
58
+ readonly supportSignature?: boolean;
59
+ readonly authorized?: Address;
60
+ readonly deadline?: bigint;
49
61
  }) => Promise<Readonly<Transaction<BlueAuthorizationAction>> | Requirement<AuthorizationRequirementSignature> | null>;
@@ -1,44 +1,54 @@
1
1
  import { getChainAddresses } from "@morpho-org/blue-sdk";
2
2
  import { blueAbi } from "@morpho-org/blue-sdk-viem";
3
- import { deepFreeze } from "@morpho-org/morpho-ts";
4
- import { encodeFunctionData, publicActions } from "viem";
5
- import { ChainIdMismatchError, } from "../../../types/index.js";
3
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
4
+ import { encodeFunctionData, isAddressEqual, maxUint256, publicActions, } from "viem";
5
+ import { ChainIdMismatchError, ExpiredDeadlineError, InputExceedsMaxError, NonPositiveInputError, UnsupportedAuthorizationOperatorError, } from "../../../types/index.js";
6
6
  import { encodeBlueSignatureAuthorization } from "../encode/encodeBlueSignatureAuthorization.js";
7
7
  /**
8
- * Resolves whether `GeneralAdapter1` needs Blue authorization for the given user, and returns
8
+ * Resolves whether a supported operator needs Blue authorization for the given user, and returns
9
9
  * the requirement to satisfy it when it does.
10
10
  *
11
- * Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
12
- * any bundled Blue path that operates on behalf of the user (`borrow`, `withdraw`,
13
- * `supplyCollateralBorrow`, `repayWithdrawCollateral`, `refinance`).
11
+ * Reads `Morpho.isAuthorized(userAddress, authorized)` on the target chain.
14
12
  *
15
13
  * - When `supportSignature` is falsy (default), returns the
16
- * `setAuthorization(generalAdapter1, true)` transaction the user submits before the bundle.
14
+ * `setAuthorization(authorized, true)` transaction the user submits before the operation.
17
15
  * - When `supportSignature` is `true`, reads the user's Morpho `nonce` and returns a signable
18
- * `Requirement`; the signed authorization is folded into the bundle via
19
- * `setAuthorizationWithSig`, removing the standalone transaction.
16
+ * `Requirement`. The selected route consumes the result: Bundler3 emits
17
+ * `setAuthorizationWithSig`, while BlueBundlesV1 embeds its signed-authorization struct.
20
18
  *
21
19
  * @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
22
- * @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
23
- * @param params.userAddress - The user that must authorize `GeneralAdapter1`.
20
+ * @param params.chainId - Target chain id used to resolve Morpho and the default GeneralAdapter1.
21
+ * @param params.userAddress - The user granting authorization.
22
+ * @param params.authorized - Operator to authorize. Defaults to GeneralAdapter1; direct Blue
23
+ * writes pass the registered BlueBundlesV1 deployment. Must be the chain's registered
24
+ * GeneralAdapter1 or BlueBundlesV1 operator.
25
+ * @param params.deadline - Optional signature deadline forwarded to the authorization encoder.
24
26
  * @param params.supportSignature - When `true`, return a signable `Requirement` instead of a
25
- * transaction so authorization can be bundled via `setAuthorizationWithSig`.
27
+ * transaction so the destination route can consume the signed authorization.
26
28
  * @returns A deep-frozen `Transaction<BlueAuthorizationAction>`, a signable authorization
27
29
  * `Requirement` (when `supportSignature` is `true`), or `null` when authorization is already in
28
30
  * place.
29
31
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
32
+ * @throws {UnsupportedAuthorizationOperatorError} when `authorized` is neither the chain's
33
+ * GeneralAdapter1 nor its BlueBundlesV1 operator.
34
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
35
+ * @throws {viem.BaseError} when an authorization or nonce RPC read fails.
30
36
  * @example
31
37
  * ```ts
32
- * import { createPublicClient, http } from "viem";
38
+ * import { createPublicClient, http, zeroAddress } from "viem";
33
39
  * import { mainnet } from "viem/chains";
34
40
  * import { getBlueAuthorizationRequirement } from "@morpho-org/morpho-sdk";
41
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
35
42
  *
36
43
  * const client = createPublicClient({ chain: mainnet, transport: http() });
44
+ * const blueBundlesV1 = getChainAddress(mainnet.id, "bundles.blueBundlesV1");
37
45
  * const requirement = await getBlueAuthorizationRequirement({
38
46
  * viemClient: client,
39
- * chainId: 1,
40
- * userAddress: borrower,
47
+ * chainId: mainnet.id,
48
+ * userAddress: zeroAddress,
41
49
  * supportSignature: true,
50
+ * authorized: blueBundlesV1,
51
+ * deadline: 1_900_000_000n,
42
52
  * });
43
53
  * // requirement is null when already authorized, a Requirement when supportSignature is true,
44
54
  * // otherwise Readonly<Transaction<BlueAuthorizationAction>>
@@ -49,9 +59,39 @@ export const getBlueAuthorizationRequirement = async (params) => {
49
59
  if (viemClient.chain?.id !== chainId) {
50
60
  throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
51
61
  }
52
- const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
62
+ const { morpho, bundler3: { generalAdapter1 }, bundles, } = getChainAddresses(chainId);
63
+ const authorized = params.authorized ?? generalAdapter1;
64
+ // The SDK only ever authorizes the chain's registered GeneralAdapter1 or BlueBundlesV1 operator;
65
+ // reject any other override so a misconfigured `authorized` cannot grant an arbitrary address
66
+ // control over the user's Morpho positions.
67
+ const supportedOperators = [generalAdapter1];
68
+ if (bundles?.blueBundlesV1 != null) {
69
+ supportedOperators.push(bundles.blueBundlesV1);
70
+ }
71
+ if (!supportedOperators.some((operator) => isAddressEqual(operator, authorized))) {
72
+ throw new UnsupportedAuthorizationOperatorError(authorized, chainId);
73
+ }
53
74
  const pc = viemClient.extend(publicActions);
54
75
  if (supportSignature) {
76
+ // The forwarded deadline is only consumed on the signable path; reject an invalid or expired
77
+ // one before the RPC reads so the caller never signs an authorization that cannot be encoded
78
+ // or would revert on-chain. An omitted deadline defaults downstream to two hours from now.
79
+ if (params.deadline != null) {
80
+ if (params.deadline <= 0n) {
81
+ throw new NonPositiveInputError("deadline", params.deadline);
82
+ }
83
+ if (params.deadline > maxUint256) {
84
+ throw new InputExceedsMaxError({
85
+ field: "deadline",
86
+ value: params.deadline,
87
+ max: maxUint256,
88
+ });
89
+ }
90
+ const timestamp = Time.timestamp();
91
+ if (params.deadline <= timestamp) {
92
+ throw new ExpiredDeadlineError(params.deadline, timestamp);
93
+ }
94
+ }
55
95
  // The signable path needs the user's Morpho nonce; fetch it alongside the
56
96
  // authorization status so both reads share a round-trip (batched into a
57
97
  // single multicall when the client enables batching) instead of
@@ -61,7 +101,7 @@ export const getBlueAuthorizationRequirement = async (params) => {
61
101
  address: morpho,
62
102
  abi: blueAbi,
63
103
  functionName: "isAuthorized",
64
- args: [userAddress, generalAdapter1],
104
+ args: [userAddress, authorized],
65
105
  }),
66
106
  pc.readContract({
67
107
  address: morpho,
@@ -74,16 +114,17 @@ export const getBlueAuthorizationRequirement = async (params) => {
74
114
  return null;
75
115
  }
76
116
  return encodeBlueSignatureAuthorization(viemClient, {
77
- authorized: generalAdapter1,
117
+ authorized,
78
118
  chainId,
79
119
  nonce,
120
+ deadline: params.deadline,
80
121
  });
81
122
  }
82
123
  const isAuthorized = await pc.readContract({
83
124
  address: morpho,
84
125
  abi: blueAbi,
85
126
  functionName: "isAuthorized",
86
- args: [userAddress, generalAdapter1],
127
+ args: [userAddress, authorized],
87
128
  });
88
129
  if (isAuthorized) {
89
130
  return null;
@@ -93,13 +134,13 @@ export const getBlueAuthorizationRequirement = async (params) => {
93
134
  data: encodeFunctionData({
94
135
  abi: blueAbi,
95
136
  functionName: "setAuthorization",
96
- args: [generalAdapter1, true],
137
+ args: [authorized, true],
97
138
  }),
98
139
  value: 0n,
99
140
  action: {
100
141
  type: "blueAuthorization",
101
142
  args: {
102
- authorized: generalAdapter1,
143
+ authorized,
103
144
  isAuthorized: true,
104
145
  },
105
146
  },
@@ -0,0 +1,48 @@
1
+ import { type Address, type Client } from "viem";
2
+ /** Parameters for {@link getUnusedPermit2Nonce}. */
3
+ export interface GetUnusedPermit2NonceParams {
4
+ /** Account that will sign the Permit2 SignatureTransfer. */
5
+ readonly owner: Address;
6
+ /** Target chain id; must match the connected client chain. */
7
+ readonly chainId: number;
8
+ /** Lowest nonce to consider; defaults to 0. */
9
+ readonly startNonce?: bigint;
10
+ }
11
+ /**
12
+ * Finds the lowest unused Permit2 unordered nonce for `owner`, ready to pass as `permit2Nonce` to
13
+ * {@link getBundlesTokenRequirements} or as `nonce` to {@link encodeErc20Permit2SignatureTransfer}.
14
+ *
15
+ * Permit2 SignatureTransfer consumes an unordered 256-bit nonce; a nonce is free when its bit in
16
+ * `nonceBitmap(owner, word)` is unset. This scans consecutive bitmap words starting at `startNonce`
17
+ * (one RPC read per 256 nonces) and returns the first free nonce, so integrators do not reimplement
18
+ * the scan. Concurrent flows for the same owner should advance `startNonce` (or read once, then
19
+ * partition locally) so two signatures never race on the same owner-global nonce.
20
+ *
21
+ * @param viemClient - Connected viem client used for the bitmap reads.
22
+ * @param params - Lookup parameters.
23
+ * @param params.owner - Account that will sign the SignatureTransfer.
24
+ * @param params.chainId - Target chain id; must match the client chain.
25
+ * @param params.startNonce - Lowest nonce to consider; defaults to 0.
26
+ * @returns The lowest unused Permit2 nonce at or after `startNonce`.
27
+ * @throws {ChainIdMismatchError} when the connected client targets another chain.
28
+ * @throws {NegativeInputError} when `startNonce` is negative.
29
+ * @throws {InputExceedsMaxError} when `startNonce` exceeds `uint256`.
30
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
31
+ * @throws {UnknownAddressError} when the chain has no registered canonical Permit2 deployment.
32
+ * @throws {viem.BaseError} when a Permit2 nonce-bitmap RPC read fails.
33
+ * @throws {NoUnusedPermit2NonceError} when every nonce at or after `startNonce` is consumed.
34
+ * @example
35
+ * ```ts
36
+ * import { createPublicClient, http, zeroAddress } from "viem";
37
+ * import { mainnet } from "viem/chains";
38
+ * import { getUnusedPermit2Nonce } from "@morpho-org/morpho-sdk";
39
+ *
40
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
41
+ * const permit2Nonce = await getUnusedPermit2Nonce(client, {
42
+ * owner: zeroAddress,
43
+ * chainId: mainnet.id,
44
+ * });
45
+ * // permit2Nonce satisfies bigint
46
+ * ```
47
+ */
48
+ export declare const getUnusedPermit2Nonce: (viemClient: Client, params: GetUnusedPermit2NonceParams) => Promise<bigint>;
@@ -0,0 +1,68 @@
1
+ import { permit2Abi } from "@morpho-org/blue-sdk-viem";
2
+ import { getChainAddress } from "@morpho-org/morpho-ts";
3
+ import { maxUint256 } from "viem";
4
+ import { readContract } from "viem/actions";
5
+ import { validateChainId } from "../../../helpers/index.js";
6
+ import { validateUint256Field } from "../../../helpers/validate.js";
7
+ import { NoUnusedPermit2NonceError } from "../../../types/index.js";
8
+ /** Highest Permit2 nonce-bitmap word index; each word covers 256 unordered nonces. */
9
+ const MAX_PERMIT2_NONCE_WORD = maxUint256 >> 8n;
10
+ /**
11
+ * Finds the lowest unused Permit2 unordered nonce for `owner`, ready to pass as `permit2Nonce` to
12
+ * {@link getBundlesTokenRequirements} or as `nonce` to {@link encodeErc20Permit2SignatureTransfer}.
13
+ *
14
+ * Permit2 SignatureTransfer consumes an unordered 256-bit nonce; a nonce is free when its bit in
15
+ * `nonceBitmap(owner, word)` is unset. This scans consecutive bitmap words starting at `startNonce`
16
+ * (one RPC read per 256 nonces) and returns the first free nonce, so integrators do not reimplement
17
+ * the scan. Concurrent flows for the same owner should advance `startNonce` (or read once, then
18
+ * partition locally) so two signatures never race on the same owner-global nonce.
19
+ *
20
+ * @param viemClient - Connected viem client used for the bitmap reads.
21
+ * @param params - Lookup parameters.
22
+ * @param params.owner - Account that will sign the SignatureTransfer.
23
+ * @param params.chainId - Target chain id; must match the client chain.
24
+ * @param params.startNonce - Lowest nonce to consider; defaults to 0.
25
+ * @returns The lowest unused Permit2 nonce at or after `startNonce`.
26
+ * @throws {ChainIdMismatchError} when the connected client targets another chain.
27
+ * @throws {NegativeInputError} when `startNonce` is negative.
28
+ * @throws {InputExceedsMaxError} when `startNonce` exceeds `uint256`.
29
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
30
+ * @throws {UnknownAddressError} when the chain has no registered canonical Permit2 deployment.
31
+ * @throws {viem.BaseError} when a Permit2 nonce-bitmap RPC read fails.
32
+ * @throws {NoUnusedPermit2NonceError} when every nonce at or after `startNonce` is consumed.
33
+ * @example
34
+ * ```ts
35
+ * import { createPublicClient, http, zeroAddress } from "viem";
36
+ * import { mainnet } from "viem/chains";
37
+ * import { getUnusedPermit2Nonce } from "@morpho-org/morpho-sdk";
38
+ *
39
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
40
+ * const permit2Nonce = await getUnusedPermit2Nonce(client, {
41
+ * owner: zeroAddress,
42
+ * chainId: mainnet.id,
43
+ * });
44
+ * // permit2Nonce satisfies bigint
45
+ * ```
46
+ */
47
+ export const getUnusedPermit2Nonce = async (viemClient, params) => {
48
+ validateChainId(viemClient.chain?.id, params.chainId);
49
+ const startNonce = params.startNonce ?? 0n;
50
+ validateUint256Field("startNonce", startNonce);
51
+ const permit2 = getChainAddress(params.chainId, "permit2");
52
+ const startWord = startNonce >> 8n;
53
+ for (let word = startWord; word <= MAX_PERMIT2_NONCE_WORD; word++) {
54
+ const bitmap = await readContract(viemClient, {
55
+ abi: permit2Abi,
56
+ address: permit2,
57
+ functionName: "nonceBitmap",
58
+ args: [params.owner, word],
59
+ });
60
+ const firstBit = word === startWord ? startNonce & 255n : 0n;
61
+ for (let bit = firstBit; bit <= 255n; bit++) {
62
+ if ((bitmap & (1n << bit)) === 0n) {
63
+ return (word << 8n) | bit;
64
+ }
65
+ }
66
+ }
67
+ throw new NoUnusedPermit2NonceError(params.owner, startNonce);
68
+ };
@@ -1 +1,2 @@
1
1
  export * from "./getBlueAuthorizationRequirement.js";
2
+ export * from "./getUnusedPermit2Nonce.js";
@@ -1 +1,2 @@
1
1
  export * from "./getBlueAuthorizationRequirement.js";
2
+ export * from "./getUnusedPermit2Nonce.js";
@@ -1,9 +1,9 @@
1
1
  import type { Address } from "@morpho-org/blue-sdk";
2
- import type { Client } from "viem";
2
+ import { type Client } from "viem";
3
3
  import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
4
4
  /** Parameters for {@link encodeBlueSignatureAuthorization}. */
5
5
  interface EncodeBlueSignatureAuthorizationParams {
6
- /** Account to authorize on Morpho (GeneralAdapter1). */
6
+ /** Operator to authorize on Morpho, such as GeneralAdapter1 or BlueBundlesV1. */
7
7
  authorized: Address;
8
8
  /** Target chain id; must match `viemClient.chain.id`. */
9
9
  chainId: number;
@@ -16,23 +16,26 @@ interface EncodeBlueSignatureAuthorizationParams {
16
16
  }
17
17
  /**
18
18
  * Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
19
- * Morpho on the signer's behalf through `setAuthorizationWithSig` — the offchain-signature
20
- * alternative to a standalone `setAuthorization` transaction.
19
+ * Morpho on the signer's behalf. Bundler3 consumes the result through `setAuthorizationWithSig`;
20
+ * BlueBundlesV1 embeds the same signed authorization in its direct-call struct.
21
21
  *
22
22
  * The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
23
23
  * typed data, verifies it against the connected account, and returns a deep-frozen
24
- * `RequirementSignature` the bundler action helpers consume. Deadline defaults to two hours from
25
- * `Time.timestamp()`.
24
+ * `RequirementSignature` the selected transaction route consumes. Deadline defaults to two hours
25
+ * from `Time.timestamp()`.
26
26
  *
27
27
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
28
28
  * @param params - Authorization encoding parameters.
29
- * @param params.authorized - Account to authorize (GeneralAdapter1).
29
+ * @param params.authorized - Operator to authorize, such as GeneralAdapter1 or BlueBundlesV1.
30
30
  * @param params.chainId - Target chain id.
31
31
  * @param params.nonce - The signer's current Morpho authorization nonce.
32
32
  * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
33
33
  * @param params.deadline - Optional signature deadline in seconds.
34
34
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
35
35
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
36
+ * @throws {NonPositiveInputError} when a provided `deadline` is not positive.
37
+ * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
38
+ * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
36
39
  * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
37
40
  * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
38
41
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
@@ -1,26 +1,30 @@
1
1
  import { getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
2
2
  import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
+ import { maxUint256 } from "viem";
3
4
  import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
4
- import { ChainIdMismatchError, } from "../../../types/index.js";
5
+ import { ChainIdMismatchError, ExpiredDeadlineError, InputExceedsMaxError, NonPositiveInputError, } from "../../../types/index.js";
5
6
  /**
6
7
  * Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
7
- * Morpho on the signer's behalf through `setAuthorizationWithSig` — the offchain-signature
8
- * alternative to a standalone `setAuthorization` transaction.
8
+ * Morpho on the signer's behalf. Bundler3 consumes the result through `setAuthorizationWithSig`;
9
+ * BlueBundlesV1 embeds the same signed authorization in its direct-call struct.
9
10
  *
10
11
  * The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
11
12
  * typed data, verifies it against the connected account, and returns a deep-frozen
12
- * `RequirementSignature` the bundler action helpers consume. Deadline defaults to two hours from
13
- * `Time.timestamp()`.
13
+ * `RequirementSignature` the selected transaction route consumes. Deadline defaults to two hours
14
+ * from `Time.timestamp()`.
14
15
  *
15
16
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
16
17
  * @param params - Authorization encoding parameters.
17
- * @param params.authorized - Account to authorize (GeneralAdapter1).
18
+ * @param params.authorized - Operator to authorize, such as GeneralAdapter1 or BlueBundlesV1.
18
19
  * @param params.chainId - Target chain id.
19
20
  * @param params.nonce - The signer's current Morpho authorization nonce.
20
21
  * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
21
22
  * @param params.deadline - Optional signature deadline in seconds.
22
23
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
23
24
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
25
+ * @throws {NonPositiveInputError} when a provided `deadline` is not positive.
26
+ * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
27
+ * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
24
28
  * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
25
29
  * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
26
30
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
@@ -44,6 +48,27 @@ export const encodeBlueSignatureAuthorization = async (viemClient, params) => {
44
48
  if (viemClient.chain?.id !== chainId) {
45
49
  throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
46
50
  }
51
+ // Reject an invalid or already-expired caller-supplied deadline before signing, so a direct caller
52
+ // is never walked through a wallet EIP-712 prompt for an authorization Morpho would reject with
53
+ // `SIGNATURE_EXPIRED`. Mirrors the sibling `encodeErc20Permit2SignatureTransfer` and the
54
+ // `getBlueAuthorizationRequirement` resolver guards. An omitted deadline defaults to two hours
55
+ // from now and is always valid.
56
+ if (params.deadline != null) {
57
+ if (params.deadline <= 0n) {
58
+ throw new NonPositiveInputError("deadline", params.deadline);
59
+ }
60
+ if (params.deadline > maxUint256) {
61
+ throw new InputExceedsMaxError({
62
+ field: "deadline",
63
+ value: params.deadline,
64
+ max: maxUint256,
65
+ });
66
+ }
67
+ const timestamp = Time.timestamp();
68
+ if (params.deadline <= timestamp) {
69
+ throw new ExpiredDeadlineError(params.deadline, timestamp);
70
+ }
71
+ }
47
72
  const deadline = params.deadline ?? Time.timestamp() + Time.s.from.h(2n);
48
73
  const action = {
49
74
  type: "authorization",
@@ -16,10 +16,11 @@ interface EncodeErc20ApprovalParams {
16
16
  * @param params - Encoding parameters.
17
17
  * @param params.token - ERC-20 token address to approve.
18
18
  * @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
19
- * Midnight, MidnightBundles, or VaultExitBundlesV1 for the chain.
19
+ * Midnight, MidnightBundles, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for the chain.
20
20
  * @param params.amount - Allowance amount before per-token cap.
21
21
  * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
22
22
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
23
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
23
24
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
24
25
  * @example
25
26
  * ```ts
@@ -1,6 +1,6 @@
1
1
  import { MathLib } from "@morpho-org/blue-sdk";
2
2
  import { deepFreeze } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
3
+ import { encodeFunctionData, erc20Abi, getAddress, maxUint256 } from "viem";
4
4
  import { MAX_TOKEN_APPROVALS } from "../../../helpers/constant.js";
5
5
  import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
6
6
  /**
@@ -12,10 +12,11 @@ import { validateRequirementSpender } from "../../../helpers/validateRequirement
12
12
  * @param params - Encoding parameters.
13
13
  * @param params.token - ERC-20 token address to approve.
14
14
  * @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
15
- * Midnight, MidnightBundles, or VaultExitBundlesV1 for the chain.
15
+ * Midnight, MidnightBundles, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for the chain.
16
16
  * @param params.amount - Allowance amount before per-token cap.
17
17
  * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
18
18
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
19
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
19
20
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
20
21
  * @example
21
22
  * ```ts
@@ -42,9 +43,14 @@ export const encodeErc20Approval = (params) => {
42
43
  "midnight",
43
44
  "midnightBundles",
44
45
  "vaultExitBundlesV1",
46
+ "vaultBundlesV1",
47
+ "blueBundlesV1",
45
48
  ],
46
49
  });
47
- const amountValue = MathLib.min(amount, MAX_TOKEN_APPROVALS[chainId]?.[token] ?? maxUint256);
50
+ const amountValue = MathLib.min(amount,
51
+ // Checksum the lookup key so a differently-cased token still resolves its per-token cap
52
+ // instead of falling back to `maxUint256`, which UNI/ONDO/COMP/FLUID reject above `uint96`.
53
+ MAX_TOKEN_APPROVALS[chainId]?.[getAddress(token)] ?? maxUint256);
48
54
  return deepFreeze({
49
55
  to: token,
50
56
  data: encodeFunctionData({