@morpho-org/morpho-sdk 4.2.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/README.md +12 -12
  2. package/lib/cjs/actions/blue/borrow.d.ts +12 -2
  3. package/lib/cjs/actions/blue/borrow.js +7 -1
  4. package/lib/cjs/actions/blue/refinance.d.ts +13 -3
  5. package/lib/cjs/actions/blue/refinance.js +8 -2
  6. package/lib/cjs/actions/blue/repay.d.ts +6 -5
  7. package/lib/cjs/actions/blue/repay.js +10 -18
  8. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +15 -6
  9. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +16 -18
  10. package/lib/cjs/actions/blue/supply.d.ts +7 -7
  11. package/lib/cjs/actions/blue/supply.js +10 -20
  12. package/lib/cjs/actions/blue/supplyCollateral.d.ts +8 -8
  13. package/lib/cjs/actions/blue/supplyCollateral.js +11 -21
  14. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +18 -6
  15. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +17 -19
  16. package/lib/cjs/actions/blue/withdraw.d.ts +10 -2
  17. package/lib/cjs/actions/blue/withdraw.js +7 -1
  18. package/lib/cjs/actions/index.d.ts +1 -0
  19. package/lib/cjs/actions/index.js +1 -0
  20. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
  21. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +111 -0
  22. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  23. package/lib/cjs/actions/requirements/blue/index.js +17 -0
  24. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
  25. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +89 -0
  26. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +17 -5
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +18 -4
  30. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
  31. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +10 -14
  32. package/lib/cjs/actions/requirements/encode/index.d.ts +2 -1
  33. package/lib/cjs/actions/requirements/encode/index.js +2 -1
  34. package/lib/cjs/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
  35. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +151 -0
  36. package/lib/{esm/actions/requirements/getRequirementsPermit.d.ts → cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
  37. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +50 -0
  38. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
  39. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +74 -0
  40. package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +3 -0
  41. package/lib/cjs/actions/requirements/generalAdapter/index.js +19 -0
  42. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
  43. package/lib/cjs/actions/requirements/getRequirementsApproval.js +3 -2
  44. package/lib/cjs/actions/requirements/index.d.ts +2 -5
  45. package/lib/cjs/actions/requirements/index.js +2 -5
  46. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
  47. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +48 -0
  48. package/lib/{esm/actions/requirements/getRequirementsAction.d.ts → cjs/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
  49. package/lib/cjs/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +26 -13
  50. package/lib/cjs/actions/signatures/index.d.ts +2 -0
  51. package/lib/cjs/actions/signatures/index.js +18 -0
  52. package/lib/cjs/actions/vaultV1/deposit.d.ts +6 -8
  53. package/lib/cjs/actions/vaultV1/deposit.js +11 -21
  54. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +6 -6
  55. package/lib/cjs/actions/vaultV1/migrateToV2.js +12 -20
  56. package/lib/cjs/actions/vaultV2/deposit.d.ts +6 -8
  57. package/lib/cjs/actions/vaultV2/deposit.js +11 -21
  58. package/lib/cjs/addresses.d.ts +2 -2
  59. package/lib/cjs/addresses.js +2 -1
  60. package/lib/cjs/bundler/actions.d.ts +44 -4
  61. package/lib/cjs/bundler/actions.js +75 -2
  62. package/lib/cjs/bundler/types.d.ts +6 -0
  63. package/lib/cjs/constants.d.ts +2 -2
  64. package/lib/cjs/constants.js +3 -3
  65. package/lib/cjs/entities/blue/blue.d.ts +58 -33
  66. package/lib/cjs/entities/blue/blue.js +171 -125
  67. package/lib/cjs/entities/reallocationData.d.ts +0 -44
  68. package/lib/cjs/entities/reallocationData.js +3 -50
  69. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +9 -9
  70. package/lib/cjs/entities/vaultV1/vaultV1.js +44 -34
  71. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +6 -6
  72. package/lib/cjs/entities/vaultV2/vaultV2.js +21 -16
  73. package/lib/cjs/errors.d.ts +3 -2
  74. package/lib/cjs/errors.js +5 -2
  75. package/lib/cjs/helpers/validate.d.ts +2 -2
  76. package/lib/cjs/helpers/validate.js +1 -1
  77. package/lib/cjs/types/action.d.ts +113 -9
  78. package/lib/cjs/types/action.js +79 -5
  79. package/lib/cjs/types/error.d.ts +73 -0
  80. package/lib/cjs/types/error.js +89 -4
  81. package/lib/cjs/types.d.ts +2 -1
  82. package/lib/cjs/utils.d.ts +1 -1
  83. package/lib/cjs/utils.js +2 -3
  84. package/lib/esm/actions/blue/borrow.d.ts +12 -2
  85. package/lib/esm/actions/blue/borrow.js +7 -1
  86. package/lib/esm/actions/blue/refinance.d.ts +13 -3
  87. package/lib/esm/actions/blue/refinance.js +8 -2
  88. package/lib/esm/actions/blue/repay.d.ts +6 -5
  89. package/lib/esm/actions/blue/repay.js +10 -18
  90. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +15 -6
  91. package/lib/esm/actions/blue/repayWithdrawCollateral.js +16 -18
  92. package/lib/esm/actions/blue/supply.d.ts +7 -7
  93. package/lib/esm/actions/blue/supply.js +10 -20
  94. package/lib/esm/actions/blue/supplyCollateral.d.ts +8 -8
  95. package/lib/esm/actions/blue/supplyCollateral.js +11 -21
  96. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +18 -6
  97. package/lib/esm/actions/blue/supplyCollateralBorrow.js +17 -19
  98. package/lib/esm/actions/blue/withdraw.d.ts +10 -2
  99. package/lib/esm/actions/blue/withdraw.js +7 -1
  100. package/lib/esm/actions/index.d.ts +1 -0
  101. package/lib/esm/actions/index.js +1 -0
  102. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
  103. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +107 -0
  104. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  105. package/lib/esm/actions/requirements/blue/index.js +1 -0
  106. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
  107. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +85 -0
  108. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
  109. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +19 -7
  110. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
  111. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +20 -6
  112. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
  113. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +8 -12
  114. package/lib/esm/actions/requirements/encode/index.d.ts +2 -1
  115. package/lib/esm/actions/requirements/encode/index.js +2 -1
  116. package/lib/esm/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
  117. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +147 -0
  118. package/lib/{cjs/actions/requirements/getRequirementsPermit.d.ts → esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
  119. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +46 -0
  120. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
  121. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +70 -0
  122. package/lib/esm/actions/requirements/generalAdapter/index.d.ts +3 -0
  123. package/lib/esm/actions/requirements/generalAdapter/index.js +3 -0
  124. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
  125. package/lib/esm/actions/requirements/getRequirementsApproval.js +3 -2
  126. package/lib/esm/actions/requirements/index.d.ts +2 -5
  127. package/lib/esm/actions/requirements/index.js +2 -5
  128. package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
  129. package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +44 -0
  130. package/lib/{cjs/actions/requirements/getRequirementsAction.d.ts → esm/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
  131. package/lib/esm/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +24 -11
  132. package/lib/esm/actions/signatures/index.d.ts +2 -0
  133. package/lib/esm/actions/signatures/index.js +2 -0
  134. package/lib/esm/actions/vaultV1/deposit.d.ts +6 -8
  135. package/lib/esm/actions/vaultV1/deposit.js +11 -21
  136. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +6 -6
  137. package/lib/esm/actions/vaultV1/migrateToV2.js +12 -20
  138. package/lib/esm/actions/vaultV2/deposit.d.ts +6 -8
  139. package/lib/esm/actions/vaultV2/deposit.js +11 -21
  140. package/lib/esm/addresses.d.ts +2 -2
  141. package/lib/esm/addresses.js +1 -1
  142. package/lib/esm/bundler/actions.d.ts +44 -4
  143. package/lib/esm/bundler/actions.js +77 -4
  144. package/lib/esm/bundler/types.d.ts +6 -0
  145. package/lib/esm/constants.d.ts +2 -2
  146. package/lib/esm/constants.js +2 -2
  147. package/lib/esm/entities/blue/blue.d.ts +58 -33
  148. package/lib/esm/entities/blue/blue.js +173 -127
  149. package/lib/esm/entities/reallocationData.d.ts +0 -44
  150. package/lib/esm/entities/reallocationData.js +2 -49
  151. package/lib/esm/entities/vaultV1/vaultV1.d.ts +9 -9
  152. package/lib/esm/entities/vaultV1/vaultV1.js +46 -36
  153. package/lib/esm/entities/vaultV2/vaultV2.d.ts +6 -6
  154. package/lib/esm/entities/vaultV2/vaultV2.js +23 -18
  155. package/lib/esm/errors.d.ts +3 -2
  156. package/lib/esm/errors.js +2 -1
  157. package/lib/esm/helpers/validate.d.ts +2 -2
  158. package/lib/esm/helpers/validate.js +1 -1
  159. package/lib/esm/types/action.d.ts +113 -9
  160. package/lib/esm/types/action.js +75 -4
  161. package/lib/esm/types/error.d.ts +73 -0
  162. package/lib/esm/types/error.js +84 -2
  163. package/lib/esm/types.d.ts +2 -1
  164. package/lib/esm/utils.d.ts +1 -1
  165. package/lib/esm/utils.js +1 -1
  166. package/package.json +6 -6
  167. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
  168. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +0 -71
  169. package/lib/cjs/actions/requirements/getRequirements.js +0 -112
  170. package/lib/cjs/actions/requirements/getRequirementsPermit.js +0 -57
  171. package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +0 -63
  172. package/lib/cjs/actions/requirements/getRequirementsPermit2.js +0 -85
  173. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
  174. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +0 -67
  175. package/lib/esm/actions/requirements/getRequirements.js +0 -108
  176. package/lib/esm/actions/requirements/getRequirementsPermit.js +0 -53
  177. package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +0 -63
  178. package/lib/esm/actions/requirements/getRequirementsPermit2.js +0 -81
@@ -0,0 +1,107 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
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";
6
+ import { encodeBlueSignatureAuthorization } from "../encode/encodeBlueSignatureAuthorization.js";
7
+ /**
8
+ * Resolves whether `GeneralAdapter1` needs Blue authorization for the given user, and returns
9
+ * the requirement to satisfy it when it does.
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`).
14
+ *
15
+ * - When `supportSignature` is falsy (default), returns the
16
+ * `setAuthorization(generalAdapter1, true)` transaction the user submits before the bundle.
17
+ * - 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.
20
+ *
21
+ * @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`.
24
+ * @param params.supportSignature - When `true`, return a signable `Requirement` instead of a
25
+ * transaction so authorization can be bundled via `setAuthorizationWithSig`.
26
+ * @returns A deep-frozen `Transaction<BlueAuthorizationAction>`, a signable authorization
27
+ * `Requirement` (when `supportSignature` is `true`), or `null` when authorization is already in
28
+ * place.
29
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
30
+ * @example
31
+ * ```ts
32
+ * import { createPublicClient, http } from "viem";
33
+ * import { mainnet } from "viem/chains";
34
+ * import { getBlueAuthorizationRequirement } from "@morpho-org/morpho-sdk";
35
+ *
36
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
37
+ * const requirement = await getBlueAuthorizationRequirement({
38
+ * viemClient: client,
39
+ * chainId: 1,
40
+ * userAddress: borrower,
41
+ * supportSignature: true,
42
+ * });
43
+ * // requirement is null when already authorized, a Requirement when supportSignature is true,
44
+ * // otherwise Readonly<Transaction<BlueAuthorizationAction>>
45
+ * ```
46
+ */
47
+ export const getBlueAuthorizationRequirement = async (params) => {
48
+ const { viemClient, chainId, userAddress, supportSignature } = params;
49
+ if (viemClient.chain?.id !== chainId) {
50
+ throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
51
+ }
52
+ const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
53
+ const pc = viemClient.extend(publicActions);
54
+ if (supportSignature) {
55
+ // The signable path needs the user's Morpho nonce; fetch it alongside the
56
+ // authorization status so both reads share a round-trip (batched into a
57
+ // single multicall when the client enables batching) instead of
58
+ // serializing the nonce read behind isAuthorized.
59
+ const [isAuthorized, nonce] = await Promise.all([
60
+ pc.readContract({
61
+ address: morpho,
62
+ abi: blueAbi,
63
+ functionName: "isAuthorized",
64
+ args: [userAddress, generalAdapter1],
65
+ }),
66
+ pc.readContract({
67
+ address: morpho,
68
+ abi: blueAbi,
69
+ functionName: "nonce",
70
+ args: [userAddress],
71
+ }),
72
+ ]);
73
+ if (isAuthorized) {
74
+ return null;
75
+ }
76
+ return encodeBlueSignatureAuthorization(viemClient, {
77
+ authorized: generalAdapter1,
78
+ chainId,
79
+ nonce,
80
+ });
81
+ }
82
+ const isAuthorized = await pc.readContract({
83
+ address: morpho,
84
+ abi: blueAbi,
85
+ functionName: "isAuthorized",
86
+ args: [userAddress, generalAdapter1],
87
+ });
88
+ if (isAuthorized) {
89
+ return null;
90
+ }
91
+ return deepFreeze({
92
+ to: morpho,
93
+ data: encodeFunctionData({
94
+ abi: blueAbi,
95
+ functionName: "setAuthorization",
96
+ args: [generalAdapter1, true],
97
+ }),
98
+ value: 0n,
99
+ action: {
100
+ type: "blueAuthorization",
101
+ args: {
102
+ authorized: generalAdapter1,
103
+ isAuthorized: true,
104
+ },
105
+ },
106
+ });
107
+ };
@@ -0,0 +1 @@
1
+ export * from "./getBlueAuthorizationRequirement.js";
@@ -0,0 +1 @@
1
+ export * from "./getBlueAuthorizationRequirement.js";
@@ -0,0 +1,55 @@
1
+ import type { Address } from "@morpho-org/blue-sdk";
2
+ import { type Client } from "viem";
3
+ import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
4
+ /** Parameters for {@link encodeBlueSignatureAuthorization}. */
5
+ interface EncodeBlueSignatureAuthorizationParams {
6
+ /** Account to authorize on Morpho (GeneralAdapter1). */
7
+ authorized: Address;
8
+ /** Target chain id; must match `viemClient.chain.id`. */
9
+ chainId: number;
10
+ /** The signer's current Morpho authorization nonce. */
11
+ nonce: bigint;
12
+ /** Whether to grant (`true`, default) or revoke (`false`) the authorization. */
13
+ isAuthorized?: boolean;
14
+ /** Signature deadline in seconds. Defaults to two hours from now. */
15
+ deadline?: bigint;
16
+ }
17
+ /**
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.
21
+ *
22
+ * The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
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()`.
26
+ *
27
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
28
+ * @param params - Authorization encoding parameters.
29
+ * @param params.authorized - Account to authorize (GeneralAdapter1).
30
+ * @param params.chainId - Target chain id.
31
+ * @param params.nonce - The signer's current Morpho authorization nonce.
32
+ * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
33
+ * @param params.deadline - Optional signature deadline in seconds.
34
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
35
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
36
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
37
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
38
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
39
+ * @example
40
+ * ```ts
41
+ * import { createWalletClient, http } from "viem";
42
+ * import { mainnet } from "viem/chains";
43
+ * import { encodeBlueSignatureAuthorization } from "@morpho-org/morpho-sdk";
44
+ *
45
+ * const client = createWalletClient({ chain: mainnet, transport: http() });
46
+ * const requirement = await encodeBlueSignatureAuthorization(client, {
47
+ * authorized: generalAdapter1,
48
+ * chainId: 1,
49
+ * nonce: 0n,
50
+ * });
51
+ * // requirement satisfies Requirement
52
+ * ```
53
+ */
54
+ export declare const encodeBlueSignatureAuthorization: (viemClient: Client, params: EncodeBlueSignatureAuthorizationParams) => Promise<Requirement<AuthorizationRequirementSignature>>;
55
+ export {};
@@ -0,0 +1,85 @@
1
+ import { getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
2
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
+ import { verifyTypedData } from "viem";
4
+ import { signTypedData } from "viem/actions";
5
+ import { validateUserAddress } from "../../../helpers/validate.js";
6
+ import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/index.js";
7
+ /**
8
+ * Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
9
+ * Morpho on the signer's behalf through `setAuthorizationWithSig` — the offchain-signature
10
+ * alternative to a standalone `setAuthorization` transaction.
11
+ *
12
+ * The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
13
+ * typed data, verifies it against the connected account, and returns a deep-frozen
14
+ * `RequirementSignature` the bundler action helpers consume. Deadline defaults to two hours from
15
+ * `Time.timestamp()`.
16
+ *
17
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
18
+ * @param params - Authorization encoding parameters.
19
+ * @param params.authorized - Account to authorize (GeneralAdapter1).
20
+ * @param params.chainId - Target chain id.
21
+ * @param params.nonce - The signer's current Morpho authorization nonce.
22
+ * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
23
+ * @param params.deadline - Optional signature deadline in seconds.
24
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
25
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
26
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
27
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
28
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
29
+ * @example
30
+ * ```ts
31
+ * import { createWalletClient, http } from "viem";
32
+ * import { mainnet } from "viem/chains";
33
+ * import { encodeBlueSignatureAuthorization } from "@morpho-org/morpho-sdk";
34
+ *
35
+ * const client = createWalletClient({ chain: mainnet, transport: http() });
36
+ * const requirement = await encodeBlueSignatureAuthorization(client, {
37
+ * authorized: generalAdapter1,
38
+ * chainId: 1,
39
+ * nonce: 0n,
40
+ * });
41
+ * // requirement satisfies Requirement
42
+ * ```
43
+ */
44
+ export const encodeBlueSignatureAuthorization = async (viemClient, params) => {
45
+ const { authorized, chainId, nonce, isAuthorized = true } = params;
46
+ if (viemClient.chain?.id !== chainId) {
47
+ throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
48
+ }
49
+ const deadline = params.deadline ?? Time.timestamp() + Time.s.from.h(2n);
50
+ const action = {
51
+ type: "authorization",
52
+ args: { authorized, isAuthorized, deadline },
53
+ };
54
+ return {
55
+ action,
56
+ async sign(client, userAddress) {
57
+ const account = client.account;
58
+ validateUserAddress(account?.address, userAddress);
59
+ const typedData = getAuthorizationTypedData({ authorizer: userAddress, authorized, isAuthorized, nonce, deadline }, chainId);
60
+ const signature = await signTypedData(client, {
61
+ ...typedData,
62
+ account,
63
+ });
64
+ const isValid = await verifyTypedData({
65
+ ...typedData,
66
+ address: userAddress, // Verify against the authorizer.
67
+ signature,
68
+ });
69
+ if (!isValid) {
70
+ throw new InvalidSignatureError();
71
+ }
72
+ return deepFreeze({
73
+ args: {
74
+ owner: userAddress,
75
+ authorized,
76
+ isAuthorized,
77
+ nonce,
78
+ deadline,
79
+ signature,
80
+ },
81
+ action,
82
+ });
83
+ },
84
+ };
85
+ };
@@ -1,5 +1,5 @@
1
1
  import { type Address } from "@morpho-org/blue-sdk";
2
- import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
2
+ import { type ERC20ApprovalAction, type Transaction } from "../../../types/index.js";
3
3
  /** Parameters for {@link encodeErc20Approval}. */
4
4
  interface EncodeErc20ApprovalParams {
5
5
  token: Address;
@@ -8,17 +8,18 @@ interface EncodeErc20ApprovalParams {
8
8
  chainId: number;
9
9
  }
10
10
  /**
11
- * Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
11
+ * Encodes a deep-frozen ERC-20 approval transaction for GeneralAdapter1 or Permit2.
12
12
  *
13
13
  * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
14
- * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
14
+ * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
15
15
  *
16
16
  * @param params - Encoding parameters.
17
17
  * @param params.token - ERC-20 token address to approve.
18
- * @param params.spender - Address granted the allowance.
18
+ * @param params.spender - Address granted the allowance. Must be GeneralAdapter1 or Permit2.
19
19
  * @param params.amount - Allowance amount before per-token cap.
20
- * @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
20
+ * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
21
21
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
22
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
22
23
  * @example
23
24
  * ```ts
24
25
  * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
@@ -1,19 +1,21 @@
1
- import { MathLib } from "@morpho-org/blue-sdk";
1
+ import { getChainAddresses, 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, isAddressEqual, maxUint256 } from "viem";
4
4
  import { MAX_TOKEN_APPROVALS } from "../../../helpers/constant.js";
5
+ import { UnsupportedErc20ApprovalSpenderError, } from "../../../types/index.js";
5
6
  /**
6
- * Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
7
+ * Encodes a deep-frozen ERC-20 approval transaction for GeneralAdapter1 or Permit2.
7
8
  *
8
9
  * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
9
- * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
10
+ * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
10
11
  *
11
12
  * @param params - Encoding parameters.
12
13
  * @param params.token - ERC-20 token address to approve.
13
- * @param params.spender - Address granted the allowance.
14
+ * @param params.spender - Address granted the allowance. Must be GeneralAdapter1 or Permit2.
14
15
  * @param params.amount - Allowance amount before per-token cap.
15
- * @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
16
+ * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
16
17
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
18
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
17
19
  * @example
18
20
  * ```ts
19
21
  * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
@@ -29,6 +31,16 @@ import { MAX_TOKEN_APPROVALS } from "../../../helpers/constant.js";
29
31
  */
30
32
  export const encodeErc20Approval = (params) => {
31
33
  const { token, spender, amount, chainId } = params;
34
+ const { permit2, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
35
+ if (!isAddressEqual(spender, generalAdapter1) &&
36
+ (permit2 == null || !isAddressEqual(spender, permit2))) {
37
+ throw new UnsupportedErc20ApprovalSpenderError({
38
+ spender,
39
+ chainId,
40
+ generalAdapter1,
41
+ permit2,
42
+ });
43
+ }
32
44
  const amountValue = MathLib.min(amount, MAX_TOKEN_APPROVALS[chainId]?.[token] ?? maxUint256);
33
45
  return deepFreeze({
34
46
  to: token,
@@ -40,7 +52,7 @@ export const encodeErc20Approval = (params) => {
40
52
  value: 0n,
41
53
  action: {
42
54
  type: "erc20Approval",
43
- args: { spender: spender, amount: amountValue },
55
+ args: { spender, amount: amountValue },
44
56
  },
45
57
  });
46
58
  };
@@ -1,6 +1,6 @@
1
- import type { Address } from "@morpho-org/blue-sdk";
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
2
  import { type Client } from "viem";
3
- import { type Requirement } from "../../../types/index.js";
3
+ import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
4
4
  /** Parameters for {@link encodeErc20Permit}. */
5
5
  interface EncodeErc20PermitParams {
6
6
  token: Address;
@@ -11,8 +11,8 @@ interface EncodeErc20PermitParams {
11
11
  supportDeployless?: boolean;
12
12
  }
13
13
  /**
14
- * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
15
- * `token`.
14
+ * Builds an EIP-2612 permit `Requirement` that, once signed, lets a supported SDK spender pull
15
+ * `amount` of `token`.
16
16
  *
17
17
  * Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
18
18
  * signature, verifies it against the connected account, and returns a `RequirementSignature`
@@ -21,13 +21,15 @@ interface EncodeErc20PermitParams {
21
21
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
22
22
  * @param params - Permit encoding parameters.
23
23
  * @param params.token - ERC-20 token address (must support EIP-2612).
24
- * @param params.spender - Address that will be granted the permit allowance.
24
+ * @param params.spender - Permit spender. Must be GeneralAdapter1 or MidnightBundles for the chain.
25
25
  * @param params.amount - Permit allowance amount.
26
26
  * @param params.chainId - Target chain id.
27
27
  * @param params.nonce - The user's current EIP-2612 nonce on `token`.
28
28
  * @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
29
29
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
30
30
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
31
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or
32
+ * MidnightBundles for `chainId`.
31
33
  * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
32
34
  * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
33
35
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
@@ -39,7 +41,7 @@ interface EncodeErc20PermitParams {
39
41
  *
40
42
  * const client = createWalletClient({ chain: mainnet, transport: http() });
41
43
  * const requirement = await encodeErc20Permit(client, {
42
- * token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
44
+ * token: USDC, // Must implement standard ERC-2612. DAI is routed through Permit2 by requirement helpers.
43
45
  * spender: generalAdapter1,
44
46
  * amount: 1_000_000n,
45
47
  * chainId: 1,
@@ -48,5 +50,5 @@ interface EncodeErc20PermitParams {
48
50
  * // requirement satisfies Requirement
49
51
  * ```
50
52
  */
51
- export declare const encodeErc20Permit: (viemClient: Client, params: EncodeErc20PermitParams) => Promise<Requirement>;
53
+ export declare const encodeErc20Permit: (viemClient: Client, params: EncodeErc20PermitParams) => Promise<Requirement<PermitRequirementSignature>>;
52
54
  export {};
@@ -1,12 +1,13 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
1
2
  import { fetchToken, getPermitTypedData } from "@morpho-org/blue-sdk-viem";
2
3
  import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
- import { verifyTypedData } from "viem";
4
+ import { isAddressEqual, verifyTypedData, } from "viem";
4
5
  import { signTypedData } from "viem/actions";
5
6
  import { validateUserAddress } from "../../../helpers/validate.js";
6
- import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/index.js";
7
+ import { ChainIdMismatchError, InvalidSignatureError, UnsupportedErc20ApprovalSpenderError, } from "../../../types/index.js";
7
8
  /**
8
- * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
9
- * `token`.
9
+ * Builds an EIP-2612 permit `Requirement` that, once signed, lets a supported SDK spender pull
10
+ * `amount` of `token`.
10
11
  *
11
12
  * Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
12
13
  * signature, verifies it against the connected account, and returns a `RequirementSignature`
@@ -15,13 +16,15 @@ import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/ind
15
16
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
16
17
  * @param params - Permit encoding parameters.
17
18
  * @param params.token - ERC-20 token address (must support EIP-2612).
18
- * @param params.spender - Address that will be granted the permit allowance.
19
+ * @param params.spender - Permit spender. Must be GeneralAdapter1 or MidnightBundles for the chain.
19
20
  * @param params.amount - Permit allowance amount.
20
21
  * @param params.chainId - Target chain id.
21
22
  * @param params.nonce - The user's current EIP-2612 nonce on `token`.
22
23
  * @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
23
24
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
24
25
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
26
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or
27
+ * MidnightBundles for `chainId`.
25
28
  * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
26
29
  * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
27
30
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
@@ -33,7 +36,7 @@ import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/ind
33
36
  *
34
37
  * const client = createWalletClient({ chain: mainnet, transport: http() });
35
38
  * const requirement = await encodeErc20Permit(client, {
36
- * token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
39
+ * token: USDC, // Must implement standard ERC-2612. DAI is routed through Permit2 by requirement helpers.
37
40
  * spender: generalAdapter1,
38
41
  * amount: 1_000_000n,
39
42
  * chainId: 1,
@@ -47,6 +50,17 @@ export const encodeErc20Permit = async (viemClient, params) => {
47
50
  if (viemClient.chain?.id !== chainId) {
48
51
  throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
49
52
  }
53
+ const { midnightBundles, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
54
+ if (!isAddressEqual(spender, generalAdapter1) &&
55
+ (midnightBundles == null || !isAddressEqual(spender, midnightBundles))) {
56
+ throw new UnsupportedErc20ApprovalSpenderError({
57
+ spender,
58
+ chainId,
59
+ generalAdapter1,
60
+ midnightBundles,
61
+ supportedSpenders: [generalAdapter1, midnightBundles],
62
+ });
63
+ }
50
64
  const now = Time.timestamp();
51
65
  const deadline = now + Time.s.from.h(2n);
52
66
  const tokenData = await fetchToken(token, viemClient, {
@@ -1,25 +1,23 @@
1
1
  import { type Address } from "@morpho-org/blue-sdk";
2
- import { type Requirement } from "../../../types/index.js";
3
- /** Parameters for {@link encodeErc20Permit2}. */
4
- interface EncodeErc20Permit2Params {
2
+ import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
3
+ /** Parameters for {@link encodeErc20Permit2Approve}. */
4
+ interface EncodeErc20Permit2ApproveParams {
5
5
  token: Address;
6
6
  amount: bigint;
7
7
  chainId: number;
8
8
  nonce: bigint;
9
- expiration: bigint;
9
+ expiration?: bigint;
10
10
  }
11
11
  /**
12
- * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
13
- * `token` via the Permit2 contract.
12
+ * Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
13
+ * via the Permit2 contract.
14
14
  *
15
- * The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
16
- * address — otherwise the resulting signature could be reused independently of the Morpho
17
- * bundle. Deadline defaults to two hours from `Time.timestamp()`.
15
+ * Deadline defaults to two hours from `Time.timestamp()`.
18
16
  *
19
17
  * @param params - Permit2 encoding parameters.
20
18
  * @param params.token - ERC-20 token address.
21
19
  * @param params.amount - Permit2 allowance amount (per-call).
22
- * @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
20
+ * @param params.chainId - Target chain id.
23
21
  * @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
24
22
  * @param params.expiration - Permit2-managed allowance expiration timestamp.
25
23
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
@@ -28,9 +26,9 @@ interface EncodeErc20Permit2Params {
28
26
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
29
27
  * @example
30
28
  * ```ts
31
- * import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
29
+ * import { encodeErc20Permit2Approve } from "@morpho-org/morpho-sdk";
32
30
  *
33
- * const requirement = encodeErc20Permit2({
31
+ * const requirement = encodeErc20Permit2Approve({
34
32
  * token: USDC,
35
33
  * amount: 1_000_000n,
36
34
  * chainId: 1,
@@ -40,5 +38,5 @@ interface EncodeErc20Permit2Params {
40
38
  * // requirement satisfies Requirement
41
39
  * ```
42
40
  */
43
- export declare const encodeErc20Permit2: (params: EncodeErc20Permit2Params) => Requirement;
41
+ export declare const encodeErc20Permit2Approve: (params: EncodeErc20Permit2ApproveParams) => Requirement<PermitRequirementSignature>;
44
42
  export {};
@@ -6,17 +6,15 @@ import { signTypedData } from "viem/actions";
6
6
  import { validateUserAddress } from "../../../helpers/validate.js";
7
7
  import { InvalidSignatureError, } from "../../../types/index.js";
8
8
  /**
9
- * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
10
- * `token` via the Permit2 contract.
9
+ * Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
10
+ * via the Permit2 contract.
11
11
  *
12
- * The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
13
- * address — otherwise the resulting signature could be reused independently of the Morpho
14
- * bundle. Deadline defaults to two hours from `Time.timestamp()`.
12
+ * Deadline defaults to two hours from `Time.timestamp()`.
15
13
  *
16
14
  * @param params - Permit2 encoding parameters.
17
15
  * @param params.token - ERC-20 token address.
18
16
  * @param params.amount - Permit2 allowance amount (per-call).
19
- * @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
17
+ * @param params.chainId - Target chain id.
20
18
  * @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
21
19
  * @param params.expiration - Permit2-managed allowance expiration timestamp.
22
20
  * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
@@ -25,9 +23,9 @@ import { InvalidSignatureError, } from "../../../types/index.js";
25
23
  * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
26
24
  * @example
27
25
  * ```ts
28
- * import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
26
+ * import { encodeErc20Permit2Approve } from "@morpho-org/morpho-sdk";
29
27
  *
30
- * const requirement = encodeErc20Permit2({
28
+ * const requirement = encodeErc20Permit2Approve({
31
29
  * token: USDC,
32
30
  * amount: 1_000_000n,
33
31
  * chainId: 1,
@@ -37,11 +35,11 @@ import { InvalidSignatureError, } from "../../../types/index.js";
37
35
  * // requirement satisfies Requirement
38
36
  * ```
39
37
  */
40
- export const encodeErc20Permit2 = (params) => {
38
+ export const encodeErc20Permit2Approve = (params) => {
41
39
  const { token, amount, chainId, nonce, expiration = MathLib.MAX_UINT_48, } = params;
40
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
42
41
  const now = Time.timestamp();
43
42
  const deadline = now + Time.s.from.h(2n);
44
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
45
43
  const action = {
46
44
  type: "permit2",
47
45
  args: {
@@ -57,8 +55,6 @@ export const encodeErc20Permit2 = (params) => {
57
55
  const account = client.account;
58
56
  validateUserAddress(account?.address, userAddress);
59
57
  const typedData = getPermit2PermitTypedData({
60
- // Never permit any other address than the GeneralAdapter1 otherwise
61
- // the signature can be used independently.
62
58
  spender: generalAdapter1,
63
59
  allowance: amount,
64
60
  erc20: token,
@@ -1,3 +1,4 @@
1
+ export * from "./encodeBlueSignatureAuthorization.js";
1
2
  export * from "./encodeErc20Approval.js";
2
3
  export * from "./encodeErc20Permit.js";
3
- export * from "./encodeErc20Permit2.js";
4
+ export * from "./encodeErc20Permit2Approve.js";
@@ -1,3 +1,4 @@
1
+ export * from "./encodeBlueSignatureAuthorization.js";
1
2
  export * from "./encodeErc20Approval.js";
2
3
  export * from "./encodeErc20Permit.js";
3
- export * from "./encodeErc20Permit2.js";
4
+ export * from "./encodeErc20Permit2Approve.js";