@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,67 +0,0 @@
|
|
|
1
|
-
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { blueAbi } from "@morpho-org/blue-sdk-viem";
|
|
3
|
-
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { encodeFunctionData, publicActions } from "viem";
|
|
5
|
-
import { ChainIdMismatchError, } from "../../types/index.js";
|
|
6
|
-
/**
|
|
7
|
-
* Resolves whether `GeneralAdapter1` needs Morpho authorization for the given user, and returns
|
|
8
|
-
* the `setAuthorization(generalAdapter1, true)` transaction when it does.
|
|
9
|
-
*
|
|
10
|
-
* Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
|
|
11
|
-
* any bundled Blue path that operates on behalf of the user (`borrow`,
|
|
12
|
-
* `supplyCollateralBorrow`, `repayWithdrawCollateral`).
|
|
13
|
-
*
|
|
14
|
-
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
15
|
-
* @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
|
|
16
|
-
* @param params.userAddress - The user that must authorize `GeneralAdapter1`.
|
|
17
|
-
* @returns A deep-frozen `Transaction<MorphoAuthorizationAction>`, or `null` when authorization
|
|
18
|
-
* is already in place.
|
|
19
|
-
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* import { createPublicClient, http } from "viem";
|
|
23
|
-
* import { mainnet } from "viem/chains";
|
|
24
|
-
* import { getMorphoAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
25
|
-
*
|
|
26
|
-
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
27
|
-
* const tx = await getMorphoAuthorizationRequirement({
|
|
28
|
-
* viemClient: client,
|
|
29
|
-
* chainId: 1,
|
|
30
|
-
* userAddress: borrower,
|
|
31
|
-
* });
|
|
32
|
-
* // tx is null when already authorized, otherwise satisfies Readonly<Transaction<MorphoAuthorizationAction>>
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export const getMorphoAuthorizationRequirement = async (params) => {
|
|
36
|
-
const { viemClient, chainId, userAddress } = params;
|
|
37
|
-
if (viemClient.chain?.id !== chainId) {
|
|
38
|
-
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
39
|
-
}
|
|
40
|
-
const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
41
|
-
const pc = viemClient.extend(publicActions);
|
|
42
|
-
const isAuthorized = await pc.readContract({
|
|
43
|
-
address: morpho,
|
|
44
|
-
abi: blueAbi,
|
|
45
|
-
functionName: "isAuthorized",
|
|
46
|
-
args: [userAddress, generalAdapter1],
|
|
47
|
-
});
|
|
48
|
-
if (isAuthorized) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
return deepFreeze({
|
|
52
|
-
to: morpho,
|
|
53
|
-
data: encodeFunctionData({
|
|
54
|
-
abi: blueAbi,
|
|
55
|
-
functionName: "setAuthorization",
|
|
56
|
-
args: [generalAdapter1, true],
|
|
57
|
-
}),
|
|
58
|
-
value: 0n,
|
|
59
|
-
action: {
|
|
60
|
-
type: "morphoAuthorization",
|
|
61
|
-
args: {
|
|
62
|
-
authorized: generalAdapter1,
|
|
63
|
-
isAuthorized: true,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { fetchHolding } from "@morpho-org/blue-sdk-viem";
|
|
3
|
-
import { isDefined } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { ChainIdMismatchError, } from "../../types/index.js";
|
|
5
|
-
import { getRequirementsApproval } from "./getRequirementsApproval.js";
|
|
6
|
-
import { getRequirementsPermit } from "./getRequirementsPermit.js";
|
|
7
|
-
import { getRequirementsPermit2 } from "./getRequirementsPermit2.js";
|
|
8
|
-
/**
|
|
9
|
-
* Resolves the approval requirements an integrator must satisfy before a Morpho bundle pulls
|
|
10
|
-
* tokens through `GeneralAdapter1`.
|
|
11
|
-
*
|
|
12
|
-
* Reads the user's current `holding` (allowances + nonces) from the chain, then picks one of
|
|
13
|
-
* three flows:
|
|
14
|
-
*
|
|
15
|
-
* 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
|
|
16
|
-
* allowance is already large enough).
|
|
17
|
-
* 2. **`supportSignature: true` + EIP-2612 supported + `useSimplePermit`** — single permit
|
|
18
|
-
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
19
|
-
* permit signature is incompatible) and falls through to Permit2 even with
|
|
20
|
-
* `useSimplePermit: true`.
|
|
21
|
-
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
22
|
-
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
23
|
-
*
|
|
24
|
-
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
25
|
-
* @param params - Requirement resolution parameters.
|
|
26
|
-
* @param params.address - ERC-20 token address.
|
|
27
|
-
* @param params.chainId - Chain id; must match `viemClient.chain.id`.
|
|
28
|
-
* @param params.args.amount - Required token amount. Returns `[]` when zero.
|
|
29
|
-
* @param params.args.from - Account that will grant the approval.
|
|
30
|
-
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
31
|
-
* permit / permit2 vs. classic approval.
|
|
32
|
-
* @param params.supportDeployless - Whether to fetch holdings via deployless multicall.
|
|
33
|
-
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
34
|
-
* the token supports it.
|
|
35
|
-
* @returns Promise resolving to an array of either deep-frozen approval transactions or
|
|
36
|
-
* `Requirement` objects (signature requirements with a `sign()` method). Empty when the
|
|
37
|
-
* existing allowance already covers `amount`.
|
|
38
|
-
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
|
|
39
|
-
* error is reachable through this entry point: the values passed into
|
|
40
|
-
* `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
|
|
41
|
-
* `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
|
|
42
|
-
* `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
|
|
43
|
-
* @example
|
|
44
|
-
* ```ts
|
|
45
|
-
* import { createPublicClient, http } from "viem";
|
|
46
|
-
* import { mainnet } from "viem/chains";
|
|
47
|
-
* import { getRequirements } from "@morpho-org/morpho-sdk";
|
|
48
|
-
*
|
|
49
|
-
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
50
|
-
* const requirements = await getRequirements(client, {
|
|
51
|
-
* address: USDC,
|
|
52
|
-
* chainId: 1,
|
|
53
|
-
* supportSignature: true,
|
|
54
|
-
* args: { amount: 1_000_000n, from: user },
|
|
55
|
-
* });
|
|
56
|
-
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export const getRequirements = async (viemClient, params) => {
|
|
60
|
-
const { address, chainId, supportSignature, args: { amount, from }, } = params;
|
|
61
|
-
if (viemClient.chain?.id !== chainId) {
|
|
62
|
-
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
63
|
-
}
|
|
64
|
-
if (amount === 0n) {
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
const { permit2, dai, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
68
|
-
const { erc20Allowances, erc2612Nonce, permit2BundlerAllowance } = await fetchHolding(from, address, viemClient, {
|
|
69
|
-
deployless: params.supportDeployless,
|
|
70
|
-
});
|
|
71
|
-
if (supportSignature) {
|
|
72
|
-
const { useSimplePermit } = params;
|
|
73
|
-
const supportSimplePermit = isDefined(erc2612Nonce) && address !== dai;
|
|
74
|
-
if (supportSimplePermit && useSimplePermit) {
|
|
75
|
-
return await getRequirementsPermit(viemClient, {
|
|
76
|
-
token: address,
|
|
77
|
-
chainId,
|
|
78
|
-
args: { amount },
|
|
79
|
-
allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
|
|
80
|
-
nonce: erc2612Nonce,
|
|
81
|
-
supportDeployless: params.supportDeployless,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (permit2) {
|
|
85
|
-
return getRequirementsPermit2({
|
|
86
|
-
address,
|
|
87
|
-
chainId,
|
|
88
|
-
permit2,
|
|
89
|
-
args: { amount },
|
|
90
|
-
allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
|
|
91
|
-
allowancesPermit2: erc20Allowances.permit2,
|
|
92
|
-
allowanceGeneralAdapterPermit2: permit2BundlerAllowance.amount,
|
|
93
|
-
allowanceGeneralAdapterExpiration: permit2BundlerAllowance.expiration,
|
|
94
|
-
nonce: permit2BundlerAllowance.nonce,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return getRequirementsApproval({
|
|
99
|
-
address,
|
|
100
|
-
chainId,
|
|
101
|
-
args: {
|
|
102
|
-
spendAmount: amount,
|
|
103
|
-
approvalAmount: amount,
|
|
104
|
-
spender: generalAdapter1,
|
|
105
|
-
},
|
|
106
|
-
allowances: erc20Allowances["bundler3.generalAdapter1"],
|
|
107
|
-
});
|
|
108
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
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
|
-
* Returns an empty array when the existing allowance for `GeneralAdapter1` already covers
|
|
8
|
-
* `amount`.
|
|
9
|
-
*
|
|
10
|
-
* @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
|
|
11
|
-
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
12
|
-
* @param params.chainId - The chain the bundle targets.
|
|
13
|
-
* @param params.args.amount - Required token amount.
|
|
14
|
-
* @param params.allowancesGeneralAdapter - The user's current allowance of `token` for
|
|
15
|
-
* `GeneralAdapter1`.
|
|
16
|
-
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
17
|
-
* @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
|
|
18
|
-
* @returns A single-element array containing the `Requirement` to sign, or an empty array when
|
|
19
|
-
* the existing allowance already covers `amount`.
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* import { createWalletClient, http } from "viem";
|
|
23
|
-
* import { mainnet } from "viem/chains";
|
|
24
|
-
* import { getRequirementsPermit } from "@morpho-org/morpho-sdk";
|
|
25
|
-
*
|
|
26
|
-
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
27
|
-
* const reqs = await getRequirementsPermit(client, {
|
|
28
|
-
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by getRequirements
|
|
29
|
-
* chainId: 1,
|
|
30
|
-
* args: { amount: 1_000_000n },
|
|
31
|
-
* allowancesGeneralAdapter: 0n,
|
|
32
|
-
* nonce: 0n,
|
|
33
|
-
* });
|
|
34
|
-
* // reqs satisfies Requirement[]
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export const getRequirementsPermit = async (viemClient, params) => {
|
|
38
|
-
const { token, chainId, args: { amount }, allowancesGeneralAdapter, nonce, supportDeployless, } = params;
|
|
39
|
-
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
40
|
-
if (allowancesGeneralAdapter < amount) {
|
|
41
|
-
return [
|
|
42
|
-
await encodeErc20Permit(viemClient, {
|
|
43
|
-
token,
|
|
44
|
-
spender: generalAdapter1,
|
|
45
|
-
amount,
|
|
46
|
-
chainId,
|
|
47
|
-
nonce,
|
|
48
|
-
supportDeployless,
|
|
49
|
-
}),
|
|
50
|
-
];
|
|
51
|
-
}
|
|
52
|
-
return [];
|
|
53
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type { ERC20ApprovalAction, Requirement, Transaction } from "../../types/index.js";
|
|
3
|
-
/**
|
|
4
|
-
* Computes the Permit2 prerequisites for `GeneralAdapter1` to pull `amount` of `address`.
|
|
5
|
-
*
|
|
6
|
-
* Returns an empty array when the direct adapter allowance already covers `amount`. Otherwise
|
|
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` (skipped when the existing
|
|
11
|
-
* Permit2-managed allowance covers `amount` and is not about to expire within four hours).
|
|
12
|
-
*
|
|
13
|
-
* @param params.address - ERC-20 token address.
|
|
14
|
-
* @param params.chainId - The chain the bundle targets.
|
|
15
|
-
* @param params.permit2 - The Permit2 contract address for the chain.
|
|
16
|
-
* @param params.args.amount - Required token amount.
|
|
17
|
-
* @param params.allowancesPermit2 - The user's current allowance of `address` for the Permit2
|
|
18
|
-
* contract.
|
|
19
|
-
* @param params.allowancesGeneralAdapter - The user's current direct allowance of `address` for
|
|
20
|
-
* `GeneralAdapter1` (separate from the Permit2-managed allowance).
|
|
21
|
-
* @param params.allowanceGeneralAdapterPermit2 - The Permit2-managed allowance for
|
|
22
|
-
* `GeneralAdapter1` to spend `address`.
|
|
23
|
-
* @param params.allowanceGeneralAdapterExpiration - Expiration timestamp of the Permit2-managed
|
|
24
|
-
* allowance.
|
|
25
|
-
* @param params.nonce - The user's current Permit2 nonce for `(address, GeneralAdapter1)`.
|
|
26
|
-
* @returns Ordered list of approval transactions and/or `Requirement` objects to satisfy before
|
|
27
|
-
* bundling.
|
|
28
|
-
* @throws {ApprovalAmountLessThanSpendAmountError} from the inner approval helper when its
|
|
29
|
-
* bookkeeping invariants break (should not happen with the values this function passes).
|
|
30
|
-
* @example
|
|
31
|
-
* ```ts
|
|
32
|
-
* import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
33
|
-
* import { getRequirementsPermit2 } from "@morpho-org/morpho-sdk";
|
|
34
|
-
*
|
|
35
|
-
* const { permit2 } = getChainAddresses(1);
|
|
36
|
-
* if (!permit2) throw new Error("Permit2 not configured for this chain");
|
|
37
|
-
* const requirements = getRequirementsPermit2({
|
|
38
|
-
* address: USDC,
|
|
39
|
-
* chainId: 1,
|
|
40
|
-
* permit2,
|
|
41
|
-
* args: { amount: 1_000_000n },
|
|
42
|
-
* allowancesPermit2: 0n,
|
|
43
|
-
* allowancesGeneralAdapter: 0n,
|
|
44
|
-
* allowanceGeneralAdapterPermit2: 0n,
|
|
45
|
-
* allowanceGeneralAdapterExpiration: 0n,
|
|
46
|
-
* nonce: 0n,
|
|
47
|
-
* });
|
|
48
|
-
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction> | Requirement>)[]
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
export declare const getRequirementsPermit2: (params: {
|
|
52
|
-
address: Address;
|
|
53
|
-
chainId: number;
|
|
54
|
-
permit2: Address;
|
|
55
|
-
args: {
|
|
56
|
-
amount: bigint;
|
|
57
|
-
};
|
|
58
|
-
allowancesPermit2: bigint;
|
|
59
|
-
allowancesGeneralAdapter: bigint;
|
|
60
|
-
allowanceGeneralAdapterPermit2: bigint;
|
|
61
|
-
allowanceGeneralAdapterExpiration: bigint;
|
|
62
|
-
nonce: bigint;
|
|
63
|
-
}) => Readonly<Transaction<ERC20ApprovalAction> | Requirement>[];
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { Time } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { encodeErc20Permit2 } from "./encode/encodeErc20Permit2.js";
|
|
4
|
-
import { getRequirementsApproval } from "./getRequirementsApproval.js";
|
|
5
|
-
/**
|
|
6
|
-
* Computes the Permit2 prerequisites for `GeneralAdapter1` to pull `amount` of `address`.
|
|
7
|
-
*
|
|
8
|
-
* Returns an empty array when the direct adapter allowance already covers `amount`. Otherwise
|
|
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` (skipped when the existing
|
|
13
|
-
* Permit2-managed allowance covers `amount` and is not about to expire within four hours).
|
|
14
|
-
*
|
|
15
|
-
* @param params.address - ERC-20 token address.
|
|
16
|
-
* @param params.chainId - The chain the bundle targets.
|
|
17
|
-
* @param params.permit2 - The Permit2 contract address for the chain.
|
|
18
|
-
* @param params.args.amount - Required token amount.
|
|
19
|
-
* @param params.allowancesPermit2 - The user's current allowance of `address` for the Permit2
|
|
20
|
-
* contract.
|
|
21
|
-
* @param params.allowancesGeneralAdapter - The user's current direct allowance of `address` for
|
|
22
|
-
* `GeneralAdapter1` (separate from the Permit2-managed allowance).
|
|
23
|
-
* @param params.allowanceGeneralAdapterPermit2 - The Permit2-managed allowance for
|
|
24
|
-
* `GeneralAdapter1` to spend `address`.
|
|
25
|
-
* @param params.allowanceGeneralAdapterExpiration - Expiration timestamp of the Permit2-managed
|
|
26
|
-
* allowance.
|
|
27
|
-
* @param params.nonce - The user's current Permit2 nonce for `(address, GeneralAdapter1)`.
|
|
28
|
-
* @returns Ordered list of approval transactions and/or `Requirement` objects to satisfy before
|
|
29
|
-
* bundling.
|
|
30
|
-
* @throws {ApprovalAmountLessThanSpendAmountError} from the inner approval helper when its
|
|
31
|
-
* bookkeeping invariants break (should not happen with the values this function passes).
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
35
|
-
* import { getRequirementsPermit2 } from "@morpho-org/morpho-sdk";
|
|
36
|
-
*
|
|
37
|
-
* const { permit2 } = getChainAddresses(1);
|
|
38
|
-
* if (!permit2) throw new Error("Permit2 not configured for this chain");
|
|
39
|
-
* const requirements = getRequirementsPermit2({
|
|
40
|
-
* address: USDC,
|
|
41
|
-
* chainId: 1,
|
|
42
|
-
* permit2,
|
|
43
|
-
* args: { amount: 1_000_000n },
|
|
44
|
-
* allowancesPermit2: 0n,
|
|
45
|
-
* allowancesGeneralAdapter: 0n,
|
|
46
|
-
* allowanceGeneralAdapterPermit2: 0n,
|
|
47
|
-
* allowanceGeneralAdapterExpiration: 0n,
|
|
48
|
-
* nonce: 0n,
|
|
49
|
-
* });
|
|
50
|
-
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction> | Requirement>)[]
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
export const getRequirementsPermit2 = (params) => {
|
|
54
|
-
const { address, chainId, permit2, args: { amount }, allowancesPermit2, allowancesGeneralAdapter, allowanceGeneralAdapterPermit2, allowanceGeneralAdapterExpiration, nonce, } = params;
|
|
55
|
-
if (allowancesGeneralAdapter >= amount) {
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
const requirements = [];
|
|
59
|
-
const approvalRequirements = getRequirementsApproval({
|
|
60
|
-
address,
|
|
61
|
-
chainId,
|
|
62
|
-
args: {
|
|
63
|
-
approvalAmount: MathLib.MAX_UINT_160, // Always approve infinite.
|
|
64
|
-
spendAmount: amount,
|
|
65
|
-
spender: permit2,
|
|
66
|
-
},
|
|
67
|
-
allowances: allowancesPermit2,
|
|
68
|
-
});
|
|
69
|
-
requirements.push(...approvalRequirements);
|
|
70
|
-
if (allowanceGeneralAdapterPermit2 < amount ||
|
|
71
|
-
allowanceGeneralAdapterExpiration < Time.timestamp() + Time.s.from.h(4n)) {
|
|
72
|
-
requirements.push(encodeErc20Permit2({
|
|
73
|
-
token: address,
|
|
74
|
-
amount,
|
|
75
|
-
chainId,
|
|
76
|
-
nonce,
|
|
77
|
-
expiration: MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
return requirements;
|
|
81
|
-
};
|