Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove getPenalties getter #180

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 63 additions & 33 deletions src/MinterGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,70 @@ contract MinterGateway is IMinterGateway, ContinuousIndexing, ERC712Extended {
}

/// @inheritdoc IMinterGateway
function getPenaltyForMissedCollateralUpdates(address minter_) external view returns (uint240) {
uint112 penaltyPrincipal_ = _getPenaltyPrincipalForMissedCollateralUpdates(minter_);
function getPenalties(
deluca-mike marked this conversation as resolved.
Show resolved Hide resolved
address minter_
) external view returns (uint240 missedIntervalPenalty_, uint240 undercollateralizedPenalty_) {
uint112 principalOfActiveOwedM_ = principalOfActiveOwedMOf(minter_);

if (principalOfActiveOwedM_ == 0) return (0, 0);

uint32 penaltyRate_ = penaltyRate();

if (penaltyRate_ == 0) return (0, 0);
deluca-mike marked this conversation as resolved.
Show resolved Hide resolved

MinterState storage minterState_ = _minterStates[minter_];

(uint40 missedIntervals_, uint40 missedUntil_) = _getMissedCollateralUpdateParameters(
deluca-mike marked this conversation as resolved.
Show resolved Hide resolved
minterState_.updateTimestamp,
minterState_.penalizedUntilTimestamp,
updateCollateralInterval()
);

unchecked {
// As an edge case precaution, cap the penalty principal to type(uint112).max.
uint112 missedIntervalPenaltyPrincipal_ = UIntMath.bound112(
(uint256(principalOfActiveOwedM_) * missedIntervals_ * penaltyRate_) / ONE
);

principalOfActiveOwedM_ = UIntMath.bound112(
uint256(principalOfActiveOwedM_) + uint256(missedIntervalPenaltyPrincipal_)
);

missedIntervalPenalty_ = _getPresentAmount(missedIntervalPenaltyPrincipal_);
}
deluca-mike marked this conversation as resolved.
Show resolved Hide resolved

uint256 maxAllowedActiveOwedM_ = maxAllowedActiveOwedMOf(minter_);

// If the minter's max allowed active owed M is greater than `type(uint240).max`, then it's definitely greater
// than the max possible active owed M for the minter, which is capped at `type(uint240).max`.
if (maxAllowedActiveOwedM_ >= type(uint240).max) return (missedIntervalPenalty_, 0);

// NOTE: Round the principal down in favor of the protocol since this is a max applied to the minter.
uint112 principalOfMaxAllowedActiveOwedM_ = _getPrincipalAmountRoundedDown(uint240(maxAllowedActiveOwedM_));

// If the minter is not undercollateralized, then no penalty is imposed.
if (principalOfMaxAllowedActiveOwedM_ >= principalOfActiveOwedM_) return (missedIntervalPenalty_, 0);

return (penaltyPrincipal_ == 0) ? 0 : _getPresentAmount(penaltyPrincipal_);
unchecked {
uint112 principalOfExcessOwedM_ = principalOfActiveOwedM_ - principalOfMaxAllowedActiveOwedM_;

// NOTE: `block.timestamp >= missedUntil_` since `_getMissedCollateralUpdateParameters` ensures that
// `missedUntil_` is only up to `block.timestamp`.
//
// NOTE: `block.timestamp - missedUntil_` will never be larger than `updateCollateralInterval_` since this
// is only computed after `_getMissedCollateralUpdateParameters`, which ensures that the
// `missedUntil_` is within one `updateCollateralInterval_` of the `block.timestamp`.
//
// NOTE: `updateCollateralInterval()` never equals 0, so the division is safe.
// Its minimum is capped at `MIN_UPDATE_COLLATERAL_INTERVAL`.
uint112 undercollateralizedPenaltyPrincipal_ = UIntMath.bound112(
(penaltyRate_ *
((principalOfExcessOwedM_ * (uint40(block.timestamp) - missedUntil_)) /
deluca-mike marked this conversation as resolved.
Show resolved Hide resolved
updateCollateralInterval())) / ONE
);

undercollateralizedPenalty_ = _getPresentAmount(undercollateralizedPenaltyPrincipal_);
}
}

/// @inheritdoc IMinterGateway
Expand Down Expand Up @@ -982,36 +1042,6 @@ contract MinterGateway is IMinterGateway, ContinuousIndexing, ERC712Extended {
}
}

/**
* @dev Returns the principal penalization base for a minter's missed collateral updates.
* @param minter_ The address of the minter.
* @return The penalty principal.
*/
function _getPenaltyPrincipalForMissedCollateralUpdates(address minter_) internal view returns (uint112) {
uint112 principalOfActiveOwedM_ = principalOfActiveOwedMOf(minter_);

if (principalOfActiveOwedM_ == 0) return 0;

uint32 penaltyRate_ = penaltyRate();

if (penaltyRate_ == 0) return 0;

MinterState storage minterState_ = _minterStates[minter_];

(uint40 missedIntervals_, ) = _getMissedCollateralUpdateParameters(
minterState_.updateTimestamp,
minterState_.penalizedUntilTimestamp,
updateCollateralInterval()
);

if (missedIntervals_ == 0) return 0;

unchecked {
// As an edge case precaution, cap the penalty principal to type(uint112).max.
return UIntMath.bound112((uint256(principalOfActiveOwedM_) * missedIntervals_ * penaltyRate_) / ONE);
}
}

/**
* @dev Returns the present amount (rounded up) given the principal amount, using the current index.
* All present amounts are rounded up in favor of the protocol, since they are owed.
Expand Down
11 changes: 9 additions & 2 deletions src/interfaces/IMinterGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,15 @@ interface IMinterGateway is IContinuousIndexing, IERC712 {
*/
function getLastSignatureTimestamp(address minter, address validator) external view returns (uint256);

/// @notice The penalty for missed collateral updates. Penalized once per missed interval.
function getPenaltyForMissedCollateralUpdates(address minter) external view returns (uint240);
/**
* @notice Returns the penalties a minter will incur for missed update intervals and/or undercollateralization.
* @param minter The address of the minter.
* @return missedIntervalsPenalty The total penalty for missed collateral update intervals.
* @return undercollateralizedPenalty The total penalty for undercollateralization.
*/
function getPenalties(
address minter
) external view returns (uint240 missedIntervalsPenalty, uint240 undercollateralizedPenalty);

/**
* @notice Returns the EIP-712 digest for updateCollateral method.
Expand Down
Loading
Loading