contract BucketLenderWithRecoveryDelay is BucketLender
{
    // ============ State Variables ============

    // number of seconds after position has closed that must be waited before force-recovering
    uint256 public RECOVERY_DELAY;

    // ============ Constructor ============

    constructor(
        address margin,
        bytes32 positionId,
        address heldToken,
        address owedToken,
        uint32[7] parameters,
        address[] trustedMarginCallers,
        address[] trustedWithdrawers,
        uint256 recoveryDelay
    )
        public
        BucketLender(
            margin,
            positionId,
            heldToken,
            owedToken,
            parameters,
            trustedMarginCallers,
            trustedWithdrawers
        )
    {
        RECOVERY_DELAY = recoveryDelay;
    }

    // ============ Margin-Only State-Changing Functions ============

    // Overrides the function in BucketLender
    function forceRecoverCollateralOnBehalfOf(
        address /* recoverer */,
        bytes32 positionId,
        address recipient
    )
        external
        onlyMargin
        nonReentrant
        onlyPosition(positionId)
        returns (address)
    {
        MarginCommon.Position memory position = MarginHelper.getPosition(DYDX_MARGIN, positionId);
        uint256 positionEnd = uint256(position.startTimestamp).add(position.maxDuration);
        if (position.callTimestamp > 0) {
            uint256 marginCallEnd = uint256(position.callTimestamp).add(position.callTimeLimit);
            positionEnd = Math.min256(positionEnd, marginCallEnd);
        }

        require (
            block.timestamp >= positionEnd.add(RECOVERY_DELAY),
            "BucketLenderWithRecoveryDelay#forceRecoverCollateralOnBehalfOf: Recovery too early"
        );

        return forceRecoverCollateralInternal(recipient);
    }
}
/**
 * @title BucketLenderWithRecoveryDelay
 * @author dYdX
 *
 * Extension of BucketLender that delays the force-recovery time
 */
function forceRecoverCollateralOnBehalfOf(
    address /* recoverer */,
    bytes32 positionId,
    address recipient
)
    external
    onlyMargin
    nonReentrant
    onlyPosition(positionId)
    returns (address)
{
    MarginCommon.Position memory position = MarginHelper.getPosition(DYDX_MARGIN, positionId);
    uint256 positionEnd = uint256(position.startTimestamp).add(position.maxDuration);
    if (position.callTimestamp > 0) {
        uint256 marginCallEnd = uint256(position.callTimestamp).add(position.callTimeLimit);
        positionEnd = Math.min256(positionEnd, marginCallEnd);
    }

    require (
        block.timestamp >= positionEnd.add(RECOVERY_DELAY),
        "BucketLenderWithRecoveryDelay#forceRecoverCollateralOnBehalfOf: Recovery too early"
    );

    return forceRecoverCollateralInternal(recipient);
}

// ============ Margin-Only State-Changing Functions ============
// Overrides the function in BucketLender