Skip to main content

MidnightVaultExecutor

Git Source - Generated with forge doc

Inherits: IMidnightVaultExecutor, IRepayCallback, ILiquidateCallback

Title: MidnightVaultExecutor

Facilitates deposit, withdraw and liquidation of ERC-4626 vault shares used as Midnight collateral.

Supports multiple vaults: the vault is derived from market.collateralParams[collateralIndex].

The executor is pass-through, not custody.

Funds must be supplied within the same call; a balance parked across calls is neither usable nor recoverable.

VaultV2 deposits can revert if a liquidity-adapter cap of the vault is reached, blocking otherwise valid deposit-and-add-collateral flows.

Uses the Midnight contract as authorization authority (caller must be onBehalf or authorized by it on Midnight). VAULT SAFETY REQUIREMENTS

List of assumptions on the vault that guarantee this executor behaves as expected:

  • deposit, mint and redeem move exactly the assets/shares they report. The executor pulls previewMint(shares) on the mint path, which equals what mint consumes in the same transaction, so no dust is left behind.
  • It must be resistant to atomic share-price manipulation (donation/sandwich): no per-share-price/slippage bound is enforced by the executor, so deposit/mint/redeem settle at whatever rate the vault reports at execution time.
  • It must not re-enter Midnight nor this executor on deposit, mint, previewMint nor redeem.

Constants

MORPHO_MIDNIGHT

IMidnight public immutable MORPHO_MIDNIGHT

Functions

constructor

constructor(address morphoMidnight) ;

depositAndAddCollateral

Deposits into the market's vault-share collateral and supplies the resulting shares for onBehalf.

Provide either assets (and shares == 0) or shares (and assets == 0), not both.

function depositAndAddCollateral(
Market memory market,
uint256 collateralIndex,
uint256 assets,
uint256 shares,
address onBehalf
) external returns (uint256 depositedShares, uint256 usedAssets);

Parameters

NameTypeDescription
marketMarketThe target market.
collateralIndexuint256The index in market.collateralParams whose token is the ERC-4626 vault deposited into.
assetsuint256The deposit amount in vault assets. Set to 0 if supplying shares.
sharesuint256The target share output. Set to 0 if supplying assets.
onBehalfaddressThe account credited with the collateral on Midnight.

Returns

NameTypeDescription
depositedSharesuint256The vault shares supplied as collateral.
usedAssetsuint256The vault assets consumed by the deposit.

withdrawCollateralAndRedeem

Withdraws vault-share collateral from Midnight and redeems it into vault assets for receiver.

function withdrawCollateralAndRedeem(
Market memory market,
uint256 collateralIndex,
uint256 shares,
address onBehalf,
address receiver
) external returns (uint256 assets);

Parameters

NameTypeDescription
marketMarketThe source market.
collateralIndexuint256The index in market.collateralParams whose token is the ERC-4626 vault redeemed from.
sharesuint256The vault shares to withdraw and redeem.
onBehalfaddressThe account whose collateral is withdrawn.
receiveraddressThe address that receives the redeemed assets.

Returns

NameTypeDescription
assetsuint256The vault assets transferred to receiver.

onRepay

function onRepay(bytes32, Market memory market, uint256 units, address onBehalf, bytes memory data)
external
override
returns (bytes32);

onLiquidate

function onLiquidate(
address liquidator,
bytes32,
Market memory market,
uint256 collateralIndex,
uint256 seizedShares,
uint256 repaidUnits,
address,
address receiver,
bytes memory,
uint256
) external override returns (bytes32);

_withdrawAndRedeem

Shared by withdrawCollateralAndRedeem and onRepay: derives the vault from the market's collateral and checks its asset matches the loan token, withdraws shares of vault-share collateral from onBehalf on Midnight, and redeems them to redeemReceiver.

withdrawCollateralAndRedeem redeems to the external receiver; onRepay redeems to the executor, which then funds the repay.

function _withdrawAndRedeem(
Market memory market,
uint256 collateralIndex,
uint256 shares,
address onBehalf,
address redeemReceiver
) internal returns (uint256 assets);

_fundRepay

function _fundRepay(address loanToken, uint256 redeemed, uint256 repaidUnits, address surplusReceiver) internal;

_checkAuthorized

function _checkAuthorized(address onBehalf) internal view;