@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
|
@@ -4,10 +4,10 @@ exports.MorphoMarketV1 = void 0;
|
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
-
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
8
7
|
const index_js_1 = require("../../actions/index.js");
|
|
9
8
|
const index_js_2 = require("../../helpers/index.js");
|
|
10
9
|
const index_js_3 = require("../../types/index.js");
|
|
10
|
+
const reallocationData_js_1 = require("../reallocationData.js");
|
|
11
11
|
class MorphoMarketV1 {
|
|
12
12
|
client;
|
|
13
13
|
marketParams;
|
|
@@ -34,6 +34,120 @@ class MorphoMarketV1 {
|
|
|
34
34
|
chainId: this.chainId,
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
+
supply({ amount = 0n, userAddress, nativeAmount, marketData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, }) {
|
|
38
|
+
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
39
|
+
if (amount < 0n) {
|
|
40
|
+
throw new index_js_3.NegativeSupplyAmountError(this.marketParams.id);
|
|
41
|
+
}
|
|
42
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
43
|
+
throw new index_js_3.NegativeNativeAmountError(nativeAmount);
|
|
44
|
+
}
|
|
45
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
46
|
+
if (totalAssets === 0n) {
|
|
47
|
+
throw new index_js_3.ZeroSupplyAmountError(this.marketParams.id);
|
|
48
|
+
}
|
|
49
|
+
if (marketData.id !== this.marketParams.id) {
|
|
50
|
+
throw new index_js_3.MarketIdMismatchError(marketData.id, this.marketParams.id);
|
|
51
|
+
}
|
|
52
|
+
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
53
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
54
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
|
|
55
|
+
}
|
|
56
|
+
const maxSharePrice = (0, index_js_2.computeMaxSupplySharePrice)({
|
|
57
|
+
supplyAssets: totalAssets,
|
|
58
|
+
market: marketData,
|
|
59
|
+
slippageTolerance,
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
|
|
63
|
+
address: this.marketParams.loanToken,
|
|
64
|
+
chainId: this.chainId,
|
|
65
|
+
supportSignature: this.client.options.supportSignature,
|
|
66
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
67
|
+
useSimplePermit: params?.useSimplePermit,
|
|
68
|
+
args: { amount, from: userAddress },
|
|
69
|
+
}),
|
|
70
|
+
buildTx: (requirementSignature) => (0, index_js_1.marketV1Supply)({
|
|
71
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
72
|
+
args: {
|
|
73
|
+
amount,
|
|
74
|
+
nativeAmount,
|
|
75
|
+
onBehalf: userAddress,
|
|
76
|
+
maxSharePrice,
|
|
77
|
+
requirementSignature,
|
|
78
|
+
},
|
|
79
|
+
metadata: this.client.options.metadata,
|
|
80
|
+
}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
withdraw(params) {
|
|
84
|
+
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
85
|
+
const { userAddress, receiver = userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, } = params;
|
|
86
|
+
// Mode normalization: a missing or undefined `assets`/`shares` key collapses to `0n`
|
|
87
|
+
// so the mutual-exclusion and positivity checks below are pure value comparisons.
|
|
88
|
+
const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
|
|
89
|
+
const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
|
|
90
|
+
// Mode conflict comes first: detect "both values present" (either non-zero)
|
|
91
|
+
// before positivity, so `{ assets: -1n, shares: 5n }` reports the actual
|
|
92
|
+
// mode conflict instead of being misclassified as a positivity error.
|
|
93
|
+
// Mirrors action layer.
|
|
94
|
+
if (assets !== 0n && shares !== 0n) {
|
|
95
|
+
throw new index_js_3.MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
|
|
96
|
+
}
|
|
97
|
+
if (assets < 0n || shares < 0n || (assets === 0n && shares === 0n)) {
|
|
98
|
+
throw new index_js_3.NonPositiveWithdrawAmountError(this.marketParams.id);
|
|
99
|
+
}
|
|
100
|
+
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
101
|
+
if (!positionData) {
|
|
102
|
+
throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
|
|
103
|
+
}
|
|
104
|
+
(0, index_js_2.validateAccrualPosition)({
|
|
105
|
+
positionData,
|
|
106
|
+
expectedMarketId: this.marketParams.id,
|
|
107
|
+
expectedUser: userAddress,
|
|
108
|
+
});
|
|
109
|
+
if (shares > 0n) {
|
|
110
|
+
(0, index_js_2.validateWithdrawShares)({
|
|
111
|
+
positionData,
|
|
112
|
+
withdrawShares: shares,
|
|
113
|
+
marketId: this.marketParams.id,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
(0, index_js_2.validateWithdrawAmount)({
|
|
118
|
+
positionData,
|
|
119
|
+
withdrawAssets: assets,
|
|
120
|
+
marketId: this.marketParams.id,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const minSharePrice = (0, index_js_2.computeMinWithdrawSharePrice)({
|
|
124
|
+
withdrawAssets: assets,
|
|
125
|
+
withdrawShares: shares,
|
|
126
|
+
market: positionData.market,
|
|
127
|
+
slippageTolerance,
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
getRequirements: async () => {
|
|
131
|
+
const authTx = await (0, index_js_1.getMorphoAuthorizationRequirement)({
|
|
132
|
+
viemClient: this.client.viemClient,
|
|
133
|
+
chainId: this.chainId,
|
|
134
|
+
userAddress,
|
|
135
|
+
});
|
|
136
|
+
return authTx ? [authTx] : [];
|
|
137
|
+
},
|
|
138
|
+
buildTx: () => (0, index_js_1.marketV1Withdraw)({
|
|
139
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
140
|
+
args: {
|
|
141
|
+
assets,
|
|
142
|
+
shares,
|
|
143
|
+
receiver,
|
|
144
|
+
minSharePrice,
|
|
145
|
+
reallocations,
|
|
146
|
+
},
|
|
147
|
+
metadata: this.client.options.metadata,
|
|
148
|
+
}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
37
151
|
supplyCollateral({ amount = 0n, userAddress, nativeAmount, }) {
|
|
38
152
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
39
153
|
if (amount < 0n) {
|
|
@@ -47,7 +161,7 @@ class MorphoMarketV1 {
|
|
|
47
161
|
throw new index_js_3.ZeroCollateralAmountError(this.marketParams.id);
|
|
48
162
|
}
|
|
49
163
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
50
|
-
(0, index_js_2.
|
|
164
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.collateralToken);
|
|
51
165
|
}
|
|
52
166
|
return {
|
|
53
167
|
getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
|
|
@@ -395,7 +509,7 @@ class MorphoMarketV1 {
|
|
|
395
509
|
}
|
|
396
510
|
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
397
511
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
398
|
-
(0, index_js_2.
|
|
512
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.collateralToken);
|
|
399
513
|
}
|
|
400
514
|
(0, index_js_2.validatePositionHealth)({
|
|
401
515
|
positionData,
|
|
@@ -447,6 +561,15 @@ class MorphoMarketV1 {
|
|
|
447
561
|
}),
|
|
448
562
|
};
|
|
449
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* Fetches all on-chain inputs needed to compute public allocator reallocations.
|
|
566
|
+
*
|
|
567
|
+
* @param params - Reallocation data fetch parameters.
|
|
568
|
+
* @param params.vaultAddresses - Vaults to inspect for source-market liquidity.
|
|
569
|
+
* @param params.block - Block number and timestamp used for consistent RPC reads.
|
|
570
|
+
* @returns Reallocation data ready for {@link getReallocations}.
|
|
571
|
+
* @throws {ChainIdMismatchError} when the client chain does not match this market.
|
|
572
|
+
*/
|
|
450
573
|
async getReallocationData({ vaultAddresses, block, }) {
|
|
451
574
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
452
575
|
const client = this.client.viemClient;
|
|
@@ -478,8 +601,7 @@ class MorphoMarketV1 {
|
|
|
478
601
|
}
|
|
479
602
|
// Phase 2: Fetch all source markets, vault configs, and positions in parallel.
|
|
480
603
|
const sourceMarketIds = [...allMarketIds].filter((mid) => mid !== targetMarketId);
|
|
481
|
-
const
|
|
482
|
-
const [markets, configs, positions, holdings] = await Promise.all([
|
|
604
|
+
const [markets, configs, positions] = await Promise.all([
|
|
483
605
|
Promise.all(sourceMarketIds.map((mid) => (0, blue_sdk_viem_1.fetchMarket)(mid, client, fetchParams))),
|
|
484
606
|
Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => (0, blue_sdk_viem_1.fetchVaultMarketConfig)(vault, mid, client, fetchParams).then((config) => ({ vault, mid, config })))),
|
|
485
607
|
Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => (0, blue_sdk_viem_1.fetchPosition)(vault, mid, client, fetchParams).then((position) => ({
|
|
@@ -487,9 +609,8 @@ class MorphoMarketV1 {
|
|
|
487
609
|
mid,
|
|
488
610
|
position,
|
|
489
611
|
})))),
|
|
490
|
-
Promise.all(vaultAddresses.map((addr) => (0, blue_sdk_viem_1.fetchHolding)(addr, loanToken, client, fetchParams))),
|
|
491
612
|
]);
|
|
492
|
-
// Assemble records for
|
|
613
|
+
// Assemble records for ReallocationData.
|
|
493
614
|
const marketsRecord = {
|
|
494
615
|
[targetMarketId]: targetMarket,
|
|
495
616
|
};
|
|
@@ -508,26 +629,52 @@ class MorphoMarketV1 {
|
|
|
508
629
|
for (const { vault, mid, position } of positions) {
|
|
509
630
|
(positionsRecord[vault] ??= {})[mid] = position;
|
|
510
631
|
}
|
|
511
|
-
|
|
512
|
-
for (const holding of holdings) {
|
|
513
|
-
(holdingsRecord[holding.user] ??= {})[holding.token] = holding;
|
|
514
|
-
}
|
|
515
|
-
return new simulation_sdk_1.SimulationState({
|
|
632
|
+
return new reallocationData_js_1.ReallocationData({
|
|
516
633
|
chainId: this.chainId,
|
|
517
|
-
block,
|
|
518
634
|
markets: marketsRecord,
|
|
519
635
|
vaults: vaultsRecord,
|
|
520
636
|
vaultMarketConfigs: vaultMarketConfigsRecord,
|
|
521
637
|
positions: positionsRecord,
|
|
522
|
-
holdings: holdingsRecord,
|
|
523
638
|
});
|
|
524
639
|
}
|
|
525
|
-
|
|
640
|
+
/**
|
|
641
|
+
* Computes public allocator reallocations for a borrow or withdraw on this market.
|
|
642
|
+
*
|
|
643
|
+
* Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
|
|
644
|
+
* or `{ operation, amount }` for a borrow or loan-asset withdraw.
|
|
645
|
+
*
|
|
646
|
+
* @param params - Reallocation computation parameters.
|
|
647
|
+
* @param params.reallocationData - State returned by {@link getReallocationData}.
|
|
648
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
649
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
650
|
+
* @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
|
|
651
|
+
* @param params.options - Optional allocator and utilization options.
|
|
652
|
+
* @returns Vault reallocations ready to pass to `borrow`, `supplyCollateralBorrow`, or `withdraw`.
|
|
653
|
+
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
654
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
|
|
655
|
+
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
|
|
656
|
+
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
657
|
+
* @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
|
|
658
|
+
*/
|
|
659
|
+
getReallocations(params) {
|
|
660
|
+
(0, index_js_2.validateChainId)(params.reallocationData.chainId, this.chainId);
|
|
661
|
+
const marketId = this.marketParams.id;
|
|
662
|
+
const options = { enabled: true, ...params.options };
|
|
663
|
+
if (params.borrowAmount !== undefined) {
|
|
664
|
+
return (0, index_js_2.computeReallocations)({
|
|
665
|
+
reallocationData: params.reallocationData,
|
|
666
|
+
marketId,
|
|
667
|
+
operation: "borrow",
|
|
668
|
+
amount: params.borrowAmount,
|
|
669
|
+
options,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
526
672
|
return (0, index_js_2.computeReallocations)({
|
|
527
|
-
reallocationData,
|
|
528
|
-
marketId
|
|
529
|
-
|
|
530
|
-
|
|
673
|
+
reallocationData: params.reallocationData,
|
|
674
|
+
marketId,
|
|
675
|
+
operation: params.operation,
|
|
676
|
+
amount: params.amount,
|
|
677
|
+
options,
|
|
531
678
|
});
|
|
532
679
|
}
|
|
533
680
|
}
|
|
@@ -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
|
+
}
|