@morpho-org/morpho-sdk 6.0.0-next.1 → 6.0.0-next.3
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/MIGRATION-v5-to-v6.md +199 -34
- package/README.md +23 -15
- package/lib/cjs/abis.d.ts +3 -2829
- package/lib/cjs/abis.js +1 -1938
- package/lib/cjs/actions/blue/borrow.d.ts +3 -2
- package/lib/cjs/actions/blue/borrow.js +3 -2
- package/lib/cjs/actions/blue/common.js +6 -35
- package/lib/cjs/actions/blue/refinance.d.ts +2 -2
- package/lib/cjs/actions/blue/refinance.js +2 -2
- package/lib/cjs/actions/blue/repay.d.ts +1 -1
- package/lib/cjs/actions/blue/repay.js +1 -1
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/cjs/actions/blue/supply.d.ts +2 -2
- package/lib/cjs/actions/blue/supply.js +4 -9
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/supplyCollateral.js +2 -2
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -2
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +3 -2
- package/lib/cjs/actions/blue/withdraw.d.ts +3 -3
- package/lib/cjs/actions/blue/withdraw.js +3 -3
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
- package/lib/cjs/actions/bundles/common.d.ts +38 -4
- package/lib/cjs/actions/bundles/common.js +91 -35
- package/lib/cjs/actions/index.d.ts +0 -1
- package/lib/cjs/actions/index.js +0 -1
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +17 -26
- package/lib/cjs/actions/requirements/blue/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/blue/index.js +0 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +24 -25
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +4 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -38
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/cjs/actions/requirements/encode/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/encode/index.js +0 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -6
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -6
- package/lib/cjs/actions/requirements/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/index.js +0 -1
- package/lib/cjs/actions/vaultV1/deposit.d.ts +1 -2
- package/lib/cjs/actions/vaultV1/deposit.js +1 -2
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +6 -4
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +12 -8
- package/lib/cjs/actions/vaultV2/deposit.d.ts +1 -2
- package/lib/cjs/actions/vaultV2/deposit.js +1 -2
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +87 -60
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +6 -4
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +12 -8
- package/lib/cjs/augment.d.ts +0 -1
- package/lib/cjs/augment.js +0 -1
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +2 -2
- package/lib/cjs/blue/entities.js +1 -2
- package/lib/cjs/blue/errors.d.ts +1 -1
- package/lib/cjs/blue/errors.js +2 -1
- package/lib/cjs/blue/fetch.d.ts +1 -1
- package/lib/cjs/blue/fetch.js +1 -2
- package/lib/cjs/blue/types.d.ts +2 -4
- package/lib/cjs/blue/utils.d.ts +1 -3
- package/lib/cjs/blue/utils.js +1 -3
- package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
- package/lib/cjs/client/morphoViemExtension.js +1 -1
- package/lib/cjs/constants.d.ts +3 -37
- package/lib/cjs/constants.js +1 -39
- package/lib/cjs/entities/blue/blue.d.ts +38 -264
- package/lib/cjs/entities/blue/blue.js +2 -203
- package/lib/cjs/entities/index.d.ts +3 -60
- package/lib/cjs/entities/index.js +1 -20
- package/lib/cjs/entities/midnight/midnight.js +20 -24
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +19 -14
- package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/cjs/entities/requirements/index.d.ts +1 -0
- package/lib/cjs/entities/requirements/index.js +1 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +18 -6
- package/lib/cjs/entities/vaultV1/vaultV1.js +25 -8
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +154 -35
- package/lib/cjs/entities/vaultV2/vaultV2.js +249 -23
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +50 -34
- package/lib/cjs/errors.d.ts +2 -46
- package/lib/cjs/errors.js +2 -46
- package/lib/cjs/fetch.d.ts +1 -15
- package/lib/cjs/fetch.js +1 -16
- package/lib/cjs/helpers/constant.d.ts +0 -2
- package/lib/cjs/helpers/constant.js +0 -2
- package/lib/cjs/helpers/index.d.ts +4 -3
- package/lib/cjs/helpers/index.js +10 -9
- package/lib/cjs/helpers/metadata.d.ts +3 -5
- package/lib/cjs/helpers/metadata.js +4 -6
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +164 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/cjs/helpers/slippage.d.ts +34 -90
- package/lib/cjs/helpers/slippage.js +52 -137
- package/lib/cjs/helpers/utilization.d.ts +12 -10
- package/lib/cjs/helpers/utilization.js +27 -12
- package/lib/cjs/helpers/validate.d.ts +66 -32
- package/lib/cjs/helpers/validate.js +84 -57
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
- package/lib/cjs/types/action.d.ts +102 -68
- package/lib/cjs/types/action.js +11 -4
- package/lib/cjs/types/error.d.ts +332 -385
- package/lib/cjs/types/error.js +369 -412
- package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
- package/lib/cjs/types.d.ts +2 -18
- package/lib/cjs/utils.d.ts +8 -35
- package/lib/cjs/utils.js +11 -36
- package/lib/esm/abis.d.ts +3 -2829
- package/lib/esm/abis.js +3 -1935
- package/lib/esm/actions/blue/borrow.d.ts +3 -2
- package/lib/esm/actions/blue/borrow.js +3 -2
- package/lib/esm/actions/blue/common.js +8 -37
- package/lib/esm/actions/blue/refinance.d.ts +2 -2
- package/lib/esm/actions/blue/refinance.js +2 -2
- package/lib/esm/actions/blue/repay.d.ts +1 -1
- package/lib/esm/actions/blue/repay.js +1 -1
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/esm/actions/blue/supply.d.ts +2 -2
- package/lib/esm/actions/blue/supply.js +6 -11
- package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/supplyCollateral.js +2 -2
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -2
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -2
- package/lib/esm/actions/blue/withdraw.d.ts +3 -3
- package/lib/esm/actions/blue/withdraw.js +3 -3
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/withdrawCollateral.js +2 -2
- package/lib/esm/actions/bundles/common.d.ts +38 -4
- package/lib/esm/actions/bundles/common.js +91 -36
- package/lib/esm/actions/index.d.ts +0 -1
- package/lib/esm/actions/index.js +0 -1
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +20 -29
- package/lib/esm/actions/requirements/blue/index.d.ts +0 -1
- package/lib/esm/actions/requirements/blue/index.js +0 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +25 -26
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -5
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -39
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/esm/actions/requirements/encode/index.d.ts +0 -1
- package/lib/esm/actions/requirements/encode/index.js +0 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -6
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -6
- package/lib/esm/actions/requirements/index.d.ts +0 -1
- package/lib/esm/actions/requirements/index.js +0 -1
- package/lib/esm/actions/vaultV1/deposit.d.ts +1 -2
- package/lib/esm/actions/vaultV1/deposit.js +1 -2
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +6 -4
- package/lib/esm/actions/vaultV1/inKindRedeem.js +12 -8
- package/lib/esm/actions/vaultV2/deposit.d.ts +1 -2
- package/lib/esm/actions/vaultV2/deposit.js +1 -2
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/esm/actions/vaultV2/forceWithdraw.js +89 -62
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +6 -4
- package/lib/esm/actions/vaultV2/inKindRedeem.js +12 -8
- package/lib/esm/augment.d.ts +0 -1
- package/lib/esm/augment.js +0 -1
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +2 -2
- package/lib/esm/blue/entities.js +1 -1
- package/lib/esm/blue/errors.d.ts +1 -1
- package/lib/esm/blue/errors.js +1 -1
- package/lib/esm/blue/fetch.d.ts +1 -1
- package/lib/esm/blue/fetch.js +1 -1
- package/lib/esm/blue/types.d.ts +2 -4
- package/lib/esm/blue/utils.d.ts +1 -3
- package/lib/esm/blue/utils.js +1 -3
- package/lib/esm/client/morphoViemExtension.d.ts +1 -1
- package/lib/esm/client/morphoViemExtension.js +1 -1
- package/lib/esm/constants.d.ts +3 -37
- package/lib/esm/constants.js +3 -37
- package/lib/esm/entities/blue/blue.d.ts +38 -264
- package/lib/esm/entities/blue/blue.js +5 -206
- package/lib/esm/entities/index.d.ts +3 -60
- package/lib/esm/entities/index.js +1 -16
- package/lib/esm/entities/midnight/midnight.js +20 -24
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +20 -15
- package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/esm/entities/requirements/index.d.ts +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +18 -6
- package/lib/esm/entities/vaultV1/vaultV1.js +29 -12
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +154 -35
- package/lib/esm/entities/vaultV2/vaultV2.js +256 -30
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.js +48 -32
- package/lib/esm/errors.d.ts +2 -46
- package/lib/esm/errors.js +2 -46
- package/lib/esm/fetch.d.ts +1 -15
- package/lib/esm/fetch.js +1 -15
- package/lib/esm/helpers/constant.d.ts +0 -2
- package/lib/esm/helpers/constant.js +0 -2
- package/lib/esm/helpers/index.d.ts +4 -3
- package/lib/esm/helpers/index.js +4 -3
- package/lib/esm/helpers/metadata.d.ts +3 -5
- package/lib/esm/helpers/metadata.js +4 -6
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +161 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/esm/helpers/slippage.d.ts +34 -90
- package/lib/esm/helpers/slippage.js +52 -134
- package/lib/esm/helpers/utilization.d.ts +12 -10
- package/lib/esm/helpers/utilization.js +26 -11
- package/lib/esm/helpers/validate.d.ts +66 -32
- package/lib/esm/helpers/validate.js +83 -57
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +1 -3
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
- package/lib/esm/types/action.d.ts +102 -68
- package/lib/esm/types/action.js +12 -5
- package/lib/esm/types/error.d.ts +332 -385
- package/lib/esm/types/error.js +355 -392
- package/lib/esm/types/sharedLiquidity.d.ts +1 -117
- package/lib/esm/types.d.ts +2 -18
- package/lib/esm/utils.d.ts +8 -35
- package/lib/esm/utils.js +7 -34
- package/package.json +7 -15
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -83
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -151
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -50
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -74
- package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +0 -3
- package/lib/cjs/actions/requirements/generalAdapter/index.js +0 -19
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +0 -48
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +0 -58
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +0 -119
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -43
- package/lib/cjs/actions/signatures/index.d.ts +0 -3
- package/lib/cjs/actions/signatures/index.js +0 -19
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +0 -8
- package/lib/cjs/bundler/actions.d.ts +0 -797
- package/lib/cjs/bundler/actions.js +0 -1371
- package/lib/cjs/bundler/index.d.ts +0 -2
- package/lib/cjs/bundler/index.js +0 -18
- package/lib/cjs/bundler/types.d.ts +0 -235
- package/lib/cjs/bundler/types.js +0 -2
- package/lib/cjs/entities/vaultV1ReallocationData.d.ts +0 -326
- package/lib/cjs/entities/vaultV1ReallocationData.js +0 -542
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -78
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -243
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -79
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -147
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -46
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -70
- package/lib/esm/actions/requirements/generalAdapter/index.d.ts +0 -3
- package/lib/esm/actions/requirements/generalAdapter/index.js +0 -3
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +0 -44
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +0 -58
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +0 -115
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -39
- package/lib/esm/actions/signatures/index.d.ts +0 -3
- package/lib/esm/actions/signatures/index.js +0 -3
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -5
- package/lib/esm/bundler/actions.d.ts +0 -797
- package/lib/esm/bundler/actions.js +0 -1368
- package/lib/esm/bundler/index.d.ts +0 -2
- package/lib/esm/bundler/index.js +0 -2
- package/lib/esm/bundler/types.d.ts +0 -235
- package/lib/esm/bundler/types.js +0 -1
- package/lib/esm/entities/vaultV1ReallocationData.d.ts +0 -326
- package/lib/esm/entities/vaultV1ReallocationData.js +0 -544
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -78
- package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -239
- /package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
- /package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { InsufficientSharedLiquidityError, MissingPublicAllocatorConfigError, ReallocationWithdrawExceedsMarketSupplyError, } from "../types/index.js";
|
|
3
|
-
import { getSupplyTargetUtilization } from "./utilization.js";
|
|
4
|
-
import { compareMarketIds } from "./validate.js";
|
|
5
|
-
const groupWithdrawalsByVault = (withdrawals) => {
|
|
6
|
-
const groups = [];
|
|
7
|
-
const groupsByVault = new Map();
|
|
8
|
-
for (const [index, { vault, id, assets }] of withdrawals.entries()) {
|
|
9
|
-
if (assets <= 0n)
|
|
10
|
-
continue;
|
|
11
|
-
let group = groupsByVault.get(vault);
|
|
12
|
-
if (group == null) {
|
|
13
|
-
group = {
|
|
14
|
-
vault,
|
|
15
|
-
firstIndex: index,
|
|
16
|
-
totalAssets: 0n,
|
|
17
|
-
withdrawals: [],
|
|
18
|
-
};
|
|
19
|
-
groupsByVault.set(vault, group);
|
|
20
|
-
groups.push(group);
|
|
21
|
-
}
|
|
22
|
-
group.totalAssets += assets;
|
|
23
|
-
group.withdrawals.push({ id, assets });
|
|
24
|
-
}
|
|
25
|
-
return groups.sort((a, b) => {
|
|
26
|
-
if (a.totalAssets > b.totalAssets)
|
|
27
|
-
return -1;
|
|
28
|
-
if (a.totalAssets < b.totalAssets)
|
|
29
|
-
return 1;
|
|
30
|
-
return a.firstIndex - b.firstIndex;
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
const capVaultWithdrawals = (withdrawals, assets) => {
|
|
34
|
-
const cappedWithdrawals = [];
|
|
35
|
-
let remainingAssets = assets;
|
|
36
|
-
for (const withdrawal of withdrawals) {
|
|
37
|
-
const cappedAssets = MathLib.min(withdrawal.assets, remainingAssets);
|
|
38
|
-
/* v8 ignore next: grouped withdrawals and positive reallocation cap keep this defensive guard unreachable. */
|
|
39
|
-
if (cappedAssets <= 0n)
|
|
40
|
-
continue;
|
|
41
|
-
const existing = cappedWithdrawals.find((item) => item.id === withdrawal.id);
|
|
42
|
-
if (existing != null) {
|
|
43
|
-
existing.assets += cappedAssets;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
cappedWithdrawals.push({
|
|
47
|
-
id: withdrawal.id,
|
|
48
|
-
assets: cappedAssets,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
remainingAssets -= cappedAssets;
|
|
52
|
-
if (remainingAssets === 0n)
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
return cappedWithdrawals;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Computes vault reallocations for a `borrow` or `withdraw` on a target market.
|
|
59
|
-
*
|
|
60
|
-
* First attempts "friendly" reallocations respecting withdrawal utilization
|
|
61
|
-
* targets, then falls back to aggressive reallocations (100% withdrawal
|
|
62
|
-
* utilization) if liquidity is still insufficient.
|
|
63
|
-
*
|
|
64
|
-
* Algebra branches on `operation`:
|
|
65
|
-
* - `"borrow"`: `S' = S`, `B' = B + amount` (additional borrow demand).
|
|
66
|
-
* - `"withdraw"`: `S' = S − amount`, `B' = B` (supply-side shrinkage).
|
|
67
|
-
*
|
|
68
|
-
* In both cases reallocated assets are added on the supply side; `requiredAssets`
|
|
69
|
-
* and `absoluteShortfall` are derived from the operation-specific post-state.
|
|
70
|
-
*
|
|
71
|
-
* @remarks Pass `options.timestamp` from the same block used to fetch `reallocationData`; when omitted, market accrual falls back to the target market's `lastUpdate`, which can diverge from the source rows' fetch block. Per-market `maxWithdrawalUtilization` overrides apply only to phase 1; phase 2 forces 100% utilization on every source market.
|
|
72
|
-
* @param params.reallocationData - The local state containing market, vault, and position data.
|
|
73
|
-
* @param params.marketId - The target market to reallocate liquidity into.
|
|
74
|
-
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
75
|
-
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
76
|
-
* @param params.options - Optional reallocation computation options.
|
|
77
|
-
* @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
|
|
78
|
-
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market — preventing fee-bearing reallocations from being attached to a call that would still revert onchain.
|
|
79
|
-
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets` — the on-chain call would revert regardless of reallocations.
|
|
80
|
-
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
81
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
82
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
83
|
-
* @example
|
|
84
|
-
* ```ts
|
|
85
|
-
* import { createPublicClient, http, parseUnits } from "viem";
|
|
86
|
-
* import { mainnet } from "viem/chains";
|
|
87
|
-
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
88
|
-
* import {
|
|
89
|
-
* computeVaultV1Reallocations,
|
|
90
|
-
* morphoViemExtension,
|
|
91
|
-
* } from "@morpho-org/morpho-sdk";
|
|
92
|
-
*
|
|
93
|
-
* const client = createPublicClient({
|
|
94
|
-
* chain: mainnet,
|
|
95
|
-
* transport: http(),
|
|
96
|
-
* }).extend(morphoViemExtension());
|
|
97
|
-
*
|
|
98
|
-
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
99
|
-
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
100
|
-
* const block = await client.getBlock();
|
|
101
|
-
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
102
|
-
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
103
|
-
* block: { number: block.number, timestamp: block.timestamp },
|
|
104
|
-
* });
|
|
105
|
-
* const borrowAmount = parseUnits("1000", 6);
|
|
106
|
-
* const reallocations = computeVaultV1Reallocations({
|
|
107
|
-
* reallocationData,
|
|
108
|
-
* marketId: marketParams.id,
|
|
109
|
-
* operation: "borrow",
|
|
110
|
-
* amount: borrowAmount,
|
|
111
|
-
* options: { timestamp: block.timestamp },
|
|
112
|
-
* });
|
|
113
|
-
* // Encode `reallocations` explicitly with
|
|
114
|
-
* // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
|
|
115
|
-
* // High-level Blue writes accept Vault V2 reallocations only.
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
export const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
|
|
119
|
-
if (options?.enabled === false)
|
|
120
|
-
return [];
|
|
121
|
-
const normalizedOptions = {
|
|
122
|
-
...options,
|
|
123
|
-
reallocatableVaults: options?.reallocatableVaults == null
|
|
124
|
-
? undefined
|
|
125
|
-
: [...options.reallocatableVaults],
|
|
126
|
-
};
|
|
127
|
-
// VaultV1ReallocationData does not retain the fetch block; pass that block timestamp
|
|
128
|
-
// to compute against the same accrued state, otherwise Market defaults to lastUpdate.
|
|
129
|
-
const market = data
|
|
130
|
-
.getMarket(marketId)
|
|
131
|
-
.accrueInterest(normalizedOptions.timestamp);
|
|
132
|
-
// Reject unreachable withdraws before any utilization math: a negative
|
|
133
|
-
// post-supply yields a negative utilization that short-circuits the
|
|
134
|
-
// utilization gate below and silently returns `[]` — masking a sure revert.
|
|
135
|
-
if (operation === "withdraw" && amount > market.totalSupplyAssets) {
|
|
136
|
-
throw new ReallocationWithdrawExceedsMarketSupplyError({
|
|
137
|
-
marketId,
|
|
138
|
-
withdrawAmount: amount,
|
|
139
|
-
totalSupplyAssets: market.totalSupplyAssets,
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
// Post-state utilization is operation-dependent.
|
|
143
|
-
const newTotalBorrowAssets = operation === "borrow"
|
|
144
|
-
? market.totalBorrowAssets + amount
|
|
145
|
-
: market.totalBorrowAssets;
|
|
146
|
-
const newTotalSupplyAssets = operation === "withdraw"
|
|
147
|
-
? market.totalSupplyAssets - amount
|
|
148
|
-
: market.totalSupplyAssets;
|
|
149
|
-
const supplyTargetUtilization = getSupplyTargetUtilization(market.params.id, normalizedOptions);
|
|
150
|
-
if (MarketUtils.getUtilization({
|
|
151
|
-
totalSupplyAssets: newTotalSupplyAssets,
|
|
152
|
-
totalBorrowAssets: newTotalBorrowAssets,
|
|
153
|
-
}) <= supplyTargetUtilization)
|
|
154
|
-
return [];
|
|
155
|
-
// Solve: newTotalBorrowAssets / (newTotalSupplyAssets + reallocatedAssets) = supplyTargetUtilization
|
|
156
|
-
let requiredAssets = supplyTargetUtilization === 0n
|
|
157
|
-
? MathLib.MAX_UINT_160
|
|
158
|
-
: MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
159
|
-
newTotalSupplyAssets;
|
|
160
|
-
// Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
|
|
161
|
-
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.computeVaultV1Reallocations(market.id, normalizedOptions);
|
|
162
|
-
const withdrawals = [...friendlyWithdrawals];
|
|
163
|
-
const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
|
|
164
|
-
// Operation-specific post-friendly check: would the on-chain call still revert?
|
|
165
|
-
const friendlyBorrow = operation === "borrow"
|
|
166
|
-
? friendlyReallocationMarket.totalBorrowAssets + amount
|
|
167
|
-
: friendlyReallocationMarket.totalBorrowAssets;
|
|
168
|
-
const friendlySupply = operation === "withdraw"
|
|
169
|
-
? friendlyReallocationMarket.totalSupplyAssets - amount
|
|
170
|
-
: friendlyReallocationMarket.totalSupplyAssets;
|
|
171
|
-
if (friendlyBorrow > friendlySupply) {
|
|
172
|
-
// Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
|
|
173
|
-
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
174
|
-
withdrawals.push(...friendlyReallocationData.computeVaultV1Reallocations(market.id, {
|
|
175
|
-
...normalizedOptions,
|
|
176
|
-
defaultMaxWithdrawalUtilization: MathLib.WAD,
|
|
177
|
-
maxWithdrawalUtilization: {},
|
|
178
|
-
}).withdrawals);
|
|
179
|
-
}
|
|
180
|
-
if (requiredAssets <= 0n)
|
|
181
|
-
return [];
|
|
182
|
-
// Liquidity strictly required for the on-chain call to succeed.
|
|
183
|
-
const absoluteShortfall = newTotalBorrowAssets > newTotalSupplyAssets
|
|
184
|
-
? newTotalBorrowAssets - newTotalSupplyAssets
|
|
185
|
-
: 0n;
|
|
186
|
-
const reallocations = [];
|
|
187
|
-
let totalReallocated = 0n;
|
|
188
|
-
let remainingRequiredAssets = requiredAssets;
|
|
189
|
-
for (const group of groupWithdrawalsByVault(withdrawals)) {
|
|
190
|
-
const reallocatedAssets = MathLib.min(group.totalAssets, remainingRequiredAssets);
|
|
191
|
-
/* v8 ignore next: groups with non-positive assets are filtered before this point. */
|
|
192
|
-
if (reallocatedAssets <= 0n)
|
|
193
|
-
continue;
|
|
194
|
-
reallocations.push({
|
|
195
|
-
vault: group.vault,
|
|
196
|
-
withdrawals: capVaultWithdrawals(group.withdrawals, reallocatedAssets),
|
|
197
|
-
});
|
|
198
|
-
remainingRequiredAssets -= reallocatedAssets;
|
|
199
|
-
totalReallocated += reallocatedAssets;
|
|
200
|
-
if (remainingRequiredAssets === 0n)
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
// Refuse fee-bearing partial plans for an unreachable operation.
|
|
204
|
-
if (totalReallocated < absoluteShortfall) {
|
|
205
|
-
throw new InsufficientSharedLiquidityError({
|
|
206
|
-
marketId,
|
|
207
|
-
shortfall: absoluteShortfall,
|
|
208
|
-
available: totalReallocated,
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
// Transform into VaultV1Reallocation[] format.
|
|
212
|
-
return reallocations
|
|
213
|
-
.filter(({ withdrawals: vaultWithdrawals }) => vaultWithdrawals.length > 0)
|
|
214
|
-
.map(({ vault, withdrawals: vaultWithdrawals }) => ({
|
|
215
|
-
vault,
|
|
216
|
-
fee: (() => {
|
|
217
|
-
const config = data.getVault(vault).publicAllocatorConfig;
|
|
218
|
-
if (config == null) {
|
|
219
|
-
throw new MissingPublicAllocatorConfigError(vault);
|
|
220
|
-
}
|
|
221
|
-
return config.fee;
|
|
222
|
-
})(),
|
|
223
|
-
withdrawals: vaultWithdrawals
|
|
224
|
-
// Reallocation withdrawals must be sorted by market id in ascending
|
|
225
|
-
// order. Normalize first because `MarketId` can be mixed-case hex.
|
|
226
|
-
.sort(({ id: idA }, { id: idB }) => compareMarketIds(idA, idB))
|
|
227
|
-
.map(({ id, assets }) => ({
|
|
228
|
-
marketParams: data.getMarket(id).params,
|
|
229
|
-
amount: assets,
|
|
230
|
-
})),
|
|
231
|
-
}));
|
|
232
|
-
};
|
|
233
|
-
/**
|
|
234
|
-
* Deprecated name for the Vault V1 amount-aware reallocation planner.
|
|
235
|
-
*
|
|
236
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
237
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
238
|
-
*/
|
|
239
|
-
export const computeReallocations = computeVaultV1Reallocations;
|
/package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts
RENAMED
|
File without changes
|
/package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts
RENAMED
|
File without changes
|