@morpho-org/morpho-sdk 9.1.0 → 9.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/README.md +4 -0
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +3 -1
- package/lib/cjs/blue/constants.d.ts +1 -1
- package/lib/cjs/blue/constants.js +2 -1
- package/lib/cjs/constants.d.ts +2 -2
- package/lib/cjs/constants.js +4 -1
- package/lib/cjs/entities/blue/blue.d.ts +71 -16
- package/lib/cjs/entities/blue/blue.js +32 -4
- package/lib/cjs/entities/midnight/midnight.d.ts +2 -1
- package/lib/cjs/entities/midnight/midnight.js +5 -4
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +8 -0
- package/lib/cjs/entities/vaultV1/vaultV1.js +5 -0
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +4 -0
- package/lib/cjs/entities/vaultV2/vaultV2.js +3 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +37 -4
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +197 -32
- package/lib/cjs/helpers/constant.d.ts +7 -0
- package/lib/cjs/helpers/constant.js +8 -1
- package/lib/cjs/helpers/deadShares.d.ts +82 -0
- package/lib/cjs/helpers/deadShares.js +200 -0
- package/lib/cjs/helpers/index.d.ts +2 -1
- package/lib/cjs/helpers/index.js +7 -1
- package/lib/cjs/midnight/abis.d.ts +1 -1
- package/lib/cjs/midnight/abis.js +3 -1
- package/lib/cjs/types/error.d.ts +54 -5
- package/lib/cjs/types/error.js +60 -6
- package/lib/cjs/types/sharedLiquidity.d.ts +10 -6
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/blue/constants.d.ts +1 -1
- package/lib/esm/blue/constants.js +1 -1
- package/lib/esm/constants.d.ts +2 -2
- package/lib/esm/constants.js +2 -2
- package/lib/esm/entities/blue/blue.d.ts +71 -16
- package/lib/esm/entities/blue/blue.js +34 -6
- package/lib/esm/entities/midnight/midnight.d.ts +2 -1
- package/lib/esm/entities/midnight/midnight.js +6 -5
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +8 -0
- package/lib/esm/entities/vaultV1/vaultV1.js +5 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +4 -0
- package/lib/esm/entities/vaultV2/vaultV2.js +3 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +37 -4
- package/lib/esm/entities/vaultV2BlueReallocationData.js +197 -32
- package/lib/esm/helpers/constant.d.ts +7 -0
- package/lib/esm/helpers/constant.js +7 -0
- package/lib/esm/helpers/deadShares.d.ts +82 -0
- package/lib/esm/helpers/deadShares.js +195 -0
- package/lib/esm/helpers/index.d.ts +2 -1
- package/lib/esm/helpers/index.js +2 -1
- package/lib/esm/midnight/abis.d.ts +1 -1
- package/lib/esm/midnight/abis.js +1 -1
- package/lib/esm/types/error.d.ts +54 -5
- package/lib/esm/types/error.js +57 -5
- package/lib/esm/types/sharedLiquidity.d.ts +10 -6
- package/package.json +8 -7
|
@@ -174,10 +174,22 @@ class VaultV2BlueReallocationData {
|
|
|
174
174
|
mutableVaults;
|
|
175
175
|
/** Mutable allocation state used only by cloned simulation transitions. */
|
|
176
176
|
mutableAllocations;
|
|
177
|
+
/** Mutable Blue token balances used only by cloned simulation transitions. */
|
|
178
|
+
mutableBlueBalances;
|
|
177
179
|
/** Chain id associated with this snapshot. */
|
|
178
180
|
chainId;
|
|
179
181
|
/** Timestamp of the block the state was fetched at. */
|
|
180
182
|
timestamp;
|
|
183
|
+
/**
|
|
184
|
+
* Morpho Blue's ERC-20 balances at the snapshot block, indexed by token address.
|
|
185
|
+
* Explicit balances increase with idle-sourced reallocations in returned
|
|
186
|
+
* post-states. Sizing has zero margin over the snapshot balance, so activity
|
|
187
|
+
* on other Blue markets with the same loan token can make a plan revert;
|
|
188
|
+
* refetch close to submission. A missing token uses summed non-negative
|
|
189
|
+
* snapshot-market supply-minus-borrow only to size plans; balance-based
|
|
190
|
+
* rejection requires an explicit balance.
|
|
191
|
+
*/
|
|
192
|
+
blueBalances;
|
|
181
193
|
/** Markets indexed by market id. */
|
|
182
194
|
markets;
|
|
183
195
|
/** Vault V2 entities indexed by address. */
|
|
@@ -204,9 +216,11 @@ class VaultV2BlueReallocationData {
|
|
|
204
216
|
this.mutableMarkets = {};
|
|
205
217
|
this.mutableVaults = {};
|
|
206
218
|
this.mutableAllocations = {};
|
|
219
|
+
this.mutableBlueBalances = {};
|
|
207
220
|
this.markets = this.mutableMarkets;
|
|
208
221
|
this.vaults = this.mutableVaults;
|
|
209
222
|
this.allocations = this.mutableAllocations;
|
|
223
|
+
this.blueBalances = this.mutableBlueBalances;
|
|
210
224
|
if (isClone) {
|
|
211
225
|
this.publicAllocatorConfigs = input.publicAllocatorConfigs;
|
|
212
226
|
this.activeAdapters = input.activeAdapters;
|
|
@@ -243,6 +257,10 @@ class VaultV2BlueReallocationData {
|
|
|
243
257
|
this.mutableMarkets[marketId] =
|
|
244
258
|
market == null ? undefined : new blue_sdk_1.Market({ ...market });
|
|
245
259
|
}
|
|
260
|
+
for (const [token, balance] of Object.entries(input.blueBalances ?? {})) {
|
|
261
|
+
this.mutableBlueBalances[token] =
|
|
262
|
+
balance == null ? undefined : BigInt(balance);
|
|
263
|
+
}
|
|
246
264
|
for (const [address, vault] of Object.entries(input.vaults ?? {})) {
|
|
247
265
|
const clonedVault = vault == null
|
|
248
266
|
? undefined
|
|
@@ -575,8 +593,9 @@ class VaultV2BlueReallocationData {
|
|
|
575
593
|
* Without `options.operation`, discovers every friendly call. With an
|
|
576
594
|
* operation, penalty tiers are considered ascending, starting with free
|
|
577
595
|
* vaults. The free tier aims to bring the target market back to 90%
|
|
578
|
-
* utilization; positive tiers cover
|
|
579
|
-
*
|
|
596
|
+
* utilization; positive tiers cover the remaining absolute shortfall or,
|
|
597
|
+
* when larger, Blue's loan-token balance deficit while the penalty is
|
|
598
|
+
* flash-loaned. Within each tier, source markets go to the
|
|
580
599
|
* friendly source utilization (90% by default) before 100%. Free capacity
|
|
581
600
|
* inside `allocatorCapHeadroom` is kept unused, so a penalized leg can cover
|
|
582
601
|
* it instead. Discovery and liquidity getters still count every vault up to
|
|
@@ -590,6 +609,16 @@ class VaultV2BlueReallocationData {
|
|
|
590
609
|
* The planner includes the referral fee and its allocator-leg penalties in
|
|
591
610
|
* destination debt, so penalized legs also cover their own penalty.
|
|
592
611
|
* Refinance never uses vaults with a 100% penalty.
|
|
612
|
+
* BlueBundlesV1 flash-loans the plan's penalty from Blue before executing
|
|
613
|
+
* reallocation legs. When an explicit `blueBalances` value is supplied, plans
|
|
614
|
+
* throw `InsufficientSharedLiquidityError` if Blue cannot fund that flash
|
|
615
|
+
* loan or a market-sourced leg's withdrawal before later idle legs arrive.
|
|
616
|
+
* Legs are not reordered. A missing balance falls back to summed snapshot-
|
|
617
|
+
* market liquidity only to size plans and may make them larger; it cannot
|
|
618
|
+
* cause rejection for Blue-balance constraints.
|
|
619
|
+
* Sizing has zero margin over the snapshot balance; activity on other Blue
|
|
620
|
+
* markets with the same loan token can make a plan revert. Refetch close to
|
|
621
|
+
* submission; there is no balance-buffer option.
|
|
593
622
|
* Friendly source utilization defaults to 90% and is configurable through
|
|
594
623
|
* `options.maxWithdrawalUtilization`. Market/vault state and the returned
|
|
595
624
|
* post-state stay at `timestamp`. An enabled operation that needs no
|
|
@@ -637,7 +666,7 @@ class VaultV2BlueReallocationData {
|
|
|
637
666
|
* @returns Flat action-ready reallocations and their post-simulation state.
|
|
638
667
|
* @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
|
|
639
668
|
* @throws {NegativeInputError} when `capAccrualBuffer`, `allocatorCapHeadroom`, `maxWithdrawalUtilization`, `maxPenalty`, or `referralFeePct` is negative.
|
|
640
|
-
* @throws {InputExceedsMaxError} when `allocatorCapHeadroom`, `maxWithdrawalUtilization`, or `maxPenalty` exceeds WAD.
|
|
669
|
+
* @throws {InputExceedsMaxError} when `allocatorCapHeadroom`, `maxWithdrawalUtilization`, or `maxPenalty` exceeds WAD, or an enabled borrow/withdraw plan otherwise covers its shortfall but its planned penalty exceeds the operation amount.
|
|
641
670
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
|
|
642
671
|
* @throws {NonPositiveInputError} when the operation amount is not positive and planning is enabled.
|
|
643
672
|
* @throws {UnknownReallocationMarketError} when a required market is absent.
|
|
@@ -646,7 +675,7 @@ class VaultV2BlueReallocationData {
|
|
|
646
675
|
* @throws {UnknownReallocationActiveAdaptersError} when active-adapter state is absent for a vault.
|
|
647
676
|
* @throws {UnknownReallocationMarketPublicAllocatorConfigError} when an adapter-market allocator configuration is absent.
|
|
648
677
|
* @throws {UnknownReallocationAllocationError} when required allocation state is absent.
|
|
649
|
-
* @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the
|
|
678
|
+
* @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the operation shortfall or refinance penalties, or an explicit Blue balance cannot fund the penalty flash loan, an earlier market-sourced withdrawal, or the final operation transfer.
|
|
650
679
|
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdraw exceeds market supply.
|
|
651
680
|
* @example
|
|
652
681
|
* ```ts
|
|
@@ -719,6 +748,31 @@ class VaultV2BlueReallocationData {
|
|
|
719
748
|
: [...options.reallocatableVaults],
|
|
720
749
|
};
|
|
721
750
|
const market = this.getMarket(marketId).accrueInterest(timestamp);
|
|
751
|
+
const blueBalanceKey = findAddressKey(this.mutableBlueBalances, market.params.loanToken);
|
|
752
|
+
const explicitBlueBalance = blueBalanceKey == null
|
|
753
|
+
? undefined
|
|
754
|
+
: this.mutableBlueBalances[blueBalanceKey];
|
|
755
|
+
const hasExplicitBlueBalance = explicitBlueBalance !== undefined;
|
|
756
|
+
const blueBalance = explicitBlueBalance ??
|
|
757
|
+
Object.values(this.mutableMarkets).reduce((total, snapshotMarket) => snapshotMarket != null &&
|
|
758
|
+
(0, viem_1.isAddressEqual)(snapshotMarket.params.loanToken, market.params.loanToken)
|
|
759
|
+
? total +
|
|
760
|
+
blue_sdk_1.MathLib.zeroFloorSub(snapshotMarket.totalSupplyAssets, snapshotMarket.totalBorrowAssets)
|
|
761
|
+
: total, 0n);
|
|
762
|
+
const getTransferShortfall = (reallocations) => {
|
|
763
|
+
const penaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(reallocations);
|
|
764
|
+
if (penaltyAssets === 0n)
|
|
765
|
+
return 0n;
|
|
766
|
+
let shortfall = blue_sdk_1.MathLib.zeroFloorSub(penaltyAssets, blueBalance);
|
|
767
|
+
let balance = blue_sdk_1.MathLib.zeroFloorSub(blueBalance, penaltyAssets);
|
|
768
|
+
for (const { from, assets } of reallocations) {
|
|
769
|
+
if (from.type === "idle")
|
|
770
|
+
balance += assets;
|
|
771
|
+
else
|
|
772
|
+
shortfall = blue_sdk_1.MathLib.max(shortfall, blue_sdk_1.MathLib.zeroFloorSub(assets, balance));
|
|
773
|
+
}
|
|
774
|
+
return shortfall;
|
|
775
|
+
};
|
|
722
776
|
if (type === "withdraw" && amount > market.totalSupplyAssets) {
|
|
723
777
|
throw new index_js_1.ReallocationWithdrawExceedsMarketSupplyError({
|
|
724
778
|
marketId,
|
|
@@ -760,25 +814,30 @@ class VaultV2BlueReallocationData {
|
|
|
760
814
|
let data = this.clone();
|
|
761
815
|
let context = createSimulationContext();
|
|
762
816
|
let remainingShortfall = absoluteShortfall;
|
|
817
|
+
let accountingShortfall = absoluteShortfall;
|
|
763
818
|
let allocatorCapBoundTargets = new Set();
|
|
764
819
|
let idleBoundVaults = new Set();
|
|
765
820
|
for (const penalty of penaltyTiers) {
|
|
766
821
|
if (type === "refinance" && penalty === blue_sdk_1.MathLib.WAD)
|
|
767
822
|
continue;
|
|
768
823
|
let isFriendlyPass = true;
|
|
824
|
+
let triedFullCapacityAttempt = false;
|
|
769
825
|
for (const utilization of [maxWithdrawalUtilization, blue_sdk_1.MathLib.WAD]) {
|
|
770
826
|
const isFreeFriendlyPass = penalty === 0n && isFriendlyPass;
|
|
771
827
|
isFriendlyPass = false;
|
|
772
828
|
if (remainingShortfall === 0n && !isFreeFriendlyPass)
|
|
773
829
|
continue;
|
|
774
|
-
let passMaxAssets =
|
|
775
|
-
?
|
|
776
|
-
:
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
}
|
|
830
|
+
let passMaxAssets = type === "refinance" && penalty > 0n
|
|
831
|
+
? blue_sdk_1.MathLib.mulDivUp(remainingShortfall, blue_sdk_1.MathLib.WAD, blue_sdk_1.MathLib.WAD - penalty)
|
|
832
|
+
: isFreeFriendlyPass
|
|
833
|
+
? maxAssets
|
|
834
|
+
: remainingShortfall;
|
|
835
|
+
const retryPaidPass = penalty > 0n;
|
|
781
836
|
let lo = retryPaidPass ? passMaxAssets - 1n : 0n;
|
|
837
|
+
let tryFullCapacityAttempt = false;
|
|
838
|
+
let isFullCapacityRetryPath = false;
|
|
839
|
+
const beforePassRemainingShortfall = remainingShortfall;
|
|
840
|
+
const beforePassAccountingShortfall = accountingShortfall;
|
|
782
841
|
// Grow by grossed-up gaps, bisect to the smallest found, and keep the best for non-monotonic shared-cap fitting.
|
|
783
842
|
const passStart = retryPaidPass
|
|
784
843
|
? {
|
|
@@ -789,9 +848,12 @@ class VaultV2BlueReallocationData {
|
|
|
789
848
|
}
|
|
790
849
|
: undefined;
|
|
791
850
|
let previousPassReallocatedAssets = 0n;
|
|
851
|
+
let previousPhysicalShortfall;
|
|
792
852
|
let hi;
|
|
793
853
|
let bestAttempt;
|
|
794
854
|
while (true) {
|
|
855
|
+
const isFullCapacityAttempt = tryFullCapacityAttempt;
|
|
856
|
+
tryFullCapacityAttempt = false;
|
|
795
857
|
if (passStart != null) {
|
|
796
858
|
data = passStart.data.clone();
|
|
797
859
|
context = passStart.context;
|
|
@@ -803,7 +865,7 @@ class VaultV2BlueReallocationData {
|
|
|
803
865
|
reserves,
|
|
804
866
|
marketId,
|
|
805
867
|
maxWithdrawalUtilization: utilization,
|
|
806
|
-
maxAssets: passMaxAssets,
|
|
868
|
+
maxAssets: isFullCapacityAttempt ? undefined : passMaxAssets,
|
|
807
869
|
capAccrualBuffer,
|
|
808
870
|
allocatorCapHeadroom: headroom,
|
|
809
871
|
penalty,
|
|
@@ -831,23 +893,56 @@ class VaultV2BlueReallocationData {
|
|
|
831
893
|
const supply = type === "withdraw"
|
|
832
894
|
? currentMarket.totalSupplyAssets - amount
|
|
833
895
|
: currentMarket.totalSupplyAssets;
|
|
834
|
-
|
|
896
|
+
accountingShortfall = borrow > supply ? borrow - supply : 0n;
|
|
897
|
+
const penaltyForPhysicalShortfall = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(reallocations);
|
|
898
|
+
const idleAssetsForPhysicalShortfall = reallocations.reduce((total, { from, assets }) => from.type === "idle" ? total + assets : total, 0n);
|
|
899
|
+
const physicalShortfall = penaltyForPhysicalShortfall === 0n
|
|
900
|
+
? 0n
|
|
901
|
+
: blue_sdk_1.MathLib.zeroFloorSub((type === "refinance"
|
|
902
|
+
? amount + feeAssets + penaltyForPhysicalShortfall
|
|
903
|
+
: amount) + penaltyForPhysicalShortfall, blueBalance + idleAssetsForPhysicalShortfall);
|
|
904
|
+
remainingShortfall = blue_sdk_1.MathLib.max(accountingShortfall, physicalShortfall);
|
|
905
|
+
const attemptTransferShortfall = hasExplicitBlueBalance && isFullCapacityRetryPath
|
|
906
|
+
? getTransferShortfall(reallocations)
|
|
907
|
+
: 0n;
|
|
908
|
+
const attemptCoversShortfall = remainingShortfall === 0n;
|
|
835
909
|
const passReallocatedAssets = pass.reallocations.reduce((total, { assets }) => total + assets, 0n);
|
|
836
910
|
if (passStart == null)
|
|
837
911
|
break;
|
|
838
|
-
if (
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
912
|
+
if (isFullCapacityAttempt)
|
|
913
|
+
passMaxAssets = passReallocatedAssets;
|
|
914
|
+
const improvesAccounting = accountingShortfall < beforePassAccountingShortfall;
|
|
915
|
+
const attemptPriority = hasExplicitBlueBalance
|
|
916
|
+
? [0n, remainingShortfall, 0n, passReallocatedAssets]
|
|
917
|
+
: [
|
|
918
|
+
improvesAccounting ? 0n : 1n,
|
|
919
|
+
improvesAccounting ? accountingShortfall : 0n,
|
|
920
|
+
remainingShortfall,
|
|
921
|
+
passReallocatedAssets,
|
|
922
|
+
];
|
|
923
|
+
const previousBestAttempt = bestAttempt;
|
|
924
|
+
let isBetterAttempt = previousBestAttempt == null;
|
|
925
|
+
if (previousBestAttempt != null) {
|
|
926
|
+
const firstDifference = attemptPriority.findIndex((priority, index) => priority !== previousBestAttempt.priority[index]);
|
|
927
|
+
isBetterAttempt =
|
|
928
|
+
firstDifference !== -1 &&
|
|
929
|
+
attemptPriority[firstDifference] <
|
|
930
|
+
previousBestAttempt.priority[firstDifference];
|
|
931
|
+
}
|
|
932
|
+
if (isBetterAttempt &&
|
|
933
|
+
(!isFullCapacityRetryPath ||
|
|
934
|
+
(attemptCoversShortfall && attemptTransferShortfall === 0n)))
|
|
842
935
|
bestAttempt = {
|
|
843
936
|
data,
|
|
844
937
|
context,
|
|
845
938
|
reserves: cloneCapReserveState(reserves),
|
|
846
939
|
reallocations: pass.reallocations,
|
|
847
940
|
remainingShortfall,
|
|
941
|
+
accountingShortfall,
|
|
848
942
|
passReallocatedAssets,
|
|
943
|
+
priority: attemptPriority,
|
|
849
944
|
};
|
|
850
|
-
if (
|
|
945
|
+
if (attemptCoversShortfall)
|
|
851
946
|
hi = hi == null ? passMaxAssets : blue_sdk_1.MathLib.min(hi, passMaxAssets);
|
|
852
947
|
else
|
|
853
948
|
lo = blue_sdk_1.MathLib.max(lo, passMaxAssets);
|
|
@@ -857,18 +952,52 @@ class VaultV2BlueReallocationData {
|
|
|
857
952
|
passMaxAssets = (lo + hi) / 2n;
|
|
858
953
|
continue;
|
|
859
954
|
}
|
|
860
|
-
if (
|
|
955
|
+
if (isFullCapacityAttempt && !attemptCoversShortfall)
|
|
956
|
+
break;
|
|
957
|
+
if (accountingShortfall === 0n &&
|
|
958
|
+
previousPhysicalShortfall != null &&
|
|
959
|
+
physicalShortfall >= previousPhysicalShortfall) {
|
|
960
|
+
if (!triedFullCapacityAttempt &&
|
|
961
|
+
passReallocatedAssets === passMaxAssets) {
|
|
962
|
+
triedFullCapacityAttempt = true;
|
|
963
|
+
tryFullCapacityAttempt = true;
|
|
964
|
+
isFullCapacityRetryPath = true;
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
previousPhysicalShortfall = physicalShortfall;
|
|
970
|
+
if (passReallocatedAssets <= previousPassReallocatedAssets ||
|
|
971
|
+
penalty === blue_sdk_1.MathLib.WAD)
|
|
861
972
|
break;
|
|
862
973
|
previousPassReallocatedAssets = passReallocatedAssets;
|
|
863
974
|
passMaxAssets += blue_sdk_1.MathLib.mulDivUp(remainingShortfall, blue_sdk_1.MathLib.WAD, blue_sdk_1.MathLib.WAD - penalty);
|
|
864
975
|
}
|
|
865
|
-
if (passStart != null
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
976
|
+
if (passStart != null) {
|
|
977
|
+
const improved = bestAttempt != null &&
|
|
978
|
+
(hasExplicitBlueBalance
|
|
979
|
+
? bestAttempt.remainingShortfall < beforePassRemainingShortfall
|
|
980
|
+
: bestAttempt.accountingShortfall <
|
|
981
|
+
beforePassAccountingShortfall ||
|
|
982
|
+
bestAttempt.remainingShortfall <
|
|
983
|
+
beforePassRemainingShortfall);
|
|
984
|
+
if (improved && bestAttempt != null) {
|
|
985
|
+
data = bestAttempt.data;
|
|
986
|
+
context = bestAttempt.context;
|
|
987
|
+
reserves = cloneCapReserveState(bestAttempt.reserves);
|
|
988
|
+
reallocations.length = passStart.reallocationCount;
|
|
989
|
+
reallocations.push(...bestAttempt.reallocations);
|
|
990
|
+
remainingShortfall = bestAttempt.remainingShortfall;
|
|
991
|
+
accountingShortfall = bestAttempt.accountingShortfall;
|
|
992
|
+
}
|
|
993
|
+
else {
|
|
994
|
+
data = passStart.data;
|
|
995
|
+
context = passStart.context;
|
|
996
|
+
reserves = cloneCapReserveState(passStart.reserves);
|
|
997
|
+
reallocations.length = passStart.reallocationCount;
|
|
998
|
+
remainingShortfall = beforePassRemainingShortfall;
|
|
999
|
+
accountingShortfall = beforePassAccountingShortfall;
|
|
1000
|
+
}
|
|
872
1001
|
}
|
|
873
1002
|
}
|
|
874
1003
|
}
|
|
@@ -881,16 +1010,28 @@ class VaultV2BlueReallocationData {
|
|
|
881
1010
|
!(from.type === "idle" && idleBoundVaults.has(vault.toLowerCase())))
|
|
882
1011
|
unboundAssets += assets;
|
|
883
1012
|
}
|
|
1013
|
+
const penaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(reallocations);
|
|
1014
|
+
const idleAssets = reallocations.reduce((total, { from, assets }) => from.type === "idle" ? total + assets : total, 0n);
|
|
1015
|
+
const physicalShortfall = penaltyAssets === 0n
|
|
1016
|
+
? 0n
|
|
1017
|
+
: blue_sdk_1.MathLib.zeroFloorSub((type === "refinance"
|
|
1018
|
+
? amount + feeAssets + penaltyAssets
|
|
1019
|
+
: amount) + penaltyAssets, blueBalance + idleAssets);
|
|
1020
|
+
const transferShortfall = getTransferShortfall(reallocations);
|
|
1021
|
+
const coversAccountingShortfall = type === "refinance"
|
|
1022
|
+
? reallocatedAssets >= absoluteShortfall + penaltyAssets
|
|
1023
|
+
: reallocatedAssets >= absoluteShortfall;
|
|
884
1024
|
return {
|
|
885
1025
|
reallocations,
|
|
886
1026
|
data,
|
|
887
1027
|
liquidityBound: allocatorCapBoundTargets.size > 0 || idleBoundVaults.size > 0,
|
|
888
1028
|
reallocatedAssets,
|
|
889
1029
|
unboundAssets,
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
1030
|
+
physicalShortfall,
|
|
1031
|
+
coversShortfall: coversAccountingShortfall &&
|
|
1032
|
+
(type === "refinance" || penaltyAssets <= amount) &&
|
|
1033
|
+
(!hasExplicitBlueBalance ||
|
|
1034
|
+
(physicalShortfall === 0n && transferShortfall === 0n)),
|
|
894
1035
|
};
|
|
895
1036
|
};
|
|
896
1037
|
// A leg that fills an allocator cap, or idle legs that together take
|
|
@@ -918,12 +1059,30 @@ class VaultV2BlueReallocationData {
|
|
|
918
1059
|
available = selected.reallocatedAssets;
|
|
919
1060
|
}
|
|
920
1061
|
if (!selected.coversShortfall) {
|
|
1062
|
+
const selectedTransferShortfall = hasExplicitBlueBalance
|
|
1063
|
+
? getTransferShortfall(selected.reallocations)
|
|
1064
|
+
: 0n;
|
|
1065
|
+
const selectedPenaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(selected.reallocations);
|
|
1066
|
+
if (type !== "refinance" &&
|
|
1067
|
+
selectedPenaltyAssets > amount &&
|
|
1068
|
+
selected.reallocatedAssets >= absoluteShortfall &&
|
|
1069
|
+
(!hasExplicitBlueBalance ||
|
|
1070
|
+
(selected.physicalShortfall === 0n &&
|
|
1071
|
+
selectedTransferShortfall === 0n)))
|
|
1072
|
+
throw new index_js_1.InputExceedsMaxError({
|
|
1073
|
+
field: "reallocationPenaltyAssets",
|
|
1074
|
+
value: selectedPenaltyAssets,
|
|
1075
|
+
max: amount,
|
|
1076
|
+
});
|
|
921
1077
|
throw new index_js_1.InsufficientSharedLiquidityError({
|
|
922
1078
|
marketId,
|
|
923
|
-
shortfall: absoluteShortfall +
|
|
1079
|
+
shortfall: blue_sdk_1.MathLib.max(absoluteShortfall +
|
|
924
1080
|
(type === "refinance"
|
|
925
1081
|
? (0, common_js_1.getBlueBundlesV1PenaltyAssets)(selected.reallocations)
|
|
926
|
-
: 0n),
|
|
1082
|
+
: 0n), selected.reallocatedAssets +
|
|
1083
|
+
(hasExplicitBlueBalance
|
|
1084
|
+
? blue_sdk_1.MathLib.max(selected.physicalShortfall, selectedTransferShortfall)
|
|
1085
|
+
: 0n)),
|
|
927
1086
|
available,
|
|
928
1087
|
});
|
|
929
1088
|
}
|
|
@@ -1572,6 +1731,12 @@ class VaultV2BlueReallocationData {
|
|
|
1572
1731
|
}
|
|
1573
1732
|
vault.assetBalance += reallocation.assets;
|
|
1574
1733
|
}
|
|
1734
|
+
if (reallocation.from.type === "idle") {
|
|
1735
|
+
const blueBalanceKey = findAddressKey(data.mutableBlueBalances, targetMarket.params.loanToken);
|
|
1736
|
+
if (blueBalanceKey != null &&
|
|
1737
|
+
data.mutableBlueBalances[blueBalanceKey] != null)
|
|
1738
|
+
data.mutableBlueBalances[blueBalanceKey] += reallocation.assets;
|
|
1739
|
+
}
|
|
1575
1740
|
if (firstTotalAssets == null) {
|
|
1576
1741
|
// Vault V2's transient firstTotalAssets tracks the first allocation in a
|
|
1577
1742
|
// transaction independently from elapsed time. Later allocations must not
|
|
@@ -30,6 +30,13 @@ export declare const DEFAULT_CAP_ACCRUAL_BUFFER: bigint;
|
|
|
30
30
|
* `VaultV2BluePublicAllocatorOptions.allocatorCapHeadroom`.
|
|
31
31
|
*/
|
|
32
32
|
export declare const DEFAULT_ALLOCATOR_CAP_HEADROOM: bigint;
|
|
33
|
+
/**
|
|
34
|
+
* Minimum shares a Morpho Blue market, Vault V1 or Vault V2 must hold at `DEAD_ADDRESS` to count as
|
|
35
|
+
* protected against share-price inflation (1e9, Protocol team rule).
|
|
36
|
+
*/
|
|
37
|
+
export declare const MIN_DEAD_SHARES = 1000000000n;
|
|
38
|
+
/** Minimum assets, in the vault asset's smallest unit, a Vault V2's dead shares must be worth (1e6). */
|
|
39
|
+
export declare const MIN_VAULT_V2_DEAD_ASSETS = 1000000n;
|
|
33
40
|
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
34
41
|
export declare const MAX_ABSOLUTE_SHARE_PRICE: bigint;
|
|
35
42
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_ALLOCATOR_CAP_HEADROOM = exports.DEFAULT_CAP_ACCRUAL_BUFFER = exports.DEFAULT_LLTV_BUFFER = exports.MAX_REALLOCATION_PENALTY = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.MAX_SLIPPAGE_TOLERANCE = exports.MIDNIGHT_MAX_REFERRAL_FEE_PCT = exports.MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS = void 0;
|
|
3
|
+
exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.MIN_VAULT_V2_DEAD_ASSETS = exports.MIN_DEAD_SHARES = exports.DEFAULT_ALLOCATOR_CAP_HEADROOM = exports.DEFAULT_CAP_ACCRUAL_BUFFER = exports.DEFAULT_LLTV_BUFFER = exports.MAX_REALLOCATION_PENALTY = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.MAX_SLIPPAGE_TOLERANCE = exports.MIDNIGHT_MAX_REFERRAL_FEE_PCT = exports.MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
6
|
const viem_1 = require("viem");
|
|
@@ -35,6 +35,13 @@ exports.DEFAULT_CAP_ACCRUAL_BUFFER = morpho_ts_1.Time.s.from.h(2n);
|
|
|
35
35
|
* `VaultV2BluePublicAllocatorOptions.allocatorCapHeadroom`.
|
|
36
36
|
*/
|
|
37
37
|
exports.DEFAULT_ALLOCATOR_CAP_HEADROOM = blue_sdk_1.MathLib.WAD / 100n;
|
|
38
|
+
/**
|
|
39
|
+
* Minimum shares a Morpho Blue market, Vault V1 or Vault V2 must hold at `DEAD_ADDRESS` to count as
|
|
40
|
+
* protected against share-price inflation (1e9, Protocol team rule).
|
|
41
|
+
*/
|
|
42
|
+
exports.MIN_DEAD_SHARES = 1000000000n;
|
|
43
|
+
/** Minimum assets, in the vault asset's smallest unit, a Vault V2's dead shares must be worth (1e6). */
|
|
44
|
+
exports.MIN_VAULT_V2_DEAD_ASSETS = 1000000n;
|
|
38
45
|
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
39
46
|
exports.MAX_ABSOLUTE_SHARE_PRICE = 100n * blue_sdk_1.MathLib.RAY;
|
|
40
47
|
/**
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type AccrualVault, type AccrualVaultV2, type IMarket } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address, Hash } from "viem";
|
|
3
|
+
type DeadSharesPath = readonly (Address | Hash)[];
|
|
4
|
+
/**
|
|
5
|
+
* Checks that a Morpho Blue market holds at least `MIN_DEAD_SHARES` supply shares at `DEAD_ADDRESS`.
|
|
6
|
+
*
|
|
7
|
+
* @param market - Fetched market snapshot.
|
|
8
|
+
* @param path - Vaults leading to this market, outermost first; empty for a direct market.
|
|
9
|
+
* @returns Nothing; returns normally when the market passes.
|
|
10
|
+
* @throws {MissingDeadSharesDataError} when `market.deadSupplyShares` was not fetched.
|
|
11
|
+
* @throws {InsufficientDeadSharesError} when the market holds fewer than `MIN_DEAD_SHARES` dead supply shares.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { validateBlueMarketDeadShares } from "@morpho-org/morpho-sdk";
|
|
15
|
+
* import { fetchBlueMarket } from "@morpho-org/morpho-sdk/fetch";
|
|
16
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
17
|
+
* import { createPublicClient, http } from "viem";
|
|
18
|
+
* import { mainnet } from "viem/chains";
|
|
19
|
+
*
|
|
20
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
21
|
+
* const market = await fetchBlueMarket(markets[mainnet.id].usdc_wbtc.id, client);
|
|
22
|
+
* validateBlueMarketDeadShares(market);
|
|
23
|
+
* // Returns void on success; throws if the snapshot fails the rule.
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function validateBlueMarketDeadShares(market: Pick<IMarket, "params" | "deadSupplyShares"> & {
|
|
27
|
+
readonly id: Hash;
|
|
28
|
+
}, path?: DeadSharesPath): void;
|
|
29
|
+
/**
|
|
30
|
+
* Checks that a Vault V1 holds at least `MIN_DEAD_SHARES` shares at `DEAD_ADDRESS` and that every
|
|
31
|
+
* market in its withdraw queue passes `validateBlueMarketDeadShares`.
|
|
32
|
+
*
|
|
33
|
+
* @param vault - Fetched vault snapshot with its withdraw-queue allocations.
|
|
34
|
+
* @param path - Vaults leading to this vault, outermost first; empty for a direct deposit.
|
|
35
|
+
* @returns Nothing; returns normally when the vault and everything it can allocate to pass.
|
|
36
|
+
* @throws {MissingDeadSharesDataError} when `vault.deadShares`, a withdraw-queue allocation or a
|
|
37
|
+
* market's `deadSupplyShares` was not fetched.
|
|
38
|
+
* @throws {InsufficientDeadSharesError} when the vault or one of its withdraw-queue markets holds
|
|
39
|
+
* too few dead shares.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { validateVaultV1DeadShares } from "@morpho-org/morpho-sdk";
|
|
43
|
+
* import { fetchAccrualVault } from "@morpho-org/morpho-sdk/fetch";
|
|
44
|
+
* import { vaults } from "@morpho-org/morpho-test";
|
|
45
|
+
* import { createPublicClient, http } from "viem";
|
|
46
|
+
* import { mainnet } from "viem/chains";
|
|
47
|
+
*
|
|
48
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
49
|
+
* const vault = await fetchAccrualVault(vaults[mainnet.id].steakUsdc.address, client);
|
|
50
|
+
* validateVaultV1DeadShares(vault);
|
|
51
|
+
* // Returns void on success; throws if the snapshot fails the rule.
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function validateVaultV1DeadShares(vault: AccrualVault, path?: DeadSharesPath): void;
|
|
55
|
+
/**
|
|
56
|
+
* Checks a Vault V2 against the dead-deposit rule: at least `MIN_DEAD_SHARES` shares at
|
|
57
|
+
* `DEAD_ADDRESS` worth at least `MIN_VAULT_V2_DEAD_ASSETS` assets, a share price above
|
|
58
|
+
* `1 / (10 * virtualShares)`, and every Blue market or Vault V1 it can allocate to (non-zero
|
|
59
|
+
* absolute cap) passing its own rule. Zero-cap entries are skipped.
|
|
60
|
+
*
|
|
61
|
+
* @param vault - Fetched vault snapshot with its adapters and `absoluteCaps`.
|
|
62
|
+
* @param path - Vaults leading to this vault, outermost first; empty for a direct deposit.
|
|
63
|
+
* @returns Nothing; returns normally when the vault and everything it can allocate to pass.
|
|
64
|
+
* @throws {MissingDeadSharesDataError} when a dead-share field, an adapter cap, or the data of an
|
|
65
|
+
* adapter the SDK cannot hydrate is missing.
|
|
66
|
+
* @throws {InsufficientDeadSharesError} when the vault or an allocatable market or Vault V1 fails the rule.
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { validateVaultV2DeadShares } from "@morpho-org/morpho-sdk";
|
|
70
|
+
* import { fetchAccrualVaultV2 } from "@morpho-org/morpho-sdk/fetch";
|
|
71
|
+
* import { createPublicClient, http } from "viem";
|
|
72
|
+
* import { mainnet } from "viem/chains";
|
|
73
|
+
*
|
|
74
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
75
|
+
* const keyrockUsdcVaultV2 = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
|
|
76
|
+
* const vault = await fetchAccrualVaultV2(keyrockUsdcVaultV2, client);
|
|
77
|
+
* validateVaultV2DeadShares(vault);
|
|
78
|
+
* // Returns void on success; throws if the snapshot fails the rule.
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function validateVaultV2DeadShares(vault: AccrualVaultV2, path?: DeadSharesPath): void;
|
|
82
|
+
export {};
|