@morpho-org/morpho-sdk 4.1.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.
- package/README.md +12 -12
- package/lib/cjs/actions/blue/borrow.d.ts +12 -2
- package/lib/cjs/actions/blue/borrow.js +7 -1
- package/lib/cjs/actions/blue/refinance.d.ts +13 -3
- package/lib/cjs/actions/blue/refinance.js +8 -2
- package/lib/cjs/actions/blue/repay.d.ts +6 -5
- package/lib/cjs/actions/blue/repay.js +10 -18
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +15 -6
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +16 -18
- package/lib/cjs/actions/blue/supply.d.ts +7 -7
- package/lib/cjs/actions/blue/supply.js +10 -20
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +8 -8
- package/lib/cjs/actions/blue/supplyCollateral.js +11 -21
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +18 -6
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +17 -19
- package/lib/cjs/actions/blue/withdraw.d.ts +10 -2
- package/lib/cjs/actions/blue/withdraw.js +7 -1
- package/lib/cjs/actions/index.d.ts +1 -0
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +111 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/blue/index.js +17 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +89 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +17 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +18 -4
- package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
- package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +10 -14
- package/lib/cjs/actions/requirements/encode/index.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/index.js +2 -1
- package/lib/cjs/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +151 -0
- package/lib/{esm/actions/requirements/getRequirementsPermit.d.ts → cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +50 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +74 -0
- package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +3 -0
- package/lib/cjs/actions/requirements/generalAdapter/index.js +19 -0
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +3 -2
- package/lib/cjs/actions/requirements/index.d.ts +2 -5
- package/lib/cjs/actions/requirements/index.js +2 -5
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +48 -0
- package/lib/{esm/actions/requirements/getRequirementsAction.d.ts → cjs/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
- package/lib/cjs/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +26 -13
- package/lib/cjs/actions/signatures/index.d.ts +2 -0
- package/lib/cjs/actions/signatures/index.js +18 -0
- package/lib/cjs/actions/vaultV1/deposit.d.ts +6 -8
- package/lib/cjs/actions/vaultV1/deposit.js +11 -21
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +6 -6
- package/lib/cjs/actions/vaultV1/migrateToV2.js +12 -20
- package/lib/cjs/actions/vaultV2/deposit.d.ts +6 -8
- package/lib/cjs/actions/vaultV2/deposit.js +11 -21
- package/lib/cjs/addresses.d.ts +2 -2
- package/lib/cjs/addresses.js +2 -1
- package/lib/cjs/bundler/actions.d.ts +44 -4
- package/lib/cjs/bundler/actions.js +75 -2
- package/lib/cjs/bundler/types.d.ts +6 -0
- package/lib/cjs/constants.d.ts +2 -2
- package/lib/cjs/constants.js +3 -3
- package/lib/cjs/entities/blue/blue.d.ts +58 -33
- package/lib/cjs/entities/blue/blue.js +171 -125
- package/lib/cjs/entities/reallocationData.d.ts +12 -12
- package/lib/cjs/entities/reallocationData.js +19 -19
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +9 -9
- package/lib/cjs/entities/vaultV1/vaultV1.js +44 -34
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +6 -6
- package/lib/cjs/entities/vaultV2/vaultV2.js +21 -16
- package/lib/cjs/errors.d.ts +3 -2
- package/lib/cjs/errors.js +5 -2
- package/lib/cjs/helpers/validate.d.ts +2 -2
- package/lib/cjs/helpers/validate.js +1 -1
- package/lib/cjs/types/action.d.ts +113 -9
- package/lib/cjs/types/action.js +79 -5
- package/lib/cjs/types/error.d.ts +73 -0
- package/lib/cjs/types/error.js +89 -4
- package/lib/cjs/types.d.ts +2 -1
- package/lib/cjs/utils.d.ts +1 -1
- package/lib/cjs/utils.js +2 -3
- package/lib/esm/actions/blue/borrow.d.ts +12 -2
- package/lib/esm/actions/blue/borrow.js +7 -1
- package/lib/esm/actions/blue/refinance.d.ts +13 -3
- package/lib/esm/actions/blue/refinance.js +8 -2
- package/lib/esm/actions/blue/repay.d.ts +6 -5
- package/lib/esm/actions/blue/repay.js +10 -18
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +15 -6
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +16 -18
- package/lib/esm/actions/blue/supply.d.ts +7 -7
- package/lib/esm/actions/blue/supply.js +10 -20
- package/lib/esm/actions/blue/supplyCollateral.d.ts +8 -8
- package/lib/esm/actions/blue/supplyCollateral.js +11 -21
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +18 -6
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +17 -19
- package/lib/esm/actions/blue/withdraw.d.ts +10 -2
- package/lib/esm/actions/blue/withdraw.js +7 -1
- package/lib/esm/actions/index.d.ts +1 -0
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +107 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
- package/lib/esm/actions/requirements/blue/index.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +85 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +19 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +20 -6
- package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
- package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +8 -12
- package/lib/esm/actions/requirements/encode/index.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/index.js +2 -1
- package/lib/esm/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +147 -0
- package/lib/{cjs/actions/requirements/getRequirementsPermit.d.ts → esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +46 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +70 -0
- package/lib/esm/actions/requirements/generalAdapter/index.d.ts +3 -0
- package/lib/esm/actions/requirements/generalAdapter/index.js +3 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.js +3 -2
- package/lib/esm/actions/requirements/index.d.ts +2 -5
- package/lib/esm/actions/requirements/index.js +2 -5
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +44 -0
- package/lib/{cjs/actions/requirements/getRequirementsAction.d.ts → esm/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
- package/lib/esm/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +24 -11
- package/lib/esm/actions/signatures/index.d.ts +2 -0
- package/lib/esm/actions/signatures/index.js +2 -0
- package/lib/esm/actions/vaultV1/deposit.d.ts +6 -8
- package/lib/esm/actions/vaultV1/deposit.js +11 -21
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +6 -6
- package/lib/esm/actions/vaultV1/migrateToV2.js +12 -20
- package/lib/esm/actions/vaultV2/deposit.d.ts +6 -8
- package/lib/esm/actions/vaultV2/deposit.js +11 -21
- package/lib/esm/addresses.d.ts +2 -2
- package/lib/esm/addresses.js +1 -1
- package/lib/esm/bundler/actions.d.ts +44 -4
- package/lib/esm/bundler/actions.js +77 -4
- package/lib/esm/bundler/types.d.ts +6 -0
- package/lib/esm/constants.d.ts +2 -2
- package/lib/esm/constants.js +2 -2
- package/lib/esm/entities/blue/blue.d.ts +58 -33
- package/lib/esm/entities/blue/blue.js +173 -127
- package/lib/esm/entities/reallocationData.d.ts +12 -12
- package/lib/esm/entities/reallocationData.js +18 -18
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +9 -9
- package/lib/esm/entities/vaultV1/vaultV1.js +46 -36
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +6 -6
- package/lib/esm/entities/vaultV2/vaultV2.js +23 -18
- package/lib/esm/errors.d.ts +3 -2
- package/lib/esm/errors.js +2 -1
- package/lib/esm/helpers/validate.d.ts +2 -2
- package/lib/esm/helpers/validate.js +1 -1
- package/lib/esm/types/action.d.ts +113 -9
- package/lib/esm/types/action.js +75 -4
- package/lib/esm/types/error.d.ts +73 -0
- package/lib/esm/types/error.js +84 -2
- package/lib/esm/types.d.ts +2 -1
- package/lib/esm/utils.d.ts +1 -1
- package/lib/esm/utils.js +1 -1
- package/package.json +6 -6
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +0 -71
- package/lib/cjs/actions/requirements/getRequirements.js +0 -112
- package/lib/cjs/actions/requirements/getRequirementsPermit.js +0 -57
- package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +0 -63
- package/lib/cjs/actions/requirements/getRequirementsPermit2.js +0 -85
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +0 -67
- package/lib/esm/actions/requirements/getRequirements.js +0 -108
- package/lib/esm/actions/requirements/getRequirementsPermit.js +0 -53
- package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +0 -63
- package/lib/esm/actions/requirements/getRequirementsPermit2.js +0 -81
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import type { Bundler3TokenSignatureRequirement, ERC20ApprovalAction, Transaction } from "../../../types/index.js";
|
|
4
|
+
/** Shared parameters for GeneralAdapter1 token requirement resolution. */
|
|
5
|
+
export type GetGeneralAdapterRequirementsBaseParams = {
|
|
5
6
|
address: Address;
|
|
6
7
|
chainId: number;
|
|
7
8
|
supportDeployless?: boolean;
|
|
@@ -10,31 +11,44 @@ type GetRequirementsBaseParams = {
|
|
|
10
11
|
from: Address;
|
|
11
12
|
};
|
|
12
13
|
};
|
|
13
|
-
|
|
14
|
+
/** Parameters for {@link getGeneralAdapterRequirements}. */
|
|
15
|
+
export type GetGeneralAdapterRequirementsParams = (GetGeneralAdapterRequirementsBaseParams & {
|
|
14
16
|
/** Signature-based approvals are not supported. Classic approval (transaction) will be used. */
|
|
15
17
|
supportSignature: false;
|
|
16
|
-
}) | (
|
|
18
|
+
}) | (GetGeneralAdapterRequirementsBaseParams & {
|
|
17
19
|
/** Signature-based approvals are supported. Will try permit (EIP-2612), else fallback to permit2. */
|
|
18
20
|
supportSignature: true;
|
|
19
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Prefer the ERC-2612 simple-permit path when the SDK detects support.
|
|
23
|
+
* Leave unset or set to `false` to force the Permit2 fallback when a token is known to be
|
|
24
|
+
* incompatible despite passing the SDK's shallow nonce probe.
|
|
25
|
+
*/
|
|
20
26
|
useSimplePermit?: boolean;
|
|
21
27
|
});
|
|
22
28
|
/**
|
|
23
|
-
* Resolves the approval requirements an integrator must satisfy before a
|
|
29
|
+
* Resolves the approval requirements an integrator must satisfy before a bundled action pulls
|
|
24
30
|
* tokens through `GeneralAdapter1`.
|
|
25
31
|
*
|
|
26
|
-
* Reads the
|
|
27
|
-
*
|
|
32
|
+
* Reads the minimum token allowance / nonce state needed from the chain, then picks one of three
|
|
33
|
+
* flows:
|
|
28
34
|
*
|
|
29
35
|
* 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
|
|
30
|
-
* allowance is already large enough).
|
|
31
|
-
* 2. **`supportSignature: true` + EIP-2612
|
|
36
|
+
* direct allowance is already large enough).
|
|
37
|
+
* 2. **`supportSignature: true` + EIP-2612 nonce detected + `useSimplePermit`** — single permit
|
|
32
38
|
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
33
39
|
* permit signature is incompatible) and falls through to Permit2 even with
|
|
34
40
|
* `useSimplePermit: true`.
|
|
35
41
|
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
36
42
|
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
37
43
|
*
|
|
44
|
+
* The simple-permit compatibility check is intentionally shallow: when requested, it probes
|
|
45
|
+
* whether the token exposes a readable ERC-2612 `nonces(owner)` value.
|
|
46
|
+
* Leaving `useSimplePermit` unset, or passing `false`, is the caller escape hatch for tokens
|
|
47
|
+
* that expose `nonces` but are still incompatible with the SDK's ERC-2612 encoder. This opt-out
|
|
48
|
+
* has proven useful in the past, but the SDK does not encode a token-specific example here.
|
|
49
|
+
* DAI is handled as a built-in version of that incompatibility: it exposes `nonces(owner)` but is
|
|
50
|
+
* always routed to Permit2/classic approval instead of DAI-specific permit signing.
|
|
51
|
+
*
|
|
38
52
|
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
39
53
|
* @param params - Requirement resolution parameters.
|
|
40
54
|
* @param params.address - ERC-20 token address.
|
|
@@ -43,12 +57,13 @@ type GetRequirementsParams = (GetRequirementsBaseParams & {
|
|
|
43
57
|
* @param params.args.from - Account that will grant the approval.
|
|
44
58
|
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
45
59
|
* permit / permit2 vs. classic approval.
|
|
46
|
-
* @param params.supportDeployless - Whether
|
|
60
|
+
* @param params.supportDeployless - Whether ERC-2612 permit metadata reads may use deployless multicall.
|
|
47
61
|
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
48
|
-
* the
|
|
62
|
+
* the nonce probe detects support. Leave unset or pass `false` to force the Permit2 fallback
|
|
63
|
+
* for tokens known to be incompatible despite passing that probe.
|
|
49
64
|
* @returns Promise resolving to an array of either deep-frozen approval transactions or
|
|
50
|
-
* `Requirement` objects (signature requirements with a `sign()` method). Empty when
|
|
51
|
-
*
|
|
65
|
+
* `Requirement` objects (signature requirements with a `sign()` method). Empty when `amount`
|
|
66
|
+
* is zero or when the classic-approval path can reuse sufficient direct allowance.
|
|
52
67
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
|
|
53
68
|
* error is reachable through this entry point: the values passed into
|
|
54
69
|
* `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
|
|
@@ -58,17 +73,16 @@ type GetRequirementsParams = (GetRequirementsBaseParams & {
|
|
|
58
73
|
* ```ts
|
|
59
74
|
* import { createPublicClient, http } from "viem";
|
|
60
75
|
* import { mainnet } from "viem/chains";
|
|
61
|
-
* import {
|
|
76
|
+
* import { getGeneralAdapterRequirements } from "@morpho-org/morpho-sdk";
|
|
62
77
|
*
|
|
63
78
|
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
64
|
-
* const requirements = await
|
|
79
|
+
* const requirements = await getGeneralAdapterRequirements(client, {
|
|
65
80
|
* address: USDC,
|
|
66
81
|
* chainId: 1,
|
|
67
82
|
* supportSignature: true,
|
|
68
83
|
* args: { amount: 1_000_000n, from: user },
|
|
69
84
|
* });
|
|
70
|
-
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> |
|
|
85
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Bundler3TokenSignatureRequirement)[]
|
|
71
86
|
* ```
|
|
72
87
|
*/
|
|
73
|
-
export declare const
|
|
74
|
-
export {};
|
|
88
|
+
export declare const getGeneralAdapterRequirements: (viemClient: Client, params: GetGeneralAdapterRequirementsParams) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Bundler3TokenSignatureRequirement)[]>;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { erc2612Abi, permit2Abi } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { isDefined } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { erc20Abi, isAddressEqual } from "viem";
|
|
5
|
+
import { readContract } from "viem/actions";
|
|
6
|
+
import { ChainIdMismatchError } from "../../../types/index.js";
|
|
7
|
+
import { getRequirementsApproval } from "../getRequirementsApproval.js";
|
|
8
|
+
import { getGeneralAdapterRequirementsPermit } from "./getGeneralAdapterRequirementsPermit.js";
|
|
9
|
+
import { getGeneralAdapterRequirementsPermit2 } from "./getGeneralAdapterRequirementsPermit2.js";
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the approval requirements an integrator must satisfy before a bundled action pulls
|
|
12
|
+
* tokens through `GeneralAdapter1`.
|
|
13
|
+
*
|
|
14
|
+
* Reads the minimum token allowance / nonce state needed from the chain, then picks one of three
|
|
15
|
+
* flows:
|
|
16
|
+
*
|
|
17
|
+
* 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
|
|
18
|
+
* direct allowance is already large enough).
|
|
19
|
+
* 2. **`supportSignature: true` + EIP-2612 nonce detected + `useSimplePermit`** — single permit
|
|
20
|
+
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
21
|
+
* permit signature is incompatible) and falls through to Permit2 even with
|
|
22
|
+
* `useSimplePermit: true`.
|
|
23
|
+
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
24
|
+
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
25
|
+
*
|
|
26
|
+
* The simple-permit compatibility check is intentionally shallow: when requested, it probes
|
|
27
|
+
* whether the token exposes a readable ERC-2612 `nonces(owner)` value.
|
|
28
|
+
* Leaving `useSimplePermit` unset, or passing `false`, is the caller escape hatch for tokens
|
|
29
|
+
* that expose `nonces` but are still incompatible with the SDK's ERC-2612 encoder. This opt-out
|
|
30
|
+
* has proven useful in the past, but the SDK does not encode a token-specific example here.
|
|
31
|
+
* DAI is handled as a built-in version of that incompatibility: it exposes `nonces(owner)` but is
|
|
32
|
+
* always routed to Permit2/classic approval instead of DAI-specific permit signing.
|
|
33
|
+
*
|
|
34
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
35
|
+
* @param params - Requirement resolution parameters.
|
|
36
|
+
* @param params.address - ERC-20 token address.
|
|
37
|
+
* @param params.chainId - Chain id; must match `viemClient.chain.id`.
|
|
38
|
+
* @param params.args.amount - Required token amount. Returns `[]` when zero.
|
|
39
|
+
* @param params.args.from - Account that will grant the approval.
|
|
40
|
+
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
41
|
+
* permit / permit2 vs. classic approval.
|
|
42
|
+
* @param params.supportDeployless - Whether ERC-2612 permit metadata reads may use deployless multicall.
|
|
43
|
+
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
44
|
+
* the nonce probe detects support. Leave unset or pass `false` to force the Permit2 fallback
|
|
45
|
+
* for tokens known to be incompatible despite passing that probe.
|
|
46
|
+
* @returns Promise resolving to an array of either deep-frozen approval transactions or
|
|
47
|
+
* `Requirement` objects (signature requirements with a `sign()` method). Empty when `amount`
|
|
48
|
+
* is zero or when the classic-approval path can reuse sufficient direct allowance.
|
|
49
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
|
|
50
|
+
* error is reachable through this entry point: the values passed into
|
|
51
|
+
* `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
|
|
52
|
+
* `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
|
|
53
|
+
* `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { createPublicClient, http } from "viem";
|
|
57
|
+
* import { mainnet } from "viem/chains";
|
|
58
|
+
* import { getGeneralAdapterRequirements } from "@morpho-org/morpho-sdk";
|
|
59
|
+
*
|
|
60
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
61
|
+
* const requirements = await getGeneralAdapterRequirements(client, {
|
|
62
|
+
* address: USDC,
|
|
63
|
+
* chainId: 1,
|
|
64
|
+
* supportSignature: true,
|
|
65
|
+
* args: { amount: 1_000_000n, from: user },
|
|
66
|
+
* });
|
|
67
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Bundler3TokenSignatureRequirement)[]
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export const getGeneralAdapterRequirements = async (viemClient, params) => {
|
|
71
|
+
const { address, chainId, supportSignature, args: { amount, from }, } = params;
|
|
72
|
+
if (viemClient.chain?.id !== chainId) {
|
|
73
|
+
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
74
|
+
}
|
|
75
|
+
if (amount === 0n) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const { permit2, dai, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
79
|
+
if (supportSignature) {
|
|
80
|
+
const { useSimplePermit } = params;
|
|
81
|
+
const isDai = isDefined(dai) && isAddressEqual(address, dai);
|
|
82
|
+
if (useSimplePermit && !isDai) {
|
|
83
|
+
const erc2612Nonce = await readContract(viemClient, {
|
|
84
|
+
abi: erc2612Abi,
|
|
85
|
+
address,
|
|
86
|
+
functionName: "nonces",
|
|
87
|
+
args: [from],
|
|
88
|
+
}).catch(() => undefined);
|
|
89
|
+
if (isDefined(erc2612Nonce)) {
|
|
90
|
+
// Do not skip this exact permit because a previous ERC-20 allowance is already enough.
|
|
91
|
+
// ERC-2612 overwrites the allowance, and the bundle spends exactly `amount`, so signing
|
|
92
|
+
// the pulled amount leaves no residual direct allowance after inclusion.
|
|
93
|
+
return getGeneralAdapterRequirementsPermit(viemClient, {
|
|
94
|
+
token: address,
|
|
95
|
+
chainId,
|
|
96
|
+
args: { amount },
|
|
97
|
+
nonce: erc2612Nonce,
|
|
98
|
+
supportDeployless: params.supportDeployless,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (permit2) {
|
|
103
|
+
// Do not check the existing Permit2-managed amount/expiration. Permit2 allowance signatures
|
|
104
|
+
// overwrite the tuple, and the bundle spends exactly `amount`, so a fresh exact signature
|
|
105
|
+
// leaves no residual Permit2 allowance. The tuple is still read because Permit2 requires
|
|
106
|
+
// its nonce in the signed allowance payload.
|
|
107
|
+
const [permit2Erc20Allowance, [, , permit2Nonce]] = await Promise.all([
|
|
108
|
+
readContract(viemClient, {
|
|
109
|
+
abi: erc20Abi,
|
|
110
|
+
address,
|
|
111
|
+
functionName: "allowance",
|
|
112
|
+
args: [from, permit2],
|
|
113
|
+
}),
|
|
114
|
+
readContract(viemClient, {
|
|
115
|
+
abi: permit2Abi,
|
|
116
|
+
address: permit2,
|
|
117
|
+
functionName: "allowance",
|
|
118
|
+
args: [from, address, generalAdapter1],
|
|
119
|
+
}),
|
|
120
|
+
]);
|
|
121
|
+
return getGeneralAdapterRequirementsPermit2({
|
|
122
|
+
address,
|
|
123
|
+
chainId,
|
|
124
|
+
permit2,
|
|
125
|
+
args: { amount },
|
|
126
|
+
erc20Allowances: { permit2: permit2Erc20Allowance },
|
|
127
|
+
permit2Nonce: BigInt(permit2Nonce),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const generalAdapterAllowance = await readContract(viemClient, {
|
|
132
|
+
abi: erc20Abi,
|
|
133
|
+
address,
|
|
134
|
+
functionName: "allowance",
|
|
135
|
+
args: [from, generalAdapter1],
|
|
136
|
+
});
|
|
137
|
+
return getRequirementsApproval({
|
|
138
|
+
address,
|
|
139
|
+
chainId,
|
|
140
|
+
args: {
|
|
141
|
+
spendAmount: amount,
|
|
142
|
+
approvalAmount: amount,
|
|
143
|
+
spender: generalAdapter1,
|
|
144
|
+
},
|
|
145
|
+
allowances: generalAdapterAllowance,
|
|
146
|
+
});
|
|
147
|
+
};
|
|
@@ -3,43 +3,35 @@ import type { Address, Client } from "viem";
|
|
|
3
3
|
* Computes the EIP-2612 permit `Requirement` an integrator must sign so that `GeneralAdapter1`
|
|
4
4
|
* can pull `amount` of `token`.
|
|
5
5
|
*
|
|
6
|
-
* Returns an empty array when the existing allowance for `GeneralAdapter1` already covers
|
|
7
|
-
* `amount`.
|
|
8
|
-
*
|
|
9
6
|
* @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
|
|
10
7
|
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
11
8
|
* @param params.chainId - The chain the bundle targets.
|
|
12
9
|
* @param params.args.amount - Required token amount.
|
|
13
|
-
* @param params.allowancesGeneralAdapter - The user's current allowance of `token` for
|
|
14
|
-
* `GeneralAdapter1`.
|
|
15
10
|
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
16
11
|
* @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
|
|
17
|
-
* @returns A single-element array containing the `Requirement` to sign
|
|
18
|
-
* the existing allowance already covers `amount`.
|
|
12
|
+
* @returns A single-element array containing the exact-amount `Requirement` to sign.
|
|
19
13
|
* @example
|
|
20
14
|
* ```ts
|
|
21
15
|
* import { createWalletClient, http } from "viem";
|
|
22
16
|
* import { mainnet } from "viem/chains";
|
|
23
|
-
* import {
|
|
17
|
+
* import { getGeneralAdapterRequirementsPermit } from "@morpho-org/morpho-sdk";
|
|
24
18
|
*
|
|
25
19
|
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
26
|
-
* const reqs = await
|
|
27
|
-
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by
|
|
20
|
+
* const reqs = await getGeneralAdapterRequirementsPermit(client, {
|
|
21
|
+
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by getGeneralAdapterRequirements
|
|
28
22
|
* chainId: 1,
|
|
29
23
|
* args: { amount: 1_000_000n },
|
|
30
|
-
* allowancesGeneralAdapter: 0n,
|
|
31
24
|
* nonce: 0n,
|
|
32
25
|
* });
|
|
33
26
|
* // reqs satisfies Requirement[]
|
|
34
27
|
* ```
|
|
35
28
|
*/
|
|
36
|
-
export declare const
|
|
29
|
+
export declare const getGeneralAdapterRequirementsPermit: (viemClient: Client, params: {
|
|
37
30
|
token: Address;
|
|
38
31
|
chainId: number;
|
|
39
32
|
args: {
|
|
40
33
|
amount: bigint;
|
|
41
34
|
};
|
|
42
|
-
allowancesGeneralAdapter: bigint;
|
|
43
35
|
nonce: bigint;
|
|
44
36
|
supportDeployless?: boolean;
|
|
45
|
-
}) => Promise<import("
|
|
37
|
+
}) => Promise<import("../../../index.js").Requirement<import("../../../index.js").PermitRequirementSignature>[]>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { encodeErc20Permit } from "../encode/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Computes the EIP-2612 permit `Requirement` an integrator must sign so that `GeneralAdapter1`
|
|
5
|
+
* can pull `amount` of `token`.
|
|
6
|
+
*
|
|
7
|
+
* @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
|
|
8
|
+
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
9
|
+
* @param params.chainId - The chain the bundle targets.
|
|
10
|
+
* @param params.args.amount - Required token amount.
|
|
11
|
+
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
12
|
+
* @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
|
|
13
|
+
* @returns A single-element array containing the exact-amount `Requirement` to sign.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { createWalletClient, http } from "viem";
|
|
17
|
+
* import { mainnet } from "viem/chains";
|
|
18
|
+
* import { getGeneralAdapterRequirementsPermit } from "@morpho-org/morpho-sdk";
|
|
19
|
+
*
|
|
20
|
+
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
21
|
+
* const reqs = await getGeneralAdapterRequirementsPermit(client, {
|
|
22
|
+
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by getGeneralAdapterRequirements
|
|
23
|
+
* chainId: 1,
|
|
24
|
+
* args: { amount: 1_000_000n },
|
|
25
|
+
* nonce: 0n,
|
|
26
|
+
* });
|
|
27
|
+
* // reqs satisfies Requirement[]
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const getGeneralAdapterRequirementsPermit = async (viemClient, params) => {
|
|
31
|
+
const { token, chainId, args: { amount }, nonce, supportDeployless, } = params;
|
|
32
|
+
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
33
|
+
// Existing direct ERC-20 allowance is intentionally not an input here. ERC-2612 overwrites the
|
|
34
|
+
// allowance with the signed amount, and the bundle spends exactly `amount`, leaving no residual
|
|
35
|
+
// allowance after inclusion.
|
|
36
|
+
return [
|
|
37
|
+
await encodeErc20Permit(viemClient, {
|
|
38
|
+
token,
|
|
39
|
+
spender: generalAdapter1,
|
|
40
|
+
amount,
|
|
41
|
+
chainId,
|
|
42
|
+
nonce,
|
|
43
|
+
supportDeployless,
|
|
44
|
+
}),
|
|
45
|
+
];
|
|
46
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Bundler3TokenSignatureRequirement, ERC20ApprovalAction, Transaction } from "../../../types/index.js";
|
|
3
|
+
interface GeneralAdapterPermit2Erc20Allowances {
|
|
4
|
+
readonly permit2: bigint;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Computes the Permit2 prerequisites for `GeneralAdapter1` to pull `amount` of `address`.
|
|
8
|
+
*
|
|
9
|
+
* Emits two ordered prerequisites:
|
|
10
|
+
*
|
|
11
|
+
* 1. A classic ERC-20 approval to the Permit2 contract (infinite, if not already in place).
|
|
12
|
+
* 2. A Permit2 `Requirement` signed against `GeneralAdapter1`.
|
|
13
|
+
*
|
|
14
|
+
* The Permit2 signature is always requested for the exact transfer amount when this path is
|
|
15
|
+
* selected. Permit2 allowance signatures overwrite the existing allowance, so relying on
|
|
16
|
+
* residual Permit2-managed allowance would make the final transfer path depend on stale state and
|
|
17
|
+
* could leave allowance behind after a partial spend.
|
|
18
|
+
*
|
|
19
|
+
* @param params.address - ERC-20 token address.
|
|
20
|
+
* @param params.chainId - The chain the bundle targets.
|
|
21
|
+
* @param params.permit2 - The Permit2 contract address for the chain.
|
|
22
|
+
* @param params.args.amount - Required token amount.
|
|
23
|
+
* @param params.erc20Allowances - Current ERC-20 allowances keyed by spender contract name.
|
|
24
|
+
* @param params.permit2Nonce - Current Permit2 nonce for the token owner / token / GeneralAdapter1 tuple.
|
|
25
|
+
* @returns Ordered list of approval transactions and/or `Requirement` objects to satisfy before
|
|
26
|
+
* bundling.
|
|
27
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} from the inner approval helper when its
|
|
28
|
+
* bookkeeping invariants break (should not happen with the values this function passes).
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
32
|
+
* import { getGeneralAdapterRequirementsPermit2 } from "@morpho-org/morpho-sdk";
|
|
33
|
+
*
|
|
34
|
+
* const { permit2 } = getChainAddresses(1);
|
|
35
|
+
* if (!permit2) throw new Error("Permit2 not configured for this chain");
|
|
36
|
+
* const requirements = getGeneralAdapterRequirementsPermit2({
|
|
37
|
+
* address: USDC,
|
|
38
|
+
* chainId: 1,
|
|
39
|
+
* permit2,
|
|
40
|
+
* args: { amount: 1_000_000n },
|
|
41
|
+
* erc20Allowances: { permit2: 0n },
|
|
42
|
+
* permit2Nonce: 0n,
|
|
43
|
+
* });
|
|
44
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction> | Bundler3TokenSignatureRequirement>)[]
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare const getGeneralAdapterRequirementsPermit2: (params: {
|
|
48
|
+
address: Address;
|
|
49
|
+
chainId: number;
|
|
50
|
+
permit2: Address;
|
|
51
|
+
args: {
|
|
52
|
+
amount: bigint;
|
|
53
|
+
};
|
|
54
|
+
erc20Allowances: GeneralAdapterPermit2Erc20Allowances;
|
|
55
|
+
permit2Nonce: bigint;
|
|
56
|
+
}) => Readonly<Transaction<ERC20ApprovalAction> | Bundler3TokenSignatureRequirement>[];
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { encodeErc20Permit2Approve } from "../encode/encodeErc20Permit2Approve.js";
|
|
3
|
+
import { getRequirementsApproval } from "../getRequirementsApproval.js";
|
|
4
|
+
/**
|
|
5
|
+
* Computes the Permit2 prerequisites for `GeneralAdapter1` to pull `amount` of `address`.
|
|
6
|
+
*
|
|
7
|
+
* Emits two ordered prerequisites:
|
|
8
|
+
*
|
|
9
|
+
* 1. A classic ERC-20 approval to the Permit2 contract (infinite, if not already in place).
|
|
10
|
+
* 2. A Permit2 `Requirement` signed against `GeneralAdapter1`.
|
|
11
|
+
*
|
|
12
|
+
* The Permit2 signature is always requested for the exact transfer amount when this path is
|
|
13
|
+
* selected. Permit2 allowance signatures overwrite the existing allowance, so relying on
|
|
14
|
+
* residual Permit2-managed allowance would make the final transfer path depend on stale state and
|
|
15
|
+
* could leave allowance behind after a partial spend.
|
|
16
|
+
*
|
|
17
|
+
* @param params.address - ERC-20 token address.
|
|
18
|
+
* @param params.chainId - The chain the bundle targets.
|
|
19
|
+
* @param params.permit2 - The Permit2 contract address for the chain.
|
|
20
|
+
* @param params.args.amount - Required token amount.
|
|
21
|
+
* @param params.erc20Allowances - Current ERC-20 allowances keyed by spender contract name.
|
|
22
|
+
* @param params.permit2Nonce - Current Permit2 nonce for the token owner / token / GeneralAdapter1 tuple.
|
|
23
|
+
* @returns Ordered list of approval transactions and/or `Requirement` objects to satisfy before
|
|
24
|
+
* bundling.
|
|
25
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} from the inner approval helper when its
|
|
26
|
+
* bookkeeping invariants break (should not happen with the values this function passes).
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
30
|
+
* import { getGeneralAdapterRequirementsPermit2 } from "@morpho-org/morpho-sdk";
|
|
31
|
+
*
|
|
32
|
+
* const { permit2 } = getChainAddresses(1);
|
|
33
|
+
* if (!permit2) throw new Error("Permit2 not configured for this chain");
|
|
34
|
+
* const requirements = getGeneralAdapterRequirementsPermit2({
|
|
35
|
+
* address: USDC,
|
|
36
|
+
* chainId: 1,
|
|
37
|
+
* permit2,
|
|
38
|
+
* args: { amount: 1_000_000n },
|
|
39
|
+
* erc20Allowances: { permit2: 0n },
|
|
40
|
+
* permit2Nonce: 0n,
|
|
41
|
+
* });
|
|
42
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction> | Bundler3TokenSignatureRequirement>)[]
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export const getGeneralAdapterRequirementsPermit2 = (params) => {
|
|
46
|
+
const { address, chainId, permit2, args: { amount }, erc20Allowances, permit2Nonce, } = params;
|
|
47
|
+
const requirements = [];
|
|
48
|
+
const approvalRequirements = getRequirementsApproval({
|
|
49
|
+
address,
|
|
50
|
+
chainId,
|
|
51
|
+
args: {
|
|
52
|
+
approvalAmount: MathLib.MAX_UINT_160, // Always approve infinite.
|
|
53
|
+
spendAmount: amount,
|
|
54
|
+
spender: permit2,
|
|
55
|
+
},
|
|
56
|
+
allowances: erc20Allowances.permit2,
|
|
57
|
+
});
|
|
58
|
+
requirements.push(...approvalRequirements);
|
|
59
|
+
// Existing Permit2-managed allowance is intentionally not checked for sufficiency. Permit2
|
|
60
|
+
// overwrites the allowance with this signed amount, and the bundle spends exactly `amount`,
|
|
61
|
+
// leaving no residual Permit2 allowance after inclusion.
|
|
62
|
+
requirements.push(encodeErc20Permit2Approve({
|
|
63
|
+
token: address,
|
|
64
|
+
amount,
|
|
65
|
+
chainId,
|
|
66
|
+
nonce: permit2Nonce,
|
|
67
|
+
expiration: MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
68
|
+
}));
|
|
69
|
+
return requirements;
|
|
70
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
|
|
3
3
|
/**
|
|
4
|
-
* Computes classic ERC-20 approval transactions for
|
|
4
|
+
* Computes classic ERC-20 approval transactions for GeneralAdapter1 or Permit2, given the existing allowance.
|
|
5
5
|
*
|
|
6
6
|
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
7
7
|
* `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
|
|
@@ -13,11 +13,12 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
|
|
|
13
13
|
* @param params.args.spendAmount - The amount the bundle will actually pull.
|
|
14
14
|
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
15
15
|
* may be `MAX_UINT_160` for Permit2 prerequisites).
|
|
16
|
-
* @param params.args.spender - Address that will be granted the approval.
|
|
16
|
+
* @param params.args.spender - Address that will be granted the approval. Must be GeneralAdapter1 or Permit2.
|
|
17
17
|
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
18
18
|
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
19
19
|
* followed by the new approval. Empty when no approval is needed.
|
|
20
20
|
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
|
|
21
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
|
|
21
22
|
* @example
|
|
22
23
|
* ```ts
|
|
23
24
|
* import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
@@ -2,7 +2,7 @@ import { APPROVE_ONLY_ONCE_TOKENS } from "../../helpers/constant.js";
|
|
|
2
2
|
import { ApprovalAmountLessThanSpendAmountError, } from "../../types/index.js";
|
|
3
3
|
import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
|
|
4
4
|
/**
|
|
5
|
-
* Computes classic ERC-20 approval transactions for
|
|
5
|
+
* Computes classic ERC-20 approval transactions for GeneralAdapter1 or Permit2, given the existing allowance.
|
|
6
6
|
*
|
|
7
7
|
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
8
8
|
* `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
|
|
@@ -14,11 +14,12 @@ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
|
|
|
14
14
|
* @param params.args.spendAmount - The amount the bundle will actually pull.
|
|
15
15
|
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
16
16
|
* may be `MAX_UINT_160` for Permit2 prerequisites).
|
|
17
|
-
* @param params.args.spender - Address that will be granted the approval.
|
|
17
|
+
* @param params.args.spender - Address that will be granted the approval. Must be GeneralAdapter1 or Permit2.
|
|
18
18
|
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
19
19
|
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
20
20
|
* followed by the new approval. Empty when no approval is needed.
|
|
21
21
|
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
|
|
22
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
|
|
22
23
|
* @example
|
|
23
24
|
* ```ts
|
|
24
25
|
* import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
+
export * from "./blue/index.js";
|
|
1
2
|
export * from "./encode/index.js";
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./getRequirements.js";
|
|
4
|
-
export * from "./getRequirementsAction.js";
|
|
3
|
+
export * from "./generalAdapter/index.js";
|
|
5
4
|
export * from "./getRequirementsApproval.js";
|
|
6
|
-
export * from "./getRequirementsPermit.js";
|
|
7
|
-
export * from "./getRequirementsPermit2.js";
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
+
export * from "./blue/index.js";
|
|
1
2
|
export * from "./encode/index.js";
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./getRequirements.js";
|
|
4
|
-
export * from "./getRequirementsAction.js";
|
|
3
|
+
export * from "./generalAdapter/index.js";
|
|
5
4
|
export * from "./getRequirementsApproval.js";
|
|
6
|
-
export * from "./getRequirementsPermit.js";
|
|
7
|
-
export * from "./getRequirementsPermit2.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Action } from "../../bundler/index.js";
|
|
2
|
+
import { type AuthorizationRequirementSignature } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Encodes the bundler action that consumes a signed Morpho authorization, granting
|
|
5
|
+
* `authorized` (GeneralAdapter1) operator rights on behalf of the signer via
|
|
6
|
+
* `setAuthorizationWithSig` — replacing a standalone `setAuthorization` transaction.
|
|
7
|
+
*
|
|
8
|
+
* The signature's `authorized` is pinned to the chain's `GeneralAdapter1`: an authorization
|
|
9
|
+
* targeting any other account is rejected so the bundle can never hand operator rights over the
|
|
10
|
+
* user's Morpho position to an unintended address. The action is emitted with `skipRevert: false`
|
|
11
|
+
* so a rejected or stale authorization fails the whole bundle rather than letting a later
|
|
12
|
+
* on-behalf Morpho call revert opaquely.
|
|
13
|
+
*
|
|
14
|
+
* @param chainId - Chain whose `GeneralAdapter1` the signature must authorize.
|
|
15
|
+
* @param signature - The signed authorization produced by `Requirement.sign()`.
|
|
16
|
+
* @returns A single `morphoSetAuthorizationWithSig` bundler `Action`.
|
|
17
|
+
* @throws {BundlerErrors.UnexpectedSignature} when `signature.args.authorized` is not the chain's
|
|
18
|
+
* `GeneralAdapter1`.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { getBlueAuthorizationAction } from "@morpho-org/morpho-sdk";
|
|
22
|
+
*
|
|
23
|
+
* // `requirement` comes from `getBlueAuthorizationRequirement` with `supportSignature: true`.
|
|
24
|
+
* const signed = await requirement.sign(walletClient, borrower);
|
|
25
|
+
* const action = getBlueAuthorizationAction(1, signed);
|
|
26
|
+
* // action satisfies { type: "morphoSetAuthorizationWithSig"; args: [...] }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare const getBlueAuthorizationAction: (chainId: number, signature: AuthorizationRequirementSignature) => Action;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { isAddressEqual } from "viem";
|
|
3
|
+
import { BundlerErrors, } from "../../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Encodes the bundler action that consumes a signed Morpho authorization, granting
|
|
6
|
+
* `authorized` (GeneralAdapter1) operator rights on behalf of the signer via
|
|
7
|
+
* `setAuthorizationWithSig` — replacing a standalone `setAuthorization` transaction.
|
|
8
|
+
*
|
|
9
|
+
* The signature's `authorized` is pinned to the chain's `GeneralAdapter1`: an authorization
|
|
10
|
+
* targeting any other account is rejected so the bundle can never hand operator rights over the
|
|
11
|
+
* user's Morpho position to an unintended address. The action is emitted with `skipRevert: false`
|
|
12
|
+
* so a rejected or stale authorization fails the whole bundle rather than letting a later
|
|
13
|
+
* on-behalf Morpho call revert opaquely.
|
|
14
|
+
*
|
|
15
|
+
* @param chainId - Chain whose `GeneralAdapter1` the signature must authorize.
|
|
16
|
+
* @param signature - The signed authorization produced by `Requirement.sign()`.
|
|
17
|
+
* @returns A single `morphoSetAuthorizationWithSig` bundler `Action`.
|
|
18
|
+
* @throws {BundlerErrors.UnexpectedSignature} when `signature.args.authorized` is not the chain's
|
|
19
|
+
* `GeneralAdapter1`.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { getBlueAuthorizationAction } from "@morpho-org/morpho-sdk";
|
|
23
|
+
*
|
|
24
|
+
* // `requirement` comes from `getBlueAuthorizationRequirement` with `supportSignature: true`.
|
|
25
|
+
* const signed = await requirement.sign(walletClient, borrower);
|
|
26
|
+
* const action = getBlueAuthorizationAction(1, signed);
|
|
27
|
+
* // action satisfies { type: "morphoSetAuthorizationWithSig"; args: [...] }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const getBlueAuthorizationAction = (chainId, signature) => {
|
|
31
|
+
const { owner, authorized, isAuthorized, nonce, deadline } = signature.args;
|
|
32
|
+
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
33
|
+
if (!isAddressEqual(authorized, generalAdapter1)) {
|
|
34
|
+
throw new BundlerErrors.UnexpectedSignature(authorized);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
type: "morphoSetAuthorizationWithSig",
|
|
38
|
+
args: [
|
|
39
|
+
{ authorizer: owner, authorized, isAuthorized, nonce, deadline },
|
|
40
|
+
signature.args.signature,
|
|
41
|
+
false /* skipRevert */,
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
};
|