ETH Price: $2,470.49 (-20.79%)
 

Overview

Max Total Supply

62.16984615 mWETH

Holders

15

Market

Onchain Market Cap

$0.00

Circulating Supply Market Cap

-

Other Info

Token Contract (WITH 8 Decimals)

Balance
9.99984701 mWETH

Value
$0.00
0x599e88b0194968c9891f6c7588c9059d4a397ace
Loading...
Loading
Loading...
Loading
Loading...
Loading

Click here to update the token information / general information
# Exchange Pair Price  24H Volume % Volume

Contract Source Code Verified (Exact Match)

Contract Name:
MWeth

Compiler Version
v0.6.12+commit.27d51765

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
File 1 of 53 : MWeth.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./MToken.sol";
import "./Moartroller.sol";
import "./AbstractInterestRateModel.sol";
import "./Interfaces/WETHInterface.sol";
import "./Interfaces/EIP20Interface.sol";
import "./Utils/SafeEIP20.sol";

/**
 * @title MOAR's MEther Contract
 * @notice MToken which wraps Ether
 * @author MOAR
 */
contract MWeth is MToken {

    using SafeEIP20 for EIP20Interface;

    /**
     * @notice Construct a new MEther money market
     * @param underlying_ The address of the underlying asset
     * @param moartroller_ The address of the Moartroller
     * @param interestRateModel_ The address of the interest rate model
     * @param initialExchangeRateMantissa_ The initial exchange rate, scaled by 1e18
     * @param name_ ERC-20 name of this token
     * @param symbol_ ERC-20 symbol of this token
     * @param decimals_ ERC-20 decimal precision of this token
     * @param admin_ Address of the administrator of this token
     */
    constructor(address underlying_,
                Moartroller moartroller_,
                AbstractInterestRateModel interestRateModel_,
                uint initialExchangeRateMantissa_,
                string memory name_,
                string memory symbol_,
                uint8 decimals_,
                address payable admin_) public {
        // Creator of the contract is admin during initialization
        admin = msg.sender;

        init(moartroller_, interestRateModel_, initialExchangeRateMantissa_, name_, symbol_, decimals_);

        // Set underlying and sanity check it
        underlying = underlying_;
        WETHInterface(underlying).totalSupply();

        // Set the proper admin now that initialization is done
        admin = admin_;
    }


    /*** User Interface ***/

    /**
     * @notice Sender supplies assets into the market and receives mTokens in exchange
     * @dev Reverts upon any failure
     */
    function mint() external payable {
        WETHInterface(underlying).deposit{value : msg.value}();
        (uint err,) = mintInternal(msg.value);
        requireNoError(err, "mint failed");
    }

    /**
     * @notice Sender redeems mTokens in exchange for the underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemTokens The number of mTokens to redeem into underlying
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeem(uint redeemTokens) external returns (uint) {
        return redeemInternal(redeemTokens);
    }

    /**
     * @notice Sender redeems mTokens in exchange for a specified amount of underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemAmount The amount of underlying to redeem
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeemUnderlying(uint redeemAmount) external returns (uint) {
        return redeemUnderlyingInternal(redeemAmount);
    }

    /**
      * @notice Sender borrows assets from the protocol to their own address
      * @param borrowAmount The amount of the underlying asset to borrow
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function borrow(uint borrowAmount) external returns (uint) {
        return borrowInternal(borrowAmount);
    }

    function borrowFor(address payable borrower, uint borrowAmount) external returns (uint) {
        return borrowForInternal(borrower, borrowAmount);
    }

    /**
     * @notice Sender repays their own borrow
     * @dev Reverts upon any failure
     */
    function repayBorrow() external payable {
        WETHInterface(underlying).deposit{value : msg.value}();
        (uint err,) = repayBorrowInternal(msg.value);
        requireNoError(err, "repayBorrow failed");
    }

    /**
     * @notice Sender repays a borrow belonging to borrower
     * @dev Reverts upon any failure
     * @param borrower the account with the debt being payed off
     */
    function repayBorrowBehalf(address borrower) external payable {
        WETHInterface(underlying).deposit{value : msg.value}();
        (uint err,) = repayBorrowBehalfInternal(borrower, msg.value);
        requireNoError(err, "repayBorrowBehalf failed");
    }

    /**
     * @notice The sender liquidates the borrowers collateral.
     *  The collateral seized is transferred to the liquidator.
     * @dev Reverts upon any failure
     * @param borrower The borrower of this mToken to be liquidated
     * @param mTokenCollateral The market in which to seize collateral from the borrower
     */
    function liquidateBorrow(address borrower, MToken mTokenCollateral) external payable {
        WETHInterface(underlying).deposit{value : msg.value}();
        (uint err,) = liquidateBorrowInternal(borrower, msg.value, mTokenCollateral);
        requireNoError(err, "liquidateBorrow failed");
    }

    /**
     * @notice The sender adds to reserves.
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _addReserves() external payable returns (uint) {
        WETHInterface(underlying).deposit{value : msg.value}();
        return _addReservesInternal(msg.value);
    }

    /**
     * @notice Send Ether to MEther to mint
     */
    receive () external payable {
        if(msg.sender != underlying){
             WETHInterface(underlying).deposit{value : msg.value}();
            (uint err,) = mintInternal(msg.value);
            requireNoError(err, "mint failed");
        }
    }

    /**
     * @notice A public function to sweep accidental ERC-20 transfers to this contract. Tokens are sent to admin (timelock)
     * @param token The address of the ERC-20 token to sweep
     */
    function sweepToken(EIP20Interface token) override external {
    	require(address(token) != underlying, "MErc20::sweepToken: can not sweep underlying token");
    	uint256 balance = token.balanceOf(address(this));
    	token.safeTransfer(admin, balance);
    }

    /*** Safe Token ***/

    /**
     * @notice Gets balance of this contract in terms of the underlying
     * @dev This excludes the value of the current message, if any
     * @return The quantity of underlying tokens owned by this contract
     */
    function getCashPrior() internal override view returns (uint) {
        EIP20Interface token = EIP20Interface(underlying);
        (MathError err, uint startingBalance) = subUInt(token.balanceOf(address(this)), msg.value);
        require(err == MathError.NO_ERROR);
        return startingBalance;
    }

    /**
     * @notice Perform the actual transfer in, which is a no-op
     * @param from Address sending the Ether
     * @param amount Amount of Ether being sent
     * @return The actual amount of Ether transferred
     */
    function doTransferIn(address from, uint amount) internal override returns (uint) {
        // Sanity checks
        require(msg.sender == from, "sender mismatch");
        require(msg.value == amount, "value mismatch");
        return amount;
    }

    /**
     * @notice Perform the transfer out
     * @param to Reciever address
     * @param amount Amount of Ether being sent
     */
    function doTransferOut(address payable to, uint amount) internal override {
        /* Send the Ether, with minimal gas and revert on failure */
        WETHInterface(underlying).withdraw(amount);
        to.transfer(amount);
    }

    function requireNoError(uint errCode, string memory message) internal pure {
        if (errCode == uint(Error.NO_ERROR)) {
            return;
        }

        bytes memory fullMessage = new bytes(bytes(message).length + 5);
        uint i;

        for (i = 0; i < bytes(message).length; i++) {
            fullMessage[i] = bytes(message)[i];
        }

        fullMessage[i+0] = byte(uint8(32));
        fullMessage[i+1] = byte(uint8(40));
        fullMessage[i+2] = byte(uint8(48 + ( errCode / 10 )));
        fullMessage[i+3] = byte(uint8(48 + ( errCode % 10 )));
        fullMessage[i+4] = byte(uint8(41));

        require(errCode == uint(Error.NO_ERROR), string(fullMessage));
    }
}

File 2 of 53 : MToken.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./Utils/ErrorReporter.sol";
import "./Utils/Exponential.sol";
import "./Interfaces/EIP20Interface.sol";
import "./MTokenStorage.sol";
import "./Interfaces/MTokenInterface.sol";
import "./Interfaces/MProxyInterface.sol";
import "./Moartroller.sol";
import "./AbstractInterestRateModel.sol";

/**
 * @title MOAR's MToken Contract
 * @notice Abstract base for MTokens
 * @author MOAR
 */
abstract contract MToken is MTokenInterface, Exponential, TokenErrorReporter, MTokenStorage {
    /**
     * @notice Indicator that this is a MToken contract (for inspection)
     */
    bool public constant isMToken = true;


    /*** Market Events ***/

    /**
     * @notice Event emitted when interest is accrued
     */
    event AccrueInterest(uint cashPrior, uint interestAccumulated, uint borrowIndex, uint totalBorrows);

    /**
     * @notice Event emitted when tokens are minted
     */
    event Mint(address minter, uint mintAmount, uint mintTokens);

    /**
     * @notice Event emitted when tokens are redeemed
     */
    event Redeem(address redeemer, uint redeemAmount, uint redeemTokens);

    /**
     * @notice Event emitted when underlying is borrowed
     */
    event Borrow(address borrower, uint borrowAmount, uint accountBorrows, uint totalBorrows);

    /**
     * @notice Event emitted when a borrow is repaid
     */
    event RepayBorrow(address payer, address borrower, uint repayAmount, uint accountBorrows, uint totalBorrows);

    /**
     * @notice Event emitted when a borrow is liquidated
     */
    event LiquidateBorrow(address liquidator, address borrower, uint repayAmount, address MTokenCollateral, uint seizeTokens);


    /*** Admin Events ***/

    /**
     * @notice Event emitted when pendingAdmin is changed
     */
    event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin);

    /**
     * @notice Event emitted when pendingAdmin is accepted, which means admin is updated
     */
    event NewAdmin(address oldAdmin, address newAdmin);

    /**
     * @notice Event emitted when moartroller is changed
     */
    event NewMoartroller(Moartroller oldMoartroller, Moartroller newMoartroller);

    /**
     * @notice Event emitted when interestRateModel is changed
     */
    event NewMarketInterestRateModel(InterestRateModelInterface oldInterestRateModel, InterestRateModelInterface newInterestRateModel);

    /**
     * @notice Event emitted when the reserve factor is changed
     */
    event NewReserveFactor(uint oldReserveFactorMantissa, uint newReserveFactorMantissa);

    /**
     * @notice Event emitted when the reserves are added
     */
    event ReservesAdded(address benefactor, uint addAmount, uint newTotalReserves);

    /**
     * @notice Event emitted when the reserves are reduced
     */
    event ReservesReduced(address admin, uint reduceAmount, uint newTotalReserves);

    /**
     * @notice EIP20 Transfer event
     */
    event Transfer(address indexed from, address indexed to, uint amount);

    /**
     * @notice EIP20 Approval event
     */
    event Approval(address indexed owner, address indexed spender, uint amount);

    /**
     * @notice Failure event
     */
    event Failure(uint error, uint info, uint detail);

    /**
     * @notice Max protection composition value updated event 
     */
    event MpcUpdated(uint newValue);

    /**
     * @notice Initialize the money market
     * @param moartroller_ The address of the Moartroller
     * @param interestRateModel_ The address of the interest rate model
     * @param initialExchangeRateMantissa_ The initial exchange rate, scaled by 1e18
     * @param name_ EIP-20 name of this token
     * @param symbol_ EIP-20 symbol of this token
     * @param decimals_ EIP-20 decimal precision of this token
     */
    function init(Moartroller moartroller_,
                        AbstractInterestRateModel interestRateModel_,
                        uint initialExchangeRateMantissa_,
                        string memory name_,
                        string memory symbol_,
                        uint8 decimals_) public {
        require(msg.sender == admin, "not_admin");
        require(accrualBlockNumber == 0 && borrowIndex == 0, "already_init");

        // Set initial exchange rate
        initialExchangeRateMantissa = initialExchangeRateMantissa_;
        require(initialExchangeRateMantissa > 0, "too_low");

        // Set the moartroller
        uint err = _setMoartroller(moartroller_);
        require(err == uint(Error.NO_ERROR), "setting moartroller failed");

        // Initialize block number and borrow index (block number mocks depend on moartroller being set)
        accrualBlockNumber = getBlockNumber();
        borrowIndex = mantissaOne;

        // Set the interest rate model (depends on block number / borrow index)
        err = _setInterestRateModelFresh(interestRateModel_);
        require(err == uint(Error.NO_ERROR), "setting IRM failed");

        name = name_;
        symbol = symbol_;
        decimals = decimals_;

        // The counter starts true to prevent changing it from zero to non-zero (i.e. smaller cost/refund)
        _notEntered = true;

        maxProtectionComposition = 5000;
        maxProtectionCompositionMantissa = 1e4;
        reserveFactorMaxMantissa = 1e18;
        borrowRateMaxMantissa = 0.0005e16;
    }

    /**
     * @notice Transfer `tokens` tokens from `src` to `dst` by `spender`
     * @dev Called by both `transfer` and `transferFrom` internally
     * @param spender The address of the account performing the transfer
     * @param src The address of the source account
     * @param dst The address of the destination account
     * @param tokens The number of tokens to transfer
     * @return Whether or not the transfer succeeded
     */
    function transferTokens(address spender, address src, address dst, uint tokens) internal returns (uint) {
        /* Fail if transfer not allowed */
        uint allowed = moartroller.transferAllowed(address(this), src, dst, tokens);
        if (allowed != 0) {
            return failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.TRANSFER_MOARTROLLER_REJECTION, allowed);
        }

        /* Do not allow self-transfers */
        if (src == dst) {
            return fail(Error.BAD_INPUT, FailureInfo.TRANSFER_NOT_ALLOWED);
        }

        /* Get the allowance, infinite for the account owner */
        uint startingAllowance = 0;
        if (spender == src) {
            startingAllowance = uint(-1);
        } else {
            startingAllowance = transferAllowances[src][spender];
        }

        /* Do the calculations, checking for {under,over}flow */
        MathError mathErr;
        uint allowanceNew;
        uint srmTokensNew;
        uint dstTokensNew;

        (mathErr, allowanceNew) = subUInt(startingAllowance, tokens);
        if (mathErr != MathError.NO_ERROR) {
            return fail(Error.MATH_ERROR, FailureInfo.TRANSFER_NOT_ALLOWED);
        }

        (mathErr, srmTokensNew) = subUInt(accountTokens[src], tokens);
        if (mathErr != MathError.NO_ERROR) {
            return fail(Error.MATH_ERROR, FailureInfo.TRANSFER_NOT_ENOUGH);
        }

        (mathErr, dstTokensNew) = addUInt(accountTokens[dst], tokens);
        if (mathErr != MathError.NO_ERROR) {
            return fail(Error.MATH_ERROR, FailureInfo.TRANSFER_TOO_MUCH);
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        accountTokens[src] = srmTokensNew;
        accountTokens[dst] = dstTokensNew;

        /* Eat some of the allowance (if necessary) */
        if (startingAllowance != uint(-1)) {
            transferAllowances[src][spender] = allowanceNew;
        }

        /* We emit a Transfer event */
        emit Transfer(src, dst, tokens);

        // unused function
        // moartroller.transferVerify(address(this), src, dst, tokens);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Transfer `amount` tokens from `msg.sender` to `dst`
     * @param dst The address of the destination account
     * @param amount The number of tokens to transfer
     * @return Whether or not the transfer succeeded
     */
    function transfer(address dst, uint256 amount) external virtual override nonReentrant returns (bool) {
        return transferTokens(msg.sender, msg.sender, dst, amount) == uint(Error.NO_ERROR);
    }

    /**
     * @notice Transfer `amount` tokens from `src` to `dst`
     * @param src The address of the source account
     * @param dst The address of the destination account
     * @param amount The number of tokens to transfer
     * @return Whether or not the transfer succeeded
     */
    function transferFrom(address src, address dst, uint256 amount) external virtual override nonReentrant returns (bool) {
        return transferTokens(msg.sender, src, dst, amount) == uint(Error.NO_ERROR);
    }

    /**
     * @notice Approve `spender` to transfer up to `amount` from `src`
     * @dev This will overwrite the approval amount for `spender`
     *  and is subject to issues noted [here](https://eips.ethereum.org/EIPS/eip-20#approve)
     * @param spender The address of the account which may transfer tokens
     * @param amount The number of tokens that are approved (-1 means infinite)
     * @return Whether or not the approval succeeded
     */
    function approve(address spender, uint256 amount) external virtual override returns (bool) {
        address src = msg.sender;
        transferAllowances[src][spender] = amount;
        emit Approval(src, spender, amount);
        return true;
    }

    /**
     * @notice Get the current allowance from `owner` for `spender`
     * @param owner The address of the account which owns the tokens to be spent
     * @param spender The address of the account which may transfer tokens
     * @return The number of tokens allowed to be spent (-1 means infinite)
     */
    function allowance(address owner, address spender) external virtual override view returns (uint256) {
        return transferAllowances[owner][spender];
    }

    /**
     * @notice Get the token balance of the `owner`
     * @param owner The address of the account to query
     * @return The number of tokens owned by `owner`
     */
    function balanceOf(address owner) external virtual override view returns (uint256) {
        return accountTokens[owner];
    }

    /**
     * @notice Get the underlying balance of the `owner`
     * @dev This also accrues interest in a transaction
     * @param owner The address of the account to query
     * @return The amount of underlying owned by `owner`
     */
    function balanceOfUnderlying(address owner) external virtual override returns (uint) {
        Exp memory exchangeRate = Exp({mantissa: exchangeRateCurrent()});
        (MathError mErr, uint balance) = mulScalarTruncate(exchangeRate, accountTokens[owner]);
        require(mErr == MathError.NO_ERROR, "balance_calculation_failed");
        return balance;
    }

    /**
     * @notice Get a snapshot of the account's balances, and the cached exchange rate
     * @dev This is used by moartroller to more efficiently perform liquidity checks.
     * @param account Address of the account to snapshot
     * @return (possible error, token balance, borrow balance, exchange rate mantissa)
     */
    function getAccountSnapshot(address account) external virtual override view returns (uint, uint, uint, uint) {
        uint mTokenBalance = accountTokens[account];
        uint borrowBalance;
        uint exchangeRateMantissa;

        MathError mErr;

        (mErr, borrowBalance) = borrowBalanceStoredInternal(account);
        if (mErr != MathError.NO_ERROR) {
            return (uint(Error.MATH_ERROR), 0, 0, 0);
        }

        (mErr, exchangeRateMantissa) = exchangeRateStoredInternal();
        if (mErr != MathError.NO_ERROR) {
            return (uint(Error.MATH_ERROR), 0, 0, 0);
        }

        return (uint(Error.NO_ERROR), mTokenBalance, borrowBalance, exchangeRateMantissa);
    }

    /**
     * @dev Function to simply retrieve block number
     *  This exists mainly for inheriting test contracts to stub this result.
     */
    function getBlockNumber() internal view returns (uint) {
        return block.number;
    }

    /**
     * @notice Returns the current per-block borrow interest rate for this mToken
     * @return The borrow interest rate per block, scaled by 1e18
     */
    function borrowRatePerBlock() external virtual override view returns (uint) {
        return interestRateModel.getBorrowRate(getCashPrior(), totalBorrows, totalReserves);
    }

    /**
     * @notice Returns the current per-block supply interest rate for this mToken
     * @return The supply interest rate per block, scaled by 1e18
     */
    function supplyRatePerBlock() external virtual override view returns (uint) {
        return interestRateModel.getSupplyRate(getCashPrior(), totalBorrows, totalReserves, reserveFactorMantissa);
    }

    /**
     * @notice Returns the current total borrows plus accrued interest
     * @return The total borrows with interest
     */
    function totalBorrowsCurrent() external virtual override nonReentrant returns (uint) {
        require(accrueInterest() == uint(Error.NO_ERROR), "accrue interest failed");
        return totalBorrows;
    }

    /**
     * @notice Accrue interest to updated borrowIndex and then calculate account's borrow balance using the updated borrowIndex
     * @param account The address whose balance should be calculated after updating borrowIndex
     * @return The calculated balance
     */
    function borrowBalanceCurrent(address account) external virtual override nonReentrant returns (uint) {
        require(accrueInterest() == uint(Error.NO_ERROR), "accrue interest failed");
        return borrowBalanceStored(account);
    }

    /**
     * @notice Return the borrow balance of account based on stored data
     * @param account The address whose balance should be calculated
     * @return The calculated balance
     */
    function borrowBalanceStored(address account) public virtual view returns (uint) {
        (MathError err, uint result) = borrowBalanceStoredInternal(account);
        require(err == MathError.NO_ERROR, "borrowBalanceStored failed");
        return result;
    }

    /**
     * @notice Return the borrow balance of account based on stored data
     * @param account The address whose balance should be calculated
     * @return (error code, the calculated balance or 0 if error code is non-zero)
     */
    function borrowBalanceStoredInternal(address account) internal view returns (MathError, uint) {
        /* Note: we do not assert that the market is up to date */
        MathError mathErr;
        uint principalTimesIndex;
        uint result;

        /* Get borrowBalance and borrowIndex */
        BorrowSnapshot storage borrowSnapshot = accountBorrows[account];

        /* If borrowBalance = 0 then borrowIndex is likely also 0.
         * Rather than failing the calculation with a division by 0, we immediately return 0 in this case.
         */
        if (borrowSnapshot.principal == 0) {
            return (MathError.NO_ERROR, 0);
        }

        /* Calculate new borrow balance using the interest index:
         *  recentBorrowBalance = borrower.borrowBalance * market.borrowIndex / borrower.borrowIndex
         */
        (mathErr, principalTimesIndex) = mulUInt(borrowSnapshot.principal, borrowIndex);
        if (mathErr != MathError.NO_ERROR) {
            return (mathErr, 0);
        }

        (mathErr, result) = divUInt(principalTimesIndex, borrowSnapshot.interestIndex);
        if (mathErr != MathError.NO_ERROR) {
            return (mathErr, 0);
        }

        return (MathError.NO_ERROR, result);
    }

    /**
     * @notice Accrue interest then return the up-to-date exchange rate
     * @return Calculated exchange rate scaled by 1e18
     */
    function exchangeRateCurrent() public virtual nonReentrant returns (uint) {
        require(accrueInterest() == uint(Error.NO_ERROR), "accrue interest failed");
        return exchangeRateStored();
    }

    /**
     * @notice Calculates the exchange rate from the underlying to the MToken
     * @dev This function does not accrue interest before calculating the exchange rate
     * @return Calculated exchange rate scaled by 1e18
     */
    function exchangeRateStored() public virtual view returns (uint) {
        (MathError err, uint result) = exchangeRateStoredInternal();
        require(err == MathError.NO_ERROR, "exchangeRateStored failed");
        return result;
    }

    /**
     * @notice Calculates the exchange rate from the underlying to the MToken
     * @dev This function does not accrue interest before calculating the exchange rate
     * @return (error code, calculated exchange rate scaled by 1e18)
     */
    function exchangeRateStoredInternal() internal view returns (MathError, uint) {
        uint _totalSupply = totalSupply;
        if (_totalSupply == 0) {
            /*
             * If there are no tokens minted:
             *  exchangeRate = initialExchangeRate
             */
            return (MathError.NO_ERROR, initialExchangeRateMantissa);
        } else {
            /*
             * Otherwise:
             *  exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
             */
            uint totalCash = getCashPrior();
            uint cashPlusBorrowsMinusReserves;
            Exp memory exchangeRate;
            MathError mathErr;

            (mathErr, cashPlusBorrowsMinusReserves) = addThenSubUInt(totalCash, totalBorrows, totalReserves);
            if (mathErr != MathError.NO_ERROR) {
                return (mathErr, 0);
            }

            (mathErr, exchangeRate) = getExp(cashPlusBorrowsMinusReserves, _totalSupply);
            if (mathErr != MathError.NO_ERROR) {
                return (mathErr, 0);
            }

            return (MathError.NO_ERROR, exchangeRate.mantissa);
        }
    }

    /**
     * @notice Get cash balance of this mToken in the underlying asset
     * @return The quantity of underlying asset owned by this contract
     */
    function getCash() external virtual override view returns (uint) {
        return getCashPrior();
    }

    function getRealBorrowIndex() public view returns (uint) {
        uint currentBlockNumber = getBlockNumber();
        uint accrualBlockNumberPrior = accrualBlockNumber;
        uint cashPrior = getCashPrior();
        uint borrowsPrior = totalBorrows;
        uint reservesPrior = totalReserves;
        uint borrowIndexPrior = borrowIndex;

        uint borrowRateMantissa = interestRateModel.getBorrowRate(cashPrior, borrowsPrior, reservesPrior);
        require(borrowRateMantissa <= borrowRateMaxMantissa, "borrow rate too high");

        (MathError mathErr, uint blockDelta) = subUInt(currentBlockNumber, accrualBlockNumberPrior);
        require(mathErr == MathError.NO_ERROR, "could not calc block delta");

        Exp memory simpleInterestFactor;
        uint borrowIndexNew;

        (mathErr, simpleInterestFactor) = mulScalar(Exp({mantissa: borrowRateMantissa}), blockDelta);
        require(mathErr == MathError.NO_ERROR, "could not calc simpleInterestFactor");

        (mathErr, borrowIndexNew) = mulScalarTruncateAddUInt(simpleInterestFactor, borrowIndexPrior, borrowIndexPrior);
        require(mathErr == MathError.NO_ERROR, "could not calc borrowIndex");

        return borrowIndexNew;
    }

    /**
     * @notice Applies accrued interest to total borrows and reserves
     * @dev This calculates interest accrued from the last checkpointed block
     *   up to the current block and writes new checkpoint to storage.
     */
    function accrueInterest() public virtual returns (uint) {
        /* Remember the initial block number */
        uint currentBlockNumber = getBlockNumber();
        uint accrualBlockNumberPrior = accrualBlockNumber;

        /* Short-circuit accumulating 0 interest */
        if (accrualBlockNumberPrior == currentBlockNumber) {
            return uint(Error.NO_ERROR);
        }

        /* Read the previous values out of storage */
        uint cashPrior = getCashPrior();
        uint borrowsPrior = totalBorrows;
        uint reservesPrior = totalReserves;
        uint borrowIndexPrior = borrowIndex;

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

        /* Calculate the number of blocks elapsed since the last accrual */
        (MathError mathErr, uint blockDelta) = subUInt(currentBlockNumber, accrualBlockNumberPrior);
        require(mathErr == MathError.NO_ERROR, "could not calc block delta");

        /*
         * Calculate the interest accumulated into borrows and reserves and the new index:
         *  simpleInterestFactor = borrowRate * blockDelta
         *  interestAccumulated = simpleInterestFactor * totalBorrows
         *  totalBorrowsNew = interestAccumulated + totalBorrows
         *  totalReservesNew = interestAccumulated * reserveFactor + totalReserves
         *  borrowIndexNew = simpleInterestFactor * borrowIndex + borrowIndex
         */

        Exp memory simpleInterestFactor;
        AccrueInterestTempStorage memory temp;

        (mathErr, simpleInterestFactor) = mulScalar(Exp({mantissa: borrowRateMantissa}), blockDelta);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_SIMPLE_INTEREST_FACTOR_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.interestAccumulated) = mulScalarTruncate(simpleInterestFactor, borrowsPrior);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_ACCUMULATED_INTEREST_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.totalBorrowsNew) = addUInt(temp.interestAccumulated, borrowsPrior);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_TOTAL_BORROWS_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.reservesAdded) = mulScalarTruncate(Exp({mantissa: reserveFactorMantissa}), temp.interestAccumulated);
        if(mathErr != MathError.NO_ERROR){
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.splitedReserves_2) = mulScalarTruncate(Exp({mantissa: reserveSplitFactorMantissa}), temp.reservesAdded);
        if(mathErr != MathError.NO_ERROR){
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.splitedReserves_1) = subUInt(temp.reservesAdded, temp.splitedReserves_2);
        if(mathErr != MathError.NO_ERROR){
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.totalReservesNew) = addUInt(temp.splitedReserves_1, reservesPrior);
        if(mathErr != MathError.NO_ERROR){
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED, uint(mathErr));
        }

        (mathErr, temp.borrowIndexNew) = mulScalarTruncateAddUInt(simpleInterestFactor, borrowIndexPrior, borrowIndexPrior);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.ACCRUE_INTEREST_NEW_BORROW_INDEX_CALCULATION_FAILED, uint(mathErr));
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /* We write the previously calculated values into storage */
        accrualBlockNumber = currentBlockNumber;
        borrowIndex = temp.borrowIndexNew;
        totalBorrows = temp.totalBorrowsNew;
        totalReserves = temp.totalReservesNew;

        if(temp.splitedReserves_2 > 0){
            address mProxy = moartroller.mProxy();
            EIP20Interface(underlying).approve(mProxy, temp.splitedReserves_2);
            MProxyInterface(mProxy).proxySplitReserves(underlying, temp.splitedReserves_2);
        }
        
        /* We emit an AccrueInterest event */
        emit AccrueInterest(cashPrior, temp.interestAccumulated, temp.borrowIndexNew, temp.totalBorrowsNew);

        return uint(Error.NO_ERROR);
    }
    
    /**
     * @notice Sender supplies assets into the market and receives mTokens in exchange
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param mintAmount The amount of the underlying asset to supply
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual mint amount.
     */
    function mintInternal(uint mintAmount) internal nonReentrant returns (uint, uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted borrow failed
            return (fail(Error(error), FailureInfo.MINT_ACCRUE_INTEREST_FAILED), 0);
        }
        // mintFresh emits the actual Mint event if successful and logs on errors, so we don't need to
        return mintFresh(msg.sender, mintAmount);
    }

    struct MintLocalVars {
        Error err;
        MathError mathErr;
        uint exchangeRateMantissa;
        uint mintTokens;
        uint totalSupplyNew;
        uint accountTokensNew;
        uint actualMintAmount;
    }

    /**
     * @notice User supplies assets into the market and receives mTokens in exchange
     * @dev Assumes interest has already been accrued up to the current block
     * @param minter The address of the account which is supplying the assets
     * @param mintAmount The amount of the underlying asset to supply
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual mint amount.
     */
    function mintFresh(address minter, uint mintAmount) internal returns (uint, uint) {
        /* Fail if mint not allowed */
        uint allowed = moartroller.mintAllowed(address(this), minter, mintAmount);
        if (allowed != 0) {
            return (failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.MINT_MOARTROLLER_REJECTION, allowed), 0);
        }

        /* Verify market's block number equals current block number */
        if (accrualBlockNumber != getBlockNumber()) {
            return (fail(Error.MARKET_NOT_FRESH, FailureInfo.MINT_FRESHNESS_CHECK), 0);
        }

        MintLocalVars memory vars;

        (vars.mathErr, vars.exchangeRateMantissa) = exchangeRateStoredInternal();
        if (vars.mathErr != MathError.NO_ERROR) {
            return (failOpaque(Error.MATH_ERROR, FailureInfo.MINT_EXCHANGE_RATE_READ_FAILED, uint(vars.mathErr)), 0);
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /*
         *  We call `doTransferIn` for the minter and the mintAmount.
         *  Note: The mToken must handle variations between ERC-20 and ETH underlying.
         *  `doTransferIn` reverts if anything goes wrong, since we can't be sure if
         *  side-effects occurred. The function returns the amount actually transferred,
         *  in case of a fee. On success, the mToken holds an additional `actualMintAmount`
         *  of cash.
         */
        vars.actualMintAmount = doTransferIn(minter, mintAmount);

        /*
         * We get the current exchange rate and calculate the number of mTokens to be minted:
         *  mintTokens = actualMintAmount / exchangeRate
         */

        (vars.mathErr, vars.mintTokens) = divScalarByExpTruncate(vars.actualMintAmount, Exp({mantissa: vars.exchangeRateMantissa}));
        require(vars.mathErr == MathError.NO_ERROR, "MINT_E");

        /*
         * We calculate the new total supply of mTokens and minter token balance, checking for overflow:
         *  totalSupplyNew = totalSupply + mintTokens
         *  accountTokensNew = accountTokens[minter] + mintTokens
         */
        (vars.mathErr, vars.totalSupplyNew) = addUInt(totalSupply, vars.mintTokens);
        require(vars.mathErr == MathError.NO_ERROR, "MINT_E");

        (vars.mathErr, vars.accountTokensNew) = addUInt(accountTokens[minter], vars.mintTokens);
        require(vars.mathErr == MathError.NO_ERROR, "MINT_E");

        /* We write previously calculated values into storage */
        totalSupply = vars.totalSupplyNew;
        accountTokens[minter] = vars.accountTokensNew;

        /* We emit a Mint event, and a Transfer event */
        emit Mint(minter, vars.actualMintAmount, vars.mintTokens);
        emit Transfer(address(this), minter, vars.mintTokens);

        /* We call the defense hook */
        // unused function
        // moartroller.mintVerify(address(this), minter, vars.actualMintAmount, vars.mintTokens);

        return (uint(Error.NO_ERROR), vars.actualMintAmount);
    }

    /**
     * @notice Sender redeems mTokens in exchange for the underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemTokens The number of mTokens to redeem into underlying
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeemInternal(uint redeemTokens) internal nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted redeem failed
            return fail(Error(error), FailureInfo.REDEEM_ACCRUE_INTEREST_FAILED);
        }
        // redeemFresh emits redeem-specific logs on errors, so we don't need to
        return redeemFresh(msg.sender, redeemTokens, 0);
    }

    /**
     * @notice Sender redeems mTokens in exchange for a specified amount of underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemAmount The amount of underlying to receive from redeeming mTokens
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeemUnderlyingInternal(uint redeemAmount) internal nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted redeem failed
            return fail(Error(error), FailureInfo.REDEEM_ACCRUE_INTEREST_FAILED);
        }
        // redeemFresh emits redeem-specific logs on errors, so we don't need to
        return redeemFresh(msg.sender, 0, redeemAmount);
    }

    struct RedeemLocalVars {
        Error err;
        MathError mathErr;
        uint exchangeRateMantissa;
        uint redeemTokens;
        uint redeemAmount;
        uint totalSupplyNew;
        uint accountTokensNew;
    }

    /**
     * @notice User redeems mTokens in exchange for the underlying asset
     * @dev Assumes interest has already been accrued up to the current block
     * @param redeemer The address of the account which is redeeming the tokens
     * @param redeemTokensIn The number of mTokens to redeem into underlying (only one of redeemTokensIn or redeemAmountIn may be non-zero)
     * @param redeemAmountIn The number of underlying tokens to receive from redeeming mTokens (only one of redeemTokensIn or redeemAmountIn may be non-zero)
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeemFresh(address payable redeemer, uint redeemTokensIn, uint redeemAmountIn) internal returns (uint) {
        require(redeemTokensIn == 0 || redeemAmountIn == 0, "redeemFresh_missing_zero");

        RedeemLocalVars memory vars;

        /* exchangeRate = invoke Exchange Rate Stored() */
        (vars.mathErr, vars.exchangeRateMantissa) = exchangeRateStoredInternal();
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_EXCHANGE_RATE_READ_FAILED, uint(vars.mathErr));
        }

        /* If redeemTokensIn > 0: */
        if (redeemTokensIn > 0) {
            /*
             * We calculate the exchange rate and the amount of underlying to be redeemed:
             *  redeemTokens = redeemTokensIn
             *  redeemAmount = redeemTokensIn x exchangeRateCurrent
             */
            vars.redeemTokens = redeemTokensIn;

            (vars.mathErr, vars.redeemAmount) = mulScalarTruncate(Exp({mantissa: vars.exchangeRateMantissa}), redeemTokensIn);
            if (vars.mathErr != MathError.NO_ERROR) {
                return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_EXCHANGE_TOKENS_CALCULATION_FAILED, uint(vars.mathErr));
            }
        } else {
            /*
             * We get the current exchange rate and calculate the amount to be redeemed:
             *  redeemTokens = redeemAmountIn / exchangeRate
             *  redeemAmount = redeemAmountIn
             */

            (vars.mathErr, vars.redeemTokens) = divScalarByExpTruncate(redeemAmountIn, Exp({mantissa: vars.exchangeRateMantissa}));
            if (vars.mathErr != MathError.NO_ERROR) {
                return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_EXCHANGE_AMOUNT_CALCULATION_FAILED, uint(vars.mathErr));
            }

            vars.redeemAmount = redeemAmountIn;
        }

        /* Fail if redeem not allowed */
        uint allowed = moartroller.redeemAllowed(address(this), redeemer, vars.redeemTokens);
        if (allowed != 0) {
            return failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.REDEEM_MOARTROLLER_REJECTION, allowed);
        }

        /* Verify market's block number equals current block number */
        if (accrualBlockNumber != getBlockNumber()) {
            return fail(Error.MARKET_NOT_FRESH, FailureInfo.REDEEM_FRESHNESS_CHECK);
        }

        /*
         * We calculate the new total supply and redeemer balance, checking for underflow:
         *  totalSupplyNew = totalSupply - redeemTokens
         *  accountTokensNew = accountTokens[redeemer] - redeemTokens
         */
        (vars.mathErr, vars.totalSupplyNew) = subUInt(totalSupply, vars.redeemTokens);
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_NEW_TOTAL_SUPPLY_CALCULATION_FAILED, uint(vars.mathErr));
        }

        (vars.mathErr, vars.accountTokensNew) = subUInt(accountTokens[redeemer], vars.redeemTokens);
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_NEW_ACCOUNT_BALANCE_CALCULATION_FAILED, uint(vars.mathErr));
        }

        /* Fail gracefully if protocol has insufficient cash */
        if (getCashPrior() < vars.redeemAmount) {
            return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.REDEEM_TRANSFER_OUT_NOT_POSSIBLE);
        }

        /* Fail if user tries to redeem more than he has locked with c-op*/
        // TODO: update error codes
        uint newTokensAmount =  div_(mul_(vars.accountTokensNew, vars.exchangeRateMantissa), 1e18);
        if (newTokensAmount < moartroller.getUserLockedAmount(this, redeemer)) {
            return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.REDEEM_TRANSFER_OUT_NOT_POSSIBLE);
        }


        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /*
         * We invoke doTransferOut for the redeemer and the redeemAmount.
         *  Note: The mToken must handle variations between ERC-20 and ETH underlying.
         *  On success, the mToken has redeemAmount less of cash.
         *  doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
         */
        doTransferOut(redeemer, vars.redeemAmount);

        /* We write previously calculated values into storage */
        totalSupply = vars.totalSupplyNew;
        accountTokens[redeemer] = vars.accountTokensNew;

        /* We emit a Transfer event, and a Redeem event */
        emit Transfer(redeemer, address(this), vars.redeemTokens);
        emit Redeem(redeemer, vars.redeemAmount, vars.redeemTokens);

        /* We call the defense hook */
        moartroller.redeemVerify(address(this), redeemer, vars.redeemAmount, vars.redeemTokens);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sender borrows assets from the protocol to their own address
      * @param borrowAmount The amount of the underlying asset to borrow
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function borrowInternal(uint borrowAmount) internal nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted borrow failed
            return fail(Error(error), FailureInfo.BORROW_ACCRUE_INTEREST_FAILED);
        }
        // borrowFresh emits borrow-specific logs on errors, so we don't need to
        return borrowFresh(msg.sender, borrowAmount);
    }

    function borrowForInternal(address payable borrower, uint borrowAmount) internal nonReentrant returns (uint) {
        require(moartroller.isPrivilegedAddress(msg.sender), "permission_missing");
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted borrow failed
            return fail(Error(error), FailureInfo.BORROW_ACCRUE_INTEREST_FAILED);
        }
        // borrowFresh emits borrow-specific logs on errors, so we don't need to
        return borrowFresh(borrower, borrowAmount);
    }

    struct BorrowLocalVars {
        MathError mathErr;
        uint accountBorrows;
        uint accountBorrowsNew;
        uint totalBorrowsNew;
    }

    /**
      * @notice Users borrow assets from the protocol to their own address
      * @param borrowAmount The amount of the underlying asset to borrow
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function borrowFresh(address payable borrower, uint borrowAmount) internal returns (uint) {
        /* Fail if borrow not allowed */
        uint allowed = moartroller.borrowAllowed(address(this), borrower, borrowAmount);
        if (allowed != 0) {
            return failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.BORROW_MOARTROLLER_REJECTION, allowed);
        }

        /* Verify market's block number equals current block number */
        if (accrualBlockNumber != getBlockNumber()) {
            return fail(Error.MARKET_NOT_FRESH, FailureInfo.BORROW_FRESHNESS_CHECK);
        }

        /* Fail gracefully if protocol has insufficient underlying cash */
        if (getCashPrior() < borrowAmount) {
            return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.BORROW_CASH_NOT_AVAILABLE);
        }

        BorrowLocalVars memory vars;

        /*
         * We calculate the new borrower and total borrow balances, failing on overflow:
         *  accountBorrowsNew = accountBorrows + borrowAmount
         *  totalBorrowsNew = totalBorrows + borrowAmount
         */
        (vars.mathErr, vars.accountBorrows) = borrowBalanceStoredInternal(borrower);
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.BORROW_ACCUMULATED_BALANCE_CALCULATION_FAILED, uint(vars.mathErr));
        }

        (vars.mathErr, vars.accountBorrowsNew) = addUInt(vars.accountBorrows, borrowAmount);
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.BORROW_NEW_ACCOUNT_BORROW_BALANCE_CALCULATION_FAILED, uint(vars.mathErr));
        }

        (vars.mathErr, vars.totalBorrowsNew) = addUInt(totalBorrows, borrowAmount);
        if (vars.mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.BORROW_NEW_TOTAL_BALANCE_CALCULATION_FAILED, uint(vars.mathErr));
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /*
         * We invoke doTransferOut for the borrower and the borrowAmount.
         *  Note: The mToken must handle variations between ERC-20 and ETH underlying.
         *  On success, the mToken borrowAmount less of cash.
         *  doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
         */
        doTransferOut(borrower, borrowAmount);

        /* We write the previously calculated values into storage */
        accountBorrows[borrower].principal = vars.accountBorrowsNew;
        accountBorrows[borrower].interestIndex = borrowIndex;
        totalBorrows = vars.totalBorrowsNew;

        /* We emit a Borrow event */
        emit Borrow(borrower, borrowAmount, vars.accountBorrowsNew, vars.totalBorrowsNew);

        /* We call the defense hook */
        //unused function
        // moartroller.borrowVerify(address(this), borrower, borrowAmount);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Sender repays their own borrow
     * @param repayAmount The amount to repay
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual repayment amount.
     */
    function repayBorrowInternal(uint repayAmount) internal nonReentrant returns (uint, uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted borrow failed
            return (fail(Error(error), FailureInfo.REPAY_BORROW_ACCRUE_INTEREST_FAILED), 0);
        }
        // repayBorrowFresh emits repay-borrow-specific logs on errors, so we don't need to
        return repayBorrowFresh(msg.sender, msg.sender, repayAmount);
    }

    /**
     * @notice Sender repays a borrow belonging to borrower
     * @param borrower the account with the debt being payed off
     * @param repayAmount The amount to repay
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual repayment amount.
     */
    function repayBorrowBehalfInternal(address borrower, uint repayAmount) internal nonReentrant returns (uint, uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted borrow failed
            return (fail(Error(error), FailureInfo.REPAY_BEHALF_ACCRUE_INTEREST_FAILED), 0);
        }
        // repayBorrowFresh emits repay-borrow-specific logs on errors, so we don't need to
        return repayBorrowFresh(msg.sender, borrower, repayAmount);
    }

    struct RepayBorrowLocalVars {
        Error err;
        MathError mathErr;
        uint repayAmount;
        uint borrowerIndex;
        uint accountBorrows;
        uint accountBorrowsNew;
        uint totalBorrowsNew;
        uint actualRepayAmount;
    }

    /**
     * @notice Borrows are repaid by another user (possibly the borrower).
     * @param payer the account paying off the borrow
     * @param borrower the account with the debt being payed off
     * @param repayAmount the amount of undelrying tokens being returned
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual repayment amount.
     */
    function repayBorrowFresh(address payer, address borrower, uint repayAmount) internal returns (uint, uint) {
        /* Fail if repayBorrow not allowed */
        uint allowed = moartroller.repayBorrowAllowed(address(this), payer, borrower, repayAmount);
        if (allowed != 0) {
            return (failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.REPAY_BORROW_MOARTROLLER_REJECTION, allowed), 0);
        }

        /* Verify market's block number equals current block number */
        if (accrualBlockNumber != getBlockNumber()) {
            return (fail(Error.MARKET_NOT_FRESH, FailureInfo.REPAY_BORROW_FRESHNESS_CHECK), 0);
        }

        RepayBorrowLocalVars memory vars;

        /* We remember the original borrowerIndex for verification purposes */
        vars.borrowerIndex = accountBorrows[borrower].interestIndex;

        /* We fetch the amount the borrower owes, with accumulated interest */
        (vars.mathErr, vars.accountBorrows) = borrowBalanceStoredInternal(borrower);
        if (vars.mathErr != MathError.NO_ERROR) {
            return (failOpaque(Error.MATH_ERROR, FailureInfo.REPAY_BORROW_ACCUMULATED_BALANCE_CALCULATION_FAILED, uint(vars.mathErr)), 0);
        }

        /* If repayAmount == -1, repayAmount = accountBorrows */
        /* If the borrow is repaid by another user -1 cannot be used to prevent borrow front-running */
        if (repayAmount == uint(-1)) {
            require(tx.origin == borrower, "specify a precise amount");
            vars.repayAmount = vars.accountBorrows;
        } else {
            vars.repayAmount = repayAmount;
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /*
         * We call doTransferIn for the payer and the repayAmount
         *  Note: The mToken must handle variations between ERC-20 and ETH underlying.
         *  On success, the mToken holds an additional repayAmount of cash.
         *  doTransferIn reverts if anything goes wrong, since we can't be sure if side effects occurred.
         *   it returns the amount actually transferred, in case of a fee.
         */
        vars.actualRepayAmount = doTransferIn(payer, vars.repayAmount);

        /*
         * We calculate the new borrower and total borrow balances, failing on underflow:
         *  accountBorrowsNew = accountBorrows - actualRepayAmount
         *  totalBorrowsNew = totalBorrows - actualRepayAmount
         */
        (vars.mathErr, vars.accountBorrowsNew) = subUInt(vars.accountBorrows, vars.actualRepayAmount);
        require(vars.mathErr == MathError.NO_ERROR, "BORROW_BALANCE_CALCULATION_FAILED");

        (vars.mathErr, vars.totalBorrowsNew) = subUInt(totalBorrows, vars.actualRepayAmount);
        require(vars.mathErr == MathError.NO_ERROR, "TOTAL_BALANCE_CALCULATION_FAILED");

        /* We write the previously calculated values into storage */
        accountBorrows[borrower].principal = vars.accountBorrowsNew;
        accountBorrows[borrower].interestIndex = borrowIndex;
        totalBorrows = vars.totalBorrowsNew;

        /* We emit a RepayBorrow event */
        emit RepayBorrow(payer, borrower, vars.actualRepayAmount, vars.accountBorrowsNew, vars.totalBorrowsNew);

        /* We call the defense hook */
        // unused function
        // moartroller.repayBorrowVerify(address(this), payer, borrower, vars.actualRepayAmount, vars.borrowerIndex);

        return (uint(Error.NO_ERROR), vars.actualRepayAmount);
    }

    /**
     * @notice The sender liquidates the borrowers collateral.
     *  The collateral seized is transferred to the liquidator.
     * @param borrower The borrower of this mToken to be liquidated
     * @param mTokenCollateral The market in which to seize collateral from the borrower
     * @param repayAmount The amount of the underlying borrowed asset to repay
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual repayment amount.
     */
    function liquidateBorrowInternal(address borrower, uint repayAmount, MToken mTokenCollateral) internal nonReentrant returns (uint, uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted liquidation failed
            return (fail(Error(error), FailureInfo.LIQUIDATE_ACCRUE_BORROW_INTEREST_FAILED), 0);
        }

        error = mTokenCollateral.accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but we still want to log the fact that an attempted liquidation failed
            return (fail(Error(error), FailureInfo.LIQUIDATE_ACCRUE_COLLATERAL_INTEREST_FAILED), 0);
        }

        // liquidateBorrowFresh emits borrow-specific logs on errors, so we don't need to
        return liquidateBorrowFresh(msg.sender, borrower, repayAmount, mTokenCollateral);
    }

    /**
     * @notice The liquidator liquidates the borrowers collateral.
     *  The collateral seized is transferred to the liquidator.
     * @param borrower The borrower of this mToken to be liquidated
     * @param liquidator The address repaying the borrow and seizing collateral
     * @param mTokenCollateral The market in which to seize collateral from the borrower
     * @param repayAmount The amount of the underlying borrowed asset to repay
     * @return (uint, uint) An error code (0=success, otherwise a failure, see ErrorReporter.sol), and the actual repayment amount.
     */
    function liquidateBorrowFresh(address liquidator, address borrower, uint repayAmount, MToken mTokenCollateral) internal returns (uint, uint) {
        /* Fail if liquidate not allowed */
        uint allowed = moartroller.liquidateBorrowAllowed(address(this), address(mTokenCollateral), liquidator, borrower, repayAmount);
        if (allowed != 0) {
            return (failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.LIQUIDATE_MOARTROLLER_REJECTION, allowed), 0);
        }

        /* Verify market's block number equals current block number */
        if (accrualBlockNumber != getBlockNumber()) {
            return (fail(Error.MARKET_NOT_FRESH, FailureInfo.LIQUIDATE_FRESHNESS_CHECK), 0);
        }

        /* Verify mTokenCollateral market's block number equals current block number */
        if (mTokenCollateral.accrualBlockNumber() != getBlockNumber()) {
            return (fail(Error.MARKET_NOT_FRESH, FailureInfo.LIQUIDATE_COLLATERAL_FRESHNESS_CHECK), 0);
        }

        /* Fail if borrower = liquidator */
        if (borrower == liquidator) {
            return (fail(Error.INVALID_ACCOUNT_PAIR, FailureInfo.LIQUIDATE_LIQUIDATOR_IS_BORROWER), 0);
        }

        /* Fail if repayAmount = 0 */
        if (repayAmount == 0) {
            return (fail(Error.INVALID_CLOSE_AMOUNT_REQUESTED, FailureInfo.LIQUIDATE_CLOSE_AMOUNT_IS_ZERO), 0);
        }

        /* Fail if repayAmount = -1 */
        if (repayAmount == uint(-1)) {
            return (fail(Error.INVALID_CLOSE_AMOUNT_REQUESTED, FailureInfo.LIQUIDATE_CLOSE_AMOUNT_IS_UINT_MAX), 0);
        }


        /* Fail if repayBorrow fails */
        (uint repayBorrowError, uint actualRepayAmount) = repayBorrowFresh(liquidator, borrower, repayAmount);
        if (repayBorrowError != uint(Error.NO_ERROR)) {
            return (fail(Error(repayBorrowError), FailureInfo.LIQUIDATE_REPAY_BORROW_FRESH_FAILED), 0);
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /* We calculate the number of collateral tokens that will be seized */
        (uint amountSeizeError, uint seizeTokens) = moartroller.liquidateCalculateSeizeUserTokens(address(this), address(mTokenCollateral), actualRepayAmount, borrower);
        require(amountSeizeError == uint(Error.NO_ERROR), "CALCULATE_AMOUNT_SEIZE_FAILED");

        /* Revert if borrower collateral token balance < seizeTokens */
        require(mTokenCollateral.balanceOf(borrower) >= seizeTokens, "TOO_MUCH");

        // If this is also the collateral, run seizeInternal to avoid re-entrancy, otherwise make an external call
        uint seizeError;
        if (address(mTokenCollateral) == address(this)) {
            seizeError = seizeInternal(address(this), liquidator, borrower, seizeTokens);
        } else {
            seizeError = mTokenCollateral.seize(liquidator, borrower, seizeTokens);
        }

        /* Revert if seize tokens fails (since we cannot be sure of side effects) */
        require(seizeError == uint(Error.NO_ERROR), "token seizure failed");

        /* We emit a LiquidateBorrow event */
        emit LiquidateBorrow(liquidator, borrower, actualRepayAmount, address(mTokenCollateral), seizeTokens);

        /* We call the defense hook */
        // unused function
        // moartroller.liquidateBorrowVerify(address(this), address(mTokenCollateral), liquidator, borrower, actualRepayAmount, seizeTokens);

        return (uint(Error.NO_ERROR), actualRepayAmount);
    }

    /**
     * @notice Transfers collateral tokens (this market) to the liquidator.
     * @dev Will fail unless called by another mToken during the process of liquidation.
     *  Its absolutely critical to use msg.sender as the borrowed mToken and not a parameter.
     * @param liquidator The account receiving seized collateral
     * @param borrower The account having collateral seized
     * @param seizeTokens The number of mTokens to seize
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function seize(address liquidator, address borrower, uint seizeTokens) external virtual override nonReentrant returns (uint) {
        return seizeInternal(msg.sender, liquidator, borrower, seizeTokens);
    }

    /**
     * @notice Transfers collateral tokens (this market) to the liquidator.
     * @dev Called only during an in-kind liquidation, or by liquidateBorrow during the liquidation of another MToken.
     *  Its absolutely critical to use msg.sender as the seizer mToken and not a parameter.
     * @param seizerToken The contract seizing the collateral (i.e. borrowed mToken)
     * @param liquidator The account receiving seized collateral
     * @param borrower The account having collateral seized
     * @param seizeTokens The number of mTokens to seize
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function seizeInternal(address seizerToken, address liquidator, address borrower, uint seizeTokens) internal returns (uint) {
        /* Fail if seize not allowed */
        uint allowed = moartroller.seizeAllowed(address(this), seizerToken, liquidator, borrower, seizeTokens);
        if (allowed != 0) {
            return failOpaque(Error.MOARTROLLER_REJECTION, FailureInfo.LIQUIDATE_SEIZE_MOARTROLLER_REJECTION, allowed);
        }

        /* Fail if borrower = liquidator */
        if (borrower == liquidator) {
            return fail(Error.INVALID_ACCOUNT_PAIR, FailureInfo.LIQUIDATE_SEIZE_LIQUIDATOR_IS_BORROWER);
        }

        MathError mathErr;
        uint borrowerTokensNew;
        uint liquidatorTokensNew;

        /*
         * We calculate the new borrower and liquidator token balances, failing on underflow/overflow:
         *  borrowerTokensNew = accountTokens[borrower] - seizeTokens
         *  liquidatorTokensNew = accountTokens[liquidator] + seizeTokens
         */
        (mathErr, borrowerTokensNew) = subUInt(accountTokens[borrower], seizeTokens);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.LIQUIDATE_SEIZE_BALANCE_DECREMENT_FAILED, uint(mathErr));
        }

        (mathErr, liquidatorTokensNew) = addUInt(accountTokens[liquidator], seizeTokens);
        if (mathErr != MathError.NO_ERROR) {
            return failOpaque(Error.MATH_ERROR, FailureInfo.LIQUIDATE_SEIZE_BALANCE_INCREMENT_FAILED, uint(mathErr));
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /* We write the previously calculated values into storage */
        accountTokens[borrower] = borrowerTokensNew;
        accountTokens[liquidator] = liquidatorTokensNew;

        /* Emit a Transfer event */
        emit Transfer(borrower, liquidator, seizeTokens);

        /* We call the defense hook */
        // unused function
        // moartroller.seizeVerify(address(this), seizerToken, liquidator, borrower, seizeTokens);

        return uint(Error.NO_ERROR);
    }


    /*** Admin Functions ***/

    /**
      * @notice Begins transfer of admin rights. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.
      * @dev Admin function to begin change of admin. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.
      * @param newPendingAdmin New pending admin.
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _setPendingAdmin(address payable newPendingAdmin) external virtual override returns (uint) {
        // Check caller = admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PENDING_ADMIN_OWNER_CHECK);
        }

        // Save current value, if any, for inclusion in log
        address oldPendingAdmin = pendingAdmin;

        // Store pendingAdmin with value newPendingAdmin
        pendingAdmin = newPendingAdmin;

        // Emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin)
        emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Accepts transfer of admin rights. msg.sender must be pendingAdmin
      * @dev Admin function for pending admin to accept role and update admin
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _acceptAdmin() external virtual override returns (uint) {
        // Check caller is pendingAdmin and pendingAdmin ≠ address(0)
        if (msg.sender != pendingAdmin || msg.sender == address(0)) {
            return fail(Error.UNAUTHORIZED, FailureInfo.ACCEPT_ADMIN_PENDING_ADMIN_CHECK);
        }

        // Save current values for inclusion in log
        address oldAdmin = admin;
        address oldPendingAdmin = pendingAdmin;

        // Store admin with value pendingAdmin
        admin = pendingAdmin;

        // Clear the pending value
        pendingAdmin = address(0);

        emit NewAdmin(oldAdmin, admin);
        emit NewPendingAdmin(oldPendingAdmin, pendingAdmin);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sets a new moartroller for the market
      * @dev Admin function to set a new moartroller
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _setMoartroller(Moartroller newMoartroller) public virtual returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_MOARTROLLER_OWNER_CHECK);
        }

        Moartroller oldMoartroller = moartroller;
        // Ensure invoke moartroller.isMoartroller() returns true
        require(newMoartroller.isMoartroller(), "not_moartroller");

        // Set market's moartroller to newMoartroller
        moartroller = newMoartroller;

        // Emit NewMoartroller(oldMoartroller, newMoartroller)
        emit NewMoartroller(oldMoartroller, newMoartroller);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice accrues interest and sets a new reserve factor for the protocol using _setReserveFactorFresh
      * @dev Admin function to accrue interest and set a new reserve factor
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _setReserveFactor(uint newReserveFactorMantissa) external virtual override nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but on top of that we want to log the fact that an attempted reserve factor change failed.
            return fail(Error(error), FailureInfo.SET_RESERVE_FACTOR_ACCRUE_INTEREST_FAILED);
        }
        // _setReserveFactorFresh emits reserve-factor-specific logs on errors, so we don't need to.
        return _setReserveFactorFresh(newReserveFactorMantissa);
    }

    function _setReserveSplitFactor(uint newReserveSplitFactorMantissa) external nonReentrant returns (uint) {
         if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_RESERVE_FACTOR_ADMIN_CHECK);
        }
        reserveSplitFactorMantissa = newReserveSplitFactorMantissa;
        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sets a new reserve factor for the protocol (*requires fresh interest accrual)
      * @dev Admin function to set a new reserve factor
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _setReserveFactorFresh(uint newReserveFactorMantissa) internal returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_RESERVE_FACTOR_ADMIN_CHECK);
        }

        // Verify market's block number equals current block number
        if (accrualBlockNumber != getBlockNumber()) {
            return fail(Error.MARKET_NOT_FRESH, FailureInfo.SET_RESERVE_FACTOR_FRESH_CHECK);
        }

        // Check newReserveFactor ≤ maxReserveFactor
        if (newReserveFactorMantissa > reserveFactorMaxMantissa) {
            return fail(Error.BAD_INPUT, FailureInfo.SET_RESERVE_FACTOR_BOUNDS_CHECK);
        }

        uint oldReserveFactorMantissa = reserveFactorMantissa;
        reserveFactorMantissa = newReserveFactorMantissa;

        emit NewReserveFactor(oldReserveFactorMantissa, newReserveFactorMantissa);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Accrues interest and reduces reserves by transferring from msg.sender
     * @param addAmount Amount of addition to reserves
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _addReservesInternal(uint addAmount) internal nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but on top of that we want to log the fact that an attempted reduce reserves failed.
            return fail(Error(error), FailureInfo.ADD_RESERVES_ACCRUE_INTEREST_FAILED);
        }

        // _addReservesFresh emits reserve-addition-specific logs on errors, so we don't need to.
        (error, ) = _addReservesFresh(addAmount);
        return error;
    }

    /**
     * @notice Add reserves by transferring from caller
     * @dev Requires fresh interest accrual
     * @param addAmount Amount of addition to reserves
     * @return (uint, uint) An error code (0=success, otherwise a failure (see ErrorReporter.sol for details)) and the actual amount added, net token fees
     */
    function _addReservesFresh(uint addAmount) internal returns (uint, uint) {
        // totalReserves + actualAddAmount
        uint totalReservesNew;
        uint actualAddAmount;

        // We fail gracefully unless market's block number equals current block number
        if (accrualBlockNumber != getBlockNumber()) {
            return (fail(Error.MARKET_NOT_FRESH, FailureInfo.ADD_RESERVES_FRESH_CHECK), actualAddAmount);
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        /*
         * We call doTransferIn for the caller and the addAmount
         *  Note: The mToken must handle variations between ERC-20 and ETH underlying.
         *  On success, the mToken holds an additional addAmount of cash.
         *  doTransferIn reverts if anything goes wrong, since we can't be sure if side effects occurred.
         *  it returns the amount actually transferred, in case of a fee.
         */

        actualAddAmount = doTransferIn(msg.sender, addAmount);

        totalReservesNew = totalReserves + actualAddAmount;

        /* Revert on overflow */
        require(totalReservesNew >= totalReserves, "overflow");

        // Store reserves[n+1] = reserves[n] + actualAddAmount
        totalReserves = totalReservesNew;

        /* Emit NewReserves(admin, actualAddAmount, reserves[n+1]) */
        emit ReservesAdded(msg.sender, actualAddAmount, totalReservesNew);

        /* Return (NO_ERROR, actualAddAmount) */
        return (uint(Error.NO_ERROR), actualAddAmount);
    }


    /**
     * @notice Accrues interest and reduces reserves by transferring to admin
     * @param reduceAmount Amount of reduction to reserves
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _reduceReserves(uint reduceAmount) external virtual override nonReentrant returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but on top of that we want to log the fact that an attempted reduce reserves failed.
            return fail(Error(error), FailureInfo.REDUCE_RESERVES_ACCRUE_INTEREST_FAILED);
        }
        // _reduceReservesFresh emits reserve-reduction-specific logs on errors, so we don't need to.
        return _reduceReservesFresh(reduceAmount);
    }

    /**
     * @notice Reduces reserves by transferring to admin
     * @dev Requires fresh interest accrual
     * @param reduceAmount Amount of reduction to reserves
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _reduceReservesFresh(uint reduceAmount) internal returns (uint) {
        // totalReserves - reduceAmount
        uint totalReservesNew;

        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.REDUCE_RESERVES_ADMIN_CHECK);
        }

        // We fail gracefully unless market's block number equals current block number
        if (accrualBlockNumber != getBlockNumber()) {
            return fail(Error.MARKET_NOT_FRESH, FailureInfo.REDUCE_RESERVES_FRESH_CHECK);
        }

        // Fail gracefully if protocol has insufficient underlying cash
        if (getCashPrior() < reduceAmount) {
            return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.REDUCE_RESERVES_CASH_NOT_AVAILABLE);
        }

        // Check reduceAmount ≤ reserves[n] (totalReserves)
        if (reduceAmount > totalReserves) {
            return fail(Error.BAD_INPUT, FailureInfo.REDUCE_RESERVES_VALIDATION);
        }

        /////////////////////////
        // EFFECTS & INTERACTIONS
        // (No safe failures beyond this point)

        totalReservesNew = totalReserves - reduceAmount;
        // We checked reduceAmount <= totalReserves above, so this should never revert.
        require(totalReservesNew <= totalReserves, "underflow");

        // Store reserves[n+1] = reserves[n] - reduceAmount
        totalReserves = totalReservesNew;

        // doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
        doTransferOut(admin, reduceAmount);

        emit ReservesReduced(admin, reduceAmount, totalReservesNew);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice accrues interest and updates the interest rate model using _setInterestRateModelFresh
     * @dev Admin function to accrue interest and update the interest rate model
     * @param newInterestRateModel the new interest rate model to use
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _setInterestRateModel(AbstractInterestRateModel newInterestRateModel) public virtual returns (uint) {
        uint error = accrueInterest();
        if (error != uint(Error.NO_ERROR)) {
            // accrueInterest emits logs on errors, but on top of that we want to log the fact that an attempted change of interest rate model failed
            return fail(Error(error), FailureInfo.SET_INTEREST_RATE_MODEL_ACCRUE_INTEREST_FAILED);
        }
        // _setInterestRateModelFresh emits interest-rate-model-update-specific logs on errors, so we don't need to.
        return _setInterestRateModelFresh(newInterestRateModel);
    }

    /**
     * @notice updates the interest rate model (*requires fresh interest accrual)
     * @dev Admin function to update the interest rate model
     * @param newInterestRateModel the new interest rate model to use
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _setInterestRateModelFresh(AbstractInterestRateModel newInterestRateModel) internal returns (uint) {

        // Used to store old model for use in the event that is emitted on success
        InterestRateModelInterface oldInterestRateModel;

        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_INTEREST_RATE_MODEL_OWNER_CHECK);
        }

        // We fail gracefully unless market's block number equals current block number
        if (accrualBlockNumber != getBlockNumber()) {
            return fail(Error.MARKET_NOT_FRESH, FailureInfo.SET_INTEREST_RATE_MODEL_FRESH_CHECK);
        }

        // Track the market's current interest rate model
        oldInterestRateModel = interestRateModel;

        // Ensure invoke newInterestRateModel.isInterestRateModel() returns true
        require(newInterestRateModel.isInterestRateModel(), "not_interest_model");

        // Set the interest rate model to newInterestRateModel
        interestRateModel = newInterestRateModel;

        // Emit NewMarketInterestRateModel(oldInterestRateModel, newInterestRateModel)
        emit NewMarketInterestRateModel(oldInterestRateModel, newInterestRateModel);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Sets new value for max protection composition parameter 
     * @param newMPC New value of MPC
     * @return uint 0=success, otherwise a failure 
     */
    function _setMaxProtectionComposition(uint256 newMPC) external returns(uint){
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_INTEREST_RATE_MODEL_OWNER_CHECK);
        }

        maxProtectionComposition = newMPC;
        emit MpcUpdated(newMPC);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Returns address of underlying token
     * @return address of underlying token
     */
    function getUnderlying() external override view returns(address){
        return underlying;
    }

    /*** Safe Token ***/

    /**
     * @notice Gets balance of this contract in terms of the underlying
     * @dev This excludes the value of the current message, if any
     * @return The quantity of underlying owned by this contract
     */
    function getCashPrior() internal virtual view returns (uint);

    /**
     * @dev Performs a transfer in, reverting upon failure. Returns the amount actually transferred to the protocol, in case of a fee.
     *  This may revert due to insufficient balance or insufficient allowance.
     */
    function doTransferIn(address from, uint amount) internal virtual returns (uint);

    /**
     * @dev Performs a transfer out, ideally returning an explanatory error code upon failure tather than reverting.
     *  If caller has not called checked protocol's balance, may revert due to insufficient cash held in the contract.
     *  If caller has checked protocol's balance, and verified it is >= amount, this should not revert in normal conditions.
     */
    function doTransferOut(address payable to, uint amount) internal virtual;


    /*** Reentrancy Guard ***/

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     */
    modifier nonReentrant() {
        require(_notEntered, "re-entered");
        _notEntered = false;
        _;
        _notEntered = true; // get a gas-refund post-Istanbul
    }
}

File 3 of 53 : Moartroller.sol
// SPDX-License-Identifier: BSD-3-Clause
// Thanks to Compound for their foundational work in DeFi and open-sourcing their code from which we build upon.

pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
// import "hardhat/console.sol";

import "./MToken.sol";
import "./Utils/ErrorReporter.sol";
import "./Utils/ExponentialNoError.sol";
import "./Interfaces/PriceOracle.sol";
import "./Interfaces/MoartrollerInterface.sol";
import "./Interfaces/Versionable.sol";
import "./Interfaces/MProxyInterface.sol";
import "./MoartrollerStorage.sol";
import "./Governance/UnionGovernanceToken.sol";
import "./MProtection.sol";
import "./Interfaces/LiquidityMathModelInterface.sol";
import "./LiquidityMathModelV1.sol";
import "./Utils/SafeEIP20.sol";
import "./Interfaces/EIP20Interface.sol";
import "./Interfaces/LiquidationModelInterface.sol";
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";

/**
 * @title MOAR's Moartroller Contract
 * @author MOAR
 */
contract Moartroller is MoartrollerV6Storage, MoartrollerInterface, MoartrollerErrorReporter, ExponentialNoError, Versionable, Initializable {

    using SafeEIP20 for EIP20Interface;

    /// @notice Indicator that this is a Moartroller contract (for inspection)
    bool public constant isMoartroller = true;

    /// @notice Emitted when an admin supports a market
    event MarketListed(MToken mToken);

    /// @notice Emitted when an account enters a market
    event MarketEntered(MToken mToken, address account);

    /// @notice Emitted when an account exits a market
    event MarketExited(MToken mToken, address account);

    /// @notice Emitted when close factor is changed by admin
    event NewCloseFactor(uint oldCloseFactorMantissa, uint newCloseFactorMantissa);

    /// @notice Emitted when a collateral factor is changed by admin
    event NewCollateralFactor(MToken mToken, uint oldCollateralFactorMantissa, uint newCollateralFactorMantissa);

    /// @notice Emitted when liquidation incentive is changed by admin
    event NewLiquidationIncentive(uint oldLiquidationIncentiveMantissa, uint newLiquidationIncentiveMantissa);

    /// @notice Emitted when price oracle is changed
    event NewPriceOracle(PriceOracle oldPriceOracle, PriceOracle newPriceOracle);

    /// @notice Emitted when protection is changed
    event NewCProtection(MProtection oldCProtection, MProtection newCProtection);

    /// @notice Emitted when pause guardian is changed
    event NewPauseGuardian(address oldPauseGuardian, address newPauseGuardian);

    /// @notice Emitted when an action is paused globally
    event ActionPaused(string action, bool pauseState);

    /// @notice Emitted when an action is paused on a market
    event ActionPausedMToken(MToken mToken, string action, bool pauseState);

    /// @notice Emitted when a new MOAR speed is calculated for a market
    event MoarSpeedUpdated(MToken indexed mToken, uint newSpeed);

    /// @notice Emitted when a new MOAR speed is set for a contributor
    event ContributorMoarSpeedUpdated(address indexed contributor, uint newSpeed);

    /// @notice Emitted when MOAR is distributed to a supplier
    event DistributedSupplierMoar(MToken indexed mToken, address indexed supplier, uint moarDelta, uint moarSupplyIndex);

    /// @notice Emitted when MOAR is distributed to a borrower
    event DistributedBorrowerMoar(MToken indexed mToken, address indexed borrower, uint moarDelta, uint moarBorrowIndex);

    /// @notice Emitted when borrow cap for a mToken is changed
    event NewBorrowCap(MToken indexed mToken, uint newBorrowCap);

    /// @notice Emitted when borrow cap guardian is changed
    event NewBorrowCapGuardian(address oldBorrowCapGuardian, address newBorrowCapGuardian);

    /// @notice Emitted when MOAR is granted by admin
    event MoarGranted(address recipient, uint amount);

    event NewLiquidityMathModel(address oldLiquidityMathModel, address newLiquidityMathModel);

    event NewLiquidationModel(address oldLiquidationModel, address newLiquidationModel);

    /// @notice The initial MOAR index for a market
    uint224 public constant moarInitialIndex = 1e36;

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

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

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

    // Custom initializer
    function initialize(LiquidityMathModelInterface mathModel, LiquidationModelInterface lqdModel) public initializer {
        admin = msg.sender;
        liquidityMathModel = mathModel;
        liquidationModel = lqdModel;
        rewardClaimEnabled = false;
    }

    /*** Assets You Are In ***/

    /**
     * @notice Returns the assets an account has entered
     * @param account The address of the account to pull assets for
     * @return A dynamic list with the assets the account has entered
     */
    function getAssetsIn(address account) external view returns (MToken[] memory) {
        MToken[] memory assetsIn = accountAssets[account];

        return assetsIn;
    }

    /**
     * @notice Returns whether the given account is entered in the given asset
     * @param account The address of the account to check
     * @param mToken The mToken to check
     * @return True if the account is in the asset, otherwise false.
     */
    function checkMembership(address account, MToken mToken) external view returns (bool) {
        return markets[address(mToken)].accountMembership[account];
    }

    /**
     * @notice Add assets to be included in account liquidity calculation
     * @param mTokens The list of addresses of the mToken markets to be enabled
     * @return Success indicator for whether each corresponding market was entered
     */
    function enterMarkets(address[] memory mTokens) public override returns (uint[] memory) {
        uint len = mTokens.length;

        uint[] memory results = new uint[](len);
        for (uint i = 0; i < len; i++) {
            MToken mToken = MToken(mTokens[i]);

            results[i] = uint(addToMarketInternal(mToken, msg.sender));
        }

        return results;
    }

    /**
     * @notice Add the market to the borrower's "assets in" for liquidity calculations
     * @param mToken The market to enter
     * @param borrower The address of the account to modify
     * @return Success indicator for whether the market was entered
     */
    function addToMarketInternal(MToken mToken, address borrower) internal returns (Error) {
        Market storage marketToJoin = markets[address(mToken)];

        if (!marketToJoin.isListed) {
            // market is not listed, cannot join
            return Error.MARKET_NOT_LISTED;
        }

        if (marketToJoin.accountMembership[borrower] == true) {
            // already joined
            return Error.NO_ERROR;
        }

        // survived the gauntlet, add to list
        // NOTE: we store these somewhat redundantly as a significant optimization
        //  this avoids having to iterate through the list for the most common use cases
        //  that is, only when we need to perform liquidity checks
        //  and not whenever we want to check if an account is in a particular market
        marketToJoin.accountMembership[borrower] = true;
        accountAssets[borrower].push(mToken);

        emit MarketEntered(mToken, borrower);

        return Error.NO_ERROR;
    }

    /**
     * @notice Removes asset from sender's account liquidity calculation
     * @dev Sender must not have an outstanding borrow balance in the asset,
     *  or be providing necessary collateral for an outstanding borrow.
     * @param mTokenAddress The address of the asset to be removed
     * @return Whether or not the account successfully exited the market
     */
    function exitMarket(address mTokenAddress) external override returns (uint) {
        MToken mToken = MToken(mTokenAddress);
        /* Get sender tokensHeld and amountOwed underlying from the mToken */
        (uint oErr, uint tokensHeld, uint amountOwed, ) = mToken.getAccountSnapshot(msg.sender);
        require(oErr == 0, "exitMarket: getAccountSnapshot failed"); // semi-opaque error code

        /* Fail if the sender has a borrow balance */
        if (amountOwed != 0) {
            return fail(Error.NONZERO_BORROW_BALANCE, FailureInfo.EXIT_MARKET_BALANCE_OWED);
        }

        /* Fail if the sender is not permitted to redeem all of their tokens */
        uint allowed = redeemAllowedInternal(mTokenAddress, msg.sender, tokensHeld);
        if (allowed != 0) {
            return failOpaque(Error.REJECTION, FailureInfo.EXIT_MARKET_REJECTION, allowed);
        }

        Market storage marketToExit = markets[address(mToken)];

        /* Return true if the sender is not already ‘in’ the market */
        if (!marketToExit.accountMembership[msg.sender]) {
            return uint(Error.NO_ERROR);
        }

        /* Set mToken account membership to false */
        delete marketToExit.accountMembership[msg.sender];

        /* Delete mToken from the account’s list of assets */
        // load into memory for faster iteration
        MToken[] memory userAssetList = accountAssets[msg.sender];
        uint len = userAssetList.length;
        uint assetIndex = len;
        for (uint i = 0; i < len; i++) {
            if (userAssetList[i] == mToken) {
                assetIndex = i;
                break;
            }
        }

        // We *must* have found the asset in the list or our redundant data structure is broken
        assert(assetIndex < len);

        // copy last item in list to location of item to be removed, reduce length by 1
        MToken[] storage storedList = accountAssets[msg.sender];
        storedList[assetIndex] = storedList[storedList.length - 1];
        storedList.pop();

        emit MarketExited(mToken, msg.sender);

        return uint(Error.NO_ERROR);
    }

    /*** Policy Hooks ***/

    /**
     * @notice Checks if the account should be allowed to mint tokens in the given market
     * @param mToken The market to verify the mint against
     * @param minter The account which would get the minted tokens
     * @param mintAmount The amount of underlying being supplied to the market in exchange for tokens
     * @return 0 if the mint is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
     */
    function mintAllowed(address mToken, address minter, uint mintAmount) external override returns (uint) {
        // Pausing is a very serious situation - we revert to sound the alarms
        require(!mintGuardianPaused[mToken], "mint is paused");

        // Shh - currently unused
        minter;
        mintAmount;

        if (!markets[mToken].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        // Keep the flywheel moving
        updateMoarSupplyIndex(mToken);
        distributeSupplierMoar(mToken, minter);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Checks if the account should be allowed to redeem tokens in the given market
     * @param mToken The market to verify the redeem against
     * @param redeemer The account which would redeem the tokens
     * @param redeemTokens The number of mTokens to exchange for the underlying asset in the market
     * @return 0 if the redeem is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
     */
    function redeemAllowed(address mToken, address redeemer, uint redeemTokens) external override returns (uint) {
        uint allowed = redeemAllowedInternal(mToken, redeemer, redeemTokens);
        if (allowed != uint(Error.NO_ERROR)) {
            return allowed;
        }

        // Keep the flywheel moving
        updateMoarSupplyIndex(mToken);
        distributeSupplierMoar(mToken, redeemer);

        return uint(Error.NO_ERROR);
    }

    function redeemAllowedInternal(address mToken, address redeemer, uint redeemTokens) internal view returns (uint) {
        if (!markets[mToken].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        /* If the redeemer is not 'in' the market, then we can bypass the liquidity check */
        if (!markets[mToken].accountMembership[redeemer]) {
            return uint(Error.NO_ERROR);
        }

        /* Otherwise, perform a hypothetical liquidity check to guard against shortfall */
        (Error err, , uint shortfall) = getHypotheticalAccountLiquidityInternal(redeemer, MToken(mToken), redeemTokens, 0);
        if (err != Error.NO_ERROR) {
            return uint(err);
        }
        if (shortfall > 0) {
            return uint(Error.INSUFFICIENT_LIQUIDITY);
        }

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Validates redeem and reverts on rejection. May emit logs.
     * @param mToken Asset being redeemed
     * @param redeemer The address redeeming the tokens
     * @param redeemAmount The amount of the underlying asset being redeemed
     * @param redeemTokens The number of tokens being redeemed
     */
    function redeemVerify(address mToken, address redeemer, uint redeemAmount, uint redeemTokens) external override {
        // Shh - currently unused
        mToken;
        redeemer;

        // Require tokens is zero or amount is also zero
        if (redeemTokens == 0 && redeemAmount > 0) {
            revert("redeemTokens zero");
        }
    }

    /**
     * @notice Checks if the account should be allowed to borrow the underlying asset of the given market
     * @param mToken The market to verify the borrow against
     * @param borrower The account which would borrow the asset
     * @param borrowAmount The amount of underlying the account would borrow
     * @return 0 if the borrow is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
     */
    function borrowAllowed(address mToken, address borrower, uint borrowAmount) external override returns (uint) {
        // Pausing is a very serious situation - we revert to sound the alarms
        require(!borrowGuardianPaused[mToken], "borrow is paused");

        if (!markets[mToken].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        if (!markets[mToken].accountMembership[borrower]) {
            // only mTokens may call borrowAllowed if borrower not in market
            require(msg.sender == mToken, "sender must be mToken");

            // attempt to add borrower to the market
            Error err = addToMarketInternal(MToken(msg.sender), borrower);
            if (err != Error.NO_ERROR) {
                return uint(err);
            }

            // it should be impossible to break the important invariant
            assert(markets[mToken].accountMembership[borrower]);
        }

        if (oracle.getUnderlyingPrice(MToken(mToken)) == 0) {
            return uint(Error.PRICE_ERROR);
        }


        uint borrowCap = borrowCaps[mToken];
        // Borrow cap of 0 corresponds to unlimited borrowing
        if (borrowCap != 0) {
            uint totalBorrows = MToken(mToken).totalBorrows();
            uint nextTotalBorrows = add_(totalBorrows, borrowAmount);
            require(nextTotalBorrows < borrowCap, "market borrow cap reached");
        }

        (Error err, , uint shortfall) = getHypotheticalAccountLiquidityInternal(borrower, MToken(mToken), 0, borrowAmount);
        if (err != Error.NO_ERROR) {
            return uint(err);
        }
        if (shortfall > 0) {
            return uint(Error.INSUFFICIENT_LIQUIDITY);
        }

        // Keep the flywheel moving
        Exp memory borrowIndex = Exp({mantissa: MToken(mToken).borrowIndex()});
        updateMoarBorrowIndex(mToken, borrowIndex);
        distributeBorrowerMoar(mToken, borrower, borrowIndex);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Checks if the account should be allowed to repay a borrow in the given market
     * @param mToken The market to verify the repay against
     * @param payer The account which would repay the asset
     * @param borrower The account which would borrowed the asset
     * @param repayAmount The amount of the underlying asset the account would repay
     * @return 0 if the repay is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
     */
    function repayBorrowAllowed(
        address mToken,
        address payer,
        address borrower,
        uint repayAmount) external override returns (uint) {
        // Shh - currently unused
        payer;
        borrower;
        repayAmount;

        if (!markets[mToken].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        // Keep the flywheel moving
        Exp memory borrowIndex = Exp({mantissa: MToken(mToken).borrowIndex()});
        updateMoarBorrowIndex(mToken, borrowIndex);
        distributeBorrowerMoar(mToken, borrower, borrowIndex);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Checks if the liquidation should be allowed to occur
     * @param mTokenBorrowed Asset which was borrowed by the borrower
     * @param mTokenCollateral Asset which was used as collateral and will be seized
     * @param liquidator The address repaying the borrow and seizing the collateral
     * @param borrower The address of the borrower
     * @param repayAmount The amount of underlying being repaid
     */
    function liquidateBorrowAllowed(
        address mTokenBorrowed,
        address mTokenCollateral,
        address liquidator,
        address borrower,
        uint repayAmount) external override returns (uint) {
        // Shh - currently unused
        liquidator;

        if (!markets[mTokenBorrowed].isListed || !markets[mTokenCollateral].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        /* The borrower must have shortfall in order to be liquidatable */
        (Error err, , uint shortfall) = getAccountLiquidityInternal(borrower);
        if (err != Error.NO_ERROR) {
            return uint(err);
        }
        if (shortfall == 0) {
            return uint(Error.INSUFFICIENT_SHORTFALL);
        }

        /* The liquidator may not repay more than what is allowed by the closeFactor */
        uint borrowBalance = MToken(mTokenBorrowed).borrowBalanceStored(borrower);
        uint maxClose = mul_ScalarTruncate(Exp({mantissa: closeFactorMantissa}), borrowBalance);
        if (repayAmount > maxClose) {
            return uint(Error.TOO_MUCH_REPAY);
        }

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Checks if the seizing of assets should be allowed to occur
     * @param mTokenCollateral Asset which was used as collateral and will be seized
     * @param mTokenBorrowed Asset which was borrowed by the borrower
     * @param liquidator The address repaying the borrow and seizing the collateral
     * @param borrower The address of the borrower
     * @param seizeTokens The number of collateral tokens to seize
     */
    function seizeAllowed(
        address mTokenCollateral,
        address mTokenBorrowed,
        address liquidator,
        address borrower,
        uint seizeTokens) external override returns (uint) {
        // Pausing is a very serious situation - we revert to sound the alarms
        require(!seizeGuardianPaused, "seize is paused");

        // Shh - currently unused
        seizeTokens;

        if (!markets[mTokenCollateral].isListed || !markets[mTokenBorrowed].isListed) {
            return uint(Error.MARKET_NOT_LISTED);
        }

        if (MToken(mTokenCollateral).moartroller() != MToken(mTokenBorrowed).moartroller()) {
            return uint(Error.MOARTROLLER_MISMATCH);
        }

        // Keep the flywheel moving
        updateMoarSupplyIndex(mTokenCollateral);
        distributeSupplierMoar(mTokenCollateral, borrower);
        distributeSupplierMoar(mTokenCollateral, liquidator);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Checks if the account should be allowed to transfer tokens in the given market
     * @param mToken The market to verify the transfer against
     * @param src The account which sources the tokens
     * @param dst The account which receives the tokens
     * @param transferTokens The number of mTokens to transfer
     * @return 0 if the transfer is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
     */
    function transferAllowed(address mToken, address src, address dst, uint transferTokens) external override returns (uint) {
        // Pausing is a very serious situation - we revert to sound the alarms
        require(!transferGuardianPaused, "transfer is paused");

        // Currently the only consideration is whether or not
        //  the src is allowed to redeem this many tokens
        uint allowed = redeemAllowedInternal(mToken, src, transferTokens);
        if (allowed != uint(Error.NO_ERROR)) {
            return allowed;
        }

        // Keep the flywheel moving
        updateMoarSupplyIndex(mToken);
        distributeSupplierMoar(mToken, src);
        distributeSupplierMoar(mToken, dst);

        return uint(Error.NO_ERROR);
    }

    /*** Liquidity/Liquidation Calculations ***/

    /**
     * @notice Determine the current account liquidity wrt collateral requirements
     * @return (possible error code (semi-opaque),
                account liquidity in excess of collateral requirements,
     *          account shortfall below collateral requirements)
     */
    function getAccountLiquidity(address account) public view returns (uint, uint, uint) {
        (Error err, uint liquidity, uint shortfall) = getHypotheticalAccountLiquidityInternal(account, MToken(0), 0, 0);

        return (uint(err), liquidity, shortfall);
    }

    /**
     * @notice Determine the current account liquidity wrt collateral requirements
     * @return (possible error code,
                account liquidity in excess of collateral requirements,
     *          account shortfall below collateral requirements)
     */
    function getAccountLiquidityInternal(address account) internal view returns (Error, uint, uint) {
        return getHypotheticalAccountLiquidityInternal(account, MToken(0), 0, 0);
    }

    /**
     * @notice Determine what the account liquidity would be if the given amounts were redeemed/borrowed
     * @param mTokenModify The market to hypothetically redeem/borrow in
     * @param account The account to determine liquidity for
     * @param redeemTokens The number of tokens to hypothetically redeem
     * @param borrowAmount The amount of underlying to hypothetically borrow
     * @return (possible error code (semi-opaque),
                hypothetical account liquidity in excess of collateral requirements,
     *          hypothetical account shortfall below collateral requirements)
     */
    function getHypotheticalAccountLiquidity(
        address account,
        address mTokenModify,
        uint redeemTokens,
        uint borrowAmount) public view returns (uint, uint, uint) {
        (Error err, uint liquidity, uint shortfall) = getHypotheticalAccountLiquidityInternal(account, MToken(mTokenModify), redeemTokens, borrowAmount);
        return (uint(err), liquidity, shortfall);
    }

    /**
     * @notice Determine what the account liquidity would be if the given amounts were redeemed/borrowed
     * @param mTokenModify The market to hypothetically redeem/borrow in
     * @param account The account to determine liquidity for
     * @param redeemTokens The number of tokens to hypothetically redeem
     * @param borrowAmount The amount of underlying to hypothetically borrow
     * @dev Note that we calculate the exchangeRateStored for each collateral mToken using stored data,
     *  without calculating accumulated interest.
     * @return (possible error code,
                hypothetical account liquidity in excess of collateral requirements,
     *          hypothetical account shortfall below collateral requirements)
     */
    function getHypotheticalAccountLiquidityInternal(
        address account,
        MToken mTokenModify,
        uint redeemTokens,
        uint borrowAmount) internal view returns (Error, uint, uint) {

        AccountLiquidityLocalVars memory vars; // Holds all our calculation results
        uint oErr;

        // For each asset the account is in
        MToken[] memory assets = accountAssets[account];
        for (uint i = 0; i < assets.length; i++) {
            MToken asset = assets[i];
            address _account = account;

            // Read the balances and exchange rate from the mToken
            (oErr, vars.mTokenBalance, vars.borrowBalance, vars.exchangeRateMantissa) = asset.getAccountSnapshot(_account);
            if (oErr != 0) { // semi-opaque error code, we assume NO_ERROR == 0 is invariant between upgrades
                return (Error.SNAPSHOT_ERROR, 0, 0);
            }
            vars.collateralFactor = Exp({mantissa: markets[address(asset)].collateralFactorMantissa});
            vars.exchangeRate = Exp({mantissa: vars.exchangeRateMantissa});

            // Get the normalized price of the asset
            vars.oraclePriceMantissa = oracle.getUnderlyingPrice(asset);
            if (vars.oraclePriceMantissa == 0) {
                return (Error.PRICE_ERROR, 0, 0);
            }
            vars.oraclePrice = mul_(Exp({mantissa: vars.oraclePriceMantissa}), 10**uint256(18 - EIP20Interface(asset.getUnderlying()).decimals()));

            // Pre-compute a conversion factor from tokens -> dai (normalized price value)
            vars.tokensToDenom = mul_(mul_(vars.collateralFactor, vars.exchangeRate), vars.oraclePrice);

            // sumCollateral += tokensToDenom * mTokenBalance
            vars.sumCollateral = mul_ScalarTruncateAddUInt(vars.tokensToDenom, vars.mTokenBalance, vars.sumCollateral);

            // Protection value calculation sumCollateral += protectionValueLocked
            // Mark to market value calculation sumCollateral += markToMarketValue
            uint protectionValueLocked;
            uint markToMarketValue;
            (protectionValueLocked, markToMarketValue) = liquidityMathModel.getTotalProtectionLockedValue(LiquidityMathModelInterface.LiquidityMathArgumentsSet(asset, _account, markets[address(asset)].collateralFactorMantissa, cprotection, oracle));
            if (vars.sumCollateral < mul_( protectionValueLocked, vars.collateralFactor)) {
                vars.sumCollateral = 0;
            } else {
                vars.sumCollateral = sub_(vars.sumCollateral, mul_( protectionValueLocked, vars.collateralFactor));
            }
            vars.sumCollateral = add_(vars.sumCollateral, protectionValueLocked);
            vars.sumCollateral = add_(vars.sumCollateral, markToMarketValue);

            // sumBorrowPlusEffects += oraclePrice * borrowBalance
            vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.oraclePrice, vars.borrowBalance, vars.sumBorrowPlusEffects);

            // Calculate effects of interacting with mTokenModify
            if (asset == mTokenModify) {
                // redeem effect
                // sumBorrowPlusEffects += tokensToDenom * redeemTokens
                vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.tokensToDenom, redeemTokens, vars.sumBorrowPlusEffects);

                // borrow effect
                // sumBorrowPlusEffects += oraclePrice * borrowAmount
                vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.oraclePrice, borrowAmount, vars.sumBorrowPlusEffects);

                _account = account;
            }
        }
        // These are safe, as the underflow condition is checked first
        if (vars.sumCollateral > vars.sumBorrowPlusEffects) {
            return (Error.NO_ERROR, vars.sumCollateral - vars.sumBorrowPlusEffects, 0);
        } else {
            return (Error.NO_ERROR, 0, vars.sumBorrowPlusEffects - vars.sumCollateral);
        }
    }

    /**
     * @notice Returns the value of possible optimization left for asset
     * @param asset The MToken address
     * @param account The owner of asset
     * @return The value of possible optimization
     */
    function getMaxOptimizableValue(MToken asset, address account) public view returns(uint){
        return liquidityMathModel.getMaxOptimizableValue(
            LiquidityMathModelInterface.LiquidityMathArgumentsSet(
                asset, account, markets[address(asset)].collateralFactorMantissa, cprotection, oracle
            )
        );
    }

    /**
     * @notice Returns the value of hypothetical optimization (ignoring existing optimization used) for asset
     * @param asset The MToken address
     * @param account The owner of asset
     * @return The amount of hypothetical optimization
     */
    function getHypotheticalOptimizableValue(MToken asset, address account) public view returns(uint){
        return liquidityMathModel.getHypotheticalOptimizableValue(
            LiquidityMathModelInterface.LiquidityMathArgumentsSet(
                asset, account, markets[address(asset)].collateralFactorMantissa, cprotection, oracle
            )
        );
    }

    function liquidateCalculateSeizeUserTokens(address mTokenBorrowed, address mTokenCollateral, uint actualRepayAmount, address account) external override view returns (uint, uint) {
        return LiquidationModelInterface(liquidationModel).liquidateCalculateSeizeUserTokens(
            LiquidationModelInterface.LiquidateCalculateSeizeUserTokensArgumentsSet(
                oracle,
                this,
                mTokenBorrowed,
                mTokenCollateral,
                actualRepayAmount,
                account,
                liquidationIncentiveMantissa
            )
        );
    }


    /**
     * @notice Returns the amount of a specific asset that is locked under all c-ops
     * @param asset The MToken address
     * @param account The owner of asset
     * @return The amount of asset locked under c-ops
     */
    function getUserLockedAmount(MToken asset, address account) public override view returns(uint) {
        uint protectionLockedAmount;
        address currency = asset.underlying();

        uint256 numOfProtections = cprotection.getUserUnderlyingProtectionTokenIdByCurrencySize(account, currency);

        for (uint i = 0; i < numOfProtections; i++) {
            uint cProtectionId = cprotection.getUserUnderlyingProtectionTokenIdByCurrency(account, currency, i);
            if(cprotection.isProtectionAlive(cProtectionId)){
                protectionLockedAmount = protectionLockedAmount + cprotection.getUnderlyingProtectionLockedAmount(cProtectionId);
            }
        }

        return protectionLockedAmount;
    }



    /*** Admin Functions ***/

    /**
      * @notice Sets a new price oracle for the moartroller
      * @dev Admin function to set a new price oracle
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function _setPriceOracle(PriceOracle newOracle) public returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PRICE_ORACLE_OWNER_CHECK);
        }

        // Track the old oracle for the moartroller
        PriceOracle oldOracle = oracle;

        // Set moartroller's oracle to newOracle
        oracle = newOracle;

        // Emit NewPriceOracle(oldOracle, newOracle)
        emit NewPriceOracle(oldOracle, newOracle);

        return uint(Error.NO_ERROR);
    }

    /**
     * @notice Sets a new CProtection that is allowed to use as a collateral optimisation
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _setProtection(address newCProtection) public returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PRICE_ORACLE_OWNER_CHECK);
        }

        MProtection oldCProtection = cprotection;
        cprotection = MProtection(newCProtection);

        // Emit NewPriceOracle(oldOracle, newOracle)
        emit NewCProtection(oldCProtection, cprotection);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sets the closeFactor used when liquidating borrows
      * @dev Admin function to set closeFactor
      * @param newCloseFactorMantissa New close factor, scaled by 1e18
      * @return uint 0=success, otherwise a failure
      */
    function _setCloseFactor(uint newCloseFactorMantissa) external returns (uint) {
        // Check caller is admin
    	require(msg.sender == admin, "only admin can set close factor");

        uint oldCloseFactorMantissa = closeFactorMantissa;
        closeFactorMantissa = newCloseFactorMantissa;
        emit NewCloseFactor(oldCloseFactorMantissa, closeFactorMantissa);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sets the collateralFactor for a market
      * @dev Admin function to set per-market collateralFactor
      * @param mToken The market to set the factor on
      * @param newCollateralFactorMantissa The new collateral factor, scaled by 1e18
      * @return uint 0=success, otherwise a failure. (See ErrorReporter for details)
      */
    function _setCollateralFactor(MToken mToken, uint newCollateralFactorMantissa) external returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_COLLATERAL_FACTOR_OWNER_CHECK);
        }

        // Verify market is listed
        Market storage market = markets[address(mToken)];
        if (!market.isListed) {
            return fail(Error.MARKET_NOT_LISTED, FailureInfo.SET_COLLATERAL_FACTOR_NO_EXISTS);
        }
    
        // TODO: this check is temporary switched off. we can make exception for UNN later

        // Exp memory newCollateralFactorExp = Exp({mantissa: newCollateralFactorMantissa});
        //
        //
        // Check collateral factor <= 0.9
        // Exp memory highLimit = Exp({mantissa: collateralFactorMaxMantissa});
        // if (lessThanExp(highLimit, newCollateralFactorExp)) {
        //     return fail(Error.INVALID_COLLATERAL_FACTOR, FailureInfo.SET_COLLATERAL_FACTOR_VALIDATION);
        // }

        // If collateral factor != 0, fail if price == 0
        if (newCollateralFactorMantissa != 0 && oracle.getUnderlyingPrice(mToken) == 0) {
            return fail(Error.PRICE_ERROR, FailureInfo.SET_COLLATERAL_FACTOR_WITHOUT_PRICE);
        }

        // Set market's collateral factor to new collateral factor, remember old value
        uint oldCollateralFactorMantissa = market.collateralFactorMantissa;
        market.collateralFactorMantissa = newCollateralFactorMantissa;

        // Emit event with asset, old collateral factor, and new collateral factor
        emit NewCollateralFactor(mToken, oldCollateralFactorMantissa, newCollateralFactorMantissa);

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Sets liquidationIncentive
      * @dev Admin function to set liquidationIncentive
      * @param newLiquidationIncentiveMantissa New liquidationIncentive scaled by 1e18
      * @return uint 0=success, otherwise a failure. (See ErrorReporter for details)
      */
    function _setLiquidationIncentive(uint newLiquidationIncentiveMantissa) external returns (uint) {
        // Check caller is admin
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_LIQUIDATION_INCENTIVE_OWNER_CHECK);
        }

        // Save current value for use in log
        uint oldLiquidationIncentiveMantissa = liquidationIncentiveMantissa;

        // Set liquidation incentive to new incentive
        liquidationIncentiveMantissa = newLiquidationIncentiveMantissa;

        // Emit event with old incentive, new incentive
        emit NewLiquidationIncentive(oldLiquidationIncentiveMantissa, newLiquidationIncentiveMantissa);

        return uint(Error.NO_ERROR);
    }

        function _setRewardClaimEnabled(bool status) external returns (uint) {
        // Check caller is admin
    	require(msg.sender == admin, "only admin can set close factor");
        rewardClaimEnabled = status;

        return uint(Error.NO_ERROR);
    }

    /**
      * @notice Add the market to the markets mapping and set it as listed
      * @dev Admin function to set isListed and add support for the market
      * @param mToken The address of the market (token) to list
      * @return uint 0=success, otherwise a failure. (See enum Error for details)
      */
    function _supportMarket(MToken mToken) external returns (uint) {
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SUPPORT_MARKET_OWNER_CHECK);
        }

        if (markets[address(mToken)].isListed) {
            return fail(Error.MARKET_ALREADY_LISTED, FailureInfo.SUPPORT_MARKET_EXISTS);
        }

        mToken.isMToken(); // Sanity check to make sure its really a MToken

        // Note that isMoared is not in active use anymore
        markets[address(mToken)] = Market({isListed: true, isMoared: false, collateralFactorMantissa: 0});
        tokenAddressToMToken[address(mToken.underlying())] = mToken;

        _addMarketInternal(address(mToken));

        emit MarketListed(mToken);

        return uint(Error.NO_ERROR);
    }

    function _addMarketInternal(address mToken) internal {
        for (uint i = 0; i < allMarkets.length; i ++) {
            require(allMarkets[i] != MToken(mToken), "market already added");
        }
        allMarkets.push(MToken(mToken));
    }

    /**
      * @notice Set the given borrow caps for the given mToken markets. Borrowing that brings total borrows to or above borrow cap will revert.
      * @dev Admin or borrowCapGuardian function to set the borrow caps. A borrow cap of 0 corresponds to unlimited borrowing.
      * @param mTokens The addresses of the markets (tokens) to change the borrow caps for
      * @param newBorrowCaps The new borrow cap values in underlying to be set. A value of 0 corresponds to unlimited borrowing.
      */
    function _setMarketBorrowCaps(MToken[] calldata mTokens, uint[] calldata newBorrowCaps) external {
    	require(msg.sender == admin || msg.sender == borrowCapGuardian, "only admin or borrow cap guardian can set borrow caps"); 

        uint numMarkets = mTokens.length;
        uint numBorrowCaps = newBorrowCaps.length;

        require(numMarkets != 0 && numMarkets == numBorrowCaps, "invalid input");

        for(uint i = 0; i < numMarkets; i++) {
            borrowCaps[address(mTokens[i])] = newBorrowCaps[i];
            emit NewBorrowCap(mTokens[i], newBorrowCaps[i]);
        }
    }

    /**
     * @notice Admin function to change the Borrow Cap Guardian
     * @param newBorrowCapGuardian The address of the new Borrow Cap Guardian
     */
    function _setBorrowCapGuardian(address newBorrowCapGuardian) external {
        require(msg.sender == admin, "only admin can set borrow cap guardian");

        // Save current value for inclusion in log
        address oldBorrowCapGuardian = borrowCapGuardian;

        // Store borrowCapGuardian with value newBorrowCapGuardian
        borrowCapGuardian = newBorrowCapGuardian;

        // Emit NewBorrowCapGuardian(OldBorrowCapGuardian, NewBorrowCapGuardian)
        emit NewBorrowCapGuardian(oldBorrowCapGuardian, newBorrowCapGuardian);
    }

    /**
     * @notice Admin function to change the Pause Guardian
     * @param newPauseGuardian The address of the new Pause Guardian
     * @return uint 0=success, otherwise a failure. (See enum Error for details)
     */
    function _setPauseGuardian(address newPauseGuardian) public returns (uint) {
        if (msg.sender != admin) {
            return fail(Error.UNAUTHORIZED, FailureInfo.SET_PAUSE_GUARDIAN_OWNER_CHECK);
        }

        // Save current value for inclusion in log
        address oldPauseGuardian = pauseGuardian;

        // Store pauseGuardian with value newPauseGuardian
        pauseGuardian = newPauseGuardian;

        // Emit NewPauseGuardian(OldPauseGuardian, NewPauseGuardian)
        emit NewPauseGuardian(oldPauseGuardian, pauseGuardian);

        return uint(Error.NO_ERROR);
    }

    function _setMintPaused(MToken mToken, bool state) public returns (bool) {
        require(markets[address(mToken)].isListed, "cannot pause a market that is not listed");
        require(msg.sender == pauseGuardian || msg.sender == admin, "only pause guardian and admin can pause");
        require(msg.sender == admin || state == true, "only admin can unpause");

        mintGuardianPaused[address(mToken)] = state;
        emit ActionPausedMToken(mToken, "Mint", state);
        return state;
    }

    function _setBorrowPaused(MToken mToken, bool state) public returns (bool) {
        require(markets[address(mToken)].isListed, "cannot pause a market that is not listed");
        require(msg.sender == pauseGuardian || msg.sender == admin, "only pause guardian and admin can pause");
        require(msg.sender == admin || state == true, "only admin can unpause");

        borrowGuardianPaused[address(mToken)] = state;
        emit ActionPausedMToken(mToken, "Borrow", state);
        return state;
    }

    function _setTransferPaused(bool state) public returns (bool) {
        require(msg.sender == pauseGuardian || msg.sender == admin, "only pause guardian and admin can pause");
        require(msg.sender == admin || state == true, "only admin can unpause");

        transferGuardianPaused = state;
        emit ActionPaused("Transfer", state);
        return state;
    }

    function _setSeizePaused(bool state) public returns (bool) {
        require(msg.sender == pauseGuardian || msg.sender == admin, "only pause guardian and admin can pause");
        require(msg.sender == admin || state == true, "only admin can unpause");

        seizeGuardianPaused = state;
        emit ActionPaused("Seize", state);
        return state;
    }

    /**
     * @notice Checks caller is admin, or this contract is becoming the new implementation
     */
    function adminOrInitializing() internal view returns (bool) {
        return msg.sender == admin || msg.sender == moartrollerImplementation;
    }

    /*** MOAR Distribution ***/

    /**
     * @notice Set MOAR speed for a single market
     * @param mToken The market whose MOAR speed to update
     * @param moarSpeed New MOAR speed for market
     */
    function setMoarSpeedInternal(MToken mToken, uint moarSpeed) internal {
        uint currentMoarSpeed = moarSpeeds[address(mToken)];
        if (currentMoarSpeed != 0) {
            // note that MOAR speed could be set to 0 to halt liquidity rewards for a market
            Exp memory borrowIndex = Exp({mantissa: mToken.borrowIndex()});
            updateMoarSupplyIndex(address(mToken));
            updateMoarBorrowIndex(address(mToken), borrowIndex);
        } else if (moarSpeed != 0) {
            // Add the MOAR market
            Market storage market = markets[address(mToken)];
            require(market.isListed == true, "MOAR market is not listed");

            if (moarSupplyState[address(mToken)].index == 0 && moarSupplyState[address(mToken)].block == 0) {
                moarSupplyState[address(mToken)] = MoarMarketState({
                    index: moarInitialIndex,
                    block: safe32(getBlockNumber(), "block number exceeds 32 bits")
                });
            }

            if (moarBorrowState[address(mToken)].index == 0 && moarBorrowState[address(mToken)].block == 0) {
                moarBorrowState[address(mToken)] = MoarMarketState({
                    index: moarInitialIndex,
                    block: safe32(getBlockNumber(), "block number exceeds 32 bits")
                });
            }
        }

        if (currentMoarSpeed != moarSpeed) {
            moarSpeeds[address(mToken)] = moarSpeed;
            emit MoarSpeedUpdated(mToken, moarSpeed);
        }
    }

    /**
     * @notice Accrue MOAR to the market by updating the supply index
     * @param mToken The market whose supply index to update
     */
    function updateMoarSupplyIndex(address mToken) internal {
        MoarMarketState storage supplyState = moarSupplyState[mToken];
        uint supplySpeed = moarSpeeds[mToken];
        uint blockNumber = getBlockNumber();
        uint deltaBlocks = sub_(blockNumber, uint(supplyState.block));
        if (deltaBlocks > 0 && supplySpeed > 0) {
            uint supplyTokens = MToken(mToken).totalSupply();
            uint moarAccrued = mul_(deltaBlocks, supplySpeed);
            Double memory ratio = supplyTokens > 0 ? fraction(moarAccrued, supplyTokens) : Double({mantissa: 0});
            Double memory index = add_(Double({mantissa: supplyState.index}), ratio);
            moarSupplyState[mToken] = MoarMarketState({
                index: safe224(index.mantissa, "new index exceeds 224 bits"),
                block: safe32(blockNumber, "block number exceeds 32 bits")
            });
        } else if (deltaBlocks > 0) {
            supplyState.block = safe32(blockNumber, "block number exceeds 32 bits");
        }
    }

    /**
     * @notice Accrue MOAR to the market by updating the borrow index
     * @param mToken The market whose borrow index to update
     */
    function updateMoarBorrowIndex(address mToken, Exp memory marketBorrowIndex) internal {
        MoarMarketState storage borrowState = moarBorrowState[mToken];
        uint borrowSpeed = moarSpeeds[mToken];
        uint blockNumber = getBlockNumber();
        uint deltaBlocks = sub_(blockNumber, uint(borrowState.block));
        if (deltaBlocks > 0 && borrowSpeed > 0) {
            uint borrowAmount = div_(MToken(mToken).totalBorrows(), marketBorrowIndex);
            uint moarAccrued = mul_(deltaBlocks, borrowSpeed);
            Double memory ratio = borrowAmount > 0 ? fraction(moarAccrued, borrowAmount) : Double({mantissa: 0});
            Double memory index = add_(Double({mantissa: borrowState.index}), ratio);
            moarBorrowState[mToken] = MoarMarketState({
                index: safe224(index.mantissa, "new index exceeds 224 bits"),
                block: safe32(blockNumber, "block number exceeds 32 bits")
            });
        } else if (deltaBlocks > 0) {
            borrowState.block = safe32(blockNumber, "block number exceeds 32 bits");
        }
    }

    /**
     * @notice Calculate MOAR accrued by a supplier and possibly transfer it to them
     * @param mToken The market in which the supplier is interacting
     * @param supplier The address of the supplier to distribute MOAR to
     */
    function distributeSupplierMoar(address mToken, address supplier) internal {
        MoarMarketState storage supplyState = moarSupplyState[mToken];
        Double memory supplyIndex = Double({mantissa: supplyState.index});
        Double memory supplierIndex = Double({mantissa: moarSupplierIndex[mToken][supplier]});
        moarSupplierIndex[mToken][supplier] = supplyIndex.mantissa;

        if (supplierIndex.mantissa == 0 && supplyIndex.mantissa > 0) {
            supplierIndex.mantissa = moarInitialIndex;
        }

        Double memory deltaIndex = sub_(supplyIndex, supplierIndex);
        uint supplierTokens = MToken(mToken).balanceOf(supplier);
        uint supplierDelta = mul_(supplierTokens, deltaIndex);
        uint supplierAccrued = add_(moarAccrued[supplier], supplierDelta);
        moarAccrued[supplier] = supplierAccrued;
        emit DistributedSupplierMoar(MToken(mToken), supplier, supplierDelta, supplyIndex.mantissa);
    }

    /**
     * @notice Calculate MOAR accrued by a borrower and possibly transfer it to them
     * @dev Borrowers will not begin to accrue until after the first interaction with the protocol.
     * @param mToken The market in which the borrower is interacting
     * @param borrower The address of the borrower to distribute MOAR to
     */
    function distributeBorrowerMoar(address mToken, address borrower, Exp memory marketBorrowIndex) internal {
        MoarMarketState storage borrowState = moarBorrowState[mToken];
        Double memory borrowIndex = Double({mantissa: borrowState.index});
        Double memory borrowerIndex = Double({mantissa: moarBorrowerIndex[mToken][borrower]});
        moarBorrowerIndex[mToken][borrower] = borrowIndex.mantissa;
        
        if (borrowerIndex.mantissa > 0) {
            Double memory deltaIndex = sub_(borrowIndex, borrowerIndex);
            uint borrowerAmount = div_(MToken(mToken).borrowBalanceStored(borrower), marketBorrowIndex);
            uint borrowerDelta = mul_(borrowerAmount, deltaIndex);
            uint borrowerAccrued = add_(moarAccrued[borrower], borrowerDelta);
            moarAccrued[borrower] = borrowerAccrued;
            emit DistributedBorrowerMoar(MToken(mToken), borrower, borrowerDelta, borrowIndex.mantissa);
        }
    }

    /**
     * @notice Calculate additional accrued MOAR for a contributor since last accrual
     * @param contributor The address to calculate contributor rewards for
     */
    function updateContributorRewards(address contributor) public {
        uint moarSpeed = moarContributorSpeeds[contributor];
        uint blockNumber = getBlockNumber();
        uint deltaBlocks = sub_(blockNumber, lastContributorBlock[contributor]);
        if (deltaBlocks > 0 && moarSpeed > 0) {
            uint newAccrued = mul_(deltaBlocks, moarSpeed);
            uint contributorAccrued = add_(moarAccrued[contributor], newAccrued);

            moarAccrued[contributor] = contributorAccrued;
            lastContributorBlock[contributor] = blockNumber;
        }
    }

    /**
     * @notice Claim all the MOAR accrued by holder in all markets
     * @param holder The address to claim MOAR for
     */
    function claimMoarReward(address holder) public {
        return claimMoar(holder, allMarkets);
    }

    /**
     * @notice Claim all the MOAR accrued by holder in the specified markets
     * @param holder The address to claim MOAR for
     * @param mTokens The list of markets to claim MOAR in
     */
    function claimMoar(address holder, MToken[] memory mTokens) public {
        address[] memory holders = new address[](1);
        holders[0] = holder;
        claimMoar(holders, mTokens, true, true);
    }

    /**
     * @notice Claim all MOAR accrued by the holders
     * @param holders The addresses to claim MOAR for
     * @param mTokens The list of markets to claim MOAR in
     * @param borrowers Whether or not to claim MOAR earned by borrowing
     * @param suppliers Whether or not to claim MOAR earned by supplying
     */
    function claimMoar(address[] memory holders, MToken[] memory mTokens, bool borrowers, bool suppliers) public {
        require(rewardClaimEnabled, "reward claim is disabled");
        for (uint i = 0; i < mTokens.length; i++) {
            MToken mToken = mTokens[i];
            require(markets[address(mToken)].isListed, "market must be listed");
            if (borrowers == true) {
                Exp memory borrowIndex = Exp({mantissa: mToken.borrowIndex()});
                updateMoarBorrowIndex(address(mToken), borrowIndex);
                for (uint j = 0; j < holders.length; j++) {
                    distributeBorrowerMoar(address(mToken), holders[j], borrowIndex);
                    moarAccrued[holders[j]] = grantMoarInternal(holders[j], moarAccrued[holders[j]]);
                }
            }
            if (suppliers == true) {
                updateMoarSupplyIndex(address(mToken));
                for (uint j = 0; j < holders.length; j++) {
                    distributeSupplierMoar(address(mToken), holders[j]);
                    moarAccrued[holders[j]] = grantMoarInternal(holders[j], moarAccrued[holders[j]]);
                }
            }
        }
    }

    /**
     * @notice Transfer MOAR to the user
     * @dev Note: If there is not enough MOAR, we do not perform the transfer all.
     * @param user The address of the user to transfer MOAR to
     * @param amount The amount of MOAR to (possibly) transfer
     * @return The amount of MOAR which was NOT transferred to the user
     */
    function grantMoarInternal(address user, uint amount) internal returns (uint) {
        EIP20Interface moar = EIP20Interface(getMoarAddress());
        uint moarRemaining = moar.balanceOf(address(this));
        if (amount > 0 && amount <= moarRemaining) {
            moar.approve(mProxy, amount);
            MProxyInterface(mProxy).proxyClaimReward(getMoarAddress(), user, amount);
            return 0;
        }
        return amount;
    }

    /*** MOAR Distribution Admin ***/

    /**
     * @notice Transfer MOAR to the recipient
     * @dev Note: If there is not enough MOAR, we do not perform the transfer all.
     * @param recipient The address of the recipient to transfer MOAR to
     * @param amount The amount of MOAR to (possibly) transfer
     */
    function _grantMoar(address recipient, uint amount) public {
        require(adminOrInitializing(), "only admin can grant MOAR");
        uint amountLeft = grantMoarInternal(recipient, amount);
        require(amountLeft == 0, "insufficient MOAR for grant");
        emit MoarGranted(recipient, amount);
    }

    /**
     * @notice Set MOAR speed for a single market
     * @param mToken The market whose MOAR speed to update
     * @param moarSpeed New MOAR speed for market
     */
    function _setMoarSpeed(MToken mToken, uint moarSpeed) public {
        require(adminOrInitializing(), "only admin can set MOAR speed");
        setMoarSpeedInternal(mToken, moarSpeed);
    }

    /**
     * @notice Set MOAR speed for a single contributor
     * @param contributor The contributor whose MOAR speed to update
     * @param moarSpeed New MOAR speed for contributor
     */
    function _setContributorMoarSpeed(address contributor, uint moarSpeed) public {
        require(adminOrInitializing(), "only admin can set MOAR speed");

        // note that MOAR speed could be set to 0 to halt liquidity rewards for a contributor
        updateContributorRewards(contributor);
        if (moarSpeed == 0) {
            // release storage
            delete lastContributorBlock[contributor];
        } else {
            lastContributorBlock[contributor] = getBlockNumber();
        }
        moarContributorSpeeds[contributor] = moarSpeed;

        emit ContributorMoarSpeedUpdated(contributor, moarSpeed);
    }

    /**
     * @notice Set liquidity math model implementation
     * @param mathModel the math model implementation
     */
    function _setLiquidityMathModel(LiquidityMathModelInterface mathModel) public {
        require(msg.sender == admin, "only admin can set liquidity math model implementation");

        LiquidityMathModelInterface oldLiquidityMathModel = liquidityMathModel;
        liquidityMathModel = mathModel;

        emit NewLiquidityMathModel(address(oldLiquidityMathModel), address(liquidityMathModel));
    }

    /**
     * @notice Set liquidation model implementation
     * @param newLiquidationModel the liquidation model implementation
     */
    function _setLiquidationModel(LiquidationModelInterface newLiquidationModel) public {
        require(msg.sender == admin, "only admin can set liquidation model implementation");

        LiquidationModelInterface oldLiquidationModel = liquidationModel;
        liquidationModel = newLiquidationModel;

        emit NewLiquidationModel(address(oldLiquidationModel), address(liquidationModel));
    }


    function _setMoarToken(address moarTokenAddress) public {
        require(msg.sender == admin, "only admin can set MOAR token address");
        moarToken = moarTokenAddress;
    }

    function _setMProxy(address mProxyAddress) public {
        require(msg.sender == admin, "only admin can set MProxy address");
        mProxy = mProxyAddress;
    }

    /**
     * @notice Add new privileged address 
     * @param privilegedAddress address to add
     */
    function _addPrivilegedAddress(address privilegedAddress) public {
        require(msg.sender == admin, "only admin can set liquidity math model implementation");
        privilegedAddresses[privilegedAddress] = 1;
    }

    /**
     * @notice Remove privileged address 
     * @param privilegedAddress address to remove
     */
    function _removePrivilegedAddress(address privilegedAddress) public {
        require(msg.sender == admin, "only admin can set liquidity math model implementation");
        delete privilegedAddresses[privilegedAddress];
    }

    /**
     * @notice Check if address if privileged
     * @param privilegedAddress address to check
     */
    function isPrivilegedAddress(address privilegedAddress) public view returns (bool) {
        return privilegedAddresses[privilegedAddress] == 1;
    }
    
    /**
     * @notice Return all of the markets
     * @dev The automatic getter may be used to access an individual market.
     * @return The list of market addresses
     */
    function getAllMarkets() public view returns (MToken[] memory) {
        return allMarkets;
    }

    function getBlockNumber() public view returns (uint) {
        return block.number;
    }

    /**
     * @notice Return the address of the MOAR token
     * @return The address of MOAR
     */
    function getMoarAddress() public view returns (address) {
        return moarToken;
    }

    function getContractVersion() external override pure returns(string memory){
        return "V1";
    }
}

File 4 of 53 : AbstractInterestRateModel.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./Interfaces/InterestRateModelInterface.sol";

abstract contract AbstractInterestRateModel is InterestRateModelInterface {

    /// @notice Indicator that this is an InterestRateModel contract (for inspection)
    bool public constant isInterestRateModel = true;
}

File 5 of 53 : WETHInterface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

interface WETHInterface {

    /**
     * @notice Get the total number of tokens in circulation
     * @return The supply of tokens
     */
    function totalSupply() external view returns (uint256);

    /**
     * @notice Gets the balance of the specified address
     * @param owner The address from which the balance will be retrieved
     * @return balance The balance
     */
    function balanceOf(address owner) external view returns (uint256 balance);

      /**
      * @notice Transfer `amount` tokens from `msg.sender` to `dst`
      * @param dst The address of the destination account
      * @param amount The number of tokens to transfer
      * @return success Whether or not the transfer succeeded
      */
    function transfer(address dst, uint256 amount) external returns (bool success);

    /**
      * @notice Transfer `amount` tokens from `src` to `dst`
      * @param src The address of the source account
      * @param dst The address of the destination account
      * @param amount The number of tokens to transfer
      * @return success Whether or not the transfer succeeded
      */
    function transferFrom(address src, address dst, uint256 amount) external returns (bool success);

    /**
      * @notice Approve `spender` to transfer up to `amount` from `src`
      * @dev This will overwrite the approval amount for `spender`
      *  and is subject to issues noted [here](https://eips.ethereum.org/EIPS/eip-20#approve)
      * @param spender The address of the account which may transfer tokens
      * @param amount The number of tokens that are approved
      * @return success Whether or not the approval succeeded
      */
    function approve(address spender, uint256 amount) external returns (bool success);

    /**
      * @notice Get the current allowance from `owner` for `spender`
      * @param owner The address of the account which owns the tokens to be spent
      * @param spender The address of the account which may transfer tokens
      * @return remaining The number of tokens allowed to be spent
      */
    function allowance(address owner, address spender) external view returns (uint256 remaining);

    function deposit() external payable;
    function withdraw(uint wad) external; 

    event Transfer(address indexed from, address indexed to, uint256 amount);
    event Approval(address indexed owner, address indexed spender, uint256 amount);
    event Deposit(address indexed dst, uint wad);
    event Withdrawal(address indexed src, uint wad);
}

File 6 of 53 : EIP20Interface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

/**
 * @title ERC 20 Token Standard Interface
 *  https://eips.ethereum.org/EIPS/eip-20
 */
interface EIP20Interface {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);

    /**
      * @notice Get the total number of tokens in circulation
      * @return The supply of tokens
      */
    function totalSupply() external view returns (uint256);

    /**
     * @notice Gets the balance of the specified address
     * @param owner The address from which the balance will be retrieved
     * @return balance The balance
     */
    function balanceOf(address owner) external view returns (uint256);

    /**
      * @notice Transfer `amount` tokens from `msg.sender` to `dst`
      * @param dst The address of the destination account
      * @param amount The number of tokens to transfer
      * @return success Whether or not the transfer succeeded
      */
    function transfer(address dst, uint256 amount) external returns (bool);

    /**
      * @notice Transfer `amount` tokens from `src` to `dst`
      * @param src The address of the source account
      * @param dst The address of the destination account
      * @param amount The number of tokens to transfer
      * @return success Whether or not the transfer succeeded
      */
    function transferFrom(address src, address dst, uint256 amount) external returns (bool);

    /**
      * @notice Approve `spender` to transfer up to `amount` from `src`
      * @dev This will overwrite the approval amount for `spender`
      *  and is subject to issues noted [here](https://eips.ethereum.org/EIPS/eip-20#approve)
      * @param spender The address of the account which may transfer tokens
      * @param amount The number of tokens that are approved (-1 means infinite)
      * @return success Whether or not the approval succeeded
      */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
      * @notice Get the current allowance from `owner` for `spender`
      * @param owner The address of the account which owns the tokens to be spent
      * @param spender The address of the account which may transfer tokens
      * @return remaining The number of tokens allowed to be spent (-1 means infinite)
      */
    function allowance(address owner, address spender) external view returns (uint256);

    event Transfer(address indexed from, address indexed to, uint256 amount);
    event Approval(address indexed owner, address indexed spender, uint256 amount);
}

File 7 of 53 : SafeEIP20.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../Interfaces/EIP20Interface.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";

/**
 * @title SafeEIP20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * This is a forked version of Openzeppelin's SafeERC20 contract but supporting
 * EIP20Interface instead of Openzeppelin's IERC20
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeEIP20 {
    using SafeMath for uint256;
    using Address for address;

    function safeTransfer(EIP20Interface token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(EIP20Interface token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(EIP20Interface token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        // solhint-disable-next-line max-line-length
        require((value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(EIP20Interface token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).add(value);
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(EIP20Interface token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(EIP20Interface token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        if (returndata.length > 0) { // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

File 8 of 53 : ErrorReporter.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

contract MoartrollerErrorReporter {
    enum Error {
        NO_ERROR,
        UNAUTHORIZED,
        MOARTROLLER_MISMATCH,
        INSUFFICIENT_SHORTFALL,
        INSUFFICIENT_LIQUIDITY,
        INVALID_CLOSE_FACTOR,
        INVALID_COLLATERAL_FACTOR,
        INVALID_LIQUIDATION_INCENTIVE,
        MARKET_NOT_ENTERED, // no longer possible
        MARKET_NOT_LISTED,
        MARKET_ALREADY_LISTED,
        MATH_ERROR,
        NONZERO_BORROW_BALANCE,
        PRICE_ERROR,
        REJECTION,
        SNAPSHOT_ERROR,
        TOO_MANY_ASSETS,
        TOO_MUCH_REPAY
    }

    enum FailureInfo {
        ACCEPT_ADMIN_PENDING_ADMIN_CHECK,
        ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK,
        EXIT_MARKET_BALANCE_OWED,
        EXIT_MARKET_REJECTION,
        SET_CLOSE_FACTOR_OWNER_CHECK,
        SET_CLOSE_FACTOR_VALIDATION,
        SET_COLLATERAL_FACTOR_OWNER_CHECK,
        SET_COLLATERAL_FACTOR_NO_EXISTS,
        SET_COLLATERAL_FACTOR_VALIDATION,
        SET_COLLATERAL_FACTOR_WITHOUT_PRICE,
        SET_IMPLEMENTATION_OWNER_CHECK,
        SET_LIQUIDATION_INCENTIVE_OWNER_CHECK,
        SET_LIQUIDATION_INCENTIVE_VALIDATION,
        SET_MAX_ASSETS_OWNER_CHECK,
        SET_PENDING_ADMIN_OWNER_CHECK,
        SET_PENDING_IMPLEMENTATION_OWNER_CHECK,
        SET_PRICE_ORACLE_OWNER_CHECK,
        SUPPORT_MARKET_EXISTS,
        SUPPORT_MARKET_OWNER_CHECK,
        SUPPORT_PROTECTION_OWNER_CHECK,
        SET_PAUSE_GUARDIAN_OWNER_CHECK
    }

    /**
      * @dev `error` corresponds to enum Error; `info` corresponds to enum FailureInfo, and `detail` is an arbitrary
      * contract-specific code that enables us to report opaque error codes from upgradeable contracts.
      **/
    event Failure(uint error, uint info, uint detail);

    /**
      * @dev use this when reporting a known error from the money market or a non-upgradeable collaborator
      */
    function fail(Error err, FailureInfo info) internal returns (uint) {
        emit Failure(uint(err), uint(info), 0);

        return uint(err);
    }

    /**
      * @dev use this when reporting an opaque error from an upgradeable collaborator contract
      */
    function failOpaque(Error err, FailureInfo info, uint opaqueError) internal returns (uint) {
        emit Failure(uint(err), uint(info), opaqueError);

        return uint(err);
    }
}

contract TokenErrorReporter {
    enum Error {
        NO_ERROR,
        UNAUTHORIZED,
        BAD_INPUT,
        MOARTROLLER_REJECTION,
        MOARTROLLER_CALCULATION_ERROR,
        INTEREST_RATE_MODEL_ERROR,
        INVALID_ACCOUNT_PAIR,
        INVALID_CLOSE_AMOUNT_REQUESTED,
        INVALID_COLLATERAL_FACTOR,
        MATH_ERROR,
        MARKET_NOT_FRESH,
        MARKET_NOT_LISTED,
        TOKEN_INSUFFICIENT_ALLOWANCE,
        TOKEN_INSUFFICIENT_BALANCE,
        TOKEN_INSUFFICIENT_CASH,
        TOKEN_TRANSFER_IN_FAILED,
        TOKEN_TRANSFER_OUT_FAILED
    }

    /*
     * Note: FailureInfo (but not Error) is kept in alphabetical order
     *       This is because FailureInfo grows significantly faster, and
     *       the order of Error has some meaning, while the order of FailureInfo
     *       is entirely arbitrary.
     */
    enum FailureInfo {
        ACCEPT_ADMIN_PENDING_ADMIN_CHECK,
        ACCRUE_INTEREST_ACCUMULATED_INTEREST_CALCULATION_FAILED,
        ACCRUE_INTEREST_BORROW_RATE_CALCULATION_FAILED,
        ACCRUE_INTEREST_NEW_BORROW_INDEX_CALCULATION_FAILED,
        ACCRUE_INTEREST_NEW_TOTAL_BORROWS_CALCULATION_FAILED,
        ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED,
        ACCRUE_INTEREST_SIMPLE_INTEREST_FACTOR_CALCULATION_FAILED,
        BORROW_ACCUMULATED_BALANCE_CALCULATION_FAILED,
        BORROW_ACCRUE_INTEREST_FAILED,
        BORROW_CASH_NOT_AVAILABLE,
        BORROW_FRESHNESS_CHECK,
        BORROW_NEW_TOTAL_BALANCE_CALCULATION_FAILED,
        BORROW_NEW_ACCOUNT_BORROW_BALANCE_CALCULATION_FAILED,
        BORROW_MARKET_NOT_LISTED,
        BORROW_MOARTROLLER_REJECTION,
        LIQUIDATE_ACCRUE_BORROW_INTEREST_FAILED,
        LIQUIDATE_ACCRUE_COLLATERAL_INTEREST_FAILED,
        LIQUIDATE_COLLATERAL_FRESHNESS_CHECK,
        LIQUIDATE_MOARTROLLER_REJECTION,
        LIQUIDATE_MOARTROLLER_CALCULATE_AMOUNT_SEIZE_FAILED,
        LIQUIDATE_CLOSE_AMOUNT_IS_UINT_MAX,
        LIQUIDATE_CLOSE_AMOUNT_IS_ZERO,
        LIQUIDATE_FRESHNESS_CHECK,
        LIQUIDATE_LIQUIDATOR_IS_BORROWER,
        LIQUIDATE_REPAY_BORROW_FRESH_FAILED,
        LIQUIDATE_SEIZE_BALANCE_INCREMENT_FAILED,
        LIQUIDATE_SEIZE_BALANCE_DECREMENT_FAILED,
        LIQUIDATE_SEIZE_MOARTROLLER_REJECTION,
        LIQUIDATE_SEIZE_LIQUIDATOR_IS_BORROWER,
        LIQUIDATE_SEIZE_TOO_MUCH,
        MINT_ACCRUE_INTEREST_FAILED,
        MINT_MOARTROLLER_REJECTION,
        MINT_EXCHANGE_CALCULATION_FAILED,
        MINT_EXCHANGE_RATE_READ_FAILED,
        MINT_FRESHNESS_CHECK,
        MINT_NEW_ACCOUNT_BALANCE_CALCULATION_FAILED,
        MINT_NEW_TOTAL_SUPPLY_CALCULATION_FAILED,
        MINT_TRANSFER_IN_FAILED,
        MINT_TRANSFER_IN_NOT_POSSIBLE,
        REDEEM_ACCRUE_INTEREST_FAILED,
        REDEEM_MOARTROLLER_REJECTION,
        REDEEM_EXCHANGE_TOKENS_CALCULATION_FAILED,
        REDEEM_EXCHANGE_AMOUNT_CALCULATION_FAILED,
        REDEEM_EXCHANGE_RATE_READ_FAILED,
        REDEEM_FRESHNESS_CHECK,
        REDEEM_NEW_ACCOUNT_BALANCE_CALCULATION_FAILED,
        REDEEM_NEW_TOTAL_SUPPLY_CALCULATION_FAILED,
        REDEEM_TRANSFER_OUT_NOT_POSSIBLE,
        REDUCE_RESERVES_ACCRUE_INTEREST_FAILED,
        REDUCE_RESERVES_ADMIN_CHECK,
        REDUCE_RESERVES_CASH_NOT_AVAILABLE,
        REDUCE_RESERVES_FRESH_CHECK,
        REDUCE_RESERVES_VALIDATION,
        REPAY_BEHALF_ACCRUE_INTEREST_FAILED,
        REPAY_BORROW_ACCRUE_INTEREST_FAILED,
        REPAY_BORROW_ACCUMULATED_BALANCE_CALCULATION_FAILED,
        REPAY_BORROW_MOARTROLLER_REJECTION,
        REPAY_BORROW_FRESHNESS_CHECK,
        REPAY_BORROW_NEW_ACCOUNT_BORROW_BALANCE_CALCULATION_FAILED,
        REPAY_BORROW_NEW_TOTAL_BALANCE_CALCULATION_FAILED,
        REPAY_BORROW_TRANSFER_IN_NOT_POSSIBLE,
        SET_COLLATERAL_FACTOR_OWNER_CHECK,
        SET_COLLATERAL_FACTOR_VALIDATION,
        SET_MOARTROLLER_OWNER_CHECK,
        SET_INTEREST_RATE_MODEL_ACCRUE_INTEREST_FAILED,
        SET_INTEREST_RATE_MODEL_FRESH_CHECK,
        SET_INTEREST_RATE_MODEL_OWNER_CHECK,
        SET_MAX_ASSETS_OWNER_CHECK,
        SET_ORACLE_MARKET_NOT_LISTED,
        SET_PENDING_ADMIN_OWNER_CHECK,
        SET_RESERVE_FACTOR_ACCRUE_INTEREST_FAILED,
        SET_RESERVE_FACTOR_ADMIN_CHECK,
        SET_RESERVE_FACTOR_FRESH_CHECK,
        SET_RESERVE_FACTOR_BOUNDS_CHECK,
        TRANSFER_MOARTROLLER_REJECTION,
        TRANSFER_NOT_ALLOWED,
        TRANSFER_NOT_ENOUGH,
        TRANSFER_TOO_MUCH,
        ADD_RESERVES_ACCRUE_INTEREST_FAILED,
        ADD_RESERVES_FRESH_CHECK,
        ADD_RESERVES_TRANSFER_IN_NOT_POSSIBLE
    }

    /**
      * @dev `error` corresponds to enum Error; `info` corresponds to enum FailureInfo, and `detail` is an arbitrary
      * contract-specific code that enables us to report opaque error codes from upgradeable contracts.
      **/
    event Failure(uint error, uint info, uint detail);

    /**
      * @dev use this when reporting a known error from the money market or a non-upgradeable collaborator
      */
    function fail(Error err, FailureInfo info) internal returns (uint) {
        emit Failure(uint(err), uint(info), 0);

        return uint(err);
    }

    /**
      * @dev use this when reporting an opaque error from an upgradeable collaborator contract
      */
    function failOpaque(Error err, FailureInfo info, uint opaqueError) internal returns (uint) {
        emit Failure(uint(err), uint(info), opaqueError);

        return uint(err);
    }
}

contract LiquidityMathModelErrorReporter {
    enum Error {
        NO_ERROR,
        UNAUTHORIZED,
        PRICE_ERROR,
        SNAPSHOT_ERROR
    }

    enum FailureInfo {
        ORACLE_PRICE_CHECK_FAILED
    }

    /**
      * @dev `error` corresponds to enum Error; `info` corresponds to enum FailureInfo, and `detail` is an arbitrary
      * contract-specific code that enables us to report opaque error codes from upgradeable contracts.
      **/
    event Failure(uint error, uint info, uint detail);

    /**
      * @dev use this when reporting a known error from the money market or a non-upgradeable collaborator
      */
    function fail(Error err, FailureInfo info) internal returns (uint) {
        emit Failure(uint(err), uint(info), 0);

        return uint(err);
    }

    /**
      * @dev use this when reporting an opaque error from an upgradeable collaborator contract
      */
    function failOpaque(Error err, FailureInfo info, uint opaqueError) internal returns (uint) {
        emit Failure(uint(err), uint(info), opaqueError);

        return uint(err);
    }
}

File 9 of 53 : Exponential.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

import "./CarefulMath.sol";
import "./ExponentialNoError.sol";

/**
 * @title Exponential module for storing fixed-precision decimals
 * @dev Legacy contract for compatibility reasons with existing contracts that still use MathError
 * @notice Exp is a struct which stores decimals with a fixed precision of 18 decimal places.
 *         Thus, if we wanted to store the 5.1, mantissa would store 5.1e18. That is:
 *         `Exp({mantissa: 5100000000000000000})`.
 */
contract Exponential is CarefulMath, ExponentialNoError {
    /**
     * @dev Creates an exponential from numerator and denominator values.
     *      Note: Returns an error if (`num` * 10e18) > MAX_INT,
     *            or if `denom` is zero.
     */
    function getExp(uint num, uint denom) pure internal returns (MathError, Exp memory) {
        (MathError err0, uint scaledNumerator) = mulUInt(num, expScale);
        if (err0 != MathError.NO_ERROR) {
            return (err0, Exp({mantissa: 0}));
        }

        (MathError err1, uint rational) = divUInt(scaledNumerator, denom);
        if (err1 != MathError.NO_ERROR) {
            return (err1, Exp({mantissa: 0}));
        }

        return (MathError.NO_ERROR, Exp({mantissa: rational}));
    }

    /**
     * @dev Adds two exponentials, returning a new exponential.
     */
    function addExp(Exp memory a, Exp memory b) pure internal returns (MathError, Exp memory) {
        (MathError error, uint result) = addUInt(a.mantissa, b.mantissa);

        return (error, Exp({mantissa: result}));
    }

    /**
     * @dev Subtracts two exponentials, returning a new exponential.
     */
    function subExp(Exp memory a, Exp memory b) pure internal returns (MathError, Exp memory) {
        (MathError error, uint result) = subUInt(a.mantissa, b.mantissa);

        return (error, Exp({mantissa: result}));
    }

    /**
     * @dev Multiply an Exp by a scalar, returning a new Exp.
     */
    function mulScalar(Exp memory a, uint scalar) pure internal returns (MathError, Exp memory) {
        (MathError err0, uint scaledMantissa) = mulUInt(a.mantissa, scalar);
        if (err0 != MathError.NO_ERROR) {
            return (err0, Exp({mantissa: 0}));
        }

        return (MathError.NO_ERROR, Exp({mantissa: scaledMantissa}));
    }

    /**
     * @dev Multiply an Exp by a scalar, then truncate to return an unsigned integer.
     */
    function mulScalarTruncate(Exp memory a, uint scalar) pure internal returns (MathError, uint) {
        (MathError err, Exp memory product) = mulScalar(a, scalar);
        if (err != MathError.NO_ERROR) {
            return (err, 0);
        }

        return (MathError.NO_ERROR, truncate(product));
    }

    /**
     * @dev Multiply an Exp by a scalar, truncate, then add an to an unsigned integer, returning an unsigned integer.
     */
    function mulScalarTruncateAddUInt(Exp memory a, uint scalar, uint addend) pure internal returns (MathError, uint) {
        (MathError err, Exp memory product) = mulScalar(a, scalar);
        if (err != MathError.NO_ERROR) {
            return (err, 0);
        }

        return addUInt(truncate(product), addend);
    }

    /**
     * @dev Divide an Exp by a scalar, returning a new Exp.
     */
    function divScalar(Exp memory a, uint scalar) pure internal returns (MathError, Exp memory) {
        (MathError err0, uint descaledMantissa) = divUInt(a.mantissa, scalar);
        if (err0 != MathError.NO_ERROR) {
            return (err0, Exp({mantissa: 0}));
        }

        return (MathError.NO_ERROR, Exp({mantissa: descaledMantissa}));
    }

    /**
     * @dev Divide a scalar by an Exp, returning a new Exp.
     */
    function divScalarByExp(uint scalar, Exp memory divisor) pure internal returns (MathError, Exp memory) {
        /*
          We are doing this as:
          getExp(mulUInt(expScale, scalar), divisor.mantissa)

          How it works:
          Exp = a / b;
          Scalar = s;
          `s / (a / b)` = `b * s / a` and since for an Exp `a = mantissa, b = expScale`
        */
        (MathError err0, uint numerator) = mulUInt(expScale, scalar);
        if (err0 != MathError.NO_ERROR) {
            return (err0, Exp({mantissa: 0}));
        }
        return getExp(numerator, divisor.mantissa);
    }

    /**
     * @dev Divide a scalar by an Exp, then truncate to return an unsigned integer.
     */
    function divScalarByExpTruncate(uint scalar, Exp memory divisor) pure internal returns (MathError, uint) {
        (MathError err, Exp memory fraction) = divScalarByExp(scalar, divisor);
        if (err != MathError.NO_ERROR) {
            return (err, 0);
        }

        return (MathError.NO_ERROR, truncate(fraction));
    }

    /**
     * @dev Multiplies two exponentials, returning a new exponential.
     */
    function mulExp(Exp memory a, Exp memory b) pure internal returns (MathError, Exp memory) {

        (MathError err0, uint doubleScaledProduct) = mulUInt(a.mantissa, b.mantissa);
        if (err0 != MathError.NO_ERROR) {
            return (err0, Exp({mantissa: 0}));
        }

        // We add half the scale before dividing so that we get rounding instead of truncation.
        //  See "Listing 6" and text above it at https://accu.org/index.php/journals/1717
        // Without this change, a result like 6.6...e-19 will be truncated to 0 instead of being rounded to 1e-18.
        (MathError err1, uint doubleScaledProductWithHalfScale) = addUInt(halfExpScale, doubleScaledProduct);
        if (err1 != MathError.NO_ERROR) {
            return (err1, Exp({mantissa: 0}));
        }

        (MathError err2, uint product) = divUInt(doubleScaledProductWithHalfScale, expScale);
        // The only error `div` can return is MathError.DIVISION_BY_ZERO but we control `expScale` and it is not zero.
        assert(err2 == MathError.NO_ERROR);

        return (MathError.NO_ERROR, Exp({mantissa: product}));
    }

    /**
     * @dev Multiplies two exponentials given their mantissas, returning a new exponential.
     */
    function mulExp(uint a, uint b) pure internal returns (MathError, Exp memory) {
        return mulExp(Exp({mantissa: a}), Exp({mantissa: b}));
    }

    /**
     * @dev Multiplies three exponentials, returning a new exponential.
     */
    function mulExp3(Exp memory a, Exp memory b, Exp memory c) pure internal returns (MathError, Exp memory) {
        (MathError err, Exp memory ab) = mulExp(a, b);
        if (err != MathError.NO_ERROR) {
            return (err, ab);
        }
        return mulExp(ab, c);
    }

    /**
     * @dev Divides two exponentials, returning a new exponential.
     *     (a/scale) / (b/scale) = (a/scale) * (scale/b) = a/b,
     *  which we can scale as an Exp by calling getExp(a.mantissa, b.mantissa)
     */
    function divExp(Exp memory a, Exp memory b) pure internal returns (MathError, Exp memory) {
        return getExp(a.mantissa, b.mantissa);
    }
}

File 10 of 53 : MTokenStorage.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./Moartroller.sol";
import "./AbstractInterestRateModel.sol";

abstract contract MTokenStorage {
    /**
     * @dev Guard variable for re-entrancy checks
     */
    bool internal _notEntered;

    /**
     * @dev EIP-20 token name for this token
     */
    string public name;

    /**
     * @dev EIP-20 token symbol for this token
     */
    string public symbol;

    /**
     * @dev EIP-20 token decimals for this token
     */
    uint8 public decimals;

    /**
     * @notice Underlying asset for this MToken
     */
    address public underlying;

    /**
     * @dev Maximum borrow rate that can ever be applied (.0005% / block)
     */

    uint internal borrowRateMaxMantissa;

    /**
     * @dev Maximum fraction of interest that can be set aside for reserves
     */
    uint internal reserveFactorMaxMantissa;

    /**
     * @dev Administrator for this contract
     */
    address payable public admin;

    /**
     * @dev Pending administrator for this contract
     */
    address payable public pendingAdmin;

    /**
     * @dev Contract which oversees inter-mToken operations
     */
    Moartroller public moartroller;

    /**
     * @dev Model which tells what the current interest rate should be
     */
    AbstractInterestRateModel public interestRateModel;

    /**
     * @dev Initial exchange rate used when minting the first MTokens (used when totalSupply = 0)
     */
    uint internal initialExchangeRateMantissa;

    /**
     * @dev Fraction of interest currently set aside for reserves
     */
    uint public reserveFactorMantissa;

    /**
     * @dev Fraction of reserves currently set aside for other usage
     */
    uint public reserveSplitFactorMantissa;

    /**
     * @dev Block number that interest was last accrued at
     */
    uint public accrualBlockNumber;

    /**
     * @dev Accumulator of the total earned interest rate since the opening of the market
     */
    uint public borrowIndex;

    /**
     * @dev Total amount of outstanding borrows of the underlying in this market
     */
    uint public totalBorrows;

    /**
     * @dev Total amount of reserves of the underlying held in this market
     */
    uint public totalReserves;

    /**
     * @dev Total number of tokens in circulation
     */
    uint public totalSupply;

    /**
     * @dev The Maximum Protection Moarosition (MPC) factor for collateral optimisation, default: 50% = 5000
     */
    uint public maxProtectionComposition;

    /**
     * @dev The Maximum Protection Moarosition (MPC) mantissa, default: 1e5
     */
    uint public maxProtectionCompositionMantissa;

    /**
     * @dev Official record of token balances for each account
     */
    mapping (address => uint) internal accountTokens;

    /**
     * @dev Approved token transfer amounts on behalf of others
     */
    mapping (address => mapping (address => uint)) internal transferAllowances;

    struct ProtectionUsage {
        uint256 protectionValueUsed;
    }

    /**
     * @dev Container for borrow balance information
     * @member principal Total balance (with accrued interest), after applying the most recent balance-changing action
     * @member interestIndex Global borrowIndex as of the most recent balance-changing action
     */
    struct BorrowSnapshot {
        uint principal;
        uint interestIndex;
        mapping (uint256 => ProtectionUsage) protectionsUsed;
    }

    struct AccrueInterestTempStorage{
        uint interestAccumulated;
        uint reservesAdded;
        uint splitedReserves_1;
        uint splitedReserves_2;
        uint totalBorrowsNew;
        uint totalReservesNew;
        uint borrowIndexNew;
    }

    /**
     * @dev Mapping of account addresses to outstanding borrow balances
     */
    mapping(address => BorrowSnapshot) public accountBorrows;


}

File 11 of 53 : MTokenInterface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./EIP20Interface.sol";

interface MTokenInterface {
    /*** User contract ***/
    function transfer(address dst, uint256 amount) external returns (bool);
    function transferFrom(address src, address dst, uint256 amount) external returns (bool);
    function approve(address spender, uint amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function balanceOfUnderlying(address owner) external returns (uint);
    function getAccountSnapshot(address account) external view returns (uint, uint, uint, uint);
    function borrowRatePerBlock() external view returns (uint);
    function supplyRatePerBlock() external view returns (uint);
    function totalBorrowsCurrent() external returns (uint);
    function borrowBalanceCurrent(address account) external returns (uint);
    function getCash() external view returns (uint);
    function seize(address liquidator, address borrower, uint seizeTokens) external returns (uint);
    function getUnderlying() external view returns(address);
    function sweepToken(EIP20Interface token) external;


    /*** Admin Functions ***/
    function _setPendingAdmin(address payable newPendingAdmin) external returns (uint);
    function _acceptAdmin() external returns (uint);
    function _setReserveFactor(uint newReserveFactorMantissa) external returns (uint);
    function _reduceReserves(uint reduceAmount) external returns (uint);
}

File 12 of 53 : MProxyInterface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

interface MProxyInterface {
  
    function proxyClaimReward(address asset, address recipient, uint amount) external;
    function proxySplitReserves(address asset, uint amount) external;
}

File 13 of 53 : CarefulMath.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

/**
  * @title Careful Math
  * @author MOAR
  * @notice Derived from OpenZeppelin's SafeMath library
  *         https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol
  */
contract CarefulMath {

    /**
     * @dev Possible error codes that we can return
     */
    enum MathError {
        NO_ERROR,
        DIVISION_BY_ZERO,
        INTEGER_OVERFLOW,
        INTEGER_UNDERFLOW
    }

    /**
    * @dev Multiplies two numbers, returns an error on overflow.
    */
    function mulUInt(uint a, uint b) internal pure returns (MathError, uint) {
        if (a == 0) {
            return (MathError.NO_ERROR, 0);
        }

        uint c = a * b;

        if (c / a != b) {
            return (MathError.INTEGER_OVERFLOW, 0);
        } else {
            return (MathError.NO_ERROR, c);
        }
    }

    /**
    * @dev Integer division of two numbers, truncating the quotient.
    */
    function divUInt(uint a, uint b) internal pure returns (MathError, uint) {
        if (b == 0) {
            return (MathError.DIVISION_BY_ZERO, 0);
        }

        return (MathError.NO_ERROR, a / b);
    }

    /**
    * @dev Subtracts two numbers, returns an error on overflow (i.e. if subtrahend is greater than minuend).
    */
    function subUInt(uint a, uint b) internal pure returns (MathError, uint) {
        if (b <= a) {
            return (MathError.NO_ERROR, a - b);
        } else {
            return (MathError.INTEGER_UNDERFLOW, 0);
        }
    }

    /**
    * @dev Adds two numbers, returns an error on overflow.
    */
    function addUInt(uint a, uint b) internal pure returns (MathError, uint) {
        uint c = a + b;

        if (c >= a) {
            return (MathError.NO_ERROR, c);
        } else {
            return (MathError.INTEGER_OVERFLOW, 0);
        }
    }

    /**
    * @dev add a and b and then subtract c
    */
    function addThenSubUInt(uint a, uint b, uint c) internal pure returns (MathError, uint) {
        (MathError err0, uint sum) = addUInt(a, b);

        if (err0 != MathError.NO_ERROR) {
            return (err0, 0);
        }

        return subUInt(sum, c);
    }
}

File 14 of 53 : ExponentialNoError.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

/**
 * @title Exponential module for storing fixed-precision decimals
 * @notice Exp is a struct which stores decimals with a fixed precision of 18 decimal places.
 *         Thus, if we wanted to store the 5.1, mantissa would store 5.1e18. That is:
 *         `Exp({mantissa: 5100000000000000000})`.
 */
contract ExponentialNoError {
    uint constant expScale = 1e18;
    uint constant doubleScale = 1e36;
    uint constant halfExpScale = expScale/2;
    uint constant mantissaOne = expScale;

    struct Exp {
        uint mantissa;
    }

    struct Double {
        uint mantissa;
    }

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

    /**
     * @dev Multiply an Exp by a scalar, then truncate to return an unsigned integer.
     */
    function mul_ScalarTruncate(Exp memory a, uint scalar) pure internal returns (uint) {
        Exp memory product = mul_(a, scalar);
        return truncate(product);
    }

    /**
     * @dev Multiply an Exp by a scalar, truncate, then add an to an unsigned integer, returning an unsigned integer.
     */
    function mul_ScalarTruncateAddUInt(Exp memory a, uint scalar, uint addend) pure internal returns (uint) {
        Exp memory product = mul_(a, scalar);
        return add_(truncate(product), addend);
    }

    /**
     * @dev Checks if first Exp is less than second Exp.
     */
    function lessThanExp(Exp memory left, Exp memory right) pure internal returns (bool) {
        return left.mantissa < right.mantissa;
    }

    /**
     * @dev Checks if left Exp <= right Exp.
     */
    function lessThanOrEqualExp(Exp memory left, Exp memory right) pure internal returns (bool) {
        return left.mantissa <= right.mantissa;
    }

    /**
     * @dev Checks if left Exp > right Exp.
     */
    function greaterThanExp(Exp memory left, Exp memory right) pure internal returns (bool) {
        return left.mantissa > right.mantissa;
    }

    /**
     * @dev returns true if Exp is exactly zero
     */
    function isZeroExp(Exp memory value) pure internal returns (bool) {
        return value.mantissa == 0;
    }

    function safe224(uint n, string memory errorMessage) pure internal returns (uint224) {
        require(n < 2**224, errorMessage);
        return uint224(n);
    }

    function safe32(uint n, string memory errorMessage) pure internal returns (uint32) {
        require(n < 2**32, errorMessage);
        return uint32(n);
    }

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

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

    function add_(uint a, uint b) pure internal returns (uint) {
        return add_(a, b, "addition overflow");
    }

    function add_(uint a, uint b, string memory errorMessage) pure internal returns (uint) {
        uint c = a + b;
        require(c >= a, errorMessage);
        return c;
    }

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

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

    function sub_(uint a, uint b) pure internal returns (uint) {
        return sub_(a, b, "subtraction underflow");
    }

    function sub_(uint a, uint b, string memory errorMessage) pure internal returns (uint) {
        require(b <= a, errorMessage);
        return a - b;
    }

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

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

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

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

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

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

    function mul_(uint a, uint b) pure internal returns (uint) {
        return mul_(a, b, "multiplication overflow");
    }

    function mul_(uint a, uint b, string memory errorMessage) pure internal returns (uint) {
        if (a == 0 || b == 0) {
            return 0;
        }
        uint c = a * b;
        require(c / a == b, errorMessage);
        return c;
    }

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

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

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

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

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

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

    function div_(uint a, uint b) pure internal returns (uint) {
        return div_(a, b, "divide by zero");
    }

    function div_(uint a, uint b, string memory errorMessage) pure internal returns (uint) {
        require(b > 0, errorMessage);
        return a / b;
    }

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

File 15 of 53 : PriceOracle.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "../MToken.sol";

interface PriceOracle {
    /**
      * @notice Get the underlying price of a mToken asset
      * @param mToken The mToken to get the underlying price of
      * @return The underlying asset price mantissa (scaled by 1e18).
      *  Zero means the price is unavailable.
      */
    function getUnderlyingPrice(MToken mToken) external view returns (uint);
}

File 16 of 53 : MoartrollerInterface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "../MToken.sol";
import "../Utils/ExponentialNoError.sol";

interface MoartrollerInterface {

    /**
 * @dev Local vars for avoiding stack-depth limits in calculating account liquidity.
 *  Note that `mTokenBalance` is the number of mTokens the account owns in the market,
 *  whereas `borrowBalance` is the amount of underlying that the account has borrowed.
 */
    struct AccountLiquidityLocalVars {
        uint sumCollateral;
        uint sumBorrowPlusEffects;
        uint mTokenBalance;
        uint borrowBalance;
        uint exchangeRateMantissa;
        uint oraclePriceMantissa;
        ExponentialNoError.Exp collateralFactor;
        ExponentialNoError.Exp exchangeRate;
        ExponentialNoError.Exp oraclePrice;
        ExponentialNoError.Exp tokensToDenom;
    }

    /*** Assets You Are In ***/

    function enterMarkets(address[] calldata mTokens) external returns (uint[] memory);
    function exitMarket(address mToken) external returns (uint);

    /*** Policy Hooks ***/

    function mintAllowed(address mToken, address minter, uint mintAmount) external returns (uint);

    function redeemAllowed(address mToken, address redeemer, uint redeemTokens) external returns (uint);
    function redeemVerify(address mToken, address redeemer, uint redeemAmount, uint redeemTokens) external;

    function borrowAllowed(address mToken, address borrower, uint borrowAmount) external returns (uint);

    function repayBorrowAllowed(
        address mToken,
        address payer,
        address borrower,
        uint repayAmount) external returns (uint);

    function liquidateBorrowAllowed(
        address mTokenBorrowed,
        address mTokenCollateral,
        address liquidator,
        address borrower,
        uint repayAmount) external returns (uint);

    function seizeAllowed(
        address mTokenCollateral,
        address mTokenBorrowed,
        address liquidator,
        address borrower,
        uint seizeTokens) external returns (uint);

    function transferAllowed(address mToken, address src, address dst, uint transferTokens) external returns (uint);

    /*** Liquidity/Liquidation Calculations ***/

    function liquidateCalculateSeizeUserTokens(
        address mTokenBorrowed,
        address mTokenCollateral,
        uint repayAmount,
        address account) external view returns (uint, uint);

    function getUserLockedAmount(MToken asset, address account) external view returns(uint);
}

File 17 of 53 : Versionable.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

interface Versionable {
    function getContractVersion() external pure returns (string memory);
}

File 18 of 53 : MoartrollerStorage.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./MToken.sol";
import "./Interfaces/PriceOracle.sol";
import "./Interfaces/LiquidityMathModelInterface.sol";
import "./Interfaces/LiquidationModelInterface.sol";
import "./MProtection.sol";

abstract contract UnitrollerAdminStorage {
    /**
    * @dev Administrator for this contract
    */
    address public admin;

    /**
    * @dev Pending administrator for this contract
    */
    address public pendingAdmin;

    /**
    * @dev Active brains of Unitroller
    */
    address public moartrollerImplementation;

    /**
    * @dev Pending brains of Unitroller
    */
    address public pendingMoartrollerImplementation;
}

contract MoartrollerV1Storage is UnitrollerAdminStorage {

    /**
     * @dev Oracle which gives the price of any given asset
     */
    PriceOracle public oracle;

    /**
     * @dev Multiplier used to calculate the maximum repayAmount when liquidating a borrow
     */
    uint public closeFactorMantissa;

    /**
     * @dev Multiplier representing the discount on collateral that a liquidator receives
     */
    uint public liquidationIncentiveMantissa;

    /**
     * @dev Max number of assets a single account can participate in (borrow or use as collateral)
     */
    uint public maxAssets;

    /**
     * @dev Per-account mapping of "assets you are in", capped by maxAssets
     */
    mapping(address => MToken[]) public accountAssets;

}

contract MoartrollerV2Storage is MoartrollerV1Storage {
    struct Market {
        // Whether or not this market is listed
        bool isListed;

        // Multiplier representing the most one can borrow against their collateral in this market.
        // For instance, 0.9 to allow borrowing 90% of collateral value.
        // Must be between 0 and 1, and stored as a mantissa.
        uint collateralFactorMantissa;

        // Per-market mapping of "accounts in this asset"
        mapping(address => bool) accountMembership;

        // Whether or not this market receives MOAR
        bool isMoared;
    }

    /**
     * @dev Official mapping of mTokens -> Market metadata
     * @dev Used e.g. to determine if a market is supported
     */
    mapping(address => Market) public markets;


    /**
     * @dev The Pause Guardian can pause certain actions as a safety mechanism.
     *  Actions which allow users to remove their own assets cannot be paused.
     *  Liquidation / seizing / transfer can only be paused globally, not by market.
     */
    address public pauseGuardian;
    bool public _mintGuardianPaused;
    bool public _borrowGuardianPaused;
    bool public transferGuardianPaused;
    bool public seizeGuardianPaused;
    mapping(address => bool) public mintGuardianPaused;
    mapping(address => bool) public borrowGuardianPaused;
}

contract MoartrollerV3Storage is MoartrollerV2Storage {
    struct MoarMarketState {
        // The market's last updated moarBorrowIndex or moarSupplyIndex
        uint224 index;

        // The block number the index was last updated at
        uint32 block;
    }

    /// @dev A list of all markets
    MToken[] public allMarkets;

    /// @dev The rate at which the flywheel distributes MOAR, per block
    uint public moarRate;

    /// @dev The portion of moarRate that each market currently receives
    mapping(address => uint) public moarSpeeds;

    /// @dev The MOAR market supply state for each market
    mapping(address => MoarMarketState) public moarSupplyState;

    /// @dev The MOAR market borrow state for each market
    mapping(address => MoarMarketState) public moarBorrowState;

    /// @dev The MOAR borrow index for each market for each supplier as of the last time they accrued MOAR
    mapping(address => mapping(address => uint)) public moarSupplierIndex;

    /// @dev The MOAR borrow index for each market for each borrower as of the last time they accrued MOAR
    mapping(address => mapping(address => uint)) public moarBorrowerIndex;

    /// @dev The MOAR accrued but not yet transferred to each user
    mapping(address => uint) public moarAccrued;
}

contract MoartrollerV4Storage is MoartrollerV3Storage {
    // @dev The borrowCapGuardian can set borrowCaps to any number for any market. Lowering the borrow cap could disable borrowing on the given market.
    address public borrowCapGuardian;

    // @dev Borrow caps enforced by borrowAllowed for each mToken address. Defaults to zero which corresponds to unlimited borrowing.
    mapping(address => uint) public borrowCaps;
}

contract MoartrollerV5Storage is MoartrollerV4Storage {
    /// @dev The portion of MOAR that each contributor receives per block
    mapping(address => uint) public moarContributorSpeeds;

    /// @dev Last block at which a contributor's MOAR rewards have been allocated
    mapping(address => uint) public lastContributorBlock;
}

contract MoartrollerV6Storage is MoartrollerV5Storage {
    /**
     * @dev Moar token address
     */
    address public moarToken;

    /**
     * @dev MProxy address
     */
    address public mProxy;
    
    /**
     * @dev CProtection contract which can be used for collateral optimisation
     */
    MProtection public cprotection;

    /**
     * @dev Mapping for basic token address to mToken
     */
    mapping(address => MToken) public tokenAddressToMToken;

    /**
     * @dev Math model for liquidity calculation
     */
    LiquidityMathModelInterface public liquidityMathModel;


    /**
     * @dev Liquidation model for liquidation related functions
     */
    LiquidationModelInterface public liquidationModel;



    /**
     * @dev List of addresses with privileged access
     */
    mapping(address => uint) public privilegedAddresses;

    /**
     * @dev Determines if reward claim feature is enabled
     */
    bool public rewardClaimEnabled;
}

File 19 of 53 : UnionGovernanceToken.sol
// Copyright (c) 2020 The UNION Protocol Foundation
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;

// import "hardhat/console.sol";

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/GSN/Context.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/EnumerableSet.sol";

/**
 * @title UNION Protocol Governance Token
 * @dev Implementation of the basic standard token.
 */
contract UnionGovernanceToken is AccessControl, IERC20 {

  using Address for address;
  using SafeMath for uint256;
  using EnumerableSet for EnumerableSet.AddressSet;

  /**
   * @notice Struct for marking number of votes from a given block
   * @member from
   * @member votes
   */
  struct VotingCheckpoint {
    uint256 from;
    uint256 votes;
  }
 
  /**
   * @notice Struct for locked tokens
   * @member amount
   * @member releaseTime
   * @member votable
   */
  struct LockedTokens{
    uint amount;
    uint releaseTime;
    bool votable;
  }

  /**
  * @notice Struct for EIP712 Domain
  * @member name
  * @member version
  * @member chainId
  * @member verifyingContract
  * @member salt
  */
  struct EIP712Domain {
    string name;
    string version;
    uint256 chainId;
    address verifyingContract;
    bytes32 salt;
  }

  /**
  * @notice Struct for EIP712 VotingDelegate call
  * @member owner
  * @member delegate
  * @member nonce
  * @member expirationTime
  */
  struct VotingDelegate {
    address owner;
    address delegate;
    uint256 nonce;
    uint256 expirationTime;
  }

  /**
  * @notice Struct for EIP712 Permit call
  * @member owner
  * @member spender
  * @member value
  * @member nonce
  * @member deadline
  */
  struct Permit {
    address owner;
    address spender;
    uint256 value;
    uint256 nonce;
    uint256 deadline;
  }

  /**
   * @notice Vote Delegation Events
   */
  event VotingDelegateChanged(address indexed _owner, address indexed _fromDelegate, address indexed _toDelegate);
  event VotingDelegateRemoved(address indexed _owner);
  
  /**
   * @notice Vote Balance Events
   * Emmitted when a delegate account's vote balance changes at the time of a written checkpoint
   */
  event VoteBalanceChanged(address indexed _account, uint256 _oldBalance, uint256 _newBalance);

  /**
   * @notice Transfer/Allocator Events
   */
  event TransferStatusChanged(bool _newTransferStatus);

  /**
   * @notice Reversion Events
   */
  event ReversionStatusChanged(bool _newReversionSetting);

  /**
   * @notice EIP-20 Approval event
   */
  event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  
  /**
   * @notice EIP-20 Transfer event
   */
  event Transfer(address indexed _from, address indexed _to, uint256 _value);

  event Burn(address indexed _from, uint256 _value);
  event AddressPermitted(address indexed _account);
  event AddressRestricted(address indexed _account);

  /**
   * @dev AccessControl recognized roles
   */
  bytes32 public constant ROLE_ADMIN = keccak256("ROLE_ADMIN");
  bytes32 public constant ROLE_ALLOCATE = keccak256("ROLE_ALLOCATE");
  bytes32 public constant ROLE_GOVERN = keccak256("ROLE_GOVERN");
  bytes32 public constant ROLE_MINT = keccak256("ROLE_MINT");
  bytes32 public constant ROLE_LOCK = keccak256("ROLE_LOCK");
  bytes32 public constant ROLE_TRUSTED = keccak256("ROLE_TRUSTED");
  bytes32 public constant ROLE_TEST = keccak256("ROLE_TEST");
   
  bytes32 public constant EIP712DOMAIN_TYPEHASH = keccak256(
    "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)"
  );
  bytes32 public constant DELEGATE_TYPEHASH = keccak256(
    "DelegateVote(address owner,address delegate,uint256 nonce,uint256 expirationTime)"
  );

  //keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;

  address private constant BURN_ADDRESS = address(0);
  address public UPGT_CONTRACT_ADDRESS;

  /**
   * @dev hashes to support EIP-712 signing and validating, EIP712DOMAIN_SEPARATOR is set at time of contract instantiation and token minting.
   */
  bytes32 public immutable EIP712DOMAIN_SEPARATOR;

  /**
   * @dev EIP-20 token name
   */
  string public name = "UNION Protocol Governance Token";

  /**
   * @dev EIP-20 token symbol
   */
  string public symbol = "UNN";

  /**
   * @dev EIP-20 token decimals
   */
  uint8 public decimals = 18;

  /**
   * @dev Contract version
   */
  string public constant version = '0.0.1';

  /**
   * @dev Initial amount of tokens
   */
  uint256 private uint256_initialSupply = 100000000000 * 10**18;

  /**
   * @dev Total amount of tokens
   */
  uint256 private uint256_totalSupply;

  /**
   * @dev Chain id
   */
  uint256 private uint256_chain_id;

  /**
   * @dev general transfer restricted as function of public sale not complete
   */
  bool private b_canTransfer = false;

  /**
   * @dev private variable that determines if failed EIP-20 functions revert() or return false.  Reversion short-circuits the return from these functions.
   */
  bool private b_revert = false; //false allows false return values

  /**
   * @dev Locked destinations list
   */
  mapping(address => bool) private m_lockedDestinations;

  /**
   * @dev EIP-20 allowance and balance maps
   */
  mapping(address => mapping(address => uint256)) private m_allowances;
  mapping(address => uint256) private m_balances;
  mapping(address => LockedTokens[]) private m_lockedBalances;

  /**
   * @dev nonces used by accounts to this contract for signing and validating signatures under EIP-712
   */
  mapping(address => uint256) private m_nonces;

  /**
   * @dev delegated account may for off-line vote delegation
   */
  mapping(address => address) private m_delegatedAccounts;

  /**
   * @dev delegated account inverse map is needed to live calculate voting power
   */
  mapping(address => EnumerableSet.AddressSet) private m_delegatedAccountsInverseMap;


  /**
   * @dev indexed mapping of vote checkpoints for each account
   */
  mapping(address => mapping(uint256 => VotingCheckpoint)) private m_votingCheckpoints;

  /**
   * @dev mapping of account addrresses to voting checkpoints
   */
  mapping(address => uint256) private m_accountVotingCheckpoints;

  /**
   * @dev Contructor for the token
   * @param _owner address of token contract owner
   * @param _initialSupply of tokens generated by this contract
   * Sets Transfer the total suppply to the owner.
   * Sets default admin role to the owner.
   * Sets ROLE_ALLOCATE to the owner.
   * Sets ROLE_GOVERN to the owner.
   * Sets ROLE_MINT to the owner.
   * Sets EIP 712 Domain Separator.
   */
  constructor(address _owner, uint256 _initialSupply) public {
    
    //set internal contract references
    UPGT_CONTRACT_ADDRESS = address(this);

    //setup roles using AccessControl
    _setupRole(DEFAULT_ADMIN_ROLE, _owner);
    _setupRole(ROLE_ADMIN, _owner);
    _setupRole(ROLE_ADMIN, _msgSender());
    _setupRole(ROLE_ALLOCATE, _owner);
    _setupRole(ROLE_ALLOCATE, _msgSender());
    _setupRole(ROLE_TRUSTED, _owner);
    _setupRole(ROLE_TRUSTED, _msgSender());
    _setupRole(ROLE_GOVERN, _owner);
    _setupRole(ROLE_MINT, _owner);
    _setupRole(ROLE_LOCK, _owner);
    _setupRole(ROLE_TEST, _owner);

    m_balances[_owner] = _initialSupply;
    uint256_totalSupply = _initialSupply;
    b_canTransfer = false;
    uint256_chain_id = _getChainId();

    EIP712DOMAIN_SEPARATOR = _hash(EIP712Domain({
        name : name,
        version : version,
        chainId : uint256_chain_id,
        verifyingContract : address(this),
        salt : keccak256(abi.encodePacked(name))
      }
    ));
   
    emit Transfer(BURN_ADDRESS, _owner, uint256_totalSupply);
  }

    /**
   * @dev Sets transfer status to lock token transfer
   * @param _canTransfer value can be true or false.
   * disables transfer when set to false and enables transfer when true
   * Only a member of ADMIN role can call to change transfer status
   */
  function setCanTransfer(bool _canTransfer) public {
    if(hasRole(ROLE_ADMIN, _msgSender())){
      b_canTransfer = _canTransfer;
      emit TransferStatusChanged(_canTransfer);
    }
  }

  /**
   * @dev Gets status of token transfer lock
   * @return true or false status of whether the token can be transfered
   */
  function getCanTransfer() public view returns (bool) {
    return b_canTransfer;
  }

  /**
   * @dev Sets transfer reversion status to either return false or throw on error
   * @param _reversion value can be true or false.
   * disables return of false values for transfer failures when set to false and enables transfer-related exceptions when true
   * Only a member of ADMIN role can call to change transfer reversion status
   */
  function setReversion(bool _reversion) public {
    if(hasRole(ROLE_ADMIN, _msgSender()) || 
       hasRole(ROLE_TEST, _msgSender())
    ) {
      b_revert = _reversion;
      emit ReversionStatusChanged(_reversion);
    }
  }

  /**
   * @dev Gets status of token transfer reversion
   * @return true or false status of whether the token transfer failures return false or are reverted
   */
  function getReversion() public view returns (bool) {
    return b_revert;
  }

  /**
   * @dev retrieve current chain id
   * @return chain id
   */
  function getChainId() public pure returns (uint256) {
    return _getChainId();
  }

  /**
   * @dev Retrieve current chain id
   * @return chain id
   */
  function _getChainId() internal pure returns (uint256) {
    uint256 id;
    assembly {
      id := chainid()
    }
    return id;
  }

  /**
   * @dev Retrieve total supply of tokens
   * @return uint256 total supply of tokens
   */
  function totalSupply() public view override returns (uint256) {
    return uint256_totalSupply;
  }

  /**
   * Balance related functions
   */

  /**
   * @dev Retrieve balance of a specified account
   * @param _account address of account holding balance
   * @return uint256 balance of the specified account address
   */
  function balanceOf(address _account) public view override returns (uint256) {
    return m_balances[_account].add(_calculateReleasedBalance(_account));
  }

  /**
   * @dev Retrieve locked balance of a specified account
   * @param _account address of account holding locked balance
   * @return uint256 locked balance of the specified account address
   */
  function lockedBalanceOf(address _account) public view returns (uint256) {
    return _calculateLockedBalance(_account);
  }

  /**
   * @dev Retrieve lenght of locked balance array for specific address
   * @param _account address of account holding locked balance
   * @return uint256 locked balance array lenght
   */
  function getLockedTokensListSize(address _account) public view returns (uint256){
    require(_msgSender() == _account || hasRole(ROLE_ADMIN, _msgSender()) || hasRole(ROLE_TRUSTED, _msgSender()), "UPGT_ERROR: insufficient permissions");
    return m_lockedBalances[_account].length;
  }

  /**
   * @dev Retrieve locked tokens struct from locked balance array for specific address
   * @param _account address of account holding locked tokens
   * @param _index index in array with locked tokens position
   * @return amount of locked tokens
   * @return releaseTime descibes time when tokens will be unlocked
   * @return votable flag is describing votability of tokens
   */
  function getLockedTokens(address _account, uint256 _index) public view returns (uint256 amount, uint256 releaseTime, bool votable){
    require(_msgSender() == _account || hasRole(ROLE_ADMIN, _msgSender()) || hasRole(ROLE_TRUSTED, _msgSender()), "UPGT_ERROR: insufficient permissions");
    require(_index < m_lockedBalances[_account].length, "UPGT_ERROR: LockedTokens position doesn't exist on given index");
    LockedTokens storage lockedTokens = m_lockedBalances[_account][_index];
    return (lockedTokens.amount, lockedTokens.releaseTime, lockedTokens.votable);
  }

  /**
   * @dev Calculates locked balance of a specified account
   * @param _account address of account holding locked balance
   * @return uint256 locked balance of the specified account address
   */
  function _calculateLockedBalance(address _account) private view returns (uint256) {
    uint256 lockedBalance = 0;
    for (uint i=0; i<m_lockedBalances[_account].length; i++) {
      if(m_lockedBalances[_account][i].releaseTime > block.timestamp){
        lockedBalance = lockedBalance.add(m_lockedBalances[_account][i].amount);
      }
    }
    return lockedBalance;
  }

  /**
   * @dev Calculates released balance of a specified account
   * @param _account address of account holding released balance
   * @return uint256 released balance of the specified account address
   */
  function _calculateReleasedBalance(address _account) private view returns (uint256) {
    uint256 releasedBalance = 0;
    for (uint i=0; i<m_lockedBalances[_account].length; i++) {
      if(m_lockedBalances[_account][i].releaseTime <= block.timestamp){
          releasedBalance = releasedBalance.add(m_lockedBalances[_account][i].amount);
      }
    }
    return releasedBalance;
  }

  /**
   * @dev Calculates locked votable balance of a specified account
   * @param _account address of account holding locked votable balance
   * @return uint256 locked votable balance of the specified account address
   */
  function _calculateLockedVotableBalance(address _account) private view returns (uint256) {
    uint256 lockedVotableBalance = 0;
    for (uint i=0; i<m_lockedBalances[_account].length; i++) {
      if(m_lockedBalances[_account][i].votable == true){
        lockedVotableBalance = lockedVotableBalance.add(m_lockedBalances[_account][i].amount);
      }
    }
    return lockedVotableBalance;
  }

  /**
   * @dev Moves released balance to normal balance for a specified account
   * @param _account address of account holding released balance
   */
  function _moveReleasedBalance(address _account) internal virtual{
    uint256 releasedToMove = 0;
    for (uint i=0; i<m_lockedBalances[_account].length; i++) {
      if(m_lockedBalances[_account][i].releaseTime <= block.timestamp){
        releasedToMove = releasedToMove.add(m_lockedBalances[_account][i].amount);
        m_lockedBalances[_account][i] = m_lockedBalances[_account][m_lockedBalances[_account].length - 1];
        m_lockedBalances[_account].pop();
      }
    }
    m_balances[_account] = m_balances[_account].add(releasedToMove);
  }

  /**
   * Allowance related functinons
   */

  /**
   * @dev Retrieve the spending allowance for a token holder by a specified account
   * @param _owner Token account holder
   * @param _spender Account given allowance
   * @return uint256 allowance value
   */
  function allowance(address _owner, address _spender) public override virtual view returns (uint256) {
    return m_allowances[_owner][_spender];
  }

  /**
   * @dev Message sender approval to spend for a specified amount
   * @param _spender address of party approved to spend
   * @param _value amount of the approval 
   * @return boolean success status 
   * public wrapper for _approve, _owner is msg.sender
   */
  function approve(address _spender, uint256 _value) public override returns (bool) {
    bool success = _approveUNN(_msgSender(), _spender, _value);
    if(!success && b_revert){
      revert("UPGT_ERROR: APPROVE ERROR");
    }
    return success;
  }
  
  /**
   * @dev Token owner approval of amount for specified spender
   * @param _owner address of party that owns the tokens being granted approval for spending
   * @param _spender address of party that is granted approval for spending
   * @param _value amount approved for spending
   * @return boolean approval status
   * if _spender allownace for a given _owner is greater than 0, 
   * increaseAllowance/decreaseAllowance should be used to prevent a race condition whereby the spender is able to spend the total value of both the old and new allowance.  _spender cannot be burn or this governance token contract address.  Addresses github.com/ethereum/EIPs/issues738
   */
  function _approveUNN(address _owner, address _spender, uint256 _value) internal returns (bool) {
    bool retval = false;
    if(_spender != BURN_ADDRESS &&
      _spender != UPGT_CONTRACT_ADDRESS &&
      (m_allowances[_owner][_spender] == 0 || _value == 0)
    ){
      m_allowances[_owner][_spender] = _value;
      emit Approval(_owner, _spender, _value);
      retval = true;
    }
    return retval;
  }

  /**
   * @dev Increase spender allowance by specified incremental value
   * @param _spender address of party that is granted approval for spending
   * @param _addedValue specified incremental increase
   * @return boolean increaseAllowance status
   * public wrapper for _increaseAllowance, _owner restricted to msg.sender
   */
  function increaseAllowance(address _spender, uint256 _addedValue) public returns (bool) {
    bool success = _increaseAllowanceUNN(_msgSender(), _spender, _addedValue);
    if(!success && b_revert){
      revert("UPGT_ERROR: INCREASE ALLOWANCE ERROR");
    }
    return success;
  }

  /**
   * @dev Allow owner to increase spender allowance by specified incremental value
   * @param _owner address of the token owner
   * @param _spender address of the token spender
   * @param _addedValue specified incremental increase
   * @return boolean return value status
   * increase the number of tokens that an _owner provides as allowance to a _spender-- does not requrire the number of tokens allowed to be set first to 0.  _spender cannot be either burn or this goverance token contract address.
   */
  function _increaseAllowanceUNN(address _owner, address _spender, uint256 _addedValue) internal returns (bool) {
    bool retval = false;
    if(_spender != BURN_ADDRESS &&
      _spender != UPGT_CONTRACT_ADDRESS &&
      _addedValue > 0
    ){
      m_allowances[_owner][_spender] = m_allowances[_owner][_spender].add(_addedValue);
      retval = true;
      emit Approval(_owner, _spender, m_allowances[_owner][_spender]);
    }
    return retval;
  }

  /**
   * @dev Decrease spender allowance by specified incremental value
   * @param _spender address of party that is granted approval for spending
   * @param _subtractedValue specified incremental decrease
   * @return boolean success status
   * public wrapper for _decreaseAllowance, _owner restricted to msg.sender
   */
  //public wrapper for _decreaseAllowance, _owner restricted to msg.sender
  function decreaseAllowance(address _spender, uint256 _subtractedValue) public returns (bool) {
    bool success = _decreaseAllowanceUNN(_msgSender(), _spender, _subtractedValue);
    if(!success && b_revert){
      revert("UPGT_ERROR: DECREASE ALLOWANCE ERROR");
    }
    return success;
  } 

  /**
   * @dev Allow owner to decrease spender allowance by specified incremental value
   * @param _owner address of the token owner
   * @param _spender address of the token spender
   * @param _subtractedValue specified incremental decrease
   * @return boolean return value status
   * decrease the number of tokens than an _owner provdes as allowance to a _spender.  A _spender cannot have a negative allowance.  Does not require existing allowance to be set first to 0.  _spender cannot be burn or this governance token contract address.
   */
  function _decreaseAllowanceUNN(address _owner, address _spender, uint256 _subtractedValue) internal returns (bool) {
    bool retval = false;
    if(_spender != BURN_ADDRESS &&
       _spender != UPGT_CONTRACT_ADDRESS &&
      _subtractedValue > 0 &&
      m_allowances[_owner][_spender] >= _subtractedValue
    ){
      m_allowances[_owner][_spender] = m_allowances[_owner][_spender].sub(_subtractedValue);
      retval = true;
      emit Approval(_owner, _spender, m_allowances[_owner][_spender]);
    }
    return retval;
  }

  /**
   * LockedDestination related functions
   */

  /**
   * @dev Adds address as a designated destination for tokens when locked for allocation only
   * @param _address Address of approved desitnation for movement during lock
   * @return success in setting address as eligible for transfer independent of token lock status
   */
  function setAsEligibleLockedDestination(address _address) public returns (bool) {
    bool retVal = false;
    if(hasRole(ROLE_ADMIN, _msgSender())){
      m_lockedDestinations[_address] = true;
      retVal = true;
    }
    return retVal;
  }

  /**
   * @dev removes desitnation as eligible for transfer
   * @param _address address being removed
   */
  function removeEligibleLockedDestination(address _address) public {
    if(hasRole(ROLE_ADMIN, _msgSender())){
      require(_address != BURN_ADDRESS, "UPGT_ERROR: address cannot be burn address");
      delete(m_lockedDestinations[_address]);
    }
  }

  /**
   * @dev checks whether a destination is eligible as recipient of transfer independent of token lock status
   * @param _address address being checked
   * @return whether desitnation is locked
   */
  function checkEligibleLockedDesination(address _address) public view returns (bool) {
    return m_lockedDestinations[_address];
  }

  /**
   * @dev Adds address as a designated allocator that can move tokens when they are locked
   * @param _address Address receiving the role of ROLE_ALLOCATE
   * @return success as true or false
   */
  function setAsAllocator(address _address) public returns (bool) {
    bool retVal = false;
    if(hasRole(ROLE_ADMIN, _msgSender())){
      grantRole(ROLE_ALLOCATE, _address);
      retVal = true;
    }
    return retVal;
  }
  
  /**
   * @dev Removes address as a designated allocator that can move tokens when they are locked
   * @param _address Address being removed from the ROLE_ALLOCATE
   * @return success as true or false
   */
  function removeAsAllocator(address _address) public returns (bool) {
    bool retVal = false;
    if(hasRole(ROLE_ADMIN, _msgSender())){
      if(hasRole(ROLE_ALLOCATE, _address)){
        revokeRole(ROLE_ALLOCATE, _address);
        retVal = true;
      }
    }
    return retVal;
  }

  /**
   * @dev Checks to see if an address has the role of being an allocator
   * @param _address Address being checked for ROLE_ALLOCATE
   * @return true or false whether the address has ROLE_ALLOCATE assigned
   */
  function checkAsAllocator(address _address) public view returns (bool) {
    return hasRole(ROLE_ALLOCATE, _address);
  }

  /**
   * Transfer related functions
   */

  /**
   * @dev Public wrapper for transfer function to move tokens of specified value to a given address
   * @param _to specified recipient
   * @param _value amount being transfered to recipient
   * @return status of transfer success
   */
  function transfer(address _to, uint256 _value) external override returns (bool) {
    bool success = _transferUNN(_msgSender(), _to, _value);
    if(!success && b_revert){
      revert("UPGT_ERROR: ERROR ON TRANSFER");
    }
    return success;
  }

  /**
   * @dev Transfer token for a specified address, but cannot transfer tokens to either the burn or this governance contract address.  Also moves voting delegates as required.
   * @param _owner The address owner where transfer originates
   * @param _to The address to transfer to
   * @param _value The amount to be transferred
   * @return status of transfer success
   */
  function _transferUNN(address _owner, address _to, uint256 _value) internal returns (bool) {
    bool retval = false;
    if(b_canTransfer || hasRole(ROLE_ALLOCATE, _msgSender()) || checkEligibleLockedDesination(_to)) {
      if(
         _to != BURN_ADDRESS &&
         _to != UPGT_CONTRACT_ADDRESS &&
         (balanceOf(_owner) >= _value) &&
         (_value >= 0)
      ){
        _moveReleasedBalance(_owner);
        m_balances[_owner] = m_balances[_owner].sub(_value);
        m_balances[_to] = m_balances[_to].add(_value);
        retval = true;
        //need to move voting delegates with transfer of tokens
        retval = retval && _moveVotingDelegates(m_delegatedAccounts[_owner], m_delegatedAccounts[_to], _value);
        emit Transfer(_owner, _to, _value);
      }
    }
    return retval;
  }

  /**
   * @dev Public wrapper for transferAndLock function to move tokens of specified value to a given address and lock them for a period of time
   * @param _to specified recipient
   * @param _value amount being transfered to recipient
   * @param _releaseTime time in seconds after amount will be released
   * @param _votable flag which describes if locked tokens are votable or not
   * @return status of transfer success
   * Requires ROLE_LOCK
   */
  function transferAndLock(address _to, uint256 _value, uint256 _releaseTime, bool _votable) public virtual returns (bool) {
    bool retval = false;
    if(hasRole(ROLE_LOCK, _msgSender())){
      retval = _transferAndLock(msg.sender, _to, _value, _releaseTime, _votable);
    }
   
    if(!retval && b_revert){
      revert("UPGT_ERROR: ERROR ON TRANSFER AND LOCK");
    }
    return retval;
  }

  /**
   * @dev Transfers tokens of specified value to a given address and lock them for a period of time
   * @param _owner The address owner where transfer originates
   * @param _to specified recipient
   * @param _value amount being transfered to recipient
   * @param _releaseTime time in seconds after amount will be released
   * @param _votable flag which describes if locked tokens are votable or not
   * @return status of transfer success
   */
  function _transferAndLock(address _owner, address _to, uint256 _value, uint256 _releaseTime, bool _votable) internal virtual returns (bool){
    bool retval = false;
    if(b_canTransfer || hasRole(ROLE_ALLOCATE, _msgSender()) || checkEligibleLockedDesination(_to)) {
      if(
         _to != BURN_ADDRESS &&
         _to != UPGT_CONTRACT_ADDRESS &&
         (balanceOf(_owner) >= _value) &&
         (_value >= 0)
      ){
        _moveReleasedBalance(_owner);
        m_balances[_owner] = m_balances[_owner].sub(_value);
        m_lockedBalances[_to].push(LockedTokens(_value, _releaseTime, _votable));
        retval = true;
        //need to move voting delegates with transfer of tokens
        // retval = retval && _moveVotingDelegates(m_delegatedAccounts[_owner], m_delegatedAccounts[_to], _value);  
        emit Transfer(_owner, _to, _value);
      }
    }
    return retval;
  }

  /**
   * @dev Public wrapper for transferFrom function
   * @param _owner The address to transfer from
   * @param _spender cannot be the burn address
   * @param _value The amount to be transferred
   * @return status of transferFrom success
   * _spender cannot be either this goverance token contract or burn
   */
  function transferFrom(address _owner, address _spender, uint256 _value) external override returns (bool) {
    bool success = _transferFromUNN(_owner, _spender, _value);
    if(!success && b_revert){
      revert("UPGT_ERROR: ERROR ON TRANSFER FROM");
    }
    return success;
  }

  /**
   * @dev Transfer token for a specified address.  _spender cannot be either this goverance token contract or burn
   * @param _owner The address to transfer from
   * @param _spender cannot be the burn address
   * @param _value The amount to be transferred
   * @return status of transferFrom success
   * _spender cannot be either this goverance token contract or burn
   */
  function _transferFromUNN(address _owner, address _spender, uint256 _value) internal returns (bool) {
    bool retval = false;
    if(b_canTransfer || hasRole(ROLE_ALLOCATE, _msgSender()) || checkEligibleLockedDesination(_spender)) {
      if(
        _spender != BURN_ADDRESS &&
        _spender != UPGT_CONTRACT_ADDRESS &&
        (balanceOf(_owner) >= _value) &&
        (_value > 0) &&
        (m_allowances[_owner][_msgSender()] >= _value)
      ){
        _moveReleasedBalance(_owner);
        m_balances[_owner] = m_balances[_owner].sub(_value);
        m_balances[_spender] = m_balances[_spender].add(_value);
        m_allowances[_owner][_msgSender()] = m_allowances[_owner][_msgSender()].sub(_value);
        retval = true;
        //need to move delegates that exist for this owner in line with transfer
        retval = retval && _moveVotingDelegates(_owner, _spender, _value); 
        emit Transfer(_owner, _spender, _value);
      }
    }
    return retval;
  }

  /**
   * @dev Public wrapper for transferFromAndLock function to move tokens of specified value from given address to another address and lock them for a period of time
   * @param _owner The address owner where transfer originates
   * @param _to specified recipient
   * @param _value amount being transfered to recipient
   * @param _releaseTime time in seconds after amount will be released
   * @param _votable flag which describes if locked tokens are votable or not
   * @return status of transfer success
   * Requires ROLE_LOCK
   */
  function transferFromAndLock(address _owner, address _to, uint256 _value, uint256 _releaseTime, bool _votable) public virtual returns (bool) {
     bool retval = false;
    if(hasRole(ROLE_LOCK, _msgSender())){
      retval = _transferFromAndLock(_owner, _to, _value, _releaseTime, _votable);
    }
   
    if(!retval && b_revert){
      revert("UPGT_ERROR: ERROR ON TRANSFER FROM AND LOCK");
    }
    return retval;
  }

  /**
   * @dev Transfers tokens of specified value from a given address to another address and lock them for a period of time
   * @param _owner The address owner where transfer originates
   * @param _to specified recipient
   * @param _value amount being transfered to recipient
   * @param _releaseTime time in seconds after amount will be released
   * @param _votable flag which describes if locked tokens are votable or not
   * @return status of transfer success
   */
  function _transferFromAndLock(address _owner, address _to, uint256 _value, uint256 _releaseTime, bool _votable) internal returns (bool) {
    bool retval = false;
    if(b_canTransfer || hasRole(ROLE_ALLOCATE, _msgSender()) || checkEligibleLockedDesination(_to)) {
      if(
        _to != BURN_ADDRESS &&
        _to != UPGT_CONTRACT_ADDRESS &&
        (balanceOf(_owner) >= _value) &&
        (_value > 0) &&
        (m_allowances[_owner][_msgSender()] >= _value)
      ){
        _moveReleasedBalance(_owner);
        m_balances[_owner] = m_balances[_owner].sub(_value);
        m_lockedBalances[_to].push(LockedTokens(_value, _releaseTime, _votable));
        m_allowances[_owner][_msgSender()] = m_allowances[_owner][_msgSender()].sub(_value);
        retval = true;
        //need to move delegates that exist for this owner in line with transfer
        // retval = retval && _moveVotingDelegates(_owner, _to, _value); 
        emit Transfer(_owner, _to, _value);
      }
    }
    return retval;
  }

  /**
   * @dev Public function to burn tokens
   * @param _value number of tokens to be burned
   * @return whether tokens were burned
   * Only ROLE_MINTER may burn tokens
   */
  function burn(uint256 _value) external returns (bool) {
    bool success = _burn(_value);
    if(!success && b_revert){
      revert("UPGT_ERROR: FAILED TO BURN");
    }
    return success;
  } 

  /**
   * @dev Private function Burn tokens
   * @param _value number of tokens to be burned
   * @return bool whether the tokens were burned
   * only a minter may burn tokens, meaning that tokens being burned must be previously send to a ROLE_MINTER wallet.
   */
  function _burn(uint256 _value) internal returns (bool) {
    bool retval = false;
    if(hasRole(ROLE_MINT, _msgSender()) &&
       (m_balances[_msgSender()] >= _value)
    ){
      m_balances[_msgSender()] -= _value;
      uint256_totalSupply = uint256_totalSupply.sub(_value);
      retval = true;
      emit Burn(_msgSender(), _value);
    }
    return retval;
  }

  /** 
  * Voting related functions
  */

  /**
   * @dev Public wrapper for _calculateVotingPower function which calulates voting power
   * @dev voting power = balance + locked votable balance + delegations
   * @return uint256 voting power
   */
  function calculateVotingPower() public view returns (uint256) {
    return _calculateVotingPower(_msgSender());
  }

  /**
   * @dev Calulates voting power of specified address
   * @param _account address of token holder
   * @return uint256 voting power
   */
  function _calculateVotingPower(address _account) private view returns (uint256) {
    uint256 votingPower = m_balances[_account].add(_calculateLockedVotableBalance(_account));
    for (uint i=0; i<m_delegatedAccountsInverseMap[_account].length(); i++) {
      if(m_delegatedAccountsInverseMap[_account].at(i) != address(0)){
        address delegatedAccount = m_delegatedAccountsInverseMap[_account].at(i);
        votingPower = votingPower.add(m_balances[delegatedAccount]).add(_calculateLockedVotableBalance(delegatedAccount));
      }
    }
    return votingPower;
  }

  /**
   * @dev Moves a number of votes from a token holder to a designated representative
   * @param _source address of token holder
   * @param _destination address of voting delegate
   * @param _amount of voting delegation transfered to designated representative
   * @return bool whether move was successful
   * Requires ROLE_TEST
   */
  function moveVotingDelegates(
    address _source,
    address _destination,
    uint256 _amount) public returns (bool) {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    return _moveVotingDelegates(_source, _destination, _amount);
  }

  /**
   * @dev Moves a number of votes from a token holder to a designated representative
   * @param _source address of token holder
   * @param _destination address of voting delegate
   * @param _amount of voting delegation transfered to designated representative
   * @return bool whether move was successful
   */
  function _moveVotingDelegates(
      address _source, 
      address _destination, 
      uint256 _amount
  ) internal returns (bool) {
    if(_source != _destination && _amount > 0) {
      if(_source != BURN_ADDRESS) {
        uint256 sourceNumberOfVotingCheckpoints = m_accountVotingCheckpoints[_source];
        uint256 sourceNumberOfVotingCheckpointsOriginal = (sourceNumberOfVotingCheckpoints > 0)? m_votingCheckpoints[_source][sourceNumberOfVotingCheckpoints.sub(1)].votes : 0;
        if(sourceNumberOfVotingCheckpointsOriginal >= _amount) {
          uint256 sourceNumberOfVotingCheckpointsNew = sourceNumberOfVotingCheckpointsOriginal.sub(_amount);
          _writeVotingCheckpoint(_source, sourceNumberOfVotingCheckpoints, sourceNumberOfVotingCheckpointsOriginal, sourceNumberOfVotingCheckpointsNew);
        }
      }

      if(_destination != BURN_ADDRESS) {
        uint256 destinationNumberOfVotingCheckpoints = m_accountVotingCheckpoints[_destination];
        uint256 destinationNumberOfVotingCheckpointsOriginal = (destinationNumberOfVotingCheckpoints > 0)? m_votingCheckpoints[_source][destinationNumberOfVotingCheckpoints.sub(1)].votes : 0;
        uint256 destinationNumberOfVotingCheckpointsNew = destinationNumberOfVotingCheckpointsOriginal.add(_amount);
        _writeVotingCheckpoint(_destination, destinationNumberOfVotingCheckpoints, destinationNumberOfVotingCheckpointsOriginal, destinationNumberOfVotingCheckpointsNew);
      }
    }
    
    return true; 
  }

  /**
   * @dev Writes voting checkpoint for a given voting delegate
   * @param _votingDelegate exercising votes
   * @param _numberOfVotingCheckpoints number of voting checkpoints for current vote
   * @param _oldVotes previous number of votes
   * @param _newVotes new number of votes
   * Public function for writing voting checkpoint
   * Requires ROLE_TEST
   */
  function writeVotingCheckpoint(
    address _votingDelegate,
    uint256 _numberOfVotingCheckpoints,
    uint256 _oldVotes,
    uint256 _newVotes) public {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    _writeVotingCheckpoint(_votingDelegate, _numberOfVotingCheckpoints, _oldVotes, _newVotes);
  }

  /**
   * @dev Writes voting checkpoint for a given voting delegate
   * @param _votingDelegate exercising votes
   * @param _numberOfVotingCheckpoints number of voting checkpoints for current vote
   * @param _oldVotes previous number of votes
   * @param _newVotes new number of votes
   * Private function for writing voting checkpoint
   */
  function _writeVotingCheckpoint(
    address _votingDelegate, 
    uint256 _numberOfVotingCheckpoints, 
    uint256 _oldVotes, 
    uint256 _newVotes) internal {
    if(_numberOfVotingCheckpoints > 0 && m_votingCheckpoints[_votingDelegate][_numberOfVotingCheckpoints.sub(1)].from == block.number) {
      m_votingCheckpoints[_votingDelegate][_numberOfVotingCheckpoints-1].votes = _newVotes;
    }
    else {
      m_votingCheckpoints[_votingDelegate][_numberOfVotingCheckpoints] = VotingCheckpoint(block.number, _newVotes);
      _numberOfVotingCheckpoints = _numberOfVotingCheckpoints.add(1);
    }
    emit VoteBalanceChanged(_votingDelegate, _oldVotes, _newVotes);
  }

  /**
   * @dev Calculate account votes as of a specific block
   * @param _account address whose votes are counted
   * @param _blockNumber from which votes are being counted
   * @return number of votes counted
   */
  function getVoteCountAtBlock(
    address _account, 
    uint256 _blockNumber) public view returns (uint256) {
    uint256 voteCount = 0;
    if(_blockNumber < block.number) {
      if(m_accountVotingCheckpoints[_account] != 0) {
        if(m_votingCheckpoints[_account][m_accountVotingCheckpoints[_account].sub(1)].from <= _blockNumber) {
          voteCount = m_votingCheckpoints[_account][m_accountVotingCheckpoints[_account].sub(1)].votes;
        }
        else if(m_votingCheckpoints[_account][0].from > _blockNumber) {
          voteCount = 0;
        }
        else {
          uint256 lower = 0;
          uint256 upper = m_accountVotingCheckpoints[_account].sub(1);
          
          while(upper > lower) {
            uint256 center = upper.sub((upper.sub(lower).div(2)));
            VotingCheckpoint memory votingCheckpoint = m_votingCheckpoints[_account][center];
            if(votingCheckpoint.from == _blockNumber) {
              voteCount = votingCheckpoint.votes;
              break;
            }
            else if(votingCheckpoint.from < _blockNumber) {
              lower = center;
            }
            else {
              upper = center.sub(1);
            }
          
          }
        }
      }
    }
    return voteCount;
  }

  /**
   * @dev Vote Delegation Functions
   * @param _to address where message sender is assigning votes
   * @return success of message sender delegating vote
   * delegate function does not allow assignment to burn
   */
  function delegateVote(address _to) public returns (bool) {
    return _delegateVote(_msgSender(), _to);
  }

  /**
   * @dev Delegate votes from token holder to another address
   * @param _from Token holder 
   * @param _toDelegate Address that will be delegated to for purpose of voting
   * @return success as to whether delegation has been a success
   */
  function _delegateVote(
    address _from, 
    address _toDelegate) internal returns (bool) {
    bool retval = false;
    if(_toDelegate != BURN_ADDRESS) {
      address currentDelegate = m_delegatedAccounts[_from];
      uint256 fromAccountBalance = m_balances[_from].add(_calculateLockedVotableBalance(_from));
      address oldToDelegate = m_delegatedAccounts[_from];
      m_delegatedAccounts[_from] = _toDelegate;

      m_delegatedAccountsInverseMap[oldToDelegate].remove(_from);
      if(_from != _toDelegate){
        m_delegatedAccountsInverseMap[_toDelegate].add(_from);
      }

      retval = true;
      retval = retval && _moveVotingDelegates(currentDelegate, _toDelegate, fromAccountBalance);
      if(retval) {
        if(_from == _toDelegate){
          emit VotingDelegateRemoved(_from);
        }
        else{
          emit VotingDelegateChanged(_from, currentDelegate, _toDelegate);
        }
      }
    }
    return retval;
  }

  /**
   * @dev Revert voting delegate control to owner account
   * @param _account  The account that has delegated its vote
   * @return success of reverting delegation to owner
   */
  function _revertVotingDelegationToOwner(address _account) internal returns (bool) {
    return _delegateVote(_account, _account);
  }

  /**
   * @dev Used by an message sending account to recall its voting delegates
   * @return success of reverting delegation to owner
   */
  function recallVotingDelegate() public returns (bool) {
    return _revertVotingDelegationToOwner(_msgSender());
  }
  
  /**
   * @dev Retrieve the voting delegate for a specified account
   * @param _account  The account that has delegated its vote
   */ 
  function getVotingDelegate(address _account) public view returns (address) {
    return m_delegatedAccounts[_account];
  }

  /** 
  * EIP-712 related functions
  */

  /**
   * @dev EIP-712 Ethereum Typed Structured Data Hashing and Signing for Allocation Permit
   * @param _owner address of token owner
   * @param _spender address of designated spender
   * @param _value value permitted for spend
   * @param _deadline expiration of signature
   * @param _ecv ECDSA v parameter
   * @param _ecr ECDSA r parameter
   * @param _ecs ECDSA s parameter
   */
  function permit(
    address _owner, 
    address _spender, 
    uint256 _value, 
    uint256 _deadline, 
    uint8 _ecv, 
    bytes32 _ecr, 
    bytes32 _ecs
  ) external returns (bool) {
    require(block.timestamp <= _deadline, "UPGT_ERROR: wrong timestamp");
    require(uint256_chain_id == _getChainId(), "UPGT_ERROR: chain_id is incorrect");
    bytes32 digest = keccak256(abi.encodePacked(
        "\x19\x01",
        EIP712DOMAIN_SEPARATOR,
        keccak256(abi.encode(PERMIT_TYPEHASH, _owner, _spender, _value, m_nonces[_owner]++, _deadline))
      )
    );
    require(_owner == _recoverSigner(digest, _ecv, _ecr, _ecs), "UPGT_ERROR: sign does not match user");
    require(_owner != BURN_ADDRESS, "UPGT_ERROR: address cannot be burn address");

    return _approveUNN(_owner, _spender, _value);
  }

  /**
   * @dev EIP-712 ETH Typed Structured Data Hashing and Signing for Delegate Vote
   * @param _owner address of token owner
   * @param _delegate address of voting delegate
   * @param _expiretimestamp expiration of delegation signature
   * @param _ecv ECDSA v parameter
   * @param _ecr ECDSA r parameter
   * @param _ecs ECDSA s parameter
   * @ @return bool true or false depedening on whether vote was successfully delegated
   */
  function delegateVoteBySignature(
    address _owner, 
    address _delegate, 
    uint256 _expiretimestamp, 
    uint8 _ecv, 
    bytes32 _ecr, 
    bytes32 _ecs
  ) external returns (bool) {
    require(block.timestamp <= _expiretimestamp, "UPGT_ERROR: wrong timestamp");
    require(uint256_chain_id == _getChainId(), "UPGT_ERROR: chain_id is incorrect");
    bytes32 digest = keccak256(abi.encodePacked(
        "\x19\x01",
        EIP712DOMAIN_SEPARATOR,
        _hash(VotingDelegate(
          {
            owner : _owner,
            delegate : _delegate,
            nonce : m_nonces[_owner]++,
            expirationTime : _expiretimestamp
          })
        )
      )
    );
    require(_owner == _recoverSigner(digest, _ecv, _ecr, _ecs), "UPGT_ERROR: sign does not match user");
    require(_owner!= BURN_ADDRESS, "UPGT_ERROR: address cannot be burn address");

    return _delegateVote(_owner, _delegate);
  }

  /**
   * @dev Public hash EIP712Domain struct for EIP-712
   * @param _eip712Domain EIP712Domain struct
   * @return bytes32 hash of _eip712Domain
   * Requires ROLE_TEST
   */
  function hashEIP712Domain(EIP712Domain memory _eip712Domain) public view returns (bytes32) {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    return _hash(_eip712Domain);
  }

  /**
   * @dev Hash Delegate struct for EIP-712
   * @param _delegate VotingDelegate struct
   * @return bytes32 hash of _delegate
   * Requires ROLE_TEST
   */
  function hashDelegate(VotingDelegate memory _delegate) public view returns (bytes32) {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    return _hash(_delegate);
  }

  /**
   * @dev Public hash Permit struct for EIP-712
   * @param _permit Permit struct
   * @return bytes32 hash of _permit
   * Requires ROLE_TEST
   */
  function hashPermit(Permit memory _permit) public view returns (bytes32) {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    return _hash(_permit);
  }

  /**
   * @param _digest signed, hashed message
   * @param _ecv ECDSA v parameter
   * @param _ecr ECDSA r parameter
   * @param _ecs ECDSA s parameter
   * @return address of the validated signer
   * based on openzeppelin/contracts/cryptography/ECDSA.sol recover() function
   * Requires ROLE_TEST
   */
  function recoverSigner(bytes32 _digest, uint8 _ecv, bytes32 _ecr, bytes32 _ecs) public view returns (address) {
    require(hasRole(ROLE_TEST, _msgSender()), "UPGT_ERROR: ROLE_TEST Required");
    return _recoverSigner(_digest, _ecv, _ecr, _ecs);
  }

  /**
  * @dev Private hash EIP712Domain struct for EIP-712
  * @param _eip712Domain EIP712Domain struct
  * @return bytes32 hash of _eip712Domain
  */
  function _hash(EIP712Domain memory _eip712Domain) internal pure returns (bytes32) {
      return keccak256(
          abi.encode(
              EIP712DOMAIN_TYPEHASH,
              keccak256(bytes(_eip712Domain.name)),
              keccak256(bytes(_eip712Domain.version)),
              _eip712Domain.chainId,
              _eip712Domain.verifyingContract,
              _eip712Domain.salt
          )
      );
  }

  /**
  * @dev Private hash Delegate struct for EIP-712
  * @param _delegate VotingDelegate struct
  * @return bytes32 hash of _delegate
  */
  function _hash(VotingDelegate memory _delegate) internal pure returns (bytes32) {
      return keccak256(
          abi.encode(
              DELEGATE_TYPEHASH,
              _delegate.owner,
              _delegate.delegate,
              _delegate.nonce,
              _delegate.expirationTime
          )
      );
  }

  /** 
  * @dev Private hash Permit struct for EIP-712
  * @param _permit Permit struct
  * @return bytes32 hash of _permit
  */
  function _hash(Permit memory _permit) internal pure returns (bytes32) {
      return keccak256(abi.encode(
      PERMIT_TYPEHASH,
      _permit.owner,
      _permit.spender,
      _permit.value,
      _permit.nonce,
      _permit.deadline
      ));
  }

  /**
  * @dev Recover signer information from provided digest
  * @param _digest signed, hashed message
  * @param _ecv ECDSA v parameter
  * @param _ecr ECDSA r parameter
  * @param _ecs ECDSA s parameter
  * @return address of the validated signer
  * based on openzeppelin/contracts/cryptography/ECDSA.sol recover() function
  */
  function _recoverSigner(bytes32 _digest, uint8 _ecv, bytes32 _ecr, bytes32 _ecs) internal pure returns (address) {
      // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
      // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
      // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
      // signatures from current libraries generate a unique signature with an s-value in the lower half order.
      //
      // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
      // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
      // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
      // these malleable signatures as well.
      if(uint256(_ecs) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
          revert("ECDSA: invalid signature 's' value");
      }

      if(_ecv != 27 && _ecv != 28) {
          revert("ECDSA: invalid signature 'v' value");
      }

      // If the signature is valid (and not malleable), return the signer address
      address signer = ecrecover(_digest, _ecv, _ecr, _ecs);
      require(signer != BURN_ADDRESS, "ECDSA: invalid signature");

      return signer;
  }
}

File 20 of 53 : MProtection.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/utils/EnumerableSet.sol";
import "./Interfaces/CopMappingInterface.sol";
import "./Interfaces/Versionable.sol";
import "./Moartroller.sol";
import "./Utils/ExponentialNoError.sol";
import "./Utils/ErrorReporter.sol";
import "./Utils/AssetHelpers.sol";
import "./MToken.sol";
import "./Interfaces/EIP20Interface.sol";
import "./Utils/SafeEIP20.sol";

/**
 * @title MOAR's MProtection Contract
 * @notice Collateral optimization ERC-721 wrapper
 * @author MOAR
 */
contract MProtection is ERC721Upgradeable, OwnableUpgradeable, ExponentialNoError, AssetHelpers, Versionable {
    using Counters for Counters.Counter;
    using EnumerableSet for EnumerableSet.UintSet;

    /**
     * @notice Event emitted when new MProtection token is minted
     */
    event Mint(address minter, uint tokenId, uint underlyingTokenId, address asset, uint amount, uint strikePrice, uint expirationTime);

    /**
     * @notice Event emitted when MProtection token is redeemed
     */
    event Redeem(address redeemer, uint tokenId, uint underlyingTokenId);

    /**
     * @notice Event emitted when MProtection token changes its locked value
     */
    event LockValue(uint tokenId, uint underlyingTokenId, uint optimizationValue);

    /**
     * @notice Event emitted when maturity window parameter is changed
     */
    event MaturityWindowUpdated(uint newMaturityWindow);

    Counters.Counter private _tokenIds;
    address private _copMappingAddress;
    address private _moartrollerAddress;
    mapping (uint256 => uint256) private _underlyingProtectionTokensMapping;
    mapping (uint256 => uint256) private _underlyingProtectionLockedValue;
    mapping (address => mapping (address => EnumerableSet.UintSet)) private _protectionCurrencyMapping;
    uint256 public _maturityWindow;

    struct ProtectionMappedData{
        address pool;
        address underlyingAsset;
        uint256 amount;
        uint256 strike;
        uint256 premium;
        uint256 lockedValue;
        uint256 totalValue;
        uint issueTime;
        uint expirationTime;
        bool isProtectionAlive;
    }

    /**
     * @notice Constructor for MProtection contract
     * @param copMappingAddress The address of data mapper for C-OP
     * @param moartrollerAddress The address of the Moartroller
     */
    function initialize(address copMappingAddress, address moartrollerAddress) public initializer {
        __Ownable_init();
        __ERC721_init("c-uUNN OC-Protection", "c-uUNN");
        _copMappingAddress = copMappingAddress;
        _moartrollerAddress = moartrollerAddress;
        _setMaturityWindow(10800); // 3 hours default
    }

    /**
     * @notice Returns C-OP mapping contract 
     */
    function copMapping() private view returns (CopMappingInterface){
        return CopMappingInterface(_copMappingAddress);
    }

    /**
     * @notice Mint new MProtection token
     * @param underlyingTokenId Id of C-OP token that will be deposited
     * @return ID of minted MProtection token
     */
    function mint(uint256 underlyingTokenId) public returns (uint256)
    {
        return mintFor(underlyingTokenId, msg.sender);
    }

    /**
     * @notice Mint new MProtection token for specified address
     * @param underlyingTokenId Id of C-OP token that will be deposited
     * @param receiver Address that will receive minted Mprotection token
     * @return ID of minted MProtection token
     */
    function mintFor(uint256 underlyingTokenId, address receiver) public returns (uint256)
    {
        CopMappingInterface copMappingInstance = copMapping();
        ERC721Upgradeable(copMappingInstance.getTokenAddress()).transferFrom(msg.sender, address(this), underlyingTokenId);
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _mint(receiver, newItemId);
        addUProtectionIndexes(receiver, newItemId, underlyingTokenId);
        emit Mint(
            receiver,
            newItemId,
            underlyingTokenId,
            copMappingInstance.getUnderlyingAsset(underlyingTokenId),
            copMappingInstance.getUnderlyingAmount(underlyingTokenId),
            copMappingInstance.getUnderlyingStrikePrice(underlyingTokenId),
            copMappingInstance.getUnderlyingDeadline(underlyingTokenId)
        );
        
        return newItemId;
    }

    /**
     * @notice Redeem C-OP token
     * @param tokenId Id of MProtection token that will be withdrawn
     * @return ID of redeemed C-OP token
     */
    function redeem(uint256 tokenId) external returns (uint256) {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "cuUNN: caller is not owner nor approved");
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        ERC721Upgradeable(copMapping().getTokenAddress()).transferFrom(address(this), msg.sender, underlyingTokenId);
        removeProtectionIndexes(tokenId);
        _burn(tokenId);
        emit Redeem(msg.sender, tokenId, underlyingTokenId);

        return underlyingTokenId;
    }

    /**
     * @notice Returns set of C-OP data
     * @param tokenId Id of MProtection token 
     * @return ProtectionMappedData struct filled with C-OP data
     */
    function getMappedProtectionData(uint256 tokenId) public view returns (ProtectionMappedData memory){
        ProtectionMappedData memory data;
        (address pool, uint256 amount, uint256 strike, uint256 premium, uint issueTime , uint expirationTime) = getProtectionData(tokenId);
        data = ProtectionMappedData(pool, getUnderlyingAsset(tokenId), amount, strike, premium, getUnderlyingProtectionLockedValue(tokenId), getUnderlyingProtectionTotalValue(tokenId), issueTime, expirationTime, isProtectionAlive(tokenId));
        return data;
    }

    /**
     * @notice Returns underlying token ID
     * @param tokenId Id of MProtection token 
     */
    function getUnderlyingProtectionTokenId(uint256 tokenId) public view returns (uint256){
        return _underlyingProtectionTokensMapping[tokenId];
    }

    /**
     * @notice Returns size of C-OPs filtered by asset address
     * @param owner Address of wallet holding C-OPs
     * @param currency Address of asset used to filter C-OPs
     */
    function getUserUnderlyingProtectionTokenIdByCurrencySize(address owner, address currency) public view returns (uint256){
        return _protectionCurrencyMapping[owner][currency].length();
    }

    /**
     * @notice Returns list of C-OP IDs filtered by asset address
     * @param owner Address of wallet holding C-OPs
     * @param currency Address of asset used to filter C-OPs
     */
    function getUserUnderlyingProtectionTokenIdByCurrency(address owner, address currency, uint256 index) public view returns (uint256){
        return _protectionCurrencyMapping[owner][currency].at(index);
    }

    /**
     * @notice Checks if address is owner of MProtection
     * @param owner Address of potential owner to check
     * @param tokenId ID of MProtection to check
     */
    function isUserProtection(address owner, uint256 tokenId) public view returns(bool) {
        if(Moartroller(_moartrollerAddress).isPrivilegedAddress(msg.sender)){
            return true;
        }
        return owner == ownerOf(tokenId);
    }

    /**
     * @notice Checks if MProtection is stil alive
     * @param tokenId ID of MProtection to check
     */
    function isProtectionAlive(uint256 tokenId) public view returns(bool) {
       uint256 deadline = getUnderlyingDeadline(tokenId);
       return (deadline - _maturityWindow) > now;
    }

    /**
     * @notice Creates appropriate indexes for C-OP
     * @param owner C-OP owner address
     * @param tokenId ID of MProtection 
     * @param underlyingTokenId ID of C-OP 
     */
    function addUProtectionIndexes(address owner, uint256 tokenId, uint256 underlyingTokenId) private{
        address currency = copMapping().getUnderlyingAsset(underlyingTokenId);
        _underlyingProtectionTokensMapping[tokenId] = underlyingTokenId;
        _protectionCurrencyMapping[owner][currency].add(tokenId);
    }

    /**
     * @notice Remove indexes for C-OP
     * @param tokenId ID of MProtection 
     */
    function removeProtectionIndexes(uint256 tokenId) private{
        address owner = ownerOf(tokenId);
        address currency = getUnderlyingAsset(tokenId);
        _underlyingProtectionTokensMapping[tokenId] = 0;
        _protectionCurrencyMapping[owner][currency].remove(tokenId);
    }

    /**
     * @notice Returns C-OP total value
     * @param tokenId ID of MProtection 
     */
    function getUnderlyingProtectionTotalValue(uint256 tokenId) public view returns(uint256){
        address underlyingAsset = getUnderlyingAsset(tokenId);
        uint256 assetDecimalsMantissa = getAssetDecimalsMantissa(underlyingAsset);

        return div_(
            mul_(
                getUnderlyingStrikePrice(tokenId),
                getUnderlyingAmount(tokenId)
            ),
            assetDecimalsMantissa
        );
    }

    /**
     * @notice Returns C-OP locked value
     * @param tokenId ID of MProtection 
     */
    function getUnderlyingProtectionLockedValue(uint256 tokenId) public view returns(uint256){
        return _underlyingProtectionLockedValue[tokenId];
    }

    /**
     * @notice get the amount of underlying asset that is locked
     * @param tokenId CProtection tokenId
     * @return amount locked
     */
    function getUnderlyingProtectionLockedAmount(uint256 tokenId) public view returns(uint256){
        address underlyingAsset = getUnderlyingAsset(tokenId);
        uint256 assetDecimalsMantissa = getAssetDecimalsMantissa(underlyingAsset);

        // calculates total protection value
        uint256 protectionValue = div_(
            mul_(
                getUnderlyingAmount(tokenId),
                getUnderlyingStrikePrice(tokenId)
            ),
            assetDecimalsMantissa
        );

        // return value is lockedValue / totalValue * amount
        return div_(
            mul_(
                getUnderlyingAmount(tokenId),
                div_(
                    mul_(
                        _underlyingProtectionLockedValue[tokenId],
                        1e18
                    ),
                    protectionValue
                )
            ),
            1e18
        );
    }

    /**
     * @notice Locks the given protection value as collateral optimization
     * @param tokenId The MProtection token id
     * @param value The value in stablecoin of protection to be locked as collateral optimization. 0 = max available optimization
     * @return locked protection value
     * TODO: convert semantic errors to standarized error codes
     */
    function lockProtectionValue(uint256 tokenId, uint value) external returns(uint) {
        //check if the protection belongs to the caller
        require(isUserProtection(msg.sender, tokenId), "ERROR: CALLER IS NOT THE OWNER OF PROTECTION");

        address currency = getUnderlyingAsset(tokenId);

        Moartroller moartroller = Moartroller(_moartrollerAddress);
        MToken mToken = moartroller.tokenAddressToMToken(currency);
        require(moartroller.oracle().getUnderlyingPrice(mToken) <= getUnderlyingStrikePrice(tokenId), "ERROR: C-OP STRIKE PRICE IS LOWER THAN ASSET SPOT PRICE");

        uint protectionTotalValue = getUnderlyingProtectionTotalValue(tokenId);
        uint maxOptimizableValue = moartroller.getMaxOptimizableValue(mToken, ownerOf(tokenId));

        // add protection locked value if any
        uint protectionLockedValue = getUnderlyingProtectionLockedValue(tokenId);
        if ( protectionLockedValue > 0) {
            maxOptimizableValue = add_(maxOptimizableValue, protectionLockedValue);
        }

        uint valueToLock;

        if (value != 0) {
            // check if lock value is at most max optimizable value
            require(value <= maxOptimizableValue, "ERROR: VALUE TO BE LOCKED EXCEEDS ALLOWED OPTIMIZATION VALUE");
            // check if lock value is at most protection total value
            require( value <= protectionTotalValue, "ERROR: VALUE TO BE LOCKED EXCEEDS PROTECTION TOTAL VALUE");
            valueToLock = value;
        } else {
            // if we want to lock maximum protection value let's lock the value that is at most max optimizable value
            if (protectionTotalValue > maxOptimizableValue) {
                valueToLock = maxOptimizableValue;
            } else {
                valueToLock = protectionTotalValue;
            }
        }

        _underlyingProtectionLockedValue[tokenId] = valueToLock;
        emit LockValue(tokenId, getUnderlyingProtectionTokenId(tokenId), valueToLock);
        return valueToLock;
    }

    function _setCopMapping(address newMapping) public onlyOwner {
        _copMappingAddress = newMapping;
    }

    function _setMoartroller(address newMoartroller) public onlyOwner {
        _moartrollerAddress = newMoartroller;
    }

    function _setMaturityWindow(uint256 maturityWindow) public onlyOwner {
        emit MaturityWindowUpdated(maturityWindow);
        _maturityWindow = maturityWindow;
    }


    // MAPPINGS 
    function getProtectionData(uint256 tokenId) public view returns (address, uint256, uint256, uint256, uint, uint){
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        return copMapping().getProtectionData(underlyingTokenId);
    }

    function getUnderlyingAsset(uint256 tokenId) public view returns (address){
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        return copMapping().getUnderlyingAsset(underlyingTokenId);
    }

    function getUnderlyingAmount(uint256 tokenId) public view returns (uint256){
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        return copMapping().getUnderlyingAmount(underlyingTokenId);
    }

    function getUnderlyingStrikePrice(uint256 tokenId) public view returns (uint){
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        return copMapping().getUnderlyingStrikePrice(underlyingTokenId);
    }

    function getUnderlyingDeadline(uint256 tokenId) public view returns (uint){
        uint256 underlyingTokenId = getUnderlyingProtectionTokenId(tokenId);
        return copMapping().getUnderlyingDeadline(underlyingTokenId);
    }

    function getContractVersion() external override pure returns(string memory){
        return "V1";
    }

}

File 21 of 53 : LiquidityMathModelInterface.sol
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity ^0.6.12;

import "../MToken.sol";
import "../MProtection.sol";
import "../Interfaces/PriceOracle.sol";

interface LiquidityMathModelInterface {
    struct LiquidityMathArgumentsSet {
        MToken asset;
        address account;
        uint collateralFactorMantissa;
        MProtection cprotection;
        PriceOracle oracle;
    }
    
    function getMaxOptimizableValue(LiquidityMathArgumentsSet memory _arguments) external view returns (uint);
    function getHypotheticalOptimizableValue(LiquidityMathArgumentsSet memory _arguments) external view returns(uint);
    function getTotalProtectionLockedValue(LiquidityMathArgumentsSet memory _arguments) external view returns(uint, uint);
}

File 22 of 53 : LiquidityMathModelV1.sol
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity ^0.6.12;

import "./Interfaces/LiquidityMathModelInterface.sol";
import "./MToken.sol";
import "./Utils/ErrorReporter.sol";
import "./Utils/ExponentialNoError.sol";
import "./Utils/AssetHelpers.sol";
import "./Moartroller.sol";
import "./SimplePriceOracle.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract LiquidityMathModelV1 is LiquidityMathModelInterface, LiquidityMathModelErrorReporter, ExponentialNoError, Ownable, AssetHelpers {


    /**
     * @notice get the maximum asset value that can be still optimized.
     * @notice if protectionId is supplied, the maxOptimizableValue is increased by the protection lock value'
     * which is helpful to recalculate how much of this protection can be optimized again
     */
    function getMaxOptimizableValue(LiquidityMathModelInterface.LiquidityMathArgumentsSet memory arguments) external override view returns (uint){
        uint returnValue;
        uint hypotheticalOptimizableValue = getHypotheticalOptimizableValue(arguments);
        uint totalProtectionLockedValue;
        (totalProtectionLockedValue, ) = getTotalProtectionLockedValue(arguments);
        if(hypotheticalOptimizableValue <= totalProtectionLockedValue){
            returnValue = 0;
        }
        else{
            returnValue = sub_(hypotheticalOptimizableValue, totalProtectionLockedValue);
        }

        return returnValue;
    }

    /**
     * @notice get the maximum value of an asset that can be optimized by protection for the given user
     * @dev optimizable = asset value * MPC
     * @return the hypothetical optimizable value
     * TODO: replace hardcoded 1e18 values
     */
    function getHypotheticalOptimizableValue(LiquidityMathModelInterface.LiquidityMathArgumentsSet memory arguments) public override view returns(uint) {
        uint assetValue = div_(
            mul_(
                div_(
                    mul_(
                    arguments.asset.balanceOf(arguments.account),
                    arguments.asset.exchangeRateStored()
                    ),
                    1e18
                ),
                arguments.oracle.getUnderlyingPrice(arguments.asset)
            ),
            getAssetDecimalsMantissa(arguments.asset.getUnderlying())
        );

        uint256 hypotheticalOptimizableValue = div_(
            mul_(
                assetValue,
                arguments.asset.maxProtectionComposition()
            ),
            arguments.asset.maxProtectionCompositionMantissa()
        );
        return hypotheticalOptimizableValue;
    }

    /**
     * @dev gets all locked protections values with mark to market value. Used by Moartroller.
     */
    function getTotalProtectionLockedValue(LiquidityMathModelInterface.LiquidityMathArgumentsSet memory arguments) public override view returns(uint, uint) {
        uint _lockedValue = 0;
        uint _markToMarket = 0;

        uint _protectionCount = arguments.cprotection.getUserUnderlyingProtectionTokenIdByCurrencySize(arguments.account, arguments.asset.underlying());
        for (uint j = 0; j < _protectionCount; j++) {
            uint protectionId = arguments.cprotection.getUserUnderlyingProtectionTokenIdByCurrency(arguments.account, arguments.asset.underlying(), j);
            bool protectionIsAlive = arguments.cprotection.isProtectionAlive(protectionId);

            if(protectionIsAlive){
                _lockedValue = add_(_lockedValue, arguments.cprotection.getUnderlyingProtectionLockedValue(protectionId));

                uint assetSpotPrice = arguments.oracle.getUnderlyingPrice(arguments.asset);
                uint protectionStrikePrice = arguments.cprotection.getUnderlyingStrikePrice(protectionId);

                if( assetSpotPrice > protectionStrikePrice) {
                    _markToMarket = _markToMarket + div_(
                        mul_(
                            div_(
                                mul_(
                                    assetSpotPrice - protectionStrikePrice,
                                    arguments.cprotection.getUnderlyingProtectionLockedAmount(protectionId)
                                ),
                                getAssetDecimalsMantissa(arguments.asset.underlying())
                            ),
                            arguments.collateralFactorMantissa
                        ),
                    1e18
                    );
                }
            }
            
        }
        return (_lockedValue , _markToMarket);
    }
}

File 23 of 53 : LiquidationModelInterface.sol
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;

import "./PriceOracle.sol";
import "./MoartrollerInterface.sol";
pragma solidity ^0.6.12;


interface LiquidationModelInterface {
    function liquidateCalculateSeizeUserTokens(LiquidateCalculateSeizeUserTokensArgumentsSet memory arguments) external view returns (uint, uint);
    function liquidateCalculateSeizeTokens(LiquidateCalculateSeizeUserTokensArgumentsSet memory arguments) external view returns (uint, uint);

    struct LiquidateCalculateSeizeUserTokensArgumentsSet {
        PriceOracle oracle;
        MoartrollerInterface moartroller;
        address mTokenBorrowed;
        address mTokenCollateral;
        uint actualRepayAmount;
        address accountForLiquidation;
        uint liquidationIncentiveMantissa;

    }
}

File 24 of 53 : Initializable.sol
// SPDX-License-Identifier: MIT

// solhint-disable-next-line compiler-version
pragma solidity >=0.4.24 <0.8.0;

import "../utils/AddressUpgradeable.sol";

/**
 * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
 * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
 * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
 * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
 *
 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
 * possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
 *
 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
 */
abstract contract Initializable {

    /**
     * @dev Indicates that the contract has been initialized.
     */
    bool private _initialized;

    /**
     * @dev Indicates that the contract is in the process of being initialized.
     */
    bool private _initializing;

    /**
     * @dev Modifier to protect an initializer function from being invoked twice.
     */
    modifier initializer() {
        require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized");

        bool isTopLevelCall = !_initializing;
        if (isTopLevelCall) {
            _initializing = true;
            _initialized = true;
        }

        _;

        if (isTopLevelCall) {
            _initializing = false;
        }
    }

    /// @dev Returns true if and only if the function is running in the constructor
    function _isConstructor() private view returns (bool) {
        return !AddressUpgradeable.isContract(address(this));
    }
}

File 25 of 53 : InterestRateModelInterface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

/**
  * @title MOAR's InterestRateModel Interface
  * @author MOAR
  */
interface InterestRateModelInterface {
    /**
      * @notice Calculates the current borrow interest rate per block
      * @param cash The total amount of cash the market has
      * @param borrows The total amount of borrows the market has outstanding
      * @param reserves The total amount of reserves the market has
      * @return The borrow rate per block (as a percentage, and scaled by 1e18)
      */
    function getBorrowRate(uint cash, uint borrows, uint reserves) external view returns (uint);

    /**
      * @notice Calculates the current supply interest rate per block
      * @param cash The total amount of cash the market has
      * @param borrows The total amount of borrows the market has outstanding
      * @param reserves The total amount of reserves the market has
      * @param reserveFactorMantissa The current reserve factor the market has
      * @return The supply rate per block (as a percentage, and scaled by 1e18)
      */
    function getSupplyRate(uint cash, uint borrows, uint reserves, uint reserveFactorMantissa) external view returns (uint);

}

File 26 of 53 : ERC721Upgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../../utils/ContextUpgradeable.sol";
import "./IERC721Upgradeable.sol";
import "./IERC721MetadataUpgradeable.sol";
import "./IERC721EnumerableUpgradeable.sol";
import "./IERC721ReceiverUpgradeable.sol";
import "../../introspection/ERC165Upgradeable.sol";
import "../../math/SafeMathUpgradeable.sol";
import "../../utils/AddressUpgradeable.sol";
import "../../utils/EnumerableSetUpgradeable.sol";
import "../../utils/EnumerableMapUpgradeable.sol";
import "../../utils/StringsUpgradeable.sol";
import "../../proxy/Initializable.sol";

/**
 * @title ERC721 Non-Fungible Token Standard basic implementation
 * @dev see https://eips.ethereum.org/EIPS/eip-721
 */
contract ERC721Upgradeable is Initializable, ContextUpgradeable, ERC165Upgradeable, IERC721Upgradeable, IERC721MetadataUpgradeable, IERC721EnumerableUpgradeable {
    using SafeMathUpgradeable for uint256;
    using AddressUpgradeable for address;
    using EnumerableSetUpgradeable for EnumerableSetUpgradeable.UintSet;
    using EnumerableMapUpgradeable for EnumerableMapUpgradeable.UintToAddressMap;
    using StringsUpgradeable for uint256;

    // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
    // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
    bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;

    // Mapping from holder address to their (enumerable) set of owned tokens
    mapping (address => EnumerableSetUpgradeable.UintSet) private _holderTokens;

    // Enumerable mapping from token ids to their owners
    EnumerableMapUpgradeable.UintToAddressMap private _tokenOwners;

    // Mapping from token ID to approved address
    mapping (uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping (address => mapping (address => bool)) private _operatorApprovals;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Optional mapping for token URIs
    mapping (uint256 => string) private _tokenURIs;

    // Base URI
    string private _baseURI;

    /*
     *     bytes4(keccak256('balanceOf(address)')) == 0x70a08231
     *     bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
     *     bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
     *     bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
     *     bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
     *     bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
     *     bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
     *     bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
     *     bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde
     *
     *     => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
     *        0xa22cb465 ^ 0xe985e9c5 ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd
     */
    bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;

    /*
     *     bytes4(keccak256('name()')) == 0x06fdde03
     *     bytes4(keccak256('symbol()')) == 0x95d89b41
     *     bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd
     *
     *     => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f
     */
    bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;

    /*
     *     bytes4(keccak256('totalSupply()')) == 0x18160ddd
     *     bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59
     *     bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7
     *
     *     => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63
     */
    bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63;

    /**
     * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
     */
    function __ERC721_init(string memory name_, string memory symbol_) internal initializer {
        __Context_init_unchained();
        __ERC165_init_unchained();
        __ERC721_init_unchained(name_, symbol_);
    }

    function __ERC721_init_unchained(string memory name_, string memory symbol_) internal initializer {
        _name = name_;
        _symbol = symbol_;

        // register the supported interfaces to conform to ERC721 via ERC165
        _registerInterface(_INTERFACE_ID_ERC721);
        _registerInterface(_INTERFACE_ID_ERC721_METADATA);
        _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE);
    }

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");
        return _holderTokens[owner].length();
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token");
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

        string memory _tokenURI = _tokenURIs[tokenId];
        string memory base = baseURI();

        // If there is no base URI, return the token URI.
        if (bytes(base).length == 0) {
            return _tokenURI;
        }
        // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
        if (bytes(_tokenURI).length > 0) {
            return string(abi.encodePacked(base, _tokenURI));
        }
        // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
        return string(abi.encodePacked(base, tokenId.toString()));
    }

    /**
    * @dev Returns the base URI set via {_setBaseURI}. This will be
    * automatically added as a prefix in {tokenURI} to each token's URI, or
    * to the token ID if no specific URI is set for that token ID.
    */
    function baseURI() public view virtual returns (string memory) {
        return _baseURI;
    }

    /**
     * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        return _holderTokens[owner].at(index);
    }

    /**
     * @dev See {IERC721Enumerable-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        // _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds
        return _tokenOwners.length();
    }

    /**
     * @dev See {IERC721Enumerable-tokenByIndex}.
     */
    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        (uint256 tokenId, ) = _tokenOwners.at(index);
        return tokenId;
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721Upgradeable.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(_msgSender() == owner || ERC721Upgradeable.isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        require(operator != _msgSender(), "ERC721: approve to caller");

        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(address from, address to, uint256 tokenId) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
        _safeTransfer(from, to, tokenId, _data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `_data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _tokenOwners.contains(tokenId);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721Upgradeable.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || ERC721Upgradeable.isApprovedForAll(owner, spender));
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     d*
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
        _mint(to, tokenId);
        require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _holderTokens[to].add(tokenId);

        _tokenOwners.set(tokenId, to);

        emit Transfer(address(0), to, tokenId);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721Upgradeable.ownerOf(tokenId); // internal owner

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        // Clear metadata (if any)
        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }

        _holderTokens[owner].remove(tokenId);

        _tokenOwners.remove(tokenId);

        emit Transfer(owner, address(0), tokenId);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(address from, address to, uint256 tokenId) internal virtual {
        require(ERC721Upgradeable.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); // internal owner
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _holderTokens[from].remove(tokenId);
        _holderTokens[to].add(tokenId);

        _tokenOwners.set(tokenId, to);

        emit Transfer(from, to, tokenId);
    }

    /**
     * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
        require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
        _tokenURIs[tokenId] = _tokenURI;
    }

    /**
     * @dev Internal function to set the base URI for all token IDs. It is
     * automatically added as a prefix to the value returned in {tokenURI},
     * or to the token ID if {tokenURI} is empty.
     */
    function _setBaseURI(string memory baseURI_) internal virtual {
        _baseURI = baseURI_;
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param _data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
        private returns (bool)
    {
        if (!to.isContract()) {
            return true;
        }
        bytes memory returndata = to.functionCall(abi.encodeWithSelector(
            IERC721ReceiverUpgradeable(to).onERC721Received.selector,
            _msgSender(),
            from,
            tokenId,
            _data
        ), "ERC721: transfer to non ERC721Receiver implementer");
        bytes4 retval = abi.decode(returndata, (bytes4));
        return (retval == _ERC721_RECEIVED);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits an {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721Upgradeable.ownerOf(tokenId), to, tokenId); // internal owner
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { }
    uint256[41] private __gap;
}

File 27 of 53 : Counters.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../math/SafeMath.sol";

/**
 * @title Counters
 * @author Matt Condon (@shrugs)
 * @dev Provides counters that can only be incremented or decremented by one. This can be used e.g. to track the number
 * of elements in a mapping, issuing ERC721 ids, or counting request ids.
 *
 * Include with `using Counters for Counters.Counter;`
 * Since it is not possible to overflow a 256 bit integer with increments of one, `increment` can skip the {SafeMath}
 * overflow check, thereby saving gas. This does assume however correct usage, in that the underlying `_value` is never
 * directly accessed.
 */
library Counters {
    using SafeMath for uint256;

    struct Counter {
        // This variable should never be directly accessed by users of the library: interactions must be restricted to
        // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
        // this feature: see https://github.com/ethereum/solidity/issues/4637
        uint256 _value; // default: 0
    }

    function current(Counter storage counter) internal view returns (uint256) {
        return counter._value;
    }

    function increment(Counter storage counter) internal {
        // The {SafeMath} overflow check can be skipped here, see the comment at the top
        counter._value += 1;
    }

    function decrement(Counter storage counter) internal {
        counter._value = counter._value.sub(1);
    }
}

File 28 of 53 : OwnableUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../utils/ContextUpgradeable.sol";
import "../proxy/Initializable.sol";
/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    function __Ownable_init() internal initializer {
        __Context_init_unchained();
        __Ownable_init_unchained();
    }

    function __Ownable_init_unchained() internal initializer {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
    uint256[49] private __gap;
}

File 29 of 53 : EnumerableSet.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;

        // Position of the value in the `values` array, plus 1 because index 0
        // means a value is not in the set.
        mapping (bytes32 => uint256) _indexes;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._indexes[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We read and store the value's index to prevent multiple reads from the same storage slot
        uint256 valueIndex = set._indexes[value];

        if (valueIndex != 0) { // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

            // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
            // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.

            bytes32 lastvalue = set._values[lastIndex];

            // Move the last value to the index where the value to delete is
            set._values[toDeleteIndex] = lastvalue;
            // Update the index for the moved value
            set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the index for the deleted slot
            delete set._indexes[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._indexes[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        require(set._values.length > index, "EnumerableSet: index out of bounds");
        return set._values[index];
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }


    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }
}

File 30 of 53 : CopMappingInterface.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

interface CopMappingInterface {
    function getTokenAddress() external view returns (address);
    function getProtectionData(uint256 underlyingTokenId) external view returns (address, uint256, uint256, uint256, uint, uint);
    function getUnderlyingAsset(uint256 underlyingTokenId) external view returns (address);
    function getUnderlyingAmount(uint256 underlyingTokenId) external view returns (uint256);
    function getUnderlyingStrikePrice(uint256 underlyingTokenId) external view returns (uint);
    function getUnderlyingDeadline(uint256 underlyingTokenId) external view returns (uint);

}

File 31 of 53 : AssetHelpers.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

import "../Interfaces/EIP20Interface.sol";

contract AssetHelpers {
    /**
     * @dev return asset decimals mantissa. Returns 1e18 if ETH
     */
    function getAssetDecimalsMantissa(address assetAddress) public view returns (uint256){
        uint assetDecimals = 1e18;
        if (assetAddress != address(0)) {
            EIP20Interface token = EIP20Interface(assetAddress);
            assetDecimals = 10 ** uint256(token.decimals());
        }
        return assetDecimals;
    }
}

File 32 of 53 : ContextUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
import "../proxy/Initializable.sol";

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with GSN meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract ContextUpgradeable is Initializable {
    function __Context_init() internal initializer {
        __Context_init_unchained();
    }

    function __Context_init_unchained() internal initializer {
    }
    function _msgSender() internal view virtual returns (address payable) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes memory) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
    uint256[50] private __gap;
}

File 33 of 53 : IERC721Upgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;

import "../../introspection/IERC165Upgradeable.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721Upgradeable is IERC165Upgradeable {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    /**
      * @dev Safely transfers `tokenId` token from `from` to `to`.
      *
      * Requirements:
      *
      * - `from` cannot be the zero address.
      * - `to` cannot be the zero address.
      * - `tokenId` token must exist and be owned by `from`.
      * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
      * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
      *
      * Emits a {Transfer} event.
      */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
}

File 34 of 53 : IERC721MetadataUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;

import "./IERC721Upgradeable.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721MetadataUpgradeable is IERC721Upgradeable {

    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

File 35 of 53 : IERC721EnumerableUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;

import "./IERC721Upgradeable.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721EnumerableUpgradeable is IERC721Upgradeable {

    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

File 36 of 53 : IERC721ReceiverUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721ReceiverUpgradeable {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
     */
    function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}

File 37 of 53 : ERC165Upgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "./IERC165Upgradeable.sol";
import "../proxy/Initializable.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts may inherit from this and call {_registerInterface} to declare
 * their support of an interface.
 */
abstract contract ERC165Upgradeable is Initializable, IERC165Upgradeable {
    /*
     * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
     */
    bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;

    /**
     * @dev Mapping of interface ids to whether or not it's supported.
     */
    mapping(bytes4 => bool) private _supportedInterfaces;

    function __ERC165_init() internal initializer {
        __ERC165_init_unchained();
    }

    function __ERC165_init_unchained() internal initializer {
        // Derived contracts need only register support for their own interfaces,
        // we register support for ERC165 itself here
        _registerInterface(_INTERFACE_ID_ERC165);
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     *
     * Time complexity O(1), guaranteed to always use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return _supportedInterfaces[interfaceId];
    }

    /**
     * @dev Registers the contract as an implementer of the interface defined by
     * `interfaceId`. Support of the actual ERC165 interface is automatic and
     * registering its interface id is not required.
     *
     * See {IERC165-supportsInterface}.
     *
     * Requirements:
     *
     * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
     */
    function _registerInterface(bytes4 interfaceId) internal virtual {
        require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
        _supportedInterfaces[interfaceId] = true;
    }
    uint256[49] private __gap;
}

File 38 of 53 : SafeMathUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMathUpgradeable {
    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        uint256 c = a + b;
        if (c < a) return (false, 0);
        return (true, c);
    }

    /**
     * @dev Returns the substraction of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b > a) return (false, 0);
        return (true, a - b);
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) return (true, 0);
        uint256 c = a * b;
        if (c / a != b) return (false, 0);
        return (true, c);
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a / b);
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a % b);
    }

    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        return a - b;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) return 0;
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: modulo by zero");
        return a % b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {trySub}.
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        return a - b;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryDiv}.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a % b;
    }
}

File 39 of 53 : AddressUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;

/**
 * @dev Collection of functions related to the address type
 */
library AddressUpgradeable {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
        (bool success, ) = recipient.call{ value: amount }("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain`call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
      return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.call{ value: value }(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                // solhint-disable-next-line no-inline-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

File 40 of 53 : EnumerableSetUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 */
library EnumerableSetUpgradeable {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;

        // Position of the value in the `values` array, plus 1 because index 0
        // means a value is not in the set.
        mapping (bytes32 => uint256) _indexes;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._indexes[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We read and store the value's index to prevent multiple reads from the same storage slot
        uint256 valueIndex = set._indexes[value];

        if (valueIndex != 0) { // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

            // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
            // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.

            bytes32 lastvalue = set._values[lastIndex];

            // Move the last value to the index where the value to delete is
            set._values[toDeleteIndex] = lastvalue;
            // Update the index for the moved value
            set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the index for the deleted slot
            delete set._indexes[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._indexes[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        require(set._values.length > index, "EnumerableSet: index out of bounds");
        return set._values[index];
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }


    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

   /**
    * @dev Returns the value stored at position `index` in the set. O(1).
    *
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }
}

File 41 of 53 : EnumerableMapUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Library for managing an enumerable variant of Solidity's
 * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
 * type.
 *
 * Maps have the following properties:
 *
 * - Entries are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Entries are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```
 * contract Example {
 *     // Add the library methods
 *     using EnumerableMap for EnumerableMap.UintToAddressMap;
 *
 *     // Declare a set state variable
 *     EnumerableMap.UintToAddressMap private myMap;
 * }
 * ```
 *
 * As of v3.0.0, only maps of type `uint256 -> address` (`UintToAddressMap`) are
 * supported.
 */
library EnumerableMapUpgradeable {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Map type with
    // bytes32 keys and values.
    // The Map implementation uses private functions, and user-facing
    // implementations (such as Uint256ToAddressMap) are just wrappers around
    // the underlying Map.
    // This means that we can only create new EnumerableMaps for types that fit
    // in bytes32.

    struct MapEntry {
        bytes32 _key;
        bytes32 _value;
    }

    struct Map {
        // Storage of map keys and values
        MapEntry[] _entries;

        // Position of the entry defined by a key in the `entries` array, plus 1
        // because index 0 means a key is not in the map.
        mapping (bytes32 => uint256) _indexes;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function _set(Map storage map, bytes32 key, bytes32 value) private returns (bool) {
        // We read and store the key's index to prevent multiple reads from the same storage slot
        uint256 keyIndex = map._indexes[key];

        if (keyIndex == 0) { // Equivalent to !contains(map, key)
            map._entries.push(MapEntry({ _key: key, _value: value }));
            // The entry is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            map._indexes[key] = map._entries.length;
            return true;
        } else {
            map._entries[keyIndex - 1]._value = value;
            return false;
        }
    }

    /**
     * @dev Removes a key-value pair from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function _remove(Map storage map, bytes32 key) private returns (bool) {
        // We read and store the key's index to prevent multiple reads from the same storage slot
        uint256 keyIndex = map._indexes[key];

        if (keyIndex != 0) { // Equivalent to contains(map, key)
            // To delete a key-value pair from the _entries array in O(1), we swap the entry to delete with the last one
            // in the array, and then remove the last entry (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = keyIndex - 1;
            uint256 lastIndex = map._entries.length - 1;

            // When the entry to delete is the last one, the swap operation is unnecessary. However, since this occurs
            // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.

            MapEntry storage lastEntry = map._entries[lastIndex];

            // Move the last entry to the index where the entry to delete is
            map._entries[toDeleteIndex] = lastEntry;
            // Update the index for the moved entry
            map._indexes[lastEntry._key] = toDeleteIndex + 1; // All indexes are 1-based

            // Delete the slot where the moved entry was stored
            map._entries.pop();

            // Delete the index for the deleted slot
            delete map._indexes[key];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function _contains(Map storage map, bytes32 key) private view returns (bool) {
        return map._indexes[key] != 0;
    }

    /**
     * @dev Returns the number of key-value pairs in the map. O(1).
     */
    function _length(Map storage map) private view returns (uint256) {
        return map._entries.length;
    }

   /**
    * @dev Returns the key-value pair stored at position `index` in the map. O(1).
    *
    * Note that there are no guarantees on the ordering of entries inside the
    * array, and it may change when more entries are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function _at(Map storage map, uint256 index) private view returns (bytes32, bytes32) {
        require(map._entries.length > index, "EnumerableMap: index out of bounds");

        MapEntry storage entry = map._entries[index];
        return (entry._key, entry._value);
    }

    /**
     * @dev Tries to returns the value associated with `key`.  O(1).
     * Does not revert if `key` is not in the map.
     */
    function _tryGet(Map storage map, bytes32 key) private view returns (bool, bytes32) {
        uint256 keyIndex = map._indexes[key];
        if (keyIndex == 0) return (false, 0); // Equivalent to contains(map, key)
        return (true, map._entries[keyIndex - 1]._value); // All indexes are 1-based
    }

    /**
     * @dev Returns the value associated with `key`.  O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function _get(Map storage map, bytes32 key) private view returns (bytes32) {
        uint256 keyIndex = map._indexes[key];
        require(keyIndex != 0, "EnumerableMap: nonexistent key"); // Equivalent to contains(map, key)
        return map._entries[keyIndex - 1]._value; // All indexes are 1-based
    }

    /**
     * @dev Same as {_get}, with a custom error message when `key` is not in the map.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {_tryGet}.
     */
    function _get(Map storage map, bytes32 key, string memory errorMessage) private view returns (bytes32) {
        uint256 keyIndex = map._indexes[key];
        require(keyIndex != 0, errorMessage); // Equivalent to contains(map, key)
        return map._entries[keyIndex - 1]._value; // All indexes are 1-based
    }

    // UintToAddressMap

    struct UintToAddressMap {
        Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
        return _set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
        return _remove(map._inner, bytes32(key));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
        return _contains(map._inner, bytes32(key));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(UintToAddressMap storage map) internal view returns (uint256) {
        return _length(map._inner);
    }

   /**
    * @dev Returns the element stored at position `index` in the set. O(1).
    * Note that there are no guarantees on the ordering of values inside the
    * array, and it may change when more values are added or removed.
    *
    * Requirements:
    *
    * - `index` must be strictly less than {length}.
    */
    function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
        (bytes32 key, bytes32 value) = _at(map._inner, index);
        return (uint256(key), address(uint160(uint256(value))));
    }

    /**
     * @dev Tries to returns the value associated with `key`.  O(1).
     * Does not revert if `key` is not in the map.
     *
     * _Available since v3.4._
     */
    function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
        (bool success, bytes32 value) = _tryGet(map._inner, bytes32(key));
        return (success, address(uint160(uint256(value))));
    }

    /**
     * @dev Returns the value associated with `key`.  O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
        return address(uint160(uint256(_get(map._inner, bytes32(key)))));
    }

    /**
     * @dev Same as {get}, with a custom error message when `key` is not in the map.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryGet}.
     */
    function get(UintToAddressMap storage map, uint256 key, string memory errorMessage) internal view returns (address) {
        return address(uint160(uint256(_get(map._inner, bytes32(key), errorMessage))));
    }
}

File 42 of 53 : StringsUpgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev String operations.
 */
library StringsUpgradeable {
    /**
     * @dev Converts a `uint256` to its ASCII `string` representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        uint256 index = digits - 1;
        temp = value;
        while (temp != 0) {
            buffer[index--] = bytes1(uint8(48 + temp % 10));
            temp /= 10;
        }
        return string(buffer);
    }
}

File 43 of 53 : IERC165Upgradeable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165Upgradeable {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

File 44 of 53 : SafeMath.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        uint256 c = a + b;
        if (c < a) return (false, 0);
        return (true, c);
    }

    /**
     * @dev Returns the substraction of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b > a) return (false, 0);
        return (true, a - b);
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) return (true, 0);
        uint256 c = a * b;
        if (c / a != b) return (false, 0);
        return (true, c);
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a / b);
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a % b);
    }

    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        return a - b;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) return 0;
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: modulo by zero");
        return a % b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {trySub}.
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        return a - b;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryDiv}.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a % b;
    }
}

File 45 of 53 : AccessControl.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../utils/EnumerableSet.sol";
import "../utils/Address.sol";
import "../utils/Context.sol";

/**
 * @dev Contract module that allows children to implement role-based access
 * control mechanisms.
 *
 * Roles are referred to by their `bytes32` identifier. These should be exposed
 * in the external API and be unique. The best way to achieve this is by
 * using `public constant` hash digests:
 *
 * ```
 * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
 * ```
 *
 * Roles can be used to represent a set of permissions. To restrict access to a
 * function call, use {hasRole}:
 *
 * ```
 * function foo() public {
 *     require(hasRole(MY_ROLE, msg.sender));
 *     ...
 * }
 * ```
 *
 * Roles can be granted and revoked dynamically via the {grantRole} and
 * {revokeRole} functions. Each role has an associated admin role, and only
 * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
 *
 * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
 * that only accounts with this role will be able to grant or revoke other
 * roles. More complex role relationships can be created by using
 * {_setRoleAdmin}.
 *
 * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
 * grant and revoke this role. Extra precautions should be taken to secure
 * accounts that have been granted it.
 */
abstract contract AccessControl is Context {
    using EnumerableSet for EnumerableSet.AddressSet;
    using Address for address;

    struct RoleData {
        EnumerableSet.AddressSet members;
        bytes32 adminRole;
    }

    mapping (bytes32 => RoleData) private _roles;

    bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;

    /**
     * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
     *
     * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
     * {RoleAdminChanged} not being emitted signaling this.
     *
     * _Available since v3.1._
     */
    event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);

    /**
     * @dev Emitted when `account` is granted `role`.
     *
     * `sender` is the account that originated the contract call, an admin role
     * bearer except when using {_setupRole}.
     */
    event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);

    /**
     * @dev Emitted when `account` is revoked `role`.
     *
     * `sender` is the account that originated the contract call:
     *   - if using `revokeRole`, it is the admin role bearer
     *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
     */
    event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);

    /**
     * @dev Returns `true` if `account` has been granted `role`.
     */
    function hasRole(bytes32 role, address account) public view returns (bool) {
        return _roles[role].members.contains(account);
    }

    /**
     * @dev Returns the number of accounts that have `role`. Can be used
     * together with {getRoleMember} to enumerate all bearers of a role.
     */
    function getRoleMemberCount(bytes32 role) public view returns (uint256) {
        return _roles[role].members.length();
    }

    /**
     * @dev Returns one of the accounts that have `role`. `index` must be a
     * value between 0 and {getRoleMemberCount}, non-inclusive.
     *
     * Role bearers are not sorted in any particular way, and their ordering may
     * change at any point.
     *
     * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
     * you perform all queries on the same block. See the following
     * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
     * for more information.
     */
    function getRoleMember(bytes32 role, uint256 index) public view returns (address) {
        return _roles[role].members.at(index);
    }

    /**
     * @dev Returns the admin role that controls `role`. See {grantRole} and
     * {revokeRole}.
     *
     * To change a role's admin, use {_setRoleAdmin}.
     */
    function getRoleAdmin(bytes32 role) public view returns (bytes32) {
        return _roles[role].adminRole;
    }

    /**
     * @dev Grants `role` to `account`.
     *
     * If `account` had not been already granted `role`, emits a {RoleGranted}
     * event.
     *
     * Requirements:
     *
     * - the caller must have ``role``'s admin role.
     */
    function grantRole(bytes32 role, address account) public virtual {
        require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to grant");

        _grantRole(role, account);
    }

    /**
     * @dev Revokes `role` from `account`.
     *
     * If `account` had been granted `role`, emits a {RoleRevoked} event.
     *
     * Requirements:
     *
     * - the caller must have ``role``'s admin role.
     */
    function revokeRole(bytes32 role, address account) public virtual {
        require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to revoke");

        _revokeRole(role, account);
    }

    /**
     * @dev Revokes `role` from the calling account.
     *
     * Roles are often managed via {grantRole} and {revokeRole}: this function's
     * purpose is to provide a mechanism for accounts to lose their privileges
     * if they are compromised (such as when a trusted device is misplaced).
     *
     * If the calling account had been granted `role`, emits a {RoleRevoked}
     * event.
     *
     * Requirements:
     *
     * - the caller must be `account`.
     */
    function renounceRole(bytes32 role, address account) public virtual {
        require(account == _msgSender(), "AccessControl: can only renounce roles for self");

        _revokeRole(role, account);
    }

    /**
     * @dev Grants `role` to `account`.
     *
     * If `account` had not been already granted `role`, emits a {RoleGranted}
     * event. Note that unlike {grantRole}, this function doesn't perform any
     * checks on the calling account.
     *
     * [WARNING]
     * ====
     * This function should only be called from the constructor when setting
     * up the initial roles for the system.
     *
     * Using this function in any other way is effectively circumventing the admin
     * system imposed by {AccessControl}.
     * ====
     */
    function _setupRole(bytes32 role, address account) internal virtual {
        _grantRole(role, account);
    }

    /**
     * @dev Sets `adminRole` as ``role``'s admin role.
     *
     * Emits a {RoleAdminChanged} event.
     */
    function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
        emit RoleAdminChanged(role, _roles[role].adminRole, adminRole);
        _roles[role].adminRole = adminRole;
    }

    function _grantRole(bytes32 role, address account) private {
        if (_roles[role].members.add(account)) {
            emit RoleGranted(role, account, _msgSender());
        }
    }

    function _revokeRole(bytes32 role, address account) private {
        if (_roles[role].members.remove(account)) {
            emit RoleRevoked(role, account, _msgSender());
        }
    }
}

File 46 of 53 : Context.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../utils/Context.sol";

File 47 of 53 : IERC20.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

File 48 of 53 : Address.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
        (bool success, ) = recipient.call{ value: amount }("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain`call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
      return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.call{ value: value }(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                // solhint-disable-next-line no-inline-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

File 49 of 53 : Context.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with GSN meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address payable) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes memory) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
}

File 50 of 53 : SimplePriceOracle.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./Interfaces/PriceOracle.sol";
import "./CErc20.sol";

/**
 * Temporary simple price feed 
 */
contract SimplePriceOracle is PriceOracle {
    /// @notice Indicator that this is a PriceOracle contract (for inspection)
    bool public constant isPriceOracle = true;

    mapping(address => uint) prices;

    event PricePosted(address asset, uint previousPriceMantissa, uint requestedPriceMantissa, uint newPriceMantissa);

    function getUnderlyingPrice(MToken mToken) public override view returns (uint) {
        if (compareStrings(mToken.symbol(), "mDAI")) {
            return 1e18;
        } else {
            return prices[address(MErc20(address(mToken)).underlying())];
        }
    }

    function setUnderlyingPrice(MToken mToken, uint underlyingPriceMantissa) public {
        address asset = address(MErc20(address(mToken)).underlying());
        emit PricePosted(asset, prices[asset], underlyingPriceMantissa, underlyingPriceMantissa);
        prices[asset] = underlyingPriceMantissa;
    }

    function setDirectPrice(address asset, uint price) public {
        emit PricePosted(asset, prices[asset], price, price);
        prices[asset] = price;
    }

    // v1 price oracle interface for use as backing of proxy
    function assetPrices(address asset) external view returns (uint) {
        return prices[asset];
    }

    function compareStrings(string memory a, string memory b) internal pure returns (bool) {
        return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
    }
}

File 51 of 53 : Ownable.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../utils/Context.sol";
/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor () internal {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

File 52 of 53 : CErc20.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "./MToken.sol";
import "./Interfaces/MErc20Interface.sol";
import "./Moartroller.sol";
import "./AbstractInterestRateModel.sol";
import "./Interfaces/EIP20Interface.sol";
import "./Utils/SafeEIP20.sol";

/**
 * @title MOAR's MErc20 Contract
 * @notice MTokens which wrap an EIP-20 underlying
 */
contract MErc20 is MToken, MErc20Interface {

    using SafeEIP20 for EIP20Interface;

    /**
     * @notice Initialize the new money market
     * @param underlying_ The address of the underlying asset
     * @param moartroller_ The address of the Moartroller
     * @param interestRateModel_ The address of the interest rate model
     * @param initialExchangeRateMantissa_ The initial exchange rate, scaled by 1e18
     * @param name_ ERC-20 name of this token
     * @param symbol_ ERC-20 symbol of this token
     * @param decimals_ ERC-20 decimal precision of this token
     */
    function init(address underlying_,
                        Moartroller moartroller_,
                        AbstractInterestRateModel interestRateModel_,
                        uint initialExchangeRateMantissa_,
                        string memory name_,
                        string memory symbol_,
                        uint8 decimals_) public {
        // MToken initialize does the bulk of the work
        super.init(moartroller_, interestRateModel_, initialExchangeRateMantissa_, name_, symbol_, decimals_);

        // Set underlying and sanity check it
        underlying = underlying_;
        EIP20Interface(underlying).totalSupply();
    }

    /*** User Interface ***/

    /**
     * @notice Sender supplies assets into the market and receives mTokens in exchange
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param mintAmount The amount of the underlying asset to supply
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function mint(uint mintAmount) external override returns (uint) {
        (uint err,) = mintInternal(mintAmount);
        return err;
    }

    /**
     * @notice Sender redeems mTokens in exchange for the underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemTokens The number of mTokens to redeem into underlying
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeem(uint redeemTokens) external override returns (uint) {
        return redeemInternal(redeemTokens);
    }

    /**
     * @notice Sender redeems mTokens in exchange for a specified amount of underlying asset
     * @dev Accrues interest whether or not the operation succeeds, unless reverted
     * @param redeemAmount The amount of underlying to redeem
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function redeemUnderlying(uint redeemAmount) external override returns (uint) {
        return redeemUnderlyingInternal(redeemAmount);
    }

    /**
      * @notice Sender borrows assets from the protocol to their own address
      * @param borrowAmount The amount of the underlying asset to borrow
      * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
      */
    function borrow(uint borrowAmount) external override returns (uint) {
        return borrowInternal(borrowAmount);
    }

    function borrowFor(address payable borrower, uint borrowAmount) external override returns (uint) {
        return borrowForInternal(borrower, borrowAmount);
    }

    /**
     * @notice Sender repays their own borrow
     * @param repayAmount The amount to repay
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function repayBorrow(uint repayAmount) external override returns (uint) {
        (uint err,) = repayBorrowInternal(repayAmount);
        return err;
    }

    /**
     * @notice Sender repays a borrow belonging to borrower.
     * @param borrower the account with the debt being payed off
     * @param repayAmount The amount to repay
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function repayBorrowBehalf(address borrower, uint repayAmount) external override returns (uint) {
        (uint err,) = repayBorrowBehalfInternal(borrower, repayAmount);
        return err;
    }

    /**
     * @notice The sender liquidates the borrowers collateral.
     *  The collateral seized is transferred to the liquidator.
     * @param borrower The borrower of this mToken to be liquidated
     * @param repayAmount The amount of the underlying borrowed asset to repay
     * @param mTokenCollateral The market in which to seize collateral from the borrower
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function liquidateBorrow(address borrower, uint repayAmount, MToken mTokenCollateral) external override returns (uint) {
        (uint err,) = liquidateBorrowInternal(borrower, repayAmount, mTokenCollateral);
        return err;
    }

    /**
     * @notice A public function to sweep accidental ERC-20 transfers to this contract. Tokens are sent to admin (timelock)
     * @param token The address of the ERC-20 token to sweep
     */
    function sweepToken(EIP20Interface token) override external {
    	require(address(token) != underlying, "MErc20::sweepToken: can not sweep underlying token");
    	uint256 balance = token.balanceOf(address(this));
    	token.safeTransfer(admin, balance);
    }

    /**
     * @notice The sender adds to reserves.
     * @param addAmount The amount fo underlying token to add as reserves
     * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
     */
    function _addReserves(uint addAmount) external override returns (uint) {
        return _addReservesInternal(addAmount);
    }

    /*** Safe Token ***/

    /**
     * @notice Gets balance of this contract in terms of the underlying
     * @dev This excludes the value of the current message, if any
     * @return The quantity of underlying tokens owned by this contract
     */
    function getCashPrior() internal override view returns (uint) {
        EIP20Interface token = EIP20Interface(underlying);
        return token.balanceOf(address(this));
    }

    /**
     * @dev Similar to EIP20 transfer, except it handles a False result from `transferFrom` and reverts in that case.
     *      This will revert due to insufficient balance or insufficient allowance.
     *      This function returns the actual amount received,
     *      which may be less than `amount` if there is a fee attached to the transfer.
     *`
     *      Note: This wrapper safely handles non-standard ERC-20 tokens that do not return a value.
     *            See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
     */
    function doTransferIn(address from, uint amount) internal override returns (uint) {
        EIP20Interface token = EIP20Interface(underlying);
        uint balanceBefore = token.balanceOf(address(this));
        token.safeTransferFrom(from, address(this), amount);

        // Calculate the amount that was *actually* transferred
        uint balanceAfter = token.balanceOf(address(this));
        require(balanceAfter >= balanceBefore, "TOKEN_TRANSFER_IN_OVERFLOW");
        return balanceAfter - balanceBefore;   // underflow already checked above, just subtract
    }

    /**
     * @dev Similar to EIP20 transfer, except it handles a False success from `transfer` and returns an explanatory
     *      error code rather than reverting. If caller has not called checked protocol's balance, this may revert due to
     *      insufficient cash held in this contract. If caller has checked protocol's balance prior to this call, and verified
     *      it is >= amount, this should not revert in normal conditions.
     *
     *      Note: This wrapper safely handles non-standard ERC-20 tokens that do not return a value.
     *            See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
     */
    function doTransferOut(address payable to, uint amount) internal override {
        EIP20Interface token = EIP20Interface(underlying);
        token.safeTransfer(to, amount);
    }
}

File 53 of 53 : MErc20Interface.sol
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.6.12;

import "../MToken.sol";

interface MErc20Interface {
    /*** User contract ***/
    function mint(uint mintAmount) external returns (uint);
    function redeem(uint redeemTokens) external returns (uint);
    function redeemUnderlying(uint redeemAmount) external returns (uint);
    function borrow(uint borrowAmount) external returns (uint);
    function borrowFor(address payable borrower, uint borrowAmount) external returns (uint);
    function repayBorrow(uint repayAmount) external returns (uint);
    function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint);
    function liquidateBorrow(address borrower, uint repayAmount, MToken mTokenCollateral) external returns (uint);

    /*** Admin Functions ***/
    function _addReserves(uint addAmount) external returns (uint);
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "abi"
      ]
    }
  },
  "libraries": {}
}

Contract Security Audit

Contract ABI

[{"inputs":[{"internalType":"address","name":"underlying_","type":"address"},{"internalType":"contract Moartroller","name":"moartroller_","type":"address"},{"internalType":"contract AbstractInterestRateModel","name":"interestRateModel_","type":"address"},{"internalType":"uint256","name":"initialExchangeRateMantissa_","type":"uint256"},{"internalType":"string","name":"name_","type":"string"},{"internalType":"string","name":"symbol_","type":"string"},{"internalType":"uint8","name":"decimals_","type":"uint8"},{"internalType":"address payable","name":"admin_","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"cashPrior","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"interestAccumulated","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"borrowIndex","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalBorrows","type":"uint256"}],"name":"AccrueInterest","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"borrower","type":"address"},{"indexed":false,"internalType":"uint256","name":"borrowAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"accountBorrows","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalBorrows","type":"uint256"}],"name":"Borrow","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"error","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"info","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"detail","type":"uint256"}],"name":"Failure","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"liquidator","type":"address"},{"indexed":false,"internalType":"address","name":"borrower","type":"address"},{"indexed":false,"internalType":"uint256","name":"repayAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"MTokenCollateral","type":"address"},{"indexed":false,"internalType":"uint256","name":"seizeTokens","type":"uint256"}],"name":"LiquidateBorrow","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"minter","type":"address"},{"indexed":false,"internalType":"uint256","name":"mintAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"mintTokens","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"newValue","type":"uint256"}],"name":"MpcUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"NewAdmin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract InterestRateModelInterface","name":"oldInterestRateModel","type":"address"},{"indexed":false,"internalType":"contract InterestRateModelInterface","name":"newInterestRateModel","type":"address"}],"name":"NewMarketInterestRateModel","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract Moartroller","name":"oldMoartroller","type":"address"},{"indexed":false,"internalType":"contract Moartroller","name":"newMoartroller","type":"address"}],"name":"NewMoartroller","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldPendingAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newPendingAdmin","type":"address"}],"name":"NewPendingAdmin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldReserveFactorMantissa","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newReserveFactorMantissa","type":"uint256"}],"name":"NewReserveFactor","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"redeemer","type":"address"},{"indexed":false,"internalType":"uint256","name":"redeemAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"redeemTokens","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"payer","type":"address"},{"indexed":false,"internalType":"address","name":"borrower","type":"address"},{"indexed":false,"internalType":"uint256","name":"repayAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"accountBorrows","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalBorrows","type":"uint256"}],"name":"RepayBorrow","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"benefactor","type":"address"},{"indexed":false,"internalType":"uint256","name":"addAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newTotalReserves","type":"uint256"}],"name":"ReservesAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"admin","type":"address"},{"indexed":false,"internalType":"uint256","name":"reduceAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newTotalReserves","type":"uint256"}],"name":"ReservesReduced","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"_acceptAdmin","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"_addReserves","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"reduceAmount","type":"uint256"}],"name":"_reduceReserves","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract AbstractInterestRateModel","name":"newInterestRateModel","type":"address"}],"name":"_setInterestRateModel","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newMPC","type":"uint256"}],"name":"_setMaxProtectionComposition","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract Moartroller","name":"newMoartroller","type":"address"}],"name":"_setMoartroller","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"newPendingAdmin","type":"address"}],"name":"_setPendingAdmin","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newReserveFactorMantissa","type":"uint256"}],"name":"_setReserveFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newReserveSplitFactorMantissa","type":"uint256"}],"name":"_setReserveSplitFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"accountBorrows","outputs":[{"internalType":"uint256","name":"principal","type":"uint256"},{"internalType":"uint256","name":"interestIndex","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"accrualBlockNumber","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"accrueInterest","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOfUnderlying","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"borrowAmount","type":"uint256"}],"name":"borrow","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"borrowBalanceCurrent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"borrowBalanceStored","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address payable","name":"borrower","type":"address"},{"internalType":"uint256","name":"borrowAmount","type":"uint256"}],"name":"borrowFor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"borrowIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"borrowRatePerBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exchangeRateCurrent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"exchangeRateStored","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"getAccountSnapshot","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCash","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRealBorrowIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUnderlying","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract Moartroller","name":"moartroller_","type":"address"},{"internalType":"contract AbstractInterestRateModel","name":"interestRateModel_","type":"address"},{"internalType":"uint256","name":"initialExchangeRateMantissa_","type":"uint256"},{"internalType":"string","name":"name_","type":"string"},{"internalType":"string","name":"symbol_","type":"string"},{"internalType":"uint8","name":"decimals_","type":"uint8"}],"name":"init","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"interestRateModel","outputs":[{"internalType":"contract AbstractInterestRateModel","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isMToken","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"borrower","type":"address"},{"internalType":"contract MToken","name":"mTokenCollateral","type":"address"}],"name":"liquidateBorrow","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"maxProtectionComposition","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxProtectionCompositionMantissa","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mint","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"moartroller","outputs":[{"internalType":"contract Moartroller","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingAdmin","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"redeemTokens","type":"uint256"}],"name":"redeem","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"redeemAmount","type":"uint256"}],"name":"redeemUnderlying","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"repayBorrow","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"borrower","type":"address"}],"name":"repayBorrowBehalf","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"reserveFactorMantissa","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"reserveSplitFactorMantissa","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"liquidator","type":"address"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"seizeTokens","type":"uint256"}],"name":"seize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"supplyRatePerBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract EIP20Interface","name":"token","type":"address"}],"name":"sweepToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalBorrows","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalBorrowsCurrent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalReserves","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"dst","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"src","type":"address"},{"internalType":"address","name":"dst","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"underlying","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]

60806040523480156200001157600080fd5b50604051620067143803806200671483398181016040526101008110156200003857600080fd5b81516020830151604080850151606086015160808701805193519597949692959194919392820192846401000000008211156200007457600080fd5b9083019060208201858111156200008a57600080fd5b8251640100000000811182820188101715620000a557600080fd5b82525081516020918201929091019080838360005b83811015620000d4578181015183820152602001620000ba565b50505050905090810190601f168015620001025780820380516001836020036101000a031916815260200191505b50604052602001805160405193929190846401000000008211156200012657600080fd5b9083019060208201858111156200013c57600080fd5b82516401000000008111828201881017156200015757600080fd5b82525081516020918201929091019080838360005b83811015620001865781810151838201526020016200016c565b50505050905090810190601f168015620001b45780820380516001836020036101000a031916815260200191505b5060409081526020820151910151600680546001600160a01b031916331790559092509050620001e9878787878787620002bb565b87600360016101000a8154816001600160a01b0302191690836001600160a01b03160217905550600360019054906101000a90046001600160a01b03166001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b1580156200025f57600080fd5b505afa15801562000274573d6000803e3d6000fd5b505050506040513d60208110156200028b57600080fd5b5050600680546001600160a01b0319166001600160a01b039290921691909117905550620008ba95505050505050565b6006546001600160a01b0316331462000307576040805162461bcd60e51b81526020600482015260096024820152683737ba2fb0b236b4b760b91b604482015290519081900360640190fd5b600d54158015620003185750600e54155b62000359576040805162461bcd60e51b815260206004820152600c60248201526b185b1c9958591e57da5b9a5d60a21b604482015290519081900360640190fd5b600a849055836200039b576040805162461bcd60e51b8152602060048201526007602482015266746f6f5f6c6f7760c81b604482015290519081900360640190fd5b6000620003a887620004e1565b90508015620003fe576040805162461bcd60e51b815260206004820152601a60248201527f73657474696e67206d6f617274726f6c6c6572206661696c6564000000000000604482015290519081900360640190fd5b620004086200062f565b600d55670de0b6b3a7640000600e55620004228662000633565b905080156200046d576040805162461bcd60e51b81526020600482015260126024820152711cd95d1d1a5b99c81254934819985a5b195960721b604482015290519081900360640190fd5b8351620004829060019060208701906200081e565b508251620004989060029060208601906200081e565b50506003805460ff90921660ff199283161790556000805490911660011790555050611388601255505061271060135550670de0b6b3a764000060055565048c27395000600455565b6006546000906001600160a01b031633146200050d57620005056001603f620007ae565b90506200062a565b6008546040805163039459d960e11b815290516001600160a01b0392831692851691630728b3b2916004808301926020929190829003018186803b1580156200055557600080fd5b505afa1580156200056a573d6000803e3d6000fd5b505050506040513d60208110156200058157600080fd5b5051620005c7576040805162461bcd60e51b815260206004820152600f60248201526e3737ba2fb6b7b0b93a3937b63632b960891b604482015290519081900360640190fd5b600880546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517f21791446553f95da6d7e0507ffbbddb0583d1998fbea8f6eb12000b94b88eff79281900390910190a160005b9150505b919050565b4390565b60065460009081906001600160a01b0316331462000662576200065960016042620007ae565b9150506200062a565b6200066c6200062f565b600d5414620006835762000659600a6041620007ae565b600960009054906101000a90046001600160a01b03169050826001600160a01b0316632191f92a6040518163ffffffff1660e01b815260040160206040518083038186803b158015620006d557600080fd5b505afa158015620006ea573d6000803e3d6000fd5b505050506040513d60208110156200070157600080fd5b50516200074a576040805162461bcd60e51b81526020600482015260126024820152711b9bdd17da5b9d195c995cdd17db5bd9195b60721b604482015290519081900360640190fd5b600980546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517fedffc32e068c7c95dfd4bdfd5c4d939a084d6b11c4199eac8436ed234d72f9269281900390910190a1600062000626565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa0836010811115620007de57fe5b836050811115620007eb57fe5b604080519283526020830191909152600082820152519081900360600190a18260108111156200081757fe5b9392505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200086157805160ff191683800117855562000891565b8280016001018555821562000891579182015b828111156200089157825182559160200191906001019062000874565b506200089f929150620008a3565b5090565b5b808211156200089f5760008155600101620008a4565b615e4a80620008ca6000396000f3fe60806040526004361061037a5760003560e01c8063852a12e3116101d1578063c37f68e211610102578063e9a1183d116100a0578063f851a4401161006f578063f851a44014610d9a578063f8f9da2814610daf578063fca7820b14610dc4578063fcb6414714610dee5761043e565b8063e9a1183d14610d04578063e9c714f214610d3d578063f2b3abbd14610d52578063f3fdb15a14610d855761043e565b8063db006a75116100dc578063db006a7514610c64578063dd62ed3e14610c8e578063dfa9f0fe14610cc9578063e597461914610cde5761043e565b8063c37f68e214610b95578063c5ebeaec14610bee578063d40e8f4a14610c185761043e565b8063a9059cbb1161016f578063ae9d70b011610149578063ae9d70b014610af5578063b2a02ff114610b0a578063b71d1a0c14610b4d578063bd6d894d14610b805761043e565b8063a9059cbb14610a79578063aa5af0fd14610ab2578063aae40a2a14610ac75761043e565b806395d89b41116101ab57806395d89b4114610a0757806395dd919314610a1c5780639816f47314610a4f578063a6afed9514610a645761043e565b8063852a12e3146108695780638f840ddd1461089357806394d6c574146108a85761043e565b80633af9e669116102ab57806360ae7af8116102495780636c540baf116102235780636c540baf146107f75780636f307dc31461080c57806370a082311461082157806373acee98146108545761043e565b806360ae7af8146107a3578063699cd5e2146107b85780636b58b0b2146107cd5761043e565b80634e4d9fea116102855780634e4d9fea1461072957806353869f0a14610731578063551a312614610746578063601a0bf1146107795761043e565b80633af9e669146106cc5780633b1d21a2146106ff57806347bd3718146107145761043e565b806318160ddd116103185780631be19560116102f25780631be19560146105fa57806323b872dd1461062d5780632678224714610670578063313ce567146106a15761043e565b806318160ddd146105bb578063182df0f5146105d057806318f68ef9146105e55761043e565b80630f5e888f116103545780630f5e888f146105415780631249c58b1461056b578063173b99041461057357806317bfdfbc146105885761043e565b806305920a391461044357806306fdde031461046a578063095ea7b3146104f45761043e565b3661043e5760035461010090046001600160a01b0316331461043c57600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156103e657600080fd5b505af11580156103fa573d6000803e3d6000fd5b5050505050600061040a34610df6565b50905061043a816040518060400160405280600b81526020016a1b5a5b9d0819985a5b195960aa1b815250610e9d565b505b005b600080fd5b34801561044f57600080fd5b506104586110b4565b60408051918252519081900360200190f35b34801561047657600080fd5b5061047f6110ba565b6040805160208082528351818301528351919283929083019185019080838360005b838110156104b95781810151838201526020016104a1565b50505050905090810190601f1680156104e65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561050057600080fd5b5061052d6004803603604081101561051757600080fd5b506001600160a01b038135169060200135611147565b604080519115158252519081900360200190f35b34801561054d57600080fd5b506104586004803603602081101561056457600080fd5b50356111b4565b61043c61121f565b34801561057f57600080fd5b506104586112c6565b34801561059457600080fd5b50610458600480360360208110156105ab57600080fd5b50356001600160a01b03166112cc565b3480156105c757600080fd5b5061045861138c565b3480156105dc57600080fd5b50610458611392565b3480156105f157600080fd5b5061045861140b565b34801561060657600080fd5b5061043c6004803603602081101561061d57600080fd5b50356001600160a01b0316611411565b34801561063957600080fd5b5061052d6004803603606081101561065057600080fd5b506001600160a01b038135811691602081013590911690604001356114fb565b34801561067c57600080fd5b5061068561156d565b604080516001600160a01b039092168252519081900360200190f35b3480156106ad57600080fd5b506106b661157c565b6040805160ff9092168252519081900360200190f35b3480156106d857600080fd5b50610458600480360360208110156106ef57600080fd5b50356001600160a01b0316611585565b34801561070b57600080fd5b5061045861163b565b34801561072057600080fd5b5061045861164a565b61043c611650565b34801561073d57600080fd5b506104586116fb565b34801561075257600080fd5b506104586004803603602081101561076957600080fd5b50356001600160a01b0316611701565b34801561078557600080fd5b506104586004803603602081101561079c57600080fd5b503561183e565b3480156107af57600080fd5b506106856118d9565b3480156107c457600080fd5b5061052d6118e8565b3480156107d957600080fd5b50610458600480360360208110156107f057600080fd5b50356118ed565b34801561080357600080fd5b5061045861196d565b34801561081857600080fd5b50610685611973565b34801561082d57600080fd5b506104586004803603602081101561084457600080fd5b50356001600160a01b0316611987565b34801561086057600080fd5b506104586119a2565b34801561087557600080fd5b506104586004803603602081101561088c57600080fd5b5035611a58565b34801561089f57600080fd5b50610458611a63565b3480156108b457600080fd5b5061043c600480360360c08110156108cb57600080fd5b6001600160a01b0382358116926020810135909116916040820135919081019060808101606082013564010000000081111561090657600080fd5b82018360208201111561091857600080fd5b8035906020019184600183028401116401000000008311171561093a57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929594936020810193503591505064010000000081111561098d57600080fd5b82018360208201111561099f57600080fd5b803590602001918460018302840111640100000000831117156109c157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295505050903560ff169150611a699050565b348015610a1357600080fd5b5061047f611c7f565b348015610a2857600080fd5b5061045860048036036020811015610a3f57600080fd5b50356001600160a01b0316611cd7565b348015610a5b57600080fd5b50610685611d4a565b348015610a7057600080fd5b50610458611d5e565b348015610a8557600080fd5b5061052d60048036036040811015610a9c57600080fd5b506001600160a01b03813516906020013561230e565b348015610abe57600080fd5b50610458612380565b61043c60048036036040811015610add57600080fd5b506001600160a01b0381358116916020013516612386565b348015610b0157600080fd5b5061045861243c565b348015610b1657600080fd5b5061045860048036036060811015610b2d57600080fd5b506001600160a01b038135811691602081013590911690604001356124db565b348015610b5957600080fd5b5061045860048036036020811015610b7057600080fd5b50356001600160a01b031661254c565b348015610b8c57600080fd5b506104586125d3565b348015610ba157600080fd5b50610bc860048036036020811015610bb857600080fd5b50356001600160a01b031661268f565b604080519485526020850193909352838301919091526060830152519081900360800190f35b348015610bfa57600080fd5b5061045860048036036020811015610c1157600080fd5b5035612724565b348015610c2457600080fd5b50610c4b60048036036020811015610c3b57600080fd5b50356001600160a01b031661272f565b6040805192835260208301919091528051918290030190f35b348015610c7057600080fd5b5061045860048036036020811015610c8757600080fd5b5035612748565b348015610c9a57600080fd5b5061045860048036036040811015610cb157600080fd5b506001600160a01b0381358116916020013516612753565b348015610cd557600080fd5b5061045861277e565b61043c60048036036020811015610cf457600080fd5b50356001600160a01b03166129e6565b348015610d1057600080fd5b5061045860048036036040811015610d2757600080fd5b506001600160a01b038135169060200135612a9d565b348015610d4957600080fd5b50610458612aa9565b348015610d5e57600080fd5b5061045860048036036020811015610d7557600080fd5b50356001600160a01b0316612b99565b348015610d9157600080fd5b50610685612bd3565b348015610da657600080fd5b50610685612be2565b348015610dbb57600080fd5b50610458612bf1565b348015610dd057600080fd5b5061045860048036036020811015610de757600080fd5b5035612c55565b610458612cd3565b60008054819060ff16610e3d576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155610e4f611d5e565b90508015610e7957610e6d816010811115610e6657fe5b601e612d47565b60009250925050610e89565b610e833385612dad565b92509250505b6000805460ff191660011790559092909150565b81610ea7576110b0565b6060815160050167ffffffffffffffff81118015610ec457600080fd5b506040519080825280601f01601f191660200182016040528015610eef576020820181803683370190505b50905060005b8251811015610f4057828181518110610f0a57fe5b602001015160f81c60f81b828281518110610f2157fe5b60200101906001600160f81b031916908160001a905350600101610ef5565b8151600160fd1b90839083908110610f5457fe5b60200101906001600160f81b031916908160001a905350602860f81b828260010181518110610f7f57fe5b60200101906001600160f81b031916908160001a905350600a840460300160f81b828260020181518110610faf57fe5b60200101906001600160f81b031916908160001a905350600a840660300160f81b828260030181518110610fdf57fe5b60200101906001600160f81b031916908160001a905350602960f81b82826004018151811061100a57fe5b60200101906001600160f81b031916908160001a9053508184156110ac5760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015611071578181015183820152602001611059565b50505050905090810190601f16801561109e5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5050505b5050565b60135481565b60018054604080516020600284861615610100026000190190941693909304601f8101849004840282018401909252818152929183018282801561113f5780601f106111145761010080835404028352916020019161113f565b820191906000526020600020905b81548152906001019060200180831161112257829003601f168201915b505050505081565b3360008181526015602090815260408083206001600160a01b03871680855290835281842086905581518681529151939493909284927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929081900390910190a360019150505b92915050565b6006546000906001600160a01b031633146111dc576111d560016042612d47565b905061121a565b60128290556040805183815290517f1461d8f734be2d09431c43882077b36addb0d247b48d4f308691cd188d1fc7249181900360200190a160005b90505b919050565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b15801561126f57600080fd5b505af1158015611283573d6000803e3d6000fd5b5050505050600061129334610df6565b5090506112c3816040518060400160405280600b81526020016a1b5a5b9d0819985a5b195960aa1b815250610e9d565b50565b600b5481565b6000805460ff16611311576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155611323611d5e565b1461136e576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b61137782611cd7565b90505b6000805460ff19166001179055919050565b60115481565b600080600061139f613164565b909250905060008260038111156113b257fe5b14611404576040805162461bcd60e51b815260206004820152601960248201527f65786368616e67655261746553746f726564206661696c656400000000000000604482015290519081900360640190fd5b9150505b90565b600c5481565b6003546001600160a01b038281166101009092041614156114635760405162461bcd60e51b8152600401808060200182810382526032815260200180615de36032913960400191505060405180910390fd5b6000816001600160a01b03166370a08231306040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b1580156114b257600080fd5b505afa1580156114c6573d6000803e3d6000fd5b505050506040513d60208110156114dc57600080fd5b50516006549091506110b0906001600160a01b03848116911683613213565b6000805460ff16611540576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561155633868686613265565b1490506000805460ff191660011790559392505050565b6007546001600160a01b031681565b60035460ff1681565b600061158f615b9e565b60405180602001604052806115a26125d3565b90526001600160a01b0384166000908152601460205260408120549192509081906115ce9084906134f7565b909250905060008260038111156115e157fe5b14611633576040805162461bcd60e51b815260206004820152601a60248201527f62616c616e63655f63616c63756c6174696f6e5f6661696c6564000000000000604482015290519081900360640190fd5b949350505050565b600061164561354a565b905090565b600f5481565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156116a057600080fd5b505af11580156116b4573d6000803e3d6000fd5b505050505060006116c4346135f3565b5090506112c381604051806040016040528060128152602001711c995c185e509bdc9c9bddc819985a5b195960721b815250610e9d565b60125481565b6006546000906001600160a01b03163314611722576111d56001603f612d47565b6008546040805163039459d960e11b815290516001600160a01b0392831692851691630728b3b2916004808301926020929190829003018186803b15801561176957600080fd5b505afa15801561177d573d6000803e3d6000fd5b505050506040513d602081101561179357600080fd5b50516117d8576040805162461bcd60e51b815260206004820152600f60248201526e3737ba2fb6b7b0b93a3937b63632b960891b604482015290519081900360640190fd5b600880546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517f21791446553f95da6d7e0507ffbbddb0583d1998fbea8f6eb12000b94b88eff79281900390910190a160005b9392505050565b6000805460ff16611883576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155611895611d5e565b905080156118bb576118b38160108111156118ac57fe5b6030612d47565b91505061137a565b6118c483613675565b9150506000805460ff19166001179055919050565b6008546001600160a01b031681565b600181565b6000805460ff16611932576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191690556006546001600160a01b031633146119615761195a60016047612d47565b905061137a565b600c8290556000611377565b600d5481565b60035461010090046001600160a01b031681565b6001600160a01b031660009081526014602052604090205490565b6000805460ff166119e7576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556119f9611d5e565b14611a44576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b50600f546000805460ff1916600117905590565b60006112178261379b565b60105481565b6006546001600160a01b03163314611ab4576040805162461bcd60e51b81526020600482015260096024820152683737ba2fb0b236b4b760b91b604482015290519081900360640190fd5b600d54158015611ac45750600e54155b611b04576040805162461bcd60e51b815260206004820152600c60248201526b185b1c9958591e57da5b9a5d60a21b604482015290519081900360640190fd5b600a84905583611b45576040805162461bcd60e51b8152602060048201526007602482015266746f6f5f6c6f7760c81b604482015290519081900360640190fd5b6000611b5087611701565b90508015611ba5576040805162461bcd60e51b815260206004820152601a60248201527f73657474696e67206d6f617274726f6c6c6572206661696c6564000000000000604482015290519081900360640190fd5b611bad61381c565b600d55670de0b6b3a7640000600e55611bc586613820565b90508015611c0f576040805162461bcd60e51b81526020600482015260126024820152711cd95d1d1a5b99c81254934819985a5b195960721b604482015290519081900360640190fd5b8351611c22906001906020870190615bb1565b508251611c36906002906020860190615bb1565b50506003805460ff90921660ff199283161790556000805490911660011790555050611388601255505061271060135550670de0b6b3a764000060055565048c27395000600455565b6002805460408051602060018416156101000260001901909316849004601f8101849004840282018401909252818152929183018282801561113f5780601f106111145761010080835404028352916020019161113f565b6000806000611ce584613985565b90925090506000826003811115611cf857fe5b14611837576040805162461bcd60e51b815260206004820152601a60248201527f626f72726f7742616c616e636553746f726564206661696c6564000000000000604482015290519081900360640190fd5b60035461010090046001600160a01b031690565b600080611d6961381c565b600d5490915080821415611d8257600092505050611408565b6000611d8c61354a565b600f54601054600e54600954604080516315f2405360e01b815260048101879052602481018690526044810185905290519596509394929391926000926001600160a01b03909216916315f24053916064808301926020929190829003018186803b158015611dfa57600080fd5b505afa158015611e0e573d6000803e3d6000fd5b505050506040513d6020811015611e2457600080fd5b5051600454909150811115611e77576040805162461bcd60e51b81526020600482015260146024820152730c4dee4e4deee40e4c2e8ca40e8dede40d0d2ced60631b604482015290519081900360640190fd5b600080611e848989613a38565b90925090506000826003811115611e9757fe5b14611ee9576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626c6f636b2064656c7461000000000000604482015290519081900360640190fd5b611ef1615b9e565b611ef9615c2f565b611f1160405180602001604052808781525084613a5b565b90945091506000846003811115611f2457fe5b14611f5357611f4160096006866003811115611f3c57fe5b613ac3565b9b505050505050505050505050611408565b611f5d82896134f7565b825293506000846003811115611f6f57fe5b14611f8757611f4160096001866003811115611f3c57fe5b8051611f939089613b29565b608083015293506000846003811115611fa857fe5b14611fc057611f4160096004866003811115611f3c57fe5b611fde6040518060200160405280600b5481525082600001516134f7565b602083015293506000846003811115611ff357fe5b1461200b57611f4160096005866003811115611f3c57fe5b6120296040518060200160405280600c5481525082602001516134f7565b60608301529350600084600381111561203e57fe5b1461205657611f4160096005866003811115611f3c57fe5b61206881602001518260600151613a38565b60408301529350600084600381111561207d57fe5b1461209557611f4160096005866003811115611f3c57fe5b6120a3816040015188613b29565b60a0830152935060008460038111156120b857fe5b146120d057611f4160096005866003811115611f3c57fe5b6120db828788613b4f565b60c0830152935060008460038111156120f057fe5b1461210857611f4160096003866003811115611f3c57fe5b600d8b905560c0810151600e556080810151600f5560a08101516010556060810151156122a85760085460408051631fa201e560e21b815290516000926001600160a01b031691637e880794916004808301926020929190829003018186803b15801561217457600080fd5b505afa158015612188573d6000803e3d6000fd5b505050506040513d602081101561219e57600080fd5b505160035460608401516040805163095ea7b360e01b81526001600160a01b03808616600483015260248201939093529051939450610100909204169163095ea7b3916044808201926020929091908290030181600087803b15801561220357600080fd5b505af1158015612217573d6000803e3d6000fd5b505050506040513d602081101561222d57600080fd5b50506003546060830151604080516327c817af60e11b81526101009093046001600160a01b03908116600485015260248401929092525190831691634f902f5e91604480830192600092919082900301818387803b15801561228e57600080fd5b505af11580156122a2573d6000803e3d6000fd5b50505050505b805160c0820151608080840151604080518e8152602081019590955284810193909352606084015290517f4dec04e750ca11537cabcd8a9eab06494de08da3735bc8871cd41250e190bc049281900390910190a160009b50505050505050505050505090565b6000805460ff16612353576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561236933338686613265565b1490505b6000805460ff1916600117905592915050565b600e5481565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156123d657600080fd5b505af11580156123ea573d6000803e3d6000fd5b505050505060006123fc833484613bab565b50905061243781604051806040016040528060168152602001751b1a5c5d5a59185d19509bdc9c9bddc819985a5b195960521b815250610e9d565b505050565b6009546000906001600160a01b031663b816881661245861354a565b600f54601054600b546040518563ffffffff1660e01b81526004018085815260200184815260200183815260200182815260200194505050505060206040518083038186803b1580156124aa57600080fd5b505afa1580156124be573d6000803e3d6000fd5b505050506040513d60208110156124d457600080fd5b5051905090565b6000805460ff16612520576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916905561253633858585613cdc565b90506000805460ff191660011790559392505050565b6006546000906001600160a01b0316331461256d576111d560016045612d47565b600780546001600160a01b038481166001600160a01b0319831681179093556040805191909216808252602082019390935281517fca4f2f25d0898edd99413412fb94012f9e54ec8142f9b093e7720646a95b16a9929181900390910190a16000611837565b6000805460ff16612618576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561262a611d5e565b14612675576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b61267d611392565b90506000805460ff1916600117905590565b6001600160a01b0381166000908152601460205260408120548190819081908180806126ba89613985565b9350905060008160038111156126cc57fe5b146126ea5760095b600080600097509750975097505050505061271d565b6126f2613164565b92509050600081600381111561270457fe5b146127105760096126d4565b5060009650919450925090505b9193509193565b600061121782613eb8565b6016602052600090815260409020805460019091015482565b600061121782613f37565b6001600160a01b03918216600090815260156020908152604080832093909416825291909152205490565b60008061278961381c565b600d54909150600061279961354a565b600f54601054600e54600954604080516315f2405360e01b815260048101879052602481018690526044810185905290519596509394929391926000926001600160a01b03909216916315f24053916064808301926020929190829003018186803b15801561280757600080fd5b505afa15801561281b573d6000803e3d6000fd5b505050506040513d602081101561283157600080fd5b5051600454909150811115612884576040805162461bcd60e51b81526020600482015260146024820152730c4dee4e4deee40e4c2e8ca40e8dede40d0d2ced60631b604482015290519081900360640190fd5b6000806128918989613a38565b909250905060008260038111156128a457fe5b146128f6576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626c6f636b2064656c7461000000000000604482015290519081900360640190fd5b6128fe615b9e565b600061291860405180602001604052808781525084613a5b565b9094509150600084600381111561292b57fe5b146129675760405162461bcd60e51b8152600401808060200182810382526023815260200180615d766023913960400191505060405180910390fd5b612972828788613b4f565b9094509050600084600381111561298557fe5b146129d7576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626f72726f77496e646578000000000000604482015290519081900360640190fd5b9a505050505050505050505090565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b158015612a3657600080fd5b505af1158015612a4a573d6000803e3d6000fd5b50505050506000612a5b8234613fb1565b5090506110b0816040518060400160405280601881526020017f7265706179426f72726f77426568616c66206661696c65640000000000000000815250610e9d565b6000611837838361405b565b6007546000906001600160a01b031633141580612ac4575033155b15612adc57612ad560016000612d47565b9050611408565b60068054600780546001600160a01b038082166001600160a01b031980861682179687905590921690925560408051938316808552949092166020840152815190927ff9ffabca9c8276e99321725bcb43fb076a6c66a54b7f21c4e8146d8519b417dc92908290030190a1600754604080516001600160a01b038085168252909216602083015280517fca4f2f25d0898edd99413412fb94012f9e54ec8142f9b093e7720646a95b16a99281900390910190a160005b9250505090565b600080612ba4611d5e565b90508015612bca57612bc2816010811115612bbb57fe5b6040612d47565b91505061121a565b61183783613820565b6009546001600160a01b031681565b6006546001600160a01b031681565b6009546000906001600160a01b03166315f24053612c0d61354a565b600f546010546040518463ffffffff1660e01b815260040180848152602001838152602001828152602001935050505060206040518083038186803b1580156124aa57600080fd5b6000805460ff16612c9a576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155612cac611d5e565b90508015612cca576118b3816010811115612cc357fe5b6046612d47565b6118c4836141b0565b6000600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b158015612d2557600080fd5b505af1158015612d39573d6000803e3d6000fd5b50505050506116453461424d565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa0836010811115612d7657fe5b836050811115612d8257fe5b604080519283526020830191909152600082820152519081900360600190a182601081111561183757fe5b60085460408051634ef4c3e160e01b81523060048201526001600160a01b03858116602483015260448201859052915160009384938493911691634ef4c3e19160648082019260209290919082900301818787803b158015612e0e57600080fd5b505af1158015612e22573d6000803e3d6000fd5b505050506040513d6020811015612e3857600080fd5b505190508015612e5b57612e4f6003601f83613ac3565b6000925092505061315d565b612e6361381c565b600d5414612e7757612e4f600a6022612d47565b612e7f615c6c565b612e87613164565b6040830181905260208301826003811115612e9e57fe5b6003811115612ea957fe5b9052506000905081602001516003811115612ec057fe5b14612ee957612edc6009602183602001516003811115611f3c57fe5b600093509350505061315d565b612ef386866142e1565b60c0820181905260408051602081018252908301518152612f14919061437d565b6060830181905260208301826003811115612f2b57fe5b6003811115612f3657fe5b9052506000905081602001516003811115612f4d57fe5b14612f88576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b612f986011548260600151613b29565b6080830181905260208301826003811115612faf57fe5b6003811115612fba57fe5b9052506000905081602001516003811115612fd157fe5b1461300c576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b6001600160a01b03861660009081526014602052604090205460608201516130349190613b29565b60a083018190526020830182600381111561304b57fe5b600381111561305657fe5b905250600090508160200151600381111561306d57fe5b146130a8576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b608081015160115560a08101516001600160a01b0387166000818152601460209081526040918290209390935560c084015160608086015183519485529484019190915282820193909352517f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f929181900390910190a1606081015160408051918252516001600160a01b038816913091600080516020615d998339815191529181900360200190a360c00151600093509150505b9250929050565b60115460009081908061317f575050600a546000915061320f565b600061318961354a565b90506000613195615b9e565b60006131a684600f54601054614394565b9350905060008160038111156131b857fe5b146131cd5795506000945061320f9350505050565b6131d783866143d2565b9250905060008160038111156131e957fe5b146131fe5795506000945061320f9350505050565b505160009550935061320f92505050565b9091565b604080516001600160a01b038416602482015260448082018490528251808303909101815260649091019091526020810180516001600160e01b031663a9059cbb60e01b179052612437908490614483565b600854604080516317b9b84b60e31b81523060048201526001600160a01b03868116602483015285811660448301526064820185905291516000938493169163bdcdc25891608480830192602092919082900301818787803b1580156132ca57600080fd5b505af11580156132de573d6000803e3d6000fd5b505050506040513d60208110156132f457600080fd5b5051905080156133135761330b6003604a83613ac3565b915050611633565b836001600160a01b0316856001600160a01b031614156133395761330b6002604b612d47565b6000856001600160a01b0316876001600160a01b0316141561335e5750600019613386565b506001600160a01b038086166000908152601560209081526040808320938a16835292905220545b6000806000806133968589613a38565b909450925060008460038111156133a957fe5b146133c7576133ba6009604b612d47565b9650505050505050611633565b6001600160a01b038a166000908152601460205260409020546133ea9089613a38565b909450915060008460038111156133fd57fe5b1461340e576133ba6009604c612d47565b6001600160a01b0389166000908152601460205260409020546134319089613b29565b9094509050600084600381111561344457fe5b14613455576133ba6009604d612d47565b6001600160a01b03808b16600090815260146020526040808220859055918b1681522081905560001985146134ad576001600160a01b03808b166000908152601560209081526040808320938f168352929052208390555b886001600160a01b03168a6001600160a01b0316600080516020615d998339815191528a6040518082815260200191505060405180910390a35060009a9950505050505050505050565b6000806000613504615b9e565b61350e8686613a5b565b9092509050600082600381111561352157fe5b14613532575091506000905061315d565b600061353d82614534565b9350935050509250929050565b600354604080516370a0823160e01b8152306004820152905160009261010090046001600160a01b031691839182916135d69185916370a08231916024808301926020929190829003018186803b1580156135a457600080fd5b505afa1580156135b8573d6000803e3d6000fd5b505050506040513d60208110156135ce57600080fd5b505134613a38565b909250905060008260038111156135e957fe5b14612b9257600080fd5b60008054819060ff1661363a576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561364c611d5e565b9050801561366a57610e6d81601081111561366357fe5b6036612d47565b610e83333386614543565b60065460009081906001600160a01b0316331461369857612bc260016031612d47565b6136a061381c565b600d54146136b457612bc2600a6033612d47565b826136bd61354a565b10156136cf57612bc2600e6032612d47565b6010548311156136e557612bc260026034612d47565b506010548281039081111561372d576040805162461bcd60e51b8152602060048201526009602482015268756e646572666c6f7760b81b604482015290519081900360640190fd5b6010819055600654613748906001600160a01b031684614902565b600654604080516001600160a01b03909216825260208201859052818101839052517f3bad0c59cf2f06e7314077049f48a93578cd16f5ef92329f1dab1420a99c177e9181900360600190a16000611837565b6000805460ff166137e0576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556137f2611d5e565b90508015613810576118b381601081111561380957fe5b6027612d47565b6118c4336000856149ab565b4390565b60065460009081906001600160a01b0316331461384357612bc260016042612d47565b61384b61381c565b600d541461385f57612bc2600a6041612d47565b600960009054906101000a90046001600160a01b03169050826001600160a01b0316632191f92a6040518163ffffffff1660e01b815260040160206040518083038186803b1580156138b057600080fd5b505afa1580156138c4573d6000803e3d6000fd5b505050506040513d60208110156138da57600080fd5b5051613922576040805162461bcd60e51b81526020600482015260126024820152711b9bdd17da5b9d195c995cdd17db5bd9195b60721b604482015290519081900360640190fd5b600980546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517fedffc32e068c7c95dfd4bdfd5c4d939a084d6b11c4199eac8436ed234d72f9269281900390910190a16000611837565b6001600160a01b0381166000908152601660205260408120805482918291829182916139bb576000809550955050505050613a33565b6139cb8160000154600e54614f4f565b909450925060008460038111156139de57fe5b146139f3578360009550955050505050613a33565b613a01838260010154614f8e565b90945091506000846003811115613a1457fe5b14613a29578360009550955050505050613a33565b5060009450925050505b915091565b600080838311613a4f57506000905081830361315d565b5060039050600061315d565b6000613a65615b9e565b600080613a76866000015186614f4f565b90925090506000826003811115613a8957fe5b14613aa85750604080516020810190915260008152909250905061315d565b60408051602081019091529081526000969095509350505050565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa0846010811115613af257fe5b846050811115613afe57fe5b604080519283526020830191909152818101859052519081900360600190a183601081111561163357fe5b600080838301848110613b415760009250905061315d565b60026000925092505061315d565b6000806000613b5c615b9e565b613b668787613a5b565b90925090506000826003811115613b7957fe5b14613b8a5750915060009050613ba3565b613b9c613b9682614534565b86613b29565b9350935050505b935093915050565b60008054819060ff16613bf2576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613c04611d5e565b90508015613c2e57613c22816010811115613c1b57fe5b600f612d47565b60009250925050613cc5565b836001600160a01b031663a6afed956040518163ffffffff1660e01b8152600401602060405180830381600087803b158015613c6957600080fd5b505af1158015613c7d573d6000803e3d6000fd5b505050506040513d6020811015613c9357600080fd5b505190508015613cb357613c22816010811115613cac57fe5b6010612d47565b613cbf33878787614fb9565b92509250505b6000805460ff191660011790559094909350915050565b6008546040805163d02f735160e01b81523060048201526001600160a01b038781166024830152868116604483015285811660648301526084820185905291516000938493169163d02f73519160a480830192602092919082900301818787803b158015613d4957600080fd5b505af1158015613d5d573d6000803e3d6000fd5b505050506040513d6020811015613d7357600080fd5b505190508015613d8a5761330b6003601b83613ac3565b846001600160a01b0316846001600160a01b03161415613db05761330b6006601c612d47565b6001600160a01b03841660009081526014602052604081205481908190613dd79087613a38565b90935091506000836003811115613dea57fe5b14613e0d57613e026009601a856003811115611f3c57fe5b945050505050611633565b6001600160a01b038816600090815260146020526040902054613e309087613b29565b90935090506000836003811115613e4357fe5b14613e5b57613e0260096019856003811115611f3c57fe5b6001600160a01b038088166000818152601460209081526040808320879055938c168083529184902085905583518a815293519193600080516020615d99833981519152929081900390910190a360009998505050505050505050565b6000805460ff16613efd576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613f0f611d5e565b90508015613f2d576118b3816010811115613f2657fe5b6008612d47565b6118c433846154b6565b6000805460ff16613f7c576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613f8e611d5e565b90508015613fa5576118b381601081111561380957fe5b6118c4338460006149ab565b60008054819060ff16613ff8576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561400a611d5e565b905080156140345761402881601081111561402157fe5b6035612d47565b60009250925050614045565b61403f338686614543565b92509250505b6000805460ff1916600117905590939092509050565b6000805460ff166140a0576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191690556008546040805163b72bb8ab60e01b815233600482015290516001600160a01b039092169163b72bb8ab91602480820192602092909190829003018186803b1580156140f557600080fd5b505afa158015614109573d6000803e3d6000fd5b505050506040513d602081101561411f57600080fd5b5051614167576040805162461bcd60e51b81526020600482015260126024820152717065726d697373696f6e5f6d697373696e6760701b604482015290519081900360640190fd5b6000614171611d5e565b9050801561419057614188816010811115613f2657fe5b91505061236d565b61419a84846154b6565b9150506000805460ff1916600117905592915050565b6006546000906001600160a01b031633146141d1576111d560016047612d47565b6141d961381c565b600d54146141ed576111d5600a6048612d47565b600554821115614203576111d560026049612d47565b600b805490839055604080518281526020810185905281517faaa68312e2ea9d50e16af5068410ab56e1a1fd06037b1a35664812c30f821460929181900390910190a16000611837565b6000805460ff16614292576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556142a4611d5e565b905080156142c2576118b38160108111156142bb57fe5b604e612d47565b6142cb8361574a565b509150506000805460ff19166001179055919050565b6000336001600160a01b03841614614332576040805162461bcd60e51b815260206004820152600f60248201526e0e6cadcc8cae440dad2e6dac2e8c6d608b1b604482015290519081900360640190fd5b813414614377576040805162461bcd60e51b815260206004820152600e60248201526d0ecc2d8eaca40dad2e6dac2e8c6d60931b604482015290519081900360640190fd5b50919050565b600080600061438a615b9e565b61350e868661581d565b6000806000806143a48787613b29565b909250905060008260038111156143b757fe5b146143c85750915060009050613ba3565b613b9c8186613a38565b60006143dc615b9e565b6000806143f186670de0b6b3a7640000614f4f565b9092509050600082600381111561440457fe5b146144235750604080516020810190915260008152909250905061315d565b6000806144308388614f8e565b9092509050600082600381111561444357fe5b14614466578160405180602001604052806000815250955095505050505061315d565b604080516020810190915290815260009890975095505050505050565b60606144d8826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b031661587c9092919063ffffffff16565b805190915015612437578080602001905160208110156144f757600080fd5b50516124375760405162461bcd60e51b815260040180806020018281038252602a815260200180615db9602a913960400191505060405180910390fd5b51670de0b6b3a7640000900490565b60085460408051631200453160e11b81523060048201526001600160a01b0386811660248301528581166044830152606482018590529151600093849384939116916324008a629160848082019260209290919082900301818787803b1580156145ac57600080fd5b505af11580156145c0573d6000803e3d6000fd5b505050506040513d60208110156145d657600080fd5b5051905080156145f9576145ed6003603883613ac3565b60009250925050613ba3565b61460161381c565b600d5414614615576145ed600a6039612d47565b61461d615caa565b6001600160a01b038616600090815260166020526040902060010154606082015261464786613985565b608083018190526020830182600381111561465e57fe5b600381111561466957fe5b905250600090508160200151600381111561468057fe5b146146a95761469c6009603783602001516003811115611f3c57fe5b6000935093505050613ba3565b60001985141561471f57326001600160a01b03871614614710576040805162461bcd60e51b815260206004820152601860248201527f737065636966792061207072656369736520616d6f756e740000000000000000604482015290519081900360640190fd5b60808101516040820152614727565b604081018590525b6147358782604001516142e1565b60e08201819052608082015161474a91613a38565b60a083018190526020830182600381111561476157fe5b600381111561476c57fe5b905250600090508160200151600381111561478357fe5b146147bf5760405162461bcd60e51b8152600401808060200182810382526021815260200180615d2f6021913960400191505060405180910390fd5b6147cf600f548260e00151613a38565b60c08301819052602083018260038111156147e657fe5b60038111156147f157fe5b905250600090508160200151600381111561480857fe5b1461485a576040805162461bcd60e51b815260206004820181905260248201527f544f54414c5f42414c414e43455f43414c43554c4154494f4e5f4641494c4544604482015290519081900360640190fd5b60a080820180516001600160a01b03808a16600081815260166020908152604091829020948555600e5460019095019490945560c0870151600f81905560e088015195518251948f16855294840192909252828101949094526060820192909252608081019190915290517f1a2a22cb034d26d1854bdc6666a5b91fe25efbbb5dcad3b0355478d6f5c362a1929181900390910190a160e00151600097909650945050505050565b600360019054906101000a90046001600160a01b03166001600160a01b0316632e1a7d4d826040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b15801561495d57600080fd5b505af1158015614971573d6000803e3d6000fd5b50506040516001600160a01b038516925083156108fc02915083906000818181858888f19350505050158015612437573d6000803e3d6000fd5b60008215806149b8575081155b614a09576040805162461bcd60e51b815260206004820152601860248201527f72656465656d46726573685f6d697373696e675f7a65726f0000000000000000604482015290519081900360640190fd5b614a11615c6c565b614a19613164565b6040830181905260208301826003811115614a3057fe5b6003811115614a3b57fe5b9052506000905081602001516003811115614a5257fe5b14614a7657614a6e6009602b83602001516003811115611f3c57fe5b915050611837565b8315614af7576060810184905260408051602081018252908201518152614a9d90856134f7565b6080830181905260208301826003811115614ab457fe5b6003811115614abf57fe5b9052506000905081602001516003811115614ad657fe5b14614af257614a6e6009602983602001516003811115611f3c57fe5b614b70565b614b13836040518060200160405280846040015181525061437d565b6060830181905260208301826003811115614b2a57fe5b6003811115614b3557fe5b9052506000905081602001516003811115614b4c57fe5b14614b6857614a6e6009602a83602001516003811115611f3c57fe5b608081018390525b60085460608201516040805163eabe7d9160e01b81523060048201526001600160a01b03898116602483015260448201939093529051600093929092169163eabe7d919160648082019260209290919082900301818787803b158015614bd557600080fd5b505af1158015614be9573d6000803e3d6000fd5b505050506040513d6020811015614bff57600080fd5b505190508015614c1f57614c166003602883613ac3565b92505050611837565b614c2761381c565b600d5414614c3b57614c16600a602c612d47565b614c4b6011548360600151613a38565b60a0840181905260208401826003811115614c6257fe5b6003811115614c6d57fe5b9052506000905082602001516003811115614c8457fe5b14614ca057614c166009602e84602001516003811115611f3c57fe5b6001600160a01b0386166000908152601460205260409020546060830151614cc89190613a38565b60c0840181905260208401826003811115614cdf57fe5b6003811115614cea57fe5b9052506000905082602001516003811115614d0157fe5b14614d1d57614c166009602d84602001516003811115611f3c57fe5b8160800151614d2a61354a565b1015614d3c57614c16600e602f612d47565b6000614d61614d538460c00151856040015161588b565b670de0b6b3a76400006158cd565b6008546040805163d1bf5e9b60e01b81523060048201526001600160a01b038b81166024830152915193945091169163d1bf5e9b91604480820192602092909190829003018186803b158015614db657600080fd5b505afa158015614dca573d6000803e3d6000fd5b505050506040513d6020811015614de057600080fd5b5051811015614dff57614df5600e602f612d47565b9350505050611837565b614e0d878460800151614902565b60a083015160115560c08301516001600160a01b038816600081815260146020908152604091829020939093556060860151815190815290513093600080516020615d99833981519152928290030190a37fe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a929878460800151856060015160405180846001600160a01b03168152602001838152602001828152602001935050505060405180910390a160085460808401516060850151604080516351dff98960e01b81523060048201526001600160a01b038c81166024830152604482019490945260648101929092525191909216916351dff98991608480830192600092919082900301818387803b158015614f2357600080fd5b505af1158015614f37573d6000803e3d6000fd5b5060009250614f44915050565b979650505050505050565b60008083614f625750600090508061315d565b83830283858281614f6f57fe5b0414614f835760026000925092505061315d565b60009250905061315d565b60008082614fa2575060019050600061315d565b6000838581614fad57fe5b04915091509250929050565b60085460408051632fe3f38f60e11b81523060048201526001600160a01b0384811660248301528781166044830152868116606483015260848201869052915160009384938493911691635fc7e71e9160a48082019260209290919082900301818787803b15801561502a57600080fd5b505af115801561503e573d6000803e3d6000fd5b505050506040513d602081101561505457600080fd5b5051905080156150775761506b6003601283613ac3565b600092509250506154ad565b61507f61381c565b600d54146150935761506b600a6016612d47565b61509b61381c565b846001600160a01b0316636c540baf6040518163ffffffff1660e01b815260040160206040518083038186803b1580156150d457600080fd5b505afa1580156150e8573d6000803e3d6000fd5b505050506040513d60208110156150fe57600080fd5b5051146151115761506b600a6011612d47565b866001600160a01b0316866001600160a01b031614156151375761506b60066017612d47565b846151485761506b60076015612d47565b60001985141561515e5761506b60076014612d47565b60008061516c898989614543565b9092509050811561519b5761518d82601081111561518657fe5b6018612d47565b6000945094505050506154ad565b60085460408051633299cac760e01b81523060048201526001600160a01b038981166024830152604482018590528b8116606483015282516000948594921692633299cac7926084808301939192829003018186803b1580156151fd57600080fd5b505afa158015615211573d6000803e3d6000fd5b505050506040513d604081101561522757600080fd5b50805160209091015190925090508115615288576040805162461bcd60e51b815260206004820152601d60248201527f43414c43554c4154455f414d4f554e545f5345495a455f4641494c4544000000604482015290519081900360640190fd5b80886001600160a01b03166370a082318c6040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b1580156152d657600080fd5b505afa1580156152ea573d6000803e3d6000fd5b505050506040513d602081101561530057600080fd5b50511015615340576040805162461bcd60e51b81526020600482015260086024820152670a89e9ebe9aaa86960c31b604482015290519081900360640190fd5b60006001600160a01b0389163014156153665761535f308d8d85613cdc565b90506153fd565b886001600160a01b031663b2a02ff18d8d856040518463ffffffff1660e01b815260040180846001600160a01b03168152602001836001600160a01b031681526020018281526020019350505050602060405180830381600087803b1580156153ce57600080fd5b505af11580156153e2573d6000803e3d6000fd5b505050506040513d60208110156153f857600080fd5b505190505b8015615447576040805162461bcd60e51b81526020600482015260146024820152731d1bdad95b881cd95a5e9d5c994819985a5b195960621b604482015290519081900360640190fd5b604080516001600160a01b03808f168252808e1660208301528183018790528b1660608201526080810184905290517f298637f684da70674f26509b10f07ec2fbc77a335ab1e7d6215a4b2484d8bb529181900360a00190a16000975092955050505050505b94509492505050565b6008546040805163368f515360e21b81523060048201526001600160a01b0385811660248301526044820185905291516000938493169163da3d454c91606480830192602092919082900301818787803b15801561551357600080fd5b505af1158015615527573d6000803e3d6000fd5b505050506040513d602081101561553d57600080fd5b50519050801561555c576155546003600e83613ac3565b9150506111ae565b61556461381c565b600d541461557757615554600a80612d47565b8261558061354a565b101561559257615554600e6009612d47565b61559a615cf0565b6155a385613985565b60208301819052828260038111156155b757fe5b60038111156155c257fe5b90525060009050815160038111156155d657fe5b146155fb576155f26009600783600001516003811115611f3c57fe5b925050506111ae565b615609816020015185613b29565b604083018190528282600381111561561d57fe5b600381111561562857fe5b905250600090508151600381111561563c57fe5b14615658576155f26009600c83600001516003811115611f3c57fe5b615664600f5485613b29565b606083018190528282600381111561567857fe5b600381111561568357fe5b905250600090508151600381111561569757fe5b146156b3576155f26009600b83600001516003811115611f3c57fe5b6156bd8585614902565b604080820180516001600160a01b03881660008181526016602090815290859020928355600e54600190930192909255606080860151600f81905593518551928352928201899052818501929092529081019190915290517f13ed6866d4e1ee6da46f845c46d7e54120883d75c5ea9a2dacc1c4ca8984ab809181900360800190a1600095945050505050565b60008060008061575861381c565b600d54146157775761576c600a604f612d47565b93509150613a339050565b61578133866142e1565b9050806010540191506010548210156157cc576040805162461bcd60e51b81526020600482015260086024820152676f766572666c6f7760c01b604482015290519081900360640190fd5b6010829055604080513381526020810183905280820184905290517fa91e67c5ea634cd43a12c5a482724b03de01e85ca68702a53d0c2f45cb7c1dc59181900360600190a160009350915050915091565b6000615827615b9e565b60008061583c670de0b6b3a764000087614f4f565b9092509050600082600381111561584f57fe5b1461586e5750604080516020810190915260008152909250905061315d565b61353d8186600001516143d2565b60606116338484600085615900565b600061183783836040518060400160405280601781526020017f6d756c7469706c69636174696f6e206f766572666c6f77000000000000000000815250615a51565b600061183783836040518060400160405280600e81526020016d646976696465206279207a65726f60901b815250615ad0565b6060824710156159415760405162461bcd60e51b8152600401808060200182810382526026815260200180615d506026913960400191505060405180910390fd5b61594a85615b32565b61599b576040805162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015290519081900360640190fd5b60006060866001600160a01b031685876040518082805190602001908083835b602083106159da5780518252601f1990920191602091820191016159bb565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d8060008114615a3c576040519150601f19603f3d011682016040523d82523d6000602084013e615a41565b606091505b5091509150614f44828286615b38565b6000831580615a5e575082155b15615a6b57506000611837565b83830283858281615a7857fe5b04148390615ac75760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315611071578181015183820152602001611059565b50949350505050565b60008183615b1f5760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315611071578181015183820152602001611059565b50828481615b2957fe5b04949350505050565b3b151590565b60608315615b47575081611837565b825115615b575782518084602001fd5b60405162461bcd60e51b8152602060048201818152845160248401528451859391928392604401919085019080838360008315611071578181015183820152602001611059565b6040518060200160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10615bf257805160ff1916838001178555615c1f565b82800160010185558215615c1f579182015b82811115615c1f578251825591602001919060010190615c04565b50615c2b929150615d19565b5090565b6040518060e00160405280600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b6040805160e0810190915280600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b6040805161010081019091528060008152602001600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b604080516080810190915280600081526020016000815260200160008152602001600081525090565b5b80821115615c2b5760008155600101615d1a56fe424f52524f575f42414c414e43455f43414c43554c4154494f4e5f4641494c4544416464726573733a20696e73756666696369656e742062616c616e636520666f722063616c6c636f756c64206e6f742063616c632073696d706c65496e746572657374466163746f72ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef5361666545524332303a204552433230206f7065726174696f6e20646964206e6f7420737563636565644d45726332303a3a7377656570546f6b656e3a2063616e206e6f7420737765657020756e6465726c79696e6720746f6b656ea26469706673582212207528264fcef720871cc095ec5c104a28ec38f1a448b2b9d58c1903d5270867fc64736f6c634300060c0033000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000077e7fa5b12b77a93e5ad7456ace11a7beb2894070000000000000000000000005b533c77d77a4f55169ab24540658302bf2b9107000000000000000000000000000000000000000000a56fa5b99019a5c800000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000800000000000000000000000061653649751a9c70b2607722c11cc385ce7208ed000000000000000000000000000000000000000000000000000000000000000b6d546f6b656e205745544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000056d57455448000000000000000000000000000000000000000000000000000000

Deployed Bytecode

0x60806040526004361061037a5760003560e01c8063852a12e3116101d1578063c37f68e211610102578063e9a1183d116100a0578063f851a4401161006f578063f851a44014610d9a578063f8f9da2814610daf578063fca7820b14610dc4578063fcb6414714610dee5761043e565b8063e9a1183d14610d04578063e9c714f214610d3d578063f2b3abbd14610d52578063f3fdb15a14610d855761043e565b8063db006a75116100dc578063db006a7514610c64578063dd62ed3e14610c8e578063dfa9f0fe14610cc9578063e597461914610cde5761043e565b8063c37f68e214610b95578063c5ebeaec14610bee578063d40e8f4a14610c185761043e565b8063a9059cbb1161016f578063ae9d70b011610149578063ae9d70b014610af5578063b2a02ff114610b0a578063b71d1a0c14610b4d578063bd6d894d14610b805761043e565b8063a9059cbb14610a79578063aa5af0fd14610ab2578063aae40a2a14610ac75761043e565b806395d89b41116101ab57806395d89b4114610a0757806395dd919314610a1c5780639816f47314610a4f578063a6afed9514610a645761043e565b8063852a12e3146108695780638f840ddd1461089357806394d6c574146108a85761043e565b80633af9e669116102ab57806360ae7af8116102495780636c540baf116102235780636c540baf146107f75780636f307dc31461080c57806370a082311461082157806373acee98146108545761043e565b806360ae7af8146107a3578063699cd5e2146107b85780636b58b0b2146107cd5761043e565b80634e4d9fea116102855780634e4d9fea1461072957806353869f0a14610731578063551a312614610746578063601a0bf1146107795761043e565b80633af9e669146106cc5780633b1d21a2146106ff57806347bd3718146107145761043e565b806318160ddd116103185780631be19560116102f25780631be19560146105fa57806323b872dd1461062d5780632678224714610670578063313ce567146106a15761043e565b806318160ddd146105bb578063182df0f5146105d057806318f68ef9146105e55761043e565b80630f5e888f116103545780630f5e888f146105415780631249c58b1461056b578063173b99041461057357806317bfdfbc146105885761043e565b806305920a391461044357806306fdde031461046a578063095ea7b3146104f45761043e565b3661043e5760035461010090046001600160a01b0316331461043c57600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156103e657600080fd5b505af11580156103fa573d6000803e3d6000fd5b5050505050600061040a34610df6565b50905061043a816040518060400160405280600b81526020016a1b5a5b9d0819985a5b195960aa1b815250610e9d565b505b005b600080fd5b34801561044f57600080fd5b506104586110b4565b60408051918252519081900360200190f35b34801561047657600080fd5b5061047f6110ba565b6040805160208082528351818301528351919283929083019185019080838360005b838110156104b95781810151838201526020016104a1565b50505050905090810190601f1680156104e65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561050057600080fd5b5061052d6004803603604081101561051757600080fd5b506001600160a01b038135169060200135611147565b604080519115158252519081900360200190f35b34801561054d57600080fd5b506104586004803603602081101561056457600080fd5b50356111b4565b61043c61121f565b34801561057f57600080fd5b506104586112c6565b34801561059457600080fd5b50610458600480360360208110156105ab57600080fd5b50356001600160a01b03166112cc565b3480156105c757600080fd5b5061045861138c565b3480156105dc57600080fd5b50610458611392565b3480156105f157600080fd5b5061045861140b565b34801561060657600080fd5b5061043c6004803603602081101561061d57600080fd5b50356001600160a01b0316611411565b34801561063957600080fd5b5061052d6004803603606081101561065057600080fd5b506001600160a01b038135811691602081013590911690604001356114fb565b34801561067c57600080fd5b5061068561156d565b604080516001600160a01b039092168252519081900360200190f35b3480156106ad57600080fd5b506106b661157c565b6040805160ff9092168252519081900360200190f35b3480156106d857600080fd5b50610458600480360360208110156106ef57600080fd5b50356001600160a01b0316611585565b34801561070b57600080fd5b5061045861163b565b34801561072057600080fd5b5061045861164a565b61043c611650565b34801561073d57600080fd5b506104586116fb565b34801561075257600080fd5b506104586004803603602081101561076957600080fd5b50356001600160a01b0316611701565b34801561078557600080fd5b506104586004803603602081101561079c57600080fd5b503561183e565b3480156107af57600080fd5b506106856118d9565b3480156107c457600080fd5b5061052d6118e8565b3480156107d957600080fd5b50610458600480360360208110156107f057600080fd5b50356118ed565b34801561080357600080fd5b5061045861196d565b34801561081857600080fd5b50610685611973565b34801561082d57600080fd5b506104586004803603602081101561084457600080fd5b50356001600160a01b0316611987565b34801561086057600080fd5b506104586119a2565b34801561087557600080fd5b506104586004803603602081101561088c57600080fd5b5035611a58565b34801561089f57600080fd5b50610458611a63565b3480156108b457600080fd5b5061043c600480360360c08110156108cb57600080fd5b6001600160a01b0382358116926020810135909116916040820135919081019060808101606082013564010000000081111561090657600080fd5b82018360208201111561091857600080fd5b8035906020019184600183028401116401000000008311171561093a57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929594936020810193503591505064010000000081111561098d57600080fd5b82018360208201111561099f57600080fd5b803590602001918460018302840111640100000000831117156109c157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295505050903560ff169150611a699050565b348015610a1357600080fd5b5061047f611c7f565b348015610a2857600080fd5b5061045860048036036020811015610a3f57600080fd5b50356001600160a01b0316611cd7565b348015610a5b57600080fd5b50610685611d4a565b348015610a7057600080fd5b50610458611d5e565b348015610a8557600080fd5b5061052d60048036036040811015610a9c57600080fd5b506001600160a01b03813516906020013561230e565b348015610abe57600080fd5b50610458612380565b61043c60048036036040811015610add57600080fd5b506001600160a01b0381358116916020013516612386565b348015610b0157600080fd5b5061045861243c565b348015610b1657600080fd5b5061045860048036036060811015610b2d57600080fd5b506001600160a01b038135811691602081013590911690604001356124db565b348015610b5957600080fd5b5061045860048036036020811015610b7057600080fd5b50356001600160a01b031661254c565b348015610b8c57600080fd5b506104586125d3565b348015610ba157600080fd5b50610bc860048036036020811015610bb857600080fd5b50356001600160a01b031661268f565b604080519485526020850193909352838301919091526060830152519081900360800190f35b348015610bfa57600080fd5b5061045860048036036020811015610c1157600080fd5b5035612724565b348015610c2457600080fd5b50610c4b60048036036020811015610c3b57600080fd5b50356001600160a01b031661272f565b6040805192835260208301919091528051918290030190f35b348015610c7057600080fd5b5061045860048036036020811015610c8757600080fd5b5035612748565b348015610c9a57600080fd5b5061045860048036036040811015610cb157600080fd5b506001600160a01b0381358116916020013516612753565b348015610cd557600080fd5b5061045861277e565b61043c60048036036020811015610cf457600080fd5b50356001600160a01b03166129e6565b348015610d1057600080fd5b5061045860048036036040811015610d2757600080fd5b506001600160a01b038135169060200135612a9d565b348015610d4957600080fd5b50610458612aa9565b348015610d5e57600080fd5b5061045860048036036020811015610d7557600080fd5b50356001600160a01b0316612b99565b348015610d9157600080fd5b50610685612bd3565b348015610da657600080fd5b50610685612be2565b348015610dbb57600080fd5b50610458612bf1565b348015610dd057600080fd5b5061045860048036036020811015610de757600080fd5b5035612c55565b610458612cd3565b60008054819060ff16610e3d576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155610e4f611d5e565b90508015610e7957610e6d816010811115610e6657fe5b601e612d47565b60009250925050610e89565b610e833385612dad565b92509250505b6000805460ff191660011790559092909150565b81610ea7576110b0565b6060815160050167ffffffffffffffff81118015610ec457600080fd5b506040519080825280601f01601f191660200182016040528015610eef576020820181803683370190505b50905060005b8251811015610f4057828181518110610f0a57fe5b602001015160f81c60f81b828281518110610f2157fe5b60200101906001600160f81b031916908160001a905350600101610ef5565b8151600160fd1b90839083908110610f5457fe5b60200101906001600160f81b031916908160001a905350602860f81b828260010181518110610f7f57fe5b60200101906001600160f81b031916908160001a905350600a840460300160f81b828260020181518110610faf57fe5b60200101906001600160f81b031916908160001a905350600a840660300160f81b828260030181518110610fdf57fe5b60200101906001600160f81b031916908160001a905350602960f81b82826004018151811061100a57fe5b60200101906001600160f81b031916908160001a9053508184156110ac5760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015611071578181015183820152602001611059565b50505050905090810190601f16801561109e5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5050505b5050565b60135481565b60018054604080516020600284861615610100026000190190941693909304601f8101849004840282018401909252818152929183018282801561113f5780601f106111145761010080835404028352916020019161113f565b820191906000526020600020905b81548152906001019060200180831161112257829003601f168201915b505050505081565b3360008181526015602090815260408083206001600160a01b03871680855290835281842086905581518681529151939493909284927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929081900390910190a360019150505b92915050565b6006546000906001600160a01b031633146111dc576111d560016042612d47565b905061121a565b60128290556040805183815290517f1461d8f734be2d09431c43882077b36addb0d247b48d4f308691cd188d1fc7249181900360200190a160005b90505b919050565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b15801561126f57600080fd5b505af1158015611283573d6000803e3d6000fd5b5050505050600061129334610df6565b5090506112c3816040518060400160405280600b81526020016a1b5a5b9d0819985a5b195960aa1b815250610e9d565b50565b600b5481565b6000805460ff16611311576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155611323611d5e565b1461136e576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b61137782611cd7565b90505b6000805460ff19166001179055919050565b60115481565b600080600061139f613164565b909250905060008260038111156113b257fe5b14611404576040805162461bcd60e51b815260206004820152601960248201527f65786368616e67655261746553746f726564206661696c656400000000000000604482015290519081900360640190fd5b9150505b90565b600c5481565b6003546001600160a01b038281166101009092041614156114635760405162461bcd60e51b8152600401808060200182810382526032815260200180615de36032913960400191505060405180910390fd5b6000816001600160a01b03166370a08231306040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b1580156114b257600080fd5b505afa1580156114c6573d6000803e3d6000fd5b505050506040513d60208110156114dc57600080fd5b50516006549091506110b0906001600160a01b03848116911683613213565b6000805460ff16611540576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561155633868686613265565b1490506000805460ff191660011790559392505050565b6007546001600160a01b031681565b60035460ff1681565b600061158f615b9e565b60405180602001604052806115a26125d3565b90526001600160a01b0384166000908152601460205260408120549192509081906115ce9084906134f7565b909250905060008260038111156115e157fe5b14611633576040805162461bcd60e51b815260206004820152601a60248201527f62616c616e63655f63616c63756c6174696f6e5f6661696c6564000000000000604482015290519081900360640190fd5b949350505050565b600061164561354a565b905090565b600f5481565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156116a057600080fd5b505af11580156116b4573d6000803e3d6000fd5b505050505060006116c4346135f3565b5090506112c381604051806040016040528060128152602001711c995c185e509bdc9c9bddc819985a5b195960721b815250610e9d565b60125481565b6006546000906001600160a01b03163314611722576111d56001603f612d47565b6008546040805163039459d960e11b815290516001600160a01b0392831692851691630728b3b2916004808301926020929190829003018186803b15801561176957600080fd5b505afa15801561177d573d6000803e3d6000fd5b505050506040513d602081101561179357600080fd5b50516117d8576040805162461bcd60e51b815260206004820152600f60248201526e3737ba2fb6b7b0b93a3937b63632b960891b604482015290519081900360640190fd5b600880546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517f21791446553f95da6d7e0507ffbbddb0583d1998fbea8f6eb12000b94b88eff79281900390910190a160005b9392505050565b6000805460ff16611883576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155611895611d5e565b905080156118bb576118b38160108111156118ac57fe5b6030612d47565b91505061137a565b6118c483613675565b9150506000805460ff19166001179055919050565b6008546001600160a01b031681565b600181565b6000805460ff16611932576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191690556006546001600160a01b031633146119615761195a60016047612d47565b905061137a565b600c8290556000611377565b600d5481565b60035461010090046001600160a01b031681565b6001600160a01b031660009081526014602052604090205490565b6000805460ff166119e7576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556119f9611d5e565b14611a44576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b50600f546000805460ff1916600117905590565b60006112178261379b565b60105481565b6006546001600160a01b03163314611ab4576040805162461bcd60e51b81526020600482015260096024820152683737ba2fb0b236b4b760b91b604482015290519081900360640190fd5b600d54158015611ac45750600e54155b611b04576040805162461bcd60e51b815260206004820152600c60248201526b185b1c9958591e57da5b9a5d60a21b604482015290519081900360640190fd5b600a84905583611b45576040805162461bcd60e51b8152602060048201526007602482015266746f6f5f6c6f7760c81b604482015290519081900360640190fd5b6000611b5087611701565b90508015611ba5576040805162461bcd60e51b815260206004820152601a60248201527f73657474696e67206d6f617274726f6c6c6572206661696c6564000000000000604482015290519081900360640190fd5b611bad61381c565b600d55670de0b6b3a7640000600e55611bc586613820565b90508015611c0f576040805162461bcd60e51b81526020600482015260126024820152711cd95d1d1a5b99c81254934819985a5b195960721b604482015290519081900360640190fd5b8351611c22906001906020870190615bb1565b508251611c36906002906020860190615bb1565b50506003805460ff90921660ff199283161790556000805490911660011790555050611388601255505061271060135550670de0b6b3a764000060055565048c27395000600455565b6002805460408051602060018416156101000260001901909316849004601f8101849004840282018401909252818152929183018282801561113f5780601f106111145761010080835404028352916020019161113f565b6000806000611ce584613985565b90925090506000826003811115611cf857fe5b14611837576040805162461bcd60e51b815260206004820152601a60248201527f626f72726f7742616c616e636553746f726564206661696c6564000000000000604482015290519081900360640190fd5b60035461010090046001600160a01b031690565b600080611d6961381c565b600d5490915080821415611d8257600092505050611408565b6000611d8c61354a565b600f54601054600e54600954604080516315f2405360e01b815260048101879052602481018690526044810185905290519596509394929391926000926001600160a01b03909216916315f24053916064808301926020929190829003018186803b158015611dfa57600080fd5b505afa158015611e0e573d6000803e3d6000fd5b505050506040513d6020811015611e2457600080fd5b5051600454909150811115611e77576040805162461bcd60e51b81526020600482015260146024820152730c4dee4e4deee40e4c2e8ca40e8dede40d0d2ced60631b604482015290519081900360640190fd5b600080611e848989613a38565b90925090506000826003811115611e9757fe5b14611ee9576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626c6f636b2064656c7461000000000000604482015290519081900360640190fd5b611ef1615b9e565b611ef9615c2f565b611f1160405180602001604052808781525084613a5b565b90945091506000846003811115611f2457fe5b14611f5357611f4160096006866003811115611f3c57fe5b613ac3565b9b505050505050505050505050611408565b611f5d82896134f7565b825293506000846003811115611f6f57fe5b14611f8757611f4160096001866003811115611f3c57fe5b8051611f939089613b29565b608083015293506000846003811115611fa857fe5b14611fc057611f4160096004866003811115611f3c57fe5b611fde6040518060200160405280600b5481525082600001516134f7565b602083015293506000846003811115611ff357fe5b1461200b57611f4160096005866003811115611f3c57fe5b6120296040518060200160405280600c5481525082602001516134f7565b60608301529350600084600381111561203e57fe5b1461205657611f4160096005866003811115611f3c57fe5b61206881602001518260600151613a38565b60408301529350600084600381111561207d57fe5b1461209557611f4160096005866003811115611f3c57fe5b6120a3816040015188613b29565b60a0830152935060008460038111156120b857fe5b146120d057611f4160096005866003811115611f3c57fe5b6120db828788613b4f565b60c0830152935060008460038111156120f057fe5b1461210857611f4160096003866003811115611f3c57fe5b600d8b905560c0810151600e556080810151600f5560a08101516010556060810151156122a85760085460408051631fa201e560e21b815290516000926001600160a01b031691637e880794916004808301926020929190829003018186803b15801561217457600080fd5b505afa158015612188573d6000803e3d6000fd5b505050506040513d602081101561219e57600080fd5b505160035460608401516040805163095ea7b360e01b81526001600160a01b03808616600483015260248201939093529051939450610100909204169163095ea7b3916044808201926020929091908290030181600087803b15801561220357600080fd5b505af1158015612217573d6000803e3d6000fd5b505050506040513d602081101561222d57600080fd5b50506003546060830151604080516327c817af60e11b81526101009093046001600160a01b03908116600485015260248401929092525190831691634f902f5e91604480830192600092919082900301818387803b15801561228e57600080fd5b505af11580156122a2573d6000803e3d6000fd5b50505050505b805160c0820151608080840151604080518e8152602081019590955284810193909352606084015290517f4dec04e750ca11537cabcd8a9eab06494de08da3735bc8871cd41250e190bc049281900390910190a160009b50505050505050505050505090565b6000805460ff16612353576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561236933338686613265565b1490505b6000805460ff1916600117905592915050565b600e5481565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156123d657600080fd5b505af11580156123ea573d6000803e3d6000fd5b505050505060006123fc833484613bab565b50905061243781604051806040016040528060168152602001751b1a5c5d5a59185d19509bdc9c9bddc819985a5b195960521b815250610e9d565b505050565b6009546000906001600160a01b031663b816881661245861354a565b600f54601054600b546040518563ffffffff1660e01b81526004018085815260200184815260200183815260200182815260200194505050505060206040518083038186803b1580156124aa57600080fd5b505afa1580156124be573d6000803e3d6000fd5b505050506040513d60208110156124d457600080fd5b5051905090565b6000805460ff16612520576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916905561253633858585613cdc565b90506000805460ff191660011790559392505050565b6006546000906001600160a01b0316331461256d576111d560016045612d47565b600780546001600160a01b038481166001600160a01b0319831681179093556040805191909216808252602082019390935281517fca4f2f25d0898edd99413412fb94012f9e54ec8142f9b093e7720646a95b16a9929181900390910190a16000611837565b6000805460ff16612618576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561262a611d5e565b14612675576040805162461bcd60e51b81526020600482015260166024820152751858d8dc9d59481a5b9d195c995cdd0819985a5b195960521b604482015290519081900360640190fd5b61267d611392565b90506000805460ff1916600117905590565b6001600160a01b0381166000908152601460205260408120548190819081908180806126ba89613985565b9350905060008160038111156126cc57fe5b146126ea5760095b600080600097509750975097505050505061271d565b6126f2613164565b92509050600081600381111561270457fe5b146127105760096126d4565b5060009650919450925090505b9193509193565b600061121782613eb8565b6016602052600090815260409020805460019091015482565b600061121782613f37565b6001600160a01b03918216600090815260156020908152604080832093909416825291909152205490565b60008061278961381c565b600d54909150600061279961354a565b600f54601054600e54600954604080516315f2405360e01b815260048101879052602481018690526044810185905290519596509394929391926000926001600160a01b03909216916315f24053916064808301926020929190829003018186803b15801561280757600080fd5b505afa15801561281b573d6000803e3d6000fd5b505050506040513d602081101561283157600080fd5b5051600454909150811115612884576040805162461bcd60e51b81526020600482015260146024820152730c4dee4e4deee40e4c2e8ca40e8dede40d0d2ced60631b604482015290519081900360640190fd5b6000806128918989613a38565b909250905060008260038111156128a457fe5b146128f6576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626c6f636b2064656c7461000000000000604482015290519081900360640190fd5b6128fe615b9e565b600061291860405180602001604052808781525084613a5b565b9094509150600084600381111561292b57fe5b146129675760405162461bcd60e51b8152600401808060200182810382526023815260200180615d766023913960400191505060405180910390fd5b612972828788613b4f565b9094509050600084600381111561298557fe5b146129d7576040805162461bcd60e51b815260206004820152601a60248201527f636f756c64206e6f742063616c6320626f72726f77496e646578000000000000604482015290519081900360640190fd5b9a505050505050505050505090565b600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b158015612a3657600080fd5b505af1158015612a4a573d6000803e3d6000fd5b50505050506000612a5b8234613fb1565b5090506110b0816040518060400160405280601881526020017f7265706179426f72726f77426568616c66206661696c65640000000000000000815250610e9d565b6000611837838361405b565b6007546000906001600160a01b031633141580612ac4575033155b15612adc57612ad560016000612d47565b9050611408565b60068054600780546001600160a01b038082166001600160a01b031980861682179687905590921690925560408051938316808552949092166020840152815190927ff9ffabca9c8276e99321725bcb43fb076a6c66a54b7f21c4e8146d8519b417dc92908290030190a1600754604080516001600160a01b038085168252909216602083015280517fca4f2f25d0898edd99413412fb94012f9e54ec8142f9b093e7720646a95b16a99281900390910190a160005b9250505090565b600080612ba4611d5e565b90508015612bca57612bc2816010811115612bbb57fe5b6040612d47565b91505061121a565b61183783613820565b6009546001600160a01b031681565b6006546001600160a01b031681565b6009546000906001600160a01b03166315f24053612c0d61354a565b600f546010546040518463ffffffff1660e01b815260040180848152602001838152602001828152602001935050505060206040518083038186803b1580156124aa57600080fd5b6000805460ff16612c9a576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155612cac611d5e565b90508015612cca576118b3816010811115612cc357fe5b6046612d47565b6118c4836141b0565b6000600360019054906101000a90046001600160a01b03166001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004016000604051808303818588803b158015612d2557600080fd5b505af1158015612d39573d6000803e3d6000fd5b50505050506116453461424d565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa0836010811115612d7657fe5b836050811115612d8257fe5b604080519283526020830191909152600082820152519081900360600190a182601081111561183757fe5b60085460408051634ef4c3e160e01b81523060048201526001600160a01b03858116602483015260448201859052915160009384938493911691634ef4c3e19160648082019260209290919082900301818787803b158015612e0e57600080fd5b505af1158015612e22573d6000803e3d6000fd5b505050506040513d6020811015612e3857600080fd5b505190508015612e5b57612e4f6003601f83613ac3565b6000925092505061315d565b612e6361381c565b600d5414612e7757612e4f600a6022612d47565b612e7f615c6c565b612e87613164565b6040830181905260208301826003811115612e9e57fe5b6003811115612ea957fe5b9052506000905081602001516003811115612ec057fe5b14612ee957612edc6009602183602001516003811115611f3c57fe5b600093509350505061315d565b612ef386866142e1565b60c0820181905260408051602081018252908301518152612f14919061437d565b6060830181905260208301826003811115612f2b57fe5b6003811115612f3657fe5b9052506000905081602001516003811115612f4d57fe5b14612f88576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b612f986011548260600151613b29565b6080830181905260208301826003811115612faf57fe5b6003811115612fba57fe5b9052506000905081602001516003811115612fd157fe5b1461300c576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b6001600160a01b03861660009081526014602052604090205460608201516130349190613b29565b60a083018190526020830182600381111561304b57fe5b600381111561305657fe5b905250600090508160200151600381111561306d57fe5b146130a8576040805162461bcd60e51b81526020600482015260066024820152654d494e545f4560d01b604482015290519081900360640190fd5b608081015160115560a08101516001600160a01b0387166000818152601460209081526040918290209390935560c084015160608086015183519485529484019190915282820193909352517f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f929181900390910190a1606081015160408051918252516001600160a01b038816913091600080516020615d998339815191529181900360200190a360c00151600093509150505b9250929050565b60115460009081908061317f575050600a546000915061320f565b600061318961354a565b90506000613195615b9e565b60006131a684600f54601054614394565b9350905060008160038111156131b857fe5b146131cd5795506000945061320f9350505050565b6131d783866143d2565b9250905060008160038111156131e957fe5b146131fe5795506000945061320f9350505050565b505160009550935061320f92505050565b9091565b604080516001600160a01b038416602482015260448082018490528251808303909101815260649091019091526020810180516001600160e01b031663a9059cbb60e01b179052612437908490614483565b600854604080516317b9b84b60e31b81523060048201526001600160a01b03868116602483015285811660448301526064820185905291516000938493169163bdcdc25891608480830192602092919082900301818787803b1580156132ca57600080fd5b505af11580156132de573d6000803e3d6000fd5b505050506040513d60208110156132f457600080fd5b5051905080156133135761330b6003604a83613ac3565b915050611633565b836001600160a01b0316856001600160a01b031614156133395761330b6002604b612d47565b6000856001600160a01b0316876001600160a01b0316141561335e5750600019613386565b506001600160a01b038086166000908152601560209081526040808320938a16835292905220545b6000806000806133968589613a38565b909450925060008460038111156133a957fe5b146133c7576133ba6009604b612d47565b9650505050505050611633565b6001600160a01b038a166000908152601460205260409020546133ea9089613a38565b909450915060008460038111156133fd57fe5b1461340e576133ba6009604c612d47565b6001600160a01b0389166000908152601460205260409020546134319089613b29565b9094509050600084600381111561344457fe5b14613455576133ba6009604d612d47565b6001600160a01b03808b16600090815260146020526040808220859055918b1681522081905560001985146134ad576001600160a01b03808b166000908152601560209081526040808320938f168352929052208390555b886001600160a01b03168a6001600160a01b0316600080516020615d998339815191528a6040518082815260200191505060405180910390a35060009a9950505050505050505050565b6000806000613504615b9e565b61350e8686613a5b565b9092509050600082600381111561352157fe5b14613532575091506000905061315d565b600061353d82614534565b9350935050509250929050565b600354604080516370a0823160e01b8152306004820152905160009261010090046001600160a01b031691839182916135d69185916370a08231916024808301926020929190829003018186803b1580156135a457600080fd5b505afa1580156135b8573d6000803e3d6000fd5b505050506040513d60208110156135ce57600080fd5b505134613a38565b909250905060008260038111156135e957fe5b14612b9257600080fd5b60008054819060ff1661363a576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561364c611d5e565b9050801561366a57610e6d81601081111561366357fe5b6036612d47565b610e83333386614543565b60065460009081906001600160a01b0316331461369857612bc260016031612d47565b6136a061381c565b600d54146136b457612bc2600a6033612d47565b826136bd61354a565b10156136cf57612bc2600e6032612d47565b6010548311156136e557612bc260026034612d47565b506010548281039081111561372d576040805162461bcd60e51b8152602060048201526009602482015268756e646572666c6f7760b81b604482015290519081900360640190fd5b6010819055600654613748906001600160a01b031684614902565b600654604080516001600160a01b03909216825260208201859052818101839052517f3bad0c59cf2f06e7314077049f48a93578cd16f5ef92329f1dab1420a99c177e9181900360600190a16000611837565b6000805460ff166137e0576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556137f2611d5e565b90508015613810576118b381601081111561380957fe5b6027612d47565b6118c4336000856149ab565b4390565b60065460009081906001600160a01b0316331461384357612bc260016042612d47565b61384b61381c565b600d541461385f57612bc2600a6041612d47565b600960009054906101000a90046001600160a01b03169050826001600160a01b0316632191f92a6040518163ffffffff1660e01b815260040160206040518083038186803b1580156138b057600080fd5b505afa1580156138c4573d6000803e3d6000fd5b505050506040513d60208110156138da57600080fd5b5051613922576040805162461bcd60e51b81526020600482015260126024820152711b9bdd17da5b9d195c995cdd17db5bd9195b60721b604482015290519081900360640190fd5b600980546001600160a01b0319166001600160a01b03858116918217909255604080519284168352602083019190915280517fedffc32e068c7c95dfd4bdfd5c4d939a084d6b11c4199eac8436ed234d72f9269281900390910190a16000611837565b6001600160a01b0381166000908152601660205260408120805482918291829182916139bb576000809550955050505050613a33565b6139cb8160000154600e54614f4f565b909450925060008460038111156139de57fe5b146139f3578360009550955050505050613a33565b613a01838260010154614f8e565b90945091506000846003811115613a1457fe5b14613a29578360009550955050505050613a33565b5060009450925050505b915091565b600080838311613a4f57506000905081830361315d565b5060039050600061315d565b6000613a65615b9e565b600080613a76866000015186614f4f565b90925090506000826003811115613a8957fe5b14613aa85750604080516020810190915260008152909250905061315d565b60408051602081019091529081526000969095509350505050565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa0846010811115613af257fe5b846050811115613afe57fe5b604080519283526020830191909152818101859052519081900360600190a183601081111561163357fe5b600080838301848110613b415760009250905061315d565b60026000925092505061315d565b6000806000613b5c615b9e565b613b668787613a5b565b90925090506000826003811115613b7957fe5b14613b8a5750915060009050613ba3565b613b9c613b9682614534565b86613b29565b9350935050505b935093915050565b60008054819060ff16613bf2576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613c04611d5e565b90508015613c2e57613c22816010811115613c1b57fe5b600f612d47565b60009250925050613cc5565b836001600160a01b031663a6afed956040518163ffffffff1660e01b8152600401602060405180830381600087803b158015613c6957600080fd5b505af1158015613c7d573d6000803e3d6000fd5b505050506040513d6020811015613c9357600080fd5b505190508015613cb357613c22816010811115613cac57fe5b6010612d47565b613cbf33878787614fb9565b92509250505b6000805460ff191660011790559094909350915050565b6008546040805163d02f735160e01b81523060048201526001600160a01b038781166024830152868116604483015285811660648301526084820185905291516000938493169163d02f73519160a480830192602092919082900301818787803b158015613d4957600080fd5b505af1158015613d5d573d6000803e3d6000fd5b505050506040513d6020811015613d7357600080fd5b505190508015613d8a5761330b6003601b83613ac3565b846001600160a01b0316846001600160a01b03161415613db05761330b6006601c612d47565b6001600160a01b03841660009081526014602052604081205481908190613dd79087613a38565b90935091506000836003811115613dea57fe5b14613e0d57613e026009601a856003811115611f3c57fe5b945050505050611633565b6001600160a01b038816600090815260146020526040902054613e309087613b29565b90935090506000836003811115613e4357fe5b14613e5b57613e0260096019856003811115611f3c57fe5b6001600160a01b038088166000818152601460209081526040808320879055938c168083529184902085905583518a815293519193600080516020615d99833981519152929081900390910190a360009998505050505050505050565b6000805460ff16613efd576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613f0f611d5e565b90508015613f2d576118b3816010811115613f2657fe5b6008612d47565b6118c433846154b6565b6000805460ff16613f7c576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff19168155613f8e611d5e565b90508015613fa5576118b381601081111561380957fe5b6118c4338460006149ab565b60008054819060ff16613ff8576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff1916815561400a611d5e565b905080156140345761402881601081111561402157fe5b6035612d47565b60009250925050614045565b61403f338686614543565b92509250505b6000805460ff1916600117905590939092509050565b6000805460ff166140a0576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191690556008546040805163b72bb8ab60e01b815233600482015290516001600160a01b039092169163b72bb8ab91602480820192602092909190829003018186803b1580156140f557600080fd5b505afa158015614109573d6000803e3d6000fd5b505050506040513d602081101561411f57600080fd5b5051614167576040805162461bcd60e51b81526020600482015260126024820152717065726d697373696f6e5f6d697373696e6760701b604482015290519081900360640190fd5b6000614171611d5e565b9050801561419057614188816010811115613f2657fe5b91505061236d565b61419a84846154b6565b9150506000805460ff1916600117905592915050565b6006546000906001600160a01b031633146141d1576111d560016047612d47565b6141d961381c565b600d54146141ed576111d5600a6048612d47565b600554821115614203576111d560026049612d47565b600b805490839055604080518281526020810185905281517faaa68312e2ea9d50e16af5068410ab56e1a1fd06037b1a35664812c30f821460929181900390910190a16000611837565b6000805460ff16614292576040805162461bcd60e51b815260206004820152600a6024820152691c994b595b9d195c995960b21b604482015290519081900360640190fd5b6000805460ff191681556142a4611d5e565b905080156142c2576118b38160108111156142bb57fe5b604e612d47565b6142cb8361574a565b509150506000805460ff19166001179055919050565b6000336001600160a01b03841614614332576040805162461bcd60e51b815260206004820152600f60248201526e0e6cadcc8cae440dad2e6dac2e8c6d608b1b604482015290519081900360640190fd5b813414614377576040805162461bcd60e51b815260206004820152600e60248201526d0ecc2d8eaca40dad2e6dac2e8c6d60931b604482015290519081900360640190fd5b50919050565b600080600061438a615b9e565b61350e868661581d565b6000806000806143a48787613b29565b909250905060008260038111156143b757fe5b146143c85750915060009050613ba3565b613b9c8186613a38565b60006143dc615b9e565b6000806143f186670de0b6b3a7640000614f4f565b9092509050600082600381111561440457fe5b146144235750604080516020810190915260008152909250905061315d565b6000806144308388614f8e565b9092509050600082600381111561444357fe5b14614466578160405180602001604052806000815250955095505050505061315d565b604080516020810190915290815260009890975095505050505050565b60606144d8826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b031661587c9092919063ffffffff16565b805190915015612437578080602001905160208110156144f757600080fd5b50516124375760405162461bcd60e51b815260040180806020018281038252602a815260200180615db9602a913960400191505060405180910390fd5b51670de0b6b3a7640000900490565b60085460408051631200453160e11b81523060048201526001600160a01b0386811660248301528581166044830152606482018590529151600093849384939116916324008a629160848082019260209290919082900301818787803b1580156145ac57600080fd5b505af11580156145c0573d6000803e3d6000fd5b505050506040513d60208110156145d657600080fd5b5051905080156145f9576145ed6003603883613ac3565b60009250925050613ba3565b61460161381c565b600d5414614615576145ed600a6039612d47565b61461d615caa565b6001600160a01b038616600090815260166020526040902060010154606082015261464786613985565b608083018190526020830182600381111561465e57fe5b600381111561466957fe5b905250600090508160200151600381111561468057fe5b146146a95761469c6009603783602001516003811115611f3c57fe5b6000935093505050613ba3565b60001985141561471f57326001600160a01b03871614614710576040805162461bcd60e51b815260206004820152601860248201527f737065636966792061207072656369736520616d6f756e740000000000000000604482015290519081900360640190fd5b60808101516040820152614727565b604081018590525b6147358782604001516142e1565b60e08201819052608082015161474a91613a38565b60a083018190526020830182600381111561476157fe5b600381111561476c57fe5b905250600090508160200151600381111561478357fe5b146147bf5760405162461bcd60e51b8152600401808060200182810382526021815260200180615d2f6021913960400191505060405180910390fd5b6147cf600f548260e00151613a38565b60c08301819052602083018260038111156147e657fe5b60038111156147f157fe5b905250600090508160200151600381111561480857fe5b1461485a576040805162461bcd60e51b815260206004820181905260248201527f544f54414c5f42414c414e43455f43414c43554c4154494f4e5f4641494c4544604482015290519081900360640190fd5b60a080820180516001600160a01b03808a16600081815260166020908152604091829020948555600e5460019095019490945560c0870151600f81905560e088015195518251948f16855294840192909252828101949094526060820192909252608081019190915290517f1a2a22cb034d26d1854bdc6666a5b91fe25efbbb5dcad3b0355478d6f5c362a1929181900390910190a160e00151600097909650945050505050565b600360019054906101000a90046001600160a01b03166001600160a01b0316632e1a7d4d826040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b15801561495d57600080fd5b505af1158015614971573d6000803e3d6000fd5b50506040516001600160a01b038516925083156108fc02915083906000818181858888f19350505050158015612437573d6000803e3d6000fd5b60008215806149b8575081155b614a09576040805162461bcd60e51b815260206004820152601860248201527f72656465656d46726573685f6d697373696e675f7a65726f0000000000000000604482015290519081900360640190fd5b614a11615c6c565b614a19613164565b6040830181905260208301826003811115614a3057fe5b6003811115614a3b57fe5b9052506000905081602001516003811115614a5257fe5b14614a7657614a6e6009602b83602001516003811115611f3c57fe5b915050611837565b8315614af7576060810184905260408051602081018252908201518152614a9d90856134f7565b6080830181905260208301826003811115614ab457fe5b6003811115614abf57fe5b9052506000905081602001516003811115614ad657fe5b14614af257614a6e6009602983602001516003811115611f3c57fe5b614b70565b614b13836040518060200160405280846040015181525061437d565b6060830181905260208301826003811115614b2a57fe5b6003811115614b3557fe5b9052506000905081602001516003811115614b4c57fe5b14614b6857614a6e6009602a83602001516003811115611f3c57fe5b608081018390525b60085460608201516040805163eabe7d9160e01b81523060048201526001600160a01b03898116602483015260448201939093529051600093929092169163eabe7d919160648082019260209290919082900301818787803b158015614bd557600080fd5b505af1158015614be9573d6000803e3d6000fd5b505050506040513d6020811015614bff57600080fd5b505190508015614c1f57614c166003602883613ac3565b92505050611837565b614c2761381c565b600d5414614c3b57614c16600a602c612d47565b614c4b6011548360600151613a38565b60a0840181905260208401826003811115614c6257fe5b6003811115614c6d57fe5b9052506000905082602001516003811115614c8457fe5b14614ca057614c166009602e84602001516003811115611f3c57fe5b6001600160a01b0386166000908152601460205260409020546060830151614cc89190613a38565b60c0840181905260208401826003811115614cdf57fe5b6003811115614cea57fe5b9052506000905082602001516003811115614d0157fe5b14614d1d57614c166009602d84602001516003811115611f3c57fe5b8160800151614d2a61354a565b1015614d3c57614c16600e602f612d47565b6000614d61614d538460c00151856040015161588b565b670de0b6b3a76400006158cd565b6008546040805163d1bf5e9b60e01b81523060048201526001600160a01b038b81166024830152915193945091169163d1bf5e9b91604480820192602092909190829003018186803b158015614db657600080fd5b505afa158015614dca573d6000803e3d6000fd5b505050506040513d6020811015614de057600080fd5b5051811015614dff57614df5600e602f612d47565b9350505050611837565b614e0d878460800151614902565b60a083015160115560c08301516001600160a01b038816600081815260146020908152604091829020939093556060860151815190815290513093600080516020615d99833981519152928290030190a37fe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a929878460800151856060015160405180846001600160a01b03168152602001838152602001828152602001935050505060405180910390a160085460808401516060850151604080516351dff98960e01b81523060048201526001600160a01b038c81166024830152604482019490945260648101929092525191909216916351dff98991608480830192600092919082900301818387803b158015614f2357600080fd5b505af1158015614f37573d6000803e3d6000fd5b5060009250614f44915050565b979650505050505050565b60008083614f625750600090508061315d565b83830283858281614f6f57fe5b0414614f835760026000925092505061315d565b60009250905061315d565b60008082614fa2575060019050600061315d565b6000838581614fad57fe5b04915091509250929050565b60085460408051632fe3f38f60e11b81523060048201526001600160a01b0384811660248301528781166044830152868116606483015260848201869052915160009384938493911691635fc7e71e9160a48082019260209290919082900301818787803b15801561502a57600080fd5b505af115801561503e573d6000803e3d6000fd5b505050506040513d602081101561505457600080fd5b5051905080156150775761506b6003601283613ac3565b600092509250506154ad565b61507f61381c565b600d54146150935761506b600a6016612d47565b61509b61381c565b846001600160a01b0316636c540baf6040518163ffffffff1660e01b815260040160206040518083038186803b1580156150d457600080fd5b505afa1580156150e8573d6000803e3d6000fd5b505050506040513d60208110156150fe57600080fd5b5051146151115761506b600a6011612d47565b866001600160a01b0316866001600160a01b031614156151375761506b60066017612d47565b846151485761506b60076015612d47565b60001985141561515e5761506b60076014612d47565b60008061516c898989614543565b9092509050811561519b5761518d82601081111561518657fe5b6018612d47565b6000945094505050506154ad565b60085460408051633299cac760e01b81523060048201526001600160a01b038981166024830152604482018590528b8116606483015282516000948594921692633299cac7926084808301939192829003018186803b1580156151fd57600080fd5b505afa158015615211573d6000803e3d6000fd5b505050506040513d604081101561522757600080fd5b50805160209091015190925090508115615288576040805162461bcd60e51b815260206004820152601d60248201527f43414c43554c4154455f414d4f554e545f5345495a455f4641494c4544000000604482015290519081900360640190fd5b80886001600160a01b03166370a082318c6040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b1580156152d657600080fd5b505afa1580156152ea573d6000803e3d6000fd5b505050506040513d602081101561530057600080fd5b50511015615340576040805162461bcd60e51b81526020600482015260086024820152670a89e9ebe9aaa86960c31b604482015290519081900360640190fd5b60006001600160a01b0389163014156153665761535f308d8d85613cdc565b90506153fd565b886001600160a01b031663b2a02ff18d8d856040518463ffffffff1660e01b815260040180846001600160a01b03168152602001836001600160a01b031681526020018281526020019350505050602060405180830381600087803b1580156153ce57600080fd5b505af11580156153e2573d6000803e3d6000fd5b505050506040513d60208110156153f857600080fd5b505190505b8015615447576040805162461bcd60e51b81526020600482015260146024820152731d1bdad95b881cd95a5e9d5c994819985a5b195960621b604482015290519081900360640190fd5b604080516001600160a01b03808f168252808e1660208301528183018790528b1660608201526080810184905290517f298637f684da70674f26509b10f07ec2fbc77a335ab1e7d6215a4b2484d8bb529181900360a00190a16000975092955050505050505b94509492505050565b6008546040805163368f515360e21b81523060048201526001600160a01b0385811660248301526044820185905291516000938493169163da3d454c91606480830192602092919082900301818787803b15801561551357600080fd5b505af1158015615527573d6000803e3d6000fd5b505050506040513d602081101561553d57600080fd5b50519050801561555c576155546003600e83613ac3565b9150506111ae565b61556461381c565b600d541461557757615554600a80612d47565b8261558061354a565b101561559257615554600e6009612d47565b61559a615cf0565b6155a385613985565b60208301819052828260038111156155b757fe5b60038111156155c257fe5b90525060009050815160038111156155d657fe5b146155fb576155f26009600783600001516003811115611f3c57fe5b925050506111ae565b615609816020015185613b29565b604083018190528282600381111561561d57fe5b600381111561562857fe5b905250600090508151600381111561563c57fe5b14615658576155f26009600c83600001516003811115611f3c57fe5b615664600f5485613b29565b606083018190528282600381111561567857fe5b600381111561568357fe5b905250600090508151600381111561569757fe5b146156b3576155f26009600b83600001516003811115611f3c57fe5b6156bd8585614902565b604080820180516001600160a01b03881660008181526016602090815290859020928355600e54600190930192909255606080860151600f81905593518551928352928201899052818501929092529081019190915290517f13ed6866d4e1ee6da46f845c46d7e54120883d75c5ea9a2dacc1c4ca8984ab809181900360800190a1600095945050505050565b60008060008061575861381c565b600d54146157775761576c600a604f612d47565b93509150613a339050565b61578133866142e1565b9050806010540191506010548210156157cc576040805162461bcd60e51b81526020600482015260086024820152676f766572666c6f7760c01b604482015290519081900360640190fd5b6010829055604080513381526020810183905280820184905290517fa91e67c5ea634cd43a12c5a482724b03de01e85ca68702a53d0c2f45cb7c1dc59181900360600190a160009350915050915091565b6000615827615b9e565b60008061583c670de0b6b3a764000087614f4f565b9092509050600082600381111561584f57fe5b1461586e5750604080516020810190915260008152909250905061315d565b61353d8186600001516143d2565b60606116338484600085615900565b600061183783836040518060400160405280601781526020017f6d756c7469706c69636174696f6e206f766572666c6f77000000000000000000815250615a51565b600061183783836040518060400160405280600e81526020016d646976696465206279207a65726f60901b815250615ad0565b6060824710156159415760405162461bcd60e51b8152600401808060200182810382526026815260200180615d506026913960400191505060405180910390fd5b61594a85615b32565b61599b576040805162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015290519081900360640190fd5b60006060866001600160a01b031685876040518082805190602001908083835b602083106159da5780518252601f1990920191602091820191016159bb565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d8060008114615a3c576040519150601f19603f3d011682016040523d82523d6000602084013e615a41565b606091505b5091509150614f44828286615b38565b6000831580615a5e575082155b15615a6b57506000611837565b83830283858281615a7857fe5b04148390615ac75760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315611071578181015183820152602001611059565b50949350505050565b60008183615b1f5760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315611071578181015183820152602001611059565b50828481615b2957fe5b04949350505050565b3b151590565b60608315615b47575081611837565b825115615b575782518084602001fd5b60405162461bcd60e51b8152602060048201818152845160248401528451859391928392604401919085019080838360008315611071578181015183820152602001611059565b6040518060200160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10615bf257805160ff1916838001178555615c1f565b82800160010185558215615c1f579182015b82811115615c1f578251825591602001919060010190615c04565b50615c2b929150615d19565b5090565b6040518060e00160405280600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b6040805160e0810190915280600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b6040805161010081019091528060008152602001600081526020016000815260200160008152602001600081526020016000815260200160008152602001600081525090565b604080516080810190915280600081526020016000815260200160008152602001600081525090565b5b80821115615c2b5760008155600101615d1a56fe424f52524f575f42414c414e43455f43414c43554c4154494f4e5f4641494c4544416464726573733a20696e73756666696369656e742062616c616e636520666f722063616c6c636f756c64206e6f742063616c632073696d706c65496e746572657374466163746f72ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef5361666545524332303a204552433230206f7065726174696f6e20646964206e6f7420737563636565644d45726332303a3a7377656570546f6b656e3a2063616e206e6f7420737765657020756e6465726c79696e6720746f6b656ea26469706673582212207528264fcef720871cc095ec5c104a28ec38f1a448b2b9d58c1903d5270867fc64736f6c634300060c0033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000077e7fa5b12b77a93e5ad7456ace11a7beb2894070000000000000000000000005b533c77d77a4f55169ab24540658302bf2b9107000000000000000000000000000000000000000000a56fa5b99019a5c800000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000800000000000000000000000061653649751a9c70b2607722c11cc385ce7208ed000000000000000000000000000000000000000000000000000000000000000b6d546f6b656e205745544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000056d57455448000000000000000000000000000000000000000000000000000000

-----Decoded View---------------
Arg [0] : underlying_ (address): 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
Arg [1] : moartroller_ (address): 0x77E7FA5B12B77A93E5Ad7456Ace11A7beB289407
Arg [2] : interestRateModel_ (address): 0x5b533c77D77A4F55169AB24540658302Bf2b9107
Arg [3] : initialExchangeRateMantissa_ (uint256): 200000000000000000000000000
Arg [4] : name_ (string): mToken WETH
Arg [5] : symbol_ (string): mWETH
Arg [6] : decimals_ (uint8): 8
Arg [7] : admin_ (address): 0x61653649751A9C70b2607722c11cc385CE7208ed

-----Encoded View---------------
12 Constructor Arguments found :
Arg [0] : 000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
Arg [1] : 00000000000000000000000077e7fa5b12b77a93e5ad7456ace11a7beb289407
Arg [2] : 0000000000000000000000005b533c77d77a4f55169ab24540658302bf2b9107
Arg [3] : 000000000000000000000000000000000000000000a56fa5b99019a5c8000000
Arg [4] : 0000000000000000000000000000000000000000000000000000000000000100
Arg [5] : 0000000000000000000000000000000000000000000000000000000000000140
Arg [6] : 0000000000000000000000000000000000000000000000000000000000000008
Arg [7] : 00000000000000000000000061653649751a9c70b2607722c11cc385ce7208ed
Arg [8] : 000000000000000000000000000000000000000000000000000000000000000b
Arg [9] : 6d546f6b656e2057455448000000000000000000000000000000000000000000
Arg [10] : 0000000000000000000000000000000000000000000000000000000000000005
Arg [11] : 6d57455448000000000000000000000000000000000000000000000000000000


Loading...
Loading
Loading...
Loading
[ Download: CSV Export  ]
[ Download: CSV Export  ]

A token is a representation of an on-chain or off-chain asset. The token page shows information such as price, total supply, holders, transfers and social links. Learn more about this page in our Knowledge Base.