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​

The Morpho Midnight protocol this gate is bound to.

IMidnight public immutable MORPHO_MIDNIGHT;

Returns

NameTypeDescription
<none>address

GRACE_PERIOD​

The seconds between startGracePeriod and the earliest liquidation.

uint256 public immutable GRACE_PERIOD;

Returns

NameTypeDescription
<none>uint256

LIQUIDATION_PERIOD​

The seconds during which liquidation is permitted, starting GRACE_PERIOD after the window opened. After this period elapses without liquidation, the window expires.

uint256 public immutable LIQUIDATION_PERIOD;

Returns

NameTypeDescription
<none>uint256

PRIORITY_PERIOD​

The seconds at the start of the liquidation window during which only the priorityLiquidator recorded at startGracePeriod time may liquidate. Once elapsed, or if it is address(0), any address may liquidate.

uint256 public immutable PRIORITY_PERIOD;

Returns

NameTypeDescription
<none>uint256

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);

Parameters

NameTypeDescription
morphoMidnightaddress
_gracePerioduint256
_liquidationPerioduint256
_priorityPerioduint256

startGracePeriod​

Opens the grace window on an unhealthy position.

Reverts if a window is already open.

Reverts if the position is still healthy at call time.

Reverts if the position's liquidation is locked on Midnight.

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);

Parameters

NameTypeDescription
accountaddress

Returns

NameTypeDescription
<none>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.

Past market.maturity, the window checks are skipped and liquidation via this gate is permissionless.

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.

Returns

NameTypeDescription
<none>uint256
<none>uint256

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);

Parameters

NameTypeDescription
callerFromMidnightaddress
marketIdbytes32
marketMarket
collateralIndexuint256
seizedAssetsuint256
repaidUnitsuint256
borroweraddress
receiveraddress
databytes
badDebtuint256

Returns

NameTypeDescription
<none>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;

Parameters

NameTypeDescription
marketMarket
borroweraddress