Skip to content

Instantly share code, notes, and snippets.

@bayological
Last active July 27, 2023 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayological/5371c8b3d47118ec226a52139390bd80 to your computer and use it in GitHub Desktop.
Save bayological/5371c8b3d47118ec226a52139390bd80 to your computer and use it in GitHub Desktop.
shouldTrigger() implementation from the Median Delta Breaker
/**
* @title Median Delta Breaker
* @notice Breaker contract that will trigger when an updated oracle median rate changes
* more than a configured relative threshold from the previous one. If this
* breaker is triggered for a rate feed it should be set to no trading mode.
*/
contract MedianDeltaBreaker is IBreaker, WithCooldown, WithThreshold, Ownable {
using SafeMath for uint256;
using FixidityLib for FixidityLib.Fraction;
/* ==================== Events ==================== */
event SmoothingFactorSet(address rateFeedId, uint256 smoothingFactor);
/* ==================== State Variables ==================== */
// Address of the Mento SortedOracles contract
ISortedOracles public sortedOracles;
// Default smoothing factor for EMA as a Fixidity value
uint256 public constant DEFAULT_SMOOTHING_FACTOR = 1e24;
// Smoothing factor per rate feed
mapping(address => FixidityLib.Fraction) public smoothingFactors;
// EMA of the median rates per rate feed
mapping(address => uint256) public medianRatesEMA;
// -- Code omitted for brevity -- //
/**
* @notice Check if the current median report rate for a rate feed change, relative
* to the last median report, is greater than the configured threshold.
* If the change is greater than the threshold the breaker will be triggered.
* @param rateFeedID The rate feed to be checked.
* @return triggerBreaker A bool indicating whether or not this breaker
* should be tripped for the rate feed.
*/
function shouldTrigger(address rateFeedID) public returns (bool triggerBreaker) {
(uint256 currentMedian, ) = sortedOracles.medianRate(rateFeedID);
uint256 previousRatesEMA = medianRatesEMA[rateFeedID];
if (previousRatesEMA == 0) {
// Previous recorded EMA will be 0 the first time this rate feed is checked.
medianRatesEMA[rateFeedID] = currentMedian;
return false;
}
FixidityLib.Fraction memory smoothingFactor = FixidityLib.wrap(getSmoothingFactor(rateFeedID));
medianRatesEMA[rateFeedID] = FixidityLib
.wrap(currentMedian)
.multiply(smoothingFactor)
.add(FixidityLib.wrap(previousRatesEMA).multiply(FixidityLib.fixed1().subtract(smoothingFactor)))
.unwrap();
return exceedsThreshold(previousRatesEMA, currentMedian, rateFeedID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment