Skip to content

Commit

Permalink
fix: bound rate
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Feb 28, 2024
1 parent 23259f8 commit a0fa8f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/fixed-rate-irm/FixedRateIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ string constant RATE_NOT_SET = "rate not set";
string constant RATE_SET = "rate set";
/// @dev Thrown when trying to set the rate at zero.
string constant RATE_ZERO = "rate zero";
/// @dev Thrown when trying to set a rate that is too high.
string constant RATE_TOO_HIGH = "rate too high";

/// @title FixedRateIrm
/// @author Morpho Labs
Expand All @@ -27,6 +29,11 @@ contract FixedRateIrm is IFixedRateIrm {
/// @notice Emitted when a borrow rate is set.
event SetBorrowRate(Id indexed id, uint256 newBorrowRate);

/* CONSTANTS */

/// @notice Max settable borrow rate (800%).
uint256 public constant MAX_BORROW_RATE = 8.0 ether / uint256(365 days);

/* STORAGE */

/// @notice Borrow rates.
Expand All @@ -40,6 +47,7 @@ contract FixedRateIrm is IFixedRateIrm {
function setBorrowRate(Id id, uint256 newBorrowRate) external {
require(borrowRateStored[id] == 0, RATE_SET);
require(newBorrowRate != 0, RATE_ZERO);
require(newBorrowRate <= MAX_BORROW_RATE, RATE_TOO_HIGH);

borrowRateStored[id] = newBorrowRate;

Expand Down
12 changes: 12 additions & 0 deletions test/forge/FixedRateIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ contract FixedRateIrmTest is Test {

function testSetBorrowRate(Id id, uint256 newBorrowRate) external {
vm.assume(newBorrowRate != 0);
vm.assume(newBorrowRate <= fixedRateIrm.MAX_BORROW_RATE());

fixedRateIrm.setBorrowRate(id, newBorrowRate);
assertEq(fixedRateIrm.borrowRateStored(id), newBorrowRate);
}

function testSetBorrowRateEvent(Id id, uint256 newBorrowRate) external {
vm.assume(newBorrowRate != 0);
vm.assume(newBorrowRate <= fixedRateIrm.MAX_BORROW_RATE());

vm.expectEmit(true, true, true, true, address(fixedRateIrm));
emit SetBorrowRate(id, newBorrowRate);
Expand All @@ -33,7 +35,9 @@ contract FixedRateIrmTest is Test {

function testSetBorrowRateAlreadySet(Id id, uint256 newBorrowRate1, uint256 newBorrowRate2) external {
vm.assume(newBorrowRate1 != 0);
vm.assume(newBorrowRate1 <= fixedRateIrm.MAX_BORROW_RATE());
vm.assume(newBorrowRate2 != 0);
vm.assume(newBorrowRate2 <= fixedRateIrm.MAX_BORROW_RATE());
fixedRateIrm.setBorrowRate(id, newBorrowRate1);
vm.expectRevert(bytes(RATE_SET));
fixedRateIrm.setBorrowRate(id, newBorrowRate2);
Expand All @@ -44,8 +48,15 @@ contract FixedRateIrmTest is Test {
fixedRateIrm.setBorrowRate(id, 0);
}

function testSetBorrowRateRateTooHigh(Id id, uint256 newBorrowRate) external {
vm.assume(newBorrowRate > fixedRateIrm.MAX_BORROW_RATE());
vm.expectRevert(bytes(RATE_TOO_HIGH));
fixedRateIrm.setBorrowRate(id, newBorrowRate);
}

function testBorrowRate(MarketParams memory marketParams, Market memory market, uint256 newBorrowRate) external {
vm.assume(newBorrowRate != 0);
vm.assume(newBorrowRate <= fixedRateIrm.MAX_BORROW_RATE());
fixedRateIrm.setBorrowRate(marketParams.id(), newBorrowRate);
assertEq(fixedRateIrm.borrowRate(marketParams, market), newBorrowRate);
}
Expand All @@ -59,6 +70,7 @@ contract FixedRateIrmTest is Test {
external
{
vm.assume(newBorrowRate != 0);
vm.assume(newBorrowRate <= fixedRateIrm.MAX_BORROW_RATE());
fixedRateIrm.setBorrowRate(marketParams.id(), newBorrowRate);
assertEq(fixedRateIrm.borrowRateView(marketParams, market), newBorrowRate);
}
Expand Down

0 comments on commit a0fa8f4

Please sign in to comment.