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