Unitroller

Description:

Proxy contract enabling upgradeable smart contract patterns. Delegates calls to an implementation contract.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "contracts/Unitroller.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./ErrorReporter.sol";\r
import "./ComptrollerStorage.sol";\r
/**\r
 * @title ComptrollerCore\r
 * @dev Storage for the comptroller is at this address, while execution is delegated to the `comptrollerImplementation`.\r
 * CTokens should reference this contract as their comptroller.\r
 */\r
contract Unitroller is UnitrollerAdminStorage, ComptrollerErrorReporter {\r
\r
    /**\r
      * @notice Emitted when pendingComptrollerImplementation is changed\r
      */\r
    event NewPendingImplementation(address oldPendingImplementation, address newPendingImplementation);\r
\r
    /**\r
      * @notice Emitted when pendingComptrollerImplementation is accepted, which means comptroller implementation is updated\r
      */\r
    event NewImplementation(address oldImplementation, address newImplementation);\r
\r
    /**\r
      * @notice Emitted when pendingAdmin is changed\r
      */\r
    event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin);\r
\r
    /**\r
      * @notice Emitted when pendingAdmin is accepted, which means admin is updated\r
      */\r
    event NewAdmin(address oldAdmin, address newAdmin);\r
\r
    constructor() public {\r
        // Set admin to caller\r
        admin = msg.sender;\r
    }\r
\r
    /*** Admin Functions ***/\r
    function _setPendingImplementation(address newPendingImplementation) public returns (uint) {\r
\r
        if (msg.sender != admin) {\r
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PENDING_IMPLEMENTATION_OWNER_CHECK);\r
        }\r
\r
        address oldPendingImplementation = pendingComptrollerImplementation;\r
\r
        pendingComptrollerImplementation = newPendingImplementation;\r
\r
        emit NewPendingImplementation(oldPendingImplementation, pendingComptrollerImplementation);\r
\r
        return uint(Error.NO_ERROR);\r
    }\r
\r
    /**\r
    * @notice Accepts new implementation of comptroller. msg.sender must be pendingImplementation\r
    * @dev Admin function for new implementation to accept it's role as implementation\r
    * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
    */\r
    function _acceptImplementation() public returns (uint) {\r
        // Check caller is pendingImplementation and pendingImplementation ≠ address(0)\r
        if (msg.sender != pendingComptrollerImplementation || pendingComptrollerImplementation == address(0)) {\r
            return fail(Error.UNAUTHORIZED, FailureInfo.ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK);\r
        }\r
\r
        // Save current values for inclusion in log\r
        address oldImplementation = comptrollerImplementation;\r
        address oldPendingImplementation = pendingComptrollerImplementation;\r
\r
        comptrollerImplementation = pendingComptrollerImplementation;\r
\r
        pendingComptrollerImplementation = address(0);\r
\r
        emit NewImplementation(oldImplementation, comptrollerImplementation);\r
        emit NewPendingImplementation(oldPendingImplementation, pendingComptrollerImplementation);\r
\r
        return uint(Error.NO_ERROR);\r
    }\r
\r
\r
    /**\r
      * @notice Begins transfer of admin rights. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.\r
      * @dev Admin function to begin change of admin. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.\r
      * @param newPendingAdmin New pending admin.\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _setPendingAdmin(address newPendingAdmin) public returns (uint) {\r
        // Check caller = admin\r
        if (msg.sender != admin) {\r
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PENDING_ADMIN_OWNER_CHECK);\r
        }\r
\r
        // Save current value, if any, for inclusion in log\r
        address oldPendingAdmin = pendingAdmin;\r
\r
        // Store pendingAdmin with value newPendingAdmin\r
        pendingAdmin = newPendingAdmin;\r
\r
        // Emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin)\r
        emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin);\r
\r
        return uint(Error.NO_ERROR);\r
    }\r
\r
    /**\r
      * @notice Accepts transfer of admin rights. msg.sender must be pendingAdmin\r
      * @dev Admin function for pending admin to accept role and update admin\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _acceptAdmin() public returns (uint) {\r
        // Check caller is pendingAdmin and pendingAdmin ≠ address(0)\r
        if (msg.sender != pendingAdmin || msg.sender == address(0)) {\r
            return fail(Error.UNAUTHORIZED, FailureInfo.ACCEPT_ADMIN_PENDING_ADMIN_CHECK);\r
        }\r
\r
        // Save current values for inclusion in log\r
        address oldAdmin = admin;\r
        address oldPendingAdmin = pendingAdmin;\r
\r
        // Store admin with value pendingAdmin\r
        admin = pendingAdmin;\r
\r
        // Clear the pending value\r
        pendingAdmin = address(0);\r
\r
        emit NewAdmin(oldAdmin, admin);\r
        emit NewPendingAdmin(oldPendingAdmin, pendingAdmin);\r
\r
        return uint(Error.NO_ERROR);\r
    }\r
\r
    /**\r
     * @dev Delegates execution to an implementation contract.\r
     * It returns to the external caller whatever the implementation returns\r
     * or forwards reverts.\r
     */\r
    fallback() payable external {\r
        // delegate all other functions to current implementation\r
        (bool success, ) = comptrollerImplementation.delegatecall(msg.data);\r
\r
        assembly {\r
              let free_mem_ptr := mload(0x40)\r
              returndatacopy(free_mem_ptr, 0, returndatasize())\r
\r
              switch success\r
              case 0 { revert(free_mem_ptr, returndatasize()) }\r
              default { return(free_mem_ptr, returndatasize()) }\r
        }\r
    }\r
}\r
"
    },
    "contracts/ComptrollerStorage.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./CToken.sol";\r
import "./PriceOracle.sol";\r
\r
contract UnitrollerAdminStorage {\r
    /**\r
    * @notice Administrator for this contract\r
    */\r
    address public admin;\r
\r
    /**\r
    * @notice Pending administrator for this contract\r
    */\r
    address public pendingAdmin;\r
\r
    /**\r
    * @notice Active brains of Unitroller\r
    */\r
    address public comptrollerImplementation;\r
\r
    /**\r
    * @notice Pending brains of Unitroller\r
    */\r
    address public pendingComptrollerImplementation;\r
}\r
\r
contract ComptrollerV1Storage is UnitrollerAdminStorage {\r
\r
    /**\r
     * @notice Oracle which gives the price of any given asset\r
     */\r
    PriceOracle public oracle;\r
\r
    /**\r
     * @notice Multiplier used to calculate the maximum repayAmount when liquidating a borrow\r
     */\r
    uint public closeFactorMantissa;\r
\r
    /**\r
     * @notice Multiplier representing the discount on collateral that a liquidator receives\r
     */\r
    uint public liquidationIncentiveMantissa;\r
\r
    /**\r
     * @notice Max number of assets a single account can participate in (borrow or use as collateral)\r
     */\r
    uint public maxAssets;\r
\r
    /**\r
     * @notice Per-account mapping of "assets you are in", capped by maxAssets\r
     */\r
    mapping(address => CToken[]) public accountAssets;\r
\r
}\r
\r
contract ComptrollerV2Storage is ComptrollerV1Storage {\r
    struct Market {\r
        // Whether or not this market is listed\r
        bool isListed;\r
\r
        //  Multiplier representing the most one can borrow against their collateral in this market.\r
        //  For instance, 0.9 to allow borrowing 90% of collateral value.\r
        //  Must be between 0 and 1, and stored as a mantissa.\r
        uint collateralFactorMantissa;\r
\r
        // Per-market mapping of "accounts in this asset"\r
        mapping(address => bool) accountMembership;\r
\r
        // Whether or not this market receives COMP\r
        bool isComped;\r
    }\r
\r
    /**\r
     * @notice Official mapping of cTokens -> Market metadata\r
     * @dev Used e.g. to determine if a market is supported\r
     */\r
    mapping(address => Market) public markets;\r
\r
\r
    /**\r
     * @notice The Pause Guardian can pause certain actions as a safety mechanism.\r
     *  Actions which allow users to remove their own assets cannot be paused.\r
     *  Liquidation / seizing / transfer can only be paused globally, not by market.\r
     */\r
    address public pauseGuardian;\r
    bool public _mintGuardianPaused;\r
    bool public _borrowGuardianPaused;\r
    bool public transferGuardianPaused;\r
    bool public seizeGuardianPaused;\r
    mapping(address => bool) public mintGuardianPaused;\r
    mapping(address => bool) public borrowGuardianPaused;\r
}\r
\r
contract ComptrollerV3Storage is ComptrollerV2Storage {\r
    struct CompMarketState {\r
        // The market's last updated compBorrowIndex or compSupplyIndex\r
        uint224 index;\r
\r
        // The block number the index was last updated at\r
        uint32 block;\r
    }\r
\r
    /// @notice A list of all markets\r
    CToken[] public allMarkets;\r
\r
    /// @notice The rate at which the flywheel distributes COMP, per block\r
    uint public compRate;\r
\r
    /// @notice The portion of compRate that each market currently receives\r
    mapping(address => uint) public compSpeeds;\r
\r
    /// @notice The COMP market supply state for each market\r
    mapping(address => CompMarketState) public compSupplyState;\r
\r
    /// @notice The COMP market borrow state for each market\r
    mapping(address => CompMarketState) public compBorrowState;\r
\r
    /// @notice The COMP borrow index for each market for each supplier as of the last time they accrued COMP\r
    mapping(address => mapping(address => uint)) public compSupplierIndex;\r
\r
    /// @notice The COMP borrow index for each market for each borrower as of the last time they accrued COMP\r
    mapping(address => mapping(address => uint)) public compBorrowerIndex;\r
\r
    /// @notice The COMP accrued but not yet transferred to each user\r
    mapping(address => uint) public compAccrued;\r
}\r
\r
contract ComptrollerV4Storage is ComptrollerV3Storage {\r
    // @notice The borrowCapGuardian can set borrowCaps to any number for any market. Lowering the borrow cap could disable borrowing on the given market.\r
    address public borrowCapGuardian;\r
\r
    // @notice Borrow caps enforced by borrowAllowed for each cToken address. Defaults to zero which corresponds to unlimited borrowing.\r
    mapping(address => uint) public borrowCaps;\r
}\r
\r
contract ComptrollerV5Storage is ComptrollerV4Storage {\r
    /// @notice The portion of COMP that each contributor receives per block\r
    mapping(address => uint) public compContributorSpeeds;\r
\r
    /// @notice Last block at which a contributor's COMP rewards have been allocated\r
    mapping(address => uint) public lastContributorBlock;\r
}\r
\r
contract ComptrollerV6Storage is ComptrollerV5Storage {\r
    /// @notice The rate at which comp is distributed to the corresponding borrow market (per block)\r
    mapping(address => uint) public compBorrowSpeeds;\r
\r
    /// @notice The rate at which comp is distributed to the corresponding supply market (per block)\r
    mapping(address => uint) public compSupplySpeeds;\r
}\r
\r
contract ComptrollerV7Storage is ComptrollerV6Storage {\r
    /// @notice Flag indicating whether the function to fix COMP accruals has been executed (RE: proposal 62 bug)\r
    bool public proposal65FixExecuted;\r
\r
    /// @notice Accounting storage mapping account addresses to how much COMP they owe the protocol.\r
    mapping(address => uint) public compReceivable;\r
}\r
"
    },
    "contracts/ErrorReporter.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
contract ComptrollerErrorReporter {\r
    enum Error {\r
        NO_ERROR,\r
        UNAUTHORIZED,\r
        COMPTROLLER_MISMATCH,\r
        INSUFFICIENT_SHORTFALL,\r
        INSUFFICIENT_LIQUIDITY,\r
        INVALID_CLOSE_FACTOR,\r
        INVALID_COLLATERAL_FACTOR,\r
        INVALID_LIQUIDATION_INCENTIVE,\r
        MARKET_NOT_ENTERED, // no longer possible\r
        MARKET_NOT_LISTED,\r
        MARKET_ALREADY_LISTED,\r
        MATH_ERROR,\r
        NONZERO_BORROW_BALANCE,\r
        PRICE_ERROR,\r
        REJECTION,\r
        SNAPSHOT_ERROR,\r
        TOO_MANY_ASSETS,\r
        TOO_MUCH_REPAY\r
    }\r
\r
    enum FailureInfo {\r
        ACCEPT_ADMIN_PENDING_ADMIN_CHECK,\r
        ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK,\r
        EXIT_MARKET_BALANCE_OWED,\r
        EXIT_MARKET_REJECTION,\r
        SET_CLOSE_FACTOR_OWNER_CHECK,\r
        SET_CLOSE_FACTOR_VALIDATION,\r
        SET_COLLATERAL_FACTOR_OWNER_CHECK,\r
        SET_COLLATERAL_FACTOR_NO_EXISTS,\r
        SET_COLLATERAL_FACTOR_VALIDATION,\r
        SET_COLLATERAL_FACTOR_WITHOUT_PRICE,\r
        SET_IMPLEMENTATION_OWNER_CHECK,\r
        SET_LIQUIDATION_INCENTIVE_OWNER_CHECK,\r
        SET_LIQUIDATION_INCENTIVE_VALIDATION,\r
        SET_MAX_ASSETS_OWNER_CHECK,\r
        SET_PENDING_ADMIN_OWNER_CHECK,\r
        SET_PENDING_IMPLEMENTATION_OWNER_CHECK,\r
        SET_PRICE_ORACLE_OWNER_CHECK,\r
        SUPPORT_MARKET_EXISTS,\r
        SUPPORT_MARKET_OWNER_CHECK,\r
        SET_PAUSE_GUARDIAN_OWNER_CHECK\r
    }\r
\r
    /**\r
      * @dev `error` corresponds to enum Error; `info` corresponds to enum FailureInfo, and `detail` is an arbitrary\r
      * contract-specific code that enables us to report opaque error codes from upgradeable contracts.\r
      **/\r
    event Failure(uint error, uint info, uint detail);\r
\r
    /**\r
      * @dev use this when reporting a known error from the money market or a non-upgradeable collaborator\r
      */\r
    function fail(Error err, FailureInfo info) internal returns (uint) {\r
        emit Failure(uint(err), uint(info), 0);\r
\r
        return uint(err);\r
    }\r
\r
    /**\r
      * @dev use this when reporting an opaque error from an upgradeable collaborator contract\r
      */\r
    function failOpaque(Error err, FailureInfo info, uint opaqueError) internal returns (uint) {\r
        emit Failure(uint(err), uint(info), opaqueError);\r
\r
        return uint(err);\r
    }\r
}\r
\r
contract TokenErrorReporter {\r
    uint public constant NO_ERROR = 0; // support legacy return codes\r
\r
    error TransferComptrollerRejection(uint256 errorCode);\r
    error TransferNotAllowed();\r
    error TransferNotEnough();\r
    error TransferTooMuch();\r
\r
    error MintComptrollerRejection(uint256 errorCode);\r
    error MintFreshnessCheck();\r
\r
    error RedeemComptrollerRejection(uint256 errorCode);\r
    error RedeemFreshnessCheck();\r
    error RedeemTransferOutNotPossible();\r
\r
    error BorrowComptrollerRejection(uint256 errorCode);\r
    error BorrowFreshnessCheck();\r
    error BorrowCashNotAvailable();\r
\r
    error RepayBorrowComptrollerRejection(uint256 errorCode);\r
    error RepayBorrowFreshnessCheck();\r
\r
    error LiquidateComptrollerRejection(uint256 errorCode);\r
    error LiquidateFreshnessCheck();\r
    error LiquidateCollateralFreshnessCheck();\r
    error LiquidateAccrueBorrowInterestFailed(uint256 errorCode);\r
    error LiquidateAccrueCollateralInterestFailed(uint256 errorCode);\r
    error LiquidateLiquidatorIsBorrower();\r
    error LiquidateCloseAmountIsZero();\r
    error LiquidateCloseAmountIsUintMax();\r
    error LiquidateRepayBorrowFreshFailed(uint256 errorCode);\r
\r
    error LiquidateSeizeComptrollerRejection(uint256 errorCode);\r
    error LiquidateSeizeLiquidatorIsBorrower();\r
\r
    error AcceptAdminPendingAdminCheck();\r
\r
    error SetComptrollerOwnerCheck();\r
    error SetPendingAdminOwnerCheck();\r
\r
    error SetReserveFactorAdminCheck();\r
    error SetReserveFactorFreshCheck();\r
    error SetReserveFactorBoundsCheck();\r
\r
    error AddReservesFactorFreshCheck(uint256 actualAddAmount);\r
\r
    error ReduceReservesAdminCheck();\r
    error ReduceReservesFreshCheck();\r
    error ReduceReservesCashNotAvailable();\r
    error ReduceReservesCashValidation();\r
\r
    error SetInterestRateModelOwnerCheck();\r
    error SetInterestRateModelFreshCheck();\r
}\r
"
    },
    "contracts/PriceOracle.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./CToken.sol";\r
\r
abstract contract PriceOracle {\r
    /// @notice Indicator that this is a PriceOracle contract (for inspection)\r
    bool public constant isPriceOracle = true;\r
\r
    /**\r
      * @notice Get the underlying price of a cToken asset\r
      * @param cToken The cToken to get the underlying price of\r
      * @return The underlying asset price mantissa (scaled by 1e18).\r
      *  Zero means the price is unavailable.\r
      */\r
    function getUnderlyingPrice(CToken cToken) virtual external view returns (uint);\r
}\r
"
    },
    "contracts/CToken.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./ComptrollerInterface.sol";\r
import "./CTokenInterfaces.sol";\r
import "./ErrorReporter.sol";\r
import "./EIP20Interface.sol";\r
import "./InterestRateModel.sol";\r
import "./ExponentialNoError.sol";\r
\r
/**\r
 * @title Compound's CToken Contract\r
 * @notice Abstract base for CTokens\r
 * @author Compound\r
 */\r
abstract contract CToken is CTokenInterface, ExponentialNoError, TokenErrorReporter {\r
    /**\r
     * @notice Initialize the money market\r
     * @param comptroller_ The address of the Comptroller\r
     * @param interestRateModel_ The address of the interest rate model\r
     * @param initialExchangeRateMantissa_ The initial exchange rate, scaled by 1e18\r
     * @param name_ EIP-20 name of this token\r
     * @param symbol_ EIP-20 symbol of this token\r
     * @param decimals_ EIP-20 decimal precision of this token\r
     */\r
    function initialize(ComptrollerInterface comptroller_,\r
                        InterestRateModel interestRateModel_,\r
                        uint initialExchangeRateMantissa_,\r
                        string memory name_,\r
                        string memory symbol_,\r
                        uint8 decimals_) public {\r
        require(msg.sender == admin, "only admin may initialize the market");\r
        require(accrualBlockNumber == 0 && borrowIndex == 0, "market may only be initialized once");\r
\r
        // Set initial exchange rate\r
        initialExchangeRateMantissa = initialExchangeRateMantissa_;\r
        require(initialExchangeRateMantissa > 0, "initial exchange rate must be greater than zero.");\r
\r
        // Set the comptroller\r
        uint err = _setComptroller(comptroller_);\r
        require(err == NO_ERROR, "setting comptroller failed");\r
\r
        // Initialize block number and borrow index (block number mocks depend on comptroller being set)\r
        accrualBlockNumber = getBlockNumber();\r
        borrowIndex = mantissaOne;\r
\r
        // Set the interest rate model (depends on block number / borrow index)\r
        err = _setInterestRateModelFresh(interestRateModel_);\r
        require(err == NO_ERROR, "setting interest rate model failed");\r
\r
        name = name_;\r
        symbol = symbol_;\r
        decimals = decimals_;\r
\r
        // The counter starts true to prevent changing it from zero to non-zero (i.e. smaller cost/refund)\r
        _notEntered = true;\r
    }\r
\r
    /**\r
     * @notice Transfer `tokens` tokens from `src` to `dst` by `spender`\r
     * @dev Called by both `transfer` and `transferFrom` internally\r
     * @param spender The address of the account performing the transfer\r
     * @param src The address of the source account\r
     * @param dst The address of the destination account\r
     * @param tokens The number of tokens to transfer\r
     * @return 0 if the transfer succeeded, else revert\r
     */\r
    function transferTokens(address spender, address src, address dst, uint tokens) internal returns (uint) {\r
        /* Fail if transfer not allowed */\r
        uint allowed = comptroller.transferAllowed(address(this), src, dst, tokens);\r
        if (allowed != 0) {\r
            revert TransferComptrollerRejection(allowed);\r
        }\r
\r
        /* Do not allow self-transfers */\r
        if (src == dst) {\r
            revert TransferNotAllowed();\r
        }\r
\r
        /* Get the allowance, infinite for the account owner */\r
        uint startingAllowance = 0;\r
        if (spender == src) {\r
            startingAllowance = type(uint).max;\r
        } else {\r
            startingAllowance = transferAllowances[src][spender];\r
        }\r
\r
        /* Do the calculations, checking for {under,over}flow */\r
        uint allowanceNew = startingAllowance - tokens;\r
        uint srcTokensNew = accountTokens[src] - tokens;\r
        uint dstTokensNew = accountTokens[dst] + tokens;\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        accountTokens[src] = srcTokensNew;\r
        accountTokens[dst] = dstTokensNew;\r
\r
        /* Eat some of the allowance (if necessary) */\r
        if (startingAllowance != type(uint).max) {\r
            transferAllowances[src][spender] = allowanceNew;\r
        }\r
\r
        /* We emit a Transfer event */\r
        emit Transfer(src, dst, tokens);\r
\r
        // unused function\r
        // comptroller.transferVerify(address(this), src, dst, tokens);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Transfer `amount` tokens from `msg.sender` to `dst`\r
     * @param dst The address of the destination account\r
     * @param amount The number of tokens to transfer\r
     * @return Whether or not the transfer succeeded\r
     */\r
    function transfer(address dst, uint256 amount) override external nonReentrant returns (bool) {\r
        return transferTokens(msg.sender, msg.sender, dst, amount) == NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Transfer `amount` tokens from `src` to `dst`\r
     * @param src The address of the source account\r
     * @param dst The address of the destination account\r
     * @param amount The number of tokens to transfer\r
     * @return Whether or not the transfer succeeded\r
     */\r
    function transferFrom(address src, address dst, uint256 amount) override external nonReentrant returns (bool) {\r
        return transferTokens(msg.sender, src, dst, amount) == NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Approve `spender` to transfer up to `amount` from `src`\r
     * @dev This will overwrite the approval amount for `spender`\r
     *  and is subject to issues noted [here](https://eips.ethereum.org/EIPS/eip-20#approve)\r
     * @param spender The address of the account which may transfer tokens\r
     * @param amount The number of tokens that are approved (uint256.max means infinite)\r
     * @return Whether or not the approval succeeded\r
     */\r
    function approve(address spender, uint256 amount) override external returns (bool) {\r
        address src = msg.sender;\r
        transferAllowances[src][spender] = amount;\r
        emit Approval(src, spender, amount);\r
        return true;\r
    }\r
\r
    /**\r
     * @notice Get the current allowance from `owner` for `spender`\r
     * @param owner The address of the account which owns the tokens to be spent\r
     * @param spender The address of the account which may transfer tokens\r
     * @return The number of tokens allowed to be spent (-1 means infinite)\r
     */\r
    function allowance(address owner, address spender) override external view returns (uint256) {\r
        return transferAllowances[owner][spender];\r
    }\r
\r
    /**\r
     * @notice Get the token balance of the `owner`\r
     * @param owner The address of the account to query\r
     * @return The number of tokens owned by `owner`\r
     */\r
    function balanceOf(address owner) override external view returns (uint256) {\r
        return accountTokens[owner];\r
    }\r
\r
    /**\r
     * @notice Get the underlying balance of the `owner`\r
     * @dev This also accrues interest in a transaction\r
     * @param owner The address of the account to query\r
     * @return The amount of underlying owned by `owner`\r
     */\r
    function balanceOfUnderlying(address owner) override external returns (uint) {\r
        Exp memory exchangeRate = Exp({mantissa: exchangeRateCurrent()});\r
        return mul_ScalarTruncate(exchangeRate, accountTokens[owner]);\r
    }\r
\r
    /**\r
     * @notice Get a snapshot of the account's balances, and the cached exchange rate\r
     * @dev This is used by comptroller to more efficiently perform liquidity checks.\r
     * @param account Address of the account to snapshot\r
     * @return (possible error, token balance, borrow balance, exchange rate mantissa)\r
     */\r
    function getAccountSnapshot(address account) override external view returns (uint, uint, uint, uint) {\r
        return (\r
            NO_ERROR,\r
            accountTokens[account],\r
            borrowBalanceStoredInternal(account),\r
            exchangeRateStoredInternal()\r
        );\r
    }\r
\r
    /**\r
     * @dev Function to simply retrieve block number\r
     *  This exists mainly for inheriting test contracts to stub this result.\r
     */\r
    function getBlockNumber() virtual internal view returns (uint) {\r
        return block.number;\r
    }\r
\r
    /**\r
     * @notice Returns the current per-block borrow interest rate for this cToken\r
     * @return The borrow interest rate per block, scaled by 1e18\r
     */\r
    function borrowRatePerBlock() override external view returns (uint) {\r
        return interestRateModel.getBorrowRate(getCashPrior(), totalBorrows, totalReserves);\r
    }\r
\r
    /**\r
     * @notice Returns the current per-block supply interest rate for this cToken\r
     * @return The supply interest rate per block, scaled by 1e18\r
     */\r
    function supplyRatePerBlock() override external view returns (uint) {\r
        return interestRateModel.getSupplyRate(getCashPrior(), totalBorrows, totalReserves, reserveFactorMantissa);\r
    }\r
\r
    /**\r
     * @notice Returns the current total borrows plus accrued interest\r
     * @return The total borrows with interest\r
     */\r
    function totalBorrowsCurrent() override external nonReentrant returns (uint) {\r
        accrueInterest();\r
        return totalBorrows;\r
    }\r
\r
    /**\r
     * @notice Accrue interest to updated borrowIndex and then calculate account's borrow balance using the updated borrowIndex\r
     * @param account The address whose balance should be calculated after updating borrowIndex\r
     * @return The calculated balance\r
     */\r
    function borrowBalanceCurrent(address account) override external nonReentrant returns (uint) {\r
        accrueInterest();\r
        return borrowBalanceStored(account);\r
    }\r
\r
    /**\r
     * @notice Return the borrow balance of account based on stored data\r
     * @param account The address whose balance should be calculated\r
     * @return The calculated balance\r
     */\r
    function borrowBalanceStored(address account) override public view returns (uint) {\r
        return borrowBalanceStoredInternal(account);\r
    }\r
\r
    /**\r
     * @notice Return the borrow balance of account based on stored data\r
     * @param account The address whose balance should be calculated\r
     * @return (error code, the calculated balance or 0 if error code is non-zero)\r
     */\r
    function borrowBalanceStoredInternal(address account) internal view returns (uint) {\r
        /* Get borrowBalance and borrowIndex */\r
        BorrowSnapshot storage borrowSnapshot = accountBorrows[account];\r
\r
        /* If borrowBalance = 0 then borrowIndex is likely also 0.\r
         * Rather than failing the calculation with a division by 0, we immediately return 0 in this case.\r
         */\r
        if (borrowSnapshot.principal == 0) {\r
            return 0;\r
        }\r
\r
        /* Calculate new borrow balance using the interest index:\r
         *  recentBorrowBalance = borrower.borrowBalance * market.borrowIndex / borrower.borrowIndex\r
         */\r
        uint principalTimesIndex = borrowSnapshot.principal * borrowIndex;\r
        return principalTimesIndex / borrowSnapshot.interestIndex;\r
    }\r
\r
    /**\r
     * @notice Accrue interest then return the up-to-date exchange rate\r
     * @return Calculated exchange rate scaled by 1e18\r
     */\r
    function exchangeRateCurrent() override public nonReentrant returns (uint) {\r
        accrueInterest();\r
        return exchangeRateStored();\r
    }\r
\r
    /**\r
     * @notice Calculates the exchange rate from the underlying to the CToken\r
     * @dev This function does not accrue interest before calculating the exchange rate\r
     * @return Calculated exchange rate scaled by 1e18\r
     */\r
    function exchangeRateStored() override public view returns (uint) {\r
        return exchangeRateStoredInternal();\r
    }\r
\r
    /**\r
     * @notice Calculates the exchange rate from the underlying to the CToken\r
     * @dev This function does not accrue interest before calculating the exchange rate\r
     * @return calculated exchange rate scaled by 1e18\r
     */\r
    function exchangeRateStoredInternal() virtual internal view returns (uint) {\r
        uint _totalSupply = totalSupply;\r
        if (_totalSupply == 0) {\r
            /*\r
             * If there are no tokens minted:\r
             *  exchangeRate = initialExchangeRate\r
             */\r
            return initialExchangeRateMantissa;\r
        } else {\r
            /*\r
             * Otherwise:\r
             *  exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply\r
             */\r
            uint totalCash = getCashPrior();\r
            uint cashPlusBorrowsMinusReserves = totalCash + totalBorrows - totalReserves;\r
            uint exchangeRate = cashPlusBorrowsMinusReserves * expScale / _totalSupply;\r
\r
            return exchangeRate;\r
        }\r
    }\r
\r
    /**\r
     * @notice Get cash balance of this cToken in the underlying asset\r
     * @return The quantity of underlying asset owned by this contract\r
     */\r
    function getCash() override external view returns (uint) {\r
        return getCashPrior();\r
    }\r
\r
    /**\r
     * @notice Applies accrued interest to total borrows and reserves\r
     * @dev This calculates interest accrued from the last checkpointed block\r
     *   up to the current block and writes new checkpoint to storage.\r
     */\r
    function accrueInterest() virtual override public returns (uint) {\r
        /* Remember the initial block number */\r
        uint currentBlockNumber = getBlockNumber();\r
        uint accrualBlockNumberPrior = accrualBlockNumber;\r
\r
        /* Short-circuit accumulating 0 interest */\r
        if (accrualBlockNumberPrior == currentBlockNumber) {\r
            return NO_ERROR;\r
        }\r
\r
        /* Read the previous values out of storage */\r
        uint cashPrior = getCashPrior();\r
        uint borrowsPrior = totalBorrows;\r
        uint reservesPrior = totalReserves;\r
        uint borrowIndexPrior = borrowIndex;\r
\r
        /* Calculate the current borrow interest rate */\r
        uint borrowRateMantissa = interestRateModel.getBorrowRate(cashPrior, borrowsPrior, reservesPrior);\r
        require(borrowRateMantissa <= borrowRateMaxMantissa, "borrow rate is absurdly high");\r
\r
        /* Calculate the number of blocks elapsed since the last accrual */\r
        uint blockDelta = currentBlockNumber - accrualBlockNumberPrior;\r
\r
        /*\r
         * Calculate the interest accumulated into borrows and reserves and the new index:\r
         *  simpleInterestFactor = borrowRate * blockDelta\r
         *  interestAccumulated = simpleInterestFactor * totalBorrows\r
         *  totalBorrowsNew = interestAccumulated + totalBorrows\r
         *  totalReservesNew = interestAccumulated * reserveFactor + totalReserves\r
         *  borrowIndexNew = simpleInterestFactor * borrowIndex + borrowIndex\r
         */\r
\r
        Exp memory simpleInterestFactor = mul_(Exp({mantissa: borrowRateMantissa}), blockDelta);\r
        uint interestAccumulated = mul_ScalarTruncate(simpleInterestFactor, borrowsPrior);\r
        uint totalBorrowsNew = interestAccumulated + borrowsPrior;\r
        uint totalReservesNew = mul_ScalarTruncateAddUInt(Exp({mantissa: reserveFactorMantissa}), interestAccumulated, reservesPrior);\r
        uint borrowIndexNew = mul_ScalarTruncateAddUInt(simpleInterestFactor, borrowIndexPrior, borrowIndexPrior);\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /* We write the previously calculated values into storage */\r
        accrualBlockNumber = currentBlockNumber;\r
        borrowIndex = borrowIndexNew;\r
        totalBorrows = totalBorrowsNew;\r
        totalReserves = totalReservesNew;\r
\r
        /* We emit an AccrueInterest event */\r
        emit AccrueInterest(cashPrior, interestAccumulated, borrowIndexNew, totalBorrowsNew);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Sender supplies assets into the market and receives cTokens in exchange\r
     * @dev Accrues interest whether or not the operation succeeds, unless reverted\r
     * @param mintAmount The amount of the underlying asset to supply\r
     */\r
    function mintInternal(uint mintAmount) internal nonReentrant {\r
        accrueInterest();\r
        // mintFresh emits the actual Mint event if successful and logs on errors, so we don't need to\r
        mintFresh(msg.sender, mintAmount);\r
    }\r
\r
    /**\r
     * @notice User supplies assets into the market and receives cTokens in exchange\r
     * @dev Assumes interest has already been accrued up to the current block\r
     * @param minter The address of the account which is supplying the assets\r
     * @param mintAmount The amount of the underlying asset to supply\r
     */\r
    function mintFresh(address minter, uint mintAmount) internal {\r
        /* Fail if mint not allowed */\r
        uint allowed = comptroller.mintAllowed(address(this), minter, mintAmount);\r
        if (allowed != 0) {\r
            revert MintComptrollerRejection(allowed);\r
        }\r
\r
        /* Verify market's block number equals current block number */\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert MintFreshnessCheck();\r
        }\r
\r
        Exp memory exchangeRate = Exp({mantissa: exchangeRateStoredInternal()});\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /*\r
         *  We call `doTransferIn` for the minter and the mintAmount.\r
         *  Note: The cToken must handle variations between ERC-20 and ETH underlying.\r
         *  `doTransferIn` reverts if anything goes wrong, since we can't be sure if\r
         *  side-effects occurred. The function returns the amount actually transferred,\r
         *  in case of a fee. On success, the cToken holds an additional `actualMintAmount`\r
         *  of cash.\r
         */\r
        uint actualMintAmount = doTransferIn(minter, mintAmount);\r
\r
        /*\r
         * We get the current exchange rate and calculate the number of cTokens to be minted:\r
         *  mintTokens = actualMintAmount / exchangeRate\r
         */\r
\r
        uint mintTokens = div_(actualMintAmount, exchangeRate);\r
\r
        /*\r
         * We calculate the new total supply of cTokens and minter token balance, checking for overflow:\r
         *  totalSupplyNew = totalSupply + mintTokens\r
         *  accountTokensNew = accountTokens[minter] + mintTokens\r
         * And write them into storage\r
         */\r
        totalSupply = totalSupply + mintTokens;\r
        accountTokens[minter] = accountTokens[minter] + mintTokens;\r
\r
        /* We emit a Mint event, and a Transfer event */\r
        emit Mint(minter, actualMintAmount, mintTokens);\r
        emit Transfer(address(this), minter, mintTokens);\r
\r
        /* We call the defense hook */\r
        // unused function\r
        // comptroller.mintVerify(address(this), minter, actualMintAmount, mintTokens);\r
    }\r
\r
    /**\r
     * @notice Sender redeems cTokens in exchange for the underlying asset\r
     * @dev Accrues interest whether or not the operation succeeds, unless reverted\r
     * @param redeemTokens The number of cTokens to redeem into underlying\r
     */\r
    function redeemInternal(uint redeemTokens) internal nonReentrant {\r
        accrueInterest();\r
        // redeemFresh emits redeem-specific logs on errors, so we don't need to\r
        redeemFresh(payable(msg.sender), redeemTokens, 0);\r
    }\r
\r
    /**\r
     * @notice Sender redeems cTokens in exchange for a specified amount of underlying asset\r
     * @dev Accrues interest whether or not the operation succeeds, unless reverted\r
     * @param redeemAmount The amount of underlying to receive from redeeming cTokens\r
     */\r
    function redeemUnderlyingInternal(uint redeemAmount) internal nonReentrant {\r
        accrueInterest();\r
        // redeemFresh emits redeem-specific logs on errors, so we don't need to\r
        redeemFresh(payable(msg.sender), 0, redeemAmount);\r
    }\r
\r
    /**\r
     * @notice User redeems cTokens in exchange for the underlying asset\r
     * @dev Assumes interest has already been accrued up to the current block\r
     * @param redeemer The address of the account which is redeeming the tokens\r
     * @param redeemTokensIn The number of cTokens to redeem into underlying (only one of redeemTokensIn or redeemAmountIn may be non-zero)\r
     * @param redeemAmountIn The number of underlying tokens to receive from redeeming cTokens (only one of redeemTokensIn or redeemAmountIn may be non-zero)\r
     */\r
    function redeemFresh(address payable redeemer, uint redeemTokensIn, uint redeemAmountIn) internal {\r
        require(redeemTokensIn == 0 || redeemAmountIn == 0, "one of redeemTokensIn or redeemAmountIn must be zero");\r
\r
        /* exchangeRate = invoke Exchange Rate Stored() */\r
        Exp memory exchangeRate = Exp({mantissa: exchangeRateStoredInternal() });\r
\r
        uint redeemTokens;\r
        uint redeemAmount;\r
        /* If redeemTokensIn > 0: */\r
        if (redeemTokensIn > 0) {\r
            /*\r
             * We calculate the exchange rate and the amount of underlying to be redeemed:\r
             *  redeemTokens = redeemTokensIn\r
             *  redeemAmount = redeemTokensIn x exchangeRateCurrent\r
             */\r
            redeemTokens = redeemTokensIn;\r
            redeemAmount = mul_ScalarTruncate(exchangeRate, redeemTokensIn);\r
        } else {\r
            /*\r
             * We get the current exchange rate and calculate the amount to be redeemed:\r
             *  redeemTokens = redeemAmountIn / exchangeRate\r
             *  redeemAmount = redeemAmountIn\r
             */\r
            redeemTokens = div_(redeemAmountIn, exchangeRate);\r
            redeemAmount = redeemAmountIn;\r
        }\r
\r
        /* Fail if redeem not allowed */\r
        uint allowed = comptroller.redeemAllowed(address(this), redeemer, redeemTokens);\r
        if (allowed != 0) {\r
            revert RedeemComptrollerRejection(allowed);\r
        }\r
\r
        /* Verify market's block number equals current block number */\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert RedeemFreshnessCheck();\r
        }\r
\r
        /* Fail gracefully if protocol has insufficient cash */\r
        if (getCashPrior() < redeemAmount) {\r
            revert RedeemTransferOutNotPossible();\r
        }\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
\r
        /*\r
         * We write the previously calculated values into storage.\r
         *  Note: Avoid token reentrancy attacks by writing reduced supply before external transfer.\r
         */\r
        totalSupply = totalSupply - redeemTokens;\r
        accountTokens[redeemer] = accountTokens[redeemer] - redeemTokens;\r
\r
        /*\r
         * We invoke doTransferOut for the redeemer and the redeemAmount.\r
         *  Note: The cToken must handle variations between ERC-20 and ETH underlying.\r
         *  On success, the cToken has redeemAmount less of cash.\r
         *  doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.\r
         */\r
        doTransferOut(redeemer, redeemAmount);\r
\r
        /* We emit a Transfer event, and a Redeem event */\r
        emit Transfer(redeemer, address(this), redeemTokens);\r
        emit Redeem(redeemer, redeemAmount, redeemTokens);\r
\r
        /* We call the defense hook */\r
        comptroller.redeemVerify(address(this), redeemer, redeemAmount, redeemTokens);\r
    }\r
\r
    /**\r
      * @notice Sender borrows assets from the protocol to their own address\r
      * @param borrowAmount The amount of the underlying asset to borrow\r
      */\r
    function borrowInternal(uint borrowAmount) internal nonReentrant {\r
        accrueInterest();\r
        // borrowFresh emits borrow-specific logs on errors, so we don't need to\r
        borrowFresh(payable(msg.sender), borrowAmount);\r
    }\r
\r
    /**\r
      * @notice Users borrow assets from the protocol to their own address\r
      * @param borrowAmount The amount of the underlying asset to borrow\r
      */\r
    function borrowFresh(address payable borrower, uint borrowAmount) internal {\r
        /* Fail if borrow not allowed */\r
        uint allowed = comptroller.borrowAllowed(address(this), borrower, borrowAmount);\r
        if (allowed != 0) {\r
            revert BorrowComptrollerRejection(allowed);\r
        }\r
\r
        /* Verify market's block number equals current block number */\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert BorrowFreshnessCheck();\r
        }\r
\r
        /* Fail gracefully if protocol has insufficient underlying cash */\r
        if (getCashPrior() < borrowAmount) {\r
            revert BorrowCashNotAvailable();\r
        }\r
\r
        /*\r
         * We calculate the new borrower and total borrow balances, failing on overflow:\r
         *  accountBorrowNew = accountBorrow + borrowAmount\r
         *  totalBorrowsNew = totalBorrows + borrowAmount\r
         */\r
        uint accountBorrowsPrev = borrowBalanceStoredInternal(borrower);\r
        uint accountBorrowsNew = accountBorrowsPrev + borrowAmount;\r
        uint totalBorrowsNew = totalBorrows + borrowAmount;\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /*\r
         * We write the previously calculated values into storage.\r
         *  Note: Avoid token reentrancy attacks by writing increased borrow before external transfer.\r
        `*/\r
        accountBorrows[borrower].principal = accountBorrowsNew;\r
        accountBorrows[borrower].interestIndex = borrowIndex;\r
        totalBorrows = totalBorrowsNew;\r
\r
        /*\r
         * We invoke doTransferOut for the borrower and the borrowAmount.\r
         *  Note: The cToken must handle variations between ERC-20 and ETH underlying.\r
         *  On success, the cToken borrowAmount less of cash.\r
         *  doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.\r
         */\r
        doTransferOut(borrower, borrowAmount);\r
\r
        /* We emit a Borrow event */\r
        emit Borrow(borrower, borrowAmount, accountBorrowsNew, totalBorrowsNew);\r
    }\r
\r
    /**\r
     * @notice Sender repays their own borrow\r
     * @param repayAmount The amount to repay, or -1 for the full outstanding amount\r
     */\r
    function repayBorrowInternal(uint repayAmount) internal nonReentrant {\r
        accrueInterest();\r
        // repayBorrowFresh emits repay-borrow-specific logs on errors, so we don't need to\r
        repayBorrowFresh(msg.sender, msg.sender, repayAmount);\r
    }\r
\r
    /**\r
     * @notice Sender repays a borrow belonging to borrower\r
     * @param borrower the account with the debt being payed off\r
     * @param repayAmount The amount to repay, or -1 for the full outstanding amount\r
     */\r
    function repayBorrowBehalfInternal(address borrower, uint repayAmount) internal nonReentrant {\r
        accrueInterest();\r
        // repayBorrowFresh emits repay-borrow-specific logs on errors, so we don't need to\r
        repayBorrowFresh(msg.sender, borrower, repayAmount);\r
    }\r
\r
    /**\r
     * @notice Borrows are repaid by another user (possibly the borrower).\r
     * @param payer the account paying off the borrow\r
     * @param borrower the account with the debt being payed off\r
     * @param repayAmount the amount of underlying tokens being returned, or -1 for the full outstanding amount\r
     * @return (uint) the actual repayment amount.\r
     */\r
    function repayBorrowFresh(address payer, address borrower, uint repayAmount) internal returns (uint) {\r
        /* Fail if repayBorrow not allowed */\r
        uint allowed = comptroller.repayBorrowAllowed(address(this), payer, borrower, repayAmount);\r
        if (allowed != 0) {\r
            revert RepayBorrowComptrollerRejection(allowed);\r
        }\r
\r
        /* Verify market's block number equals current block number */\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert RepayBorrowFreshnessCheck();\r
        }\r
\r
        /* We fetch the amount the borrower owes, with accumulated interest */\r
        uint accountBorrowsPrev = borrowBalanceStoredInternal(borrower);\r
\r
        /* If repayAmount == -1, repayAmount = accountBorrows */\r
        uint repayAmountFinal = repayAmount == type(uint).max ? accountBorrowsPrev : repayAmount;\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /*\r
         * We call doTransferIn for the payer and the repayAmount\r
         *  Note: The cToken must handle variations between ERC-20 and ETH underlying.\r
         *  On success, the cToken holds an additional repayAmount of cash.\r
         *  doTransferIn reverts if anything goes wrong, since we can't be sure if side effects occurred.\r
         *   it returns the amount actually transferred, in case of a fee.\r
         */\r
        uint actualRepayAmount = doTransferIn(payer, repayAmountFinal);\r
\r
        /*\r
         * We calculate the new borrower and total borrow balances, failing on underflow:\r
         *  accountBorrowsNew = accountBorrows - actualRepayAmount\r
         *  totalBorrowsNew = totalBorrows - actualRepayAmount\r
         */\r
        uint accountBorrowsNew = accountBorrowsPrev - actualRepayAmount;\r
        uint totalBorrowsNew = totalBorrows - actualRepayAmount;\r
\r
        /* We write the previously calculated values into storage */\r
        accountBorrows[borrower].principal = accountBorrowsNew;\r
        accountBorrows[borrower].interestIndex = borrowIndex;\r
        totalBorrows = totalBorrowsNew;\r
\r
        /* We emit a RepayBorrow event */\r
        emit RepayBorrow(payer, borrower, actualRepayAmount, accountBorrowsNew, totalBorrowsNew);\r
\r
        return actualRepayAmount;\r
    }\r
\r
    /**\r
     * @notice The sender liquidates the borrowers collateral.\r
     *  The collateral seized is transferred to the liquidator.\r
     * @param borrower The borrower of this cToken to be liquidated\r
     * @param cTokenCollateral The market in which to seize collateral from the borrower\r
     * @param repayAmount The amount of the underlying borrowed asset to repay\r
     */\r
    function liquidateBorrowInternal(address borrower, uint repayAmount, CTokenInterface cTokenCollateral) internal nonReentrant {\r
        accrueInterest();\r
\r
        uint error = cTokenCollateral.accrueInterest();\r
        if (error != NO_ERROR) {\r
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted liquidation failed\r
            revert LiquidateAccrueCollateralInterestFailed(error);\r
        }\r
\r
        // liquidateBorrowFresh emits borrow-specific logs on errors, so we don't need to\r
        liquidateBorrowFresh(msg.sender, borrower, repayAmount, cTokenCollateral);\r
    }\r
\r
    /**\r
     * @notice The liquidator liquidates the borrowers collateral.\r
     *  The collateral seized is transferred to the liquidator.\r
     * @param borrower The borrower of this cToken to be liquidated\r
     * @param liquidator The address repaying the borrow and seizing collateral\r
     * @param cTokenCollateral The market in which to seize collateral from the borrower\r
     * @param repayAmount The amount of the underlying borrowed asset to repay\r
     */\r
    function liquidateBorrowFresh(address liquidator, address borrower, uint repayAmount, CTokenInterface cTokenCollateral) internal {\r
        /* Fail if liquidate not allowed */\r
        uint allowed = comptroller.liquidateBorrowAllowed(address(this), address(cTokenCollateral), liquidator, borrower, repayAmount);\r
        if (allowed != 0) {\r
            revert LiquidateComptrollerRejection(allowed);\r
        }\r
\r
        /* Verify market's block number equals current block number */\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert LiquidateFreshnessCheck();\r
        }\r
\r
        /* Verify cTokenCollateral market's block number equals current block number */\r
        if (cTokenCollateral.accrualBlockNumber() != getBlockNumber()) {\r
            revert LiquidateCollateralFreshnessCheck();\r
        }\r
\r
        /* Fail if borrower = liquidator */\r
        if (borrower == liquidator) {\r
            revert LiquidateLiquidatorIsBorrower();\r
        }\r
\r
        /* Fail if repayAmount = 0 */\r
        if (repayAmount == 0) {\r
            revert LiquidateCloseAmountIsZero();\r
        }\r
\r
        /* Fail if repayAmount = -1 */\r
        if (repayAmount == type(uint).max) {\r
            revert LiquidateCloseAmountIsUintMax();\r
        }\r
\r
        /* Fail if repayBorrow fails */\r
        uint actualRepayAmount = repayBorrowFresh(liquidator, borrower, repayAmount);\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /* We calculate the number of collateral tokens that will be seized */\r
        (uint amountSeizeError, uint seizeTokens) = comptroller.liquidateCalculateSeizeTokens(address(this), address(cTokenCollateral), actualRepayAmount);\r
        require(amountSeizeError == NO_ERROR, "LIQUIDATE_COMPTROLLER_CALCULATE_AMOUNT_SEIZE_FAILED");\r
\r
        /* Revert if borrower collateral token balance < seizeTokens */\r
        require(cTokenCollateral.balanceOf(borrower) >= seizeTokens, "LIQUIDATE_SEIZE_TOO_MUCH");\r
\r
        // If this is also the collateral, run seizeInternal to avoid re-entrancy, otherwise make an external call\r
        if (address(cTokenCollateral) == address(this)) {\r
            seizeInternal(address(this), liquidator, borrower, seizeTokens);\r
        } else {\r
            require(cTokenCollateral.seize(liquidator, borrower, seizeTokens) == NO_ERROR, "token seizure failed");\r
        }\r
\r
        /* We emit a LiquidateBorrow event */\r
        emit LiquidateBorrow(liquidator, borrower, actualRepayAmount, address(cTokenCollateral), seizeTokens);\r
    }\r
\r
    /**\r
     * @notice Transfers collateral tokens (this market) to the liquidator.\r
     * @dev Will fail unless called by another cToken during the process of liquidation.\r
     *  Its absolutely critical to use msg.sender as the borrowed cToken and not a parameter.\r
     * @param liquidator The account receiving seized collateral\r
     * @param borrower The account having collateral seized\r
     * @param seizeTokens The number of cTokens to seize\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function seize(address liquidator, address borrower, uint seizeTokens) override external nonReentrant returns (uint) {\r
        seizeInternal(msg.sender, liquidator, borrower, seizeTokens);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Transfers collateral tokens (this market) to the liquidator.\r
     * @dev Called only during an in-kind liquidation, or by liquidateBorrow during the liquidation of another CToken.\r
     *  Its absolutely critical to use msg.sender as the seizer cToken and not a parameter.\r
     * @param seizerToken The contract seizing the collateral (i.e. borrowed cToken)\r
     * @param liquidator The account receiving seized collateral\r
     * @param borrower The account having collateral seized\r
     * @param seizeTokens The number of cTokens to seize\r
     */\r
    function seizeInternal(address seizerToken, address liquidator, address borrower, uint seizeTokens) internal {\r
        /* Fail if seize not allowed */\r
        uint allowed = comptroller.seizeAllowed(address(this), seizerToken, liquidator, borrower, seizeTokens);\r
        if (allowed != 0) {\r
            revert LiquidateSeizeComptrollerRejection(allowed);\r
        }\r
\r
        /* Fail if borrower = liquidator */\r
        if (borrower == liquidator) {\r
            revert LiquidateSeizeLiquidatorIsBorrower();\r
        }\r
\r
        /*\r
         * We calculate the new borrower and liquidator token balances, failing on underflow/overflow:\r
         *  borrowerTokensNew = accountTokens[borrower] - seizeTokens\r
         *  liquidatorTokensNew = accountTokens[liquidator] + seizeTokens\r
         */\r
        uint protocolSeizeTokens = mul_(seizeTokens, Exp({mantissa: protocolSeizeShareMantissa}));\r
        uint liquidatorSeizeTokens = seizeTokens - protocolSeizeTokens;\r
        Exp memory exchangeRate = Exp({mantissa: exchangeRateStoredInternal()});\r
        uint protocolSeizeAmount = mul_ScalarTruncate(exchangeRate, protocolSeizeTokens);\r
        uint totalReservesNew = totalReserves + protocolSeizeAmount;\r
\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /* We write the calculated values into storage */\r
        totalReserves = totalReservesNew;\r
        totalSupply = totalSupply - protocolSeizeTokens;\r
        accountTokens[borrower] = accountTokens[borrower] - seizeTokens;\r
        accountTokens[liquidator] = accountTokens[liquidator] + liquidatorSeizeTokens;\r
\r
        /* Emit a Transfer event */\r
        emit Transfer(borrower, liquidator, liquidatorSeizeTokens);\r
        emit Transfer(borrower, address(this), protocolSeizeTokens);\r
        emit ReservesAdded(address(this), protocolSeizeAmount, totalReservesNew);\r
    }\r
\r
\r
    /*** Admin Functions ***/\r
\r
    /**\r
      * @notice Begins transfer of admin rights. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.\r
      * @dev Admin function to begin change of admin. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.\r
      * @param newPendingAdmin New pending admin.\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _setPendingAdmin(address payable newPendingAdmin) override external returns (uint) {\r
        // Check caller = admin\r
        if (msg.sender != admin) {\r
            revert SetPendingAdminOwnerCheck();\r
        }\r
\r
        // Save current value, if any, for inclusion in log\r
        address oldPendingAdmin = pendingAdmin;\r
\r
        // Store pendingAdmin with value newPendingAdmin\r
        pendingAdmin = newPendingAdmin;\r
\r
        // Emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin)\r
        emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
      * @notice Accepts transfer of admin rights. msg.sender must be pendingAdmin\r
      * @dev Admin function for pending admin to accept role and update admin\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _acceptAdmin() override external returns (uint) {\r
        // Check caller is pendingAdmin and pendingAdmin ≠ address(0)\r
        if (msg.sender != pendingAdmin || msg.sender == address(0)) {\r
            revert AcceptAdminPendingAdminCheck();\r
        }\r
\r
        // Save current values for inclusion in log\r
        address oldAdmin = admin;\r
        address oldPendingAdmin = pendingAdmin;\r
\r
        // Store admin with value pendingAdmin\r
        admin = pendingAdmin;\r
\r
        // Clear the pending value\r
        pendingAdmin = payable(address(0));\r
\r
        emit NewAdmin(oldAdmin, admin);\r
        emit NewPendingAdmin(oldPendingAdmin, pendingAdmin);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
      * @notice Sets a new comptroller for the market\r
      * @dev Admin function to set a new comptroller\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _setComptroller(ComptrollerInterface newComptroller) override public returns (uint) {\r
        // Check caller is admin\r
        if (msg.sender != admin) {\r
            revert SetComptrollerOwnerCheck();\r
        }\r
\r
        ComptrollerInterface oldComptroller = comptroller;\r
        // Ensure invoke comptroller.isComptroller() returns true\r
        require(newComptroller.isComptroller(), "marker method returned false");\r
\r
        // Set market's comptroller to newComptroller\r
        comptroller = newComptroller;\r
\r
        // Emit NewComptroller(oldComptroller, newComptroller)\r
        emit NewComptroller(oldComptroller, newComptroller);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
      * @notice accrues interest and sets a new reserve factor for the protocol using _setReserveFactorFresh\r
      * @dev Admin function to accrue interest and set a new reserve factor\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _setReserveFactor(uint newReserveFactorMantissa) override external nonReentrant returns (uint) {\r
        accrueInterest();\r
        // _setReserveFactorFresh emits reserve-factor-specific logs on errors, so we don't need to.\r
        return _setReserveFactorFresh(newReserveFactorMantissa);\r
    }\r
\r
    /**\r
      * @notice Sets a new reserve factor for the protocol (*requires fresh interest accrual)\r
      * @dev Admin function to set a new reserve factor\r
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function _setReserveFactorFresh(uint newReserveFactorMantissa) internal returns (uint) {\r
        // Check caller is admin\r
        if (msg.sender != admin) {\r
            revert SetReserveFactorAdminCheck();\r
        }\r
\r
        // Verify market's block number equals current block number\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert SetReserveFactorFreshCheck();\r
        }\r
\r
        // Check newReserveFactor ≤ maxReserveFactor\r
        if (newReserveFactorMantissa > reserveFactorMaxMantissa) {\r
            revert SetReserveFactorBoundsCheck();\r
        }\r
\r
        uint oldReserveFactorMantissa = reserveFactorMantissa;\r
        reserveFactorMantissa = newReserveFactorMantissa;\r
\r
        emit NewReserveFactor(oldReserveFactorMantissa, newReserveFactorMantissa);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Accrues interest and reduces reserves by transferring from msg.sender\r
     * @param addAmount Amount of addition to reserves\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _addReservesInternal(uint addAmount) internal nonReentrant returns (uint) {\r
        accrueInterest();\r
\r
        // _addReservesFresh emits reserve-addition-specific logs on errors, so we don't need to.\r
        _addReservesFresh(addAmount);\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice Add reserves by transferring from caller\r
     * @dev Requires fresh interest accrual\r
     * @param addAmount Amount of addition to reserves\r
     * @return (uint, uint) An error code (0=success, otherwise a failure (see ErrorReporter.sol for details)) and the actual amount added, net token fees\r
     */\r
    function _addReservesFresh(uint addAmount) internal returns (uint, uint) {\r
        // totalReserves + actualAddAmount\r
        uint totalReservesNew;\r
        uint actualAddAmount;\r
\r
        // We fail gracefully unless market's block number equals current block number\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert AddReservesFactorFreshCheck(actualAddAmount);\r
        }\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        /*\r
         * We call doTransferIn for the caller and the addAmount\r
         *  Note: The cToken must handle variations between ERC-20 and ETH underlying.\r
         *  On success, the cToken holds an additional addAmount of cash.\r
         *  doTransferIn reverts if anything goes wrong, since we can't be sure if side effects occurred.\r
         *  it returns the amount actually transferred, in case of a fee.\r
         */\r
\r
        actualAddAmount = doTransferIn(msg.sender, addAmount);\r
\r
        totalReservesNew = totalReserves + actualAddAmount;\r
\r
        // Store reserves[n+1] = reserves[n] + actualAddAmount\r
        totalReserves = totalReservesNew;\r
\r
        /* Emit NewReserves(admin, actualAddAmount, reserves[n+1]) */\r
        emit ReservesAdded(msg.sender, actualAddAmount, totalReservesNew);\r
\r
        /* Return (NO_ERROR, actualAddAmount) */\r
        return (NO_ERROR, actualAddAmount);\r
    }\r
\r
\r
    /**\r
     * @notice Accrues interest and reduces reserves by transferring to admin\r
     * @param reduceAmount Amount of reduction to reserves\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _reduceReserves(uint reduceAmount) override external nonReentrant returns (uint) {\r
        accrueInterest();\r
        // _reduceReservesFresh emits reserve-reduction-specific logs on errors, so we don't need to.\r
        return _reduceReservesFresh(reduceAmount);\r
    }\r
\r
    /**\r
     * @notice Reduces reserves by transferring to admin\r
     * @dev Requires fresh interest accrual\r
     * @param reduceAmount Amount of reduction to reserves\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _reduceReservesFresh(uint reduceAmount) internal returns (uint) {\r
        // totalReserves - reduceAmount\r
        uint totalReservesNew;\r
\r
        // Check caller is admin\r
        if (msg.sender != admin) {\r
            revert ReduceReservesAdminCheck();\r
        }\r
\r
        // We fail gracefully unless market's block number equals current b

Tags:
ERC20, Proxy, Mintable, Liquidity, Voting, Upgradeable, Factory, Oracle|addr:0xd553d107181087b114bbf9e07cdc821e13233f5c|verified:true|block:23731452|tx:0x2e760d033c99fb3a72fea07f3e07e46e168d07f5a433e2ea30bff0c32f385142|first_check:1762345757

Submitted on: 2025-11-05 13:29:18

Comments

Log in to comment.

No comments yet.