Midnight Vault Executor
MidnightVaultExecutor deposits, withdraws, and redeems ERC-4626 vault-share collateral on Morpho Midnight. It lets a position use ERC-4626 vault shares as collateral by handling the conversion between the underlying asset and vault shares: it deposits the underlying to mint shares before supplying them to the market, and redeems shares back to the underlying after withdrawing them.
Functions
depositAndAddCollateral
depositAndAddCollateral(marketId, vault, collateralIndex, assets, onBehalf)
Deposits assets of the underlying asset into the ERC-4626 vault, then supplies the resulting shares as collateral on the Midnight market for onBehalf.
| Argument | Description |
|---|---|
marketId | The Midnight market the collateral is supplied to. |
vault | The ERC-4626 vault that converts the underlying asset into shares. |
collateralIndex | The collateral slot on the market that holds the vault shares. |
assets | The amount of the underlying asset to deposit into the vault. |
onBehalf | The account whose position receives the supplied collateral. |
Flow: pull assets of the underlying → deposit into vault (shares minted to the executor) → supply those shares as collateral on Midnight at collateralIndex for onBehalf.
withdrawCollateralAndRedeem
withdrawCollateralAndRedeem(marketId, vault, collateralIndex, shares, onBehalf, receiver)
Withdraws vault-share collateral from the market and redeems it for the underlying asset, sending the underlying to receiver.
| Argument | Description |
|---|---|
marketId | The Midnight market the collateral is withdrawn from. |
vault | The ERC-4626 vault that converts the shares back into the underlying asset. |
collateralIndex | The collateral slot on the market that holds the vault shares. |
shares | The amount of vault shares to withdraw and redeem. |
onBehalf | The account whose position the collateral is withdrawn from. |
receiver | The account that receives the redeemed underlying asset. |
Flow: withdraw shares of vault-share collateral from Midnight at collateralIndex for onBehalf → redeem those shares from vault → send the underlying asset to receiver.
Callback support
MidnightVaultExecutor implements onRepay and onLiquidate so it can participate in composable repay and liquidation flows. These callbacks let the executor's deposit/redeem conversion run inline when a repay or liquidation moves vault-share collateral, rather than requiring the caller to convert separately.
onLiquidate reverts unless the liquidation's receiver is the executor itself, because the executor redeems the seized shares to its own balance before settling. The supported way to liquidate vault-share collateral is therefore to call Midnight.liquidate directly with both the receiver and the callback set to the executor.
For this reason, a market that uses vault-share collateral routed through MidnightVaultExecutor cannot also set the Delayed Liquidation Gate as its liquidatorGate. The gate forwards liquidations with itself as the callback, which conflicts with the executor's requirement that the callback and receiver both be the executor, so both call paths revert and the position cannot be liquidated. Choose one or the other when configuring a market.
Gate allowlist requirement
When the vault uses a Vault V2 Allowlist Gate, MidnightVaultExecutor receives vault shares while withdrawing, repaying, and redeeming, so it must be allowlisted with canReceiveShares on that gate. A gate that allowlists only Morpho Midnight and the vault-shares callbacks can still open positions, but withdrawCollateralAndRedeem, onRepay, and onLiquidate will revert with CannotReceiveShares because the executor cannot receive the returned shares. The canonical allowlist for a vault-collateral market covers Morpho Midnight, the vault-shares supply and withdraw callbacks, and MidnightVaultExecutor. If borrow renewals are enabled, BorrowMidnightRenewalCallback must also be allowlisted for canReceiveShares, since it receives vault-share collateral while migrating a borrow position between Midnight markets.
Authorization
For onBehalf operations the executor delegates to Midnight's isAuthorized check: the caller must be authorized by onBehalf on Midnight for the operation to proceed. The executor does not maintain its own authorization records.
Because authorization is enforced by Midnight's isAuthorized, granting the executor authority is done through Midnight, not through the executor. An account that has not authorized the caller on Midnight cannot have its collateral supplied or withdrawn through this contract.
Trust assumptions
The vault is trusted. depositAndAddCollateral and withdrawCollateralAndRedeem perform no slippage protection on the ERC-4626 deposit or redeem: the executor does not enforce a minimum number of shares minted on deposit or a minimum amount of underlying received on redeem. A malicious or misconfigured vault, or adverse share-price movement between submission and execution, can change the amount of shares supplied or the amount of underlying received. Only route through vaults whose behavior and share pricing are trusted.