@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,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1SupplyCollateralBorrow = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const index_js_2 = require("../../types/index.js");
|
|
9
|
+
const getRequirementsAction_js_1 = require("../requirements/getRequirementsAction.js");
|
|
10
|
+
const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
11
|
+
/**
|
|
12
|
+
* Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
|
|
13
|
+
*
|
|
14
|
+
* Routed through bundler3: collateral transfer → `morphoSupplyCollateral` → optional
|
|
15
|
+
* `reallocateTo` calls → `morphoBorrow`. When `nativeAmount > 0`, native ETH is wrapped via
|
|
16
|
+
* `GeneralAdapter1.wrapNative()` before the supply leg.
|
|
17
|
+
*
|
|
18
|
+
* Prerequisite: `GeneralAdapter1` must be authorized on Morpho to borrow on behalf of the user.
|
|
19
|
+
* Use `getRequirements()` on the entity to check and obtain the authorization transaction.
|
|
20
|
+
*
|
|
21
|
+
* Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver. No dust left
|
|
22
|
+
* in bundler or adapter.
|
|
23
|
+
*
|
|
24
|
+
* @param params.market.chainId - The chain the market lives on.
|
|
25
|
+
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
26
|
+
* @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
|
|
27
|
+
* `nativeAmount` must be positive. Defaults to `0n`.
|
|
28
|
+
* @param params.args.borrowAmount - Loan asset amount to borrow.
|
|
29
|
+
* @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
|
|
30
|
+
* @param params.args.receiver - Address that receives the borrowed assets.
|
|
31
|
+
* @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
|
|
32
|
+
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
33
|
+
* collateral transfer.
|
|
34
|
+
* @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
|
|
35
|
+
* collateral supply. Requires the collateral token to be the chain's wNative.
|
|
36
|
+
* @param params.args.reallocations - Optional vault reallocations to execute between the supply
|
|
37
|
+
* and borrow legs, computed by the entity layer.
|
|
38
|
+
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
39
|
+
* @returns A deep-frozen `Transaction<MarketV1SupplyCollateralBorrowAction>` with `to`, `value`,
|
|
40
|
+
* `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
41
|
+
* @throws {NonPositiveAssetAmountError} when `amount < 0n`.
|
|
42
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
43
|
+
* @throws {NonPositiveBorrowAmountError} when `borrowAmount <= 0n`.
|
|
44
|
+
* @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`.
|
|
45
|
+
* @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
|
|
46
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
|
|
47
|
+
* @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
|
|
48
|
+
* token is not the chain's wNative.
|
|
49
|
+
* @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
50
|
+
* is provided and the signed asset differs from `marketParams.collateralToken`.
|
|
51
|
+
* @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
|
|
52
|
+
* is provided and the signed amount differs from `args.amount`.
|
|
53
|
+
* @throws {NegativeReallocationFeeError} from `buildReallocationActions` when
|
|
54
|
+
* `reallocations` is non-empty and any `reallocation.fee < 0n`.
|
|
55
|
+
* @throws {EmptyReallocationWithdrawalsError} from `buildReallocationActions` when any
|
|
56
|
+
* `reallocation.withdrawals` is empty.
|
|
57
|
+
* @throws {NonPositiveReallocationAmountError} from `buildReallocationActions` when any
|
|
58
|
+
* `reallocation.withdrawals[i].amount <= 0n`.
|
|
59
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} from `buildReallocationActions` when any
|
|
60
|
+
* reallocation withdrawal references the target market.
|
|
61
|
+
* @throws {UnsortedReallocationWithdrawalsError} from `buildReallocationActions` when
|
|
62
|
+
* reallocation withdrawals are not strictly sorted by market id.
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { marketV1SupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
|
|
66
|
+
*
|
|
67
|
+
* const tx = marketV1SupplyCollateralBorrow({
|
|
68
|
+
* market: { chainId: 1, marketParams },
|
|
69
|
+
* args: {
|
|
70
|
+
* amount: 1_000_000_000_000_000_000n,
|
|
71
|
+
* borrowAmount: 500_000_000n,
|
|
72
|
+
* onBehalf: borrower,
|
|
73
|
+
* receiver: borrower,
|
|
74
|
+
* minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
|
|
75
|
+
* },
|
|
76
|
+
* });
|
|
77
|
+
* // tx satisfies Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: { amount = 0n, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }) => {
|
|
81
|
+
if (amount < 0n) {
|
|
82
|
+
throw new index_js_2.NonPositiveAssetAmountError(marketParams.collateralToken);
|
|
83
|
+
}
|
|
84
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
85
|
+
throw new index_js_2.NegativeNativeAmountError(nativeAmount);
|
|
86
|
+
}
|
|
87
|
+
if (borrowAmount <= 0n) {
|
|
88
|
+
throw new index_js_2.NonPositiveBorrowAmountError(marketParams.id);
|
|
89
|
+
}
|
|
90
|
+
if (minSharePrice < 0n) {
|
|
91
|
+
throw new index_js_2.NonPositiveMinBorrowSharePriceError(marketParams.id);
|
|
92
|
+
}
|
|
93
|
+
const totalCollateral = amount + (nativeAmount ?? 0n);
|
|
94
|
+
if (totalCollateral === 0n) {
|
|
95
|
+
throw new index_js_2.ZeroCollateralAmountError(marketParams.id);
|
|
96
|
+
}
|
|
97
|
+
const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
98
|
+
const actions = [];
|
|
99
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
100
|
+
(0, index_js_1.validateNativeCollateral)(chainId, marketParams.collateralToken);
|
|
101
|
+
actions.push({
|
|
102
|
+
type: "nativeTransfer",
|
|
103
|
+
args: [bundler3, generalAdapter1, nativeAmount, false],
|
|
104
|
+
}, {
|
|
105
|
+
type: "wrapNative",
|
|
106
|
+
args: [nativeAmount, generalAdapter1, false],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (amount > 0n) {
|
|
110
|
+
if (requirementSignature) {
|
|
111
|
+
actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
|
|
112
|
+
chainId,
|
|
113
|
+
asset: marketParams.collateralToken,
|
|
114
|
+
amount,
|
|
115
|
+
requirementSignature,
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
actions.push({
|
|
120
|
+
type: "erc20TransferFrom",
|
|
121
|
+
args: [marketParams.collateralToken, amount, generalAdapter1, false],
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
actions.push({
|
|
126
|
+
type: "morphoSupplyCollateral",
|
|
127
|
+
args: [marketParams, totalCollateral, onBehalf, [], false],
|
|
128
|
+
});
|
|
129
|
+
let reallocationFee = 0n;
|
|
130
|
+
if (reallocations && reallocations.length > 0) {
|
|
131
|
+
const result = (0, buildReallocationActions_js_1.buildReallocationActions)(reallocations, marketParams);
|
|
132
|
+
actions.push(...result.actions);
|
|
133
|
+
reallocationFee = result.fee;
|
|
134
|
+
}
|
|
135
|
+
actions.push({
|
|
136
|
+
type: "morphoBorrow",
|
|
137
|
+
args: [marketParams, borrowAmount, 0n, minSharePrice, receiver, false],
|
|
138
|
+
});
|
|
139
|
+
let tx = {
|
|
140
|
+
...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
|
|
141
|
+
value: (nativeAmount ?? 0n) + reallocationFee,
|
|
142
|
+
};
|
|
143
|
+
if (metadata) {
|
|
144
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, metadata);
|
|
145
|
+
}
|
|
146
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
147
|
+
...tx,
|
|
148
|
+
action: {
|
|
149
|
+
type: "marketV1SupplyCollateralBorrow",
|
|
150
|
+
args: {
|
|
151
|
+
market: marketParams.id,
|
|
152
|
+
collateralAmount: totalCollateral,
|
|
153
|
+
borrowAmount,
|
|
154
|
+
minSharePrice,
|
|
155
|
+
onBehalf,
|
|
156
|
+
receiver,
|
|
157
|
+
nativeAmount,
|
|
158
|
+
reallocationFee,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
exports.marketV1SupplyCollateralBorrow = marketV1SupplyCollateralBorrow;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type MarketV1WithdrawCollateralAction, type Metadata, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for {@link marketV1WithdrawCollateral}. */
|
|
5
|
+
export interface MarketV1WithdrawCollateralParams {
|
|
6
|
+
market: {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly marketParams: MarketParams;
|
|
9
|
+
};
|
|
10
|
+
args: {
|
|
11
|
+
amount: bigint;
|
|
12
|
+
onBehalf: Address;
|
|
13
|
+
receiver: Address;
|
|
14
|
+
};
|
|
15
|
+
metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares a withdraw-collateral transaction for a Morpho Blue market.
|
|
19
|
+
*
|
|
20
|
+
* Direct call to `Morpho.withdrawCollateral` — no bundler needed. Collateral flows out of Morpho,
|
|
21
|
+
* so there is no inflation-attack surface requiring the bundler.
|
|
22
|
+
*
|
|
23
|
+
* The caller (`msg.sender`) must be `onBehalf` or be authorized by them on Morpho.
|
|
24
|
+
*
|
|
25
|
+
* @param params.market.chainId - The chain the market lives on.
|
|
26
|
+
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
27
|
+
* @param params.args.amount - Amount of collateral to withdraw.
|
|
28
|
+
* @param params.args.onBehalf - Address whose Morpho position the collateral is withdrawn from.
|
|
29
|
+
* @param params.args.receiver - Address that receives the withdrawn collateral.
|
|
30
|
+
* @param params.metadata - Optional analytics metadata attached to the transaction.
|
|
31
|
+
* @returns A deep-frozen `Transaction<MarketV1WithdrawCollateralAction>` with `to`, `value`,
|
|
32
|
+
* `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
33
|
+
* @throws {NonPositiveWithdrawCollateralAmountError} when `amount <= 0n`.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { marketV1WithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
37
|
+
*
|
|
38
|
+
* const tx = marketV1WithdrawCollateral({
|
|
39
|
+
* market: { chainId: 1, marketParams },
|
|
40
|
+
* args: {
|
|
41
|
+
* amount: 1_000_000_000_000_000_000n,
|
|
42
|
+
* onBehalf: borrower,
|
|
43
|
+
* receiver: borrower,
|
|
44
|
+
* },
|
|
45
|
+
* });
|
|
46
|
+
* // tx satisfies Readonly<Transaction<MarketV1WithdrawCollateralAction>>
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const marketV1WithdrawCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }: MarketV1WithdrawCollateralParams) => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1WithdrawCollateral = 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("../../helpers/index.js");
|
|
9
|
+
const index_js_2 = require("../../types/index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Prepares a withdraw-collateral transaction for a Morpho Blue market.
|
|
12
|
+
*
|
|
13
|
+
* Direct call to `Morpho.withdrawCollateral` — no bundler needed. Collateral flows out of Morpho,
|
|
14
|
+
* so there is no inflation-attack surface requiring the bundler.
|
|
15
|
+
*
|
|
16
|
+
* The caller (`msg.sender`) must be `onBehalf` or be authorized by them on Morpho.
|
|
17
|
+
*
|
|
18
|
+
* @param params.market.chainId - The chain the market lives on.
|
|
19
|
+
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
20
|
+
* @param params.args.amount - Amount of collateral to withdraw.
|
|
21
|
+
* @param params.args.onBehalf - Address whose Morpho position the collateral is withdrawn from.
|
|
22
|
+
* @param params.args.receiver - Address that receives the withdrawn collateral.
|
|
23
|
+
* @param params.metadata - Optional analytics metadata attached to the transaction.
|
|
24
|
+
* @returns A deep-frozen `Transaction<MarketV1WithdrawCollateralAction>` with `to`, `value`,
|
|
25
|
+
* `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
26
|
+
* @throws {NonPositiveWithdrawCollateralAmountError} when `amount <= 0n`.
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { marketV1WithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
30
|
+
*
|
|
31
|
+
* const tx = marketV1WithdrawCollateral({
|
|
32
|
+
* market: { chainId: 1, marketParams },
|
|
33
|
+
* args: {
|
|
34
|
+
* amount: 1_000_000_000_000_000_000n,
|
|
35
|
+
* onBehalf: borrower,
|
|
36
|
+
* receiver: borrower,
|
|
37
|
+
* },
|
|
38
|
+
* });
|
|
39
|
+
* // tx satisfies Readonly<Transaction<MarketV1WithdrawCollateralAction>>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const marketV1WithdrawCollateral = ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }) => {
|
|
43
|
+
if (amount <= 0n) {
|
|
44
|
+
throw new index_js_2.NonPositiveWithdrawCollateralAmountError(marketParams.id);
|
|
45
|
+
}
|
|
46
|
+
const { morpho } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
47
|
+
let tx = {
|
|
48
|
+
to: morpho,
|
|
49
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
50
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
51
|
+
functionName: "withdrawCollateral",
|
|
52
|
+
args: [marketParams, amount, onBehalf, receiver],
|
|
53
|
+
}),
|
|
54
|
+
value: 0n,
|
|
55
|
+
};
|
|
56
|
+
if (metadata) {
|
|
57
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, metadata);
|
|
58
|
+
}
|
|
59
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
60
|
+
...tx,
|
|
61
|
+
action: {
|
|
62
|
+
type: "marketV1WithdrawCollateral",
|
|
63
|
+
args: {
|
|
64
|
+
market: marketParams.id,
|
|
65
|
+
amount,
|
|
66
|
+
onBehalf,
|
|
67
|
+
receiver,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
exports.marketV1WithdrawCollateral = marketV1WithdrawCollateral;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
|
|
3
|
+
/** Parameters for {@link encodeErc20Approval}. */
|
|
4
|
+
interface EncodeErc20ApprovalParams {
|
|
5
|
+
token: Address;
|
|
6
|
+
spender: Address;
|
|
7
|
+
amount: bigint;
|
|
8
|
+
chainId: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
|
|
12
|
+
*
|
|
13
|
+
* Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
|
|
14
|
+
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
|
|
15
|
+
*
|
|
16
|
+
* @param params - Encoding parameters.
|
|
17
|
+
* @param params.token - ERC-20 token address to approve.
|
|
18
|
+
* @param params.spender - Address granted the allowance.
|
|
19
|
+
* @param params.amount - Allowance amount before per-token cap.
|
|
20
|
+
* @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
|
|
21
|
+
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
|
|
25
|
+
*
|
|
26
|
+
* const tx = encodeErc20Approval({
|
|
27
|
+
* token: USDC,
|
|
28
|
+
* spender: generalAdapter1,
|
|
29
|
+
* amount: 1_000_000n,
|
|
30
|
+
* chainId: 1,
|
|
31
|
+
* });
|
|
32
|
+
* // tx satisfies Readonly<Transaction<ERC20ApprovalAction>>
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const encodeErc20Approval: (params: EncodeErc20ApprovalParams) => Transaction<ERC20ApprovalAction>;
|
|
36
|
+
export {};
|
|
@@ -5,6 +5,31 @@ const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
|
5
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
6
|
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
7
7
|
const viem_1 = require("viem");
|
|
8
|
+
/**
|
|
9
|
+
* Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
|
|
10
|
+
*
|
|
11
|
+
* Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
|
|
12
|
+
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
|
|
13
|
+
*
|
|
14
|
+
* @param params - Encoding parameters.
|
|
15
|
+
* @param params.token - ERC-20 token address to approve.
|
|
16
|
+
* @param params.spender - Address granted the allowance.
|
|
17
|
+
* @param params.amount - Allowance amount before per-token cap.
|
|
18
|
+
* @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
|
|
19
|
+
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
|
|
23
|
+
*
|
|
24
|
+
* const tx = encodeErc20Approval({
|
|
25
|
+
* token: USDC,
|
|
26
|
+
* spender: generalAdapter1,
|
|
27
|
+
* amount: 1_000_000n,
|
|
28
|
+
* chainId: 1,
|
|
29
|
+
* });
|
|
30
|
+
* // tx satisfies Readonly<Transaction<ERC20ApprovalAction>>
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
8
33
|
const encodeErc20Approval = (params) => {
|
|
9
34
|
const { token, spender, amount, chainId } = params;
|
|
10
35
|
const amountValue = blue_sdk_1.MathLib.min(amount, simulation_sdk_1.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import { type Requirement } from "../../../types/index.js";
|
|
4
|
+
/** Parameters for {@link encodeErc20Permit}. */
|
|
5
|
+
interface EncodeErc20PermitParams {
|
|
6
|
+
token: Address;
|
|
7
|
+
spender: Address;
|
|
8
|
+
amount: bigint;
|
|
9
|
+
chainId: number;
|
|
10
|
+
nonce: bigint;
|
|
11
|
+
supportDeployless?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
|
|
15
|
+
* `token`.
|
|
16
|
+
*
|
|
17
|
+
* Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
|
|
18
|
+
* signature, verifies it against the connected account, and returns a `RequirementSignature`
|
|
19
|
+
* the bundler action helpers can consume. Deadline defaults to two hours from `Time.timestamp()`.
|
|
20
|
+
*
|
|
21
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
22
|
+
* @param params - Permit encoding parameters.
|
|
23
|
+
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
24
|
+
* @param params.spender - Address that will be granted the permit allowance.
|
|
25
|
+
* @param params.amount - Permit allowance amount.
|
|
26
|
+
* @param params.chainId - Target chain id.
|
|
27
|
+
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
28
|
+
* @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
|
|
29
|
+
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
30
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
31
|
+
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
32
|
+
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
33
|
+
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { createWalletClient, http } from "viem";
|
|
37
|
+
* import { mainnet } from "viem/chains";
|
|
38
|
+
* import { encodeErc20Permit } from "@morpho-org/morpho-sdk";
|
|
39
|
+
*
|
|
40
|
+
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
41
|
+
* const requirement = await encodeErc20Permit(client, {
|
|
42
|
+
* token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
|
|
43
|
+
* spender: generalAdapter1,
|
|
44
|
+
* amount: 1_000_000n,
|
|
45
|
+
* chainId: 1,
|
|
46
|
+
* nonce: 0n,
|
|
47
|
+
* });
|
|
48
|
+
* // requirement satisfies Requirement
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare const encodeErc20Permit: (viemClient: Client, params: EncodeErc20PermitParams) => Promise<Requirement>;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeErc20Permit = void 0;
|
|
4
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const actions_1 = require("viem/actions");
|
|
8
|
+
const index_js_1 = require("../../../types/index.js");
|
|
9
|
+
/**
|
|
10
|
+
* Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
|
|
11
|
+
* `token`.
|
|
12
|
+
*
|
|
13
|
+
* Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
|
|
14
|
+
* signature, verifies it against the connected account, and returns a `RequirementSignature`
|
|
15
|
+
* the bundler action helpers can consume. Deadline defaults to two hours from `Time.timestamp()`.
|
|
16
|
+
*
|
|
17
|
+
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
18
|
+
* @param params - Permit encoding parameters.
|
|
19
|
+
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
20
|
+
* @param params.spender - Address that will be granted the permit allowance.
|
|
21
|
+
* @param params.amount - Permit allowance amount.
|
|
22
|
+
* @param params.chainId - Target chain id.
|
|
23
|
+
* @param params.nonce - The user's current EIP-2612 nonce on `token`.
|
|
24
|
+
* @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
|
|
25
|
+
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
26
|
+
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
27
|
+
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
28
|
+
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
29
|
+
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { createWalletClient, http } from "viem";
|
|
33
|
+
* import { mainnet } from "viem/chains";
|
|
34
|
+
* import { encodeErc20Permit } from "@morpho-org/morpho-sdk";
|
|
35
|
+
*
|
|
36
|
+
* const client = createWalletClient({ chain: mainnet, transport: http() });
|
|
37
|
+
* const requirement = await encodeErc20Permit(client, {
|
|
38
|
+
* token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
|
|
39
|
+
* spender: generalAdapter1,
|
|
40
|
+
* amount: 1_000_000n,
|
|
41
|
+
* chainId: 1,
|
|
42
|
+
* nonce: 0n,
|
|
43
|
+
* });
|
|
44
|
+
* // requirement satisfies Requirement
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
const encodeErc20Permit = async (viemClient, params) => {
|
|
48
|
+
const { token, spender, amount, chainId, nonce, supportDeployless } = params;
|
|
49
|
+
if (viemClient.chain?.id !== chainId) {
|
|
50
|
+
throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
51
|
+
}
|
|
52
|
+
const now = morpho_ts_1.Time.timestamp();
|
|
53
|
+
const deadline = now + morpho_ts_1.Time.s.from.h(2n);
|
|
54
|
+
const tokenData = await (0, blue_sdk_viem_1.fetchToken)(token, viemClient, {
|
|
55
|
+
deployless: supportDeployless,
|
|
56
|
+
});
|
|
57
|
+
const action = {
|
|
58
|
+
type: "permit",
|
|
59
|
+
args: {
|
|
60
|
+
spender,
|
|
61
|
+
amount,
|
|
62
|
+
deadline,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
action,
|
|
67
|
+
async sign(client, userAddress) {
|
|
68
|
+
if (!client.account?.address) {
|
|
69
|
+
throw new index_js_1.MissingClientPropertyError("client.account.address");
|
|
70
|
+
}
|
|
71
|
+
if (client.account.address !== userAddress) {
|
|
72
|
+
throw new index_js_1.AddressMismatchError(client.account.address, userAddress);
|
|
73
|
+
}
|
|
74
|
+
const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
|
|
75
|
+
erc20: tokenData,
|
|
76
|
+
owner: userAddress,
|
|
77
|
+
spender,
|
|
78
|
+
allowance: amount,
|
|
79
|
+
nonce,
|
|
80
|
+
deadline,
|
|
81
|
+
}, chainId);
|
|
82
|
+
const signature = await (0, actions_1.signTypedData)(client, {
|
|
83
|
+
...typedData,
|
|
84
|
+
account: client.account,
|
|
85
|
+
});
|
|
86
|
+
const isValid = await (0, viem_1.verifyTypedData)({
|
|
87
|
+
...typedData,
|
|
88
|
+
address: userAddress, // Verify against the permit's owner.
|
|
89
|
+
signature,
|
|
90
|
+
});
|
|
91
|
+
if (!isValid) {
|
|
92
|
+
throw new index_js_1.InvalidSignatureError();
|
|
93
|
+
}
|
|
94
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
95
|
+
args: {
|
|
96
|
+
owner: userAddress,
|
|
97
|
+
signature,
|
|
98
|
+
deadline,
|
|
99
|
+
amount,
|
|
100
|
+
asset: token,
|
|
101
|
+
nonce,
|
|
102
|
+
},
|
|
103
|
+
action,
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
exports.encodeErc20Permit = encodeErc20Permit;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Requirement } from "../../../types/index.js";
|
|
3
|
+
/** Parameters for {@link encodeErc20Permit2}. */
|
|
4
|
+
interface EncodeErc20Permit2Params {
|
|
5
|
+
token: Address;
|
|
6
|
+
amount: bigint;
|
|
7
|
+
chainId: number;
|
|
8
|
+
nonce: bigint;
|
|
9
|
+
expiration: bigint;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
|
|
13
|
+
* `token` via the Permit2 contract.
|
|
14
|
+
*
|
|
15
|
+
* The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
|
|
16
|
+
* address — otherwise the resulting signature could be reused independently of the Morpho
|
|
17
|
+
* bundle. Deadline defaults to two hours from `Time.timestamp()`.
|
|
18
|
+
*
|
|
19
|
+
* @param params - Permit2 encoding parameters.
|
|
20
|
+
* @param params.token - ERC-20 token address.
|
|
21
|
+
* @param params.amount - Permit2 allowance amount (per-call).
|
|
22
|
+
* @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
|
|
23
|
+
* @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
|
|
24
|
+
* @param params.expiration - Permit2-managed allowance expiration timestamp.
|
|
25
|
+
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
26
|
+
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
27
|
+
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
28
|
+
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
|
|
32
|
+
*
|
|
33
|
+
* const requirement = encodeErc20Permit2({
|
|
34
|
+
* token: USDC,
|
|
35
|
+
* amount: 1_000_000n,
|
|
36
|
+
* chainId: 1,
|
|
37
|
+
* nonce: 0n,
|
|
38
|
+
* expiration: 281_474_976_710_655n, // MAX_UINT_48 (2^48 - 1, effectively indefinite)
|
|
39
|
+
* });
|
|
40
|
+
* // requirement satisfies Requirement
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare const encodeErc20Permit2: (params: EncodeErc20Permit2Params) => Requirement;
|
|
44
|
+
export {};
|
|
@@ -6,7 +6,39 @@ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
7
|
const viem_1 = require("viem");
|
|
8
8
|
const actions_1 = require("viem/actions");
|
|
9
|
-
const
|
|
9
|
+
const index_js_1 = require("../../../types/index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
|
|
12
|
+
* `token` via the Permit2 contract.
|
|
13
|
+
*
|
|
14
|
+
* The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
|
|
15
|
+
* address — otherwise the resulting signature could be reused independently of the Morpho
|
|
16
|
+
* bundle. Deadline defaults to two hours from `Time.timestamp()`.
|
|
17
|
+
*
|
|
18
|
+
* @param params - Permit2 encoding parameters.
|
|
19
|
+
* @param params.token - ERC-20 token address.
|
|
20
|
+
* @param params.amount - Permit2 allowance amount (per-call).
|
|
21
|
+
* @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
|
|
22
|
+
* @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
|
|
23
|
+
* @param params.expiration - Permit2-managed allowance expiration timestamp.
|
|
24
|
+
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
25
|
+
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
26
|
+
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
27
|
+
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
|
|
31
|
+
*
|
|
32
|
+
* const requirement = encodeErc20Permit2({
|
|
33
|
+
* token: USDC,
|
|
34
|
+
* amount: 1_000_000n,
|
|
35
|
+
* chainId: 1,
|
|
36
|
+
* nonce: 0n,
|
|
37
|
+
* expiration: 281_474_976_710_655n, // MAX_UINT_48 (2^48 - 1, effectively indefinite)
|
|
38
|
+
* });
|
|
39
|
+
* // requirement satisfies Requirement
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
10
42
|
const encodeErc20Permit2 = (params) => {
|
|
11
43
|
const { token, amount, chainId, nonce, expiration = blue_sdk_1.MathLib.MAX_UINT_48, } = params;
|
|
12
44
|
const now = morpho_ts_1.Time.timestamp();
|
|
@@ -25,10 +57,10 @@ const encodeErc20Permit2 = (params) => {
|
|
|
25
57
|
action,
|
|
26
58
|
async sign(client, userAddress) {
|
|
27
59
|
if (!client.account?.address) {
|
|
28
|
-
throw new
|
|
60
|
+
throw new index_js_1.MissingClientPropertyError("client.account.address");
|
|
29
61
|
}
|
|
30
62
|
if (client.account.address !== userAddress) {
|
|
31
|
-
throw new
|
|
63
|
+
throw new index_js_1.AddressMismatchError(client.account.address, userAddress);
|
|
32
64
|
}
|
|
33
65
|
const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
|
|
34
66
|
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
@@ -50,7 +82,7 @@ const encodeErc20Permit2 = (params) => {
|
|
|
50
82
|
signature,
|
|
51
83
|
});
|
|
52
84
|
if (!isValid) {
|
|
53
|
-
throw new
|
|
85
|
+
throw new index_js_1.InvalidSignatureError();
|
|
54
86
|
}
|
|
55
87
|
return (0, morpho_ts_1.deepFreeze)({
|
|
56
88
|
args: {
|
|
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./encodeErc20Approval"), exports);
|
|
18
|
-
__exportStar(require("./encodeErc20Permit"), exports);
|
|
19
|
-
__exportStar(require("./encodeErc20Permit2"), exports);
|
|
17
|
+
__exportStar(require("./encodeErc20Approval.js"), exports);
|
|
18
|
+
__exportStar(require("./encodeErc20Permit.js"), exports);
|
|
19
|
+
__exportStar(require("./encodeErc20Permit2.js"), exports);
|