-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCappedFallbackRateSource.sol
47 lines (38 loc) · 1.44 KB
/
CappedFallbackRateSource.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;
import { IRateSource } from "./interfaces/IRateSource.sol";
/**
* @title CappedFallbackRateSource
* @notice Wraps another rate source, caps the rate and protects against reverts with a fallback value.
*/
contract CappedFallbackRateSource is IRateSource {
IRateSource public immutable source;
uint256 public immutable lowerBound;
uint256 public immutable upperBound;
uint256 public immutable defaultRate;
constructor(
address _source,
uint256 _lowerBound,
uint256 _upperBound,
uint256 _defaultRate
) {
require(_lowerBound <= _upperBound, "CappedFallbackRateSource/invalid-bounds");
require(_defaultRate >= _lowerBound && _defaultRate <= _upperBound, "CappedFallbackRateSource/invalid-default-rate");
source = IRateSource(_source);
lowerBound = _lowerBound;
upperBound = _upperBound;
defaultRate = _defaultRate;
}
function getAPR() external override view returns (uint256) {
try source.getAPR() returns (uint256 rate) {
if (rate < lowerBound) return lowerBound;
else if (rate > upperBound) return upperBound;
return rate;
} catch {
return defaultRate;
}
}
function decimals() external view override returns (uint8) {
return source.decimals();
}
}