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

bound (fixed) rate #128

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions src/fixed-rate-irm/FixedRateIrm.sol
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
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 {
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
require(borrowRateStored[id] == 0, RATE_SET);
require(newBorrowRate != 0, RATE_ZERO);
require(newBorrowRate <= MAX_BORROW_RATE, RATE_TOO_HIGH);

borrowRateStored[id] = newBorrowRate;

Expand Down
19 changes: 13 additions & 6 deletions test/forge/FixedRateIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ contract FixedRateIrmTest is Test {
}

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

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

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

vm.expectEmit(true, true, true, true, address(fixedRateIrm));
emit SetBorrowRate(id, newBorrowRate);
fixedRateIrm.setBorrowRate(id, newBorrowRate);
}

function testSetBorrowRateAlreadySet(Id id, uint256 newBorrowRate1, uint256 newBorrowRate2) external {
vm.assume(newBorrowRate1 != 0);
vm.assume(newBorrowRate2 != 0);
newBorrowRate1 = bound(newBorrowRate1, 1, fixedRateIrm.MAX_BORROW_RATE());
newBorrowRate2 = bound(newBorrowRate2, 1, fixedRateIrm.MAX_BORROW_RATE());

fixedRateIrm.setBorrowRate(id, newBorrowRate1);
vm.expectRevert(bytes(RATE_SET));
fixedRateIrm.setBorrowRate(id, newBorrowRate2);
Expand All @@ -44,8 +45,14 @@ contract FixedRateIrmTest is Test {
fixedRateIrm.setBorrowRate(id, 0);
}

function testSetBorrowRateTooHigh(Id id, uint256 newBorrowRate) external {
newBorrowRate = bound(newBorrowRate, fixedRateIrm.MAX_BORROW_RATE() + 1, type(uint256).max);
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);
newBorrowRate = bound(newBorrowRate, 1, fixedRateIrm.MAX_BORROW_RATE());
fixedRateIrm.setBorrowRate(marketParams.id(), newBorrowRate);
assertEq(fixedRateIrm.borrowRate(marketParams, market), newBorrowRate);
}
Expand All @@ -58,7 +65,7 @@ contract FixedRateIrmTest is Test {
function testBorrowRateView(MarketParams memory marketParams, Market memory market, uint256 newBorrowRate)
external
{
vm.assume(newBorrowRate != 0);
newBorrowRate = bound(newBorrowRate, 1, fixedRateIrm.MAX_BORROW_RATE());
fixedRateIrm.setBorrowRate(marketParams.id(), newBorrowRate);
assertEq(fixedRateIrm.borrowRateView(marketParams, market), newBorrowRate);
}
Expand Down
Loading