@morpho-org/morpho-sdk 6.0.0-next.2 → 6.0.0-next.4
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 +88 -32
- package/README.md +9 -7
- package/lib/cjs/abis.d.ts +7 -2844
- package/lib/cjs/abis.js +3 -1951
- 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 +1 -9
- 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 +2 -2
- 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 +6 -2
- package/lib/cjs/actions/bundles/common.js +31 -5
- 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 +6 -8
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -16
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
- 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 +5 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +6 -11
- 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 +7 -5
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +11 -7
- 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 +1 -1
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -3
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +7 -5
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +11 -7
- package/lib/cjs/augment/AccrualPosition.d.ts +1 -0
- package/lib/cjs/augment.d.ts +0 -1
- package/lib/cjs/augment.js +0 -1
- package/lib/cjs/blue/abis.d.ts +5 -5
- package/lib/cjs/blue/abis.js +3 -7
- package/lib/cjs/blue/entities.d.ts +10 -8
- package/lib/cjs/blue/entities.js +3 -3
- package/lib/cjs/blue/errors.d.ts +4 -2
- package/lib/cjs/blue/errors.js +3 -1
- package/lib/cjs/blue/fetch.d.ts +5 -3
- package/lib/cjs/blue/fetch.js +3 -3
- package/lib/cjs/blue/types.d.ts +2 -4
- package/lib/cjs/blue/utils.d.ts +6 -4
- package/lib/cjs/blue/utils.js +3 -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 +30 -265
- package/lib/cjs/entities/blue/blue.js +1 -205
- package/lib/cjs/entities/index.d.ts +11 -66
- package/lib/cjs/entities/index.js +3 -21
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +13 -5
- package/lib/cjs/entities/vaultV1/vaultV1.js +16 -10
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +40 -19
- package/lib/cjs/entities/vaultV2/vaultV2.js +57 -26
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +4 -21
- package/lib/cjs/errors.d.ts +5 -47
- package/lib/cjs/errors.js +4 -46
- package/lib/cjs/fetch.d.ts +5 -17
- package/lib/cjs/fetch.js +3 -17
- package/lib/cjs/helpers/constant.d.ts +0 -2
- package/lib/cjs/helpers/constant.js +0 -2
- package/lib/cjs/helpers/index.d.ts +2 -3
- package/lib/cjs/helpers/index.js +1 -9
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +11 -5
- package/lib/cjs/helpers/slippage.d.ts +1 -103
- package/lib/cjs/helpers/slippage.js +0 -150
- package/lib/cjs/helpers/utilization.d.ts +12 -10
- package/lib/cjs/helpers/utilization.js +27 -12
- package/lib/cjs/helpers/validate.d.ts +1 -24
- package/lib/cjs/helpers/validate.js +2 -49
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +48 -15
- package/lib/cjs/midnight/errors.d.ts +1 -1
- package/lib/cjs/midnight/errors.js +2 -1
- package/lib/cjs/types/action.d.ts +17 -65
- package/lib/cjs/types/action.js +11 -4
- package/lib/cjs/types/error.d.ts +50 -381
- package/lib/cjs/types/error.js +64 -395
- package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
- package/lib/cjs/types.d.ts +2 -18
- package/lib/cjs/utils.d.ts +10 -35
- package/lib/cjs/utils.js +4 -36
- package/lib/esm/abis.d.ts +7 -2844
- package/lib/esm/abis.js +7 -1950
- 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 +1 -9
- 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 +2 -2
- 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 +6 -2
- package/lib/esm/actions/bundles/common.js +32 -6
- 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 +6 -8
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +12 -17
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
- 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 +5 -5
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +6 -11
- 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 +7 -5
- package/lib/esm/actions/vaultV1/inKindRedeem.js +11 -7
- 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 +1 -1
- package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -3
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +7 -5
- package/lib/esm/actions/vaultV2/inKindRedeem.js +11 -7
- package/lib/esm/augment/AccrualPosition.d.ts +1 -0
- package/lib/esm/augment.d.ts +0 -1
- package/lib/esm/augment.js +0 -1
- package/lib/esm/blue/abis.d.ts +5 -5
- package/lib/esm/blue/abis.js +5 -5
- package/lib/esm/blue/entities.d.ts +10 -8
- package/lib/esm/blue/entities.js +5 -3
- package/lib/esm/blue/errors.d.ts +4 -2
- package/lib/esm/blue/errors.js +4 -2
- package/lib/esm/blue/fetch.d.ts +5 -3
- package/lib/esm/blue/fetch.js +5 -3
- package/lib/esm/blue/types.d.ts +2 -4
- package/lib/esm/blue/utils.d.ts +6 -4
- package/lib/esm/blue/utils.js +6 -4
- 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 +30 -265
- package/lib/esm/entities/blue/blue.js +4 -208
- package/lib/esm/entities/index.d.ts +11 -66
- package/lib/esm/entities/index.js +5 -18
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +13 -5
- package/lib/esm/entities/vaultV1/vaultV1.js +19 -13
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +40 -19
- package/lib/esm/entities/vaultV2/vaultV2.js +61 -30
- package/lib/esm/entities/vaultV2BlueReallocationData.js +2 -19
- package/lib/esm/errors.d.ts +5 -47
- package/lib/esm/errors.js +5 -47
- package/lib/esm/fetch.d.ts +5 -17
- package/lib/esm/fetch.js +5 -17
- package/lib/esm/helpers/constant.d.ts +0 -2
- package/lib/esm/helpers/constant.js +0 -2
- package/lib/esm/helpers/index.d.ts +2 -3
- package/lib/esm/helpers/index.js +2 -3
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +11 -5
- package/lib/esm/helpers/slippage.d.ts +1 -103
- package/lib/esm/helpers/slippage.js +1 -147
- package/lib/esm/helpers/utilization.d.ts +12 -10
- package/lib/esm/helpers/utilization.js +26 -11
- package/lib/esm/helpers/validate.d.ts +1 -24
- package/lib/esm/helpers/validate.js +2 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +1 -3
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +48 -15
- package/lib/esm/midnight/errors.d.ts +1 -1
- package/lib/esm/midnight/errors.js +1 -1
- package/lib/esm/types/action.d.ts +17 -65
- package/lib/esm/types/action.js +12 -5
- package/lib/esm/types/error.d.ts +50 -381
- package/lib/esm/types/error.js +58 -377
- package/lib/esm/types/sharedLiquidity.d.ts +1 -117
- package/lib/esm/types.d.ts +2 -18
- package/lib/esm/utils.d.ts +10 -35
- package/lib/esm/utils.js +9 -34
- package/package.json +6 -17
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -88
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -152
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -53
- 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 -50
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -45
- 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 -11
- 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 -332
- package/lib/cjs/entities/vaultV1ReallocationData.js +0 -557
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -80
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -245
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -84
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -148
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -49
- 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 -50
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -41
- 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 -11
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -7
- 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 -332
- package/lib/esm/entities/vaultV1ReallocationData.js +0 -559
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -80
- package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -241
|
@@ -1,559 +0,0 @@
|
|
|
1
|
-
import { AccrualPosition, Market, MarketUtils, MathLib, Position, UnknownDataError, Vault, VaultMarketConfig, VaultMarketPublicAllocatorConfig, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { _try, bigIntComparator } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, } from "../helpers/constant.js";
|
|
4
|
-
import { getSupplyTargetUtilization } from "../helpers/utilization.js";
|
|
5
|
-
import { DisabledReallocationMarketError, MissingPublicAllocatorConfigError, UnknownReallocationMarketError, UnknownReallocationPositionError, UnknownReallocationVaultError, UnknownReallocationVaultMarketConfigError, } from "../types/index.js";
|
|
6
|
-
/**
|
|
7
|
-
* Clones a market so simulated interest and liquidity changes never mutate caller input.
|
|
8
|
-
*
|
|
9
|
-
* @internal
|
|
10
|
-
*/
|
|
11
|
-
const cloneMarket = (market) => new Market({ ...market });
|
|
12
|
-
/**
|
|
13
|
-
* Clones a market position before simulated supply or withdrawal changes are applied.
|
|
14
|
-
*
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
const clonePosition = (position) => new Position({ ...position });
|
|
18
|
-
/**
|
|
19
|
-
* Clones vault state, including queue arrays and public allocator accounting.
|
|
20
|
-
*
|
|
21
|
-
* @internal
|
|
22
|
-
*/
|
|
23
|
-
const cloneVault = (vault) => new Vault({
|
|
24
|
-
...vault,
|
|
25
|
-
pendingTimelock: { ...vault.pendingTimelock },
|
|
26
|
-
pendingGuardian: { ...vault.pendingGuardian },
|
|
27
|
-
supplyQueue: [...vault.supplyQueue],
|
|
28
|
-
withdrawQueue: [...vault.withdrawQueue],
|
|
29
|
-
publicAllocatorConfig: vault.publicAllocatorConfig == null
|
|
30
|
-
? undefined
|
|
31
|
-
: { ...vault.publicAllocatorConfig },
|
|
32
|
-
});
|
|
33
|
-
/**
|
|
34
|
-
* Clones a vault-market config, including nested public allocator limits.
|
|
35
|
-
*
|
|
36
|
-
* @internal
|
|
37
|
-
*/
|
|
38
|
-
const cloneVaultMarketConfig = (config) => new VaultMarketConfig({
|
|
39
|
-
...config,
|
|
40
|
-
pendingCap: { ...config.pendingCap },
|
|
41
|
-
publicAllocatorConfig: config.publicAllocatorConfig == null
|
|
42
|
-
? undefined
|
|
43
|
-
: new VaultMarketPublicAllocatorConfig({
|
|
44
|
-
...config.publicAllocatorConfig,
|
|
45
|
-
}),
|
|
46
|
-
});
|
|
47
|
-
/**
|
|
48
|
-
* Narrow state container for computing public allocator reallocations.
|
|
49
|
-
*
|
|
50
|
-
* @remarks
|
|
51
|
-
* The class owns only the market, vault, position, vault-market-config,
|
|
52
|
-
* and chain data needed by the shared-liquidity algorithm. Constructor inputs
|
|
53
|
-
* are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances
|
|
54
|
-
* so fetched caller inputs are not mutated.
|
|
55
|
-
*
|
|
56
|
-
* Public records are exposed for inspection and snapshotting only. Treat
|
|
57
|
-
* `markets`, `vaults`, `positions`, and `vaultMarketConfigs` as a read
|
|
58
|
-
* contract keyed by market id or address; use the getters for typed absence
|
|
59
|
-
* errors and use simulation methods to produce updated state.
|
|
60
|
-
*
|
|
61
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
62
|
-
* `VaultV2BlueReallocationData`.
|
|
63
|
-
*/
|
|
64
|
-
export class VaultV1ReallocationData {
|
|
65
|
-
/** Chain id associated with the fetched reallocation data. */
|
|
66
|
-
chainId;
|
|
67
|
-
/** Markets indexed by market id. */
|
|
68
|
-
markets;
|
|
69
|
-
/** Vaults indexed by vault address. */
|
|
70
|
-
vaults;
|
|
71
|
-
/** Positions indexed by user or vault address, then by market id. */
|
|
72
|
-
positions;
|
|
73
|
-
/** Vault market configs indexed by vault address, then by market id. */
|
|
74
|
-
vaultMarketConfigs;
|
|
75
|
-
/**
|
|
76
|
-
* Creates a cloned reallocation state from fetched market, vault, position,
|
|
77
|
-
* and vault-market-config data.
|
|
78
|
-
*
|
|
79
|
-
* @param input - Reallocation input data fetched at a consistent chain state.
|
|
80
|
-
*/
|
|
81
|
-
constructor(input) {
|
|
82
|
-
const { chainId, markets, vaults, positions, vaultMarketConfigs } = input;
|
|
83
|
-
this.chainId = chainId;
|
|
84
|
-
this.markets = {};
|
|
85
|
-
this.vaults = {};
|
|
86
|
-
this.positions = {};
|
|
87
|
-
this.vaultMarketConfigs = {};
|
|
88
|
-
for (const [id, market] of Object.entries(markets ?? {})) {
|
|
89
|
-
this.markets[id] = market == null ? undefined : cloneMarket(market);
|
|
90
|
-
}
|
|
91
|
-
for (const [address, vault] of Object.entries(vaults ?? {})) {
|
|
92
|
-
this.vaults[address] = vault == null ? undefined : cloneVault(vault);
|
|
93
|
-
}
|
|
94
|
-
for (const [user, byMarket] of Object.entries(positions ?? {})) {
|
|
95
|
-
this.positions[user] = {};
|
|
96
|
-
for (const [id, position] of Object.entries(byMarket)) {
|
|
97
|
-
this.positions[user][id] =
|
|
98
|
-
position == null ? undefined : clonePosition(position);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
for (const [vault, byMarket] of Object.entries(vaultMarketConfigs ?? {})) {
|
|
102
|
-
this.vaultMarketConfigs[vault] = {};
|
|
103
|
-
for (const [id, config] of Object.entries(byMarket)) {
|
|
104
|
-
this.vaultMarketConfigs[vault][id] =
|
|
105
|
-
config == null ? undefined : cloneVaultMarketConfig(config);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Creates a deep clone of this reallocation state.
|
|
111
|
-
*
|
|
112
|
-
* @returns A new `VaultV1ReallocationData` instance with cloned entity objects.
|
|
113
|
-
*/
|
|
114
|
-
clone() {
|
|
115
|
-
return new VaultV1ReallocationData(this);
|
|
116
|
-
}
|
|
117
|
-
forkAliasedState() {
|
|
118
|
-
const data = new VaultV1ReallocationData({ chainId: this.chainId });
|
|
119
|
-
Object.assign(data.markets, this.markets);
|
|
120
|
-
Object.assign(data.vaults, this.vaults);
|
|
121
|
-
for (const [user, positions] of Object.entries(this.positions)) {
|
|
122
|
-
data.positions[user] = positions;
|
|
123
|
-
}
|
|
124
|
-
for (const [vault, configs] of Object.entries(this.vaultMarketConfigs)) {
|
|
125
|
-
data.vaultMarketConfigs[vault] = configs;
|
|
126
|
-
}
|
|
127
|
-
return data;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Gets a market by id.
|
|
131
|
-
*
|
|
132
|
-
* @param marketId - Market id to read.
|
|
133
|
-
* @returns The cloned market data.
|
|
134
|
-
* @throws {@link UnknownReallocationMarketError} when the market is absent.
|
|
135
|
-
*/
|
|
136
|
-
getMarket(marketId) {
|
|
137
|
-
const market = this.markets[marketId];
|
|
138
|
-
if (market == null)
|
|
139
|
-
throw new UnknownReallocationMarketError(marketId);
|
|
140
|
-
return market;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Gets a vault by address.
|
|
144
|
-
*
|
|
145
|
-
* @param vault - Vault address to read.
|
|
146
|
-
* @returns The cloned vault data.
|
|
147
|
-
* @throws {@link UnknownReallocationVaultError} when the vault is absent.
|
|
148
|
-
*/
|
|
149
|
-
getVault(vault) {
|
|
150
|
-
const data = this.vaults[vault];
|
|
151
|
-
if (data == null)
|
|
152
|
-
throw new UnknownReallocationVaultError(vault);
|
|
153
|
-
return data;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Gets a raw market position.
|
|
157
|
-
*
|
|
158
|
-
* @param user - Position owner address, usually a MetaMorpho vault.
|
|
159
|
-
* @param marketId - Market id for the position.
|
|
160
|
-
* @returns The cloned position data.
|
|
161
|
-
* @throws {@link UnknownReallocationPositionError} when the position is absent.
|
|
162
|
-
*/
|
|
163
|
-
getPosition(user, marketId) {
|
|
164
|
-
const position = this.positions[user]?.[marketId];
|
|
165
|
-
if (position == null)
|
|
166
|
-
throw new UnknownReallocationPositionError(user, marketId);
|
|
167
|
-
return position;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Gets a position wrapped with its market accrual helpers.
|
|
171
|
-
*
|
|
172
|
-
* @param user - Position owner address, usually a MetaMorpho vault.
|
|
173
|
-
* @param marketId - Market id for the position.
|
|
174
|
-
* @returns Accrual-aware position data.
|
|
175
|
-
* @throws {@link UnknownReallocationPositionError} when the position is absent.
|
|
176
|
-
* @throws {@link UnknownReallocationMarketError} when the market is absent.
|
|
177
|
-
*/
|
|
178
|
-
getAccrualPosition(user, marketId) {
|
|
179
|
-
return new AccrualPosition(this.getPosition(user, marketId), this.getMarket(marketId));
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Gets a vault-market config.
|
|
183
|
-
*
|
|
184
|
-
* @param vault - Vault address.
|
|
185
|
-
* @param marketId - Market id configured by the vault.
|
|
186
|
-
* @returns The cloned vault-market config.
|
|
187
|
-
* @throws {@link UnknownReallocationVaultMarketConfigError} when the config is absent.
|
|
188
|
-
*/
|
|
189
|
-
getVaultMarketConfig(vault, marketId) {
|
|
190
|
-
const config = this.vaultMarketConfigs[vault]?.[marketId];
|
|
191
|
-
if (config == null)
|
|
192
|
-
throw new UnknownReallocationVaultMarketConfigError(vault, marketId);
|
|
193
|
-
return config;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Calculates public reallocations that can supply liquidity to `marketId`.
|
|
197
|
-
*
|
|
198
|
-
* @remarks
|
|
199
|
-
* The algorithm repeatedly chooses the single largest currently available
|
|
200
|
-
* source-market withdrawal across reallocatable vaults, applies that
|
|
201
|
-
* withdrawal to a cloned state, and repeats until no valid withdrawal remains.
|
|
202
|
-
*
|
|
203
|
-
* Pass `options.timestamp` to evaluate market interest and pending public
|
|
204
|
-
* allocator caps at the same block as the fetched reallocation data.
|
|
205
|
-
* This method does not add an implicit delay margin before measuring
|
|
206
|
-
* target-market vault headroom. If a transaction may land later than the
|
|
207
|
-
* fetched block, pass a future `timestamp` or reserve your own headroom so
|
|
208
|
-
* interest accrued before inclusion does not make `reallocateTo` exceed the
|
|
209
|
-
* target market cap.
|
|
210
|
-
* Returned `data` normalizes `vault.publicAllocatorConfig.accruedFee` to the
|
|
211
|
-
* on-chain `reallocateTo` fee semantics: one fee charge per vault with at
|
|
212
|
-
* least one computed withdrawal.
|
|
213
|
-
* Sources with zero allocator withdrawal capacity and destinations with no deposit capacity
|
|
214
|
-
* are skipped before projecting source interest.
|
|
215
|
-
*
|
|
216
|
-
* @param marketId - Target market to supply with shared liquidity.
|
|
217
|
-
* @param options - Optional allocator discovery options.
|
|
218
|
-
* @returns Computed source-market withdrawals and the post-reallocation state.
|
|
219
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
220
|
-
* @throws {@link UnknownReallocationMarketError} when the target market is absent.
|
|
221
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
222
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
223
|
-
* @example
|
|
224
|
-
* ```ts
|
|
225
|
-
* import { createPublicClient, http } from "viem";
|
|
226
|
-
* import { mainnet } from "viem/chains";
|
|
227
|
-
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
228
|
-
* import {
|
|
229
|
-
* morphoViemExtension,
|
|
230
|
-
* type PublicReallocation,
|
|
231
|
-
* } from "@morpho-org/morpho-sdk";
|
|
232
|
-
* import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
|
|
233
|
-
*
|
|
234
|
-
* const client = createPublicClient({
|
|
235
|
-
* chain: mainnet,
|
|
236
|
-
* transport: http(),
|
|
237
|
-
* }).extend(morphoViemExtension());
|
|
238
|
-
*
|
|
239
|
-
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
240
|
-
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
241
|
-
* const block = await client.getBlock();
|
|
242
|
-
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
243
|
-
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
244
|
-
* block: { number: block.number, timestamp: block.timestamp },
|
|
245
|
-
* });
|
|
246
|
-
*
|
|
247
|
-
* const result: {
|
|
248
|
-
* withdrawals: readonly PublicReallocation[];
|
|
249
|
-
* data: VaultV1ReallocationData;
|
|
250
|
-
* } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
|
|
251
|
-
* timestamp: block.timestamp,
|
|
252
|
-
* });
|
|
253
|
-
* ```
|
|
254
|
-
*/
|
|
255
|
-
computeVaultV1Reallocations(marketId, options = {}) {
|
|
256
|
-
const { enabled = true, timestamp, reallocatableVaults, defaultMaxWithdrawalUtilization = DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, maxWithdrawalUtilization = {}, } = options;
|
|
257
|
-
if (!enabled)
|
|
258
|
-
return { withdrawals: [], data: this };
|
|
259
|
-
const accrualTimestamp = BigInt(timestamp ?? this.getMarket(marketId).lastUpdate);
|
|
260
|
-
const configuredVaults = Object.keys(this.vaultMarketConfigs);
|
|
261
|
-
const vaultKeyByLower = new Map(configuredVaults.map((vault) => [vault.toLowerCase(), vault]));
|
|
262
|
-
const vaults = Array.from(new Set([...(reallocatableVaults ?? configuredVaults)]
|
|
263
|
-
.map((vault) => vaultKeyByLower.get(vault.toLowerCase()))
|
|
264
|
-
.filter((vault) => vault != null))).filter((vault) => {
|
|
265
|
-
const vaultMarketConfig = this.vaultMarketConfigs[vault]?.[marketId];
|
|
266
|
-
return (vaultMarketConfig?.enabled === true &&
|
|
267
|
-
this.vaults[vault]?.publicAllocatorConfig != null);
|
|
268
|
-
});
|
|
269
|
-
let data = this.clone();
|
|
270
|
-
const withdrawals = [];
|
|
271
|
-
while (true) {
|
|
272
|
-
const vaultWithdrawals = vaults
|
|
273
|
-
.map((vaultAddress) => data.getLargestVaultWithdrawal({
|
|
274
|
-
vault: vaultAddress,
|
|
275
|
-
marketId,
|
|
276
|
-
withdrawals,
|
|
277
|
-
defaultMaxWithdrawalUtilization,
|
|
278
|
-
maxWithdrawalUtilization,
|
|
279
|
-
timestamp: accrualTimestamp,
|
|
280
|
-
}))
|
|
281
|
-
.filter((withdrawal) => withdrawal?.largestWithdrawal != null)
|
|
282
|
-
.sort(bigIntComparator((withdrawal) => withdrawal.largestWithdrawal.assets, "desc"));
|
|
283
|
-
const largestVaultWithdrawal = vaultWithdrawals[0];
|
|
284
|
-
if (largestVaultWithdrawal?.largestWithdrawal == null) {
|
|
285
|
-
const withdrawalCountsByVault = new Map();
|
|
286
|
-
for (const { vault } of withdrawals) {
|
|
287
|
-
withdrawalCountsByVault.set(vault, (withdrawalCountsByVault.get(vault) ?? 0) + 1);
|
|
288
|
-
}
|
|
289
|
-
for (const [vault, count] of withdrawalCountsByVault) {
|
|
290
|
-
if (count <= 1)
|
|
291
|
-
continue;
|
|
292
|
-
const vaultPublicAllocatorConfig = data.getVault(vault).publicAllocatorConfig;
|
|
293
|
-
/* v8 ignore next: repeated withdrawals require a configured public allocator earlier in the flow. */
|
|
294
|
-
if (vaultPublicAllocatorConfig == null)
|
|
295
|
-
throw new MissingPublicAllocatorConfigError(vault);
|
|
296
|
-
vaultPublicAllocatorConfig.accruedFee -=
|
|
297
|
-
BigInt(count - 1) * vaultPublicAllocatorConfig.fee;
|
|
298
|
-
}
|
|
299
|
-
return { withdrawals, data };
|
|
300
|
-
}
|
|
301
|
-
const { vault, largestWithdrawal } = largestVaultWithdrawal;
|
|
302
|
-
withdrawals.push({ ...largestWithdrawal, vault });
|
|
303
|
-
data = data.applyPublicReallocation({
|
|
304
|
-
vault,
|
|
305
|
-
supplyMarketId: marketId,
|
|
306
|
-
withdrawal: largestWithdrawal,
|
|
307
|
-
timestamp: accrualTimestamp,
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Calculates Vault V1 public reallocations that can supply liquidity to `marketId`.
|
|
313
|
-
*
|
|
314
|
-
* @param marketId - Target market to supply with shared liquidity.
|
|
315
|
-
* @param options - Optional allocator discovery options.
|
|
316
|
-
* @returns Computed source-market withdrawals and the post-reallocation state.
|
|
317
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
318
|
-
* @throws {@link UnknownReallocationMarketError} when the target market is absent.
|
|
319
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
320
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
321
|
-
*/
|
|
322
|
-
getMarketPublicReallocations(marketId, options = {}) {
|
|
323
|
-
return this.computeVaultV1Reallocations(marketId, options);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Sums the public-allocator liquidity reallocatable into `marketId` from
|
|
327
|
-
* sibling markets.
|
|
328
|
-
*
|
|
329
|
-
* Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
|
|
330
|
-
* each source market's withdrawal utilization cap and the target market's
|
|
331
|
-
* vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
|
|
332
|
-
* widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
|
|
333
|
-
*
|
|
334
|
-
* @param marketId - Target market that would receive the liquidity.
|
|
335
|
-
* @param options - Optional allocator discovery options.
|
|
336
|
-
* @returns Total reallocatable assets in loan-token units; `0n` when none is available.
|
|
337
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
338
|
-
* @throws {@link UnknownReallocationMarketError} when the target market is absent.
|
|
339
|
-
* @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use
|
|
340
|
-
* `VaultV2BlueReallocationData.getPublicReallocationLiquidity`.
|
|
341
|
-
* @example
|
|
342
|
-
* ```ts
|
|
343
|
-
* import { createPublicClient, http } from "viem";
|
|
344
|
-
* import { mainnet } from "viem/chains";
|
|
345
|
-
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
346
|
-
* import { morphoViemExtension } from "@morpho-org/morpho-sdk";
|
|
347
|
-
*
|
|
348
|
-
* const client = createPublicClient({
|
|
349
|
-
* chain: mainnet,
|
|
350
|
-
* transport: http(),
|
|
351
|
-
* }).extend(morphoViemExtension());
|
|
352
|
-
*
|
|
353
|
-
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
354
|
-
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
355
|
-
* const block = await client.getBlock();
|
|
356
|
-
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
357
|
-
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
358
|
-
* block: { number: block.number, timestamp: block.timestamp },
|
|
359
|
-
* });
|
|
360
|
-
*
|
|
361
|
-
* const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
|
|
362
|
-
* marketParams.id,
|
|
363
|
-
* { timestamp: block.timestamp },
|
|
364
|
-
* );
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
367
|
-
getPublicReallocationLiquidity(marketId, options) {
|
|
368
|
-
const { withdrawals } = this.computeVaultV1Reallocations(marketId, options);
|
|
369
|
-
return withdrawals.reduce((total, { assets }) => total + assets, 0n);
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Computes the liquidity available to bring `marketId` to `utilization`,
|
|
373
|
-
* counting the public-allocator liquidity reallocatable into it.
|
|
374
|
-
*
|
|
375
|
-
* Returns the max borrow `x` keeping post-borrow utilization
|
|
376
|
-
* `(borrow + x) / (supply + L) ≤ utilization`, where `L` is the
|
|
377
|
-
* reallocatable liquidity added to the market's supply — equivalently
|
|
378
|
-
* `getBorrowToUtilization({ supply + L, borrow }, utilization)`. Below
|
|
379
|
-
* `utilization` this is the market's own borrow headroom plus `utilization · L`;
|
|
380
|
-
* reallocated supply also raises the supply denominator, so only that scaled
|
|
381
|
-
* share backs further borrow.
|
|
382
|
-
*
|
|
383
|
-
* Read-only metric — never throws on insufficiency:
|
|
384
|
-
* - returns only the market's own borrow headroom when
|
|
385
|
-
* `supplyTargetUtilization > utilization` (reallocation would not
|
|
386
|
-
* trigger at that utilization);
|
|
387
|
-
* - returns `0n` when the market is already at or above `utilization` and `L`
|
|
388
|
-
* is too small to bring it back under.
|
|
389
|
-
*
|
|
390
|
-
* @param marketId - Target market to borrow from.
|
|
391
|
-
* @param utilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
|
|
392
|
-
* @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
|
|
393
|
-
* @returns Available liquidity to the given utilization in loan-token units; `0n` when none is available.
|
|
394
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
395
|
-
* @throws {@link UnknownReallocationMarketError} when the target market is absent.
|
|
396
|
-
* @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use
|
|
397
|
-
* `VaultV2BlueReallocationData.getAvailableLiquidityToUtilization`.
|
|
398
|
-
* @example
|
|
399
|
-
* ```ts
|
|
400
|
-
* import { createPublicClient, http, parseEther } from "viem";
|
|
401
|
-
* import { mainnet } from "viem/chains";
|
|
402
|
-
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
403
|
-
* import { morphoViemExtension } from "@morpho-org/morpho-sdk";
|
|
404
|
-
*
|
|
405
|
-
* const client = createPublicClient({
|
|
406
|
-
* chain: mainnet,
|
|
407
|
-
* transport: http(),
|
|
408
|
-
* }).extend(morphoViemExtension());
|
|
409
|
-
*
|
|
410
|
-
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
411
|
-
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
412
|
-
* const block = await client.getBlock();
|
|
413
|
-
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
414
|
-
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
415
|
-
* block: { number: block.number, timestamp: block.timestamp },
|
|
416
|
-
* });
|
|
417
|
-
*
|
|
418
|
-
* // Max borrow keeping utilization at or below 90%, counting shared liquidity.
|
|
419
|
-
* const available: bigint =
|
|
420
|
-
* reallocationData.getAvailableLiquidityToUtilization(
|
|
421
|
-
* marketParams.id,
|
|
422
|
-
* parseEther("0.9"),
|
|
423
|
-
* { timestamp: block.timestamp },
|
|
424
|
-
* );
|
|
425
|
-
* ```
|
|
426
|
-
*/
|
|
427
|
-
// biome-ignore lint/complexity/useMaxParams: (marketId, utilization, options) is the metric's public API
|
|
428
|
-
getAvailableLiquidityToUtilization(marketId, utilization = DEFAULT_SUPPLY_TARGET_UTILIZATION, options) {
|
|
429
|
-
const market = this.getMarket(marketId).accrueInterest(options?.timestamp);
|
|
430
|
-
// Below the allocator's trigger, no reallocation happens: own headroom only.
|
|
431
|
-
const supplyTargetUtilization = getSupplyTargetUtilization(marketId, options);
|
|
432
|
-
if (supplyTargetUtilization > utilization)
|
|
433
|
-
return market.getBorrowToUtilization(utilization);
|
|
434
|
-
// Borrow-to-target on the post-reallocation supply; clamps to 0n above target.
|
|
435
|
-
const availableLiquidity = this.getPublicReallocationLiquidity(marketId, options);
|
|
436
|
-
return MarketUtils.getBorrowToUtilization({
|
|
437
|
-
totalSupplyAssets: market.totalSupplyAssets + availableLiquidity,
|
|
438
|
-
totalBorrowAssets: market.totalBorrowAssets,
|
|
439
|
-
}, utilization);
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* Gets the largest currently valid source-market withdrawal for a vault.
|
|
443
|
-
*
|
|
444
|
-
* @param params - Candidate vault, target market, prior withdrawals, and allocator limits.
|
|
445
|
-
* @returns The largest withdrawal candidate, or `undefined` if required data is missing.
|
|
446
|
-
*
|
|
447
|
-
* @internal
|
|
448
|
-
*/
|
|
449
|
-
getLargestVaultWithdrawal({ vault, marketId, withdrawals, defaultMaxWithdrawalUtilization, maxWithdrawalUtilization, timestamp, }) {
|
|
450
|
-
return _try(() => {
|
|
451
|
-
const { cap, pendingCap, publicAllocatorConfig } = this.getVaultMarketConfig(vault, marketId);
|
|
452
|
-
const maxIn = publicAllocatorConfig?.maxIn ?? 0n;
|
|
453
|
-
const validCap = pendingCap.validAt >= timestamp
|
|
454
|
-
? MathLib.min(pendingCap.value, cap)
|
|
455
|
-
: cap;
|
|
456
|
-
if (maxIn === 0n || validCap === 0n)
|
|
457
|
-
return { vault };
|
|
458
|
-
const suppliable = MathLib.zeroFloorSub(validCap, this.getAccrualPosition(vault, marketId).accrueInterest(timestamp)
|
|
459
|
-
.supplyAssets);
|
|
460
|
-
if (suppliable === 0n)
|
|
461
|
-
return { vault };
|
|
462
|
-
const marketWithdrawals = this.getVault(vault)
|
|
463
|
-
.withdrawQueue.filter((srcMarketId) => srcMarketId !== marketId &&
|
|
464
|
-
!withdrawals.some((withdrawal) => withdrawal.id === srcMarketId && withdrawal.vault === vault))
|
|
465
|
-
.map((srcMarketId) => {
|
|
466
|
-
const withdrawal = _try(() => {
|
|
467
|
-
const srcConfig = this.getVaultMarketConfig(vault, srcMarketId);
|
|
468
|
-
const maxOut = srcConfig.publicAllocatorConfig?.maxOut ?? 0n;
|
|
469
|
-
if (!srcConfig.enabled || maxOut === 0n)
|
|
470
|
-
return { id: srcMarketId, assets: 0n };
|
|
471
|
-
const rawSrcPosition = this.getAccrualPosition(vault, srcMarketId);
|
|
472
|
-
if (rawSrcPosition.supplyShares === 0n)
|
|
473
|
-
return { id: srcMarketId, assets: 0n };
|
|
474
|
-
const srcPosition = rawSrcPosition.accrueInterest(timestamp);
|
|
475
|
-
const targetUtilizationLiquidity = srcPosition.market.getWithdrawToUtilization(maxWithdrawalUtilization[srcMarketId] ??
|
|
476
|
-
defaultMaxWithdrawalUtilization);
|
|
477
|
-
return {
|
|
478
|
-
id: srcMarketId,
|
|
479
|
-
assets: MathLib.min(srcPosition.supplyAssets, targetUtilizationLiquidity, suppliable, maxIn, maxOut),
|
|
480
|
-
};
|
|
481
|
-
}, UnknownDataError);
|
|
482
|
-
return withdrawal ?? { id: srcMarketId, assets: 0n };
|
|
483
|
-
})
|
|
484
|
-
.filter(({ assets }) => assets > 0n)
|
|
485
|
-
.sort(bigIntComparator(({ assets }) => assets, "desc"));
|
|
486
|
-
return { vault, largestWithdrawal: marketWithdrawals[0] };
|
|
487
|
-
}, UnknownDataError);
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Applies one public allocator withdrawal and corresponding target-market supply.
|
|
491
|
-
*
|
|
492
|
-
* @param vault - Vault performing the public reallocation.
|
|
493
|
-
* @param supplyMarketId - Target market that receives the withdrawn assets.
|
|
494
|
-
* @param withdrawal - Source market id and asset amount to withdraw.
|
|
495
|
-
* @param timestamp - Timestamp used to accrue source and target positions.
|
|
496
|
-
* @returns A cloned state after applying the simulated reallocation.
|
|
497
|
-
* @throws {@link MissingPublicAllocatorConfigError} when the vault allocator config is absent.
|
|
498
|
-
* @throws {@link UnknownReallocationVaultMarketConfigError} when source or target allocator limits are absent.
|
|
499
|
-
*
|
|
500
|
-
* @internal
|
|
501
|
-
*/
|
|
502
|
-
applyPublicReallocation({ vault, supplyMarketId, withdrawal, timestamp, }) {
|
|
503
|
-
const data = this.forkAliasedState();
|
|
504
|
-
data.vaults[vault] = cloneVault(this.getVault(vault));
|
|
505
|
-
/* v8 ignore next: forkAliasedState keeps vault position buckets initialized for known vaults. */
|
|
506
|
-
data.positions[vault] = { ...(this.positions[vault] ?? {}) };
|
|
507
|
-
/* v8 ignore next: forkAliasedState keeps vault market config buckets initialized for known vaults. */
|
|
508
|
-
data.vaultMarketConfigs[vault] = {
|
|
509
|
-
...(this.vaultMarketConfigs[vault] ?? {}),
|
|
510
|
-
};
|
|
511
|
-
const vaultPublicAllocatorConfig = data.getVault(vault).publicAllocatorConfig;
|
|
512
|
-
if (vaultPublicAllocatorConfig == null)
|
|
513
|
-
throw new MissingPublicAllocatorConfigError(vault);
|
|
514
|
-
vaultPublicAllocatorConfig.accruedFee += vaultPublicAllocatorConfig.fee;
|
|
515
|
-
const sourceConfig = cloneVaultMarketConfig(this.getVaultMarketConfig(vault, withdrawal.id));
|
|
516
|
-
data.vaultMarketConfigs[vault][withdrawal.id] = sourceConfig;
|
|
517
|
-
if (!sourceConfig.enabled)
|
|
518
|
-
throw new DisabledReallocationMarketError(vault, withdrawal.id);
|
|
519
|
-
const sourcePublicAllocatorConfig = sourceConfig.publicAllocatorConfig;
|
|
520
|
-
if (sourcePublicAllocatorConfig == null)
|
|
521
|
-
throw new UnknownReallocationVaultMarketConfigError(vault, withdrawal.id);
|
|
522
|
-
const supplyConfig = supplyMarketId === withdrawal.id
|
|
523
|
-
? sourceConfig
|
|
524
|
-
: cloneVaultMarketConfig(this.getVaultMarketConfig(vault, supplyMarketId));
|
|
525
|
-
data.vaultMarketConfigs[vault][supplyMarketId] = supplyConfig;
|
|
526
|
-
if (!supplyConfig.enabled)
|
|
527
|
-
throw new DisabledReallocationMarketError(vault, supplyMarketId);
|
|
528
|
-
const supplyPublicAllocatorConfig = supplyConfig.publicAllocatorConfig;
|
|
529
|
-
if (supplyPublicAllocatorConfig == null)
|
|
530
|
-
throw new UnknownReallocationVaultMarketConfigError(vault, supplyMarketId);
|
|
531
|
-
sourcePublicAllocatorConfig.maxIn += withdrawal.assets;
|
|
532
|
-
sourcePublicAllocatorConfig.maxOut -= withdrawal.assets;
|
|
533
|
-
supplyPublicAllocatorConfig.maxIn -= withdrawal.assets;
|
|
534
|
-
supplyPublicAllocatorConfig.maxOut += withdrawal.assets;
|
|
535
|
-
const { position: sourcePosition } = data
|
|
536
|
-
.getAccrualPosition(vault, withdrawal.id)
|
|
537
|
-
.withdraw(withdrawal.assets, 0n, timestamp);
|
|
538
|
-
data.markets[withdrawal.id] = cloneMarket(sourcePosition.market);
|
|
539
|
-
data.positions[vault] ??= {};
|
|
540
|
-
data.positions[vault][withdrawal.id] = clonePosition(sourcePosition);
|
|
541
|
-
const { position: supplyPosition } = data
|
|
542
|
-
.getAccrualPosition(vault, supplyMarketId)
|
|
543
|
-
.supply(withdrawal.assets, 0n, timestamp);
|
|
544
|
-
data.markets[supplyMarketId] = cloneMarket(supplyPosition.market);
|
|
545
|
-
data.positions[vault][supplyMarketId] = clonePosition(supplyPosition);
|
|
546
|
-
void _try(() => {
|
|
547
|
-
const vaultData = data.getVault(vault);
|
|
548
|
-
vaultData.totalAssets = vaultData.withdrawQueue.reduce((total, marketId) => total + data.getAccrualPosition(vault, marketId).supplyAssets, 0n);
|
|
549
|
-
}, UnknownDataError);
|
|
550
|
-
return data;
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* Deprecated class name for Vault V1 reallocation data.
|
|
555
|
-
*
|
|
556
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
557
|
-
* `VaultV2BlueReallocationData`.
|
|
558
|
-
*/
|
|
559
|
-
export { VaultV1ReallocationData as ReallocationData };
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type { VaultV1ReallocationData } from "../entities/vaultV1ReallocationData.js";
|
|
3
|
-
import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../types/index.js";
|
|
4
|
-
/**
|
|
5
|
-
* Computes vault reallocations for a `borrow` or `withdraw` on a target market.
|
|
6
|
-
*
|
|
7
|
-
* First attempts "friendly" reallocations respecting withdrawal utilization
|
|
8
|
-
* targets, then falls back to aggressive reallocations (100% withdrawal
|
|
9
|
-
* utilization) if liquidity is still insufficient.
|
|
10
|
-
*
|
|
11
|
-
* Algebra branches on `operation`:
|
|
12
|
-
* - `"borrow"`: `S' = S`, `B' = B + amount` (additional borrow demand).
|
|
13
|
-
* - `"withdraw"`: `S' = S − amount`, `B' = B` (supply-side shrinkage).
|
|
14
|
-
*
|
|
15
|
-
* In both cases reallocated assets are added on the supply side; `requiredAssets`
|
|
16
|
-
* and `absoluteShortfall` are derived from the operation-specific post-state.
|
|
17
|
-
*
|
|
18
|
-
* @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.
|
|
19
|
-
* @param params.reallocationData - The local state containing market, vault, and position data.
|
|
20
|
-
* @param params.marketId - The target market to reallocate liquidity into.
|
|
21
|
-
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
22
|
-
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
23
|
-
* @param params.options - Optional reallocation computation options.
|
|
24
|
-
* @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
|
|
25
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
26
|
-
* @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.
|
|
27
|
-
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets` — the on-chain call would revert regardless of reallocations.
|
|
28
|
-
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
29
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
30
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* import { createPublicClient, http, parseUnits } from "viem";
|
|
34
|
-
* import { mainnet } from "viem/chains";
|
|
35
|
-
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
36
|
-
* import {
|
|
37
|
-
* computeVaultV1Reallocations,
|
|
38
|
-
* morphoViemExtension,
|
|
39
|
-
* } from "@morpho-org/morpho-sdk";
|
|
40
|
-
*
|
|
41
|
-
* const client = createPublicClient({
|
|
42
|
-
* chain: mainnet,
|
|
43
|
-
* transport: http(),
|
|
44
|
-
* }).extend(morphoViemExtension());
|
|
45
|
-
*
|
|
46
|
-
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
47
|
-
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
48
|
-
* const block = await client.getBlock();
|
|
49
|
-
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
50
|
-
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
51
|
-
* block: { number: block.number, timestamp: block.timestamp },
|
|
52
|
-
* });
|
|
53
|
-
* const borrowAmount = parseUnits("1000", 6);
|
|
54
|
-
* const reallocations = computeVaultV1Reallocations({
|
|
55
|
-
* reallocationData,
|
|
56
|
-
* marketId: marketParams.id,
|
|
57
|
-
* operation: "borrow",
|
|
58
|
-
* amount: borrowAmount,
|
|
59
|
-
* options: { timestamp: block.timestamp },
|
|
60
|
-
* });
|
|
61
|
-
* // Encode `reallocations` explicitly with
|
|
62
|
-
* // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
|
|
63
|
-
* // High-level Blue writes accept Vault V2 reallocations only.
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export declare const computeVaultV1Reallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
|
|
67
|
-
readonly reallocationData: VaultV1ReallocationData;
|
|
68
|
-
readonly marketId: MarketId;
|
|
69
|
-
readonly operation: "borrow" | "withdraw";
|
|
70
|
-
readonly amount: bigint;
|
|
71
|
-
readonly options?: ReallocationComputeOptions;
|
|
72
|
-
}) => readonly VaultV1Reallocation[];
|
|
73
|
-
/**
|
|
74
|
-
* Deprecated name for the Vault V1 amount-aware reallocation planner.
|
|
75
|
-
*
|
|
76
|
-
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
77
|
-
* @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
|
|
78
|
-
* `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
|
|
79
|
-
*/
|
|
80
|
-
export declare const computeReallocations: typeof computeVaultV1Reallocations;
|