@morpho-org/morpho-sdk 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +31 -33
- package/lib/cjs/actions/index.d.ts +4 -0
- package/lib/{actions → cjs/actions}/index.js +4 -4
- package/lib/cjs/actions/marketV1/borrow.d.ts +69 -0
- package/lib/cjs/actions/marketV1/borrow.js +97 -0
- package/lib/cjs/actions/marketV1/buildReallocationActions.d.ts +28 -0
- package/lib/cjs/actions/marketV1/buildReallocationActions.js +47 -0
- package/lib/cjs/actions/marketV1/index.d.ts +6 -0
- package/lib/{actions → cjs/actions}/marketV1/index.js +6 -6
- package/lib/cjs/actions/marketV1/repay.d.ts +89 -0
- package/lib/cjs/actions/marketV1/repay.js +134 -0
- package/lib/cjs/actions/marketV1/repayWithdrawCollateral.d.ts +103 -0
- package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +155 -0
- package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +59 -0
- package/lib/cjs/actions/marketV1/supplyCollateral.js +116 -0
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +91 -0
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +163 -0
- package/lib/cjs/actions/marketV1/withdrawCollateral.d.ts +49 -0
- package/lib/cjs/actions/marketV1/withdrawCollateral.js +72 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +36 -0
- package/lib/{actions → cjs/actions}/requirements/encode/encodeErc20Approval.js +25 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +52 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +108 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2.d.ts +44 -0
- package/lib/{actions → cjs/actions}/requirements/encode/encodeErc20Permit2.js +36 -4
- package/lib/cjs/actions/requirements/encode/index.d.ts +3 -0
- package/lib/{actions → cjs/actions}/requirements/encode/index.js +3 -3
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +37 -0
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +71 -0
- package/lib/cjs/actions/requirements/getRequirements.d.ts +74 -0
- package/lib/cjs/actions/requirements/getRequirements.js +112 -0
- package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +33 -0
- package/lib/{actions → cjs/actions}/requirements/getRequirementsAction.js +20 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +43 -0
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +63 -0
- package/lib/cjs/actions/requirements/getRequirementsPermit.d.ts +45 -0
- package/lib/cjs/actions/requirements/getRequirementsPermit.js +57 -0
- package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +63 -0
- package/lib/cjs/actions/requirements/getRequirementsPermit2.js +85 -0
- package/lib/cjs/actions/requirements/index.d.ts +6 -0
- package/lib/{actions → cjs/actions}/requirements/index.js +6 -6
- package/lib/cjs/actions/vaultV1/deposit.d.ts +72 -0
- package/lib/cjs/actions/vaultV1/deposit.js +146 -0
- package/lib/cjs/actions/vaultV1/index.d.ts +4 -0
- package/lib/{actions → cjs/actions}/vaultV1/index.js +4 -4
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +84 -0
- package/lib/cjs/actions/vaultV1/migrateToV2.js +143 -0
- package/lib/cjs/actions/vaultV1/redeem.d.ts +39 -0
- package/lib/cjs/actions/vaultV1/redeem.js +57 -0
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +39 -0
- package/lib/cjs/actions/vaultV1/withdraw.js +57 -0
- package/lib/cjs/actions/vaultV2/deposit.d.ts +72 -0
- package/lib/cjs/actions/vaultV2/deposit.js +146 -0
- package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +63 -0
- package/lib/cjs/actions/vaultV2/forceRedeem.js +99 -0
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -0
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +94 -0
- package/lib/cjs/actions/vaultV2/index.d.ts +5 -0
- package/lib/{actions → cjs/actions}/vaultV2/index.js +5 -5
- package/lib/cjs/actions/vaultV2/redeem.d.ts +40 -0
- package/lib/cjs/actions/vaultV2/redeem.js +58 -0
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +40 -0
- package/lib/cjs/actions/vaultV2/withdraw.js +58 -0
- package/lib/cjs/client/index.d.ts +2 -0
- package/lib/{client → cjs/client}/index.js +2 -2
- package/lib/cjs/client/morphoClient.d.ts +113 -0
- package/lib/cjs/client/morphoClient.js +123 -0
- package/lib/cjs/client/morphoViemExtension.d.ts +45 -0
- package/lib/cjs/client/morphoViemExtension.js +46 -0
- package/lib/cjs/entities/index.d.ts +3 -0
- package/lib/{entities → cjs/entities}/index.js +3 -3
- package/lib/cjs/entities/marketV1/index.d.ts +1 -0
- package/lib/{entities/vaultV1 → cjs/entities/marketV1}/index.js +1 -1
- package/lib/{entities → cjs/entities}/marketV1/marketV1.d.ts +9 -6
- package/lib/{entities → cjs/entities}/marketV1/marketV1.js +89 -87
- package/lib/cjs/entities/vaultV1/index.d.ts +1 -0
- package/lib/{entities/marketV1 → cjs/entities/vaultV1}/index.js +1 -1
- package/lib/{entities → cjs/entities}/vaultV1/vaultV1.d.ts +4 -8
- package/lib/{entities → cjs/entities}/vaultV1/vaultV1.js +39 -35
- package/lib/cjs/entities/vaultV2/index.d.ts +1 -0
- package/lib/{entities → cjs/entities}/vaultV2/index.js +1 -1
- package/lib/{entities → cjs/entities}/vaultV2/vaultV2.d.ts +2 -2
- package/lib/{entities → cjs/entities}/vaultV2/vaultV2.js +25 -24
- package/lib/{helpers → cjs/helpers}/computeReallocations.d.ts +1 -1
- package/lib/{helpers → cjs/helpers}/computeReallocations.js +2 -2
- package/lib/{helpers → cjs/helpers}/encodeDeallocation.d.ts +1 -1
- package/lib/{helpers → cjs/helpers}/encodeDeallocation.js +2 -2
- package/lib/{helpers → cjs/helpers}/index.d.ts +4 -4
- package/lib/{helpers → cjs/helpers}/index.js +19 -19
- package/lib/{helpers → cjs/helpers}/metadata.d.ts +1 -1
- package/lib/{helpers → cjs/helpers}/slippage.js +7 -7
- package/lib/{helpers → cjs/helpers}/validate.d.ts +1 -1
- package/lib/{helpers → cjs/helpers}/validate.js +33 -33
- package/lib/cjs/index.d.ts +5 -0
- package/lib/cjs/index.js +21 -0
- package/lib/cjs/package.json +1 -0
- package/lib/{types → cjs/types}/action.d.ts +1 -1
- package/lib/{types → cjs/types}/client.d.ts +6 -2
- package/lib/{types → cjs/types}/error.d.ts +46 -0
- package/lib/{types → cjs/types}/error.js +46 -0
- package/lib/cjs/types/index.d.ts +7 -0
- package/lib/cjs/types/index.js +23 -0
- package/lib/esm/actions/index.d.ts +4 -0
- package/lib/esm/actions/index.js +4 -0
- package/lib/esm/actions/marketV1/borrow.d.ts +69 -0
- package/lib/esm/actions/marketV1/borrow.js +93 -0
- package/lib/esm/actions/marketV1/buildReallocationActions.d.ts +28 -0
- package/lib/esm/actions/marketV1/buildReallocationActions.js +43 -0
- package/lib/esm/actions/marketV1/index.d.ts +6 -0
- package/lib/esm/actions/marketV1/index.js +6 -0
- package/lib/esm/actions/marketV1/repay.d.ts +89 -0
- package/lib/esm/actions/marketV1/repay.js +130 -0
- package/lib/esm/actions/marketV1/repayWithdrawCollateral.d.ts +103 -0
- package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +151 -0
- package/lib/esm/actions/marketV1/supplyCollateral.d.ts +59 -0
- package/lib/esm/actions/marketV1/supplyCollateral.js +112 -0
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +91 -0
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +159 -0
- package/lib/esm/actions/marketV1/withdrawCollateral.d.ts +49 -0
- package/lib/esm/actions/marketV1/withdrawCollateral.js +68 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +36 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +46 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +52 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +104 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2.d.ts +44 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2.js +98 -0
- package/lib/esm/actions/requirements/encode/index.d.ts +3 -0
- package/lib/esm/actions/requirements/encode/index.js +3 -0
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +37 -0
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +67 -0
- package/lib/esm/actions/requirements/getRequirements.d.ts +74 -0
- package/lib/esm/actions/requirements/getRequirements.js +108 -0
- package/lib/esm/actions/requirements/getRequirementsAction.d.ts +33 -0
- package/lib/esm/actions/requirements/getRequirementsAction.js +76 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +43 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.js +59 -0
- package/lib/esm/actions/requirements/getRequirementsPermit.d.ts +45 -0
- package/lib/esm/actions/requirements/getRequirementsPermit.js +53 -0
- package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +63 -0
- package/lib/esm/actions/requirements/getRequirementsPermit2.js +81 -0
- package/lib/esm/actions/requirements/index.d.ts +6 -0
- package/lib/esm/actions/requirements/index.js +6 -0
- package/lib/esm/actions/vaultV1/deposit.d.ts +72 -0
- package/lib/esm/actions/vaultV1/deposit.js +142 -0
- package/lib/esm/actions/vaultV1/index.d.ts +4 -0
- package/lib/esm/actions/vaultV1/index.js +4 -0
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +84 -0
- package/lib/esm/actions/vaultV1/migrateToV2.js +139 -0
- package/lib/esm/actions/vaultV1/redeem.d.ts +39 -0
- package/lib/esm/actions/vaultV1/redeem.js +53 -0
- package/lib/esm/actions/vaultV1/withdraw.d.ts +39 -0
- package/lib/esm/actions/vaultV1/withdraw.js +53 -0
- package/lib/esm/actions/vaultV2/deposit.d.ts +72 -0
- package/lib/esm/actions/vaultV2/deposit.js +142 -0
- package/lib/esm/actions/vaultV2/forceRedeem.d.ts +63 -0
- package/lib/esm/actions/vaultV2/forceRedeem.js +95 -0
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -0
- package/lib/esm/actions/vaultV2/forceWithdraw.js +90 -0
- package/lib/esm/actions/vaultV2/index.d.ts +5 -0
- package/lib/esm/actions/vaultV2/index.js +5 -0
- package/lib/esm/actions/vaultV2/redeem.d.ts +40 -0
- package/lib/esm/actions/vaultV2/redeem.js +54 -0
- package/lib/esm/actions/vaultV2/withdraw.d.ts +40 -0
- package/lib/esm/actions/vaultV2/withdraw.js +54 -0
- package/lib/esm/client/index.d.ts +2 -0
- package/lib/esm/client/index.js +2 -0
- package/lib/esm/client/morphoClient.d.ts +113 -0
- package/lib/esm/client/morphoClient.js +119 -0
- package/lib/esm/client/morphoViemExtension.d.ts +45 -0
- package/lib/esm/client/morphoViemExtension.js +43 -0
- package/lib/esm/entities/index.d.ts +3 -0
- package/lib/esm/entities/index.js +3 -0
- package/lib/esm/entities/marketV1/index.d.ts +1 -0
- package/lib/esm/entities/marketV1/index.js +1 -0
- package/lib/esm/entities/marketV1/marketV1.d.ts +293 -0
- package/lib/esm/entities/marketV1/marketV1.js +529 -0
- package/lib/esm/entities/vaultV1/index.d.ts +1 -0
- package/lib/esm/entities/vaultV1/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +129 -0
- package/lib/esm/entities/vaultV1/vaultV1.js +186 -0
- package/lib/esm/entities/vaultV2/index.d.ts +1 -0
- package/lib/esm/entities/vaultV2/index.js +1 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +180 -0
- package/lib/esm/entities/vaultV2/vaultV2.js +164 -0
- package/lib/esm/helpers/computeReallocations.d.ts +23 -0
- package/lib/esm/helpers/computeReallocations.js +94 -0
- package/lib/esm/helpers/constant.d.ts +6 -0
- package/lib/esm/helpers/constant.js +7 -0
- package/lib/esm/helpers/encodeDeallocation.d.ts +10 -0
- package/lib/esm/helpers/encodeDeallocation.js +34 -0
- package/lib/esm/helpers/index.d.ts +4 -0
- package/lib/esm/helpers/index.js +4 -0
- package/lib/esm/helpers/metadata.d.ts +33 -0
- package/lib/esm/helpers/metadata.js +46 -0
- package/lib/esm/helpers/slippage.d.ts +46 -0
- package/lib/esm/helpers/slippage.js +69 -0
- package/lib/esm/helpers/validate.d.ts +156 -0
- package/lib/esm/helpers/validate.js +273 -0
- package/lib/esm/index.d.ts +5 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/types/action.d.ts +216 -0
- package/lib/esm/types/action.js +21 -0
- package/lib/esm/types/client.d.ts +19 -0
- package/lib/esm/types/client.js +1 -0
- package/lib/esm/types/data.d.ts +2 -0
- package/lib/esm/types/data.js +1 -0
- package/lib/esm/types/deallocation.d.ts +15 -0
- package/lib/esm/types/deallocation.js +1 -0
- package/lib/esm/types/entity.d.ts +5 -0
- package/lib/esm/types/entity.js +1 -0
- package/lib/esm/types/error.d.ts +205 -0
- package/lib/esm/types/error.js +276 -0
- package/lib/esm/types/index.d.ts +7 -0
- package/lib/esm/types/index.js +7 -0
- package/lib/esm/types/metadata.d.ts +4 -0
- package/lib/esm/types/metadata.js +1 -0
- package/lib/esm/types/sharedLiquidity.d.ts +41 -0
- package/lib/esm/types/sharedLiquidity.js +1 -0
- package/package.json +29 -54
- package/src/index.ts +5 -0
- package/lib/actions/index.d.ts +0 -4
- package/lib/actions/marketV1/borrow.d.ts +0 -34
- package/lib/actions/marketV1/borrow.js +0 -62
- package/lib/actions/marketV1/buildReallocationActions.d.ts +0 -17
- package/lib/actions/marketV1/buildReallocationActions.js +0 -36
- package/lib/actions/marketV1/index.d.ts +0 -6
- package/lib/actions/marketV1/repay.d.ts +0 -44
- package/lib/actions/marketV1/repay.js +0 -93
- package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +0 -51
- package/lib/actions/marketV1/repayWithdrawCollateral.js +0 -108
- package/lib/actions/marketV1/supplyCollateral.d.ts +0 -28
- package/lib/actions/marketV1/supplyCollateral.js +0 -85
- package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +0 -37
- package/lib/actions/marketV1/supplyCollateralBorrow.js +0 -109
- package/lib/actions/marketV1/withdrawCollateral.d.ts +0 -28
- package/lib/actions/marketV1/withdrawCollateral.js +0 -51
- package/lib/actions/requirements/encode/encodeErc20Approval.d.ts +0 -10
- package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +0 -13
- package/lib/actions/requirements/encode/encodeErc20Permit.js +0 -70
- package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +0 -11
- package/lib/actions/requirements/encode/index.d.ts +0 -3
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -21
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +0 -55
- package/lib/actions/requirements/getRequirements.d.ts +0 -42
- package/lib/actions/requirements/getRequirements.js +0 -80
- package/lib/actions/requirements/getRequirementsAction.d.ts +0 -17
- package/lib/actions/requirements/getRequirementsApproval.d.ts +0 -31
- package/lib/actions/requirements/getRequirementsApproval.js +0 -51
- package/lib/actions/requirements/getRequirementsPermit.d.ts +0 -28
- package/lib/actions/requirements/getRequirementsPermit.js +0 -40
- package/lib/actions/requirements/getRequirementsPermit2.d.ts +0 -37
- package/lib/actions/requirements/getRequirementsPermit2.js +0 -59
- package/lib/actions/requirements/index.d.ts +0 -6
- package/lib/actions/vaultV1/deposit.d.ts +0 -41
- package/lib/actions/vaultV1/deposit.js +0 -116
- package/lib/actions/vaultV1/index.d.ts +0 -4
- package/lib/actions/vaultV1/migrateToV2.d.ts +0 -50
- package/lib/actions/vaultV1/migrateToV2.js +0 -108
- package/lib/actions/vaultV1/redeem.d.ts +0 -29
- package/lib/actions/vaultV1/redeem.js +0 -48
- package/lib/actions/vaultV1/withdraw.d.ts +0 -29
- package/lib/actions/vaultV1/withdraw.js +0 -48
- package/lib/actions/vaultV2/deposit.d.ts +0 -41
- package/lib/actions/vaultV2/deposit.js +0 -116
- package/lib/actions/vaultV2/forceRedeem.d.ts +0 -48
- package/lib/actions/vaultV2/forceRedeem.js +0 -85
- package/lib/actions/vaultV2/forceWithdraw.d.ts +0 -40
- package/lib/actions/vaultV2/forceWithdraw.js +0 -77
- package/lib/actions/vaultV2/index.d.ts +0 -5
- package/lib/actions/vaultV2/redeem.d.ts +0 -32
- package/lib/actions/vaultV2/redeem.js +0 -51
- package/lib/actions/vaultV2/withdraw.d.ts +0 -32
- package/lib/actions/vaultV2/withdraw.js +0 -51
- package/lib/client/index.d.ts +0 -2
- package/lib/client/morphoClient.d.ts +0 -25
- package/lib/client/morphoClient.js +0 -28
- package/lib/client/morphoViemExtension.d.ts +0 -39
- package/lib/client/morphoViemExtension.js +0 -40
- package/lib/entities/index.d.ts +0 -3
- package/lib/entities/marketV1/index.d.ts +0 -1
- package/lib/entities/vaultV1/index.d.ts +0 -1
- package/lib/entities/vaultV2/index.d.ts +0 -1
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -21
- package/lib/types/index.d.ts +0 -7
- package/lib/types/index.js +0 -23
- /package/lib/{helpers → cjs/helpers}/constant.d.ts +0 -0
- /package/lib/{helpers → cjs/helpers}/constant.js +0 -0
- /package/lib/{helpers → cjs/helpers}/metadata.js +0 -0
- /package/lib/{helpers → cjs/helpers}/slippage.d.ts +0 -0
- /package/lib/{types → cjs/types}/action.js +0 -0
- /package/lib/{types → cjs/types}/client.js +0 -0
- /package/lib/{types → cjs/types}/data.d.ts +0 -0
- /package/lib/{types → cjs/types}/data.js +0 -0
- /package/lib/{types → cjs/types}/deallocation.d.ts +0 -0
- /package/lib/{types → cjs/types}/deallocation.js +0 -0
- /package/lib/{types → cjs/types}/entity.d.ts +0 -0
- /package/lib/{types → cjs/types}/entity.js +0 -0
- /package/lib/{types → cjs/types}/metadata.d.ts +0 -0
- /package/lib/{types → cjs/types}/metadata.js +0 -0
- /package/lib/{types → cjs/types}/sharedLiquidity.d.ts +0 -0
- /package/lib/{types → cjs/types}/sharedLiquidity.js +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Client } from "viem";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type MorphoAuthorizationAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resolves whether `GeneralAdapter1` needs Morpho authorization for the given user, and returns
|
|
6
|
+
* the `setAuthorization(generalAdapter1, true)` transaction when it does.
|
|
7
|
+
*
|
|
8
|
+
* Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
|
|
9
|
+
* any bundled MarketV1 path that operates on behalf of the user (`borrow`,
|
|
10
|
+
* `supplyCollateralBorrow`, `repayWithdrawCollateral`).
|
|
11
|
+
*
|
|
12
|
+
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
13
|
+
* @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
|
|
14
|
+
* @param params.userAddress - The user that must authorize `GeneralAdapter1`.
|
|
15
|
+
* @returns A deep-frozen `Transaction<MorphoAuthorizationAction>`, or `null` when authorization
|
|
16
|
+
* is already in place.
|
|
17
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { createPublicClient, http } from "viem";
|
|
21
|
+
* import { mainnet } from "viem/chains";
|
|
22
|
+
* import { getMorphoAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
23
|
+
*
|
|
24
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
25
|
+
* const tx = await getMorphoAuthorizationRequirement({
|
|
26
|
+
* viemClient: client,
|
|
27
|
+
* chainId: 1,
|
|
28
|
+
* userAddress: borrower,
|
|
29
|
+
* });
|
|
30
|
+
* // tx is null when already authorized, otherwise satisfies Readonly<Transaction<MorphoAuthorizationAction>>
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const getMorphoAuthorizationRequirement: (params: {
|
|
34
|
+
viemClient: Client;
|
|
35
|
+
chainId: number;
|
|
36
|
+
userAddress: Address;
|
|
37
|
+
}) => Promise<Readonly<Transaction<MorphoAuthorizationAction>> | null>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMorphoAuthorizationRequirement = 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 index_js_1 = require("../../types/index.js");
|
|
9
|
+
/**
|
|
10
|
+
* Resolves whether `GeneralAdapter1` needs Morpho authorization for the given user, and returns
|
|
11
|
+
* the `setAuthorization(generalAdapter1, true)` transaction when it does.
|
|
12
|
+
*
|
|
13
|
+
* Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
|
|
14
|
+
* any bundled MarketV1 path that operates on behalf of the user (`borrow`,
|
|
15
|
+
* `supplyCollateralBorrow`, `repayWithdrawCollateral`).
|
|
16
|
+
*
|
|
17
|
+
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
18
|
+
* @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
|
|
19
|
+
* @param params.userAddress - The user that must authorize `GeneralAdapter1`.
|
|
20
|
+
* @returns A deep-frozen `Transaction<MorphoAuthorizationAction>`, or `null` when authorization
|
|
21
|
+
* is already in place.
|
|
22
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { createPublicClient, http } from "viem";
|
|
26
|
+
* import { mainnet } from "viem/chains";
|
|
27
|
+
* import { getMorphoAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
30
|
+
* const tx = await getMorphoAuthorizationRequirement({
|
|
31
|
+
* viemClient: client,
|
|
32
|
+
* chainId: 1,
|
|
33
|
+
* userAddress: borrower,
|
|
34
|
+
* });
|
|
35
|
+
* // tx is null when already authorized, otherwise satisfies Readonly<Transaction<MorphoAuthorizationAction>>
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
const getMorphoAuthorizationRequirement = async (params) => {
|
|
39
|
+
const { viemClient, chainId, userAddress } = params;
|
|
40
|
+
if (viemClient.chain?.id !== chainId) {
|
|
41
|
+
throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
42
|
+
}
|
|
43
|
+
const { morpho, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
44
|
+
const pc = viemClient.extend(viem_1.publicActions);
|
|
45
|
+
const isAuthorized = await pc.readContract({
|
|
46
|
+
address: morpho,
|
|
47
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
48
|
+
functionName: "isAuthorized",
|
|
49
|
+
args: [userAddress, generalAdapter1],
|
|
50
|
+
});
|
|
51
|
+
if (isAuthorized) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
55
|
+
to: morpho,
|
|
56
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
57
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
58
|
+
functionName: "setAuthorization",
|
|
59
|
+
args: [generalAdapter1, true],
|
|
60
|
+
}),
|
|
61
|
+
value: 0n,
|
|
62
|
+
action: {
|
|
63
|
+
type: "morphoAuthorization",
|
|
64
|
+
args: {
|
|
65
|
+
authorized: generalAdapter1,
|
|
66
|
+
isAuthorized: true,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
exports.getMorphoAuthorizationRequirement = getMorphoAuthorizationRequirement;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Client } from "viem";
|
|
3
|
+
import { type ERC20ApprovalAction, type Requirement, type Transaction } from "../../types/index.js";
|
|
4
|
+
type GetRequirementsBaseParams = {
|
|
5
|
+
address: Address;
|
|
6
|
+
chainId: number;
|
|
7
|
+
supportDeployless?: boolean;
|
|
8
|
+
args: {
|
|
9
|
+
amount: bigint;
|
|
10
|
+
from: Address;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
type GetRequirementsParams = (GetRequirementsBaseParams & {
|
|
14
|
+
/** Signature-based approvals are not supported. Classic approval (transaction) will be used. */
|
|
15
|
+
supportSignature: false;
|
|
16
|
+
}) | (GetRequirementsBaseParams & {
|
|
17
|
+
/** Signature-based approvals are supported. Will try permit (EIP-2612), else fallback to permit2. */
|
|
18
|
+
supportSignature: true;
|
|
19
|
+
/** Allow simple permit if EIP-2612 is supported. Only applicable when `supportSignature` is `true`. */
|
|
20
|
+
useSimplePermit?: boolean;
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the approval requirements an integrator must satisfy before a Morpho bundle pulls
|
|
24
|
+
* tokens through `GeneralAdapter1`.
|
|
25
|
+
*
|
|
26
|
+
* Reads the user's current `holding` (allowances + nonces) from the chain, then picks one of
|
|
27
|
+
* three flows:
|
|
28
|
+
*
|
|
29
|
+
* 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 supported + `useSimplePermit`** — single permit
|
|
32
|
+
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
33
|
+
* permit signature is incompatible) and falls through to Permit2 even with
|
|
34
|
+
* `useSimplePermit: true`.
|
|
35
|
+
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
36
|
+
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
37
|
+
*
|
|
38
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
39
|
+
* @param params - Requirement resolution parameters.
|
|
40
|
+
* @param params.address - ERC-20 token address.
|
|
41
|
+
* @param params.chainId - Chain id; must match `viemClient.chain.id`.
|
|
42
|
+
* @param params.args.amount - Required token amount. Returns `[]` when zero.
|
|
43
|
+
* @param params.args.from - Account that will grant the approval.
|
|
44
|
+
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
45
|
+
* permit / permit2 vs. classic approval.
|
|
46
|
+
* @param params.supportDeployless - Whether to fetch holdings via deployless multicall.
|
|
47
|
+
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
48
|
+
* the token supports it.
|
|
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 the
|
|
51
|
+
* existing allowance already covers `amount`.
|
|
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 { getRequirements } from "@morpho-org/morpho-sdk";
|
|
62
|
+
*
|
|
63
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
64
|
+
* const requirements = await getRequirements(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>> | Requirement)[]
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare const getRequirements: (viemClient: Client, params: GetRequirementsParams) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirements = 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 index_js_1 = require("../../types/index.js");
|
|
8
|
+
const getRequirementsApproval_js_1 = require("./getRequirementsApproval.js");
|
|
9
|
+
const getRequirementsPermit_js_1 = require("./getRequirementsPermit.js");
|
|
10
|
+
const getRequirementsPermit2_js_1 = require("./getRequirementsPermit2.js");
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the approval requirements an integrator must satisfy before a Morpho bundle pulls
|
|
13
|
+
* tokens through `GeneralAdapter1`.
|
|
14
|
+
*
|
|
15
|
+
* Reads the user's current `holding` (allowances + nonces) from the chain, then picks one of
|
|
16
|
+
* three flows:
|
|
17
|
+
*
|
|
18
|
+
* 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
|
|
19
|
+
* allowance is already large enough).
|
|
20
|
+
* 2. **`supportSignature: true` + EIP-2612 supported + `useSimplePermit`** — single permit
|
|
21
|
+
* signature against the token itself. DAI is excluded from this branch (its non-standard
|
|
22
|
+
* permit signature is incompatible) and falls through to Permit2 even with
|
|
23
|
+
* `useSimplePermit: true`.
|
|
24
|
+
* 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
|
|
25
|
+
* contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
|
|
26
|
+
*
|
|
27
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
28
|
+
* @param params - Requirement resolution parameters.
|
|
29
|
+
* @param params.address - ERC-20 token address.
|
|
30
|
+
* @param params.chainId - Chain id; must match `viemClient.chain.id`.
|
|
31
|
+
* @param params.args.amount - Required token amount. Returns `[]` when zero.
|
|
32
|
+
* @param params.args.from - Account that will grant the approval.
|
|
33
|
+
* @param params.supportSignature - Whether the integrator can collect a signature; controls
|
|
34
|
+
* permit / permit2 vs. classic approval.
|
|
35
|
+
* @param params.supportDeployless - Whether to fetch holdings via deployless multicall.
|
|
36
|
+
* @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
|
|
37
|
+
* the token supports it.
|
|
38
|
+
* @returns Promise resolving to an array of either deep-frozen approval transactions or
|
|
39
|
+
* `Requirement` objects (signature requirements with a `sign()` method). Empty when the
|
|
40
|
+
* existing allowance already covers `amount`.
|
|
41
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
|
|
42
|
+
* error is reachable through this entry point: the values passed into
|
|
43
|
+
* `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
|
|
44
|
+
* `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
|
|
45
|
+
* `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { createPublicClient, http } from "viem";
|
|
49
|
+
* import { mainnet } from "viem/chains";
|
|
50
|
+
* import { getRequirements } from "@morpho-org/morpho-sdk";
|
|
51
|
+
*
|
|
52
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
53
|
+
* const requirements = await getRequirements(client, {
|
|
54
|
+
* address: USDC,
|
|
55
|
+
* chainId: 1,
|
|
56
|
+
* supportSignature: true,
|
|
57
|
+
* args: { amount: 1_000_000n, from: user },
|
|
58
|
+
* });
|
|
59
|
+
* // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
const getRequirements = async (viemClient, params) => {
|
|
63
|
+
const { address, chainId, supportSignature, args: { amount, from }, } = params;
|
|
64
|
+
if (viemClient.chain?.id !== chainId) {
|
|
65
|
+
throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
66
|
+
}
|
|
67
|
+
if (amount === 0n) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
const { permit2, dai, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
71
|
+
const { erc20Allowances, erc2612Nonce, permit2BundlerAllowance } = await (0, blue_sdk_viem_1.fetchHolding)(from, address, viemClient, {
|
|
72
|
+
deployless: params.supportDeployless,
|
|
73
|
+
});
|
|
74
|
+
if (supportSignature) {
|
|
75
|
+
const { useSimplePermit } = params;
|
|
76
|
+
const supportSimplePermit = (0, morpho_ts_1.isDefined)(erc2612Nonce) && address !== dai;
|
|
77
|
+
if (supportSimplePermit && useSimplePermit) {
|
|
78
|
+
return await (0, getRequirementsPermit_js_1.getRequirementsPermit)(viemClient, {
|
|
79
|
+
token: address,
|
|
80
|
+
chainId,
|
|
81
|
+
args: { amount },
|
|
82
|
+
allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
|
|
83
|
+
nonce: erc2612Nonce,
|
|
84
|
+
supportDeployless: params.supportDeployless,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (permit2) {
|
|
88
|
+
return (0, getRequirementsPermit2_js_1.getRequirementsPermit2)({
|
|
89
|
+
address,
|
|
90
|
+
chainId,
|
|
91
|
+
permit2,
|
|
92
|
+
args: { amount },
|
|
93
|
+
allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
|
|
94
|
+
allowancesPermit2: erc20Allowances.permit2,
|
|
95
|
+
allowanceGeneralAdapterPermit2: permit2BundlerAllowance.amount,
|
|
96
|
+
allowanceGeneralAdapterExpiration: permit2BundlerAllowance.expiration,
|
|
97
|
+
nonce: permit2BundlerAllowance.nonce,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return (0, getRequirementsApproval_js_1.getRequirementsApproval)({
|
|
102
|
+
address,
|
|
103
|
+
chainId,
|
|
104
|
+
args: {
|
|
105
|
+
spendAmount: amount,
|
|
106
|
+
approvalAmount: amount,
|
|
107
|
+
spender: generalAdapter1,
|
|
108
|
+
},
|
|
109
|
+
allowances: erc20Allowances["bundler3.generalAdapter1"],
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
exports.getRequirements = getRequirements;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Action } from "@morpho-org/bundler-sdk-viem";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Permit2Action, type PermitAction, type PermitArgs } from "../../types/index.js";
|
|
4
|
+
interface GetRequirementsActionParams {
|
|
5
|
+
chainId: number;
|
|
6
|
+
asset: Address;
|
|
7
|
+
amount: bigint;
|
|
8
|
+
requirementSignature: {
|
|
9
|
+
args: PermitArgs;
|
|
10
|
+
action: PermitAction | Permit2Action;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
|
|
15
|
+
* the asset into `GeneralAdapter1`.
|
|
16
|
+
*
|
|
17
|
+
* Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
|
|
18
|
+
* `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
|
|
19
|
+
* `amount` exactly, otherwise the function throws so the caller does not silently spend a
|
|
20
|
+
* wider-than-expected approval. Internal helper — consumed only by the action builders that
|
|
21
|
+
* accept a `requirementSignature`; not re-exported on the public surface.
|
|
22
|
+
*
|
|
23
|
+
* @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
|
|
24
|
+
* @param params.asset - The ERC-20 to pull into the adapter.
|
|
25
|
+
* @param params.amount - The amount to pull, in the asset's smallest unit.
|
|
26
|
+
* @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
|
|
27
|
+
* @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
|
|
28
|
+
* @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
|
|
29
|
+
* @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare const getRequirementsAction: ({ chainId, asset, amount, requirementSignature, }: GetRequirementsActionParams) => Action[];
|
|
33
|
+
export {};
|
|
@@ -3,16 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getRequirementsAction = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
|
-
const
|
|
6
|
+
const index_js_1 = require("../../types/index.js");
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
|
|
9
|
+
* the asset into `GeneralAdapter1`.
|
|
10
|
+
*
|
|
11
|
+
* Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
|
|
12
|
+
* `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
|
|
13
|
+
* `amount` exactly, otherwise the function throws so the caller does not silently spend a
|
|
14
|
+
* wider-than-expected approval. Internal helper — consumed only by the action builders that
|
|
15
|
+
* accept a `requirementSignature`; not re-exported on the public surface.
|
|
16
|
+
*
|
|
17
|
+
* @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
|
|
18
|
+
* @param params.asset - The ERC-20 to pull into the adapter.
|
|
19
|
+
* @param params.amount - The amount to pull, in the asset's smallest unit.
|
|
20
|
+
* @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
|
|
21
|
+
* @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
|
|
22
|
+
* @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
|
|
23
|
+
* @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
|
|
24
|
+
* @internal
|
|
9
25
|
*/
|
|
10
26
|
const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }) => {
|
|
11
27
|
if (!(0, viem_1.isAddressEqual)(requirementSignature.args.asset, asset)) {
|
|
12
|
-
throw new
|
|
28
|
+
throw new index_js_1.DepositAssetMismatchError(asset, requirementSignature.args.asset);
|
|
13
29
|
}
|
|
14
30
|
if (requirementSignature.args.amount !== amount) {
|
|
15
|
-
throw new
|
|
31
|
+
throw new index_js_1.DepositAmountMismatchError(amount, requirementSignature.args.amount);
|
|
16
32
|
}
|
|
17
33
|
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
18
34
|
if (requirementSignature.action.type === "permit2") {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Computes classic ERC-20 approval transactions for a spender, given the existing allowance.
|
|
5
|
+
*
|
|
6
|
+
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
7
|
+
* `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
|
|
8
|
+
* `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
|
|
9
|
+
* before re-approving.
|
|
10
|
+
*
|
|
11
|
+
* @param params.address - ERC-20 token address.
|
|
12
|
+
* @param params.chainId - The chain the bundle targets.
|
|
13
|
+
* @param params.args.spendAmount - The amount the bundle will actually pull.
|
|
14
|
+
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
15
|
+
* may be `MAX_UINT_160` for Permit2 prerequisites).
|
|
16
|
+
* @param params.args.spender - Address that will be granted the approval.
|
|
17
|
+
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
18
|
+
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
19
|
+
* followed by the new approval. Empty when no approval is needed.
|
|
20
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
24
|
+
*
|
|
25
|
+
* const txs = getRequirementsApproval({
|
|
26
|
+
* address: USDC,
|
|
27
|
+
* chainId: 1,
|
|
28
|
+
* args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: generalAdapter1 },
|
|
29
|
+
* allowances: 0n,
|
|
30
|
+
* });
|
|
31
|
+
* // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare const getRequirementsApproval: (params: {
|
|
35
|
+
address: Address;
|
|
36
|
+
chainId: number;
|
|
37
|
+
args: {
|
|
38
|
+
approvalAmount: bigint;
|
|
39
|
+
spendAmount: bigint;
|
|
40
|
+
spender: Address;
|
|
41
|
+
};
|
|
42
|
+
allowances: bigint;
|
|
43
|
+
}) => Readonly<Transaction<ERC20ApprovalAction>>[];
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsApproval = void 0;
|
|
4
|
+
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
5
|
+
const index_js_1 = require("../../types/index.js");
|
|
6
|
+
const encodeErc20Approval_js_1 = require("./encode/encodeErc20Approval.js");
|
|
7
|
+
/**
|
|
8
|
+
* Computes classic ERC-20 approval transactions for a spender, given the existing allowance.
|
|
9
|
+
*
|
|
10
|
+
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
11
|
+
* `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
|
|
12
|
+
* `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
|
|
13
|
+
* before re-approving.
|
|
14
|
+
*
|
|
15
|
+
* @param params.address - ERC-20 token address.
|
|
16
|
+
* @param params.chainId - The chain the bundle targets.
|
|
17
|
+
* @param params.args.spendAmount - The amount the bundle will actually pull.
|
|
18
|
+
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
19
|
+
* may be `MAX_UINT_160` for Permit2 prerequisites).
|
|
20
|
+
* @param params.args.spender - Address that will be granted the approval.
|
|
21
|
+
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
22
|
+
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
23
|
+
* followed by the new approval. Empty when no approval is needed.
|
|
24
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* const txs = getRequirementsApproval({
|
|
30
|
+
* address: USDC,
|
|
31
|
+
* chainId: 1,
|
|
32
|
+
* args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: generalAdapter1 },
|
|
33
|
+
* allowances: 0n,
|
|
34
|
+
* });
|
|
35
|
+
* // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
const getRequirementsApproval = (params) => {
|
|
39
|
+
const { address, chainId, args: { spendAmount, approvalAmount, spender }, allowances, } = params;
|
|
40
|
+
if (approvalAmount < spendAmount) {
|
|
41
|
+
throw new index_js_1.ApprovalAmountLessThanSpendAmountError();
|
|
42
|
+
}
|
|
43
|
+
const approvals = [];
|
|
44
|
+
if (allowances < spendAmount) {
|
|
45
|
+
if (simulation_sdk_1.APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(address) &&
|
|
46
|
+
allowances > 0n) {
|
|
47
|
+
approvals.push((0, encodeErc20Approval_js_1.encodeErc20Approval)({
|
|
48
|
+
token: address,
|
|
49
|
+
spender,
|
|
50
|
+
amount: 0n,
|
|
51
|
+
chainId,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
approvals.push((0, encodeErc20Approval_js_1.encodeErc20Approval)({
|
|
55
|
+
token: address,
|
|
56
|
+
spender,
|
|
57
|
+
amount: approvalAmount,
|
|
58
|
+
chainId,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
return approvals;
|
|
62
|
+
};
|
|
63
|
+
exports.getRequirementsApproval = getRequirementsApproval;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Address, Client } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Computes the EIP-2612 permit `Requirement` an integrator must sign so that `GeneralAdapter1`
|
|
4
|
+
* can pull `amount` of `token`.
|
|
5
|
+
*
|
|
6
|
+
* Returns an empty array when the existing allowance for `GeneralAdapter1` already covers
|
|
7
|
+
* `amount`.
|
|
8
|
+
*
|
|
9
|
+
* @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
|
|
10
|
+
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
11
|
+
* @param params.chainId - The chain the bundle targets.
|
|
12
|
+
* @param params.args.amount - Required token amount.
|
|
13
|
+
* @param params.allowancesGeneralAdapter - The user's current allowance of `token` for
|
|
14
|
+
* `GeneralAdapter1`.
|
|
15
|
+
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
16
|
+
* @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
|
|
17
|
+
* @returns A single-element array containing the `Requirement` to sign, or an empty array when
|
|
18
|
+
* the existing allowance already covers `amount`.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { createWalletClient, http } from "viem";
|
|
22
|
+
* import { mainnet } from "viem/chains";
|
|
23
|
+
* import { getRequirementsPermit } from "@morpho-org/morpho-sdk";
|
|
24
|
+
*
|
|
25
|
+
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
26
|
+
* const reqs = await getRequirementsPermit(client, {
|
|
27
|
+
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by getRequirements
|
|
28
|
+
* chainId: 1,
|
|
29
|
+
* args: { amount: 1_000_000n },
|
|
30
|
+
* allowancesGeneralAdapter: 0n,
|
|
31
|
+
* nonce: 0n,
|
|
32
|
+
* });
|
|
33
|
+
* // reqs satisfies Requirement[]
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const getRequirementsPermit: (viemClient: Client, params: {
|
|
37
|
+
token: Address;
|
|
38
|
+
chainId: number;
|
|
39
|
+
args: {
|
|
40
|
+
amount: bigint;
|
|
41
|
+
};
|
|
42
|
+
allowancesGeneralAdapter: bigint;
|
|
43
|
+
nonce: bigint;
|
|
44
|
+
supportDeployless?: boolean;
|
|
45
|
+
}) => Promise<import("../../index.js").Requirement[]>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsPermit = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const index_js_1 = require("./encode/index.js");
|
|
6
|
+
/**
|
|
7
|
+
* Computes the EIP-2612 permit `Requirement` an integrator must sign so that `GeneralAdapter1`
|
|
8
|
+
* can pull `amount` of `token`.
|
|
9
|
+
*
|
|
10
|
+
* Returns an empty array when the existing allowance for `GeneralAdapter1` already covers
|
|
11
|
+
* `amount`.
|
|
12
|
+
*
|
|
13
|
+
* @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
|
|
14
|
+
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
15
|
+
* @param params.chainId - The chain the bundle targets.
|
|
16
|
+
* @param params.args.amount - Required token amount.
|
|
17
|
+
* @param params.allowancesGeneralAdapter - The user's current allowance of `token` for
|
|
18
|
+
* `GeneralAdapter1`.
|
|
19
|
+
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
20
|
+
* @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
|
|
21
|
+
* @returns A single-element array containing the `Requirement` to sign, or an empty array when
|
|
22
|
+
* the existing allowance already covers `amount`.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { createWalletClient, http } from "viem";
|
|
26
|
+
* import { mainnet } from "viem/chains";
|
|
27
|
+
* import { getRequirementsPermit } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
30
|
+
* const reqs = await getRequirementsPermit(client, {
|
|
31
|
+
* token: USDC, // an ERC-2612-compatible token; DAI is excluded by getRequirements
|
|
32
|
+
* chainId: 1,
|
|
33
|
+
* args: { amount: 1_000_000n },
|
|
34
|
+
* allowancesGeneralAdapter: 0n,
|
|
35
|
+
* nonce: 0n,
|
|
36
|
+
* });
|
|
37
|
+
* // reqs satisfies Requirement[]
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const getRequirementsPermit = async (viemClient, params) => {
|
|
41
|
+
const { token, chainId, args: { amount }, allowancesGeneralAdapter, nonce, supportDeployless, } = params;
|
|
42
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
43
|
+
if (allowancesGeneralAdapter < amount) {
|
|
44
|
+
return [
|
|
45
|
+
await (0, index_js_1.encodeErc20Permit)(viemClient, {
|
|
46
|
+
token,
|
|
47
|
+
spender: generalAdapter1,
|
|
48
|
+
amount,
|
|
49
|
+
chainId,
|
|
50
|
+
nonce,
|
|
51
|
+
supportDeployless,
|
|
52
|
+
}),
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
return [];
|
|
56
|
+
};
|
|
57
|
+
exports.getRequirementsPermit = getRequirementsPermit;
|
|
@@ -0,0 +1,63 @@
|
|
|
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>[];
|