@morpho-org/morpho-sdk 1.1.0 → 1.2.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/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 +33 -56
- 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,53 @@
|
|
|
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
|
+
};
|
|
@@ -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>[];
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./encode/index.js";
|
|
2
|
+
export * from "./getMorphoAuthorizationRequirement.js";
|
|
3
|
+
export * from "./getRequirements.js";
|
|
4
|
+
export * from "./getRequirementsApproval.js";
|
|
5
|
+
export * from "./getRequirementsPermit.js";
|
|
6
|
+
export * from "./getRequirementsPermit2.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./encode/index.js";
|
|
2
|
+
export * from "./getMorphoAuthorizationRequirement.js";
|
|
3
|
+
export * from "./getRequirements.js";
|
|
4
|
+
export * from "./getRequirementsApproval.js";
|
|
5
|
+
export * from "./getRequirementsPermit.js";
|
|
6
|
+
export * from "./getRequirementsPermit2.js";
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { type DepositAmountArgs, type Metadata, type RequirementSignature, type Transaction, type VaultV1DepositAction } from "../../types/index.js";
|
|
3
|
+
/** Parameters for {@link vaultV1Deposit}. */
|
|
4
|
+
export interface VaultV1DepositParams {
|
|
5
|
+
vault: {
|
|
6
|
+
chainId: number;
|
|
7
|
+
address: Address;
|
|
8
|
+
asset: Address;
|
|
9
|
+
};
|
|
10
|
+
args: DepositAmountArgs & {
|
|
11
|
+
maxSharePrice: bigint;
|
|
12
|
+
recipient: Address;
|
|
13
|
+
requirementSignature?: RequirementSignature;
|
|
14
|
+
};
|
|
15
|
+
metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares a deposit transaction for a VaultV1 (MetaMorpho) contract.
|
|
19
|
+
*
|
|
20
|
+
* Routed through bundler3 to atomically execute the asset transfer and vault deposit. The
|
|
21
|
+
* `GeneralAdapter1` enforces `maxSharePrice` on-chain to prevent inflation attacks. Never bypass
|
|
22
|
+
* the general adapter.
|
|
23
|
+
*
|
|
24
|
+
* When `nativeAmount > 0`, that amount of native ETH is sent as `msg.value` to the bundler3
|
|
25
|
+
* multicall and wrapped into wNative via `GeneralAdapter1.wrapNative()`. The vault's underlying
|
|
26
|
+
* asset must be the chain's wrapped native token.
|
|
27
|
+
*
|
|
28
|
+
* @param params.vault.chainId - The chain the vault lives on.
|
|
29
|
+
* @param params.vault.address - The VaultV1 (MetaMorpho) address.
|
|
30
|
+
* @param params.vault.asset - The vault's underlying ERC-20 asset.
|
|
31
|
+
* @param params.args.amount - Amount of ERC-20 assets to deposit. At least one of `amount` or
|
|
32
|
+
* `nativeAmount` must be positive. Defaults to `0n`.
|
|
33
|
+
* @param params.args.maxSharePrice - Maximum acceptable share price (in RAY, slippage
|
|
34
|
+
* protection enforced on-chain by `GeneralAdapter1`).
|
|
35
|
+
* @param params.args.recipient - Address that receives the minted vault shares.
|
|
36
|
+
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval. When
|
|
37
|
+
* absent, the bundle uses a plain `erc20TransferFrom` and assumes the user has already
|
|
38
|
+
* approved `GeneralAdapter1`.
|
|
39
|
+
* @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
|
|
40
|
+
* deposit. Requires the vault asset to be the chain's wNative.
|
|
41
|
+
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
42
|
+
* @returns A deep-frozen `Transaction<VaultV1DepositAction>` with `to`, `value`, `data`, and the
|
|
43
|
+
* typed `action` discriminator the simulation layer consumes.
|
|
44
|
+
* @throws {NonPositiveAssetAmountError} when `amount < 0n`.
|
|
45
|
+
* @throws {NonPositiveMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
46
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
47
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount` is provided but the chain has no
|
|
48
|
+
* configured wNative.
|
|
49
|
+
* @throws {NativeAmountOnNonWNativeVaultError} when `nativeAmount` is provided but the vault
|
|
50
|
+
* asset is not the chain's wNative.
|
|
51
|
+
* @throws {DepositAssetMismatchError} from `getRequirementsAction` when `amount > 0n` and
|
|
52
|
+
* `requirementSignature` is provided and the signed asset differs from `vault.asset`. The
|
|
53
|
+
* signature is ignored on the native-only path (`amount === 0n` with `nativeAmount > 0n`).
|
|
54
|
+
* @throws {DepositAmountMismatchError} from `getRequirementsAction` when `amount > 0n` and
|
|
55
|
+
* `requirementSignature` is provided and the signed amount differs from `args.amount`.
|
|
56
|
+
* @throws {ZeroDepositAmountError} when both `amount` and `nativeAmount` resolve to zero.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { vaultV1Deposit } from "@morpho-org/morpho-sdk";
|
|
60
|
+
*
|
|
61
|
+
* const tx = vaultV1Deposit({
|
|
62
|
+
* vault: { chainId: 1, address: vaultAddress, asset: USDC },
|
|
63
|
+
* args: {
|
|
64
|
+
* amount: 1_000_000n,
|
|
65
|
+
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
66
|
+
* recipient: depositor,
|
|
67
|
+
* },
|
|
68
|
+
* });
|
|
69
|
+
* // tx satisfies Readonly<Transaction<VaultV1DepositAction>>
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare const vaultV1Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { amount, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }: VaultV1DepositParams) => Readonly<Transaction<VaultV1DepositAction>>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { BundlerAction } from "@morpho-org/bundler-sdk-viem";
|
|
3
|
+
import { deepFreeze, isDefined } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { isAddressEqual } from "viem";
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
|
+
import { ChainWNativeMissingError, NativeAmountOnNonWNativeVaultError, NegativeNativeAmountError, NonPositiveAssetAmountError, NonPositiveMaxSharePriceError, ZeroDepositAmountError, } from "../../types/index.js";
|
|
7
|
+
import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
|
|
8
|
+
/**
|
|
9
|
+
* Prepares a deposit transaction for a VaultV1 (MetaMorpho) contract.
|
|
10
|
+
*
|
|
11
|
+
* Routed through bundler3 to atomically execute the asset transfer and vault deposit. The
|
|
12
|
+
* `GeneralAdapter1` enforces `maxSharePrice` on-chain to prevent inflation attacks. Never bypass
|
|
13
|
+
* the general adapter.
|
|
14
|
+
*
|
|
15
|
+
* When `nativeAmount > 0`, that amount of native ETH is sent as `msg.value` to the bundler3
|
|
16
|
+
* multicall and wrapped into wNative via `GeneralAdapter1.wrapNative()`. The vault's underlying
|
|
17
|
+
* asset must be the chain's wrapped native token.
|
|
18
|
+
*
|
|
19
|
+
* @param params.vault.chainId - The chain the vault lives on.
|
|
20
|
+
* @param params.vault.address - The VaultV1 (MetaMorpho) address.
|
|
21
|
+
* @param params.vault.asset - The vault's underlying ERC-20 asset.
|
|
22
|
+
* @param params.args.amount - Amount of ERC-20 assets to deposit. At least one of `amount` or
|
|
23
|
+
* `nativeAmount` must be positive. Defaults to `0n`.
|
|
24
|
+
* @param params.args.maxSharePrice - Maximum acceptable share price (in RAY, slippage
|
|
25
|
+
* protection enforced on-chain by `GeneralAdapter1`).
|
|
26
|
+
* @param params.args.recipient - Address that receives the minted vault shares.
|
|
27
|
+
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval. When
|
|
28
|
+
* absent, the bundle uses a plain `erc20TransferFrom` and assumes the user has already
|
|
29
|
+
* approved `GeneralAdapter1`.
|
|
30
|
+
* @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
|
|
31
|
+
* deposit. Requires the vault asset to be the chain's wNative.
|
|
32
|
+
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
33
|
+
* @returns A deep-frozen `Transaction<VaultV1DepositAction>` with `to`, `value`, `data`, and the
|
|
34
|
+
* typed `action` discriminator the simulation layer consumes.
|
|
35
|
+
* @throws {NonPositiveAssetAmountError} when `amount < 0n`.
|
|
36
|
+
* @throws {NonPositiveMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
37
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
38
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount` is provided but the chain has no
|
|
39
|
+
* configured wNative.
|
|
40
|
+
* @throws {NativeAmountOnNonWNativeVaultError} when `nativeAmount` is provided but the vault
|
|
41
|
+
* asset is not the chain's wNative.
|
|
42
|
+
* @throws {DepositAssetMismatchError} from `getRequirementsAction` when `amount > 0n` and
|
|
43
|
+
* `requirementSignature` is provided and the signed asset differs from `vault.asset`. The
|
|
44
|
+
* signature is ignored on the native-only path (`amount === 0n` with `nativeAmount > 0n`).
|
|
45
|
+
* @throws {DepositAmountMismatchError} from `getRequirementsAction` when `amount > 0n` and
|
|
46
|
+
* `requirementSignature` is provided and the signed amount differs from `args.amount`.
|
|
47
|
+
* @throws {ZeroDepositAmountError} when both `amount` and `nativeAmount` resolve to zero.
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { vaultV1Deposit } from "@morpho-org/morpho-sdk";
|
|
51
|
+
*
|
|
52
|
+
* const tx = vaultV1Deposit({
|
|
53
|
+
* vault: { chainId: 1, address: vaultAddress, asset: USDC },
|
|
54
|
+
* args: {
|
|
55
|
+
* amount: 1_000_000n,
|
|
56
|
+
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
57
|
+
* recipient: depositor,
|
|
58
|
+
* },
|
|
59
|
+
* });
|
|
60
|
+
* // tx satisfies Readonly<Transaction<VaultV1DepositAction>>
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export const vaultV1Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { amount = 0n, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }) => {
|
|
64
|
+
if (amount < 0n) {
|
|
65
|
+
throw new NonPositiveAssetAmountError(asset);
|
|
66
|
+
}
|
|
67
|
+
if (maxSharePrice <= 0n) {
|
|
68
|
+
throw new NonPositiveMaxSharePriceError(vaultAddress);
|
|
69
|
+
}
|
|
70
|
+
const actions = [];
|
|
71
|
+
const { bundler3: { generalAdapter1, bundler3 }, wNative, } = getChainAddresses(chainId);
|
|
72
|
+
if (nativeAmount) {
|
|
73
|
+
if (nativeAmount < 0n) {
|
|
74
|
+
throw new NegativeNativeAmountError(nativeAmount);
|
|
75
|
+
}
|
|
76
|
+
if (!isDefined(wNative)) {
|
|
77
|
+
throw new ChainWNativeMissingError(chainId);
|
|
78
|
+
}
|
|
79
|
+
if (!isAddressEqual(asset, wNative)) {
|
|
80
|
+
throw new NativeAmountOnNonWNativeVaultError(asset, wNative);
|
|
81
|
+
}
|
|
82
|
+
actions.push(
|
|
83
|
+
// Transfers native token from Bundler3 to GeneralAdapter1 for wrapping.
|
|
84
|
+
{
|
|
85
|
+
type: "nativeTransfer",
|
|
86
|
+
args: [bundler3, generalAdapter1, nativeAmount, false /* skipRevert */],
|
|
87
|
+
}, {
|
|
88
|
+
type: "wrapNative",
|
|
89
|
+
args: [nativeAmount, generalAdapter1, false /* skipRevert */],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (amount > 0n) {
|
|
93
|
+
if (requirementSignature) {
|
|
94
|
+
actions.push(...getRequirementsAction({
|
|
95
|
+
chainId,
|
|
96
|
+
asset,
|
|
97
|
+
amount,
|
|
98
|
+
requirementSignature,
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
actions.push({
|
|
103
|
+
type: "erc20TransferFrom",
|
|
104
|
+
args: [asset, amount, generalAdapter1, false /* skipRevert */],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
109
|
+
if (totalAssets === 0n) {
|
|
110
|
+
throw new ZeroDepositAmountError(vaultAddress);
|
|
111
|
+
}
|
|
112
|
+
actions.push({
|
|
113
|
+
type: "erc4626Deposit",
|
|
114
|
+
args: [
|
|
115
|
+
vaultAddress,
|
|
116
|
+
totalAssets,
|
|
117
|
+
maxSharePrice,
|
|
118
|
+
recipient,
|
|
119
|
+
false /* skipRevert */,
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
let tx = BundlerAction.encodeBundle(chainId, actions);
|
|
123
|
+
if (nativeAmount) {
|
|
124
|
+
tx = { ...tx, value: nativeAmount };
|
|
125
|
+
}
|
|
126
|
+
if (metadata) {
|
|
127
|
+
tx = addTransactionMetadata(tx, metadata);
|
|
128
|
+
}
|
|
129
|
+
return deepFreeze({
|
|
130
|
+
...tx,
|
|
131
|
+
action: {
|
|
132
|
+
type: "vaultV1Deposit",
|
|
133
|
+
args: {
|
|
134
|
+
vault: vaultAddress,
|
|
135
|
+
amount,
|
|
136
|
+
maxSharePrice,
|
|
137
|
+
recipient,
|
|
138
|
+
nativeAmount,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { type Metadata, type RequirementSignature, type Transaction, type VaultV1MigrateToV2Action } from "../../types/index.js";
|
|
3
|
+
/** Parameters for {@link vaultV1MigrateToV2}. */
|
|
4
|
+
export interface VaultV1MigrateToV2Params {
|
|
5
|
+
vault: {
|
|
6
|
+
chainId: number;
|
|
7
|
+
address: Address;
|
|
8
|
+
/** Underlying asset of the source V1 vault. */
|
|
9
|
+
asset: Address;
|
|
10
|
+
};
|
|
11
|
+
args: {
|
|
12
|
+
targetVault: Address;
|
|
13
|
+
/** Underlying asset of the target V2 vault. */
|
|
14
|
+
targetAsset: Address;
|
|
15
|
+
/** Number of V1 shares to migrate. */
|
|
16
|
+
shares: bigint;
|
|
17
|
+
/** Minimum acceptable share price for V1 redeem (slippage protection, in RAY). */
|
|
18
|
+
minSharePriceVaultV1: bigint;
|
|
19
|
+
/** Maximum acceptable share price for V2 deposit (inflation protection, in RAY). */
|
|
20
|
+
maxSharePriceVaultV2: bigint;
|
|
21
|
+
/** Receives the V2 vault shares. */
|
|
22
|
+
recipient: Address;
|
|
23
|
+
/** Pre-signed permit/permit2 approval for V1 share transfer. */
|
|
24
|
+
requirementSignature?: RequirementSignature;
|
|
25
|
+
};
|
|
26
|
+
metadata?: Metadata;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Prepares an atomic full-migration transaction from VaultV1 to VaultV2.
|
|
30
|
+
*
|
|
31
|
+
* Routed through bundler3: transfers V1 shares to `GeneralAdapter1` (via `erc20TransferFrom` or
|
|
32
|
+
* permit/permit2), redeems them via `erc4626Redeem` (GA1 redeems its own shares — no allowance
|
|
33
|
+
* check), then deposits the resulting assets into V2 via `erc4626Deposit`. All operations
|
|
34
|
+
* execute atomically in a single transaction.
|
|
35
|
+
*
|
|
36
|
+
* Prerequisite: the user must either approve `GeneralAdapter1` to spend their V1 vault shares
|
|
37
|
+
* (classic approve) or provide a pre-signed permit/permit2 via `requirementSignature`. Use
|
|
38
|
+
* `getRequirements()` on the entity to resolve the appropriate approval.
|
|
39
|
+
*
|
|
40
|
+
* @param params.vault.chainId - The chain the source vault lives on (used to resolve bundler
|
|
41
|
+
* addresses).
|
|
42
|
+
* @param params.vault.address - The source VaultV1 (MetaMorpho) address.
|
|
43
|
+
* @param params.vault.asset - The underlying asset of the source V1 vault.
|
|
44
|
+
* @param params.args.targetVault - The target VaultV2 address.
|
|
45
|
+
* @param params.args.targetAsset - The underlying asset of the target V2 vault. Must equal
|
|
46
|
+
* `vault.asset`.
|
|
47
|
+
* @param params.args.shares - Number of V1 shares to migrate.
|
|
48
|
+
* @param params.args.minSharePriceVaultV1 - Minimum V1 share price in RAY (slippage protection
|
|
49
|
+
* for the redeem leg).
|
|
50
|
+
* @param params.args.maxSharePriceVaultV2 - Maximum V2 share price in RAY (inflation protection
|
|
51
|
+
* for the deposit leg).
|
|
52
|
+
* @param params.args.recipient - Address that receives the V2 vault shares.
|
|
53
|
+
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 for the V1 share
|
|
54
|
+
* transfer. When absent, the bundle falls back to a plain `erc20TransferFrom`.
|
|
55
|
+
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
56
|
+
* @returns A deep-frozen `Transaction<VaultV1MigrateToV2Action>` with `to`, `value`, `data`, and
|
|
57
|
+
* the typed `action` discriminator the simulation layer consumes.
|
|
58
|
+
* @throws {VaultAssetMismatchError} when `targetAsset` differs from `vault.asset`.
|
|
59
|
+
* @throws {NonPositiveSharesAmountError} when `shares <= 0n`.
|
|
60
|
+
* @throws {NegativeMinSharePriceError} when `minSharePriceVaultV1 < 0n`.
|
|
61
|
+
* @throws {NonPositiveMaxSharePriceError} when `maxSharePriceVaultV2 <= 0n`.
|
|
62
|
+
* @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
63
|
+
* is provided and the signed asset differs from `vault.address` (the V1 share token).
|
|
64
|
+
* @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
65
|
+
* is provided and the signed amount differs from `args.shares`.
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { vaultV1MigrateToV2 } from "@morpho-org/morpho-sdk";
|
|
69
|
+
*
|
|
70
|
+
* const tx = vaultV1MigrateToV2({
|
|
71
|
+
* vault: { chainId: 1, address: sourceVault, asset: USDC },
|
|
72
|
+
* args: {
|
|
73
|
+
* targetVault,
|
|
74
|
+
* targetAsset: USDC,
|
|
75
|
+
* shares: 1_000_000n,
|
|
76
|
+
* minSharePriceVaultV1: 0n, // disables redeem-leg slippage protection — production code should compute from source vault state + slippage tolerance
|
|
77
|
+
* maxSharePriceVaultV2: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
78
|
+
* recipient,
|
|
79
|
+
* },
|
|
80
|
+
* });
|
|
81
|
+
* // tx satisfies Readonly<Transaction<VaultV1MigrateToV2Action>>
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare const vaultV1MigrateToV2: ({ vault: { chainId, address: sourceVault, asset: sourceAsset }, args: { targetVault, targetAsset, shares, minSharePriceVaultV1, maxSharePriceVaultV2, recipient, requirementSignature, }, metadata, }: VaultV1MigrateToV2Params) => Readonly<Transaction<VaultV1MigrateToV2Action>>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { BundlerAction } from "@morpho-org/bundler-sdk-viem";
|
|
3
|
+
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { isAddressEqual, maxUint256 } from "viem";
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
|
+
import { NegativeMinSharePriceError, NonPositiveMaxSharePriceError, NonPositiveSharesAmountError, VaultAssetMismatchError, } from "../../types/index.js";
|
|
7
|
+
import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
|
|
8
|
+
/**
|
|
9
|
+
* Prepares an atomic full-migration transaction from VaultV1 to VaultV2.
|
|
10
|
+
*
|
|
11
|
+
* Routed through bundler3: transfers V1 shares to `GeneralAdapter1` (via `erc20TransferFrom` or
|
|
12
|
+
* permit/permit2), redeems them via `erc4626Redeem` (GA1 redeems its own shares — no allowance
|
|
13
|
+
* check), then deposits the resulting assets into V2 via `erc4626Deposit`. All operations
|
|
14
|
+
* execute atomically in a single transaction.
|
|
15
|
+
*
|
|
16
|
+
* Prerequisite: the user must either approve `GeneralAdapter1` to spend their V1 vault shares
|
|
17
|
+
* (classic approve) or provide a pre-signed permit/permit2 via `requirementSignature`. Use
|
|
18
|
+
* `getRequirements()` on the entity to resolve the appropriate approval.
|
|
19
|
+
*
|
|
20
|
+
* @param params.vault.chainId - The chain the source vault lives on (used to resolve bundler
|
|
21
|
+
* addresses).
|
|
22
|
+
* @param params.vault.address - The source VaultV1 (MetaMorpho) address.
|
|
23
|
+
* @param params.vault.asset - The underlying asset of the source V1 vault.
|
|
24
|
+
* @param params.args.targetVault - The target VaultV2 address.
|
|
25
|
+
* @param params.args.targetAsset - The underlying asset of the target V2 vault. Must equal
|
|
26
|
+
* `vault.asset`.
|
|
27
|
+
* @param params.args.shares - Number of V1 shares to migrate.
|
|
28
|
+
* @param params.args.minSharePriceVaultV1 - Minimum V1 share price in RAY (slippage protection
|
|
29
|
+
* for the redeem leg).
|
|
30
|
+
* @param params.args.maxSharePriceVaultV2 - Maximum V2 share price in RAY (inflation protection
|
|
31
|
+
* for the deposit leg).
|
|
32
|
+
* @param params.args.recipient - Address that receives the V2 vault shares.
|
|
33
|
+
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 for the V1 share
|
|
34
|
+
* transfer. When absent, the bundle falls back to a plain `erc20TransferFrom`.
|
|
35
|
+
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
36
|
+
* @returns A deep-frozen `Transaction<VaultV1MigrateToV2Action>` with `to`, `value`, `data`, and
|
|
37
|
+
* the typed `action` discriminator the simulation layer consumes.
|
|
38
|
+
* @throws {VaultAssetMismatchError} when `targetAsset` differs from `vault.asset`.
|
|
39
|
+
* @throws {NonPositiveSharesAmountError} when `shares <= 0n`.
|
|
40
|
+
* @throws {NegativeMinSharePriceError} when `minSharePriceVaultV1 < 0n`.
|
|
41
|
+
* @throws {NonPositiveMaxSharePriceError} when `maxSharePriceVaultV2 <= 0n`.
|
|
42
|
+
* @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
43
|
+
* is provided and the signed asset differs from `vault.address` (the V1 share token).
|
|
44
|
+
* @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
45
|
+
* is provided and the signed amount differs from `args.shares`.
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { vaultV1MigrateToV2 } from "@morpho-org/morpho-sdk";
|
|
49
|
+
*
|
|
50
|
+
* const tx = vaultV1MigrateToV2({
|
|
51
|
+
* vault: { chainId: 1, address: sourceVault, asset: USDC },
|
|
52
|
+
* args: {
|
|
53
|
+
* targetVault,
|
|
54
|
+
* targetAsset: USDC,
|
|
55
|
+
* shares: 1_000_000n,
|
|
56
|
+
* minSharePriceVaultV1: 0n, // disables redeem-leg slippage protection — production code should compute from source vault state + slippage tolerance
|
|
57
|
+
* maxSharePriceVaultV2: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
58
|
+
* recipient,
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
* // tx satisfies Readonly<Transaction<VaultV1MigrateToV2Action>>
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export const vaultV1MigrateToV2 = ({ vault: { chainId, address: sourceVault, asset: sourceAsset }, args: { targetVault, targetAsset, shares, minSharePriceVaultV1, maxSharePriceVaultV2, recipient, requirementSignature, }, metadata, }) => {
|
|
65
|
+
// Both bundle legs use maxUint256: an asset mismatch leaves the redeemed
|
|
66
|
+
// source asset stranded on GA1 while the user receives only dust shares.
|
|
67
|
+
if (!isAddressEqual(sourceAsset, targetAsset)) {
|
|
68
|
+
throw new VaultAssetMismatchError(sourceAsset, targetAsset);
|
|
69
|
+
}
|
|
70
|
+
if (shares <= 0n) {
|
|
71
|
+
throw new NonPositiveSharesAmountError(sourceVault);
|
|
72
|
+
}
|
|
73
|
+
if (minSharePriceVaultV1 < 0n) {
|
|
74
|
+
throw new NegativeMinSharePriceError(sourceVault);
|
|
75
|
+
}
|
|
76
|
+
if (maxSharePriceVaultV2 <= 0n) {
|
|
77
|
+
throw new NonPositiveMaxSharePriceError(targetVault);
|
|
78
|
+
}
|
|
79
|
+
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
80
|
+
const actions = [];
|
|
81
|
+
// Transfer V1 shares from user to GA1.
|
|
82
|
+
// With a signature: permit/permit2 + transferFrom for the signed amount.
|
|
83
|
+
// Without: plain erc20TransferFrom for the specified shares amount.
|
|
84
|
+
if (requirementSignature) {
|
|
85
|
+
actions.push(...getRequirementsAction({
|
|
86
|
+
chainId,
|
|
87
|
+
asset: sourceVault,
|
|
88
|
+
amount: shares,
|
|
89
|
+
requirementSignature,
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
actions.push({
|
|
94
|
+
type: "erc20TransferFrom",
|
|
95
|
+
args: [sourceVault, shares, generalAdapter1, false /* skipRevert */],
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// GA1 redeems its own shares (owner = GA1, no allowance check).
|
|
99
|
+
actions.push({
|
|
100
|
+
type: "erc4626Redeem",
|
|
101
|
+
args: [
|
|
102
|
+
sourceVault,
|
|
103
|
+
maxUint256,
|
|
104
|
+
minSharePriceVaultV1,
|
|
105
|
+
generalAdapter1,
|
|
106
|
+
generalAdapter1,
|
|
107
|
+
false /* skipRevert */,
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
// Deposit all resulting assets into V2.
|
|
111
|
+
actions.push({
|
|
112
|
+
type: "erc4626Deposit",
|
|
113
|
+
args: [
|
|
114
|
+
targetVault,
|
|
115
|
+
maxUint256,
|
|
116
|
+
maxSharePriceVaultV2,
|
|
117
|
+
recipient,
|
|
118
|
+
false /* skipRevert */,
|
|
119
|
+
],
|
|
120
|
+
});
|
|
121
|
+
let tx = BundlerAction.encodeBundle(chainId, actions);
|
|
122
|
+
if (metadata) {
|
|
123
|
+
tx = addTransactionMetadata(tx, metadata);
|
|
124
|
+
}
|
|
125
|
+
return deepFreeze({
|
|
126
|
+
...tx,
|
|
127
|
+
action: {
|
|
128
|
+
type: "vaultV1MigrateToV2",
|
|
129
|
+
args: {
|
|
130
|
+
sourceVault,
|
|
131
|
+
targetVault,
|
|
132
|
+
shares,
|
|
133
|
+
minSharePriceVaultV1,
|
|
134
|
+
maxSharePriceVaultV2,
|
|
135
|
+
recipient,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
};
|