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

[SC-406] Optional Max DSR #18

Merged
merged 5 commits into from
Apr 24, 2024
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
3 changes: 0 additions & 3 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ contract Deploy is Script {

address internal constant MCD_POT = 0x197E90f9FAD81970bA7976f33CbD77088E5D7cf7;

uint256 internal constant DSR_1000APY = 1.000000076036763190083298291e27;

function deploy(string memory remoteRpcUrl) internal {
address deployer = msg.sender;
address admin = vm.envOr("ORACLE_ADMIN", address(0));
Expand All @@ -49,7 +47,6 @@ contract Deploy is Script {

// Configure
oracle.grantRole(oracle.DATA_PROVIDER_ROLE(), receiver);
oracle.setMaxDSR(DSR_1000APY);
if (admin != address(0)) {
oracle.grantRole(oracle.DEFAULT_ADMIN_ROLE(), admin);
}
Expand Down
22 changes: 12 additions & 10 deletions src/DSRAuthOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ contract DSRAuthOracle is AccessControl, DSROracleBase, IDSRAuthOracle {
constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setRoleAdmin(DATA_PROVIDER_ROLE, DEFAULT_ADMIN_ROLE);

maxDSR = RAY;
}

function setMaxDSR(uint256 _maxDSR) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(_maxDSR >= RAY, 'DSRAuthOracle/invalid-max-dsr');
require(_maxDSR >= RAY || _maxDSR == 0, 'DSRAuthOracle/invalid-max-dsr');

maxDSR = _maxDSR;
emit SetMaxDSR(_maxDSR);
Expand All @@ -51,17 +49,21 @@ contract DSRAuthOracle is AccessControl, DSROracleBase, IDSRAuthOracle {
// Timestamp must be in the past
require(nextData.rho <= block.timestamp, 'DSRAuthOracle/invalid-rho');

// DSR sanity bounds
uint256 _maxDSR = maxDSR;
require(nextData.dsr >= RAY, 'DSRAuthOracle/invalid-dsr');
require(nextData.dsr <= _maxDSR, 'DSRAuthOracle/invalid-dsr');
// DSR lower bound
require(nextData.dsr >= RAY, 'DSRAuthOracle/invalid-dsr');

// `chi` must be non-decreasing
require(nextData.chi >= previousData.chi, 'DSRAuthOracle/invalid-chi');

// Accumulation cannot be larger than the time elapsed at the max dsr
uint256 chiMax = _rpow(_maxDSR, nextData.rho - previousData.rho) * previousData.chi / RAY;
require(nextData.chi <= chiMax, 'DSRAuthOracle/invalid-chi');
// Extra checks if `maxDSR` is set
uint256 _maxDSR = maxDSR;
if (_maxDSR != 0) {
require(nextData.dsr <= _maxDSR, 'DSRAuthOracle/invalid-dsr');

// Accumulation cannot be larger than the time elapsed at the max dsr
uint256 chiMax = _rpow(_maxDSR, nextData.rho - previousData.rho) * previousData.chi / RAY;
require(nextData.chi <= chiMax, 'DSRAuthOracle/invalid-chi');
}

_setPotData(nextData);
}
Expand Down
55 changes: 51 additions & 4 deletions test/DSRAuthOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DSRAuthOracle, IDSROracle } from "../src/DSRAuthOracle.sol";

contract DSRAuthOracleTest is Test {

event SetPotData(IDSROracle.PotData nextData);
event SetMaxDSR(uint256 maxDSR);

uint256 constant FIVE_PCT_APY_DSR = 1.000000001547125957863212448e27;
uint256 constant ONE_HUNDRED_PCT_APY_DSR = 1.00000002197955315123915302e27;
Expand All @@ -23,7 +23,6 @@ contract DSRAuthOracleTest is Test {

// Feed initial data and set limits
oracle.grantRole(oracle.DATA_PROVIDER_ROLE(), address(this));
oracle.setMaxDSR(ONE_HUNDRED_PCT_APY_DSR);
oracle.setPotData(IDSROracle.PotData({
dsr: uint96(FIVE_PCT_APY_DSR),
chi: uint120(1e27),
Expand All @@ -33,10 +32,36 @@ contract DSRAuthOracleTest is Test {
skip(1 * (365 days));
}

function test_setMaxDSR_boundary() public {
function test_constructor() public {
assertEq(oracle.maxDSR(), 0);
}

hexonaut marked this conversation as resolved.
Show resolved Hide resolved
function test_setMaxDSR_notAdmin() public {
address randomAddress = makeAddr("randomAddress");

vm.expectRevert(abi.encodeWithSignature("AccessControlUnauthorizedAccount(address,bytes32)", randomAddress, oracle.DEFAULT_ADMIN_ROLE()));
vm.prank(randomAddress);
oracle.setMaxDSR(ONE_HUNDRED_PCT_APY_DSR);
}

function test_setMaxDSR_setToZero() public {
oracle.setMaxDSR(ONE_HUNDRED_PCT_APY_DSR);

assertEq(oracle.maxDSR(), ONE_HUNDRED_PCT_APY_DSR);

vm.expectEmit(address(oracle));
emit SetMaxDSR(0);
oracle.setMaxDSR(0);

assertEq(oracle.maxDSR(), 0);
}

function test_setMaxDSR_ray_boundary() public {
vm.expectRevert("DSRAuthOracle/invalid-max-dsr");
oracle.setMaxDSR(RAY - 1);

vm.expectEmit(address(oracle));
emit SetMaxDSR(RAY);
oracle.setMaxDSR(RAY);
}

Expand Down Expand Up @@ -86,7 +111,9 @@ contract DSRAuthOracleTest is Test {
}));
}

function test_setPotData_dsr_above_100pct_boundary() public {
function test_setPotData_dsr_above_max_boundary() public {
oracle.setMaxDSR(ONE_HUNDRED_PCT_APY_DSR);

vm.expectRevert("DSRAuthOracle/invalid-dsr");
oracle.setPotData(IDSROracle.PotData({
dsr: uint96(ONE_HUNDRED_PCT_APY_DSR + 1),
Expand All @@ -101,6 +128,15 @@ contract DSRAuthOracleTest is Test {
}));
}

function test_setPotData_very_high_dsr_no_max() public {
// Set the DSR to be a very high number (Doubling every second)
oracle.setPotData(IDSROracle.PotData({
dsr: uint96(2e27),
chi: uint120(1.03e27),
rho: uint40(block.timestamp)
}));
}

function test_setPotData_chi_decreasing_boundary() public {
vm.expectRevert("DSRAuthOracle/invalid-chi");
oracle.setPotData(IDSROracle.PotData({
Expand All @@ -117,6 +153,8 @@ contract DSRAuthOracleTest is Test {
}

function test_setPotData_chi_growth_too_fast_boundary() public {
oracle.setMaxDSR(ONE_HUNDRED_PCT_APY_DSR);

uint256 chiMax = _rpow(ONE_HUNDRED_PCT_APY_DSR, 365 days);
assertEq(chiMax, 1.999999999999999999505617035e27); // Max APY is 100% so ~2x return in 1 year is highest

Expand All @@ -134,6 +172,15 @@ contract DSRAuthOracleTest is Test {
}));
}

function test_setPotData_chi_large_growth_no_max_dsr() public {
// A 100,000x return in 1 year is fine with no upper dsr limit
oracle.setPotData(IDSROracle.PotData({
dsr: uint96(FIVE_PCT_APY_DSR),
chi: uint120(100000e27),
rho: uint40(block.timestamp)
}));
}

function _rpow(uint256 x, uint256 n) internal pure returns (uint256 z) {
assembly {
switch x case 0 {switch n case 0 {z := RAY} default {z := 0}}
Expand Down
Loading