CErc20Immutable

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/CErc20Immutable.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./CErc20.sol";\r
\r
/**\r
 * @title Compound's CErc20Immutable Contract\r
 * @notice CTokens which wrap an EIP-20 underlying and are immutable\r
 * @author Compound\r
 */\r
contract CErc20Immutable is CErc20 {\r
    /**\r
     * @notice Construct a new money market\r
     * @param underlying_ The address of the underlying asset\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_ ERC-20 name of this token\r
     * @param symbol_ ERC-20 symbol of this token\r
     * @param decimals_ ERC-20 decimal precision of this token\r
     * @param admin_ Address of the administrator of this token\r
     */\r
    constructor(address underlying_,\r
                ComptrollerInterface comptroller_,\r
                InterestRateModel interestRateModel_,\r
                uint initialExchangeRateMantissa_,\r
                string memory name_,\r
                string memory symbol_,\r
                uint8 decimals_,\r
                address payable admin_) {\r
        // Creator of the contract is admin during initialization\r
        admin = payable(msg.sender);\r
\r
        // Initialize the market\r
        initialize(underlying_, comptroller_, interestRateModel_, initialExchangeRateMantissa_, name_, symbol_, decimals_);\r
\r
        // Set the proper admin now that initialization is done\r
        admin = admin_;\r
    }\r
}\r
"
    },
    "contracts/CErc20.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r
pragma solidity ^0.8.10;\r
\r
import "./CToken.sol";\r
\r
interface CompLike {\r
    function delegate(address delegatee) external;\r
}\r
\r
/**\r
 * @title Compound's CErc20 Contract\r
 * @notice CTokens which wrap an EIP-20 underlying\r
 * @author Compound\r
 */\r
contract CErc20 is CToken, CErc20Interface {\r
    /**\r
     * @notice Initialize the new money market\r
     * @param underlying_ The address of the underlying asset\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_ ERC-20 name of this token\r
     * @param symbol_ ERC-20 symbol of this token\r
     * @param decimals_ ERC-20 decimal precision of this token\r
     */\r
    function initialize(address underlying_,\r
                        ComptrollerInterface comptroller_,\r
                        InterestRateModel interestRateModel_,\r
                        uint initialExchangeRateMantissa_,\r
                        string memory name_,\r
                        string memory symbol_,\r
                        uint8 decimals_) public {\r
        // CToken initialize does the bulk of the work\r
        super.initialize(comptroller_, interestRateModel_, initialExchangeRateMantissa_, name_, symbol_, decimals_);\r
\r
        // Set underlying and sanity check it\r
        underlying = underlying_;\r
        EIP20Interface(underlying).totalSupply();\r
    }\r
\r
    /*** User Interface ***/\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
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function mint(uint mintAmount) override external returns (uint) {\r
        mintInternal(mintAmount);\r
        return NO_ERROR;\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
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function redeem(uint redeemTokens) override external returns (uint) {\r
        redeemInternal(redeemTokens);\r
        return NO_ERROR;\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 redeem\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function redeemUnderlying(uint redeemAmount) override external returns (uint) {\r
        redeemUnderlyingInternal(redeemAmount);\r
        return NO_ERROR;\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
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
      */\r
    function borrow(uint borrowAmount) override external returns (uint) {\r
        borrowInternal(borrowAmount);\r
        return NO_ERROR;\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
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function repayBorrow(uint repayAmount) override external returns (uint) {\r
        repayBorrowInternal(repayAmount);\r
        return NO_ERROR;\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
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function repayBorrowBehalf(address borrower, uint repayAmount) override external returns (uint) {\r
        repayBorrowBehalfInternal(borrower, repayAmount);\r
        return NO_ERROR;\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 repayAmount The amount of the underlying borrowed asset to repay\r
     * @param cTokenCollateral The market in which to seize collateral from the borrower\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function liquidateBorrow(address borrower, uint repayAmount, CTokenInterface cTokenCollateral) override external returns (uint) {\r
        liquidateBorrowInternal(borrower, repayAmount, cTokenCollateral);\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice A public function to sweep accidental ERC-20 transfers to this contract. Tokens are sent to admin (timelock)\r
     * @param token The address of the ERC-20 token to sweep\r
     */\r
    function sweepToken(EIP20NonStandardInterface token) override external {\r
        require(msg.sender == admin, "CErc20::sweepToken: only admin can sweep tokens");\r
        require(address(token) != underlying, "CErc20::sweepToken: can not sweep underlying token");\r
        uint256 balance = token.balanceOf(address(this));\r
        token.transfer(admin, balance);\r
    }\r
\r
    /**\r
     * @notice The sender adds to reserves.\r
     * @param addAmount The amount fo underlying token to add as reserves\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _addReserves(uint addAmount) override external returns (uint) {\r
        return _addReservesInternal(addAmount);\r
    }\r
\r
    /*** Safe Token ***/\r
\r
    /**\r
     * @notice Gets balance of this contract in terms of the underlying\r
     * @dev This excludes the value of the current message, if any\r
     * @return The quantity of underlying tokens owned by this contract\r
     */\r
    function getCashPrior() virtual override internal view returns (uint) {\r
        EIP20Interface token = EIP20Interface(underlying);\r
        return token.balanceOf(address(this));\r
    }\r
\r
    /**\r
     * @dev Similar to EIP20 transfer, except it handles a False result from `transferFrom` and reverts in that case.\r
     *      This will revert due to insufficient balance or insufficient allowance.\r
     *      This function returns the actual amount received,\r
     *      which may be less than `amount` if there is a fee attached to the transfer.\r
     *\r
     *      Note: This wrapper safely handles non-standard ERC-20 tokens that do not return a value.\r
     *            See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca\r
     */\r
    function doTransferIn(address from, uint amount) virtual override internal returns (uint) {\r
        // Read from storage once\r
        address underlying_ = underlying;\r
        EIP20NonStandardInterface token = EIP20NonStandardInterface(underlying_);\r
        uint balanceBefore = EIP20Interface(underlying_).balanceOf(address(this));\r
        token.transferFrom(from, address(this), amount);\r
\r
        bool success;\r
        assembly {\r
            switch returndatasize()\r
                case 0 {                       // This is a non-standard ERC-20\r
                    success := not(0)          // set success to true\r
                }\r
                case 32 {                      // This is a compliant ERC-20\r
                    returndatacopy(0, 0, 32)\r
                    success := mload(0)        // Set `success = returndata` of override external call\r
                }\r
                default {                      // This is an excessively non-compliant ERC-20, revert.\r
                    revert(0, 0)\r
                }\r
        }\r
        require(success, "TOKEN_TRANSFER_IN_FAILED");\r
\r
        // Calculate the amount that was *actually* transferred\r
        uint balanceAfter = EIP20Interface(underlying_).balanceOf(address(this));\r
        return balanceAfter - balanceBefore;   // underflow already checked above, just subtract\r
    }\r
\r
    /**\r
     * @dev Similar to EIP20 transfer, except it handles a False success from `transfer` and returns an explanatory\r
     *      error code rather than reverting. If caller has not called checked protocol's balance, this may revert due to\r
     *      insufficient cash held in this contract. If caller has checked protocol's balance prior to this call, and verified\r
     *      it is >= amount, this should not revert in normal conditions.\r
     *\r
     *      Note: This wrapper safely handles non-standard ERC-20 tokens that do not return a value.\r
     *            See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca\r
     */\r
    function doTransferOut(address payable to, uint amount) virtual override internal {\r
        EIP20NonStandardInterface token = EIP20NonStandardInterface(underlying);\r
        token.transfer(to, amount);\r
\r
        bool success;\r
        assembly {\r
            switch returndatasize()\r
                case 0 {                      // This is a non-standard ERC-20\r
                    success := not(0)          // set success to true\r
                }\r
                case 32 {                     // This is a compliant ERC-20\r
                    returndatacopy(0, 0, 32)\r
                    success := mload(0)        // Set `success = returndata` of override external call\r
                }\r
                default {                     // This is an excessively non-compliant ERC-20, revert.\r
                    revert(0, 0)\r
                }\r
        }\r
        require(success, "TOKEN_TRANSFER_OUT_FAILED");\r
    }\r
\r
    /**\r
    * @notice Admin call to delegate the votes of the COMP-like underlying\r
    * @param compLikeDelegatee The address to delegate votes to\r
    * @dev CTokens whose underlying are not CompLike should revert here\r
    */\r
    function _delegateCompLikeTo(address compLikeDelegatee) external {\r
        require(msg.sender == admin, "only the admin may set the comp-like delegate");\r
        CompLike(underlying).delegate(compLikeDelegatee);\r
    }\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 block number\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert ReduceReservesFreshCheck();\r
        }\r
\r
        // Fail gracefully if protocol has insufficient underlying cash\r
        if (getCashPrior() < reduceAmount) {\r
            revert ReduceReservesCashNotAvailable();\r
        }\r
\r
        // Check reduceAmount ≤ reserves[n] (totalReserves)\r
        if (reduceAmount > totalReserves) {\r
            revert ReduceReservesCashValidation();\r
        }\r
\r
        /////////////////////////\r
        // EFFECTS & INTERACTIONS\r
        // (No safe failures beyond this point)\r
\r
        totalReservesNew = totalReserves - reduceAmount;\r
\r
        // Store reserves[n+1] = reserves[n] - reduceAmount\r
        totalReserves = totalReservesNew;\r
\r
        // doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.\r
        doTransferOut(admin, reduceAmount);\r
\r
        emit ReservesReduced(admin, reduceAmount, totalReservesNew);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /**\r
     * @notice accrues interest and updates the interest rate model using _setInterestRateModelFresh\r
     * @dev Admin function to accrue interest and update the interest rate model\r
     * @param newInterestRateModel the new interest rate model to use\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _setInterestRateModel(InterestRateModel newInterestRateModel) override public returns (uint) {\r
        accrueInterest();\r
        // _setInterestRateModelFresh emits interest-rate-model-update-specific logs on errors, so we don't need to.\r
        return _setInterestRateModelFresh(newInterestRateModel);\r
    }\r
\r
    /**\r
     * @notice updates the interest rate model (*requires fresh interest accrual)\r
     * @dev Admin function to update the interest rate model\r
     * @param newInterestRateModel the new interest rate model to use\r
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)\r
     */\r
    function _setInterestRateModelFresh(InterestRateModel newInterestRateModel) internal returns (uint) {\r
\r
        // Used to store old model for use in the event that is emitted on success\r
        InterestRateModel oldInterestRateModel;\r
\r
        // Check caller is admin\r
        if (msg.sender != admin) {\r
            revert SetInterestRateModelOwnerCheck();\r
        }\r
\r
        // We fail gracefully unless market's block number equals current block number\r
        if (accrualBlockNumber != getBlockNumber()) {\r
            revert SetInterestRateModelFreshCheck();\r
        }\r
\r
        // Track the market's current interest rate model\r
        oldInterestRateModel = interestRateModel;\r
\r
        // Ensure invoke newInterestRateModel.isInterestRateModel() returns true\r
        require(newInterestRateModel.isInterestRateModel(), "marker method returned false");\r
\r
        // Set the interest rate model to newInterestRateModel\r
        interestRateModel = newInterestRateModel;\r
\r
        // Emit NewMarketInterestRateModel(oldInterestRateModel, newInterestRateModel)\r
        emit NewMarketInterestRateModel(oldInterestRateModel, newInterestRateModel);\r
\r
        return NO_ERROR;\r
    }\r
\r
    /*** Safe Token ***/\r
\r
    /**\r
     * @notice Gets balance of this contract in terms of the underlying\r
     * @dev This excludes the value of the current message, if any\r
     * @return The quantity of underlying owned by this contract\r
     */\r
    function getCashPrior() virtual internal view returns (uint);\r
\r
    /**\r
     * @dev Performs a transfer in, reverting upon failure. Returns the amount actually transferred to the protocol, in case of a fee.\r
     *  This may revert due to insufficient balance or insufficient allowance.\r
     */\r
    function doTransferIn(address from, uint amount) virtual internal returns (uint);\r
\r
    /**\r
     * @dev Performs a transfer out, ideally returning an explanatory error code upon failure rather than reverting.\r
     *  If caller has not called checked protocol's balance, may revert due to insufficient cash held in the contract.\r
     *  If caller has checked protocol's balance, and verified it is >= amount, this should not revert in normal conditions.\r
     */\r
    function doTransferOut(address payable to, uint amount) virtual internal;\r
\r
\r
    /*** Reentrancy Guard ***/\r
\r
    /**\r
     * @dev Prevents a contract from calling itself, directly or indirectly.\r
     */\r
    modifier nonReentrant() {\r
        require(_notEntered, "re-entered");\r
        _notEntered

Tags:
ERC20, Proxy, Mintable, Liquidity, Voting, Timelock, Upgradeable, Factory|addr:0x3bef807ac66522d7759caf07b668a7eb00e1d00e|verified:true|block:23731490|tx:0xebff941b4a20329967beec0aaaae669eb1fbce531d71592a2b5079d6782cd3e7|first_check:1762345854

Submitted on: 2025-11-05 13:30:56

Comments

Log in to comment.

No comments yet.