Skip to content

Commit

Permalink
chore: 👕 lint sol
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Nov 3, 2022
1 parent eda5952 commit 2c10962
Show file tree
Hide file tree
Showing 46 changed files with 3,904 additions and 2,065 deletions.
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rules": {
"quotes": ["error", "double"],
"max-line-length": ["error", 200],
"compiler-version": ["error", "^0.8.0"],
"compiler-version": ["error", ">=0.5.0"],
"reason-string": ["warn", { "maxLength": 128 }],
"not-rely-on-block-hash": "off",
"not-rely-on-time": "off",
Expand Down
108 changes: 80 additions & 28 deletions contracts/BaseJumpRateModelV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ pragma solidity ^0.8.10;
import "./InterestRateModel.sol";

/**
* @title Logic for Compound's JumpRateModel Contract V2.
* @author Compound (modified by Dharma Labs, refactored by Arr00)
* @notice Version 2 modifies Version 1 by enabling updateable parameters.
*/
* @title Logic for Compound's JumpRateModel Contract V2.
* @author Compound (modified by Dharma Labs, refactored by Arr00)
* @notice Version 2 modifies Version 1 by enabling updateable parameters.
*/
abstract contract BaseJumpRateModelV2 is InterestRateModel {
event NewInterestParams(uint baseRatePerBlock, uint multiplierPerBlock, uint jumpMultiplierPerBlock, uint kink);
event NewInterestParams(
uint256 baseRatePerBlock,
uint256 multiplierPerBlock,
uint256 jumpMultiplierPerBlock,
uint256 kink
);

uint256 private constant BASE = 1e18;

Expand All @@ -21,27 +26,27 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
/**
* @notice The approximate number of blocks per year that is assumed by the interest rate model
*/
uint public constant blocksPerYear = 2102400;
uint256 public constant blocksPerYear = 2102400;

/**
* @notice The multiplier of utilization rate that gives the slope of the interest rate
*/
uint public multiplierPerBlock;
uint256 public multiplierPerBlock;

/**
* @notice The base interest rate which is the y-intercept when utilization rate is 0
*/
uint public baseRatePerBlock;
uint256 public baseRatePerBlock;

/**
* @notice The multiplierPerBlock after hitting a specified utilization point
*/
uint public jumpMultiplierPerBlock;
uint256 public jumpMultiplierPerBlock;

/**
* @notice The utilization point at which the jump multiplier is applied
*/
uint public kink;
uint256 public kink;

/**
* @notice Construct an interest rate model
Expand All @@ -51,10 +56,21 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param kink_ The utilization point at which the jump multiplier is applied
* @param owner_ The address of the owner, i.e. the Timelock contract (which has the ability to update parameters directly)
*/
constructor(uint baseRatePerYear, uint multiplierPerYear, uint jumpMultiplierPerYear, uint kink_, address owner_) internal {
constructor(
uint256 baseRatePerYear,
uint256 multiplierPerYear,
uint256 jumpMultiplierPerYear,
uint256 kink_,
address owner_
) internal {
owner = owner_;

updateJumpRateModelInternal(baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_);
updateJumpRateModelInternal(
baseRatePerYear,
multiplierPerYear,
jumpMultiplierPerYear,
kink_
);
}

/**
Expand All @@ -64,10 +80,20 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param jumpMultiplierPerYear The multiplierPerBlock after hitting a specified utilization point
* @param kink_ The utilization point at which the jump multiplier is applied
*/
function updateJumpRateModel(uint baseRatePerYear, uint multiplierPerYear, uint jumpMultiplierPerYear, uint kink_) virtual external {
function updateJumpRateModel(
uint256 baseRatePerYear,
uint256 multiplierPerYear,
uint256 jumpMultiplierPerYear,
uint256 kink_
) external virtual {
require(msg.sender == owner, "only the owner may call this function.");

updateJumpRateModelInternal(baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_);
updateJumpRateModelInternal(
baseRatePerYear,
multiplierPerYear,
jumpMultiplierPerYear,
kink_
);
}

/**
Expand All @@ -77,13 +103,17 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param reserves The amount of reserves in the market (currently unused)
* @return The utilization rate as a mantissa between [0, BASE]
*/
function utilizationRate(uint cash, uint borrows, uint reserves) public pure returns (uint) {
function utilizationRate(
uint256 cash,
uint256 borrows,
uint256 reserves
) public pure returns (uint256) {
// Utilization rate is 0 when there are no borrows
if (borrows == 0) {
return 0;
}

return borrows * BASE / (cash + borrows - reserves);
return (borrows * BASE) / (cash + borrows - reserves);
}

/**
Expand All @@ -93,14 +123,19 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param reserves The amount of reserves in the market
* @return The borrow rate percentage per block as a mantissa (scaled by BASE)
*/
function getBorrowRateInternal(uint cash, uint borrows, uint reserves) internal view returns (uint) {
uint util = utilizationRate(cash, borrows, reserves);
function getBorrowRateInternal(
uint256 cash,
uint256 borrows,
uint256 reserves
) internal view returns (uint256) {
uint256 util = utilizationRate(cash, borrows, reserves);

if (util <= kink) {
return ((util * multiplierPerBlock) / BASE) + baseRatePerBlock;
} else {
uint normalRate = ((kink * multiplierPerBlock) / BASE) + baseRatePerBlock;
uint excessUtil = util - kink;
uint256 normalRate = ((kink * multiplierPerBlock) / BASE) +
baseRatePerBlock;
uint256 excessUtil = util - kink;
return ((excessUtil * jumpMultiplierPerBlock) / BASE) + normalRate;
}
}
Expand All @@ -113,11 +148,16 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param reserveFactorMantissa The current reserve factor for the market
* @return The supply rate percentage per block as a mantissa (scaled by BASE)
*/
function getSupplyRate(uint cash, uint borrows, uint reserves, uint reserveFactorMantissa) virtual override public view returns (uint) {
uint oneMinusReserveFactor = BASE - reserveFactorMantissa;
uint borrowRate = getBorrowRateInternal(cash, borrows, reserves);
uint rateToPool = borrowRate * oneMinusReserveFactor / BASE;
return utilizationRate(cash, borrows, reserves) * rateToPool / BASE;
function getSupplyRate(
uint256 cash,
uint256 borrows,
uint256 reserves,
uint256 reserveFactorMantissa
) public view virtual override returns (uint256) {
uint256 oneMinusReserveFactor = BASE - reserveFactorMantissa;
uint256 borrowRate = getBorrowRateInternal(cash, borrows, reserves);
uint256 rateToPool = (borrowRate * oneMinusReserveFactor) / BASE;
return (utilizationRate(cash, borrows, reserves) * rateToPool) / BASE;
}

/**
Expand All @@ -127,12 +167,24 @@ abstract contract BaseJumpRateModelV2 is InterestRateModel {
* @param jumpMultiplierPerYear The multiplierPerBlock after hitting a specified utilization point
* @param kink_ The utilization point at which the jump multiplier is applied
*/
function updateJumpRateModelInternal(uint baseRatePerYear, uint multiplierPerYear, uint jumpMultiplierPerYear, uint kink_) internal {
function updateJumpRateModelInternal(
uint256 baseRatePerYear,
uint256 multiplierPerYear,
uint256 jumpMultiplierPerYear,
uint256 kink_
) internal {
baseRatePerBlock = baseRatePerYear / blocksPerYear;
multiplierPerBlock = (multiplierPerYear * BASE) / (blocksPerYear * kink_);
multiplierPerBlock =
(multiplierPerYear * BASE) /
(blocksPerYear * kink_);
jumpMultiplierPerBlock = jumpMultiplierPerYear / blocksPerYear;
kink = kink_;

emit NewInterestParams(baseRatePerBlock, multiplierPerBlock, jumpMultiplierPerBlock, kink);
emit NewInterestParams(
baseRatePerBlock,
multiplierPerBlock,
jumpMultiplierPerBlock,
kink
);
}
}
Loading

0 comments on commit 2c10962

Please sign in to comment.