Skip to content

Commit

Permalink
style!: [N-07] use SNAKE_CASE for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kkirka committed May 24, 2023
1 parent d44d1bb commit 3b6d8be
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 58 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": {
"code-complexity": ["error", 9],
"compiler-version": ["error", ">=0.5.0"],
"const-name-snakecase": "off",
"const-name-snakecase": "error",
"constructor-syntax": "error",
"func-visibility": ["error", { "ignoreConstructors": true }],
"max-line-length": ["error", 175],
Expand Down
12 changes: 6 additions & 6 deletions contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ contract Comptroller is
);

Exp memory percentage = div_(collateral, scaledBorrows);
if (lessThanExp(Exp({ mantissa: mantissaOne }), percentage)) {
if (lessThanExp(Exp({ mantissa: MANTISSA_ONE }), percentage)) {
revert CollateralExceedsThreshold(scaledBorrows.mantissa, collateral.mantissa);
}

Expand Down Expand Up @@ -704,8 +704,8 @@ contract Comptroller is
*/
function setCloseFactor(uint256 newCloseFactorMantissa) external {
_checkAccessAllowed("setCloseFactor(uint256)");
require(closeFactorMaxMantissa >= newCloseFactorMantissa, "Close factor greater than maximum close factor");
require(closeFactorMinMantissa <= newCloseFactorMantissa, "Close factor smaller than minimum close factor");
require(MAX_CLOSE_FACTOR_MANTISSA >= newCloseFactorMantissa, "Close factor greater than maximum close factor");
require(MIN_CLOSE_FACTOR_MANTISSA <= newCloseFactorMantissa, "Close factor smaller than minimum close factor");

uint256 oldCloseFactorMantissa = closeFactorMantissa;
closeFactorMantissa = newCloseFactorMantissa;
Expand Down Expand Up @@ -740,12 +740,12 @@ contract Comptroller is
}

// Check collateral factor <= 0.9
if (newCollateralFactorMantissa > collateralFactorMaxMantissa) {
if (newCollateralFactorMantissa > MAX_COLLATERAL_FACTOR_MANTISSA) {
revert InvalidCollateralFactor();
}

// Ensure that liquidation threshold <= 1
if (newLiquidationThresholdMantissa > mantissaOne) {
if (newLiquidationThresholdMantissa > MANTISSA_ONE) {
revert InvalidLiquidationThreshold();
}

Expand Down Expand Up @@ -1146,7 +1146,7 @@ contract Comptroller is
* @notice A marker method that returns true for a valid Comptroller contract
*/
function isComptroller() external pure override returns (bool) {
return _isComptroller;
return true;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions contracts/ComptrollerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,13 @@ contract ComptrollerStorage {
uint256 internal constant NO_ERROR = 0;

// closeFactorMantissa must be strictly greater than this value
uint256 internal constant closeFactorMinMantissa = 0.05e18; // 0.05
uint256 internal constant MIN_CLOSE_FACTOR_MANTISSA = 0.05e18; // 0.05

// closeFactorMantissa must not exceed this value
uint256 internal constant closeFactorMaxMantissa = 0.9e18; // 0.9
uint256 internal constant MAX_CLOSE_FACTOR_MANTISSA = 0.9e18; // 0.9

// No collateralFactorMantissa may exceed this value
uint256 internal constant collateralFactorMaxMantissa = 0.9e18; // 0.9

/// @notice Indicator that this is a Comptroller contract (for inspection)
bool internal constant _isComptroller = true;
uint256 internal constant MAX_COLLATERAL_FACTOR_MANTISSA = 0.9e18; // 0.9

/**
* @dev This empty reserved space is put in place to allow future versions to add new
Expand Down
32 changes: 17 additions & 15 deletions contracts/ExponentialNoError.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.13;

import { EXP_SCALE as EXP_SCALE_, MANTISSA_ONE as MANTISSA_ONE_ } from "./lib/constants.sol";

/**
* @title Exponential module for storing fixed-precision decimals
* @author Compound
Expand All @@ -17,18 +19,18 @@ contract ExponentialNoError {
uint256 mantissa;
}

uint256 internal constant expScale = 1e18;
uint256 internal constant doubleScale = 1e36;
uint256 internal constant halfExpScale = expScale / 2;
uint256 internal constant mantissaOne = expScale;
uint256 internal constant EXP_SCALE = EXP_SCALE_;
uint256 internal constant DOUBLE_SCALE = 1e36;
uint256 internal constant HALF_EXP_SCALE = EXP_SCALE / 2;
uint256 internal constant MANTISSA_ONE = MANTISSA_ONE_;

/**
* @dev Truncates the given exp to a whole number value.
* For example, truncate(Exp{mantissa: 15 * expScale}) = 15
* For example, truncate(Exp{mantissa: 15 * EXP_SCALE}) = 15
*/
function truncate(Exp memory exp) internal pure returns (uint256) {
// Note: We are not using careful math here as we're performing a division that cannot fail
return exp.mantissa / expScale;
return exp.mantissa / EXP_SCALE;
}

/**
Expand Down Expand Up @@ -95,62 +97,62 @@ contract ExponentialNoError {
}

function mul_(Exp memory a, Exp memory b) internal pure returns (Exp memory) {
return Exp({ mantissa: mul_(a.mantissa, b.mantissa) / expScale });
return Exp({ mantissa: mul_(a.mantissa, b.mantissa) / EXP_SCALE });
}

function mul_(Exp memory a, uint256 b) internal pure returns (Exp memory) {
return Exp({ mantissa: mul_(a.mantissa, b) });
}

function mul_(uint256 a, Exp memory b) internal pure returns (uint256) {
return mul_(a, b.mantissa) / expScale;
return mul_(a, b.mantissa) / EXP_SCALE;
}

function mul_(Double memory a, Double memory b) internal pure returns (Double memory) {
return Double({ mantissa: mul_(a.mantissa, b.mantissa) / doubleScale });
return Double({ mantissa: mul_(a.mantissa, b.mantissa) / DOUBLE_SCALE });
}

function mul_(Double memory a, uint256 b) internal pure returns (Double memory) {
return Double({ mantissa: mul_(a.mantissa, b) });
}

function mul_(uint256 a, Double memory b) internal pure returns (uint256) {
return mul_(a, b.mantissa) / doubleScale;
return mul_(a, b.mantissa) / DOUBLE_SCALE;
}

function mul_(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}

function div_(Exp memory a, Exp memory b) internal pure returns (Exp memory) {
return Exp({ mantissa: div_(mul_(a.mantissa, expScale), b.mantissa) });
return Exp({ mantissa: div_(mul_(a.mantissa, EXP_SCALE), b.mantissa) });
}

function div_(Exp memory a, uint256 b) internal pure returns (Exp memory) {
return Exp({ mantissa: div_(a.mantissa, b) });
}

function div_(uint256 a, Exp memory b) internal pure returns (uint256) {
return div_(mul_(a, expScale), b.mantissa);
return div_(mul_(a, EXP_SCALE), b.mantissa);
}

function div_(Double memory a, Double memory b) internal pure returns (Double memory) {
return Double({ mantissa: div_(mul_(a.mantissa, doubleScale), b.mantissa) });
return Double({ mantissa: div_(mul_(a.mantissa, DOUBLE_SCALE), b.mantissa) });
}

function div_(Double memory a, uint256 b) internal pure returns (Double memory) {
return Double({ mantissa: div_(a.mantissa, b) });
}

function div_(uint256 a, Double memory b) internal pure returns (uint256) {
return div_(mul_(a, doubleScale), b.mantissa);
return div_(mul_(a, DOUBLE_SCALE), b.mantissa);
}

function div_(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}

function fraction(uint256 a, uint256 b) internal pure returns (Double memory) {
return Double({ mantissa: div_(mul_(a, doubleScale), b) });
return Double({ mantissa: div_(mul_(a, DOUBLE_SCALE), b) });
}
}
8 changes: 5 additions & 3 deletions contracts/InterestRateModel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ pragma solidity 0.8.13;
* @author Compound
*/
abstract contract InterestRateModel {
/// @notice Indicator that this is an InterestRateModel contract (for inspection)
bool public constant isInterestRateModel = true;

/**
* @notice Calculates the current borrow interest rate per block
* @param cash The total amount of cash the market has
Expand Down Expand Up @@ -36,4 +33,9 @@ abstract contract InterestRateModel {
uint256 reserves,
uint256 reserveFactorMantissa
) external view virtual returns (uint256);

/// @notice Indicator that this is an InterestRateModel contract (for inspection)
function isInterestRateModel() external pure virtual returns (bool) {
return true;
}
}
4 changes: 2 additions & 2 deletions contracts/Lens/PoolLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ contract PoolLens is ExponentialNoError {
});
if (borrowerIndex.mantissa == 0 && borrowIndex.mantissa > 0) {
// Covers the case where users borrowed tokens before the market's borrow state index was set
borrowerIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
borrowerIndex.mantissa = rewardsDistributor.INITIAL_INDEX();
}
Double memory deltaIndex = sub_(borrowIndex, borrowerIndex);
uint256 borrowerAmount = div_(VToken(vToken).borrowBalanceStored(borrower), marketBorrowIndex);
Expand All @@ -528,7 +528,7 @@ contract PoolLens is ExponentialNoError {
});
if (supplierIndex.mantissa == 0 && supplyIndex.mantissa > 0) {
// Covers the case where users supplied tokens before the market's supply state index was set
supplierIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
supplierIndex.mantissa = rewardsDistributor.INITIAL_INDEX();
}
Double memory deltaIndex = sub_(supplyIndex, supplierIndex);
uint256 supplierTokens = VToken(vToken).balanceOf(supplier);
Expand Down
18 changes: 9 additions & 9 deletions contracts/Rewards/RewardsDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ contract RewardsDistributor is ExponentialNoError, Ownable2StepUpgradeable, Acce
uint32 block;
}

/// @notice The initial REWARD TOKEN index for a market
uint224 public constant INITIAL_INDEX = 1e36;

/// @notice The REWARD TOKEN market supply state for each market
mapping(address => RewardToken) public rewardTokenSupplyState;

/// @notice The REWARD TOKEN borrow index for each market for each supplier as of the last time they accrued REWARD TOKEN
mapping(address => mapping(address => uint256)) public rewardTokenSupplierIndex;

/// @notice The initial REWARD TOKEN index for a market
uint224 public constant rewardTokenInitialIndex = 1e36;

/// @notice The REWARD TOKEN accrued but not yet transferred to each user
mapping(address => uint256) public rewardTokenAccrued;

Expand Down Expand Up @@ -135,12 +135,12 @@ contract RewardsDistributor is ExponentialNoError, Ownable2StepUpgradeable, Acce
*/
if (supplyState.index == 0) {
// Initialize supply state index with default value
supplyState.index = rewardTokenInitialIndex;
supplyState.index = INITIAL_INDEX;
}

if (borrowState.index == 0) {
// Initialize borrow state index with default value
borrowState.index = rewardTokenInitialIndex;
borrowState.index = INITIAL_INDEX;
}

/*
Expand Down Expand Up @@ -347,11 +347,11 @@ contract RewardsDistributor is ExponentialNoError, Ownable2StepUpgradeable, Acce
// Update supplier's index to the current index since we are distributing accrued REWARD TOKEN
rewardTokenSupplierIndex[vToken][supplier] = supplyIndex;

if (supplierIndex == 0 && supplyIndex >= rewardTokenInitialIndex) {
if (supplierIndex == 0 && supplyIndex >= INITIAL_INDEX) {
// Covers the case where users supplied tokens before the market's supply state index was set.
// Rewards the user with REWARD TOKEN accrued from the start of when supplier rewards were first
// set for the market.
supplierIndex = rewardTokenInitialIndex;
supplierIndex = INITIAL_INDEX;
}

// Calculate change in the cumulative sum of the REWARD TOKEN per vToken accrued
Expand Down Expand Up @@ -385,11 +385,11 @@ contract RewardsDistributor is ExponentialNoError, Ownable2StepUpgradeable, Acce
// Update borrowers's index to the current index since we are distributing accrued REWARD TOKEN
rewardTokenBorrowerIndex[vToken][borrower] = borrowIndex;

if (borrowerIndex == 0 && borrowIndex >= rewardTokenInitialIndex) {
if (borrowerIndex == 0 && borrowIndex >= INITIAL_INDEX) {
// Covers the case where users borrowed tokens before the market's borrow state index was set.
// Rewards the user with REWARD TOKEN accrued from the start of when borrower rewards were first
// set for the market.
borrowerIndex = rewardTokenInitialIndex;
borrowerIndex = INITIAL_INDEX;
}

// Calculate change in the cumulative sum of the REWARD TOKEN per borrowed unit accrued
Expand Down
6 changes: 3 additions & 3 deletions contracts/RiskFund/ProtocolShareReserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ contract ProtocolShareReserve is Ownable2StepUpgradeable, ExponentialNoError, Re
address private protocolIncome;
address private riskFund;
// Percentage of funds not sent to the RiskFund contract when the funds are released, following the project Tokenomics
uint256 private constant protocolSharePercentage = 70;
uint256 private constant baseUnit = 100;
uint256 private constant PROTOCOL_SHARE_PERCENTAGE = 70;
uint256 private constant BASE_UNIT = 100;

/// @notice Emitted when funds are released
event FundsReleased(address comptroller, address asset, uint256 amount);
Expand Down Expand Up @@ -81,7 +81,7 @@ contract ProtocolShareReserve is Ownable2StepUpgradeable, ExponentialNoError, Re
poolsAssetsReserves[comptroller][asset] -= amount;
uint256 protocolIncomeAmount = mul_(
Exp({ mantissa: amount }),
div_(Exp({ mantissa: protocolSharePercentage * expScale }), baseUnit)
div_(Exp({ mantissa: PROTOCOL_SHARE_PERCENTAGE * EXP_SCALE }), BASE_UNIT)
).mantissa;

IERC20Upgradeable(asset).safeTransfer(protocolIncome, protocolIncomeAmount);
Expand Down
8 changes: 4 additions & 4 deletions contracts/VToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ contract VToken is

/* Calculate the current borrow interest rate */
uint256 borrowRateMantissa = interestRateModel.getBorrowRate(cashPrior, borrowsPrior, reservesPrior);
require(borrowRateMantissa <= borrowRateMaxMantissa, "borrow rate is absurdly high");
require(borrowRateMantissa <= MAX_BORROW_RATE_MANTISSA, "borrow rate is absurdly high");

/* Calculate the number of blocks elapsed since the last accrual */
uint256 blockDelta = currentBlockNumber - accrualBlockNumberPrior;
Expand Down Expand Up @@ -1167,7 +1167,7 @@ contract VToken is
}

// Check newReserveFactor ≤ maxReserveFactor
if (newReserveFactorMantissa > reserveFactorMaxMantissa) {
if (newReserveFactorMantissa > MAX_RESERVE_FACTOR_MANTISSA) {
revert SetReserveFactorBoundsCheck();
}

Expand Down Expand Up @@ -1381,7 +1381,7 @@ contract VToken is

// Initialize block number and borrow index (block number mocks depend on comptroller being set)
accrualBlockNumber = _getBlockNumber();
borrowIndex = mantissaOne;
borrowIndex = MANTISSA_ONE;

// Set the interest rate model (depends on block number / borrow index)
_setInterestRateModelFresh(interestRateModel_);
Expand Down Expand Up @@ -1480,7 +1480,7 @@ contract VToken is
*/
uint256 totalCash = _getCashPrior();
uint256 cashPlusBorrowsMinusReserves = totalCash + totalBorrows + badDebt - totalReserves;
uint256 exchangeRate = (cashPlusBorrowsMinusReserves * expScale) / _totalSupply;
uint256 exchangeRate = (cashPlusBorrowsMinusReserves * EXP_SCALE) / _totalSupply;

return exchangeRate;
}
Expand Down
16 changes: 9 additions & 7 deletions contracts/VTokenInterfaces.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ contract VTokenStorage {
address payable public protocolShareReserve;

// Maximum borrow rate that can ever be applied (.0005% / block)
uint256 internal constant borrowRateMaxMantissa = 0.0005e16;
uint256 internal constant MAX_BORROW_RATE_MANTISSA = 0.0005e16;

// Maximum fraction of interest that can be set aside for reserves
uint256 internal constant reserveFactorMaxMantissa = 1e18;
uint256 internal constant MAX_RESERVE_FACTOR_MANTISSA = 1e18;

/**
* @notice Contract which oversees inter-vToken operations
Expand Down Expand Up @@ -136,11 +136,6 @@ abstract contract VTokenInterface is VTokenStorage {
address payable protocolShareReserve;
}

/**
* @notice Indicator that this is a VToken contract (for inspection)
*/
bool public constant isVToken = true;

/*** Market Events ***/

/**
Expand Down Expand Up @@ -368,4 +363,11 @@ abstract contract VTokenInterface is VTokenStorage {
function exchangeRateStored() external view virtual returns (uint256);

function getCash() external view virtual returns (uint256);

/**
* @notice Indicator that this is a VToken contract (for inspection)
*/
function isVToken() external pure virtual returns (bool) {
return true;
}
}
2 changes: 1 addition & 1 deletion contracts/test/VTokenHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ contract VTokenHarness is VToken {
}

function getBorrowRateMaxMantissa() external pure returns (uint256) {
return borrowRateMaxMantissa;
return MAX_BORROW_RATE_MANTISSA;
}

function harnessSetInterestRateModel(address newInterestRateModelAddress) public {
Expand Down
2 changes: 1 addition & 1 deletion tests/hardhat/Lens/RewardsSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe("PoolLens: Rewards Summary", () => {
index: convertToUnit(1, 36), // Current index is 1.0, double scale
block: await ethers.provider.getBlockNumber(),
});
rewardDistributor3.rewardTokenInitialIndex.returns(convertToUnit(0.6, 36)); // Should start accruing rewards at 0.6 of the current index
rewardDistributor3.INITIAL_INDEX.returns(convertToUnit(0.6, 36)); // Should start accruing rewards at 0.6 of the current index

const pendingRewards = await poolLens.getPendingRewards(await account.getAddress(), comptroller.address);

Expand Down

0 comments on commit 3b6d8be

Please sign in to comment.