@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,186 @@
|
|
|
1
|
+
import { DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { isAddressEqual } from "viem";
|
|
4
|
+
import { getRequirements, vaultV1Deposit, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
|
|
5
|
+
import { MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, } from "../../helpers/constant.js";
|
|
6
|
+
import { validateChainId, validateUserAddress } from "../../helpers/index.js";
|
|
7
|
+
import { ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, NativeAmountOnNonWNativeVaultError, NegativeNativeAmountError, NegativeSlippageToleranceError, NonPositiveAssetAmountError, NonPositiveSharesAmountError, VaultAddressMismatchError, VaultAssetMismatchError, } from "../../types/index.js";
|
|
8
|
+
export class MorphoVaultV1 {
|
|
9
|
+
client;
|
|
10
|
+
vault;
|
|
11
|
+
chainId;
|
|
12
|
+
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
13
|
+
constructor(client, vault, chainId) {
|
|
14
|
+
this.client = client;
|
|
15
|
+
this.vault = vault;
|
|
16
|
+
this.chainId = chainId;
|
|
17
|
+
}
|
|
18
|
+
async getData(parameters) {
|
|
19
|
+
if (this.client.viemClient.chain?.id &&
|
|
20
|
+
this.client.viemClient.chain?.id !== this.chainId) {
|
|
21
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
22
|
+
}
|
|
23
|
+
return fetchAccrualVault(this.vault, this.client.viemClient, {
|
|
24
|
+
...parameters,
|
|
25
|
+
chainId: this.chainId,
|
|
26
|
+
deployless: this.client.options.supportDeployless,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
deposit({ amount = 0n, userAddress, accrualVault, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, nativeAmount, }) {
|
|
30
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
31
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
32
|
+
}
|
|
33
|
+
if (!isAddressEqual(accrualVault.address, this.vault)) {
|
|
34
|
+
throw new VaultAddressMismatchError(this.vault, accrualVault.address);
|
|
35
|
+
}
|
|
36
|
+
if (amount < 0n) {
|
|
37
|
+
throw new NonPositiveAssetAmountError(this.vault);
|
|
38
|
+
}
|
|
39
|
+
if (nativeAmount && nativeAmount < 0n) {
|
|
40
|
+
throw new NegativeNativeAmountError(nativeAmount);
|
|
41
|
+
}
|
|
42
|
+
let wNative;
|
|
43
|
+
if (nativeAmount) {
|
|
44
|
+
({ wNative } = getChainAddresses(this.chainId));
|
|
45
|
+
if (!wNative) {
|
|
46
|
+
throw new ChainWNativeMissingError(this.chainId);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (slippageTolerance < 0n) {
|
|
50
|
+
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
51
|
+
}
|
|
52
|
+
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
53
|
+
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
54
|
+
}
|
|
55
|
+
if (nativeAmount && wNative) {
|
|
56
|
+
if (!isAddressEqual(accrualVault.asset, wNative)) {
|
|
57
|
+
throw new NativeAmountOnNonWNativeVaultError(accrualVault.asset, wNative);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
61
|
+
const shares = accrualVault.toShares(totalAssets);
|
|
62
|
+
if (shares <= 0n) {
|
|
63
|
+
throw new NonPositiveSharesAmountError(this.vault);
|
|
64
|
+
}
|
|
65
|
+
const maxSharePrice = MathLib.min(MathLib.mulDivUp(totalAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
66
|
+
return {
|
|
67
|
+
getRequirements: async (params) => await getRequirements(this.client.viemClient, {
|
|
68
|
+
address: accrualVault.asset,
|
|
69
|
+
chainId: this.chainId,
|
|
70
|
+
supportSignature: this.client.options.supportSignature,
|
|
71
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
72
|
+
useSimplePermit: params?.useSimplePermit,
|
|
73
|
+
args: {
|
|
74
|
+
amount,
|
|
75
|
+
from: userAddress,
|
|
76
|
+
},
|
|
77
|
+
}),
|
|
78
|
+
buildTx: (requirementSignature) => vaultV1Deposit({
|
|
79
|
+
vault: {
|
|
80
|
+
chainId: this.chainId,
|
|
81
|
+
address: this.vault,
|
|
82
|
+
asset: accrualVault.asset,
|
|
83
|
+
},
|
|
84
|
+
args: {
|
|
85
|
+
amount,
|
|
86
|
+
maxSharePrice,
|
|
87
|
+
recipient: userAddress,
|
|
88
|
+
requirementSignature,
|
|
89
|
+
nativeAmount,
|
|
90
|
+
},
|
|
91
|
+
metadata: this.client.options.metadata,
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
withdraw({ amount, userAddress }) {
|
|
96
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
97
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
buildTx: () => vaultV1Withdraw({
|
|
101
|
+
vault: { address: this.vault },
|
|
102
|
+
args: {
|
|
103
|
+
amount,
|
|
104
|
+
recipient: userAddress,
|
|
105
|
+
onBehalf: userAddress,
|
|
106
|
+
},
|
|
107
|
+
metadata: this.client.options.metadata,
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
redeem({ shares, userAddress }) {
|
|
112
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
113
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
buildTx: () => vaultV1Redeem({
|
|
117
|
+
vault: { address: this.vault },
|
|
118
|
+
args: {
|
|
119
|
+
shares,
|
|
120
|
+
recipient: userAddress,
|
|
121
|
+
onBehalf: userAddress,
|
|
122
|
+
},
|
|
123
|
+
metadata: this.client.options.metadata,
|
|
124
|
+
}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
migrateToV2({ userAddress, sourceVault, targetVault, shares, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, }) {
|
|
128
|
+
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
129
|
+
validateUserAddress(this.client.viemClient.account?.address, userAddress);
|
|
130
|
+
if (!isAddressEqual(sourceVault.address, this.vault)) {
|
|
131
|
+
throw new VaultAddressMismatchError(this.vault, sourceVault.address);
|
|
132
|
+
}
|
|
133
|
+
if (!isAddressEqual(sourceVault.asset, targetVault.asset)) {
|
|
134
|
+
throw new VaultAssetMismatchError(sourceVault.asset, targetVault.asset);
|
|
135
|
+
}
|
|
136
|
+
if (shares <= 0n) {
|
|
137
|
+
throw new NonPositiveSharesAmountError(this.vault);
|
|
138
|
+
}
|
|
139
|
+
if (slippageTolerance < 0n) {
|
|
140
|
+
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
141
|
+
}
|
|
142
|
+
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
143
|
+
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
144
|
+
}
|
|
145
|
+
// Compute minSharePriceVaultV1 for V1 redeem (slippage downward)
|
|
146
|
+
const v1RefAssets = sourceVault.toAssets(shares);
|
|
147
|
+
const minSharePriceVaultV1 = MathLib.mulDivDown(v1RefAssets, MathLib.wToRay(MathLib.WAD - slippageTolerance), shares);
|
|
148
|
+
// Compute maxSharePriceVaultV2 for V2 deposit (slippage upward)
|
|
149
|
+
const v2RefShares = targetVault.toShares(v1RefAssets);
|
|
150
|
+
if (v2RefShares <= 0n) {
|
|
151
|
+
throw new NonPositiveSharesAmountError(targetVault.address);
|
|
152
|
+
}
|
|
153
|
+
const maxSharePriceVaultV2 = MathLib.min(MathLib.mulDivUp(v1RefAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), v2RefShares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
154
|
+
return {
|
|
155
|
+
getRequirements: async () => await getRequirements(this.client.viemClient, {
|
|
156
|
+
address: this.vault,
|
|
157
|
+
chainId: this.chainId,
|
|
158
|
+
supportSignature: this.client.options.supportSignature,
|
|
159
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
160
|
+
// V1 shares always implement EIP-2612.
|
|
161
|
+
useSimplePermit: true,
|
|
162
|
+
args: {
|
|
163
|
+
amount: shares,
|
|
164
|
+
from: userAddress,
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
buildTx: (requirementSignature) => vaultV1MigrateToV2({
|
|
168
|
+
vault: {
|
|
169
|
+
chainId: this.chainId,
|
|
170
|
+
address: this.vault,
|
|
171
|
+
asset: sourceVault.asset,
|
|
172
|
+
},
|
|
173
|
+
args: {
|
|
174
|
+
targetVault: targetVault.address,
|
|
175
|
+
targetAsset: targetVault.asset,
|
|
176
|
+
shares,
|
|
177
|
+
minSharePriceVaultV1,
|
|
178
|
+
maxSharePriceVaultV2,
|
|
179
|
+
recipient: userAddress,
|
|
180
|
+
requirementSignature,
|
|
181
|
+
},
|
|
182
|
+
metadata: this.client.options.metadata,
|
|
183
|
+
}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./vaultV2.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./vaultV2.js";
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { type AccrualVaultV2 } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { type Address } from "viem";
|
|
4
|
+
import type { FetchParameters } from "../../types/data.js";
|
|
5
|
+
import { type Deallocation, type DepositAmountArgs, type ERC20ApprovalAction, type MorphoClientType, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types/index.js";
|
|
6
|
+
export interface VaultV2Actions {
|
|
7
|
+
/**
|
|
8
|
+
* Fetches the latest vault data.
|
|
9
|
+
*
|
|
10
|
+
* This function fetches the latest vault data from the blockchain.
|
|
11
|
+
* @param {FetchParameters} [parameters] - The parameters for the fetch operation.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<Awaited<ReturnType<typeof fetchAccrualVaultV2>>>} The latest vault data.
|
|
14
|
+
*/
|
|
15
|
+
getData: (parameters?: FetchParameters) => Promise<Awaited<ReturnType<typeof fetchAccrualVaultV2>>>;
|
|
16
|
+
/**
|
|
17
|
+
* Prepares a deposit transaction for the VaultV2 contract.
|
|
18
|
+
*
|
|
19
|
+
* This function constructs the transaction data required to deposit a specified amount of assets into the vault.
|
|
20
|
+
* Uses pre-fetched accrual vault data for accurate calculations of slippage and asset address,
|
|
21
|
+
* then returns the prepared deposit transaction and a function for retrieving all required approval transactions.
|
|
22
|
+
* Bundler Integration: This flow uses the bundler to atomically execute the user's asset transfer and vault deposit in a single transaction for slippage protection.
|
|
23
|
+
*
|
|
24
|
+
* @param {Object} params - The deposit parameters.
|
|
25
|
+
* @param {bigint} [params.amount=0n] - Amount of ERC-20 assets to deposit. At least one of amount or nativeAmount must be provided.
|
|
26
|
+
* @param {Address} params.userAddress - User address initiating the deposit.
|
|
27
|
+
* @param {AccrualVaultV2} params.accrualVault - Pre-fetched vault data with asset address and share conversion.
|
|
28
|
+
* @param {bigint} [params.slippageTolerance=DEFAULT_SLIPPAGE_TOLERANCE] - Optional slippage tolerance value. Default is 0.03%. Slippage tolerance must be less than 10%.
|
|
29
|
+
* @param {bigint} [params.nativeAmount] - Amount of native token to wrap into wNative. Vault asset must be wNative.
|
|
30
|
+
* @returns {Object} The result object.
|
|
31
|
+
* @returns {Readonly<Transaction<VaultV2DepositAction>>} returns.tx The prepared deposit transaction.
|
|
32
|
+
* @returns {Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>} returns.getRequirements The function for retrieving all required approval transactions.
|
|
33
|
+
*/
|
|
34
|
+
deposit: (params: {
|
|
35
|
+
userAddress: Address;
|
|
36
|
+
accrualVault: AccrualVaultV2;
|
|
37
|
+
slippageTolerance?: bigint;
|
|
38
|
+
} & DepositAmountArgs) => {
|
|
39
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
40
|
+
getRequirements: (params?: {
|
|
41
|
+
useSimplePermit?: boolean;
|
|
42
|
+
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Prepares a withdraw transaction for the VaultV2 contract.
|
|
46
|
+
*
|
|
47
|
+
* This function constructs the transaction data required to withdraw a specified amount of assets from the vault.
|
|
48
|
+
*
|
|
49
|
+
* @param {Object} params - The withdraw parameters.
|
|
50
|
+
* @param {bigint} params.amount - The amount of assets to withdraw.
|
|
51
|
+
* @param {Address} params.userAddress - User address initiating the withdraw.
|
|
52
|
+
* @returns {Object} The result object.
|
|
53
|
+
* @returns {Readonly<Transaction<VaultV2WithdrawAction>>} returns.tx The prepared withdraw transaction.
|
|
54
|
+
*/
|
|
55
|
+
withdraw: (params: {
|
|
56
|
+
amount: bigint;
|
|
57
|
+
userAddress: Address;
|
|
58
|
+
}) => {
|
|
59
|
+
buildTx: () => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Prepares a redeem transaction for the VaultV2 contract.
|
|
63
|
+
*
|
|
64
|
+
* This function constructs the transaction data required to redeem a specified amount of shares from the vault.
|
|
65
|
+
*
|
|
66
|
+
* @param {Object} params - The redeem parameters.
|
|
67
|
+
* @param {bigint} params.shares - The amount of shares to redeem.
|
|
68
|
+
* @param {Address} params.userAddress - User address initiating the redeem.
|
|
69
|
+
* @returns {Object} The result object.
|
|
70
|
+
* @returns {Readonly<Transaction<VaultV2RedeemAction>>} returns.tx The prepared redeem transaction.
|
|
71
|
+
*/
|
|
72
|
+
redeem: (params: {
|
|
73
|
+
shares: bigint;
|
|
74
|
+
userAddress: Address;
|
|
75
|
+
}) => {
|
|
76
|
+
buildTx: () => Readonly<Transaction<VaultV2RedeemAction>>;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Prepares a force withdraw transaction for the VaultV2 contract using the vault's native multicall.
|
|
80
|
+
*
|
|
81
|
+
* This function encodes one or more on-chain forceDeallocate calls followed by a single withdraw,
|
|
82
|
+
* executed atomically via VaultV2's multicall. This allows a user to free liquidity from multiple
|
|
83
|
+
* illiquid markets and withdraw the resulting assets in one transaction.
|
|
84
|
+
*
|
|
85
|
+
* @param {Object} params - The force withdraw parameters.
|
|
86
|
+
* @param {readonly Deallocation[]} params.deallocations - The typed list of deallocations to perform.
|
|
87
|
+
* @param {Object} params.withdraw - The withdraw parameters applied after deallocations.
|
|
88
|
+
* @param {bigint} params.withdraw.amount - The amount of assets to withdraw.
|
|
89
|
+
* @param {Address} params.userAddress - User address (penalty source and withdraw recipient).
|
|
90
|
+
* @returns {Object} The result object.
|
|
91
|
+
* @returns {Readonly<Transaction<VaultV2ForceWithdrawAction>>} returns.buildTx The prepared multicall transaction.
|
|
92
|
+
*/
|
|
93
|
+
forceWithdraw: (params: {
|
|
94
|
+
deallocations: readonly Deallocation[];
|
|
95
|
+
withdraw: {
|
|
96
|
+
amount: bigint;
|
|
97
|
+
};
|
|
98
|
+
userAddress: Address;
|
|
99
|
+
}) => {
|
|
100
|
+
buildTx: () => Readonly<Transaction<VaultV2ForceWithdrawAction>>;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Prepares a force redeem transaction for the VaultV2 contract using the vault's native multicall.
|
|
104
|
+
*
|
|
105
|
+
* This function encodes one or more on-chain forceDeallocate calls followed by a single redeem,
|
|
106
|
+
* executed atomically via VaultV2's multicall. This allows a user to free liquidity from multiple
|
|
107
|
+
* illiquid markets and redeem all their shares in one transaction.
|
|
108
|
+
*
|
|
109
|
+
* This is the share-based counterpart to forceWithdraw, useful for maximum withdrawal scenarios
|
|
110
|
+
* where specifying an exact asset amount is impractical.
|
|
111
|
+
*
|
|
112
|
+
* The total assets passed to forceDeallocate calls must be greater than or equal to the
|
|
113
|
+
* asset-equivalent of the redeemed shares. The caller should apply a buffer on the deallocated
|
|
114
|
+
* amounts to account for share-price drift between submission and execution.
|
|
115
|
+
*
|
|
116
|
+
* @param {Object} params - The force redeem parameters.
|
|
117
|
+
* @param {readonly Deallocation[]} params.deallocations - The typed list of deallocations to perform.
|
|
118
|
+
* @param {Object} params.redeem - The redeem parameters applied after deallocations.
|
|
119
|
+
* @param {bigint} params.redeem.shares - The amount of shares to redeem.
|
|
120
|
+
* @param {Address} params.userAddress - User address (penalty source and redeem recipient).
|
|
121
|
+
* @returns {Object} The result object.
|
|
122
|
+
* @returns {Readonly<Transaction<VaultV2ForceRedeemAction>>} returns.buildTx The prepared multicall transaction.
|
|
123
|
+
*/
|
|
124
|
+
forceRedeem: (params: {
|
|
125
|
+
deallocations: readonly Deallocation[];
|
|
126
|
+
redeem: {
|
|
127
|
+
shares: bigint;
|
|
128
|
+
};
|
|
129
|
+
userAddress: Address;
|
|
130
|
+
}) => {
|
|
131
|
+
buildTx: () => Readonly<Transaction<VaultV2ForceRedeemAction>>;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
135
|
+
private readonly client;
|
|
136
|
+
private readonly vault;
|
|
137
|
+
private readonly chainId;
|
|
138
|
+
constructor(client: MorphoClientType, vault: Address, chainId: number);
|
|
139
|
+
getData(parameters?: FetchParameters): Promise<AccrualVaultV2>;
|
|
140
|
+
deposit({ amount, userAddress, accrualVault, slippageTolerance, nativeAmount, }: {
|
|
141
|
+
userAddress: Address;
|
|
142
|
+
accrualVault: AccrualVaultV2;
|
|
143
|
+
slippageTolerance?: bigint;
|
|
144
|
+
} & DepositAmountArgs): {
|
|
145
|
+
getRequirements: (params?: {
|
|
146
|
+
useSimplePermit?: boolean;
|
|
147
|
+
}) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
|
|
148
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
149
|
+
};
|
|
150
|
+
withdraw({ amount, userAddress }: {
|
|
151
|
+
amount: bigint;
|
|
152
|
+
userAddress: Address;
|
|
153
|
+
}): {
|
|
154
|
+
buildTx: () => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
155
|
+
};
|
|
156
|
+
redeem({ shares, userAddress }: {
|
|
157
|
+
shares: bigint;
|
|
158
|
+
userAddress: Address;
|
|
159
|
+
}): {
|
|
160
|
+
buildTx: () => Readonly<Transaction<VaultV2RedeemAction>>;
|
|
161
|
+
};
|
|
162
|
+
forceWithdraw({ deallocations, withdraw, userAddress, }: {
|
|
163
|
+
deallocations: readonly Deallocation[];
|
|
164
|
+
withdraw: {
|
|
165
|
+
amount: bigint;
|
|
166
|
+
};
|
|
167
|
+
userAddress: Address;
|
|
168
|
+
}): {
|
|
169
|
+
buildTx: () => Readonly<Transaction<VaultV2ForceWithdrawAction>>;
|
|
170
|
+
};
|
|
171
|
+
forceRedeem({ deallocations, redeem, userAddress, }: {
|
|
172
|
+
deallocations: readonly Deallocation[];
|
|
173
|
+
redeem: {
|
|
174
|
+
shares: bigint;
|
|
175
|
+
};
|
|
176
|
+
userAddress: Address;
|
|
177
|
+
}): {
|
|
178
|
+
buildTx: () => Readonly<Transaction<VaultV2ForceRedeemAction>>;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { isAddressEqual } from "viem";
|
|
4
|
+
import { getRequirements, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
|
|
5
|
+
import { MAX_SLIPPAGE_TOLERANCE } from "../../helpers/constant.js";
|
|
6
|
+
import { ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, NativeAmountOnNonWNativeVaultError, NegativeNativeAmountError, NegativeSlippageToleranceError, NonPositiveAssetAmountError, NonPositiveSharesAmountError, VaultAddressMismatchError, } from "../../types/index.js";
|
|
7
|
+
export class MorphoVaultV2 {
|
|
8
|
+
client;
|
|
9
|
+
vault;
|
|
10
|
+
chainId;
|
|
11
|
+
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
12
|
+
constructor(client, vault, chainId) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
this.vault = vault;
|
|
15
|
+
this.chainId = chainId;
|
|
16
|
+
}
|
|
17
|
+
async getData(parameters) {
|
|
18
|
+
if (this.client.viemClient.chain?.id &&
|
|
19
|
+
this.client.viemClient.chain?.id !== this.chainId) {
|
|
20
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
21
|
+
}
|
|
22
|
+
return fetchAccrualVaultV2(this.vault, this.client.viemClient, {
|
|
23
|
+
...parameters,
|
|
24
|
+
chainId: this.chainId,
|
|
25
|
+
deployless: this.client.options.supportDeployless,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
deposit({ amount = 0n, userAddress, accrualVault, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, nativeAmount, }) {
|
|
29
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
30
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
31
|
+
}
|
|
32
|
+
if (!isAddressEqual(accrualVault.address, this.vault)) {
|
|
33
|
+
throw new VaultAddressMismatchError(this.vault, accrualVault.address);
|
|
34
|
+
}
|
|
35
|
+
if (amount < 0n) {
|
|
36
|
+
throw new NonPositiveAssetAmountError(this.vault);
|
|
37
|
+
}
|
|
38
|
+
if (nativeAmount && nativeAmount < 0n) {
|
|
39
|
+
throw new NegativeNativeAmountError(nativeAmount);
|
|
40
|
+
}
|
|
41
|
+
let wNative;
|
|
42
|
+
if (nativeAmount) {
|
|
43
|
+
({ wNative } = getChainAddresses(this.chainId));
|
|
44
|
+
if (!wNative) {
|
|
45
|
+
throw new ChainWNativeMissingError(this.chainId);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (slippageTolerance < 0n) {
|
|
49
|
+
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
50
|
+
}
|
|
51
|
+
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
52
|
+
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
53
|
+
}
|
|
54
|
+
if (nativeAmount && wNative) {
|
|
55
|
+
if (!isAddressEqual(accrualVault.asset, wNative)) {
|
|
56
|
+
throw new NativeAmountOnNonWNativeVaultError(accrualVault.asset, wNative);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
60
|
+
const shares = accrualVault.toShares(totalAssets);
|
|
61
|
+
if (shares <= 0n) {
|
|
62
|
+
throw new NonPositiveSharesAmountError(this.vault);
|
|
63
|
+
}
|
|
64
|
+
const maxSharePrice = MathLib.min(MathLib.mulDivUp(totalAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares), MathLib.RAY * 100n);
|
|
65
|
+
return {
|
|
66
|
+
getRequirements: async (params) => await getRequirements(this.client.viemClient, {
|
|
67
|
+
address: accrualVault.asset,
|
|
68
|
+
chainId: this.chainId,
|
|
69
|
+
supportSignature: this.client.options.supportSignature,
|
|
70
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
71
|
+
useSimplePermit: params?.useSimplePermit,
|
|
72
|
+
args: {
|
|
73
|
+
amount,
|
|
74
|
+
from: userAddress,
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
buildTx: (requirementSignature) => vaultV2Deposit({
|
|
78
|
+
vault: {
|
|
79
|
+
chainId: this.chainId,
|
|
80
|
+
address: this.vault,
|
|
81
|
+
asset: accrualVault.asset,
|
|
82
|
+
},
|
|
83
|
+
args: {
|
|
84
|
+
amount,
|
|
85
|
+
maxSharePrice,
|
|
86
|
+
recipient: userAddress,
|
|
87
|
+
requirementSignature,
|
|
88
|
+
nativeAmount,
|
|
89
|
+
},
|
|
90
|
+
metadata: this.client.options.metadata,
|
|
91
|
+
}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
withdraw({ amount, userAddress }) {
|
|
95
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
96
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
buildTx: () => vaultV2Withdraw({
|
|
100
|
+
vault: { address: this.vault },
|
|
101
|
+
args: {
|
|
102
|
+
amount,
|
|
103
|
+
recipient: userAddress,
|
|
104
|
+
onBehalf: userAddress,
|
|
105
|
+
},
|
|
106
|
+
metadata: this.client.options.metadata,
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
redeem({ shares, userAddress }) {
|
|
111
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
112
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
buildTx: () => vaultV2Redeem({
|
|
116
|
+
vault: { address: this.vault },
|
|
117
|
+
args: {
|
|
118
|
+
shares,
|
|
119
|
+
recipient: userAddress,
|
|
120
|
+
onBehalf: userAddress,
|
|
121
|
+
},
|
|
122
|
+
metadata: this.client.options.metadata,
|
|
123
|
+
}),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
forceWithdraw({ deallocations, withdraw, userAddress, }) {
|
|
127
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
128
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
buildTx: () => vaultV2ForceWithdraw({
|
|
132
|
+
vault: { address: this.vault },
|
|
133
|
+
args: {
|
|
134
|
+
deallocations,
|
|
135
|
+
withdraw: {
|
|
136
|
+
amount: withdraw.amount,
|
|
137
|
+
recipient: userAddress,
|
|
138
|
+
},
|
|
139
|
+
onBehalf: userAddress,
|
|
140
|
+
},
|
|
141
|
+
metadata: this.client.options.metadata,
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
forceRedeem({ deallocations, redeem, userAddress, }) {
|
|
146
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
147
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
buildTx: () => vaultV2ForceRedeem({
|
|
151
|
+
vault: { address: this.vault },
|
|
152
|
+
args: {
|
|
153
|
+
deallocations,
|
|
154
|
+
redeem: {
|
|
155
|
+
shares: redeem.shares,
|
|
156
|
+
recipient: userAddress,
|
|
157
|
+
},
|
|
158
|
+
onBehalf: userAddress,
|
|
159
|
+
},
|
|
160
|
+
metadata: this.client.options.metadata,
|
|
161
|
+
}),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { SimulationState } from "@morpho-org/simulation-sdk";
|
|
3
|
+
import { type ReallocationComputeOptions, type VaultReallocation } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Computes vault reallocations for a borrow operation on a target market.
|
|
6
|
+
*
|
|
7
|
+
* Replicates the shared liquidity algorithm from `populateSubBundle` in
|
|
8
|
+
* `@morpho-org/bundler-sdk-viem`. First attempts "friendly" reallocations
|
|
9
|
+
* respecting withdrawal utilization targets, then falls back to aggressive
|
|
10
|
+
* reallocations (100% withdrawal utilization) if liquidity is still insufficient.
|
|
11
|
+
*
|
|
12
|
+
* @param params.reallocationData - The simulation state containing market, vault, and position data.
|
|
13
|
+
* @param params.marketId - The target market to reallocate liquidity into.
|
|
14
|
+
* @param params.borrowAmount - The intended borrow amount (used to compute post-borrow utilization).
|
|
15
|
+
* @param params.options - Optional reallocation computation options.
|
|
16
|
+
* @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
|
|
17
|
+
*/
|
|
18
|
+
export declare const computeReallocations: ({ reallocationData: data, marketId, borrowAmount, options, }: {
|
|
19
|
+
readonly reallocationData: SimulationState;
|
|
20
|
+
readonly marketId: MarketId;
|
|
21
|
+
readonly borrowAmount: bigint;
|
|
22
|
+
readonly options?: ReallocationComputeOptions;
|
|
23
|
+
}) => readonly VaultReallocation[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { DEFAULT_SUPPLY_TARGET_UTILIZATION } from "@morpho-org/bundler-sdk-viem";
|
|
3
|
+
import { MissingPublicAllocatorConfigError, } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Computes vault reallocations for a borrow operation on a target market.
|
|
6
|
+
*
|
|
7
|
+
* Replicates the shared liquidity algorithm from `populateSubBundle` in
|
|
8
|
+
* `@morpho-org/bundler-sdk-viem`. First attempts "friendly" reallocations
|
|
9
|
+
* respecting withdrawal utilization targets, then falls back to aggressive
|
|
10
|
+
* reallocations (100% withdrawal utilization) if liquidity is still insufficient.
|
|
11
|
+
*
|
|
12
|
+
* @param params.reallocationData - The simulation state containing market, vault, and position data.
|
|
13
|
+
* @param params.marketId - The target market to reallocate liquidity into.
|
|
14
|
+
* @param params.borrowAmount - The intended borrow amount (used to compute post-borrow utilization).
|
|
15
|
+
* @param params.options - Optional reallocation computation options.
|
|
16
|
+
* @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
|
|
17
|
+
*/
|
|
18
|
+
export const computeReallocations = ({ reallocationData: data, marketId, borrowAmount, options, }) => {
|
|
19
|
+
if (options?.enabled === false)
|
|
20
|
+
return [];
|
|
21
|
+
const market = data.getMarket(marketId).accrueInterest(data.block.timestamp);
|
|
22
|
+
const newTotalBorrowAssets = market.totalBorrowAssets + borrowAmount;
|
|
23
|
+
const newTotalSupplyAssets = market.totalSupplyAssets;
|
|
24
|
+
const supplyTargetUtilization = options?.supplyTargetUtilization?.[market.params.id] ??
|
|
25
|
+
options?.defaultSupplyTargetUtilization ??
|
|
26
|
+
DEFAULT_SUPPLY_TARGET_UTILIZATION;
|
|
27
|
+
if (MarketUtils.getUtilization({
|
|
28
|
+
totalSupplyAssets: newTotalSupplyAssets,
|
|
29
|
+
totalBorrowAssets: newTotalBorrowAssets,
|
|
30
|
+
}) <= supplyTargetUtilization)
|
|
31
|
+
return [];
|
|
32
|
+
// Solve: newTotalBorrowAssets / (newTotalSupplyAssets + reallocatedAssets) = supplyTargetUtilization
|
|
33
|
+
let requiredAssets = supplyTargetUtilization === 0n
|
|
34
|
+
? MathLib.MAX_UINT_160
|
|
35
|
+
: MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
36
|
+
newTotalSupplyAssets;
|
|
37
|
+
// Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
|
|
38
|
+
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.getMarketPublicReallocations(market.id, options);
|
|
39
|
+
const withdrawals = [...friendlyWithdrawals];
|
|
40
|
+
const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
|
|
41
|
+
if (friendlyReallocationMarket.totalBorrowAssets + borrowAmount >
|
|
42
|
+
friendlyReallocationMarket.totalSupplyAssets) {
|
|
43
|
+
// Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
|
|
44
|
+
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
45
|
+
withdrawals.push(...friendlyReallocationData.getMarketPublicReallocations(market.id, {
|
|
46
|
+
...options,
|
|
47
|
+
defaultMaxWithdrawalUtilization: MathLib.WAD,
|
|
48
|
+
maxWithdrawalUtilization: {},
|
|
49
|
+
}).withdrawals);
|
|
50
|
+
}
|
|
51
|
+
if (requiredAssets <= 0n)
|
|
52
|
+
return [];
|
|
53
|
+
// Group withdrawals by vault, capping total at requiredAssets.
|
|
54
|
+
const reallocationsMap = {};
|
|
55
|
+
for (const { vault, ...withdrawal } of withdrawals) {
|
|
56
|
+
const vaultReallocations = (reallocationsMap[vault] ??= []);
|
|
57
|
+
const existing = vaultReallocations.find((item) => item.id === withdrawal.id);
|
|
58
|
+
const reallocatedAssets = MathLib.min(withdrawal.assets, requiredAssets);
|
|
59
|
+
if (reallocatedAssets <= 0n)
|
|
60
|
+
continue;
|
|
61
|
+
if (existing != null) {
|
|
62
|
+
existing.assets += reallocatedAssets;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
vaultReallocations.push({
|
|
66
|
+
...withdrawal,
|
|
67
|
+
assets: reallocatedAssets,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
requiredAssets -= reallocatedAssets;
|
|
71
|
+
if (requiredAssets === 0n)
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
// Transform into VaultReallocation[] format.
|
|
75
|
+
return Object.entries(reallocationsMap)
|
|
76
|
+
.filter(([, vaultWithdrawals]) => vaultWithdrawals.length > 0)
|
|
77
|
+
.map(([vault, vaultWithdrawals]) => ({
|
|
78
|
+
vault: vault,
|
|
79
|
+
fee: (() => {
|
|
80
|
+
const config = data.getVault(vault).publicAllocatorConfig;
|
|
81
|
+
if (config == null) {
|
|
82
|
+
throw new MissingPublicAllocatorConfigError(vault);
|
|
83
|
+
}
|
|
84
|
+
return config.fee;
|
|
85
|
+
})(),
|
|
86
|
+
withdrawals: vaultWithdrawals
|
|
87
|
+
// Reallocation withdrawals must be sorted by market id in ascending order.
|
|
88
|
+
.sort(({ id: idA }, { id: idB }) => idA > idB ? 1 : idA < idB ? -1 : 0)
|
|
89
|
+
.map(({ id, assets }) => ({
|
|
90
|
+
marketParams: data.getMarket(id).params,
|
|
91
|
+
amount: assets,
|
|
92
|
+
})),
|
|
93
|
+
}));
|
|
94
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Maximum slippage tolerance: 10% */
|
|
2
|
+
export declare const MAX_SLIPPAGE_TOLERANCE: bigint;
|
|
3
|
+
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
4
|
+
export declare const DEFAULT_LLTV_BUFFER: bigint;
|
|
5
|
+
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
6
|
+
export declare const MAX_ABSOLUTE_SHARE_PRICE: bigint;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
/** Maximum slippage tolerance: 10% */
|
|
3
|
+
export const MAX_SLIPPAGE_TOLERANCE = MathLib.WAD / 10n;
|
|
4
|
+
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
5
|
+
export const DEFAULT_LLTV_BUFFER = MathLib.WAD / 200n;
|
|
6
|
+
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
7
|
+
export const MAX_ABSOLUTE_SHARE_PRICE = 100n * MathLib.RAY;
|