@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.
- 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 +0 -44
- package/lib/cjs/entities/reallocationData.js +3 -50
- 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 +0 -44
- package/lib/esm/entities/reallocationData.js +2 -49
- 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,5 +1,5 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type
|
|
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
|
|
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
|
|
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";
|
|
@@ -5,18 +5,20 @@ const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
|
5
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
6
|
const viem_1 = require("viem");
|
|
7
7
|
const constant_js_1 = require("../../../helpers/constant.js");
|
|
8
|
+
const index_js_1 = require("../../../types/index.js");
|
|
8
9
|
/**
|
|
9
|
-
* Encodes a deep-frozen ERC-20
|
|
10
|
+
* Encodes a deep-frozen ERC-20 approval transaction for GeneralAdapter1 or Permit2.
|
|
10
11
|
*
|
|
11
12
|
* Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
|
|
12
|
-
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link
|
|
13
|
+
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
|
|
13
14
|
*
|
|
14
15
|
* @param params - Encoding parameters.
|
|
15
16
|
* @param params.token - ERC-20 token address to approve.
|
|
16
|
-
* @param params.spender - Address granted the allowance.
|
|
17
|
+
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1 or Permit2.
|
|
17
18
|
* @param params.amount - Allowance amount before per-token cap.
|
|
18
|
-
* @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
|
|
19
|
+
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
19
20
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
21
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
|
|
20
22
|
* @example
|
|
21
23
|
* ```ts
|
|
22
24
|
* import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
|
|
@@ -32,6 +34,16 @@ const constant_js_1 = require("../../../helpers/constant.js");
|
|
|
32
34
|
*/
|
|
33
35
|
const encodeErc20Approval = (params) => {
|
|
34
36
|
const { token, spender, amount, chainId } = params;
|
|
37
|
+
const { permit2, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
38
|
+
if (!(0, viem_1.isAddressEqual)(spender, generalAdapter1) &&
|
|
39
|
+
(permit2 == null || !(0, viem_1.isAddressEqual)(spender, permit2))) {
|
|
40
|
+
throw new index_js_1.UnsupportedErc20ApprovalSpenderError({
|
|
41
|
+
spender,
|
|
42
|
+
chainId,
|
|
43
|
+
generalAdapter1,
|
|
44
|
+
permit2,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
35
47
|
const amountValue = blue_sdk_1.MathLib.min(amount, constant_js_1.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
|
|
36
48
|
return (0, morpho_ts_1.deepFreeze)({
|
|
37
49
|
to: token,
|
|
@@ -43,7 +55,7 @@ const encodeErc20Approval = (params) => {
|
|
|
43
55
|
value: 0n,
|
|
44
56
|
action: {
|
|
45
57
|
type: "erc20Approval",
|
|
46
|
-
args: { spender
|
|
58
|
+
args: { spender, amount: amountValue },
|
|
47
59
|
},
|
|
48
60
|
});
|
|
49
61
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
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
|
|
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 -
|
|
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, //
|
|
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,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.encodeErc20Permit = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
4
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
5
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
7
|
const viem_1 = require("viem");
|
|
@@ -8,8 +9,8 @@ const actions_1 = require("viem/actions");
|
|
|
8
9
|
const validate_js_1 = require("../../../helpers/validate.js");
|
|
9
10
|
const index_js_1 = require("../../../types/index.js");
|
|
10
11
|
/**
|
|
11
|
-
* Builds an EIP-2612 permit `Requirement` that, once signed, lets
|
|
12
|
-
* `token`.
|
|
12
|
+
* Builds an EIP-2612 permit `Requirement` that, once signed, lets a supported SDK spender pull
|
|
13
|
+
* `amount` of `token`.
|
|
13
14
|
*
|
|
14
15
|
* Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
|
|
15
16
|
* signature, verifies it against the connected account, and returns a `RequirementSignature`
|
|
@@ -18,13 +19,15 @@ const index_js_1 = require("../../../types/index.js");
|
|
|
18
19
|
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
19
20
|
* @param params - Permit encoding parameters.
|
|
20
21
|
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
21
|
-
* @param params.spender -
|
|
22
|
+
* @param params.spender - Permit spender. Must be GeneralAdapter1 or MidnightBundles for the chain.
|
|
22
23
|
* @param params.amount - Permit allowance amount.
|
|
23
24
|
* @param params.chainId - Target chain id.
|
|
24
25
|
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
25
26
|
* @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
|
|
26
27
|
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
27
28
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
29
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or
|
|
30
|
+
* MidnightBundles for `chainId`.
|
|
28
31
|
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
29
32
|
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
30
33
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
@@ -36,7 +39,7 @@ const index_js_1 = require("../../../types/index.js");
|
|
|
36
39
|
*
|
|
37
40
|
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
38
41
|
* const requirement = await encodeErc20Permit(client, {
|
|
39
|
-
* token: USDC, //
|
|
42
|
+
* token: USDC, // Must implement standard ERC-2612. DAI is routed through Permit2 by requirement helpers.
|
|
40
43
|
* spender: generalAdapter1,
|
|
41
44
|
* amount: 1_000_000n,
|
|
42
45
|
* chainId: 1,
|
|
@@ -50,6 +53,17 @@ const encodeErc20Permit = async (viemClient, params) => {
|
|
|
50
53
|
if (viemClient.chain?.id !== chainId) {
|
|
51
54
|
throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
52
55
|
}
|
|
56
|
+
const { midnightBundles, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
57
|
+
if (!(0, viem_1.isAddressEqual)(spender, generalAdapter1) &&
|
|
58
|
+
(midnightBundles == null || !(0, viem_1.isAddressEqual)(spender, midnightBundles))) {
|
|
59
|
+
throw new index_js_1.UnsupportedErc20ApprovalSpenderError({
|
|
60
|
+
spender,
|
|
61
|
+
chainId,
|
|
62
|
+
generalAdapter1,
|
|
63
|
+
midnightBundles,
|
|
64
|
+
supportedSpenders: [generalAdapter1, midnightBundles],
|
|
65
|
+
});
|
|
66
|
+
}
|
|
53
67
|
const now = morpho_ts_1.Time.timestamp();
|
|
54
68
|
const deadline = now + morpho_ts_1.Time.s.from.h(2n);
|
|
55
69
|
const tokenData = await (0, blue_sdk_viem_1.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
|
|
4
|
-
interface
|
|
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
|
|
9
|
+
expiration?: bigint;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Builds a Permit2 `Requirement` that, once signed, lets
|
|
13
|
-
*
|
|
12
|
+
* Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
|
|
13
|
+
* via the Permit2 contract.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
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
|
|
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 {
|
|
29
|
+
* import { encodeErc20Permit2Approve } from "@morpho-org/morpho-sdk";
|
|
32
30
|
*
|
|
33
|
-
* const requirement =
|
|
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
|
|
41
|
+
export declare const encodeErc20Permit2Approve: (params: EncodeErc20Permit2ApproveParams) => Requirement<PermitRequirementSignature>;
|
|
44
42
|
export {};
|
package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.encodeErc20Permit2Approve = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
@@ -9,17 +9,15 @@ const actions_1 = require("viem/actions");
|
|
|
9
9
|
const validate_js_1 = require("../../../helpers/validate.js");
|
|
10
10
|
const index_js_1 = require("../../../types/index.js");
|
|
11
11
|
/**
|
|
12
|
-
* Builds a Permit2 `Requirement` that, once signed, lets
|
|
13
|
-
*
|
|
12
|
+
* Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
|
|
13
|
+
* via the Permit2 contract.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
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
|
|
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 @@ const index_js_1 = require("../../../types/index.js");
|
|
|
28
26
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
29
27
|
* @example
|
|
30
28
|
* ```ts
|
|
31
|
-
* import {
|
|
29
|
+
* import { encodeErc20Permit2Approve } from "@morpho-org/morpho-sdk";
|
|
32
30
|
*
|
|
33
|
-
* const requirement =
|
|
31
|
+
* const requirement = encodeErc20Permit2Approve({
|
|
34
32
|
* token: USDC,
|
|
35
33
|
* amount: 1_000_000n,
|
|
36
34
|
* chainId: 1,
|
|
@@ -40,11 +38,11 @@ const index_js_1 = require("../../../types/index.js");
|
|
|
40
38
|
* // requirement satisfies Requirement
|
|
41
39
|
* ```
|
|
42
40
|
*/
|
|
43
|
-
const
|
|
41
|
+
const encodeErc20Permit2Approve = (params) => {
|
|
44
42
|
const { token, amount, chainId, nonce, expiration = blue_sdk_1.MathLib.MAX_UINT_48, } = params;
|
|
43
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
45
44
|
const now = morpho_ts_1.Time.timestamp();
|
|
46
45
|
const deadline = now + morpho_ts_1.Time.s.from.h(2n);
|
|
47
|
-
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
48
46
|
const action = {
|
|
49
47
|
type: "permit2",
|
|
50
48
|
args: {
|
|
@@ -60,8 +58,6 @@ const encodeErc20Permit2 = (params) => {
|
|
|
60
58
|
const account = client.account;
|
|
61
59
|
(0, validate_js_1.validateUserAddress)(account?.address, userAddress);
|
|
62
60
|
const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
|
|
63
|
-
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
64
|
-
// the signature can be used independently.
|
|
65
61
|
spender: generalAdapter1,
|
|
66
62
|
allowance: amount,
|
|
67
63
|
erc20: token,
|
|
@@ -96,4 +92,4 @@ const encodeErc20Permit2 = (params) => {
|
|
|
96
92
|
},
|
|
97
93
|
};
|
|
98
94
|
};
|
|
99
|
-
exports.
|
|
95
|
+
exports.encodeErc20Permit2Approve = encodeErc20Permit2Approve;
|
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./encodeBlueSignatureAuthorization.js"), exports);
|
|
17
18
|
__exportStar(require("./encodeErc20Approval.js"), exports);
|
|
18
19
|
__exportStar(require("./encodeErc20Permit.js"), exports);
|
|
19
|
-
__exportStar(require("./
|
|
20
|
+
__exportStar(require("./encodeErc20Permit2Approve.js"), exports);
|
|
@@ -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,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGeneralAdapterRequirements = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const actions_1 = require("viem/actions");
|
|
9
|
+
const index_js_1 = require("../../../types/index.js");
|
|
10
|
+
const getRequirementsApproval_js_1 = require("../getRequirementsApproval.js");
|
|
11
|
+
const getGeneralAdapterRequirementsPermit_js_1 = require("./getGeneralAdapterRequirementsPermit.js");
|
|
12
|
+
const getGeneralAdapterRequirementsPermit2_js_1 = require("./getGeneralAdapterRequirementsPermit2.js");
|
|
13
|
+
/**
|
|
14
|
+
* Resolves the approval requirements an integrator must satisfy before a bundled action pulls
|
|
15
|
+
* tokens through `GeneralAdapter1`.
|
|
16
|
+
*
|
|
17
|
+
* Reads the minimum token allowance / nonce state needed from the chain, then picks one of three
|
|
18
|
+
* flows:
|
|
19
|
+
*
|
|
20
|
+
* 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
|
|
21
|
+
* direct allowance is already large enough).
|
|
22
|
+
* 2. **`supportSignature: true` + EIP-2612 nonce detected + `useSimplePermit`** — single permit
|
|
23
|
+
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
24
|
+
* permit signature is incompatible) and falls through to Permit2 even with
|
|
25
|
+
* `useSimplePermit: true`.
|
|
26
|
+
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
27
|
+
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
28
|
+
*
|
|
29
|
+
* The simple-permit compatibility check is intentionally shallow: when requested, it probes
|
|
30
|
+
* whether the token exposes a readable ERC-2612 `nonces(owner)` value.
|
|
31
|
+
* Leaving `useSimplePermit` unset, or passing `false`, is the caller escape hatch for tokens
|
|
32
|
+
* that expose `nonces` but are still incompatible with the SDK's ERC-2612 encoder. This opt-out
|
|
33
|
+
* has proven useful in the past, but the SDK does not encode a token-specific example here.
|
|
34
|
+
* DAI is handled as a built-in version of that incompatibility: it exposes `nonces(owner)` but is
|
|
35
|
+
* always routed to Permit2/classic approval instead of DAI-specific permit signing.
|
|
36
|
+
*
|
|
37
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
38
|
+
* @param params - Requirement resolution parameters.
|
|
39
|
+
* @param params.address - ERC-20 token address.
|
|
40
|
+
* @param params.chainId - Chain id; must match `viemClient.chain.id`.
|
|
41
|
+
* @param params.args.amount - Required token amount. Returns `[]` when zero.
|
|
42
|
+
* @param params.args.from - Account that will grant the approval.
|
|
43
|
+
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
44
|
+
* permit / permit2 vs. classic approval.
|
|
45
|
+
* @param params.supportDeployless - Whether ERC-2612 permit metadata reads may use deployless multicall.
|
|
46
|
+
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
47
|
+
* the nonce probe detects support. Leave unset or pass `false` to force the Permit2 fallback
|
|
48
|
+
* for tokens known to be incompatible despite passing that probe.
|
|
49
|
+
* @returns Promise resolving to an array of either deep-frozen approval transactions or
|
|
50
|
+
* `Requirement` objects (signature requirements with a `sign()` method). Empty when `amount`
|
|
51
|
+
* is zero or when the classic-approval path can reuse sufficient direct allowance.
|
|
52
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
|
|
53
|
+
* error is reachable through this entry point: the values passed into
|
|
54
|
+
* `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
|
|
55
|
+
* `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
|
|
56
|
+
* `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { createPublicClient, http } from "viem";
|
|
60
|
+
* import { mainnet } from "viem/chains";
|
|
61
|
+
* import { getGeneralAdapterRequirements } from "@morpho-org/morpho-sdk";
|
|
62
|
+
*
|
|
63
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
64
|
+
* const requirements = await getGeneralAdapterRequirements(client, {
|
|
65
|
+
* address: USDC,
|
|
66
|
+
* chainId: 1,
|
|
67
|
+
* supportSignature: true,
|
|
68
|
+
* args: { amount: 1_000_000n, from: user },
|
|
69
|
+
* });
|
|
70
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Bundler3TokenSignatureRequirement)[]
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const getGeneralAdapterRequirements = async (viemClient, params) => {
|
|
74
|
+
const { address, chainId, supportSignature, args: { amount, from }, } = params;
|
|
75
|
+
if (viemClient.chain?.id !== chainId) {
|
|
76
|
+
throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
77
|
+
}
|
|
78
|
+
if (amount === 0n) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
const { permit2, dai, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
82
|
+
if (supportSignature) {
|
|
83
|
+
const { useSimplePermit } = params;
|
|
84
|
+
const isDai = (0, morpho_ts_1.isDefined)(dai) && (0, viem_1.isAddressEqual)(address, dai);
|
|
85
|
+
if (useSimplePermit && !isDai) {
|
|
86
|
+
const erc2612Nonce = await (0, actions_1.readContract)(viemClient, {
|
|
87
|
+
abi: blue_sdk_viem_1.erc2612Abi,
|
|
88
|
+
address,
|
|
89
|
+
functionName: "nonces",
|
|
90
|
+
args: [from],
|
|
91
|
+
}).catch(() => undefined);
|
|
92
|
+
if ((0, morpho_ts_1.isDefined)(erc2612Nonce)) {
|
|
93
|
+
// Do not skip this exact permit because a previous ERC-20 allowance is already enough.
|
|
94
|
+
// ERC-2612 overwrites the allowance, and the bundle spends exactly `amount`, so signing
|
|
95
|
+
// the pulled amount leaves no residual direct allowance after inclusion.
|
|
96
|
+
return (0, getGeneralAdapterRequirementsPermit_js_1.getGeneralAdapterRequirementsPermit)(viemClient, {
|
|
97
|
+
token: address,
|
|
98
|
+
chainId,
|
|
99
|
+
args: { amount },
|
|
100
|
+
nonce: erc2612Nonce,
|
|
101
|
+
supportDeployless: params.supportDeployless,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (permit2) {
|
|
106
|
+
// Do not check the existing Permit2-managed amount/expiration. Permit2 allowance signatures
|
|
107
|
+
// overwrite the tuple, and the bundle spends exactly `amount`, so a fresh exact signature
|
|
108
|
+
// leaves no residual Permit2 allowance. The tuple is still read because Permit2 requires
|
|
109
|
+
// its nonce in the signed allowance payload.
|
|
110
|
+
const [permit2Erc20Allowance, [, , permit2Nonce]] = await Promise.all([
|
|
111
|
+
(0, actions_1.readContract)(viemClient, {
|
|
112
|
+
abi: viem_1.erc20Abi,
|
|
113
|
+
address,
|
|
114
|
+
functionName: "allowance",
|
|
115
|
+
args: [from, permit2],
|
|
116
|
+
}),
|
|
117
|
+
(0, actions_1.readContract)(viemClient, {
|
|
118
|
+
abi: blue_sdk_viem_1.permit2Abi,
|
|
119
|
+
address: permit2,
|
|
120
|
+
functionName: "allowance",
|
|
121
|
+
args: [from, address, generalAdapter1],
|
|
122
|
+
}),
|
|
123
|
+
]);
|
|
124
|
+
return (0, getGeneralAdapterRequirementsPermit2_js_1.getGeneralAdapterRequirementsPermit2)({
|
|
125
|
+
address,
|
|
126
|
+
chainId,
|
|
127
|
+
permit2,
|
|
128
|
+
args: { amount },
|
|
129
|
+
erc20Allowances: { permit2: permit2Erc20Allowance },
|
|
130
|
+
permit2Nonce: BigInt(permit2Nonce),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const generalAdapterAllowance = await (0, actions_1.readContract)(viemClient, {
|
|
135
|
+
abi: viem_1.erc20Abi,
|
|
136
|
+
address,
|
|
137
|
+
functionName: "allowance",
|
|
138
|
+
args: [from, generalAdapter1],
|
|
139
|
+
});
|
|
140
|
+
return (0, getRequirementsApproval_js_1.getRequirementsApproval)({
|
|
141
|
+
address,
|
|
142
|
+
chainId,
|
|
143
|
+
args: {
|
|
144
|
+
spendAmount: amount,
|
|
145
|
+
approvalAmount: amount,
|
|
146
|
+
spender: generalAdapter1,
|
|
147
|
+
},
|
|
148
|
+
allowances: generalAdapterAllowance,
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
exports.getGeneralAdapterRequirements = getGeneralAdapterRequirements;
|
|
@@ -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>[]>;
|