Skip to main content

DelayedLiquidationGate

Git Source - Generated with forge doc

Inherits: IDelayedLiquidationGate, ILiquidatorGate

Title: DelayedLiquidationGate

Combined ILiquidatorGate and liquidation router enforcing a grace period before liquidations.

Set as the market's liquidatorGate. Liquidators call liquidate here instead of Midnight directly.

Token flows are handled through Midnight's onLiquidate callback.

Incompatible with markets whose collateral is gated VaultV2 shares routed through MidnightVaultExecutor: both call paths revert and the position becomes unliquidatable.

startGracePeriod is permissionless and callable whenever the position is unhealthy (including mid-transaction); the caller freely picks the priority liquidator, who has exclusive rights for PRIORITY_PERIOD.

The grace timer is not reset if the borrower recovers to healthy; a position that later becomes unhealthy again inherits the old timer and may be liquidatable without a fresh grace period.

Constants

MORPHO_MIDNIGHT

IMidnight public immutable MORPHO_MIDNIGHT

GRACE_PERIOD

uint256 public immutable GRACE_PERIOD

LIQUIDATION_PERIOD

uint256 public immutable LIQUIDATION_PERIOD

PRIORITY_PERIOD

uint256 public immutable PRIORITY_PERIOD

State Variables

gracePeriodInfo

Maps borrower => marketId => grace period info (timestamp and priority liquidator packed in one slot).

mapping(address borrower => mapping(bytes32 marketId => GracePeriodInfo)) public gracePeriodInfo

Functions

constructor

constructor(address morphoMidnight, uint256 _gracePeriod, uint256 _liquidationPeriod, uint256 _priorityPeriod) ;

startGracePeriod

Opens the grace window on an unhealthy position.

Reverts if a window is already open.

function startGracePeriod(bytes32 marketId, address borrower, address _priorityLiquidator) external;

Parameters

NameTypeDescription
marketIdbytes32The market in distress.
borroweraddressThe borrower being liquidated.
_priorityLiquidatoraddressThe address with exclusive liquidation rights during the priority sub-window.

canLiquidate

function canLiquidate(address account) external view returns (bool);

liquidate

Liquidates a position whose grace window has elapsed.

Pre-maturity, reverts if no grace window has been opened, before GRACE_PERIOD elapses, after the window expires, or during the priority sub-window when called by a non-priority address.

function liquidate(
Market calldata market,
uint256 collateralIndex,
uint256 seizedAssets,
uint256 repaidUnits,
address borrower,
bool postMaturityMode,
address receiver,
address callback,
bytes calldata data
) external returns (uint256, uint256);

Parameters

NameTypeDescription
marketMarket
collateralIndexuint256
seizedAssetsuint256
repaidUnitsuint256
borroweraddress
postMaturityModeboolSelects the Midnight liquidation mode: true = post-maturity mode (requires block.timestamp > market.maturity; LIF ramps from 1 to maxLif over TIME_TO_MAX_LIF; RCF disabled), false = normal mode (requires originalDebt > maxDebt; LIF = maxLif; RCF applies). See IMidnight.liquidate.
receiveraddressThe address receiving the seized collateral, forwarded to Midnight.
callbackaddressOptional flash-liquidation callback. Must be zero or the caller; when nonzero, must implement ILiquidateCallback.onLiquidate.
databytesArbitrary data forwarded to the callback's onLiquidate.

onLiquidate

function onLiquidate(
address callerFromMidnight,
bytes32 marketId,
Market memory market,
uint256 collateralIndex,
uint256 seizedAssets,
uint256 repaidUnits,
address borrower,
address receiver,
bytes memory data,
uint256 badDebt
) external returns (bytes32);

_requireLiquidationAllowed

Pre-maturity only: once market.maturity is crossed, gate checks are skipped and liquidation is permissionless by design.

function _requireLiquidationAllowed(Market calldata market, address borrower) internal view;