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: add protected median reset function #254

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions contracts/oracles/breakers/MedianDeltaBreaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ contract MedianDeltaBreaker is IBreaker, WithCooldown, WithThreshold, Ownable {
/* ==================== Events ==================== */
event SmoothingFactorSet(address rateFeedId, uint256 smoothingFactor);

event MedianRateEMAReset(address rateFeedID);

/* ==================== State Variables ==================== */
// Address of the Mento SortedOracles contract
ISortedOracles public sortedOracles;
Expand Down Expand Up @@ -119,6 +121,17 @@ contract MedianDeltaBreaker is IBreaker, WithCooldown, WithThreshold, Ownable {
emit SmoothingFactorSet(rateFeedID, newSmoothingFactor);
}

/**
* @notice Resets the median rates EMA for a rate feed.
* @param rateFeedID the targeted rate feed.
* @dev Should be called when the breaker is disabled for a rate feed.
*/
function resetMedianRateEMA(address rateFeedID) external onlyOwner {
require(rateFeedID != address(0), "RateFeed address must be set");
medianRatesEMA[rateFeedID] = 0;
emit MedianRateEMAReset(rateFeedID);
}

/* ==================== View Functions ==================== */

/**
Expand Down
2 changes: 1 addition & 1 deletion slither.db.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions test/oracles/breakers/MedianDeltaBreaker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ contract MedianDeltaBreakerTest is BaseTest {
event SortedOraclesUpdated(address newSortedOracles);
event RateChangeThresholdUpdated(address rateFeedID1, uint256 rateChangeThreshold);
event SmoothingFactorSet(address rateFeedId, uint256 newSmoothingFactor);
event MedianRateEMAReset(address rateFeedID);

function setUp() public {
notDeployer = actor("notDeployer");
Expand Down Expand Up @@ -200,6 +201,36 @@ contract MedianDeltaBreakerTest_constructorAndSetters is MedianDeltaBreakerTest
breaker.setSmoothingFactor(rateFeedIDs[0], 1 * 1e24);
}

function test_resetMedianRateEMA_whenCallerIsNotOwner_shouldRevert() public {
changePrank(notDeployer);
vm.expectRevert("Ownable: caller is not the owner");
breaker.resetMedianRateEMA(address(0));
}

function test_resetMedianRateEMA_whenRateFeedIdIsNotSet_shouldRevert() public {
vm.expectRevert("RateFeed address must be set");
breaker.resetMedianRateEMA(address(0));
}

function test_resetMedianRateEMA_whenCallerIsOwner_shouldUpdateAndEmit() public {
// Set median rate
vm.mockCall(address(sortedOracles), abi.encodeWithSelector(sortedOracles.medianRate.selector), abi.encode(1, 1));

// Update ema for rate feed
breaker.shouldTrigger(rateFeedIDs[0]);

// Verify median is not zero before reset
uint256 medianEMABefore = breaker.medianRatesEMA(rateFeedIDs[0]);
assertTrue(medianEMABefore > 0);

vm.expectEmit(true, true, true, true);
emit MedianRateEMAReset(rateFeedIDs[0]);
breaker.resetMedianRateEMA(rateFeedIDs[0]);

uint256 medianEMAAfter = breaker.medianRatesEMA(rateFeedIDs[0]);
assertEq(medianEMAAfter, 0);
}

/* ---------- Getters ---------- */
function test_getCooldown_withDefault_shouldReturnDefaultCooldown() public {
assertEq(breaker.getCooldown(rateFeedID1), defaultCooldownTime);
Expand Down
Loading