@morpho-org/morpho-sdk 2.1.1 → 3.1.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/lib/cjs/abis.d.ts +2816 -0
- package/lib/cjs/abis.js +1951 -0
- package/lib/cjs/actions/index.d.ts +3 -0
- package/lib/cjs/actions/marketV1/borrow.js +7 -10
- package/lib/cjs/actions/marketV1/buildReallocationActions.d.ts +2 -3
- package/lib/cjs/actions/marketV1/buildReallocationActions.js +1 -2
- package/lib/cjs/actions/marketV1/index.d.ts +2 -0
- package/lib/cjs/actions/marketV1/index.js +2 -0
- package/lib/cjs/actions/marketV1/repay.d.ts +2 -0
- package/lib/cjs/actions/marketV1/repay.js +7 -8
- package/lib/cjs/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
- package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +9 -10
- package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
- package/lib/cjs/actions/marketV1/supply.js +128 -0
- package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +3 -1
- package/lib/cjs/actions/marketV1/supplyCollateral.js +12 -13
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +3 -1
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +14 -15
- package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
- package/lib/cjs/actions/marketV1/withdraw.js +110 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -2
- package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
- package/lib/cjs/actions/vaultV1/deposit.d.ts +2 -0
- package/lib/cjs/actions/vaultV1/deposit.js +13 -14
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +2 -0
- package/lib/cjs/actions/vaultV1/migrateToV2.js +11 -9
- package/lib/cjs/actions/vaultV2/deposit.d.ts +2 -0
- package/lib/cjs/actions/vaultV2/deposit.js +13 -14
- package/lib/cjs/addresses.d.ts +2 -0
- package/lib/cjs/addresses.js +18 -0
- package/lib/cjs/augment/AccrualPosition.d.ts +9 -0
- package/lib/cjs/augment/AccrualPosition.js +8 -0
- package/lib/cjs/augment/AccrualVault.d.ts +8 -0
- package/lib/cjs/augment/AccrualVault.js +7 -0
- package/lib/cjs/augment/Holding.d.ts +8 -0
- package/lib/cjs/augment/Holding.js +7 -0
- package/lib/cjs/augment/Market.d.ts +8 -0
- package/lib/cjs/augment/Market.js +7 -0
- package/lib/cjs/augment/MarketParams.d.ts +8 -0
- package/lib/cjs/augment/MarketParams.js +7 -0
- package/lib/cjs/augment/Position.d.ts +8 -0
- package/lib/cjs/augment/Position.js +7 -0
- package/lib/cjs/augment/Token.d.ts +8 -0
- package/lib/cjs/augment/Token.js +7 -0
- package/lib/cjs/augment/User.d.ts +8 -0
- package/lib/cjs/augment/User.js +7 -0
- package/lib/cjs/augment/Vault.d.ts +8 -0
- package/lib/cjs/augment/Vault.js +7 -0
- package/lib/cjs/augment/VaultConfig.d.ts +8 -0
- package/lib/cjs/augment/VaultConfig.js +7 -0
- package/lib/cjs/augment/VaultMarketAllocation.d.ts +8 -0
- package/lib/cjs/augment/VaultMarketAllocation.js +7 -0
- package/lib/cjs/augment/VaultMarketConfig.d.ts +8 -0
- package/lib/cjs/augment/VaultMarketConfig.js +7 -0
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +8 -0
- package/lib/cjs/augment/VaultUser.d.ts +8 -0
- package/lib/cjs/augment/VaultUser.js +7 -0
- package/lib/cjs/augment.d.ts +14 -0
- package/lib/cjs/augment.js +16 -0
- package/lib/cjs/bundler/actions.d.ts +637 -0
- package/lib/cjs/bundler/actions.js +1088 -0
- package/lib/cjs/bundler/index.d.ts +2 -0
- package/lib/cjs/bundler/index.js +18 -0
- package/lib/cjs/bundler/types.d.ts +200 -0
- package/lib/cjs/bundler/types.js +2 -0
- package/lib/cjs/constants.d.ts +3 -0
- package/lib/cjs/constants.js +32 -0
- package/lib/cjs/entities/index.d.ts +6 -3
- package/lib/cjs/entities/index.js +41 -17
- package/lib/cjs/entities/marketV1/marketV1.d.ts +161 -24
- package/lib/cjs/entities/marketV1/marketV1.js +166 -19
- package/lib/cjs/entities/reallocationData.d.ts +193 -0
- package/lib/cjs/entities/reallocationData.js +406 -0
- package/lib/cjs/errors.d.ts +2 -0
- package/lib/cjs/errors.js +18 -0
- package/lib/cjs/fetch.d.ts +1 -0
- package/lib/cjs/fetch.js +30 -0
- package/lib/cjs/format.d.ts +2 -0
- package/lib/cjs/format.js +22 -0
- package/lib/cjs/helpers/computeReallocations.d.ts +63 -12
- package/lib/cjs/helpers/computeReallocations.js +160 -41
- package/lib/cjs/helpers/constant.d.ts +17 -0
- package/lib/cjs/helpers/constant.js +30 -1
- package/lib/cjs/helpers/index.d.ts +3 -2
- package/lib/cjs/helpers/index.js +14 -2
- package/lib/cjs/helpers/slippage.d.ts +57 -0
- package/lib/cjs/helpers/slippage.js +82 -0
- package/lib/cjs/helpers/validate.d.ts +40 -7
- package/lib/cjs/helpers/validate.js +67 -12
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/types/action.d.ts +27 -6
- package/lib/cjs/types/client.d.ts +1 -1
- package/lib/cjs/types/error.d.ts +155 -7
- package/lib/cjs/types/error.js +202 -11
- package/lib/cjs/types/sharedLiquidity.d.ts +43 -3
- package/lib/cjs/types.d.ts +3 -0
- package/lib/cjs/types.js +17 -0
- package/lib/cjs/utils.d.ts +9 -0
- package/lib/cjs/utils.js +65 -0
- package/lib/esm/abis.d.ts +2816 -0
- package/lib/esm/abis.js +1922 -0
- package/lib/esm/actions/index.d.ts +3 -0
- package/lib/esm/actions/marketV1/borrow.js +2 -5
- package/lib/esm/actions/marketV1/buildReallocationActions.d.ts +2 -3
- package/lib/esm/actions/marketV1/buildReallocationActions.js +1 -2
- package/lib/esm/actions/marketV1/index.d.ts +2 -0
- package/lib/esm/actions/marketV1/index.js +2 -0
- package/lib/esm/actions/marketV1/repay.d.ts +2 -0
- package/lib/esm/actions/marketV1/repay.js +4 -5
- package/lib/esm/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
- package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +4 -5
- package/lib/esm/actions/marketV1/supply.d.ts +69 -0
- package/lib/esm/actions/marketV1/supply.js +124 -0
- package/lib/esm/actions/marketV1/supplyCollateral.d.ts +3 -1
- package/lib/esm/actions/marketV1/supplyCollateral.js +7 -8
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +3 -1
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +7 -8
- package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
- package/lib/esm/actions/marketV1/withdraw.js +106 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +1 -1
- package/lib/esm/actions/requirements/getRequirementsAction.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.js +1 -1
- package/lib/esm/actions/vaultV1/deposit.d.ts +2 -0
- package/lib/esm/actions/vaultV1/deposit.js +3 -4
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +2 -0
- package/lib/esm/actions/vaultV1/migrateToV2.js +3 -1
- package/lib/esm/actions/vaultV2/deposit.d.ts +2 -0
- package/lib/esm/actions/vaultV2/deposit.js +3 -4
- package/lib/esm/addresses.d.ts +2 -0
- package/lib/esm/addresses.js +1 -0
- package/lib/esm/augment/AccrualPosition.d.ts +9 -0
- package/lib/esm/augment/AccrualPosition.js +5 -0
- package/lib/esm/augment/AccrualVault.d.ts +8 -0
- package/lib/esm/augment/AccrualVault.js +4 -0
- package/lib/esm/augment/Holding.d.ts +8 -0
- package/lib/esm/augment/Holding.js +4 -0
- package/lib/esm/augment/Market.d.ts +8 -0
- package/lib/esm/augment/Market.js +4 -0
- package/lib/esm/augment/MarketParams.d.ts +8 -0
- package/lib/esm/augment/MarketParams.js +4 -0
- package/lib/esm/augment/Position.d.ts +8 -0
- package/lib/esm/augment/Position.js +4 -0
- package/lib/esm/augment/Token.d.ts +8 -0
- package/lib/esm/augment/Token.js +4 -0
- package/lib/esm/augment/User.d.ts +8 -0
- package/lib/esm/augment/User.js +4 -0
- package/lib/esm/augment/Vault.d.ts +8 -0
- package/lib/esm/augment/Vault.js +4 -0
- package/lib/esm/augment/VaultConfig.d.ts +8 -0
- package/lib/esm/augment/VaultConfig.js +4 -0
- package/lib/esm/augment/VaultMarketAllocation.d.ts +8 -0
- package/lib/esm/augment/VaultMarketAllocation.js +4 -0
- package/lib/esm/augment/VaultMarketConfig.d.ts +8 -0
- package/lib/esm/augment/VaultMarketConfig.js +4 -0
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +5 -0
- package/lib/esm/augment/VaultUser.d.ts +8 -0
- package/lib/esm/augment/VaultUser.js +4 -0
- package/lib/esm/augment.d.ts +14 -0
- package/lib/esm/augment.js +14 -0
- package/lib/esm/bundler/actions.d.ts +637 -0
- package/lib/esm/bundler/actions.js +1085 -0
- package/lib/esm/bundler/index.d.ts +2 -0
- package/lib/esm/bundler/index.js +2 -0
- package/lib/esm/bundler/types.d.ts +200 -0
- package/lib/esm/bundler/types.js +1 -0
- package/lib/esm/constants.d.ts +3 -0
- package/lib/esm/constants.js +3 -0
- package/lib/esm/entities/index.d.ts +6 -3
- package/lib/esm/entities/index.js +5 -3
- package/lib/esm/entities/marketV1/marketV1.d.ts +161 -24
- package/lib/esm/entities/marketV1/marketV1.js +170 -23
- package/lib/esm/entities/reallocationData.d.ts +193 -0
- package/lib/esm/entities/reallocationData.js +402 -0
- package/lib/esm/errors.d.ts +2 -0
- package/lib/esm/errors.js +1 -0
- package/lib/esm/fetch.d.ts +1 -0
- package/lib/esm/fetch.js +1 -0
- package/lib/esm/format.d.ts +2 -0
- package/lib/esm/format.js +1 -0
- package/lib/esm/helpers/computeReallocations.d.ts +63 -12
- package/lib/esm/helpers/computeReallocations.js +160 -41
- package/lib/esm/helpers/constant.d.ts +17 -0
- package/lib/esm/helpers/constant.js +30 -1
- package/lib/esm/helpers/index.d.ts +3 -2
- package/lib/esm/helpers/index.js +3 -2
- package/lib/esm/helpers/slippage.d.ts +57 -0
- package/lib/esm/helpers/slippage.js +80 -0
- package/lib/esm/helpers/validate.d.ts +40 -7
- package/lib/esm/helpers/validate.js +63 -11
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/types/action.d.ts +27 -6
- package/lib/esm/types/client.d.ts +1 -1
- package/lib/esm/types/error.d.ts +155 -7
- package/lib/esm/types/error.js +185 -9
- package/lib/esm/types/sharedLiquidity.d.ts +43 -3
- package/lib/esm/types.d.ts +3 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.d.ts +9 -0
- package/lib/esm/utils.js +7 -0
- package/package.json +128 -17
- package/src/index.ts +0 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { DEFAULT_SLIPPAGE_TOLERANCE, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { fetchAccrualPosition,
|
|
2
|
+
import { fetchAccrualPosition, fetchMarket, fetchPosition, fetchVault, fetchVaultMarketConfig, } from "@morpho-org/blue-sdk-viem";
|
|
3
3
|
import { Time } from "@morpho-org/morpho-ts";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { getMorphoAuthorizationRequirement, getRequirements, marketV1Borrow, marketV1Repay, marketV1RepayWithdrawCollateral, marketV1Supply, marketV1SupplyCollateral, marketV1SupplyCollateralBorrow, marketV1Withdraw, marketV1WithdrawCollateral, } from "../../actions/index.js";
|
|
5
|
+
import { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, computeReallocations, validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
|
|
6
|
+
import { MarketIdMismatchError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeNativeAmountError, NegativeSupplyAmountError, NonPositiveAssetAmountError, NonPositiveBorrowAmountError, NonPositiveRepayAmountError, NonPositiveWithdrawAmountError, NonPositiveWithdrawCollateralAmountError, WithdrawExceedsCollateralError, ZeroCollateralAmountError, ZeroSupplyAmountError, } from "../../types/index.js";
|
|
7
|
+
import { ReallocationData } from "../reallocationData.js";
|
|
8
8
|
export class MorphoMarketV1 {
|
|
9
9
|
client;
|
|
10
10
|
marketParams;
|
|
@@ -31,6 +31,120 @@ export class MorphoMarketV1 {
|
|
|
31
31
|
chainId: this.chainId,
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
+
supply({ amount = 0n, userAddress, nativeAmount, marketData, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, }) {
|
|
35
|
+
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
36
|
+
if (amount < 0n) {
|
|
37
|
+
throw new NegativeSupplyAmountError(this.marketParams.id);
|
|
38
|
+
}
|
|
39
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
40
|
+
throw new NegativeNativeAmountError(nativeAmount);
|
|
41
|
+
}
|
|
42
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
43
|
+
if (totalAssets === 0n) {
|
|
44
|
+
throw new ZeroSupplyAmountError(this.marketParams.id);
|
|
45
|
+
}
|
|
46
|
+
if (marketData.id !== this.marketParams.id) {
|
|
47
|
+
throw new MarketIdMismatchError(marketData.id, this.marketParams.id);
|
|
48
|
+
}
|
|
49
|
+
validateSlippageTolerance(slippageTolerance);
|
|
50
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
51
|
+
validateNativeAsset(this.chainId, this.marketParams.loanToken);
|
|
52
|
+
}
|
|
53
|
+
const maxSharePrice = computeMaxSupplySharePrice({
|
|
54
|
+
supplyAssets: totalAssets,
|
|
55
|
+
market: marketData,
|
|
56
|
+
slippageTolerance,
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
getRequirements: (params) => getRequirements(this.client.viemClient, {
|
|
60
|
+
address: this.marketParams.loanToken,
|
|
61
|
+
chainId: this.chainId,
|
|
62
|
+
supportSignature: this.client.options.supportSignature,
|
|
63
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
64
|
+
useSimplePermit: params?.useSimplePermit,
|
|
65
|
+
args: { amount, from: userAddress },
|
|
66
|
+
}),
|
|
67
|
+
buildTx: (requirementSignature) => marketV1Supply({
|
|
68
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
69
|
+
args: {
|
|
70
|
+
amount,
|
|
71
|
+
nativeAmount,
|
|
72
|
+
onBehalf: userAddress,
|
|
73
|
+
maxSharePrice,
|
|
74
|
+
requirementSignature,
|
|
75
|
+
},
|
|
76
|
+
metadata: this.client.options.metadata,
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
withdraw(params) {
|
|
81
|
+
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
82
|
+
const { userAddress, receiver = userAddress, positionData, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, reallocations, } = params;
|
|
83
|
+
// Mode normalization: a missing or undefined `assets`/`shares` key collapses to `0n`
|
|
84
|
+
// so the mutual-exclusion and positivity checks below are pure value comparisons.
|
|
85
|
+
const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
|
|
86
|
+
const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
|
|
87
|
+
// Mode conflict comes first: detect "both values present" (either non-zero)
|
|
88
|
+
// before positivity, so `{ assets: -1n, shares: 5n }` reports the actual
|
|
89
|
+
// mode conflict instead of being misclassified as a positivity error.
|
|
90
|
+
// Mirrors action layer.
|
|
91
|
+
if (assets !== 0n && shares !== 0n) {
|
|
92
|
+
throw new MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
|
|
93
|
+
}
|
|
94
|
+
if (assets < 0n || shares < 0n || (assets === 0n && shares === 0n)) {
|
|
95
|
+
throw new NonPositiveWithdrawAmountError(this.marketParams.id);
|
|
96
|
+
}
|
|
97
|
+
validateSlippageTolerance(slippageTolerance);
|
|
98
|
+
if (!positionData) {
|
|
99
|
+
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
100
|
+
}
|
|
101
|
+
validateAccrualPosition({
|
|
102
|
+
positionData,
|
|
103
|
+
expectedMarketId: this.marketParams.id,
|
|
104
|
+
expectedUser: userAddress,
|
|
105
|
+
});
|
|
106
|
+
if (shares > 0n) {
|
|
107
|
+
validateWithdrawShares({
|
|
108
|
+
positionData,
|
|
109
|
+
withdrawShares: shares,
|
|
110
|
+
marketId: this.marketParams.id,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
validateWithdrawAmount({
|
|
115
|
+
positionData,
|
|
116
|
+
withdrawAssets: assets,
|
|
117
|
+
marketId: this.marketParams.id,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const minSharePrice = computeMinWithdrawSharePrice({
|
|
121
|
+
withdrawAssets: assets,
|
|
122
|
+
withdrawShares: shares,
|
|
123
|
+
market: positionData.market,
|
|
124
|
+
slippageTolerance,
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
getRequirements: async () => {
|
|
128
|
+
const authTx = await getMorphoAuthorizationRequirement({
|
|
129
|
+
viemClient: this.client.viemClient,
|
|
130
|
+
chainId: this.chainId,
|
|
131
|
+
userAddress,
|
|
132
|
+
});
|
|
133
|
+
return authTx ? [authTx] : [];
|
|
134
|
+
},
|
|
135
|
+
buildTx: () => marketV1Withdraw({
|
|
136
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
137
|
+
args: {
|
|
138
|
+
assets,
|
|
139
|
+
shares,
|
|
140
|
+
receiver,
|
|
141
|
+
minSharePrice,
|
|
142
|
+
reallocations,
|
|
143
|
+
},
|
|
144
|
+
metadata: this.client.options.metadata,
|
|
145
|
+
}),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
34
148
|
supplyCollateral({ amount = 0n, userAddress, nativeAmount, }) {
|
|
35
149
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
36
150
|
if (amount < 0n) {
|
|
@@ -44,7 +158,7 @@ export class MorphoMarketV1 {
|
|
|
44
158
|
throw new ZeroCollateralAmountError(this.marketParams.id);
|
|
45
159
|
}
|
|
46
160
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
47
|
-
|
|
161
|
+
validateNativeAsset(this.chainId, this.marketParams.collateralToken);
|
|
48
162
|
}
|
|
49
163
|
return {
|
|
50
164
|
getRequirements: (params) => getRequirements(this.client.viemClient, {
|
|
@@ -392,7 +506,7 @@ export class MorphoMarketV1 {
|
|
|
392
506
|
}
|
|
393
507
|
validateSlippageTolerance(slippageTolerance);
|
|
394
508
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
395
|
-
|
|
509
|
+
validateNativeAsset(this.chainId, this.marketParams.collateralToken);
|
|
396
510
|
}
|
|
397
511
|
validatePositionHealth({
|
|
398
512
|
positionData,
|
|
@@ -444,6 +558,15 @@ export class MorphoMarketV1 {
|
|
|
444
558
|
}),
|
|
445
559
|
};
|
|
446
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* Fetches all on-chain inputs needed to compute public allocator reallocations.
|
|
563
|
+
*
|
|
564
|
+
* @param params - Reallocation data fetch parameters.
|
|
565
|
+
* @param params.vaultAddresses - Vaults to inspect for source-market liquidity.
|
|
566
|
+
* @param params.block - Block number and timestamp used for consistent RPC reads.
|
|
567
|
+
* @returns Reallocation data ready for {@link getReallocations}.
|
|
568
|
+
* @throws {ChainIdMismatchError} when the client chain does not match this market.
|
|
569
|
+
*/
|
|
447
570
|
async getReallocationData({ vaultAddresses, block, }) {
|
|
448
571
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
449
572
|
const client = this.client.viemClient;
|
|
@@ -475,8 +598,7 @@ export class MorphoMarketV1 {
|
|
|
475
598
|
}
|
|
476
599
|
// Phase 2: Fetch all source markets, vault configs, and positions in parallel.
|
|
477
600
|
const sourceMarketIds = [...allMarketIds].filter((mid) => mid !== targetMarketId);
|
|
478
|
-
const
|
|
479
|
-
const [markets, configs, positions, holdings] = await Promise.all([
|
|
601
|
+
const [markets, configs, positions] = await Promise.all([
|
|
480
602
|
Promise.all(sourceMarketIds.map((mid) => fetchMarket(mid, client, fetchParams))),
|
|
481
603
|
Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => fetchVaultMarketConfig(vault, mid, client, fetchParams).then((config) => ({ vault, mid, config })))),
|
|
482
604
|
Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => fetchPosition(vault, mid, client, fetchParams).then((position) => ({
|
|
@@ -484,9 +606,8 @@ export class MorphoMarketV1 {
|
|
|
484
606
|
mid,
|
|
485
607
|
position,
|
|
486
608
|
})))),
|
|
487
|
-
Promise.all(vaultAddresses.map((addr) => fetchHolding(addr, loanToken, client, fetchParams))),
|
|
488
609
|
]);
|
|
489
|
-
// Assemble records for
|
|
610
|
+
// Assemble records for ReallocationData.
|
|
490
611
|
const marketsRecord = {
|
|
491
612
|
[targetMarketId]: targetMarket,
|
|
492
613
|
};
|
|
@@ -505,26 +626,52 @@ export class MorphoMarketV1 {
|
|
|
505
626
|
for (const { vault, mid, position } of positions) {
|
|
506
627
|
(positionsRecord[vault] ??= {})[mid] = position;
|
|
507
628
|
}
|
|
508
|
-
|
|
509
|
-
for (const holding of holdings) {
|
|
510
|
-
(holdingsRecord[holding.user] ??= {})[holding.token] = holding;
|
|
511
|
-
}
|
|
512
|
-
return new SimulationState({
|
|
629
|
+
return new ReallocationData({
|
|
513
630
|
chainId: this.chainId,
|
|
514
|
-
block,
|
|
515
631
|
markets: marketsRecord,
|
|
516
632
|
vaults: vaultsRecord,
|
|
517
633
|
vaultMarketConfigs: vaultMarketConfigsRecord,
|
|
518
634
|
positions: positionsRecord,
|
|
519
|
-
holdings: holdingsRecord,
|
|
520
635
|
});
|
|
521
636
|
}
|
|
522
|
-
|
|
637
|
+
/**
|
|
638
|
+
* Computes public allocator reallocations for a borrow or withdraw on this market.
|
|
639
|
+
*
|
|
640
|
+
* Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
|
|
641
|
+
* or `{ operation, amount }` for a borrow or loan-asset withdraw.
|
|
642
|
+
*
|
|
643
|
+
* @param params - Reallocation computation parameters.
|
|
644
|
+
* @param params.reallocationData - State returned by {@link getReallocationData}.
|
|
645
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
646
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
647
|
+
* @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
|
|
648
|
+
* @param params.options - Optional allocator and utilization options.
|
|
649
|
+
* @returns Vault reallocations ready to pass to `borrow`, `supplyCollateralBorrow`, or `withdraw`.
|
|
650
|
+
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
651
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
|
|
652
|
+
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
|
|
653
|
+
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
654
|
+
* @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
|
|
655
|
+
*/
|
|
656
|
+
getReallocations(params) {
|
|
657
|
+
validateChainId(params.reallocationData.chainId, this.chainId);
|
|
658
|
+
const marketId = this.marketParams.id;
|
|
659
|
+
const options = { enabled: true, ...params.options };
|
|
660
|
+
if (params.borrowAmount !== undefined) {
|
|
661
|
+
return computeReallocations({
|
|
662
|
+
reallocationData: params.reallocationData,
|
|
663
|
+
marketId,
|
|
664
|
+
operation: "borrow",
|
|
665
|
+
amount: params.borrowAmount,
|
|
666
|
+
options,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
523
669
|
return computeReallocations({
|
|
524
|
-
reallocationData,
|
|
525
|
-
marketId
|
|
526
|
-
|
|
527
|
-
|
|
670
|
+
reallocationData: params.reallocationData,
|
|
671
|
+
marketId,
|
|
672
|
+
operation: params.operation,
|
|
673
|
+
amount: params.amount,
|
|
674
|
+
options,
|
|
528
675
|
});
|
|
529
676
|
}
|
|
530
677
|
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { AccrualPosition, Market, type MarketId, Position, Vault, VaultMarketConfig } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import type { PublicAllocatorOptions, PublicReallocation } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Input state required to construct {@link ReallocationData}.
|
|
6
|
+
*/
|
|
7
|
+
export interface InputReallocationData {
|
|
8
|
+
/** Chain id associated with the fetched state. */
|
|
9
|
+
readonly chainId: number;
|
|
10
|
+
/** Markets indexed by market id. */
|
|
11
|
+
readonly markets?: Readonly<Record<MarketId, Market | undefined>>;
|
|
12
|
+
/** Vaults indexed by vault address. */
|
|
13
|
+
readonly vaults?: Readonly<Record<Address, Vault | undefined>>;
|
|
14
|
+
/** Positions indexed by user or vault address, then by market id. */
|
|
15
|
+
readonly positions?: Readonly<Record<Address, Readonly<Record<MarketId, Position | undefined>>>>;
|
|
16
|
+
/** Vault market configs indexed by vault address, then by market id. */
|
|
17
|
+
readonly vaultMarketConfigs?: Readonly<Record<Address, Readonly<Record<MarketId, VaultMarketConfig | undefined>>>>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Narrow state container for computing public allocator reallocations.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* The class owns only the market, vault, position, vault-market-config,
|
|
24
|
+
* and chain data needed by the shared-liquidity algorithm. Constructor inputs
|
|
25
|
+
* are cloned, and simulation steps return cloned `ReallocationData` instances
|
|
26
|
+
* so fetched caller inputs are not mutated.
|
|
27
|
+
*
|
|
28
|
+
* Public records are exposed for inspection and snapshotting only. Treat
|
|
29
|
+
* `markets`, `vaults`, `positions`, and `vaultMarketConfigs` as a read
|
|
30
|
+
* contract keyed by market id or address; use the getters for typed absence
|
|
31
|
+
* errors and use simulation methods to produce updated state.
|
|
32
|
+
*/
|
|
33
|
+
export declare class ReallocationData implements InputReallocationData {
|
|
34
|
+
/** Chain id associated with the fetched reallocation data. */
|
|
35
|
+
readonly chainId: number;
|
|
36
|
+
/** Markets indexed by market id. */
|
|
37
|
+
readonly markets: Record<MarketId, Market | undefined>;
|
|
38
|
+
/** Vaults indexed by vault address. */
|
|
39
|
+
readonly vaults: Record<Address, Vault | undefined>;
|
|
40
|
+
/** Positions indexed by user or vault address, then by market id. */
|
|
41
|
+
readonly positions: Record<Address, Record<MarketId, Position | undefined>>;
|
|
42
|
+
/** Vault market configs indexed by vault address, then by market id. */
|
|
43
|
+
readonly vaultMarketConfigs: Record<Address, Record<MarketId, VaultMarketConfig | undefined>>;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a cloned reallocation state from fetched market, vault, position,
|
|
46
|
+
* and vault-market-config data.
|
|
47
|
+
*
|
|
48
|
+
* @param input - Reallocation input data fetched at a consistent chain state.
|
|
49
|
+
*/
|
|
50
|
+
constructor(input: InputReallocationData);
|
|
51
|
+
/**
|
|
52
|
+
* Creates a deep clone of this reallocation state.
|
|
53
|
+
*
|
|
54
|
+
* @returns A new `ReallocationData` instance with cloned entity objects.
|
|
55
|
+
*/
|
|
56
|
+
clone(): ReallocationData;
|
|
57
|
+
private forkAliasedState;
|
|
58
|
+
/**
|
|
59
|
+
* Gets a market by id.
|
|
60
|
+
*
|
|
61
|
+
* @param marketId - Market id to read.
|
|
62
|
+
* @returns The cloned market data.
|
|
63
|
+
* @throws {@link UnknownReallocationMarketError} when the market is absent.
|
|
64
|
+
*/
|
|
65
|
+
getMarket(marketId: MarketId): Market;
|
|
66
|
+
/**
|
|
67
|
+
* Gets a vault by address.
|
|
68
|
+
*
|
|
69
|
+
* @param vault - Vault address to read.
|
|
70
|
+
* @returns The cloned vault data.
|
|
71
|
+
* @throws {@link UnknownReallocationVaultError} when the vault is absent.
|
|
72
|
+
*/
|
|
73
|
+
getVault(vault: Address): Vault;
|
|
74
|
+
/**
|
|
75
|
+
* Gets a raw market position.
|
|
76
|
+
*
|
|
77
|
+
* @param user - Position owner address, usually a MetaMorpho vault.
|
|
78
|
+
* @param marketId - Market id for the position.
|
|
79
|
+
* @returns The cloned position data.
|
|
80
|
+
* @throws {@link UnknownReallocationPositionError} when the position is absent.
|
|
81
|
+
*/
|
|
82
|
+
getPosition(user: Address, marketId: MarketId): Position;
|
|
83
|
+
/**
|
|
84
|
+
* Gets a position wrapped with its market accrual helpers.
|
|
85
|
+
*
|
|
86
|
+
* @param user - Position owner address, usually a MetaMorpho vault.
|
|
87
|
+
* @param marketId - Market id for the position.
|
|
88
|
+
* @returns Accrual-aware position data.
|
|
89
|
+
* @throws {@link UnknownReallocationPositionError} when the position is absent.
|
|
90
|
+
* @throws {@link UnknownReallocationMarketError} when the market is absent.
|
|
91
|
+
*/
|
|
92
|
+
getAccrualPosition(user: Address, marketId: MarketId): AccrualPosition;
|
|
93
|
+
/**
|
|
94
|
+
* Gets a vault-market config.
|
|
95
|
+
*
|
|
96
|
+
* @param vault - Vault address.
|
|
97
|
+
* @param marketId - Market id configured by the vault.
|
|
98
|
+
* @returns The cloned vault-market config.
|
|
99
|
+
* @throws {@link UnknownReallocationVaultMarketConfigError} when the config is absent.
|
|
100
|
+
*/
|
|
101
|
+
getVaultMarketConfig(vault: Address, marketId: MarketId): VaultMarketConfig;
|
|
102
|
+
/**
|
|
103
|
+
* Calculates public reallocations that can supply liquidity to `marketId`.
|
|
104
|
+
*
|
|
105
|
+
* @remarks
|
|
106
|
+
* The algorithm repeatedly chooses the single largest currently available
|
|
107
|
+
* source-market withdrawal across reallocatable vaults, applies that
|
|
108
|
+
* withdrawal to a cloned state, and repeats until no valid withdrawal remains.
|
|
109
|
+
*
|
|
110
|
+
* Pass `options.timestamp` to evaluate market interest and pending public
|
|
111
|
+
* allocator caps at the same block as the fetched reallocation data.
|
|
112
|
+
* This method does not add the legacy one-hour delay margin that
|
|
113
|
+
* `SimulationState.getMarketPublicReallocations` applied before measuring
|
|
114
|
+
* target-market vault headroom. If a transaction may land later than the
|
|
115
|
+
* fetched block, pass a future `timestamp` or reserve your own headroom so
|
|
116
|
+
* interest accrued before inclusion does not make `reallocateTo` exceed the
|
|
117
|
+
* target market cap.
|
|
118
|
+
* Returned `data` normalizes `vault.publicAllocatorConfig.accruedFee` to the
|
|
119
|
+
* on-chain `reallocateTo` fee semantics: one fee charge per vault with at
|
|
120
|
+
* least one computed withdrawal.
|
|
121
|
+
*
|
|
122
|
+
* @param marketId - Target market to supply with shared liquidity.
|
|
123
|
+
* @param options - Optional allocator discovery options.
|
|
124
|
+
* @returns Computed source-market withdrawals and the post-reallocation state.
|
|
125
|
+
* @throws {@link UnknownReallocationMarketError} when the target market is absent.
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { createPublicClient, http } from "viem";
|
|
129
|
+
* import { mainnet } from "viem/chains";
|
|
130
|
+
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
131
|
+
* import {
|
|
132
|
+
* morphoViemExtension,
|
|
133
|
+
* type PublicReallocation,
|
|
134
|
+
* } from "@morpho-org/morpho-sdk";
|
|
135
|
+
* import type { ReallocationData } from "@morpho-org/morpho-sdk/entities";
|
|
136
|
+
*
|
|
137
|
+
* const client = createPublicClient({
|
|
138
|
+
* chain: mainnet,
|
|
139
|
+
* transport: http(),
|
|
140
|
+
* }).extend(morphoViemExtension());
|
|
141
|
+
*
|
|
142
|
+
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
143
|
+
* const market = client.morpho.marketV1(marketParams, mainnet.id);
|
|
144
|
+
* const block = await client.getBlock();
|
|
145
|
+
* const reallocationData = await market.getReallocationData({
|
|
146
|
+
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
147
|
+
* block: { number: block.number, timestamp: block.timestamp },
|
|
148
|
+
* });
|
|
149
|
+
*
|
|
150
|
+
* const result: {
|
|
151
|
+
* withdrawals: readonly PublicReallocation[];
|
|
152
|
+
* data: ReallocationData;
|
|
153
|
+
* } = reallocationData.getMarketPublicReallocations(marketParams.id, {
|
|
154
|
+
* timestamp: block.timestamp,
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
getMarketPublicReallocations(marketId: MarketId, options?: PublicAllocatorOptions): {
|
|
159
|
+
readonly withdrawals: readonly PublicReallocation[];
|
|
160
|
+
data: ReallocationData;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Gets the largest currently valid source-market withdrawal for a vault.
|
|
164
|
+
*
|
|
165
|
+
* @param params - Candidate vault, target market, prior withdrawals, and allocator limits.
|
|
166
|
+
* @returns The largest withdrawal candidate, or `undefined` if required data is missing.
|
|
167
|
+
*
|
|
168
|
+
* @internal
|
|
169
|
+
*/
|
|
170
|
+
private getLargestVaultWithdrawal;
|
|
171
|
+
/**
|
|
172
|
+
* Applies one public allocator withdrawal and corresponding target-market supply.
|
|
173
|
+
*
|
|
174
|
+
* @param vault - Vault performing the public reallocation.
|
|
175
|
+
* @param supplyMarketId - Target market that receives the withdrawn assets.
|
|
176
|
+
* @param withdrawal - Source market id and asset amount to withdraw.
|
|
177
|
+
* @param timestamp - Timestamp used to accrue source and target positions.
|
|
178
|
+
* @returns A cloned state after applying the simulated reallocation.
|
|
179
|
+
* @throws {@link MissingPublicAllocatorConfigError} when the vault allocator config is absent.
|
|
180
|
+
* @throws {@link UnknownReallocationVaultMarketConfigError} when source or target allocator limits are absent.
|
|
181
|
+
*
|
|
182
|
+
* @internal
|
|
183
|
+
*/
|
|
184
|
+
protected applyPublicReallocation({ vault, supplyMarketId, withdrawal, timestamp, }: {
|
|
185
|
+
readonly vault: Address;
|
|
186
|
+
readonly supplyMarketId: MarketId;
|
|
187
|
+
readonly withdrawal: {
|
|
188
|
+
readonly id: MarketId;
|
|
189
|
+
readonly assets: bigint;
|
|
190
|
+
};
|
|
191
|
+
readonly timestamp: bigint;
|
|
192
|
+
}): ReallocationData;
|
|
193
|
+
}
|