@morpho-org/morpho-sdk 5.4.1 → 5.6.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 +23 -7
- package/lib/cjs/abis.d.ts +314 -1
- package/lib/cjs/abis.js +169 -1
- package/lib/cjs/actions/blue/borrow.d.ts +21 -15
- package/lib/cjs/actions/blue/borrow.js +25 -18
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +1 -1
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +1 -1
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +62 -19
- package/lib/cjs/actions/blue/buildReallocationActions.js +123 -27
- package/lib/cjs/actions/blue/refinance.d.ts +14 -7
- package/lib/cjs/actions/blue/refinance.js +18 -10
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +28 -19
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +39 -23
- package/lib/cjs/actions/blue/withdraw.d.ts +20 -13
- package/lib/cjs/actions/blue/withdraw.js +23 -18
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +9 -2
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +131 -0
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -3
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +91 -0
- package/lib/cjs/actions/signatures/index.d.ts +1 -0
- package/lib/cjs/actions/signatures/index.js +1 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +54 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +96 -0
- package/lib/cjs/actions/vaultV1/index.d.ts +1 -0
- package/lib/cjs/actions/vaultV1/index.js +1 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +56 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +97 -0
- package/lib/cjs/actions/vaultV2/index.d.ts +1 -0
- package/lib/cjs/actions/vaultV2/index.js +1 -0
- package/lib/cjs/bundler/actions.d.ts +118 -2
- package/lib/cjs/bundler/actions.js +212 -6
- package/lib/cjs/bundler/types.d.ts +20 -0
- package/lib/cjs/constants.d.ts +1 -1
- package/lib/cjs/constants.js +3 -1
- package/lib/cjs/entities/blue/blue.d.ts +365 -76
- package/lib/cjs/entities/blue/blue.js +285 -52
- package/lib/cjs/entities/index.d.ts +2 -1
- package/lib/cjs/entities/index.js +6 -3
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +82 -4
- package/lib/cjs/entities/vaultV1/vaultV1.js +164 -0
- package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
- package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +30 -18
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +89 -3
- package/lib/cjs/entities/vaultV2/vaultV2.js +181 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
- package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
- package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
- package/lib/cjs/helpers/constant.d.ts +5 -3
- package/lib/cjs/helpers/constant.js +6 -4
- package/lib/cjs/helpers/index.d.ts +3 -2
- package/lib/cjs/helpers/index.js +8 -3
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +73 -0
- package/lib/cjs/helpers/validate.d.ts +31 -15
- package/lib/cjs/helpers/validate.js +140 -27
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +2 -1
- package/lib/cjs/types/action.d.ts +33 -1
- package/lib/cjs/types/error.d.ts +363 -5
- package/lib/cjs/types/error.js +455 -6
- package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +6 -4
- package/lib/esm/abis.d.ts +314 -1
- package/lib/esm/abis.js +167 -1
- package/lib/esm/actions/blue/borrow.d.ts +21 -15
- package/lib/esm/actions/blue/borrow.js +26 -19
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
- package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
- package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
- package/lib/esm/actions/blue/refinance.d.ts +14 -7
- package/lib/esm/actions/blue/refinance.js +19 -11
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
- package/lib/esm/actions/blue/withdraw.d.ts +20 -13
- package/lib/esm/actions/blue/withdraw.js +24 -19
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -2
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +127 -0
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -3
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +87 -0
- package/lib/esm/actions/signatures/index.d.ts +1 -0
- package/lib/esm/actions/signatures/index.js +1 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +54 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.js +92 -0
- package/lib/esm/actions/vaultV1/index.d.ts +1 -0
- package/lib/esm/actions/vaultV1/index.js +1 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +56 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.js +93 -0
- package/lib/esm/actions/vaultV2/index.d.ts +1 -0
- package/lib/esm/actions/vaultV2/index.js +1 -0
- package/lib/esm/bundler/actions.d.ts +118 -2
- package/lib/esm/bundler/actions.js +215 -9
- package/lib/esm/bundler/types.d.ts +20 -0
- package/lib/esm/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +362 -73
- package/lib/esm/entities/blue/blue.js +288 -55
- package/lib/esm/entities/index.d.ts +2 -1
- package/lib/esm/entities/index.js +2 -1
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +80 -2
- package/lib/esm/entities/vaultV1/vaultV1.js +170 -6
- package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
- package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +33 -16
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +88 -2
- package/lib/esm/entities/vaultV2/vaultV2.js +187 -6
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
- package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
- package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
- package/lib/esm/helpers/constant.d.ts +5 -3
- package/lib/esm/helpers/constant.js +5 -3
- package/lib/esm/helpers/index.d.ts +3 -2
- package/lib/esm/helpers/index.js +3 -2
- package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +70 -0
- package/lib/esm/helpers/validate.d.ts +31 -15
- package/lib/esm/helpers/validate.js +141 -30
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +2 -1
- package/lib/esm/types/action.d.ts +33 -1
- package/lib/esm/types/error.d.ts +363 -5
- package/lib/esm/types/error.js +428 -4
- package/lib/esm/types/sharedLiquidity.d.ts +72 -8
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +9 -11
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
3
|
-
import { Time } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { isAddressEqual } from "viem";
|
|
5
|
-
import {
|
|
1
|
+
import { AccrualVaultV2MorphoMarketV1AdapterV2, DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { erc2612Abi, fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { getChainAddress, Time } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { erc20Abi, isAddressEqual } from "viem";
|
|
5
|
+
import { multicall } from "viem/actions";
|
|
6
|
+
import { encodeErc20Approval, encodeVaultSharesPermit, getGeneralAdapterRequirements, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
|
|
6
7
|
import { validateSlippageTolerance } from "../../helpers/index.js";
|
|
7
|
-
import { ChainIdMismatchError, ChainWNativeMissingError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, } from "../../types/index.js";
|
|
8
|
+
import { AdapterNotPartOfVaultError, ChainIdMismatchError, ChainWNativeMissingError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemRequiresSingleAdapterError, InKindRedeemZeroDeallocationError, InsufficientBlueBalanceForInKindRedeemError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, UnsupportedInKindAdapterError, VaultAddressMismatchError, } from "../../types/index.js";
|
|
8
9
|
export class MorphoVaultV2 {
|
|
9
10
|
client;
|
|
10
11
|
vault;
|
|
@@ -131,6 +132,186 @@ export class MorphoVaultV2 {
|
|
|
131
132
|
}),
|
|
132
133
|
};
|
|
133
134
|
}
|
|
135
|
+
/** {@inheritDoc VaultV2Actions.inKindRedeem} */
|
|
136
|
+
inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }) {
|
|
137
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
138
|
+
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
139
|
+
}
|
|
140
|
+
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
141
|
+
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
142
|
+
}
|
|
143
|
+
if (amount <= 0n)
|
|
144
|
+
throw new NonPositiveInputError("amount", amount);
|
|
145
|
+
const marketParamsListSnapshot = marketParamsList.map(({ loanToken, collateralToken, oracle, irm, lltv }) => ({
|
|
146
|
+
loanToken,
|
|
147
|
+
collateralToken,
|
|
148
|
+
oracle,
|
|
149
|
+
irm,
|
|
150
|
+
lltv,
|
|
151
|
+
}));
|
|
152
|
+
const marketIdListSnapshot = marketParamsListSnapshot.map((marketParams) => MarketUtils.getMarketId(marketParams));
|
|
153
|
+
const now = Time.timestamp();
|
|
154
|
+
const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
|
|
155
|
+
if (deadline <= now)
|
|
156
|
+
throw new ExpiredDeadlineError(deadline, now);
|
|
157
|
+
if (vaultData.accrualAdapters.length !== 1) {
|
|
158
|
+
throw new InKindRedeemRequiresSingleAdapterError(this.vault, vaultData.accrualAdapters.length);
|
|
159
|
+
}
|
|
160
|
+
const soleAdapter = vaultData.accrualAdapters[0];
|
|
161
|
+
if (soleAdapter == null) {
|
|
162
|
+
throw new InKindRedeemRequiresSingleAdapterError(this.vault, 0);
|
|
163
|
+
}
|
|
164
|
+
const adapter = adapterOverride ?? soleAdapter.address;
|
|
165
|
+
if (!isAddressEqual(adapter, soleAdapter.address)) {
|
|
166
|
+
throw new AdapterNotPartOfVaultError(this.vault, adapter);
|
|
167
|
+
}
|
|
168
|
+
if (!(soleAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
169
|
+
throw new UnsupportedInKindAdapterError(adapter);
|
|
170
|
+
}
|
|
171
|
+
const penalty = vaultData.forceDeallocatePenalties[soleAdapter.address] ?? 0n;
|
|
172
|
+
const idleAssets = MathLib.min(vaultData.assetBalance, amount);
|
|
173
|
+
const assetsToDeallocate = MathLib.wDivDown(amount - idleAssets, MathLib.WAD + penalty);
|
|
174
|
+
if (idleAssets === 0n && assetsToDeallocate === 0n) {
|
|
175
|
+
throw new InKindRedeemZeroDeallocationError({
|
|
176
|
+
vault: this.vault,
|
|
177
|
+
amount,
|
|
178
|
+
penalty,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
if (assetsToDeallocate > 0n && marketParamsListSnapshot.length === 0) {
|
|
182
|
+
throw new EmptyMarketParamsListError();
|
|
183
|
+
}
|
|
184
|
+
const assetsByMarket = new Map(soleAdapter.markets.map((market) => [
|
|
185
|
+
market.id,
|
|
186
|
+
market
|
|
187
|
+
.accrueInterest(now)
|
|
188
|
+
.toSupplyAssets(soleAdapter.supplyShares[market.id] ?? 0n),
|
|
189
|
+
]));
|
|
190
|
+
const uniqueMarketIds = new Set(marketIdListSnapshot);
|
|
191
|
+
let covered = 0n;
|
|
192
|
+
for (const id of uniqueMarketIds)
|
|
193
|
+
covered += assetsByMarket.get(id) ?? 0n;
|
|
194
|
+
if (covered < assetsToDeallocate) {
|
|
195
|
+
const maxExitAssets = covered === 0n
|
|
196
|
+
? idleAssets
|
|
197
|
+
: idleAssets +
|
|
198
|
+
MathLib.wMulUp(covered + 1n, MathLib.WAD + penalty) -
|
|
199
|
+
1n;
|
|
200
|
+
throw new InKindRedeemCoverageError({
|
|
201
|
+
required: assetsToDeallocate,
|
|
202
|
+
covered,
|
|
203
|
+
maxExitAssets,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
let remaining = assetsToDeallocate;
|
|
207
|
+
let peak = 0n;
|
|
208
|
+
const consumedMarketIds = new Set();
|
|
209
|
+
const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
|
|
210
|
+
// Interest can lower the burn, while management fees can raise it; bound both endpoints.
|
|
211
|
+
const previewAllowance = (assets) => MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
|
|
212
|
+
let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
|
|
213
|
+
// Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
|
|
214
|
+
requiredShareAllowance += previewAllowance(idleAssets);
|
|
215
|
+
for (const id of marketIdListSnapshot) {
|
|
216
|
+
const available = consumedMarketIds.has(id)
|
|
217
|
+
? 0n
|
|
218
|
+
: (assetsByMarket.get(id) ?? 0n);
|
|
219
|
+
consumedMarketIds.add(id);
|
|
220
|
+
const chunk = MathLib.min(available, remaining);
|
|
221
|
+
peak = MathLib.max(peak, chunk);
|
|
222
|
+
requiredShareAllowance += previewAllowance(MathLib.wMulUp(chunk, penalty));
|
|
223
|
+
requiredShareAllowance += previewAllowance(chunk);
|
|
224
|
+
remaining -= chunk;
|
|
225
|
+
}
|
|
226
|
+
const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
|
|
227
|
+
const addresses = getChainAddresses(this.chainId);
|
|
228
|
+
const blue = addresses.blue ?? addresses.morpho;
|
|
229
|
+
return {
|
|
230
|
+
getRequirements: async () => {
|
|
231
|
+
const requirementsTimestamp = Time.timestamp();
|
|
232
|
+
if (deadline <= requirementsTimestamp) {
|
|
233
|
+
throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
|
|
234
|
+
}
|
|
235
|
+
// Vault gates are intentionally left to the final transaction. A receive gate may inspect
|
|
236
|
+
// VaultExitBundlesV1's transient `initiator`, which is populated only during the actual
|
|
237
|
+
// periphery call. A send-share gate is arbitrary external code and is evaluated repeatedly
|
|
238
|
+
// after intermediate penalty share burns, so one standalone read is not execution-equivalent
|
|
239
|
+
// either. Simulate the finalized transaction after authorization when gate compatibility
|
|
240
|
+
// must be checked before submission.
|
|
241
|
+
const [allowance, nonce, blueBalance] = await multicall(this.client.viemClient, {
|
|
242
|
+
allowFailure: false,
|
|
243
|
+
contracts: [
|
|
244
|
+
{
|
|
245
|
+
address: this.vault,
|
|
246
|
+
abi: erc20Abi,
|
|
247
|
+
functionName: "allowance",
|
|
248
|
+
args: [userAddress, vaultExitBundlesV1],
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
address: this.vault,
|
|
252
|
+
abi: erc2612Abi,
|
|
253
|
+
functionName: "nonces",
|
|
254
|
+
args: [userAddress],
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
address: vaultData.asset,
|
|
258
|
+
abi: erc20Abi,
|
|
259
|
+
functionName: "balanceOf",
|
|
260
|
+
args: [blue],
|
|
261
|
+
},
|
|
262
|
+
],
|
|
263
|
+
});
|
|
264
|
+
if (blueBalance < peak) {
|
|
265
|
+
throw new InsufficientBlueBalanceForInKindRedeemError({
|
|
266
|
+
asset: vaultData.asset,
|
|
267
|
+
available: blueBalance,
|
|
268
|
+
required: peak,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
if (allowance >= requiredShareAllowance)
|
|
272
|
+
return [];
|
|
273
|
+
if (this.client.options.supportSignature) {
|
|
274
|
+
return [
|
|
275
|
+
encodeVaultSharesPermit({
|
|
276
|
+
vault: vaultData,
|
|
277
|
+
version: "vaultV2",
|
|
278
|
+
spender: vaultExitBundlesV1,
|
|
279
|
+
owner: userAddress,
|
|
280
|
+
chainId: this.chainId,
|
|
281
|
+
nonce,
|
|
282
|
+
amount: requiredShareAllowance,
|
|
283
|
+
deadline,
|
|
284
|
+
}),
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
return [
|
|
288
|
+
encodeErc20Approval({
|
|
289
|
+
token: this.vault,
|
|
290
|
+
spender: vaultExitBundlesV1,
|
|
291
|
+
amount: requiredShareAllowance,
|
|
292
|
+
chainId: this.chainId,
|
|
293
|
+
}),
|
|
294
|
+
];
|
|
295
|
+
},
|
|
296
|
+
buildTx: (signatures) => {
|
|
297
|
+
const { permit } = selectRequirementSignatures(signatures, {
|
|
298
|
+
permit: true,
|
|
299
|
+
});
|
|
300
|
+
return vaultV2InKindRedeem({
|
|
301
|
+
vault: { chainId: this.chainId, address: this.vault },
|
|
302
|
+
args: {
|
|
303
|
+
adapter,
|
|
304
|
+
amount,
|
|
305
|
+
marketParamsList: marketParamsListSnapshot,
|
|
306
|
+
userAddress,
|
|
307
|
+
deadline,
|
|
308
|
+
requirementSignature: permit,
|
|
309
|
+
},
|
|
310
|
+
metadata: this.client.options.metadata,
|
|
311
|
+
});
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
}
|
|
134
315
|
forceWithdraw({ deallocations, withdraw, userAddress, }) {
|
|
135
316
|
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
136
317
|
throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|