@morpho-org/morpho-sdk 9.6.0-next.0 → 9.7.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/MIGRATION-v6-to-v7.md +1 -1
- package/README.md +123 -3
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +1 -5
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/cjs/actions/bundles/common.js +39 -32
- package/lib/cjs/actions/midnight/cancelAndMake.js +20 -98
- package/lib/cjs/actions/midnight/index.d.ts +2 -0
- package/lib/cjs/actions/midnight/index.js +2 -0
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +5 -6
- package/lib/cjs/actions/midnight/supplyCollateralBatch.d.ts +39 -0
- package/lib/cjs/actions/midnight/supplyCollateralBatch.js +91 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +31 -32
- package/lib/cjs/actions/midnight/takeBorrow.js +24 -23
- package/lib/cjs/actions/midnight/takeLend.js +24 -23
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
- package/lib/cjs/actions/midnight/takeWithdraw.js +24 -23
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +104 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
- package/lib/cjs/actions/vaultV1/migrateToV2.js +7 -7
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +1 -3
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +1 -5
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +1 -3
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +1 -2
- package/lib/cjs/entities/index.d.ts +5 -5
- package/lib/cjs/entities/index.js +1 -3
- package/lib/cjs/entities/midnight/midnight.d.ts +77 -3
- package/lib/cjs/entities/midnight/midnight.js +174 -0
- package/lib/cjs/entities/midnight/types.d.ts +16 -0
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +12 -46
- package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/cjs/entities/requirements/getErc2612Permit.js +60 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +10 -35
- package/lib/cjs/entities/vaultV2/vaultV2.js +1 -33
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +6 -7
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +1 -3
- package/lib/cjs/helpers/deadShares.js +0 -2
- package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +1 -0
- package/lib/cjs/helpers/signAndVerifyTypedData.js +12 -5
- package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
- package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.js +129 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +15 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +28 -0
- package/lib/cjs/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
- package/lib/cjs/helpers/validateMidnightCollateralSupplies.js +28 -0
- package/lib/cjs/helpers/validatePositiveInput.d.ts +10 -0
- package/lib/cjs/helpers/validatePositiveInput.js +18 -0
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +18 -1
- package/lib/cjs/midnight/errors.d.ts +1 -2
- package/lib/cjs/midnight/errors.js +5 -5
- package/lib/cjs/transactionPlan/approval.d.ts +2 -0
- package/lib/cjs/transactionPlan/approval.js +16 -0
- package/lib/cjs/transactionPlan/error.d.ts +145 -0
- package/lib/cjs/transactionPlan/error.js +186 -0
- package/lib/cjs/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/cjs/transactionPlan/getTokenPermit.js +18 -0
- package/lib/cjs/transactionPlan/index.d.ts +9 -0
- package/lib/cjs/transactionPlan/index.js +24 -0
- package/lib/cjs/transactionPlan/intent.d.ts +51 -0
- package/lib/cjs/transactionPlan/intent.js +54 -0
- package/lib/cjs/transactionPlan/permit.d.ts +13 -0
- package/lib/cjs/transactionPlan/permit.js +145 -0
- package/lib/cjs/transactionPlan/plan.d.ts +99 -0
- package/lib/cjs/transactionPlan/plan.js +232 -0
- package/lib/cjs/transactionPlan/requirements.d.ts +44 -0
- package/lib/cjs/transactionPlan/requirements.js +79 -0
- package/lib/cjs/transactionPlan/serde.d.ts +33 -0
- package/lib/cjs/transactionPlan/serde.js +205 -0
- package/lib/cjs/transactionPlan/types.d.ts +91 -0
- package/lib/cjs/transactionPlan/types.js +2 -0
- package/lib/cjs/transactionPlan/validateActions.d.ts +8 -0
- package/lib/cjs/transactionPlan/validateActions.js +19 -0
- package/lib/cjs/transactionPlan/validation.d.ts +17 -0
- package/lib/cjs/transactionPlan/validation.js +70 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +211 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +190 -0
- package/lib/cjs/transactionPlan/version.d.ts +2 -0
- package/lib/cjs/transactionPlan/version.js +9 -0
- package/lib/cjs/types/action.d.ts +18 -3
- package/lib/cjs/types/error.d.ts +23 -2
- package/lib/cjs/types/error.js +45 -5
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +2 -4
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/esm/actions/bundles/common.js +39 -32
- package/lib/esm/actions/midnight/cancelAndMake.js +25 -103
- package/lib/esm/actions/midnight/index.d.ts +2 -0
- package/lib/esm/actions/midnight/index.js +2 -0
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +5 -6
- package/lib/esm/actions/midnight/supplyCollateralBatch.d.ts +39 -0
- package/lib/esm/actions/midnight/supplyCollateralBatch.js +87 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +33 -34
- package/lib/esm/actions/midnight/takeBorrow.js +25 -24
- package/lib/esm/actions/midnight/takeLend.js +25 -24
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
- package/lib/esm/actions/midnight/takeWithdraw.js +25 -24
- package/lib/esm/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
- package/lib/esm/actions/midnight/withdrawCollateralBatch.js +100 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
- package/lib/esm/actions/vaultV1/migrateToV2.js +8 -8
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/index.d.ts +5 -5
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +77 -3
- package/lib/esm/entities/midnight/midnight.js +176 -2
- package/lib/esm/entities/midnight/types.d.ts +16 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +14 -48
- package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/esm/entities/requirements/getErc2612Permit.js +57 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +10 -35
- package/lib/esm/entities/vaultV2/vaultV2.js +1 -33
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +1 -3
- package/lib/esm/helpers/signAndVerifyTypedData.d.ts +1 -0
- package/lib/esm/helpers/signAndVerifyTypedData.js +13 -6
- package/lib/esm/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
- package/lib/esm/helpers/validateMidnightCancelAndMakePublication.js +124 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +15 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +25 -0
- package/lib/esm/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
- package/lib/esm/helpers/validateMidnightCollateralSupplies.js +24 -0
- package/lib/esm/helpers/validatePositiveInput.d.ts +10 -0
- package/lib/esm/helpers/validatePositiveInput.js +14 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/midnight/errors.d.ts +1 -2
- package/lib/esm/midnight/errors.js +1 -2
- package/lib/esm/transactionPlan/approval.d.ts +2 -0
- package/lib/esm/transactionPlan/approval.js +13 -0
- package/lib/esm/transactionPlan/error.d.ts +145 -0
- package/lib/esm/transactionPlan/error.js +175 -0
- package/lib/esm/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/esm/transactionPlan/getTokenPermit.js +15 -0
- package/lib/esm/transactionPlan/index.d.ts +9 -0
- package/lib/esm/transactionPlan/index.js +5 -0
- package/lib/esm/transactionPlan/intent.d.ts +51 -0
- package/lib/esm/transactionPlan/intent.js +51 -0
- package/lib/esm/transactionPlan/permit.d.ts +13 -0
- package/lib/esm/transactionPlan/permit.js +140 -0
- package/lib/esm/transactionPlan/plan.d.ts +99 -0
- package/lib/esm/transactionPlan/plan.js +226 -0
- package/lib/esm/transactionPlan/requirements.d.ts +44 -0
- package/lib/esm/transactionPlan/requirements.js +74 -0
- package/lib/esm/transactionPlan/serde.d.ts +33 -0
- package/lib/esm/transactionPlan/serde.js +201 -0
- package/lib/esm/transactionPlan/types.d.ts +91 -0
- package/lib/esm/transactionPlan/types.js +1 -0
- package/lib/esm/transactionPlan/validateActions.d.ts +8 -0
- package/lib/esm/transactionPlan/validateActions.js +16 -0
- package/lib/esm/transactionPlan/validation.d.ts +17 -0
- package/lib/esm/transactionPlan/validation.js +64 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.js +206 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +185 -0
- package/lib/esm/transactionPlan/version.d.ts +2 -0
- package/lib/esm/transactionPlan/version.js +6 -0
- package/lib/esm/types/action.d.ts +18 -3
- package/lib/esm/types/error.d.ts +23 -2
- package/lib/esm/types/error.js +40 -3
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +8 -7
|
@@ -15,6 +15,7 @@ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldat
|
|
|
15
15
|
const index_js_3 = require("../../helpers/index.js");
|
|
16
16
|
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
17
17
|
const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
|
|
18
|
+
const validateMidnightCollateralBatch_js_1 = require("../../helpers/validateMidnightCollateralBatch.js");
|
|
18
19
|
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
19
20
|
const validateMidnightPublication_js_1 = require("../../helpers/validateMidnightPublication.js");
|
|
20
21
|
const validateMidnightTakeFeasibility_js_1 = require("../../helpers/validateMidnightTakeFeasibility.js");
|
|
@@ -1164,6 +1165,106 @@ class MorphoMidnight {
|
|
|
1164
1165
|
}
|
|
1165
1166
|
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1166
1167
|
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Withdraws an ordered collateral batch atomically from Midnight without bundle authorization or token pulls.
|
|
1170
|
+
*
|
|
1171
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
1172
|
+
* Duplicates are rejected, unlike bundle flows which sum them.
|
|
1173
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
1174
|
+
*
|
|
1175
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
1176
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
1177
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
1178
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
1179
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
1180
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
1181
|
+
* @param params.collateralWithdrawals - Nonempty ordered withdrawals, one per index.
|
|
1182
|
+
* maxUint256 resolves that index's full snapshot balance.
|
|
1183
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
1184
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
1185
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1186
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
1187
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
1188
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
1189
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
1190
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
1191
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
1192
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
1193
|
+
* @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
|
|
1194
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
1195
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
1196
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
1197
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
1198
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
1199
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
1200
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
1201
|
+
* @example
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* const output = midnight.withdrawCollateralBatch({
|
|
1204
|
+
* accountAddress: user,
|
|
1205
|
+
* marketData,
|
|
1206
|
+
* positionData,
|
|
1207
|
+
* collateralPrices,
|
|
1208
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: 1_000n }],
|
|
1209
|
+
* });
|
|
1210
|
+
* const tx = output.buildTx();
|
|
1211
|
+
* ```
|
|
1212
|
+
*/
|
|
1213
|
+
withdrawCollateralBatch(params) {
|
|
1214
|
+
(0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
1215
|
+
// Reject invalid owners before preparing the direct call.
|
|
1216
|
+
(0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
|
|
1217
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1218
|
+
validatePositionBinding(params);
|
|
1219
|
+
// Direct batches reject duplicates instead of summing them.
|
|
1220
|
+
(0, validateMidnightCollateralBatch_js_1.validateMidnightCollateralBatch)(params.collateralWithdrawals, new index_js_4.EmptyMidnightCollateralWithdrawalsError());
|
|
1221
|
+
const collateralWithdrawals = params.collateralWithdrawals.map(({ collateralIndex, assets }) => {
|
|
1222
|
+
// Reject unconfigured indexes before reading the position balance.
|
|
1223
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.marketData.params, collateralIndex);
|
|
1224
|
+
return {
|
|
1225
|
+
collateralIndex,
|
|
1226
|
+
assets: assets === viem_1.maxUint256
|
|
1227
|
+
? (params.positionData.collateral[Number(collateralIndex)] ?? 0n)
|
|
1228
|
+
: assets,
|
|
1229
|
+
};
|
|
1230
|
+
});
|
|
1231
|
+
const tx = (0, index_js_1.midnightWithdrawCollateralBatch)({
|
|
1232
|
+
chainId: this.chainId,
|
|
1233
|
+
market: params.marketData.params,
|
|
1234
|
+
collateralWithdrawals,
|
|
1235
|
+
onBehalf: params.accountAddress,
|
|
1236
|
+
receiver: params.receiver ?? params.accountAddress,
|
|
1237
|
+
metadata: this.client.options.metadata,
|
|
1238
|
+
});
|
|
1239
|
+
const collateral = Array.from({ length: params.marketData.params.collateralParams.length }, (_, i) => params.positionData.collateral[i] ?? 0n);
|
|
1240
|
+
for (const { collateralIndex, assets } of collateralWithdrawals) {
|
|
1241
|
+
const index = Number(collateralIndex);
|
|
1242
|
+
const available = collateral[index];
|
|
1243
|
+
if (assets > available) {
|
|
1244
|
+
throw new index_js_4.WithdrawExceedsCollateralError({
|
|
1245
|
+
withdrawAmount: assets,
|
|
1246
|
+
available,
|
|
1247
|
+
market: params.marketData.id,
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
collateral[index] = available - assets;
|
|
1251
|
+
}
|
|
1252
|
+
if (params.positionData.debt > 0n) {
|
|
1253
|
+
const maxSafeDebt = (0, index_js_3.getMidnightMaxSafeDebt)({
|
|
1254
|
+
market: params.marketData,
|
|
1255
|
+
collateral,
|
|
1256
|
+
collateralPrices: params.collateralPrices,
|
|
1257
|
+
});
|
|
1258
|
+
if (params.positionData.debt > maxSafeDebt) {
|
|
1259
|
+
throw new index_js_4.WithdrawMakesPositionUnhealthyError({
|
|
1260
|
+
withdrawAmount: collateralWithdrawals.reduce((sum, entry) => sum + entry.assets, 0n),
|
|
1261
|
+
borrowAssets: params.positionData.debt,
|
|
1262
|
+
maxSafeBorrow: maxSafeDebt,
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1267
|
+
}
|
|
1167
1268
|
/**
|
|
1168
1269
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
1169
1270
|
*
|
|
@@ -1238,6 +1339,79 @@ class MorphoMidnight {
|
|
|
1238
1339
|
}),
|
|
1239
1340
|
};
|
|
1240
1341
|
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Prepares ordered direct collateral supplies in one Midnight multicall.
|
|
1344
|
+
* Pass one entry per index: duplicates are rejected, unlike bundle flows which sum them.
|
|
1345
|
+
*
|
|
1346
|
+
* @param params.accountAddress - Sender and collateral beneficiary.
|
|
1347
|
+
* @param params.marketData - Snapshot of the target market.
|
|
1348
|
+
* @param params.collateralSupplies - Ordered supplies with optional reserved allowance amounts.
|
|
1349
|
+
* @returns A lazy approval resolver and synchronous stateless transaction builder.
|
|
1350
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1351
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
1352
|
+
* @throws {MidnightCalldataEncodingError} when the account is malformed.
|
|
1353
|
+
* @throws {NegativeInputError} when reserved assets are negative.
|
|
1354
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
1355
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
1356
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
1357
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
1358
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
1359
|
+
* @throws {MidnightZeroOnBehalfError} when requirements or construction uses the zero account.
|
|
1360
|
+
* @example
|
|
1361
|
+
* ```ts
|
|
1362
|
+
* const output = midnight.supplyCollateralBatch({
|
|
1363
|
+
* accountAddress: user, marketData,
|
|
1364
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
1365
|
+
* });
|
|
1366
|
+
* const tx = output.buildTx();
|
|
1367
|
+
* ```
|
|
1368
|
+
*/
|
|
1369
|
+
supplyCollateralBatch(params) {
|
|
1370
|
+
(0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
1371
|
+
// Reject malformed accounts before resolving token requirements.
|
|
1372
|
+
(0, validateMidnightAccount_js_1.validateMidnightAddress)("accountAddress", params.accountAddress);
|
|
1373
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1374
|
+
// Reject empty and duplicate-index batches before resolving allowance thresholds.
|
|
1375
|
+
(0, validateMidnightCollateralBatch_js_1.validateMidnightCollateralBatch)(params.collateralSupplies, new index_js_4.EmptyMidnightCollateralSuppliesError());
|
|
1376
|
+
for (const supply of params.collateralSupplies) {
|
|
1377
|
+
assertPositiveAmount("assets", supply.assets);
|
|
1378
|
+
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)("assets", supply.assets);
|
|
1379
|
+
assertNonNegativeAmount("reservedCollateralAssets", supply.reservedCollateralAssets ?? 0n);
|
|
1380
|
+
// Fail eagerly for collateral absent from the supplied snapshot.
|
|
1381
|
+
params.marketData.getCollateralByIndex(supply.collateralIndex);
|
|
1382
|
+
}
|
|
1383
|
+
const midnight = (0, morpho_ts_1.getChainAddress)(this.chainId, "midnight");
|
|
1384
|
+
return {
|
|
1385
|
+
getRequirements: async (_requirementsParams) => {
|
|
1386
|
+
if ((0, viem_1.isAddressEqual)(params.accountAddress, viem_1.zeroAddress)) {
|
|
1387
|
+
throw new index_js_4.MidnightZeroOnBehalfError("onBehalf");
|
|
1388
|
+
}
|
|
1389
|
+
const amounts = new Map();
|
|
1390
|
+
for (const supply of params.collateralSupplies) {
|
|
1391
|
+
const token = params.marketData.getCollateralByIndex(supply.collateralIndex).token;
|
|
1392
|
+
amounts.set(token, (amounts.get(token) ?? 0n) +
|
|
1393
|
+
supply.assets +
|
|
1394
|
+
(supply.reservedCollateralAssets ?? 0n));
|
|
1395
|
+
}
|
|
1396
|
+
const requirements = await Promise.all([...amounts].map(([token, amount]) => (0, index_js_2.getMidnightApprovalRequirements)({
|
|
1397
|
+
viemClient: this.client.viemClient,
|
|
1398
|
+
chainId: this.chainId,
|
|
1399
|
+
token,
|
|
1400
|
+
owner: params.accountAddress,
|
|
1401
|
+
spender: midnight,
|
|
1402
|
+
amount,
|
|
1403
|
+
})));
|
|
1404
|
+
return requirements.flat();
|
|
1405
|
+
},
|
|
1406
|
+
buildTx: () => (0, index_js_1.midnightSupplyCollateralBatch)({
|
|
1407
|
+
chainId: this.chainId,
|
|
1408
|
+
market: params.marketData.params,
|
|
1409
|
+
collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
1410
|
+
onBehalf: params.accountAddress,
|
|
1411
|
+
metadata: this.client.options.metadata,
|
|
1412
|
+
}),
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1241
1415
|
/**
|
|
1242
1416
|
* Prepares an atomic lend-offer publication or repost through `MidnightBundlesV2`.
|
|
1243
1417
|
*
|
|
@@ -235,6 +235,15 @@ export interface SupplyCollateralParams extends MarketActionParams {
|
|
|
235
235
|
readonly reservedCollateralAssets?: bigint;
|
|
236
236
|
readonly collateralIndex?: bigint;
|
|
237
237
|
}
|
|
238
|
+
/** Parameters for an atomic direct Midnight collateral supply batch. */
|
|
239
|
+
export interface SupplyCollateralBatchParams extends MarketActionParams {
|
|
240
|
+
readonly collateralSupplies: readonly {
|
|
241
|
+
readonly collateralIndex: bigint;
|
|
242
|
+
readonly assets: bigint;
|
|
243
|
+
/** Additional assets included only in the allowance threshold. */
|
|
244
|
+
readonly reservedCollateralAssets?: bigint;
|
|
245
|
+
}[];
|
|
246
|
+
}
|
|
238
247
|
/** Parameters for a direct Midnight collateral withdrawal. */
|
|
239
248
|
export interface WithdrawCollateralParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
240
249
|
readonly collateralIndex?: bigint;
|
|
@@ -243,6 +252,13 @@ export interface WithdrawCollateralParams extends MarketActionParams, MidnightPo
|
|
|
243
252
|
/** Defaults to accountAddress. */
|
|
244
253
|
readonly receiver?: Address;
|
|
245
254
|
}
|
|
255
|
+
/** Parameters for an atomic direct Midnight collateral withdrawal batch. */
|
|
256
|
+
export interface WithdrawCollateralBatchParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
257
|
+
/** Ordered unique indexes; maxUint256 resolves that index's whole snapshot balance. */
|
|
258
|
+
readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
|
|
259
|
+
/** Defaults to accountAddress for every withdrawal. */
|
|
260
|
+
readonly receiver?: Address;
|
|
261
|
+
}
|
|
246
262
|
/** Parameters for the Midnight redeem flow. */
|
|
247
263
|
export interface RedeemParams {
|
|
248
264
|
readonly accountAddress: Address;
|
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getBundlesTokenRequirements = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
-
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
6
|
const viem_1 = require("viem");
|
|
8
7
|
const actions_1 = require("viem/actions");
|
|
9
8
|
const index_js_1 = require("../../actions/bundles/index.js");
|
|
10
|
-
const encodeErc20Permit_js_1 = require("../../actions/requirements/encode/encodeErc20Permit.js");
|
|
11
9
|
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
12
10
|
const index_js_2 = require("../../helpers/index.js");
|
|
13
11
|
const index_js_3 = require("../../types/index.js");
|
|
12
|
+
const getErc2612Permit_js_1 = require("./getErc2612Permit.js");
|
|
14
13
|
/**
|
|
15
14
|
* Resolves direct approval or ERC-2612 prerequisites for a registered fixed bundles contract.
|
|
16
15
|
*
|
|
@@ -129,51 +128,18 @@ const getBundlesTokenRequirements = async (viemClient, params) => {
|
|
|
129
128
|
if (params.supportSignature) {
|
|
130
129
|
const isDai = (0, morpho_ts_1.isDefined)(dai) && (0, viem_1.isAddressEqual)(params.token, dai);
|
|
131
130
|
if ((params.useSimplePermit ?? true) && !isDai) {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return undefined;
|
|
142
|
-
throw error;
|
|
131
|
+
const permit = await (0, getErc2612Permit_js_1.getErc2612Permit)(viemClient, {
|
|
132
|
+
token: params.token,
|
|
133
|
+
owner: params.owner,
|
|
134
|
+
spender: params.spender,
|
|
135
|
+
amount: params.amount,
|
|
136
|
+
chainId: params.chainId,
|
|
137
|
+
deadline: params.deadline,
|
|
138
|
+
now: timestamp,
|
|
139
|
+
supportDeployless: params.supportDeployless,
|
|
143
140
|
});
|
|
144
|
-
if ((0, morpho_ts_1.isDefined)(
|
|
145
|
-
|
|
146
|
-
token: params.token,
|
|
147
|
-
owner: params.owner,
|
|
148
|
-
spender: params.spender,
|
|
149
|
-
amount: params.amount,
|
|
150
|
-
chainId: params.chainId,
|
|
151
|
-
nonce,
|
|
152
|
-
deadline: params.deadline,
|
|
153
|
-
now: timestamp,
|
|
154
|
-
supportDeployless: params.supportDeployless,
|
|
155
|
-
}), blue_sdk_viem_1.MissingPermitDomainNameError, blue_sdk_viem_1.InvalidPermitDomainChainIdError, blue_sdk_viem_1.InvalidPermitDomainVerifyingContractError, blue_sdk_viem_1.UnsupportedPermitDomainExtensionsError);
|
|
156
|
-
if ((0, morpho_ts_1.isDefined)(permit)) {
|
|
157
|
-
const separator = await (0, actions_1.readContract)(viemClient, {
|
|
158
|
-
abi: blue_sdk_viem_1.erc2612Abi,
|
|
159
|
-
address: params.token,
|
|
160
|
-
functionName: "DOMAIN_SEPARATOR",
|
|
161
|
-
}).catch((error) => {
|
|
162
|
-
if ((0, blue_sdk_viem_1.isTransportError)(error))
|
|
163
|
-
throw error;
|
|
164
|
-
if ((0, blue_sdk_viem_1.isContractRevertError)(error) || (0, blue_sdk_viem_1.isUndecodableResultError)(error))
|
|
165
|
-
return undefined;
|
|
166
|
-
throw error;
|
|
167
|
-
});
|
|
168
|
-
const domain = permit.action.typedData.domain ?? {};
|
|
169
|
-
if (separator?.toLowerCase() ===
|
|
170
|
-
(0, viem_1.hashDomain)({
|
|
171
|
-
domain,
|
|
172
|
-
types: { EIP712Domain: (0, viem_1.getTypesForEIP712Domain)({ domain }) },
|
|
173
|
-
}).toLowerCase())
|
|
174
|
-
return [permit];
|
|
175
|
-
}
|
|
176
|
-
}
|
|
141
|
+
if ((0, morpho_ts_1.isDefined)(permit))
|
|
142
|
+
return [permit];
|
|
177
143
|
}
|
|
178
144
|
}
|
|
179
145
|
return (0, index_js_1.resolveBundlesTokenRequirements)({
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Client } from "viem";
|
|
2
|
+
import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
|
|
3
|
+
/**
|
|
4
|
+
* @internal Resolves ERC-2612 eligibility from the nonce, signing domain and onchain separator.
|
|
5
|
+
* Unsupported domains and reverted or undecodable probes return undefined; other errors propagate.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getErc2612Permit(viemClient: Client, params: Omit<Parameters<typeof encodeErc20Permit>[1], "nonce">): Promise<import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined> | undefined>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getErc2612Permit = getErc2612Permit;
|
|
4
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const actions_1 = require("viem/actions");
|
|
8
|
+
const encodeErc20Permit_js_1 = require("../../actions/requirements/encode/encodeErc20Permit.js");
|
|
9
|
+
/**
|
|
10
|
+
* @internal Resolves ERC-2612 eligibility from the nonce, signing domain and onchain separator.
|
|
11
|
+
* Unsupported domains and reverted or undecodable probes return undefined; other errors propagate.
|
|
12
|
+
*/
|
|
13
|
+
async function getErc2612Permit(viemClient, params) {
|
|
14
|
+
const nonce = await (0, actions_1.readContract)(viemClient, {
|
|
15
|
+
abi: blue_sdk_viem_1.erc2612Abi,
|
|
16
|
+
address: params.token,
|
|
17
|
+
functionName: "nonces",
|
|
18
|
+
args: [params.owner],
|
|
19
|
+
}).catch((error) => {
|
|
20
|
+
if ((0, blue_sdk_viem_1.isTransportError)(error))
|
|
21
|
+
throw error;
|
|
22
|
+
if ((0, blue_sdk_viem_1.isContractRevertError)(error) || (0, blue_sdk_viem_1.isUndecodableResultError)(error))
|
|
23
|
+
return undefined;
|
|
24
|
+
throw error;
|
|
25
|
+
});
|
|
26
|
+
if ((0, morpho_ts_1.isDefined)(nonce)) {
|
|
27
|
+
const permit = await (0, morpho_ts_1._try)(() => (0, encodeErc20Permit_js_1.encodeErc20Permit)(viemClient, {
|
|
28
|
+
token: params.token,
|
|
29
|
+
owner: params.owner,
|
|
30
|
+
spender: params.spender,
|
|
31
|
+
amount: params.amount,
|
|
32
|
+
chainId: params.chainId,
|
|
33
|
+
nonce,
|
|
34
|
+
deadline: params.deadline,
|
|
35
|
+
now: params.now,
|
|
36
|
+
supportDeployless: params.supportDeployless,
|
|
37
|
+
}), blue_sdk_viem_1.MissingPermitDomainNameError, blue_sdk_viem_1.InvalidPermitDomainChainIdError, blue_sdk_viem_1.InvalidPermitDomainVerifyingContractError, blue_sdk_viem_1.UnsupportedPermitDomainExtensionsError);
|
|
38
|
+
if ((0, morpho_ts_1.isDefined)(permit)) {
|
|
39
|
+
const separator = await (0, actions_1.readContract)(viemClient, {
|
|
40
|
+
abi: blue_sdk_viem_1.erc2612Abi,
|
|
41
|
+
address: params.token,
|
|
42
|
+
functionName: "DOMAIN_SEPARATOR",
|
|
43
|
+
}).catch((error) => {
|
|
44
|
+
if ((0, blue_sdk_viem_1.isTransportError)(error))
|
|
45
|
+
throw error;
|
|
46
|
+
if ((0, blue_sdk_viem_1.isContractRevertError)(error) || (0, blue_sdk_viem_1.isUndecodableResultError)(error))
|
|
47
|
+
return undefined;
|
|
48
|
+
throw error;
|
|
49
|
+
});
|
|
50
|
+
const domain = permit.action.typedData.domain ?? {};
|
|
51
|
+
if (separator?.toLowerCase() ===
|
|
52
|
+
(0, viem_1.hashDomain)({
|
|
53
|
+
domain,
|
|
54
|
+
types: { EIP712Domain: (0, viem_1.getTypesForEIP712Domain)({ domain }) },
|
|
55
|
+
}).toLowerCase())
|
|
56
|
+
return permit;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Token } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { Address, Client } from "viem";
|
|
3
|
-
import { type
|
|
3
|
+
import { type ERC20ApprovalAction, type PermitRequirementSignature, type Requirement, type Transaction } from "../../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Resolves the vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
|
|
6
6
|
*
|
|
@@ -27,4 +27,4 @@ export declare const getVaultBundlesSharesRequirements: (viemClient: Client, par
|
|
|
27
27
|
readonly deadline: bigint;
|
|
28
28
|
readonly supportSignature: boolean;
|
|
29
29
|
readonly now?: bigint;
|
|
30
|
-
}) => Promise<readonly
|
|
30
|
+
}) => Promise<readonly (Readonly<Transaction<ERC20ApprovalAction>> | Requirement<PermitRequirementSignature>)[]>;
|
|
@@ -500,7 +500,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
500
500
|
readonly referralFeeRecipient?: Address;
|
|
501
501
|
readonly deadline?: bigint;
|
|
502
502
|
}): Readonly<{
|
|
503
|
-
getRequirements: () => Promise<readonly
|
|
503
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../index.js").Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
504
504
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../index.js").Transaction<VaultV1WithdrawAction>>;
|
|
505
505
|
}>;
|
|
506
506
|
/** {@inheritDoc VaultV1Actions.redeem} */
|
|
@@ -511,7 +511,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
511
511
|
readonly referralFeeRecipient?: Address;
|
|
512
512
|
readonly deadline?: bigint;
|
|
513
513
|
}): Readonly<{
|
|
514
|
-
getRequirements: () => Promise<readonly
|
|
514
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../index.js").Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
515
515
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../index.js").Transaction<VaultV1RedeemAction>>;
|
|
516
516
|
}>;
|
|
517
517
|
/** {@inheritDoc VaultV1Actions.inKindRedeem} */
|
|
@@ -532,7 +532,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
532
532
|
readonly referralFeeRecipient?: Address;
|
|
533
533
|
readonly deadline?: bigint;
|
|
534
534
|
} & VaultV1MigrateToV2AmountArgs): Readonly<{
|
|
535
|
-
getRequirements: () => Promise<readonly
|
|
535
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../index.js").Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
536
536
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../index.js").Transaction<VaultV1MigrateToV2Action>>;
|
|
537
537
|
}>;
|
|
538
538
|
}
|
|
@@ -65,9 +65,7 @@ export interface VaultV2Actions {
|
|
|
65
65
|
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
66
66
|
* @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
|
|
67
67
|
* @throws {NativeAmountOnNonWNativeVaultError} when native funding targets a non-wNative vault.
|
|
68
|
-
* @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256
|
|
69
|
-
* liquidity adapter is a Midnight adapter (it cannot receive vault allocations, so no deposit
|
|
70
|
-
* fits and `max` is `0n`).
|
|
68
|
+
* @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256.
|
|
71
69
|
* @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
|
|
72
70
|
* @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
|
|
73
71
|
* @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
|
|
@@ -149,9 +147,7 @@ export interface VaultV2Actions {
|
|
|
149
147
|
* @throws {NonPositiveInputError} when `amount` or the computed required share amount is not positive.
|
|
150
148
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
151
149
|
* at any `getRequirements()` call.
|
|
152
|
-
* @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation
|
|
153
|
-
* or when the vault's liquidity adapter is a Midnight adapter and `amount` exceeds the vault's
|
|
154
|
-
* idle assets.
|
|
150
|
+
* @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation.
|
|
155
151
|
* @throws {NegativeInputError} when `referralFeePct` or `slippageTolerance` is negative.
|
|
156
152
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
|
|
157
153
|
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
|
|
@@ -217,24 +213,7 @@ export interface VaultV2Actions {
|
|
|
217
213
|
* @throws {NonPositiveInputError} when `shares` is not positive.
|
|
218
214
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
219
215
|
* requirement resolution.
|
|
220
|
-
* @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256
|
|
221
|
-
* `getRequirements()` when the vault's liquidity adapter is a Midnight adapter and `shares`
|
|
222
|
-
* are worth more than the vault's idle assets.
|
|
223
|
-
* @throws {UnknownBlueFactory} from `getRequirements()` when the configured chain has no VaultV2 factory.
|
|
224
|
-
* @throws {UnknownBlueOfFactory} from `getRequirements()` when the vault is not a VaultV2 from the
|
|
225
|
-
* configured factory.
|
|
226
|
-
* @throws {UnsupportedBlueVaultV2AdapterError} from `getRequirements()` when the vault or one of its
|
|
227
|
-
* adapters uses an unsupported adapter class.
|
|
228
|
-
* @throws {UnsupportedBlueMarketIrmError} from `getRequirements()` when the vault's liquidity adapter
|
|
229
|
-
* is a Midnight adapter and accrual projects an underlying market with positive debt that uses an
|
|
230
|
-
* unsupported IRM.
|
|
231
|
-
* @throws {UnknownBlueMarketAllocationError} from `getRequirements()` when the vault's liquidity
|
|
232
|
-
* adapter is a Midnight adapter and accrual meets a nested Vault V1 withdraw queue market without
|
|
233
|
-
* allocation data.
|
|
234
|
-
* @throws {InvalidMidnightPositionLossFactorError} from `getRequirements()` when the vault's liquidity
|
|
235
|
-
* adapter is a Midnight adapter and accrual meets a market loss factor below a position's.
|
|
236
|
-
* @throws {InvalidMidnightPositionAccrualStateError} from `getRequirements()` when the vault's
|
|
237
|
-
* liquidity adapter is a Midnight adapter and accrual meets invalid Midnight position inputs.
|
|
216
|
+
* @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
|
|
238
217
|
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
239
218
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
240
219
|
* @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
|
|
@@ -242,7 +221,7 @@ export interface VaultV2Actions {
|
|
|
242
221
|
* @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
|
|
243
222
|
* @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
|
|
244
223
|
* contract has no code at its registered address.
|
|
245
|
-
* @throws {viem.BaseError} from `getRequirements()` when
|
|
224
|
+
* @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, `eth_chainId`, or
|
|
246
225
|
* `eth_getCode` read fails.
|
|
247
226
|
* @throws {UnknownTokenError} from `getRequirements()` when the share token fetched by
|
|
248
227
|
* `getData()` has unusable decimals.
|
|
@@ -320,11 +299,9 @@ export interface VaultV2Actions {
|
|
|
320
299
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
|
|
321
300
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
322
301
|
* requirement resolution.
|
|
323
|
-
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter
|
|
324
|
-
* a vault that mixes Blue and Midnight adapters.
|
|
302
|
+
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter.
|
|
325
303
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
326
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
327
|
-
* including a Midnight adapter.
|
|
304
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
|
|
328
305
|
* @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
|
|
329
306
|
* @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
|
|
330
307
|
* @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
|
|
@@ -449,11 +426,9 @@ export interface VaultV2Actions {
|
|
|
449
426
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
450
427
|
* requirement resolution.
|
|
451
428
|
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
452
|
-
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter
|
|
453
|
-
* including a vault that mixes Blue and Midnight adapters.
|
|
429
|
+
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter.
|
|
454
430
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
455
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
456
|
-
* including a Midnight adapter.
|
|
431
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
|
|
457
432
|
* @throws {VaultV2UnsupportedLiquidityAdapterError} when the vault routes liquidity through
|
|
458
433
|
* another adapter.
|
|
459
434
|
* @throws {VaultV2UndecodableLiquidityDataError} when the vault's `liquidityData` does not decode
|
|
@@ -599,7 +574,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
599
574
|
readonly referralFeeRecipient?: Address;
|
|
600
575
|
readonly deadline?: bigint;
|
|
601
576
|
}): Readonly<{
|
|
602
|
-
getRequirements: () => Promise<readonly
|
|
577
|
+
getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
603
578
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
604
579
|
}>;
|
|
605
580
|
/** {@inheritDoc VaultV2Actions.redeem} */
|
|
@@ -610,7 +585,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
610
585
|
readonly referralFeeRecipient?: Address;
|
|
611
586
|
readonly deadline?: bigint;
|
|
612
587
|
}): Readonly<{
|
|
613
|
-
getRequirements: () => Promise<readonly
|
|
588
|
+
getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
614
589
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2RedeemAction>>;
|
|
615
590
|
}>;
|
|
616
591
|
/** {@inheritDoc VaultV2Actions.inKindRedeem} */
|
|
@@ -18,11 +18,6 @@ const index_js_4 = require("../requirements/index.js");
|
|
|
18
18
|
// Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
|
|
19
19
|
// accrual model stays well-defined inside it.
|
|
20
20
|
const VAULT_V2_FEE_PROJECTION_HORIZON = morpho_ts_1.Time.s.from.y(1n);
|
|
21
|
-
/**
|
|
22
|
-
* Midnight adapters revert vault-initiated `allocate` and `deallocate` (`SelfAllocationOnly`), so a
|
|
23
|
-
* vault routing liquidity through one accepts no deposit and serves exits from idle assets only.
|
|
24
|
-
*/
|
|
25
|
-
const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
|
|
26
21
|
/** Binds a viem client to a Vault V2 vault's action builders. */
|
|
27
22
|
class MorphoVaultV2 {
|
|
28
23
|
client;
|
|
@@ -81,13 +76,6 @@ class MorphoVaultV2 {
|
|
|
81
76
|
// The native path must target the chain's registered wrapped-native asset.
|
|
82
77
|
(0, validate_js_1.validateNativeVaultAsset)(this.chainId, vaultAsset);
|
|
83
78
|
}
|
|
84
|
-
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
85
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
86
|
-
field: funding.value > 0n ? "nativeAmount" : "amount",
|
|
87
|
-
value: funding.assets,
|
|
88
|
-
max: 0n,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
79
|
const referralFeeAssets = (0, common_js_1.getBundlesReferralFeeAssets)(funding.assets, common.referralFeePct);
|
|
92
80
|
const maxSharePrice = (0, index_js_2.computeVaultMaxSharePrice)({
|
|
93
81
|
vaultData,
|
|
@@ -168,14 +156,6 @@ class MorphoVaultV2 {
|
|
|
168
156
|
throw new index_js_3.NonPositiveInputError("amount", amount);
|
|
169
157
|
// Reject values outside the ABI range before resolving any requirements.
|
|
170
158
|
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
171
|
-
if (isMidnightLiquidityAdapter(vaultData) &&
|
|
172
|
-
amount > vaultData.assetBalance) {
|
|
173
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
174
|
-
field: "amount",
|
|
175
|
-
value: amount,
|
|
176
|
-
max: vaultData.assetBalance,
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
159
|
const createdAt = morpho_ts_1.Time.timestamp();
|
|
180
160
|
const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
|
|
181
161
|
if (deadline <= createdAt)
|
|
@@ -256,20 +236,8 @@ class MorphoVaultV2 {
|
|
|
256
236
|
if (deadline <= now)
|
|
257
237
|
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
258
238
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
259
|
-
const vaultData = await this.getData();
|
|
260
|
-
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
261
|
-
// Onchain redeem accrues interest before converting shares to assets.
|
|
262
|
-
const { vault: accrued } = vaultData.accrueInterest(now);
|
|
263
|
-
if (accrued.toAssets(shares) > accrued.assetBalance) {
|
|
264
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
265
|
-
field: "shares",
|
|
266
|
-
value: shares,
|
|
267
|
-
max: accrued.toShares(accrued.assetBalance),
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
239
|
return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
|
|
272
|
-
vaultData,
|
|
240
|
+
vaultData: await this.getData(),
|
|
273
241
|
version: "vaultV2",
|
|
274
242
|
owner: userAddress,
|
|
275
243
|
chainId: this.chainId,
|
|
@@ -56,9 +56,8 @@ export interface InputVaultV2BlueReallocationData {
|
|
|
56
56
|
/**
|
|
57
57
|
* Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
|
|
58
58
|
*
|
|
59
|
-
* Constructor inputs are cloned
|
|
60
|
-
*
|
|
61
|
-
* reallocation returns a new instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
59
|
+
* Constructor inputs are cloned. Every simulated reallocation returns a new
|
|
60
|
+
* instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
62
61
|
* denominator outside the durable snapshot while applying reallocations in
|
|
63
62
|
* contract order. Penalty donations update simulated vault accounting but are
|
|
64
63
|
* excluded from same-plan idle liquidity so round-up dust cannot create
|
|
@@ -132,8 +131,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
|
|
|
132
131
|
/**
|
|
133
132
|
* Clones the complete simulation snapshot.
|
|
134
133
|
*
|
|
135
|
-
* @returns A deep clone of this simulation state.
|
|
136
|
-
* shared by reference and must be treated as read-only.
|
|
134
|
+
* @returns A deep clone of this simulation state.
|
|
137
135
|
* @example
|
|
138
136
|
* ```ts
|
|
139
137
|
* import { markets } from "@morpho-org/morpho-test";
|
|
@@ -90,10 +90,6 @@ const cloneAccrualVault = (vault, markets) => new blue_sdk_1.AccrualVault({
|
|
|
90
90
|
position: clonePosition(position, markets),
|
|
91
91
|
})));
|
|
92
92
|
const cloneAdapter = (adapter, markets) => {
|
|
93
|
-
// BluePublicAllocator never moves Midnight liquidity, and reallocation never mutates a Midnight
|
|
94
|
-
// adapter, so its read-only snapshot is shared as-is.
|
|
95
|
-
if (adapter.type === "VaultV2MidnightAdapter")
|
|
96
|
-
return adapter;
|
|
97
93
|
const base = {
|
|
98
94
|
address: adapter.address,
|
|
99
95
|
parentVault: adapter.parentVault,
|
|
@@ -141,9 +137,8 @@ const cloneVault = (vault, { markets, adapterKeysToClone, }) => {
|
|
|
141
137
|
/**
|
|
142
138
|
* Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
|
|
143
139
|
*
|
|
144
|
-
* Constructor inputs are cloned
|
|
145
|
-
*
|
|
146
|
-
* reallocation returns a new instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
140
|
+
* Constructor inputs are cloned. Every simulated reallocation returns a new
|
|
141
|
+
* instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
147
142
|
* denominator outside the durable snapshot while applying reallocations in
|
|
148
143
|
* contract order. Penalty donations update simulated vault accounting but are
|
|
149
144
|
* excluded from same-plan idle liquidity so round-up dust cannot create
|
|
@@ -287,8 +282,7 @@ class VaultV2BlueReallocationData {
|
|
|
287
282
|
/**
|
|
288
283
|
* Clones the complete simulation snapshot.
|
|
289
284
|
*
|
|
290
|
-
* @returns A deep clone of this simulation state.
|
|
291
|
-
* shared by reference and must be treated as read-only.
|
|
285
|
+
* @returns A deep clone of this simulation state.
|
|
292
286
|
* @example
|
|
293
287
|
* ```ts
|
|
294
288
|
* import { markets } from "@morpho-org/morpho-test";
|