Skip to content

Commit

Permalink
feat: revert on not yet set rate
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Feb 5, 2024
1 parent 6ae27d9 commit 0cb5741
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/FixedRateIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ contract FixedRateIrm is IFixedRateIrm {

/* ERRORS */

/// @dev Thrown when the rate is not already set for this market.
string public constant RATE_NOT_SET = "rate not set";
/// @dev Thrown when the rate is already set for this market.
string public constant RATE_ALREADY_SET = "rate already set";
string public constant RATE_SET = "rate set";
/// @dev Thrown when trying to set the rate at zero.
string public constant RATE_ZERO = "rate zero";

Expand All @@ -34,7 +36,7 @@ contract FixedRateIrm is IFixedRateIrm {

/// @inheritdoc IFixedRateIrm
function setBorrowRate(Id id, uint256 newBorrowRate) external {
require(_borrowRate[id] == 0, RATE_ALREADY_SET);
require(_borrowRate[id] == 0, RATE_SET);
require(newBorrowRate != 0, RATE_ZERO);

_borrowRate[id] = newBorrowRate;
Expand All @@ -46,11 +48,15 @@ contract FixedRateIrm is IFixedRateIrm {

/// @inheritdoc IIrm
function borrowRateView(MarketParams memory marketParams, Market memory) external view returns (uint256) {
return _borrowRate[marketParams.id()];
uint256 borrowRateCached = _borrowRate[marketParams.id()];
require(borrowRateCached != 0, RATE_NOT_SET);
return borrowRateCached;
}

/// @inheritdoc IIrm
function borrowRate(MarketParams memory marketParams, Market memory) external view returns (uint256) {
return _borrowRate[marketParams.id()];
uint256 borrowRateCached = _borrowRate[marketParams.id()];
require(borrowRateCached != 0, RATE_NOT_SET);
return borrowRateCached;
}
}
16 changes: 15 additions & 1 deletion test/forge/FixedRateIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract FixedRateIrmTest is Test {
vm.assume(newBorrowRate1 != 0);
vm.assume(newBorrowRate2 != 0);
fixedRateIrm.setBorrowRate(id, newBorrowRate1);
vm.expectRevert("rate already set");
vm.expectRevert("rate set");
fixedRateIrm.setBorrowRate(id, newBorrowRate2);
}

Expand All @@ -50,11 +50,25 @@ contract FixedRateIrmTest is Test {
assertEq(fixedRateIrm.borrowRate(marketParams, market), newBorrowRate);
}

function testBorrowRateRateZero(MarketParams memory marketParams, Market memory market, uint256 newBorrowRate)
external
{
vm.expectRevert("rate not set");
fixedRateIrm.borrowRate(marketParams, market);
}

function testBorrowRateView(MarketParams memory marketParams, Market memory market, uint256 newBorrowRate)
external
{
vm.assume(newBorrowRate != 0);
fixedRateIrm.setBorrowRate(marketParams.id(), newBorrowRate);
assertEq(fixedRateIrm.borrowRateView(marketParams, market), newBorrowRate);
}

function testBorrowRateViewRateZero(MarketParams memory marketParams, Market memory market, uint256 newBorrowRate)
external
{
vm.expectRevert("rate not set");
fixedRateIrm.borrowRateView(marketParams, market);
}
}

0 comments on commit 0cb5741

Please sign in to comment.