Overview
APE Balance
0 APE
APE Value
$0.00More Info
Private Name Tags
ContractCreator
Latest 1 from a total of 1 transactions
Transaction Hash |
Method
|
Block
|
From
|
To
|
|||||
---|---|---|---|---|---|---|---|---|---|
_become | 452804 | 58 days ago | IN | 0 APE | 0.00135578 |
Loading...
Loading
Contract Name:
Comptroller
Compiler Version
v0.8.19+commit.7dd6d404
Optimization Enabled:
Yes with 200 runs
Other Settings:
paris EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./MToken.sol"; import "./ErrorReporter.sol"; import "./oracles/PriceOracle.sol"; import "./ComptrollerInterface.sol"; import "./ComptrollerStorage.sol"; import "./Unitroller.sol"; /** * @title Moonwell's Comptroller Contract * @author Moonwell */ contract Comptroller is ComptrollerV2Storage, ComptrollerInterface, ComptrollerErrorReporter, ExponentialNoError { /// @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 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 ActionPaused(MToken mToken, string action, bool pauseState); /// @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 supply cap for a mToken is changed event NewSupplyCap(MToken indexed mToken, uint newSupplyCap); /// @notice Emitted when supply cap guardian is changed event NewSupplyCapGuardian( address oldSupplyCapGuardian, address newSupplyCapGuardian ); /// @notice Emitted when reward distributor is changed event NewRewardDistributor( MultiRewardDistributor oldRewardDistributor, MultiRewardDistributor newRewardDistributor ); // 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 constructor() { admin = msg.sender; } /*** 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 mintAmount; if (!markets[mToken].isListed) { return uint(Error.MARKET_NOT_LISTED); } uint supplyCap = supplyCaps[mToken]; // Supply cap of 0 corresponds to unlimited supplying if (supplyCap != 0) { uint totalCash = MToken(mToken).getCash(); uint totalBorrows = MToken(mToken).totalBorrows(); uint totalReserves = MToken(mToken).totalReserves(); // totalSupplies = totalCash + totalBorrows - totalReserves uint totalSupplies = sub_( add_(totalCash, totalBorrows), totalReserves ); uint nextTotalSupplies = add_(totalSupplies, mintAmount); require(nextTotalSupplies < supplyCap, "market supply cap reached"); } // Keep the flywheel moving updateAndDistributeSupplierRewardsForToken(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 updateAndDistributeSupplierRewardsForToken(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 pure 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 addToMarketErr = addToMarketInternal( MToken(msg.sender), borrower ); if (addToMarketErr != Error.NO_ERROR) { return uint(addToMarketErr); } // 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 updateAndDistributeBorrowerRewardsForToken(mToken, borrower); 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 updateAndDistributeBorrowerRewardsForToken(mToken, borrower); 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 view 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).comptroller() != MToken(mTokenBorrowed).comptroller() ) { return uint(Error.COMPTROLLER_MISMATCH); } // Keep the flywheel moving // Note: We don't update borrower indices here because as part of liquidations // repayBorrowFresh is called, which in turn calls `borrowAllow`, which updates // the liquidated borrower's indices. updateAndDistributeSupplierRewardsForToken(mTokenCollateral, borrower); updateAndDistributeSupplierRewardsForToken( 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 updateAndDistributeSupplierRewardsForToken(mToken, src); updateAndDistributeSupplierRewardsForToken(mToken, dst); return uint(Error.NO_ERROR); } /*** Liquidity/Liquidation Calculations ***/ /** * @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; Exp collateralFactor; Exp exchangeRate; Exp oraclePrice; Exp tokensToDenom; } /** * @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(address(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(address(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]; // 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 = Exp({mantissa: vars.oraclePriceMantissa}); // Pre-compute a conversion factor from tokens -> glmr (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 ); // 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 ); } } // 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 Calculate number of tokens of collateral asset to seize given an underlying amount * @dev Used in liquidation (called in mToken.liquidateBorrowFresh) * @param mTokenBorrowed The address of the borrowed mToken * @param mTokenCollateral The address of the collateral mToken * @param actualRepayAmount The amount of mTokenBorrowed underlying to convert into mTokenCollateral tokens * @return (errorCode, number of mTokenCollateral tokens to be seized in a liquidation) */ function liquidateCalculateSeizeTokens( address mTokenBorrowed, address mTokenCollateral, uint actualRepayAmount ) external view override returns (uint, uint) { /* Read oracle prices for borrowed and collateral markets */ uint priceBorrowedMantissa = oracle.getUnderlyingPrice( MToken(mTokenBorrowed) ); uint priceCollateralMantissa = oracle.getUnderlyingPrice( MToken(mTokenCollateral) ); if (priceBorrowedMantissa == 0 || priceCollateralMantissa == 0) { return (uint(Error.PRICE_ERROR), 0); } /* * Get the exchange rate and calculate the number of collateral tokens to seize: * seizeAmount = actualRepayAmount * liquidationIncentive * priceBorrowed / priceCollateral * seizeTokens = seizeAmount / exchangeRate * = actualRepayAmount * (liquidationIncentive * priceBorrowed) / (priceCollateral * exchangeRate) */ uint exchangeRateMantissa = MToken(mTokenCollateral) .exchangeRateStored(); // Note: reverts on error uint seizeTokens; Exp memory numerator; Exp memory denominator; Exp memory ratio; numerator = mul_( Exp({mantissa: liquidationIncentiveMantissa}), Exp({mantissa: priceBorrowedMantissa}) ); denominator = mul_( Exp({mantissa: priceCollateralMantissa}), Exp({mantissa: exchangeRateMantissa}) ); ratio = div_(numerator, denominator); seizeTokens = mul_ScalarTruncate(ratio, actualRepayAmount); return (uint(Error.NO_ERROR), seizeTokens); } /*** Admin Functions ***/ /** * @notice Sets a new price oracle for the comptroller * @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 comptroller PriceOracle oldOracle = oracle; // Set comptroller's oracle to newOracle oracle = newOracle; // Emit NewPriceOracle(oldOracle, newOracle) emit NewPriceOracle(oldOracle, newOracle); 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 ); } 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); } /** * @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 ); } require(mToken.isMToken(), "Must be an MToken"); // Sanity check to make sure its really a MToken Market storage newMarket = markets[address(mToken)]; newMarket.isListed = true; newMarket.collateralFactorMantissa = 0; _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 Set the given supply caps for the given mToken markets. Supplying that brings total supplies to or above supply cap will revert. * @dev Admin or supplyCapGuardian function to set the supply caps. A supply cap of 0 corresponds to unlimited supplying. * @param mTokens The addresses of the markets (tokens) to change the supply caps for * @param newSupplyCaps The new supply cap values in underlying to be set. A value of 0 corresponds to unlimited supplying. */ function _setMarketSupplyCaps( MToken[] calldata mTokens, uint[] calldata newSupplyCaps ) external { require( msg.sender == admin || msg.sender == supplyCapGuardian, "only admin or supply cap guardian can set supply caps" ); uint numMarkets = mTokens.length; uint numSupplyCaps = newSupplyCaps.length; require( numMarkets != 0 && numMarkets == numSupplyCaps, "invalid input" ); for (uint i = 0; i < numMarkets; i++) { supplyCaps[address(mTokens[i])] = newSupplyCaps[i]; emit NewSupplyCap(mTokens[i], newSupplyCaps[i]); } } /** * @notice Admin function to change the Supply Cap Guardian * @param newSupplyCapGuardian The address of the new Supply Cap Guardian */ function _setSupplyCapGuardian(address newSupplyCapGuardian) external { require(msg.sender == admin, "only admin can set supply cap guardian"); // Save current value for inclusion in log address oldSupplyCapGuardian = supplyCapGuardian; // Store supplyCapGuardian with value newSupplyCapGuardian supplyCapGuardian = newSupplyCapGuardian; // Emit NewSupplyCapGuardian(OldSupplyCapGuardian, NewSupplyCapGuardian) emit NewSupplyCapGuardian(oldSupplyCapGuardian, newSupplyCapGuardian); } /** * @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); } /** * @notice Admin function to change the Reward Distributor * @param newRewardDistributor The address of the new Reward Distributor */ function _setRewardDistributor( MultiRewardDistributor newRewardDistributor ) public { require(msg.sender == admin, "Unauthorized"); MultiRewardDistributor oldRewardDistributor = rewardDistributor; rewardDistributor = newRewardDistributor; emit NewRewardDistributor(oldRewardDistributor, newRewardDistributor); } 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 ActionPaused(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 ActionPaused(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; } function _become(Unitroller unitroller) public { require( msg.sender == unitroller.admin(), "only unitroller admin can change brains" ); require( unitroller._acceptImplementation() == 0, "change not authorized" ); } /** * @notice Sweep ERC-20 tokens from the comptroller to the admin * @param _tokenAddress The address of the token to transfer * @param _amount The amount of tokens to sweep, uint.max means everything */ function _rescueFunds(address _tokenAddress, uint _amount) external { require(msg.sender == admin, "Unauthorized"); IERC20 token = IERC20(_tokenAddress); // Similar to mTokens, if this is uint.max that means "transfer everything" if (_amount == type(uint).max) { token.transfer(admin, token.balanceOf(address(this))); } else { token.transfer(admin, _amount); } } /*** WELL Distribution ***/ /** * @notice Call out to the reward distributor to update its supply index and this user's index too * @param mToken The market to synchronize indexes for * @param supplier The supplier to whom rewards are going */ function updateAndDistributeSupplierRewardsForToken( address mToken, address supplier ) internal { if (address(rewardDistributor) != address(0)) { rewardDistributor.updateMarketSupplyIndexAndDisburseSupplierRewards( MToken(mToken), supplier, false ); } } /** * @notice Call out to the reward distributor to update its borrow index and this user's index too * @param mToken The market to synchronize indexes for * @param borrower The borrower to whom rewards are going */ function updateAndDistributeBorrowerRewardsForToken( address mToken, address borrower ) internal { if (address(rewardDistributor) != address(0)) { rewardDistributor.updateMarketBorrowIndexAndDisburseBorrowerRewards( MToken(mToken), borrower, false ); } } /** * @notice Claim all the WELL accrued by holder in all markets */ function claimReward() public { claimReward(msg.sender, allMarkets); } /** * @notice Claim all the rewards accrued by specified holder in all markets * @param holder The address to claim rewards for */ function claimReward(address holder) public { claimReward(holder, allMarkets); } /** * @notice Claim all the rewards accrued by holder in the specified markets * @param holder The address to claim rewards for * @param mTokens The list of markets to claim rewards in */ function claimReward(address holder, MToken[] memory mTokens) public { address[] memory holders = new address[](1); holders[0] = holder; claimReward(holders, mTokens, true, true); } /** * @notice Claim all rewards for a specified group of users, tokens, and market sides * @param holders The addresses to claim for * @param mTokens The list of markets to claim in * @param borrowers Whether or not to claim earned by borrowing * @param suppliers Whether or not to claim earned by supplying */ function claimReward( address[] memory holders, MToken[] memory mTokens, bool borrowers, bool suppliers ) public { require( address(rewardDistributor) != address(0), "No reward distributor configured!" ); for (uint i = 0; i < mTokens.length; i++) { // Safety check that the supplied mTokens are active/listed MToken mToken = mTokens[i]; require(markets[address(mToken)].isListed, "market must be listed"); // Disburse supply side if (suppliers == true) { rewardDistributor.updateMarketSupplyIndex(mToken); for ( uint holderIndex = 0; holderIndex < holders.length; holderIndex++ ) { rewardDistributor.disburseSupplierRewards( mToken, holders[holderIndex], true ); } } // Disburse borrow side if (borrowers == true) { rewardDistributor.updateMarketBorrowIndex(mToken); for ( uint holderIndex = 0; holderIndex < holders.length; holderIndex++ ) { rewardDistributor.disburseBorrowerRewards( mToken, holders[holderIndex], true ); } } } } /** * @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 getBlockTimestamp() public view returns (uint) { return block.timestamp; } /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and make it call a * `private` function that does the actual work. */ modifier nonReentrant() { // On the first call to nonReentrant, _notEntered will be true require(_locked != 1, "ReentrancyGuard: reentrant call"); // Any calls to nonReentrant after this point will fail _locked = 1; _; // By storing the original value once again, a refund is triggered (see // https://eips.ethereum.org/EIPS/eip-2200) _locked = 0; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/Initializable.sol) pragma solidity ^0.8.2; import "../../utils/Address.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 proxied contracts do not make use of 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. * * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in * case an upgrade adds a module that needs to be initialized. * * For example: * * [.hljs-theme-light.nopadding] * ```solidity * contract MyToken is ERC20Upgradeable { * function initialize() initializer public { * __ERC20_init("MyToken", "MTK"); * } * } * * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable { * function initializeV2() reinitializer(2) public { * __ERC20Permit_init("MyToken"); * } * } * ``` * * 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 {ERC1967Proxy-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. * * [CAUTION] * ==== * Avoid leaving a contract uninitialized. * * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed: * * [.hljs-theme-light.nopadding] * ``` * /// @custom:oz-upgrades-unsafe-allow constructor * constructor() { * _disableInitializers(); * } * ``` * ==== */ abstract contract Initializable { /** * @dev Indicates that the contract has been initialized. * @custom:oz-retyped-from bool */ uint8 private _initialized; /** * @dev Indicates that the contract is in the process of being initialized. */ bool private _initializing; /** * @dev Triggered when the contract has been initialized or reinitialized. */ event Initialized(uint8 version); /** * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope, * `onlyInitializing` functions can be used to initialize parent contracts. * * Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a * constructor. * * Emits an {Initialized} event. */ modifier initializer() { bool isTopLevelCall = !_initializing; require( (isTopLevelCall && _initialized < 1) || (!Address.isContract(address(this)) && _initialized == 1), "Initializable: contract is already initialized" ); _initialized = 1; if (isTopLevelCall) { _initializing = true; } _; if (isTopLevelCall) { _initializing = false; emit Initialized(1); } } /** * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be * used to initialize parent contracts. * * A reinitializer may be used after the original initialization step. This is essential to configure modules that * are added through upgrades and that require initialization. * * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer` * cannot be nested. If one is invoked in the context of another, execution will revert. * * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in * a contract, executing them in the right order is up to the developer or operator. * * WARNING: setting the version to 255 will prevent any future reinitialization. * * Emits an {Initialized} event. */ modifier reinitializer(uint8 version) { require(!_initializing && _initialized < version, "Initializable: contract is already initialized"); _initialized = version; _initializing = true; _; _initializing = false; emit Initialized(version); } /** * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the * {initializer} and {reinitializer} modifiers, directly or indirectly. */ modifier onlyInitializing() { require(_initializing, "Initializable: contract is not initializing"); _; } /** * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call. * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized * to any version. It is recommended to use this to lock implementation contracts that are designed to be called * through proxies. * * Emits an {Initialized} event the first time it is successfully executed. */ function _disableInitializers() internal virtual { require(!_initializing, "Initializable: contract is initializing"); if (_initialized != type(uint8).max) { _initialized = type(uint8).max; emit Initialized(type(uint8).max); } } /** * @dev Returns the highest version that has been initialized. See {reinitializer}. */ function _getInitializedVersion() internal view returns (uint8) { return _initialized; } /** * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}. */ function _isInitializing() internal view returns (bool) { return _initializing; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol) pragma solidity ^0.8.0; import "../utils/Context.sol"; /** * @dev Contract module which allows children to implement an emergency stop * mechanism that can be triggered by an authorized account. * * This module is used through inheritance. It will make available the * modifiers `whenNotPaused` and `whenPaused`, which can be applied to * the functions of your contract. Note that they will not be pausable by * simply including this module, only once the modifiers are put in place. */ abstract contract Pausable is Context { /** * @dev Emitted when the pause is triggered by `account`. */ event Paused(address account); /** * @dev Emitted when the pause is lifted by `account`. */ event Unpaused(address account); bool private _paused; /** * @dev Initializes the contract in unpaused state. */ constructor() { _paused = false; } /** * @dev Modifier to make a function callable only when the contract is not paused. * * Requirements: * * - The contract must not be paused. */ modifier whenNotPaused() { _requireNotPaused(); _; } /** * @dev Modifier to make a function callable only when the contract is paused. * * Requirements: * * - The contract must be paused. */ modifier whenPaused() { _requirePaused(); _; } /** * @dev Returns true if the contract is paused, and false otherwise. */ function paused() public view virtual returns (bool) { return _paused; } /** * @dev Throws if the contract is paused. */ function _requireNotPaused() internal view virtual { require(!paused(), "Pausable: paused"); } /** * @dev Throws if the contract is not paused. */ function _requirePaused() internal view virtual { require(paused(), "Pausable: not paused"); } /** * @dev Triggers stopped state. * * Requirements: * * - The contract must not be paused. */ function _pause() internal virtual whenNotPaused { _paused = true; emit Paused(_msgSender()); } /** * @dev Returns to normal state. * * Requirements: * * - The contract must be paused. */ function _unpause() internal virtual whenPaused { _paused = false; emit Unpaused(_msgSender()); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol) pragma solidity ^0.8.0; /** * @dev Contract module that helps prevent reentrant calls to a function. * * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier * available, which can be applied to functions to make sure there are no nested * (reentrant) calls to them. * * Note that because there is a single `nonReentrant` guard, functions marked as * `nonReentrant` may not call one another. This can be worked around by making * those functions `private`, and then adding `external` `nonReentrant` entry * points to them. * * TIP: If you would like to learn more about reentrancy and alternative ways * to protect against it, check out our blog post * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. */ abstract contract ReentrancyGuard { // Booleans are more expensive than uint256 or any type that takes up a full // word because each write operation emits an extra SLOAD to first read the // slot's contents, replace the bits taken up by the boolean, and then write // back. This is the compiler's defense against contract upgrades and // pointer aliasing, and it cannot be disabled. // The values being non-zero value makes deployment a bit more expensive, // but in exchange the refund on every call to nonReentrant will be lower in // amount. Since refunds are capped to a percentage of the total // transaction's gas, it is best to keep them low in cases like this one, to // increase the likelihood of the full refund coming into effect. uint256 private constant _NOT_ENTERED = 1; uint256 private constant _ENTERED = 2; uint256 private _status; constructor() { _status = _NOT_ENTERED; } /** * @dev Prevents a contract from calling itself, directly or indirectly. * Calling a `nonReentrant` function from another `nonReentrant` * function is not supported. It is possible to prevent this from happening * by making the `nonReentrant` function external, and making it call a * `private` function that does the actual work. */ modifier nonReentrant() { _nonReentrantBefore(); _; _nonReentrantAfter(); } function _nonReentrantBefore() private { // On the first call to nonReentrant, _status will be _NOT_ENTERED require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); // Any calls to nonReentrant after this point will fail _status = _ENTERED; } function _nonReentrantAfter() private { // By storing the original value once again, a refund is triggered (see // https://eips.ethereum.org/EIPS/eip-2200) _status = _NOT_ENTERED; } /** * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ function _reentrancyGuardEntered() internal view returns (bool) { return _status == _ENTERED; } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/IERC20Permit.sol) pragma solidity ^0.8.0; /** * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612]. * * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't * need to send a transaction, and thus is not required to hold Ether at all. */ interface IERC20Permit { /** * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens, * given ``owner``'s signed approval. * * IMPORTANT: The same issues {IERC20-approve} has related to transaction * ordering also apply here. * * Emits an {Approval} event. * * Requirements: * * - `spender` cannot be the zero address. * - `deadline` must be a timestamp in the future. * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner` * over the EIP712-formatted function arguments. * - the signature must use ``owner``'s current nonce (see {nonces}). * * For more information on the signature format, see the * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP * section]. */ function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /** * @dev Returns the current nonce for `owner`. This value must be * included whenever a signature is generated for {permit}. * * Every successful call to {permit} increases ``owner``'s nonce by one. This * prevents a signature from being used multiple times. */ function nonces(address owner) external view returns (uint256); /** * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}. */ // solhint-disable-next-line func-name-mixedcase function DOMAIN_SEPARATOR() external view returns (bytes32); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol) pragma solidity ^0.8.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20 { /** * @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); /** * @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 `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, 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 `from` to `to` 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 from, address to, uint256 amount) external returns (bool); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/utils/SafeERC20.sol) pragma solidity ^0.8.0; import "../IERC20.sol"; import "../extensions/IERC20Permit.sol"; import "../../../utils/Address.sol"; /** * @title SafeERC20 * @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. * 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 SafeERC20 { using Address for address; /** * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value, * non-reverting calls are assumed to be successful. */ function safeTransfer(IERC20 token, address to, uint256 value) internal { _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); } /** * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful. */ function safeTransferFrom(IERC20 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(IERC20 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' 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)); } /** * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value, * non-reverting calls are assumed to be successful. */ function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { uint256 oldAllowance = token.allowance(address(this), spender); _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value)); } /** * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value, * non-reverting calls are assumed to be successful. */ function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { unchecked { uint256 oldAllowance = token.allowance(address(this), spender); require(oldAllowance >= value, "SafeERC20: decreased allowance below zero"); _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value)); } } /** * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value, * non-reverting calls are assumed to be successful. Compatible with tokens that require the approval to be set to * 0 before setting it to a non-zero value. */ function forceApprove(IERC20 token, address spender, uint256 value) internal { bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value); if (!_callOptionalReturnBool(token, approvalCall)) { _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0)); _callOptionalReturn(token, approvalCall); } } /** * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`. * Revert on invalid signature. */ function safePermit( IERC20Permit token, address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) internal { uint256 nonceBefore = token.nonces(owner); token.permit(owner, spender, value, deadline, v, r, s); uint256 nonceAfter = token.nonces(owner); require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed"); } /** * @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(IERC20 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"); require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); } /** * @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). * * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead. */ function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) { // 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 cannot use {Address-functionCall} here since this should return false // and not revert is the subcall reverts. (bool success, bytes memory returndata) = address(token).call(data); return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token)); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol) pragma solidity ^0.8.1; /** * @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 * * Furthermore, `isContract` will also return true if the target contract within * the same transaction is already scheduled for destruction by `SELFDESTRUCT`, * which only has an effect at the end of a transaction. * ==== * * [IMPORTANT] * ==== * You shouldn't rely on `isContract` to protect against flash loan attacks! * * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract * constructor. * ==== */ function isContract(address account) internal view returns (bool) { // This method relies on extcodesize/address.code.length, which returns 0 // for contracts in construction, since the code is only stored at the end // of the constructor execution. return account.code.length > 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://consensys.net/diligence/blog/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.8.0/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"); (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 functionCallWithValue(target, data, 0, "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"); (bool success, bytes memory returndata) = target.call{value: value}(data); return verifyCallResultFromTarget(target, 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) { (bool success, bytes memory returndata) = target.staticcall(data); return verifyCallResultFromTarget(target, 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) { (bool success, bytes memory returndata) = target.delegatecall(data); return verifyCallResultFromTarget(target, success, returndata, errorMessage); } /** * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract. * * _Available since v4.8._ */ function verifyCallResultFromTarget( address target, bool success, bytes memory returndata, string memory errorMessage ) internal view returns (bytes memory) { if (success) { if (returndata.length == 0) { // only check isContract if the call was successful and the return data is empty // otherwise we already know that it was a contract require(isContract(target), "Address: call to non-contract"); } return returndata; } else { _revert(returndata, errorMessage); } } /** * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the * revert reason or using the provided one. * * _Available since v4.3._ */ function verifyCallResult( bool success, bytes memory returndata, string memory errorMessage ) internal pure returns (bytes memory) { if (success) { return returndata; } else { _revert(returndata, errorMessage); } } function _revert(bytes memory returndata, string memory errorMessage) private pure { // 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 /// @solidity memory-safe-assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert(errorMessage); } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/Context.sol) pragma solidity ^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 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) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; /** * @title Careful Math * @author Moonwell * @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); } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; abstract contract ComptrollerInterface { /// @notice Indicator that this is a Comptroller contract (for inspection) bool public constant isComptroller = true; /*** Assets You Are In ***/ function enterMarkets( address[] calldata mTokens ) external virtual returns (uint[] memory); function exitMarket(address mToken) external virtual returns (uint); /*** Policy Hooks ***/ function mintAllowed( address mToken, address minter, uint mintAmount ) external virtual returns (uint); function redeemAllowed( address mToken, address redeemer, uint redeemTokens ) external virtual returns (uint); // Do not remove, still used by MToken function redeemVerify( address mToken, address redeemer, uint redeemAmount, uint redeemTokens ) external pure virtual; function borrowAllowed( address mToken, address borrower, uint borrowAmount ) external virtual returns (uint); function repayBorrowAllowed( address mToken, address payer, address borrower, uint repayAmount ) external virtual returns (uint); function liquidateBorrowAllowed( address mTokenBorrowed, address mTokenCollateral, address liquidator, address borrower, uint repayAmount ) external view virtual returns (uint); function seizeAllowed( address mTokenCollateral, address mTokenBorrowed, address liquidator, address borrower, uint seizeTokens ) external virtual returns (uint); function transferAllowed( address mToken, address src, address dst, uint transferTokens ) external virtual returns (uint); /*** Liquidity/Liquidation Calculations ***/ function liquidateCalculateSeizeTokens( address mTokenBorrowed, address mTokenCollateral, uint repayAmount ) external view virtual returns (uint, uint); } // The hooks that were patched out of the comptroller to make room for the supply caps, if we need them abstract contract ComptrollerInterfaceWithAllVerificationHooks is ComptrollerInterface { function mintVerify( address mToken, address minter, uint mintAmount, uint mintTokens ) external virtual; // Included in ComptrollerInterface already // function redeemVerify(address mToken, address redeemer, uint redeemAmount, uint redeemTokens) virtual external; function borrowVerify( address mToken, address borrower, uint borrowAmount ) external virtual; function repayBorrowVerify( address mToken, address payer, address borrower, uint repayAmount, uint borrowerIndex ) external virtual; function liquidateBorrowVerify( address mTokenBorrowed, address mTokenCollateral, address liquidator, address borrower, uint repayAmount, uint seizeTokens ) external virtual; function seizeVerify( address mTokenCollateral, address mTokenBorrowed, address liquidator, address borrower, uint seizeTokens ) external virtual; function transferVerify( address mToken, address src, address dst, uint transferTokens ) external virtual; }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./MToken.sol"; import "./oracles/PriceOracle.sol"; import "./rewards/MultiRewardDistributor.sol"; contract UnitrollerAdminStorage { /** * @notice Administrator for this contract */ address public admin; /** * @notice Pending administrator for this contract */ address public pendingAdmin; /** * @notice Active brains of Unitroller */ address public comptrollerImplementation; /** * @notice Pending brains of Unitroller */ address public pendingComptrollerImplementation; } contract ComptrollerVXStorage is UnitrollerAdminStorage { /** * @notice Oracle which gives the price of any given asset */ PriceOracle public oracle; /** * @notice Multiplier used to calculate the maximum repayAmount when liquidating a borrow */ uint public closeFactorMantissa; /** * @notice Multiplier representing the discount on collateral that a liquidator receives */ uint public liquidationIncentiveMantissa; /** * @notice Per-account mapping of "assets you are in", capped by maxAssets */ mapping(address => MToken[]) public accountAssets; struct Market { /// @notice Whether or not this market is listed bool isListed; /** * @notice 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; /// @notice Per-market mapping of "accounts in this asset" mapping(address => bool) accountMembership; } /** * @notice Official mapping of mTokens -> Market metadata * @dev Used e.g. to determine if a market is supported */ mapping(address => Market) public markets; /** * @notice 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 transferGuardianPaused; bool public seizeGuardianPaused; mapping(address => bool) public mintGuardianPaused; mapping(address => bool) public borrowGuardianPaused; /// @notice A list of all markets MToken[] public allMarkets; // @notice 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; // @notice Borrow caps enforced by borrowAllowed for each mToken address. Defaults to zero which corresponds to unlimited borrowing. mapping(address => uint) public borrowCaps; struct RewardMarketState { /// @notice The market's last updated rewardBorrowIndex or rewardSupplyIndex uint224 index; /// @notice The block timestamp the index was last updated at uint32 timestamp; } /// @notice Reentrant status, 0: not entered, 1: entered uint256 internal _locked; } contract ComptrollerV2Storage is ComptrollerVXStorage { /// @notice The supplyCapGuardian can set supplyCaps to any number for any market. Lowering the supply cap could disable supplying to the given market. address public supplyCapGuardian; /// @notice Supply caps enforced by mintAllowed for each cToken address. Defaults to zero which corresponds to unlimited supplying. mapping(address => uint) public supplyCaps; /// @notice The Reward distributor used to emit protocol rewards MultiRewardDistributor public rewardDistributor; }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; /** * @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 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 (-1 means infinite) * @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 (-1 means infinite) */ function allowance( address owner, address spender ) external view returns (uint256 remaining); event Transfer(address indexed from, address indexed to, uint256 amount); event Approval( address indexed owner, address indexed spender, uint256 amount ); }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; /** * @title EIP20NonStandardInterface * @dev Version of ERC20 with no return values for `transfer` and `transferFrom` * See https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca */ interface EIP20NonStandardInterface { /** * @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` does not return a value, in violation of the ERC-20 specification /// !!!!!!!!!!!!!! /// /** * @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 */ function transfer(address dst, uint256 amount) external; /// /// !!!!!!!!!!!!!! /// !!! NOTICE !!! `transferFrom` does not return a value, in violation of the ERC-20 specification /// !!!!!!!!!!!!!! /// /** * @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 */ function transferFrom(address src, address dst, uint256 amount) external; /** * @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); event Transfer(address indexed from, address indexed to, uint256 amount); event Approval( address indexed owner, address indexed spender, uint256 amount ); }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; contract ComptrollerErrorReporter { enum Error { NO_ERROR, UNAUTHORIZED, COMPTROLLER_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, SET_PAUSE_GUARDIAN_OWNER_CHECK, SET_GAS_AMOUNT_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, COMPTROLLER_REJECTION, COMPTROLLER_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_COMPTROLLER_REJECTION, LIQUIDATE_ACCRUE_BORROW_INTEREST_FAILED, LIQUIDATE_ACCRUE_COLLATERAL_INTEREST_FAILED, LIQUIDATE_COLLATERAL_FRESHNESS_CHECK, LIQUIDATE_COMPTROLLER_REJECTION, LIQUIDATE_COMPTROLLER_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_COMPTROLLER_REJECTION, LIQUIDATE_SEIZE_LIQUIDATOR_IS_BORROWER, LIQUIDATE_SEIZE_TOO_MUCH, MINT_ACCRUE_INTEREST_FAILED, MINT_COMPTROLLER_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_COMPTROLLER_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_COMPTROLLER_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_COMPTROLLER_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_COMPTROLLER_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, SET_PROTOCOL_SEIZE_SHARE_ACCRUE_INTEREST_FAILED, SET_PROTOCOL_SEIZE_SHARE_OWNER_CHECK, SET_PROTOCOL_SEIZE_SHARE_FRESH_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); } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./CarefulMath.sol"; import "./ExponentialNoError.sol"; /** * @title Exponential module for storing fixed-precision decimals * @author Moonwell * @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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure returns (MathError, Exp memory) { return getExp(a.mantissa, b.mantissa); } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; /** * @title Exponential module for storing fixed-precision decimals * @author Moonwell * @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) internal pure 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 ) internal pure 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 ) internal pure 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 ) internal pure returns (bool) { return left.mantissa < right.mantissa; } /** * @dev Checks if left Exp <= right Exp. */ function lessThanOrEqualExp( Exp memory left, Exp memory right ) internal pure returns (bool) { return left.mantissa <= right.mantissa; } /** * @dev Checks if left Exp > right Exp. */ function greaterThanExp( Exp memory left, Exp memory right ) internal pure returns (bool) { return left.mantissa > right.mantissa; } /** * @dev returns true if Exp is exactly zero */ function isZeroExp(Exp memory value) internal pure returns (bool) { return value.mantissa == 0; } function safe224( uint n, string memory errorMessage ) internal pure returns (uint224) { require(n < 2 ** 224, errorMessage); return uint224(n); } function safe32( uint n, string memory errorMessage ) internal pure returns (uint32) { require(n < 2 ** 32, errorMessage); return uint32(n); } function add_( Exp memory a, Exp memory b ) internal pure returns (Exp memory) { return Exp({mantissa: add_(a.mantissa, b.mantissa)}); } function add_( Double memory a, Double memory b ) internal pure returns (Double memory) { return Double({mantissa: add_(a.mantissa, b.mantissa)}); } function add_(uint a, uint b) internal pure returns (uint) { return add_(a, b, "addition overflow"); } function add_( uint a, uint b, string memory errorMessage ) internal pure returns (uint) { uint c = a + b; require(c >= a, errorMessage); return c; } function sub_( Exp memory a, Exp memory b ) internal pure returns (Exp memory) { return Exp({mantissa: sub_(a.mantissa, b.mantissa)}); } function sub_( Double memory a, Double memory b ) internal pure returns (Double memory) { return Double({mantissa: sub_(a.mantissa, b.mantissa)}); } function sub_(uint a, uint b) internal pure returns (uint) { return sub_(a, b, "subtraction underflow"); } function sub_( uint a, uint b, string memory errorMessage ) internal pure returns (uint) { require(b <= a, errorMessage); return a - b; } function mul_( Exp memory a, Exp memory b ) internal pure returns (Exp memory) { return Exp({mantissa: mul_(a.mantissa, b.mantissa) / expScale}); } function mul_(Exp memory a, uint b) internal pure returns (Exp memory) { return Exp({mantissa: mul_(a.mantissa, b)}); } function mul_(uint a, Exp memory b) internal pure returns (uint) { return mul_(a, b.mantissa) / expScale; } function mul_( Double memory a, Double memory b ) internal pure returns (Double memory) { return Double({mantissa: mul_(a.mantissa, b.mantissa) / doubleScale}); } function mul_( Double memory a, uint b ) internal pure returns (Double memory) { return Double({mantissa: mul_(a.mantissa, b)}); } function mul_(uint a, Double memory b) internal pure returns (uint) { return mul_(a, b.mantissa) / doubleScale; } function mul_(uint a, uint b) internal pure returns (uint) { return mul_(a, b, "multiplication overflow"); } function mul_( uint a, uint b, string memory errorMessage ) internal pure 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 ) internal pure returns (Exp memory) { return Exp({mantissa: div_(mul_(a.mantissa, expScale), b.mantissa)}); } function div_(Exp memory a, uint b) internal pure returns (Exp memory) { return Exp({mantissa: div_(a.mantissa, b)}); } function div_(uint a, Exp memory b) internal pure returns (uint) { return div_(mul_(a, expScale), b.mantissa); } function div_( Double memory a, Double memory b ) internal pure returns (Double memory) { return Double({mantissa: div_(mul_(a.mantissa, doubleScale), b.mantissa)}); } function div_( Double memory a, uint b ) internal pure returns (Double memory) { return Double({mantissa: div_(a.mantissa, b)}); } function div_(uint a, Double memory b) internal pure returns (uint) { return div_(mul_(a, doubleScale), b.mantissa); } function div_(uint a, uint b) internal pure returns (uint) { return div_(a, b, "divide by zero"); } function div_( uint a, uint b, string memory errorMessage ) internal pure returns (uint) { require(b > 0, errorMessage); return a / b; } function fraction(uint a, uint b) internal pure returns (Double memory) { return Double({mantissa: div_(mul_(a, doubleScale), b)}); } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; /** * @title Moonwell's InterestRateModel Interface * @author Moonwell */ abstract contract InterestRateModel { /// @notice Indicator that this is an InterestRateModel contract (for inspection) bool public constant isInterestRateModel = true; /** * @notice Calculates the current borrow interest rate per timestamp * @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 timestamp (as a percentage, and scaled by 1e18) */ function getBorrowRate( uint cash, uint borrows, uint reserves ) external view virtual returns (uint); /** * @notice Calculates the current supply interest rate per timestamp * @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 timestamp (as a percentage, and scaled by 1e18) */ function getSupplyRate( uint cash, uint borrows, uint reserves, uint reserveFactorMantissa ) external view virtual returns (uint); }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./ComptrollerInterface.sol"; import "./MTokenInterfaces.sol"; import "./ErrorReporter.sol"; import "./Exponential.sol"; import "./EIP20Interface.sol"; import "./InterestRateModel.sol"; /** * @title Moonwell's MToken Contract * @notice Abstract base for MTokens * @author Moonwell */ abstract contract MToken is MTokenInterface, Exponential, TokenErrorReporter { /** * @notice Initialize the money market * @param comptroller_ The address of the Comptroller * @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 initialize( ComptrollerInterface comptroller_, InterestRateModel interestRateModel_, uint initialExchangeRateMantissa_, string memory name_, string memory symbol_, uint8 decimals_ ) public { require(msg.sender == admin, "only admin may initialize the market"); require( accrualBlockTimestamp == 0 && borrowIndex == 0, "market may only be initialized once" ); // Set initial exchange rate initialExchangeRateMantissa = initialExchangeRateMantissa_; require( initialExchangeRateMantissa > 0, "initial exchange rate must be greater than zero." ); // Set the comptroller uint err = _setComptroller(comptroller_); require(err == uint(Error.NO_ERROR), "setting comptroller failed"); // Initialize block timestamp and borrow index (block timestamp mocks depend on comptroller being set) accrualBlockTimestamp = getBlockTimestamp(); borrowIndex = mantissaOne; // Set the interest rate model (depends on block timestamp / borrow index) err = _setInterestRateModelFresh(interestRateModel_); require( err == uint(Error.NO_ERROR), "setting interest rate model 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; } /** * @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 = comptroller.transferAllowed( address(this), src, dst, tokens ); if (allowed != 0) { return failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.TRANSFER_COMPTROLLER_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 = type(uint).max; } else { startingAllowance = transferAllowances[src][spender]; } /* Do the calculations, checking for {under,over}flow */ MathError mathErr; uint allowanceNew; uint srcTokensNew; uint dstTokensNew; (mathErr, allowanceNew) = subUInt(startingAllowance, tokens); if (mathErr != MathError.NO_ERROR) { return fail(Error.MATH_ERROR, FailureInfo.TRANSFER_NOT_ALLOWED); } (mathErr, srcTokensNew) = 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] = srcTokensNew; accountTokens[dst] = dstTokensNew; /* Eat some of the allowance (if necessary) */ if (startingAllowance != type(uint).max) { transferAllowances[src][spender] = allowanceNew; } /* We emit a Transfer event */ emit Transfer(src, dst, tokens); // unused function // comptroller.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 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 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 (uint.max means infinite) * @return Whether or not the approval succeeded */ function approve( address spender, uint256 amount ) external 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 (uint.max means infinite) */ function allowance( address owner, address spender ) external view override 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 view override 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 override returns (uint) { Exp memory exchangeRate = Exp({mantissa: exchangeRateCurrent()}); (MathError mErr, uint balance) = mulScalarTruncate( exchangeRate, accountTokens[owner] ); require(mErr == MathError.NO_ERROR, "balance could not be calculated"); return balance; } /** * @notice Get a snapshot of the account's balances, and the cached exchange rate * @dev This is used by comptroller 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 view override 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 timestamp * This exists mainly for inheriting test contracts to stub this result. */ function getBlockTimestamp() internal view virtual returns (uint) { return block.timestamp; } /** * @notice Returns the current per-timestamp borrow interest rate for this mToken * @return The borrow interest rate per timestamp, scaled by 1e18 */ function borrowRatePerTimestamp() external view override returns (uint) { return interestRateModel.getBorrowRate( getCashPrior(), totalBorrows, totalReserves ); } /** * @notice Returns the current per-timestamp supply interest rate for this mToken * @return The supply interest rate per timestamp, scaled by 1e18 */ function supplyRatePerTimestamp() external view override 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 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 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 view override returns (uint) { (MathError err, uint result) = borrowBalanceStoredInternal(account); require( err == MathError.NO_ERROR, "borrowBalanceStored: borrowBalanceStoredInternal 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 override 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 view override returns (uint) { (MathError err, uint result) = exchangeRateStoredInternal(); require( err == MathError.NO_ERROR, "exchangeRateStored: exchangeRateStoredInternal 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 virtual 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 view override returns (uint) { return getCashPrior(); } /** * @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 override returns (uint) { /* Remember the initial block timestamp */ uint currentBlockTimestamp = getBlockTimestamp(); uint accrualBlockTimestampPrior = accrualBlockTimestamp; /* Short-circuit accumulating 0 interest */ if (accrualBlockTimestampPrior == currentBlockTimestamp) { 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 is absurdly high" ); /* Calculate the number of blocks elapsed since the last accrual */ (MathError mathErr, uint blockDelta) = subUInt( currentBlockTimestamp, accrualBlockTimestampPrior ); require( mathErr == MathError.NO_ERROR, "could not calculate 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; uint interestAccumulated; uint totalBorrowsNew; uint totalReservesNew; uint borrowIndexNew; (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, interestAccumulated) = mulScalarTruncate( simpleInterestFactor, borrowsPrior ); if (mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo .ACCRUE_INTEREST_ACCUMULATED_INTEREST_CALCULATION_FAILED, uint(mathErr) ); } (mathErr, totalBorrowsNew) = addUInt(interestAccumulated, borrowsPrior); if (mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo .ACCRUE_INTEREST_NEW_TOTAL_BORROWS_CALCULATION_FAILED, uint(mathErr) ); } (mathErr, totalReservesNew) = mulScalarTruncateAddUInt( Exp({mantissa: reserveFactorMantissa}), interestAccumulated, reservesPrior ); if (mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo .ACCRUE_INTEREST_NEW_TOTAL_RESERVES_CALCULATION_FAILED, uint(mathErr) ); } (mathErr, 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 */ accrualBlockTimestamp = currentBlockTimestamp; borrowIndex = borrowIndexNew; totalBorrows = totalBorrowsNew; totalReserves = totalReservesNew; /* We emit an AccrueInterest event */ emit AccrueInterest( cashPrior, interestAccumulated, borrowIndexNew, 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 = comptroller.mintAllowed( address(this), minter, mintAmount ); if (allowed != 0) { return ( failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.MINT_COMPTROLLER_REJECTION, allowed ), 0 ); } /* Verify market's block timestamp equals current block timestamp */ if (accrualBlockTimestamp != getBlockTimestamp()) { 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 GLMR 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_EXCHANGE_CALCULATION_FAILED" ); /* * 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_NEW_TOTAL_SUPPLY_CALCULATION_FAILED" ); (vars.mathErr, vars.accountTokensNew) = addUInt( accountTokens[minter], vars.mintTokens ); require( vars.mathErr == MathError.NO_ERROR, "MINT_NEW_ACCOUNT_BALANCE_CALCULATION_FAILED" ); /* 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 // comptroller.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(payable(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(payable(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, "one of redeemTokensIn or redeemAmountIn must be 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 */ if (redeemTokensIn == type(uint).max) { vars.redeemTokens = accountTokens[redeemer]; } else { vars.redeemTokens = redeemTokensIn; } (vars.mathErr, vars.redeemAmount) = mulScalarTruncate( Exp({mantissa: vars.exchangeRateMantissa}), vars.redeemTokens ); 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 */ if (redeemAmountIn == type(uint).max) { vars.redeemTokens = accountTokens[redeemer]; (vars.mathErr, vars.redeemAmount) = mulScalarTruncate( Exp({mantissa: vars.exchangeRateMantissa}), vars.redeemTokens ); if (vars.mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo .REDEEM_EXCHANGE_TOKENS_CALCULATION_FAILED, uint(vars.mathErr) ); } } else { vars.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) ); } } } /* Fail if redeem not allowed */ uint allowed = comptroller.redeemAllowed( address(this), redeemer, vars.redeemTokens ); if (allowed != 0) { return failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.REDEEM_COMPTROLLER_REJECTION, allowed ); } /* Verify market's block timestamp equals current block timestamp */ if (accrualBlockTimestamp != getBlockTimestamp()) { 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 ); } ///////////////////////// // EFFECTS & INTERACTIONS // (No safe failures beyond this point) /* 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 */ comptroller.redeemVerify( address(this), redeemer, vars.redeemAmount, vars.redeemTokens ); /* * We invoke doTransferOut for the redeemer and the redeemAmount. * Note: The mToken must handle variations between ERC-20 and GLMR 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); 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(payable(msg.sender), 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 = comptroller.borrowAllowed( address(this), borrower, borrowAmount ); if (allowed != 0) { return failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.BORROW_COMPTROLLER_REJECTION, allowed ); } /* Verify market's block timestamp equals current block timestamp */ if (accrualBlockTimestamp != getBlockTimestamp()) { 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 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 invoke doTransferOut for the borrower and the borrowAmount. * Note: The mToken must handle variations between ERC-20 and GLMR 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 call the defense hook */ // unused function // comptroller.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 underlying 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 = comptroller.repayBorrowAllowed( address(this), payer, borrower, repayAmount ); if (allowed != 0) { return ( failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.REPAY_BORROW_COMPTROLLER_REJECTION, allowed ), 0 ); } /* Verify market's block timestamp equals current block timestamp */ if (accrualBlockTimestamp != getBlockTimestamp()) { 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 == uint.max, repayAmount = accountBorrows */ if (repayAmount == type(uint).max) { 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 GLMR 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, "REPAY_BORROW_NEW_ACCOUNT_BORROW_BALANCE_CALCULATION_FAILED" ); (vars.mathErr, vars.totalBorrowsNew) = subUInt( totalBorrows, vars.actualRepayAmount ); require( vars.mathErr == MathError.NO_ERROR, "REPAY_BORROW_NEW_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 // comptroller.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, MTokenInterface 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, MTokenInterface mTokenCollateral ) internal returns (uint, uint) { /* Fail if liquidate not allowed */ uint allowed = comptroller.liquidateBorrowAllowed( address(this), address(mTokenCollateral), liquidator, borrower, repayAmount ); if (allowed != 0) { return ( failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.LIQUIDATE_COMPTROLLER_REJECTION, allowed ), 0 ); } /* Verify market's block timestamp equals current block timestamp */ if (accrualBlockTimestamp != getBlockTimestamp()) { return ( fail( Error.MARKET_NOT_FRESH, FailureInfo.LIQUIDATE_FRESHNESS_CHECK ), 0 ); } /* Verify mTokenCollateral market's block timestamp equals current block timestamp */ if (mTokenCollateral.accrualBlockTimestamp() != getBlockTimestamp()) { 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 = uint.max */ if (repayAmount == type(uint).max) { 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) = comptroller .liquidateCalculateSeizeTokens( address(this), address(mTokenCollateral), actualRepayAmount ); require( amountSeizeError == uint(Error.NO_ERROR), "LIQUIDATE_COMPTROLLER_CALCULATE_AMOUNT_SEIZE_FAILED" ); /* Revert if borrower collateral token balance < seizeTokens */ require( mTokenCollateral.balanceOf(borrower) >= seizeTokens, "LIQUIDATE_SEIZE_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 // comptroller.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 override nonReentrant returns (uint) { return seizeInternal(msg.sender, liquidator, borrower, seizeTokens); } struct SeizeInternalLocalVars { MathError mathErr; uint borrowerTokensNew; uint liquidatorTokensNew; uint liquidatorSeizeTokens; uint protocolSeizeTokens; uint protocolSeizeAmount; uint exchangeRateMantissa; uint totalReservesNew; uint totalSupplyNew; } /** * @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 = comptroller.seizeAllowed( address(this), seizerToken, liquidator, borrower, seizeTokens ); if (allowed != 0) { return failOpaque( Error.COMPTROLLER_REJECTION, FailureInfo.LIQUIDATE_SEIZE_COMPTROLLER_REJECTION, allowed ); } /* Fail if borrower = liquidator */ if (borrower == liquidator) { return fail( Error.INVALID_ACCOUNT_PAIR, FailureInfo.LIQUIDATE_SEIZE_LIQUIDATOR_IS_BORROWER ); } SeizeInternalLocalVars memory vars; /* * We calculate the new borrower and liquidator token balances, failing on underflow/overflow: * borrowerTokensNew = accountTokens[borrower] - seizeTokens * liquidatorTokensNew = accountTokens[liquidator] + seizeTokens */ (vars.mathErr, vars.borrowerTokensNew) = subUInt( accountTokens[borrower], seizeTokens ); if (vars.mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo.LIQUIDATE_SEIZE_BALANCE_DECREMENT_FAILED, uint(vars.mathErr) ); } vars.protocolSeizeTokens = mul_( seizeTokens, Exp({mantissa: protocolSeizeShareMantissa}) ); vars.liquidatorSeizeTokens = sub_( seizeTokens, vars.protocolSeizeTokens ); ( vars.mathErr, vars.exchangeRateMantissa ) = exchangeRateStoredInternal(); require(vars.mathErr == MathError.NO_ERROR, "exchange rate math error"); vars.protocolSeizeAmount = mul_ScalarTruncate( Exp({mantissa: vars.exchangeRateMantissa}), vars.protocolSeizeTokens ); vars.totalReservesNew = add_(totalReserves, vars.protocolSeizeAmount); vars.totalSupplyNew = sub_(totalSupply, vars.protocolSeizeTokens); (vars.mathErr, vars.liquidatorTokensNew) = addUInt( accountTokens[liquidator], vars.liquidatorSeizeTokens ); if (vars.mathErr != MathError.NO_ERROR) { return failOpaque( Error.MATH_ERROR, FailureInfo.LIQUIDATE_SEIZE_BALANCE_INCREMENT_FAILED, uint(vars.mathErr) ); } ///////////////////////// // EFFECTS & INTERACTIONS // (No safe failures beyond this point) /* We write the previously calculated values into storage */ totalReserves = vars.totalReservesNew; totalSupply = vars.totalSupplyNew; accountTokens[borrower] = vars.borrowerTokensNew; accountTokens[liquidator] = vars.liquidatorTokensNew; /* Emit a Transfer event */ emit Transfer(borrower, liquidator, vars.liquidatorSeizeTokens); emit Transfer(borrower, address(this), vars.protocolSeizeTokens); emit ReservesAdded( address(this), vars.protocolSeizeAmount, vars.totalReservesNew ); /* We call the defense hook */ // unused function // comptroller.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 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 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 = payable(address(0)); emit NewAdmin(oldAdmin, admin); emit NewPendingAdmin(oldPendingAdmin, pendingAdmin); return uint(Error.NO_ERROR); } /** * @notice Sets a new comptroller for the market * @dev Admin function to set a new comptroller * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details) */ function _setComptroller( ComptrollerInterface newComptroller ) public override returns (uint) { // Check caller is admin if (msg.sender != admin) { return fail( Error.UNAUTHORIZED, FailureInfo.SET_COMPTROLLER_OWNER_CHECK ); } ComptrollerInterface oldComptroller = comptroller; // Ensure invoke comptroller.isComptroller() returns true require(newComptroller.isComptroller(), "marker method returned false"); // Set market's comptroller to newComptroller comptroller = newComptroller; // Emit NewComptroller(oldComptroller, newComptroller) emit NewComptroller(oldComptroller, newComptroller); 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 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); } /** * @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 timestamp equals current block timestamp if (accrualBlockTimestamp != getBlockTimestamp()) { 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 timestamp equals current block timestamp if (accrualBlockTimestamp != getBlockTimestamp()) { 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 GLMR 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, "add reserves unexpected 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 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 timestamp equals current block timestamp if (accrualBlockTimestamp != getBlockTimestamp()) { 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, "reduce reserves unexpected 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( InterestRateModel newInterestRateModel ) public override 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( InterestRateModel newInterestRateModel ) internal returns (uint) { // Used to store old model for use in the event that is emitted on success InterestRateModel 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 timestamp equals current block timestamp if (accrualBlockTimestamp != getBlockTimestamp()) { 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(), "marker method returned false" ); // Set the interest rate model to newInterestRateModel interestRateModel = newInterestRateModel; // Emit NewMarketInterestRateModel(oldInterestRateModel, newInterestRateModel) emit NewMarketInterestRateModel( oldInterestRateModel, newInterestRateModel ); return uint(Error.NO_ERROR); } /** * @notice accrues interest and updates the protocol seize share using _setProtocolSeizeShareFresh * @dev Admin function to accrue interest and update the protocol seize share * @param newProtocolSeizeShareMantissa the new protocol seize share to use * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details) */ function _setProtocolSeizeShare( uint newProtocolSeizeShareMantissa ) external 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 change of protocol seize share failed return fail( Error(error), FailureInfo.SET_PROTOCOL_SEIZE_SHARE_ACCRUE_INTEREST_FAILED ); } // _setProtocolSeizeShareFresh emits protocol-seize-share-update-specific logs on errors, so we don't need to. return _setProtocolSeizeShareFresh(newProtocolSeizeShareMantissa); } /** * @notice updates the protocol seize share (*requires fresh interest accrual) * @dev Admin function to update the protocol seize share * @param newProtocolSeizeShareMantissa the new protocol seize share to use * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details) */ function _setProtocolSeizeShareFresh( uint newProtocolSeizeShareMantissa ) internal returns (uint) { // Used to store old share for use in the event that is emitted on success uint oldProtocolSeizeShareMantissa; // Check caller is admin if (msg.sender != admin) { return fail( Error.UNAUTHORIZED, FailureInfo.SET_PROTOCOL_SEIZE_SHARE_OWNER_CHECK ); } // We fail gracefully unless market's block timestamp equals current block timestamp if (accrualBlockTimestamp != getBlockTimestamp()) { return fail( Error.MARKET_NOT_FRESH, FailureInfo.SET_PROTOCOL_SEIZE_SHARE_FRESH_CHECK ); } // Track the market's current protocol seize share oldProtocolSeizeShareMantissa = protocolSeizeShareMantissa; // Set the protocol seize share to newProtocolSeizeShareMantissa protocolSeizeShareMantissa = newProtocolSeizeShareMantissa; // Emit NewProtocolSeizeShareMantissa(oldProtocolSeizeShareMantissa, newProtocolSeizeShareMantissa) emit NewProtocolSeizeShare( oldProtocolSeizeShareMantissa, newProtocolSeizeShareMantissa ); return uint(Error.NO_ERROR); } /*** 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 view virtual 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 } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./ComptrollerInterface.sol"; import "./InterestRateModel.sol"; import "./EIP20NonStandardInterface.sol"; import "./ErrorReporter.sol"; contract MTokenStorage { /// @dev Guard variable for re-entrancy checks bool internal _notEntered; /// @notice EIP-20 token name for this token string public name; /// @notice EIP-20 token symbol for this token string public symbol; /// @notice EIP-20 token decimals for this token uint8 public decimals; /// @notice Maximum borrow rate that can ever be applied (.0005% / block) uint internal constant borrowRateMaxMantissa = 0.0005e16; // @notice Maximum fraction of interest that can be set aside for reserves uint internal constant reserveFactorMaxMantissa = 1e18; /// @notice Administrator for this contract address payable public admin; /// @notice Pending administrator for this contract address payable public pendingAdmin; /// @notice Contract which oversees inter-mToken operations ComptrollerInterface public comptroller; /// @notice Model which tells what the current interest rate should be InterestRateModel public interestRateModel; // @notice Initial exchange rate used when minting the first MTokens (used when totalSupply = 0) uint internal initialExchangeRateMantissa; /// @notice Fraction of interest currently set aside for reserves uint public reserveFactorMantissa; /// @notice Block number that interest was last accrued at uint public accrualBlockTimestamp; /// @notice Accumulator of the total earned interest rate since the opening of the market uint public borrowIndex; /// @notice Total amount of outstanding borrows of the underlying in this market uint public totalBorrows; /// @notice Total amount of reserves of the underlying held in this market uint public totalReserves; /// @notice Total number of tokens in circulation uint public totalSupply; /// @notice Official record of token balances for each account mapping(address => uint) internal accountTokens; /// @notice Approved token transfer amounts on behalf of others mapping(address => mapping(address => uint)) internal transferAllowances; /** * @notice 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; } // @notice Mapping of account addresses to outstanding borrow balances mapping(address => BorrowSnapshot) internal accountBorrows; /// @notice Share of seized collateral that is added to reserves uint public protocolSeizeShareMantissa; } abstract contract MTokenInterface is 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 comptroller is changed event NewComptroller( ComptrollerInterface oldComptroller, ComptrollerInterface newComptroller ); /// @notice Event emitted when interestRateModel is changed event NewMarketInterestRateModel( InterestRateModel oldInterestRateModel, InterestRateModel newInterestRateModel ); /// @notice Event emitted when the reserve factor is changed event NewReserveFactor( uint oldReserveFactorMantissa, uint newReserveFactorMantissa ); /// @notice Event emitted when the protocol seize share is changed event NewProtocolSeizeShare( uint oldProtocolSeizeShareMantissa, uint newProtocolSeizeShareMantissa ); /// @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); /*** User Interface ***/ function transfer(address dst, uint amount) external virtual returns (bool); function transferFrom( address src, address dst, uint amount ) external virtual returns (bool); function approve( address spender, uint amount ) external virtual returns (bool); function allowance( address owner, address spender ) external view virtual returns (uint); function balanceOf(address owner) external view virtual returns (uint); function balanceOfUnderlying(address owner) external virtual returns (uint); function getAccountSnapshot( address account ) external view virtual returns (uint, uint, uint, uint); function borrowRatePerTimestamp() external view virtual returns (uint); function supplyRatePerTimestamp() external view virtual returns (uint); function totalBorrowsCurrent() external virtual returns (uint); function borrowBalanceCurrent( address account ) external virtual returns (uint); function borrowBalanceStored( address account ) external view virtual returns (uint); function exchangeRateCurrent() external virtual returns (uint); function exchangeRateStored() external view virtual returns (uint); function getCash() external view virtual returns (uint); function accrueInterest() external virtual returns (uint); function seize( address liquidator, address borrower, uint seizeTokens ) external virtual returns (uint); /*** Admin Functions ***/ function _setPendingAdmin( address payable newPendingAdmin ) external virtual returns (uint); function _acceptAdmin() external virtual returns (uint); function _setComptroller( ComptrollerInterface newComptroller ) external virtual returns (uint); function _setReserveFactor( uint newReserveFactorMantissa ) external virtual returns (uint); function _reduceReserves(uint reduceAmount) external virtual returns (uint); function _setInterestRateModel( InterestRateModel newInterestRateModel ) external virtual returns (uint); function _setProtocolSeizeShare( uint newProtocolSeizeShareMantissa ) external virtual returns (uint); } contract MErc20Storage { /// @notice Underlying asset for this MToken address public underlying; } abstract contract MErc20Interface is MErc20Storage { /*** User Interface ***/ function mint(uint mintAmount) external virtual returns (uint); function mintWithPermit( uint mintAmount, uint deadline, uint8 v, bytes32 r, bytes32 s ) external virtual returns (uint); function redeem(uint redeemTokens) external virtual returns (uint); function redeemUnderlying( uint redeemAmount ) external virtual returns (uint); function borrow(uint borrowAmount) external virtual returns (uint); function repayBorrow(uint repayAmount) external virtual returns (uint); function repayBorrowBehalf( address borrower, uint repayAmount ) external virtual returns (uint); function liquidateBorrow( address borrower, uint repayAmount, MTokenInterface mTokenCollateral ) external virtual returns (uint); function sweepToken(EIP20NonStandardInterface token) external virtual; /*** Admin Functions ***/ function _addReserves(uint addAmount) external virtual returns (uint); } contract MDelegationStorage { /// @notice Implementation address for this contract address public implementation; } abstract contract MDelegatorInterface is MDelegationStorage { /// @notice Emitted when implementation is changed event NewImplementation( address oldImplementation, address newImplementation ); /** * @notice Called by the admin to update the implementation of the delegator * @param implementation_ The address of the new implementation for delegation * @param allowResign Flag to indicate whether to call _resignImplementation on the old implementation * @param becomeImplementationData The encoded bytes data to be passed to _becomeImplementation */ function _setImplementation( address implementation_, bool allowResign, bytes memory becomeImplementationData ) external virtual; } abstract contract MDelegateInterface is MDelegationStorage { /** * @notice Called by the delegator on a delegate to initialize it for duty * @dev Should revert if any issues arise which make it unfit for delegation * @param data The encoded bytes data for any initialization */ function _becomeImplementation(bytes memory data) external virtual; /// @notice Called by the delegator on a delegate to forfeit its responsibility function _resignImplementation() external virtual; }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "../MToken.sol"; abstract contract PriceOracle { /// @notice Indicator that this is a PriceOracle contract (for inspection) bool public constant isPriceOracle = true; /** * @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 virtual returns (uint); }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import {ReentrancyGuard} from "@openzeppelin-contracts/contracts/security/ReentrancyGuard.sol"; import {Initializable} from "@openzeppelin-contracts/contracts/proxy/utils/Initializable.sol"; import {SafeERC20} from "@openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol"; import {Pausable} from "@openzeppelin-contracts/contracts/security/Pausable.sol"; import {IERC20} from "@openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; import {MToken} from "../MToken.sol"; import {Comptroller} from "../Comptroller.sol"; import {MTokenInterface} from "../MTokenInterfaces.sol"; import {ExponentialNoError} from "../ExponentialNoError.sol"; import {MultiRewardDistributorCommon} from "./MultiRewardDistributorCommon.sol"; /** @title A multi-asset distributor that tracks mTokens supply/borrows @author Octavius - [email protected] This contract integrates with the Moonwell Comptroller and manages all reward disbursal and index calculations both for the global market indices as well as individual user indices on those markets. It is largely the same logic that compound uses, just generalized (meaning that transfers will not fail if things can't be sent out, but the excess is accrued on the books to be sent later). Each market has an array of configs, each with a unique emission token owned by a specific team/user. That owner can adjust supply and borrow emissions, end times, and This emitter also supports native assets, but keep in mind that things get complicated with multiple owners managing a native asset emitter - one owner can drain the contract by increasing their own Delegates admin control to the comptroller's admin (no internal admin controls). There is a hard rule that each market should only have 1 config with a specific emission token. Emission configs are non-removable because they hold the supplier/borrower indices and that would cause rewards to not be disbursed properly when a config is removed. There is a pause guardian in this contract that can immediately stop all token emissions. Accruals still happen but no tokens will be sent out when the circuit breaker is popped. Much like the pause guardians on the Comptroller, only the comptroller's admin can actually unpause things. */ contract MultiRewardDistributor is Pausable, ReentrancyGuard, Initializable, MultiRewardDistributorCommon, ExponentialNoError { using SafeERC20 for IERC20; /// @notice The main data storage for this contract, holds a mapping of mToken to array // of market configs mapping(address => MarketEmissionConfig[]) public marketConfigs; /// @notice Comptroller this distributor is bound to Comptroller public comptroller; /// we can't make this immutable because we are using proxies /// @notice The pause guardian for this contract address public pauseGuardian; /// @notice The initialIndexConstant, used to initialize indexes, and taken from the Comptroller uint224 public constant initialIndexConstant = 1e36; /// @notice The emission cap dictates an upper limit for reward speed emission speed configs /// @dev By default, is set to 100 1e18 token emissions / sec to avoid unbounded /// computation/multiplication overflows uint256 public emissionCap; // Some structs we can't move to the interface struct CurrentMarketData { uint256 totalMTokens; uint256 totalBorrows; Exp marketBorrowIndex; } struct CalculatedData { CurrentMarketData marketData; MTokenData mTokenInfo; } /// construct the logic contract and initialize so that the initialize function is uncallable /// from the implementation and only callable from the proxy constructor() { // _disableInitializers(); } function initialize( address _comptroller, address _pauseGuardian ) external initializer { // Sanity check the params require( _comptroller != address(0), "Comptroller can't be the 0 address!" ); require( _pauseGuardian != address(0), "Pause Guardian can't be the 0 address!" ); comptroller = Comptroller(payable(_comptroller)); require( comptroller.isComptroller(), "Can't bind to something that's not a comptroller!" ); pauseGuardian = _pauseGuardian; emissionCap = 100e18; } /* ==================================================================================================== ACL Modifiers all modifiers allow for the admin to call in to take actions within this contract, the idea being that the timelock can act like an owner of the config to set parameters, and act like the comptroller to kick the reward index updates, and act like a pause guardian to pause things. ==================================================================================================== */ /// @notice Only allow the comptroller's admin to take an action, usually the timelock modifier onlyComptrollersAdmin() { require( msg.sender == address(comptroller.admin()), "Only the comptroller's administrator can do this!" ); _; } /// @notice Only allow the comptroller OR the comptroller's admin to take an action modifier onlyComptrollerOrAdmin() { require( msg.sender == address(comptroller) || msg.sender == comptroller.admin(), "Only the comptroller or comptroller admin can call this function" ); _; } /// @notice Only allow the emission config owner OR the comptroller's admin to take an action modifier onlyEmissionConfigOwnerOrAdmin( MToken _mToken, address emissionToken ) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, emissionToken ); require( msg.sender == emissionConfig.config.owner || msg.sender == comptroller.admin(), "Only the config owner or comptroller admin can call this function" ); _; } /// @notice Only allow the pause guardian OR the comptroller's admin to take an action modifier onlyPauseGuardianOrAdmin() { require( msg.sender == pauseGuardian || msg.sender == comptroller.admin(), "Only the pause guardian or comptroller admin can call this function" ); _; } /* ==================================================================================================== External/publicly accessible API The main public API for the contract, generally focused on getting a user's outstanding rewards or pulling down specific configs. Users should call `claimRewards` on the comptroller as usual to recv their rewards. ==================================================================================================== */ /** * @notice Get the current owner of a config * @param _mToken The market to get a config for * @param _emissionToken The reward token address */ function getCurrentOwner( MToken _mToken, address _emissionToken ) external view returns (address) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); return emissionConfig.config.owner; } /// @notice A view to enumerate all configs for a given market, does not include index data function getAllMarketConfigs( MToken _mToken ) external view returns (MarketConfig[] memory) { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; MarketConfig[] memory outputMarketConfigs = new MarketConfig[]( configs.length ); // Pop out the MarketConfigs to return them for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; outputMarketConfigs[index] = emissionConfig.config; } return outputMarketConfigs; } /// @notice A view to get a config for a specific market/emission token pair function getConfigForMarket( MToken _mToken, address _emissionToken ) external view returns (MarketConfig memory) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); return emissionConfig.config; } /// @notice A view to enumerate a user's rewards across all markets and all emission tokens function getOutstandingRewardsForUser( address _user ) external view returns (RewardWithMToken[] memory) { MToken[] memory markets = comptroller.getAllMarkets(); RewardWithMToken[] memory outputData = new RewardWithMToken[]( markets.length ); for (uint256 index = 0; index < markets.length; index++) { RewardInfo[] memory rewardInfo = getOutstandingRewardsForUser( markets[index], _user ); outputData[index] = RewardWithMToken( address(markets[index]), rewardInfo ); } return outputData; } /// @notice A view to enumerate a user's rewards across a specified market and all emission tokens for that market function getOutstandingRewardsForUser( MToken _mToken, address _user ) public view returns (RewardInfo[] memory) { // Global config for this mToken MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; // Output var RewardInfo[] memory outputRewardData = new RewardInfo[](configs.length); // Code golf to avoid too many local vars :rolling-eyes: CalculatedData memory calcData = CalculatedData({ marketData: CurrentMarketData({ totalMTokens: _mToken.totalSupply(), totalBorrows: _mToken.totalBorrows(), marketBorrowIndex: Exp({mantissa: _mToken.borrowIndex()}) }), mTokenInfo: MTokenData({ mTokenBalance: _mToken.balanceOf(_user), borrowBalanceStored: _mToken.borrowBalanceStored(_user) }) }); for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; // Calculate our new global supply index IndexUpdate memory supplyUpdate = calculateNewIndex( emissionConfig.config.supplyEmissionsPerSec, emissionConfig.config.supplyGlobalTimestamp, emissionConfig.config.supplyGlobalIndex, emissionConfig.config.endTime, calcData.marketData.totalMTokens ); // Calculate our new global borrow index IndexUpdate memory borrowUpdate = calculateNewIndex( emissionConfig.config.borrowEmissionsPerSec, emissionConfig.config.borrowGlobalTimestamp, emissionConfig.config.borrowGlobalIndex, emissionConfig.config.endTime, div_( calcData.marketData.totalBorrows, calcData.marketData.marketBorrowIndex ) ); // Calculate outstanding supplier side rewards uint256 supplierRewardsAccrued = calculateSupplyRewardsForUser( emissionConfig, supplyUpdate.newIndex, calcData.mTokenInfo.mTokenBalance, _user ); uint256 borrowerRewardsAccrued = calculateBorrowRewardsForUser( emissionConfig, borrowUpdate.newIndex, calcData.marketData.marketBorrowIndex, calcData.mTokenInfo, _user ); outputRewardData[index] = RewardInfo({ emissionToken: emissionConfig.config.emissionToken, totalAmount: borrowerRewardsAccrued + supplierRewardsAccrued, supplySide: supplierRewardsAccrued, borrowSide: borrowerRewardsAccrued }); } return outputRewardData; } /// @notice A view to get the current emission caps function getCurrentEmissionCap() external view returns (uint256) { return emissionCap; } /// @notice view to get the cached global supply index for an mToken and emission index /// @param mToken The market to get a config for /// @param index The index of the config to get function getGlobalSupplyIndex( address mToken, uint256 index ) public view returns (uint256) { MarketEmissionConfig storage emissionConfig = marketConfigs[mToken][ index ]; // Set the new values in storage return emissionConfig.config.supplyGlobalIndex; } /// @notice view to get the cached global borrow index for an mToken and emission index /// @param mToken The market to get a config for /// @param index The index of the config to get function getGlobalBorrowIndex( address mToken, uint256 index ) public view returns (uint256) { MarketEmissionConfig storage emissionConfig = marketConfigs[mToken][ index ]; // Set the new values in storage return emissionConfig.config.borrowGlobalIndex; } /* ==================================================================================================== Administrative API Should be only callable by the comptroller's admin (usually the timelock), this is the only way to add new configurations to the markets. There's also a rescue assets function that will sweep tokens out of this contract and to the timelock, the thought being that rescuing accidentally sent funds or sweeping existing tokens to a new distributor is possible. ==================================================================================================== */ /** * @notice Add a new emission configuration for a specific market * @dev Emission config must not already exist for the specified market (unique to the emission token) */ function _addEmissionConfig( MToken _mToken, address _owner, address _emissionToken, uint256 _supplyEmissionPerSec, uint256 _borrowEmissionsPerSec, uint256 _endTime ) external onlyComptrollersAdmin { // Ensure market is listed in the comptroller before accepting a config for it (should always be checked // in the comptroller first, but never hurts to codify that assertion/requirement here. (bool tokenIsListed, ) = comptroller.markets(address(_mToken)); require( tokenIsListed, "The market requested to be added is un-listed!" ); // Sanity check emission speeds are below emissionCap require( _supplyEmissionPerSec < emissionCap, "Cannot set a supply reward speed higher than the emission cap!" ); require( _borrowEmissionsPerSec < emissionCap, "Cannot set a borrow reward speed higher than the emission cap!" ); // Sanity check end time is some time in the future require( _endTime > block.timestamp + 1, "The _endTime parameter must be in the future!" ); MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; // Sanity check to ensure that the emission token doesn't already exist in a config for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage mTokenConfig = configs[index]; require( mTokenConfig.config.emissionToken != _emissionToken, "Emission token already listed!" ); } // Things look good, create a config MarketConfig memory config = MarketConfig({ // Set the owner of the reward distributor config owner: _owner, // Set the emission token address emissionToken: _emissionToken, // Set the time that the emission campaign should end at endTime: _endTime, // Initialize the global supply supplyGlobalTimestamp: safe32( block.timestamp, "block timestamp exceeds 32 bits" ), supplyGlobalIndex: initialIndexConstant, // Initialize the global borrow index + timestamp borrowGlobalTimestamp: safe32( block.timestamp, "block timestamp exceeds 32 bits" ), borrowGlobalIndex: initialIndexConstant, // Set supply and reward borrow speeds supplyEmissionsPerSec: _supplyEmissionPerSec, borrowEmissionsPerSec: _borrowEmissionsPerSec }); emit NewConfigCreated( _mToken, _owner, _emissionToken, _supplyEmissionPerSec, _borrowEmissionsPerSec, _endTime ); // Go push in our new config MarketEmissionConfig storage newConfig = configs.push(); newConfig.config = config; } /** * @notice Sweep ERC-20 tokens from the comptroller to the admin * @param _tokenAddress The address of the token to transfer * @param _amount The amount of tokens to sweep, uint256.max means everything */ function _rescueFunds( address _tokenAddress, uint256 _amount ) external onlyComptrollersAdmin { IERC20 token = IERC20(_tokenAddress); // Similar to mTokens, if this is uint256.max that means "transfer everything" if (_amount == type(uint256).max) { token.safeTransfer( comptroller.admin(), token.balanceOf(address(this)) ); } else { token.safeTransfer(comptroller.admin(), _amount); } emit FundsRescued(_tokenAddress, _amount); } /** * @notice Sets a new pause guardian, callable by the CURRENT pause guardian or comptroller's admin * @param _newPauseGuardian The new pause guardian */ function _setPauseGuardian( address _newPauseGuardian ) external onlyPauseGuardianOrAdmin { require( _newPauseGuardian != address(0), "Pause Guardian can't be the 0 address!" ); address currentPauseGuardian = pauseGuardian; pauseGuardian = _newPauseGuardian; emit NewPauseGuardian(currentPauseGuardian, _newPauseGuardian); } /** * @notice Sets a new emission cap for supply/borrow speeds * @param _newEmissionCap The new emission cap */ function _setEmissionCap( uint256 _newEmissionCap ) external onlyComptrollersAdmin { uint256 oldEmissionCap = emissionCap; emissionCap = _newEmissionCap; emit NewEmissionCap(oldEmissionCap, _newEmissionCap); } /* ==================================================================================================== Comptroller specific API This is the main integration points with the Moonwell Comptroller. Within the `allowMint`/`allowBorrow`/etc hooks, the comptroller will reach out to kick the global index update (updateMarketIndex) as well as update the supplier's/borrower's token specific distribution indices for that market ==================================================================================================== */ /** * @notice Updates the supply indices for a given market * @param _mToken The market to update */ function updateMarketSupplyIndex( MToken _mToken ) external onlyComptrollerOrAdmin { updateMarketSupplyIndexInternal(_mToken); } /** * @notice Calculate the deltas in indices between this user's index and the global supplier index for all configs, * and accrue any owed emissions to their supplierRewardsAccrued for this market's configs * @param _mToken The market to update * @param _supplier The supplier whose index will be updated * @param _sendTokens Whether to send tokens as part of calculating owed rewards */ function disburseSupplierRewards( MToken _mToken, address _supplier, bool _sendTokens ) external onlyComptrollerOrAdmin { disburseSupplierRewardsInternal(_mToken, _supplier, _sendTokens); } /** * @notice Combine the above 2 functions into one that will update the global and user supplier indexes and * disburse rewards * @param _mToken The market to update * @param _supplier The supplier whose index will be updated * @param _sendTokens Whether to send tokens as part of calculating owed rewards */ function updateMarketSupplyIndexAndDisburseSupplierRewards( MToken _mToken, address _supplier, bool _sendTokens ) external onlyComptrollerOrAdmin { updateMarketSupplyIndexInternal(_mToken); disburseSupplierRewardsInternal(_mToken, _supplier, _sendTokens); } /** * @notice Updates the borrow indices for a given market * @param _mToken The market to update */ function updateMarketBorrowIndex( MToken _mToken ) external onlyComptrollerOrAdmin { updateMarketBorrowIndexInternal(_mToken); } /** * @notice Calculate the deltas in indices between this user's index and the global borrower index for all configs, * and accrue any owed emissions to their borrowerRewardsAccrued for this market's configs * @param _mToken The market to update * @param _borrower The borrower whose index will be updated * @param _sendTokens Whether to send tokens as part of calculating owed rewards */ function disburseBorrowerRewards( MToken _mToken, address _borrower, bool _sendTokens ) external onlyComptrollerOrAdmin { disburseBorrowerRewardsInternal(_mToken, _borrower, _sendTokens); } /** * @notice Combine the above 2 functions into one that will update the global and user borrower indexes and * disburse rewards * @param _mToken The market to update * @param _borrower The borrower whose index will be updated * @param _sendTokens Whether to send tokens as part of calculating owed rewards */ function updateMarketBorrowIndexAndDisburseBorrowerRewards( MToken _mToken, address _borrower, bool _sendTokens ) external onlyComptrollerOrAdmin { updateMarketBorrowIndexInternal(_mToken); disburseBorrowerRewardsInternal(_mToken, _borrower, _sendTokens); } /* ==================================================================================================== Pause Guardian API The pause guardian tooling is responsible for toggling on and off actual reward emissions. Things will still be accrued as normal, but the `sendRewards` function will simply not attempt to transfer any tokens out. Similarly to the pause guardians in the Comptroller, when the pause guardian pops this circuit breaker, only the comptroller's admin is able to unpause things and get tokens emitting again. ==================================================================================================== */ /// @notice Pauses reward sending *but not accrual* function _pauseRewards() external onlyPauseGuardianOrAdmin { _pause(); emit RewardsPaused(); } /// @notice Unpauses and allows reward sending once again function _unpauseRewards() external onlyComptrollersAdmin { _unpause(); emit RewardsUnpaused(); } /* ==================================================================================================== Configuration Owner API This is a set of APIs for external parties/emission config owners to update their configs. They're able to transfer ownership, update emission speeds, and update the end time for a campaign. Worth noting, if the endTime is hit, no more rewards will be accrued, BUT you can call `_updateEndTime` to extend the specified campaign - if the campaign has ended already, then rewards will start accruing from the time of reactivation. ==================================================================================================== */ /** * @notice Update the supply emissions for a given mToken + emission token pair. * @param _mToken The market to change a config for * @param _emissionToken The underlying reward token address * @param _newSupplySpeed The supply side emission speed denoted in the underlying emission token's decimals */ function _updateSupplySpeed( MToken _mToken, address _emissionToken, uint256 _newSupplySpeed ) external onlyEmissionConfigOwnerOrAdmin(_mToken, _emissionToken) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); uint256 currentSupplySpeed = emissionConfig .config .supplyEmissionsPerSec; require( _newSupplySpeed != currentSupplySpeed, "Can't set new supply emissions to be equal to current!" ); require( _newSupplySpeed < emissionCap, "Cannot set a supply reward speed higher than the emission cap!" ); // Make sure we update our indices before setting the new speed updateMarketSupplyIndexInternal(_mToken); // Update supply speed emissionConfig.config.supplyEmissionsPerSec = _newSupplySpeed; emit NewSupplyRewardSpeed( _mToken, _emissionToken, currentSupplySpeed, _newSupplySpeed ); } /** * @notice Update the borrow emissions for a given mToken + emission token pair. * @param _mToken The market to change a config for * @param _emissionToken The underlying reward token address * @param _newBorrowSpeed The borrow side emission speed denoted in the underlying emission token's decimals */ function _updateBorrowSpeed( MToken _mToken, address _emissionToken, uint256 _newBorrowSpeed ) external onlyEmissionConfigOwnerOrAdmin(_mToken, _emissionToken) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); uint256 currentBorrowSpeed = emissionConfig .config .borrowEmissionsPerSec; require( _newBorrowSpeed != currentBorrowSpeed, "Can't set new borrow emissions to be equal to current!" ); require( _newBorrowSpeed < emissionCap, "Cannot set a borrow reward speed higher than the emission cap!" ); // Make sure we update our indices before setting the new speed updateMarketBorrowIndexInternal(_mToken); // Update borrow speed emissionConfig.config.borrowEmissionsPerSec = _newBorrowSpeed; emit NewBorrowRewardSpeed( _mToken, _emissionToken, currentBorrowSpeed, _newBorrowSpeed ); } /** * @notice Update the owner of a config * @param _mToken The market to change a config for * @param _emissionToken The underlying reward token address * @param _newOwner The new owner for this config */ function _updateOwner( MToken _mToken, address _emissionToken, address _newOwner ) external onlyEmissionConfigOwnerOrAdmin(_mToken, _emissionToken) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); address currentOwner = emissionConfig.config.owner; emissionConfig.config.owner = _newOwner; emit NewEmissionConfigOwner( _mToken, _emissionToken, currentOwner, _newOwner ); } /** * @notice Update the end time for an emission campaign, must be in the future * @param _mToken The market to change a config for * @param _emissionToken The underlying reward token address * @param _newEndTime The new desired end time */ function _updateEndTime( MToken _mToken, address _emissionToken, uint256 _newEndTime ) external onlyEmissionConfigOwnerOrAdmin(_mToken, _emissionToken) { MarketEmissionConfig storage emissionConfig = fetchConfigByEmissionToken( _mToken, _emissionToken ); uint256 currentEndTime = emissionConfig.config.endTime; // Must be older than our existing end time AND the current block require( _newEndTime > currentEndTime, "_newEndTime MUST be > currentEndTime" ); require( _newEndTime > block.timestamp, "_newEndTime MUST be > block.timestamp" ); // Update both global indices before setting the new end time. If rewards are off this just updates the // global block timestamp to the current second updateMarketBorrowIndexInternal(_mToken); updateMarketSupplyIndexInternal(_mToken); emissionConfig.config.endTime = _newEndTime; emit NewRewardEndTime( _mToken, _emissionToken, currentEndTime, _newEndTime ); } /* ==================================================================================================== Internal functions Internal functions used by other parts of this contract, views first then mutation functions ==================================================================================================== */ /** * @notice An internal view to calculate the total owed supplier rewards for a given supplier address * @param _emissionConfig The emission config to read index data from * @param _globalSupplyIndex The global supply index for a market * @param _supplierTokens The amount of this market's mTokens owned by a user * @param _supplier The address of the supplier */ function calculateSupplyRewardsForUser( MarketEmissionConfig storage _emissionConfig, uint224 _globalSupplyIndex, uint256 _supplierTokens, address _supplier ) internal view returns (uint256) { uint256 userSupplyIndex = _emissionConfig.supplierIndices[_supplier]; // If our user's index isn't set yet, set to the current global supply index if ( userSupplyIndex == 0 && _globalSupplyIndex >= initialIndexConstant ) { userSupplyIndex = initialIndexConstant; //_globalSupplyIndex; } // Calculate change in the cumulative sum of the reward per cToken accrued Double memory deltaIndex = Double({ mantissa: sub_(_globalSupplyIndex, userSupplyIndex) }); // Calculate reward accrued: cTokenAmount * accruedPerCToken uint256 supplierDelta = mul_(_supplierTokens, deltaIndex); return add_( _emissionConfig.supplierRewardsAccrued[_supplier], supplierDelta ); } /** * @notice An internal view to calculate the total owed borrower rewards for a given borrower address * @param _emissionConfig The emission config to read index data from * @param _globalBorrowIndex The global borrow index for a market * @param _marketBorrowIndex The mToken's borrowIndex * @param _mTokenData A struct holding a borrower's * @param _borrower The address of the supplier mToken balance and borrowed balance */ function calculateBorrowRewardsForUser( MarketEmissionConfig storage _emissionConfig, uint224 _globalBorrowIndex, Exp memory _marketBorrowIndex, MTokenData memory _mTokenData, address _borrower ) internal view returns (uint256) { uint256 userBorrowIndex = _emissionConfig.borrowerIndices[_borrower]; // If our user's index isn't set yet, set to the current global borrow index if ( userBorrowIndex == 0 && _globalBorrowIndex >= initialIndexConstant ) { userBorrowIndex = initialIndexConstant; //userBorrowIndex = _globalBorrowIndex; } // Calculate change in the cumulative sum of the reward per cToken accrued Double memory deltaIndex = Double({ mantissa: sub_(_globalBorrowIndex, userBorrowIndex) }); uint borrowerAmount = div_( _mTokenData.borrowBalanceStored, _marketBorrowIndex ); // Calculate reward accrued: mTokenAmount * accruedPerMToken uint borrowerDelta = mul_(borrowerAmount, deltaIndex); return add_( _emissionConfig.borrowerRewardsAccrued[_borrower], borrowerDelta ); } /** * @notice An internal view to calculate the global reward indices while taking into account emissions end times. * @dev Denominator here is whatever fractional denominator is used to calculate the index. On the supply side * it's simply mToken.totalSupply(), while on the borrow side it's (mToken.totalBorrows() / mToken.borrowIndex()) * @param _emissionsPerSecond The configured emissions per second for this index * @param _currentTimestamp The current index timestamp * @param _currentIndex The current index * @param _rewardEndTime The end time for this reward config * @param _denominator The denominator used in the calculation (supply side == mToken.totalSupply, * borrow side is (mToken.totalBorrows() / mToken.borrowIndex()). */ function calculateNewIndex( uint256 _emissionsPerSecond, uint32 _currentTimestamp, uint224 _currentIndex, uint256 _rewardEndTime, uint256 _denominator ) internal view returns (IndexUpdate memory) { uint32 blockTimestamp = safe32( block.timestamp, "block timestamp exceeds 32 bits" ); uint256 deltaTimestamps = sub_( blockTimestamp, uint256(_currentTimestamp) ); // If our current block timestamp is newer than our emission end time, we need to halt // reward emissions by stinting the growth of the global index, but importantly not // the global timestamp. Should not be gte because the equivalent case makes a // 0 deltaTimestamp which doesn't accrue the last bit of rewards properly. if (blockTimestamp > _rewardEndTime) { // If our current index timestamp is less than our end time it means this // is the first time the endTime threshold has been breached, and we have // some left over rewards to accrue, so clamp deltaTimestamps to the whatever // window of rewards still remains. if (_currentTimestamp < _rewardEndTime) { deltaTimestamps = sub_(_rewardEndTime, _currentTimestamp); } else { // Otherwise just set deltaTimestamps to 0 to ensure that we short circuit // in the next step deltaTimestamps = 0; } } // Short circuit to update the timestamp but *not* the index if there's nothing // to calculate if (deltaTimestamps == 0 || _emissionsPerSecond == 0) { return IndexUpdate({ newIndex: _currentIndex, newTimestamp: blockTimestamp }); } // At this point we know we have to calculate a new index, so do so uint256 tokenAccrued = mul_(deltaTimestamps, _emissionsPerSecond); Double memory ratio = _denominator > 0 ? fraction(tokenAccrued, _denominator) : Double({mantissa: 0}); uint224 newIndex = safe224( add_(Double({mantissa: _currentIndex}), ratio).mantissa, "new index exceeds 224 bits" ); return IndexUpdate({newIndex: newIndex, newTimestamp: blockTimestamp}); } /** * @notice An internal view to find a config for a given market given a specific emission token * @dev Reverts if the mtoken + emission token combo could not be found. * @param _mToken The market to fetch a config for * @param _emissionToken The emission token to fetch a config for */ function fetchConfigByEmissionToken( MToken _mToken, address _emissionToken ) internal view returns (MarketEmissionConfig storage) { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; if (emissionConfig.config.emissionToken == _emissionToken) { return emissionConfig; } } revert("Unable to find emission token in mToken configs"); } // // Internal mutable functions // /** * @notice An internal function to update the global supply index for a given mToken * @param _mToken The market to update the global supply index for */ function updateMarketSupplyIndexInternal(MToken _mToken) internal { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; uint256 totalMTokens = MTokenInterface(_mToken).totalSupply(); // Iterate over all market configs and update their indexes + timestamps for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; // Go calculate our new values IndexUpdate memory supplyUpdate = calculateNewIndex( emissionConfig.config.supplyEmissionsPerSec, emissionConfig.config.supplyGlobalTimestamp, emissionConfig.config.supplyGlobalIndex, emissionConfig.config.endTime, totalMTokens ); // Set the new values in storage emissionConfig.config.supplyGlobalIndex = supplyUpdate.newIndex; emissionConfig.config.supplyGlobalTimestamp = supplyUpdate .newTimestamp; emit GlobalSupplyIndexUpdated( _mToken, emissionConfig.config.emissionToken, supplyUpdate.newIndex, supplyUpdate.newTimestamp ); } } /** * @notice An internal function to disburse rewards for the supplier side of a a specific mToken * @dev will only send tokens when _sendTokens == true, otherwise just accrue rewards * @param _mToken The market to update the global supply index for * @param _supplier The supplier to disburse rewards for * @param _sendTokens Whether to actually send tokens instead of just accruing */ function disburseSupplierRewardsInternal( MToken _mToken, address _supplier, bool _sendTokens ) internal { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; uint256 supplierTokens = _mToken.balanceOf(_supplier); // Iterate over all market configs and update their indexes + timestamps for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; uint256 totalRewardsOwed = calculateSupplyRewardsForUser( emissionConfig, emissionConfig.config.supplyGlobalIndex, supplierTokens, _supplier ); // Update user's index to match global index emissionConfig.supplierIndices[_supplier] = emissionConfig .config .supplyGlobalIndex; // Update the user's total rewards owed emissionConfig.supplierRewardsAccrued[_supplier] = totalRewardsOwed; emit DisbursedSupplierRewards( _mToken, _supplier, emissionConfig.config.emissionToken, emissionConfig.supplierRewardsAccrued[_supplier] ); // SendRewards will attempt to send only if it has enough emission tokens to do so, // and if it doesn't have enough it emits a InsufficientTokensToEmit event and returns // the rewards that couldn't be sent, which are the total of what a user is owed, so we // store it in supplierRewardsAccrued to make sure we don't lose rewards accrual if there's // not enough funds in the rewarder if (_sendTokens) { // Emit rewards for this token/pair uint256 unsendableRewards = sendReward( payable(_supplier), emissionConfig.supplierRewardsAccrued[_supplier], emissionConfig.config.emissionToken ); emissionConfig.supplierRewardsAccrued[ _supplier ] = unsendableRewards; } } } /** * @notice An internal function to update the global borrow index for a given mToken * @param _mToken The market to update the global borrow index for */ function updateMarketBorrowIndexInternal(MToken _mToken) internal { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; Exp memory marketBorrowIndex = Exp({ mantissa: MToken(_mToken).borrowIndex() }); uint256 totalBorrows = MToken(_mToken).totalBorrows(); // Iterate over all market configs and update their indexes + timestamps for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; // Go calculate our new borrow index IndexUpdate memory borrowIndexUpdate = calculateNewIndex( emissionConfig.config.borrowEmissionsPerSec, emissionConfig.config.borrowGlobalTimestamp, emissionConfig.config.borrowGlobalIndex, emissionConfig.config.endTime, div_(totalBorrows, marketBorrowIndex) ); // Set the new values in storage emissionConfig.config.borrowGlobalIndex = borrowIndexUpdate .newIndex; emissionConfig.config.borrowGlobalTimestamp = borrowIndexUpdate .newTimestamp; // Emit an update emit GlobalBorrowIndexUpdated( _mToken, emissionConfig.config.emissionToken, emissionConfig.config.borrowGlobalIndex, emissionConfig.config.borrowGlobalTimestamp ); } } /** * @notice An internal function to disburse rewards for the borrower side of a a specific mToken * @dev will only send tokens when _sendTokens == true, otherwise just accrue rewards * @param _mToken The market to update the global borrow index for * @param _borrower The borrower to disburse rewards for * @param _sendTokens Whether to actually send tokens instead of just accruing */ function disburseBorrowerRewardsInternal( MToken _mToken, address _borrower, bool _sendTokens ) internal { MarketEmissionConfig[] storage configs = marketConfigs[ address(_mToken) ]; Exp memory marketBorrowIndex = Exp({mantissa: _mToken.borrowIndex()}); MTokenData memory mTokenData = MTokenData({ mTokenBalance: _mToken.balanceOf(_borrower), borrowBalanceStored: _mToken.borrowBalanceStored(_borrower) }); // Iterate over all market configs and update their indexes + timestamps for (uint256 index = 0; index < configs.length; index++) { MarketEmissionConfig storage emissionConfig = configs[index]; // Go calculate the total outstanding rewards for this user uint256 owedRewards = calculateBorrowRewardsForUser( emissionConfig, emissionConfig.config.borrowGlobalIndex, marketBorrowIndex, mTokenData, _borrower ); // Update user's index to global index emissionConfig.borrowerIndices[_borrower] = emissionConfig .config .borrowGlobalIndex; // Update the accrued borrow side rewards for this user emissionConfig.borrowerRewardsAccrued[_borrower] = owedRewards; emit DisbursedBorrowerRewards( _mToken, _borrower, emissionConfig.config.emissionToken, emissionConfig.borrowerRewardsAccrued[_borrower] ); // If we are instructed to send out rewards, do so and update the borrowerRewardsAccrued to // 0 if it was successful, or to `pendingRewards` if there was insufficient balance to send if (_sendTokens) { // Emit rewards for this token/pair uint256 pendingRewards = sendReward( payable(_borrower), emissionConfig.borrowerRewardsAccrued[_borrower], emissionConfig.config.emissionToken ); emissionConfig.borrowerRewardsAccrued[ _borrower ] = pendingRewards; } } } /** * @notice An internal function to send rewards to a user * @dev Non-reentrant and returns the amount of tokens that were successfully sent * @param _user The user address to send tokens to * @param _amount The amount of tokens to send * @param _rewardToken The reward token to send */ function sendReward( address payable _user, uint256 _amount, address _rewardToken ) internal nonReentrant returns (uint256) { // Short circuit if we don't have anything to send out if (_amount == 0) { return _amount; } // If pause guardian is active, bypass all token transfers, but still accrue to local tally if (paused()) { return _amount; } IERC20 token = IERC20(_rewardToken); // Get the distributor's current balance uint256 currentTokenHoldings = token.balanceOf(address(this)); // Only transfer out if we have enough of a balance to cover it (otherwise just accrue without sending) if (_amount > 0 && _amount <= currentTokenHoldings) { // Ensure we use SafeERC20 to revert even if the reward token isn't ERC20 compliant token.safeTransfer(_user, _amount); return 0; } else { // If we've hit here it means we weren't able to emit the reward and we should emit an event // instead of failing. emit InsufficientTokensToEmit(_user, _rewardToken, _amount); // By default, return the same amount as what's left over to send, we accrue reward but don't send them out return _amount; } } }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "../MToken.sol"; // The commonly structures and events for the MultiRewardDistributor interface MultiRewardDistributorCommon { struct MarketConfig { // The owner/admin of the emission config address owner; // The emission token address emissionToken; // Scheduled to end at this time uint endTime; // Supplier global state uint224 supplyGlobalIndex; uint32 supplyGlobalTimestamp; // Borrower global state uint224 borrowGlobalIndex; uint32 borrowGlobalTimestamp; uint supplyEmissionsPerSec; uint borrowEmissionsPerSec; } struct MarketEmissionConfig { MarketConfig config; mapping(address => uint) supplierIndices; mapping(address => uint) supplierRewardsAccrued; mapping(address => uint) borrowerIndices; mapping(address => uint) borrowerRewardsAccrued; } struct RewardInfo { address emissionToken; uint totalAmount; uint supplySide; uint borrowSide; } struct IndexUpdate { uint224 newIndex; uint32 newTimestamp; } struct MTokenData { uint mTokenBalance; uint borrowBalanceStored; } struct RewardWithMToken { address mToken; RewardInfo[] rewards; } // Global index updates event GlobalSupplyIndexUpdated( MToken mToken, address emissionToken, uint newSupplyIndex, uint32 newSupplyGlobalTimestamp ); event GlobalBorrowIndexUpdated( MToken mToken, address emissionToken, uint newIndex, uint32 newTimestamp ); // Reward Disbursal event DisbursedSupplierRewards( MToken indexed mToken, address indexed supplier, address indexed emissionToken, uint totalAccrued ); event DisbursedBorrowerRewards( MToken indexed mToken, address indexed borrower, address indexed emissionToken, uint totalAccrued ); // Admin update events event NewConfigCreated( MToken indexed mToken, address indexed owner, address indexed emissionToken, uint supplySpeed, uint borrowSpeed, uint endTime ); event NewPauseGuardian(address oldPauseGuardian, address newPauseGuardian); event NewEmissionCap(uint oldEmissionCap, uint newEmissionCap); event NewEmissionConfigOwner( MToken indexed mToken, address indexed emissionToken, address currentOwner, address newOwner ); event NewRewardEndTime( MToken indexed mToken, address indexed emissionToken, uint currentEndTime, uint newEndTime ); event NewSupplyRewardSpeed( MToken indexed mToken, address indexed emissionToken, uint oldRewardSpeed, uint newRewardSpeed ); event NewBorrowRewardSpeed( MToken indexed mToken, address indexed emissionToken, uint oldRewardSpeed, uint newRewardSpeed ); event FundsRescued(address token, uint amount); // Pause guardian stuff event RewardsPaused(); event RewardsUnpaused(); // Errors event InsufficientTokensToEmit( address payable user, address rewardToken, uint amount ); }
// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.19; import "./ErrorReporter.sol"; import "./ComptrollerStorage.sol"; /** * @title ComptrollerCore * @dev Storage for the comptroller is at this address, while execution is delegated to the `comptrollerImplementation`. * MTokens should reference this contract as their comptroller. */ contract Unitroller is UnitrollerAdminStorage, ComptrollerErrorReporter { /** * @notice Emitted when pendingComptrollerImplementation is changed */ event NewPendingImplementation( address oldPendingImplementation, address newPendingImplementation ); /** * @notice Emitted when pendingComptrollerImplementation is accepted, which means comptroller implementation is updated */ event NewImplementation( address oldImplementation, address newImplementation ); /** * @notice Emitted when pendingAdmin is changed */ event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); /** * @notice Emitted when pendingAdmin is accepted, which means admin is updated */ event NewAdmin(address oldAdmin, address newAdmin); constructor() { // Set admin to caller admin = msg.sender; } /*** Admin Functions ***/ function _setPendingImplementation( address newPendingImplementation ) public returns (uint) { if (msg.sender != admin) { return fail( Error.UNAUTHORIZED, FailureInfo.SET_PENDING_IMPLEMENTATION_OWNER_CHECK ); } address oldPendingImplementation = pendingComptrollerImplementation; pendingComptrollerImplementation = newPendingImplementation; emit NewPendingImplementation( oldPendingImplementation, pendingComptrollerImplementation ); return uint(Error.NO_ERROR); } /** * @notice Accepts new implementation of comptroller. msg.sender must be pendingImplementation * @dev Admin function for new implementation to accept it's role as implementation * @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details) */ function _acceptImplementation() public returns (uint) { // Check caller is pendingImplementation and pendingImplementation �� address(0) if ( msg.sender != pendingComptrollerImplementation || pendingComptrollerImplementation == address(0) ) { return fail( Error.UNAUTHORIZED, FailureInfo.ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK ); } // Save current values for inclusion in log address oldImplementation = comptrollerImplementation; address oldPendingImplementation = pendingComptrollerImplementation; comptrollerImplementation = pendingComptrollerImplementation; pendingComptrollerImplementation = address(0); emit NewImplementation(oldImplementation, comptrollerImplementation); emit NewPendingImplementation( oldPendingImplementation, pendingComptrollerImplementation ); return uint(Error.NO_ERROR); } /** * @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 newPendingAdmin) public 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() public 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); } /** * @dev Delegates execution to an implementation contract. * It returns to the external caller whatever the implementation returns * or forwards reverts. */ fallback() external { // delegate all other functions to current implementation (bool success, ) = comptrollerImplementation.delegatecall(msg.data); assembly { let free_mem_ptr := mload(0x40) returndatacopy(free_mem_ptr, 0, returndatasize()) switch success case 0 { revert(free_mem_ptr, returndatasize()) } default { return(free_mem_ptr, returndatasize()) } } } }
{ "optimizer": { "enabled": true, "runs": 200 }, "evmVersion": "paris", "viaIR": false, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"action","type":"string"},{"indexed":false,"internalType":"bool","name":"pauseState","type":"bool"}],"name":"ActionPaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"string","name":"action","type":"string"},{"indexed":false,"internalType":"bool","name":"pauseState","type":"bool"}],"name":"ActionPaused","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":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"MarketEntered","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"MarketExited","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MToken","name":"mToken","type":"address"}],"name":"MarketListed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"newBorrowCap","type":"uint256"}],"name":"NewBorrowCap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldBorrowCapGuardian","type":"address"},{"indexed":false,"internalType":"address","name":"newBorrowCapGuardian","type":"address"}],"name":"NewBorrowCapGuardian","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldCloseFactorMantissa","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newCloseFactorMantissa","type":"uint256"}],"name":"NewCloseFactor","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"oldCollateralFactorMantissa","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newCollateralFactorMantissa","type":"uint256"}],"name":"NewCollateralFactor","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldLiquidationIncentiveMantissa","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newLiquidationIncentiveMantissa","type":"uint256"}],"name":"NewLiquidationIncentive","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldPauseGuardian","type":"address"},{"indexed":false,"internalType":"address","name":"newPauseGuardian","type":"address"}],"name":"NewPauseGuardian","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract PriceOracle","name":"oldPriceOracle","type":"address"},{"indexed":false,"internalType":"contract PriceOracle","name":"newPriceOracle","type":"address"}],"name":"NewPriceOracle","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MultiRewardDistributor","name":"oldRewardDistributor","type":"address"},{"indexed":false,"internalType":"contract MultiRewardDistributor","name":"newRewardDistributor","type":"address"}],"name":"NewRewardDistributor","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract MToken","name":"mToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"newSupplyCap","type":"uint256"}],"name":"NewSupplyCap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldSupplyCapGuardian","type":"address"},{"indexed":false,"internalType":"address","name":"newSupplyCapGuardian","type":"address"}],"name":"NewSupplyCapGuardian","type":"event"},{"inputs":[{"internalType":"contract Unitroller","name":"unitroller","type":"address"}],"name":"_become","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"_rescueFunds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newBorrowCapGuardian","type":"address"}],"name":"_setBorrowCapGuardian","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken","name":"mToken","type":"address"},{"internalType":"bool","name":"state","type":"bool"}],"name":"_setBorrowPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newCloseFactorMantissa","type":"uint256"}],"name":"_setCloseFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken","name":"mToken","type":"address"},{"internalType":"uint256","name":"newCollateralFactorMantissa","type":"uint256"}],"name":"_setCollateralFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newLiquidationIncentiveMantissa","type":"uint256"}],"name":"_setLiquidationIncentive","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken[]","name":"mTokens","type":"address[]"},{"internalType":"uint256[]","name":"newBorrowCaps","type":"uint256[]"}],"name":"_setMarketBorrowCaps","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken[]","name":"mTokens","type":"address[]"},{"internalType":"uint256[]","name":"newSupplyCaps","type":"uint256[]"}],"name":"_setMarketSupplyCaps","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken","name":"mToken","type":"address"},{"internalType":"bool","name":"state","type":"bool"}],"name":"_setMintPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newPauseGuardian","type":"address"}],"name":"_setPauseGuardian","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract PriceOracle","name":"newOracle","type":"address"}],"name":"_setPriceOracle","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MultiRewardDistributor","name":"newRewardDistributor","type":"address"}],"name":"_setRewardDistributor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"state","type":"bool"}],"name":"_setSeizePaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newSupplyCapGuardian","type":"address"}],"name":"_setSupplyCapGuardian","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"state","type":"bool"}],"name":"_setTransferPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract MToken","name":"mToken","type":"address"}],"name":"_supportMarket","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"accountAssets","outputs":[{"internalType":"contract MToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allMarkets","outputs":[{"internalType":"contract MToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"borrowAmount","type":"uint256"}],"name":"borrowAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"borrowCapGuardian","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"borrowCaps","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"borrowGuardianPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"contract MToken","name":"mToken","type":"address"}],"name":"checkMembership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"holders","type":"address[]"},{"internalType":"contract MToken[]","name":"mTokens","type":"address[]"},{"internalType":"bool","name":"borrowers","type":"bool"},{"internalType":"bool","name":"suppliers","type":"bool"}],"name":"claimReward","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"holder","type":"address"},{"internalType":"contract MToken[]","name":"mTokens","type":"address[]"}],"name":"claimReward","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claimReward","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"holder","type":"address"}],"name":"claimReward","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"closeFactorMantissa","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"comptrollerImplementation","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"mTokens","type":"address[]"}],"name":"enterMarkets","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"mTokenAddress","type":"address"}],"name":"exitMarket","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"getAccountLiquidity","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAllMarkets","outputs":[{"internalType":"contract MToken[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"getAssetsIn","outputs":[{"internalType":"contract MToken[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBlockTimestamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"mTokenModify","type":"address"},{"internalType":"uint256","name":"redeemTokens","type":"uint256"},{"internalType":"uint256","name":"borrowAmount","type":"uint256"}],"name":"getHypotheticalAccountLiquidity","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isComptroller","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mTokenBorrowed","type":"address"},{"internalType":"address","name":"mTokenCollateral","type":"address"},{"internalType":"address","name":"liquidator","type":"address"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"repayAmount","type":"uint256"}],"name":"liquidateBorrowAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mTokenBorrowed","type":"address"},{"internalType":"address","name":"mTokenCollateral","type":"address"},{"internalType":"uint256","name":"actualRepayAmount","type":"uint256"}],"name":"liquidateCalculateSeizeTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"liquidationIncentiveMantissa","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"markets","outputs":[{"internalType":"bool","name":"isListed","type":"bool"},{"internalType":"uint256","name":"collateralFactorMantissa","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"minter","type":"address"},{"internalType":"uint256","name":"mintAmount","type":"uint256"}],"name":"mintAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"mintGuardianPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"oracle","outputs":[{"internalType":"contract PriceOracle","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pauseGuardian","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingAdmin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingComptrollerImplementation","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"redeemer","type":"address"},{"internalType":"uint256","name":"redeemTokens","type":"uint256"}],"name":"redeemAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"redeemer","type":"address"},{"internalType":"uint256","name":"redeemAmount","type":"uint256"},{"internalType":"uint256","name":"redeemTokens","type":"uint256"}],"name":"redeemVerify","outputs":[],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"payer","type":"address"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"repayAmount","type":"uint256"}],"name":"repayBorrowAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardDistributor","outputs":[{"internalType":"contract MultiRewardDistributor","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mTokenCollateral","type":"address"},{"internalType":"address","name":"mTokenBorrowed","type":"address"},{"internalType":"address","name":"liquidator","type":"address"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"seizeTokens","type":"uint256"}],"name":"seizeAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"seizeGuardianPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"supplyCapGuardian","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"supplyCaps","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"mToken","type":"address"},{"internalType":"address","name":"src","type":"address"},{"internalType":"address","name":"dst","type":"address"},{"internalType":"uint256","name":"transferTokens","type":"uint256"}],"name":"transferAllowed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"transferGuardianPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]
Contract Creation Code
608060405234801561001057600080fd5b50600080546001600160a01b031916331790556144b1806100326000396000f3fe608060405234801561001057600080fd5b50600436106103985760003560e01c80636d154ea5116101e9578063bb82aa5e1161010f578063dbc7779b116100ad578063e87554461161007c578063e87554461461089c578063eabe7d91146108a5578063ede4edd0146108b8578063f851a440146108cb57600080fd5b8063dbc7779b14610850578063dce1544914610863578063dcfbc0c714610876578063e4028eee1461088957600080fd5b8063c488847b116100e9578063c488847b146107ef578063d02f735114610817578063d279c1911461082a578063da3d454c1461083d57600080fd5b8063bb82aa5e146107a9578063bdcdc258146107bc578063c2998238146107cf57600080fd5b8063929fe9a111610187578063ac0b0bb711610156578063ac0b0bb714610772578063acc2166a14610786578063b0772d0b14610799578063b88a802f146107a157600080fd5b8063929fe9a1146106eb578063a76b3fda1461072c578063a979f0c51461073f578063abfceffc1461075257600080fd5b80637dc0d1d0116101c35780637dc0d1d01461066d57806387f76303146106805780638e8f294b146106945780638ebf6364146106d857600080fd5b80636d154ea514610621578063731f0c2b14610644578063796b89b91461066757600080fd5b80633bcf7ec1116102ce57806351dff9891161026c5780635ec88c791161023b5780635ec88c79146105d55780635f5af1aa146105e85780635fc7e71e146105fb578063607ef6c11461060e57600080fd5b806351dff9891461058957806352d84d1e1461059c57806355ee1fe1146105af5780635c254d11146105c257600080fd5b80634e79238f116102a85780634e79238f146105225780634ef4c3e1146105505780634fd42e171461056357806351a485e41461057657600080fd5b80633bcf7ec1146104e65780634a584432146104f95780634ada90af1461051957600080fd5b806324a3d6221161033b578063317b0b7711610315578063317b0b771461049a5780633685ffe7146104ad57806338b8f4c3146104c0578063391957d7146104d357600080fd5b806324a3d6221461046157806326782247146104745780632d70db781461048757600080fd5b806318c882a51161037757806318c882a5146103fd5780631d504dc61461041057806321af45691461042357806324008a621461044e57600080fd5b80627e3dd21461039d57806302c3bcbb146103ba578063114b9d19146103e8575b600080fd5b6103a5600181565b60405190151581526020015b60405180910390f35b6103da6103c8366004613c1d565b60116020526000908152604090205481565b6040519081526020016103b1565b6103fb6103f6366004613d8b565b6108de565b005b6103a561040b366004613e14565b610c0c565b6103fb61041e366004613c1d565b610d45565b600d54610436906001600160a01b031681565b6040516001600160a01b0390911681526020016103b1565b6103da61045c366004613e4d565b610ec3565b600954610436906001600160a01b031681565b600154610436906001600160a01b031681565b6103a5610495366004613e9e565b610f01565b6103da6104a8366004613ebb565b610ff1565b6103fb6104bb366004613ed4565b611098565b6103fb6104ce366004613c1d565b611100565b6103fb6104e1366004613c1d565b6111cb565b6103a56104f4366004613e14565b61128e565b6103da610507366004613c1d565b600e6020526000908152604090205481565b6103da60065481565b610535610530366004613f24565b6113b8565b604080519384526020840192909252908201526060016103b1565b6103da61055e366004613f6a565b6113f8565b6103da610571366004613ebb565b61165d565b6103fb610584366004613ff7565b6116ba565b6103fb610597366004613f24565b611898565b6104366105aa366004613ebb565b6118ed565b6103da6105bd366004613c1d565b611917565b6103fb6105d0366004613c1d565b611990565b6105356105e3366004613c1d565b611a33565b6103da6105f6366004613c1d565b611a6e565b6103da610609366004614063565b611ae7565b6103fb61061c366004613ff7565b611c41565b6103a561062f366004613c1d565b600b6020526000908152604090205460ff1681565b6103a5610652366004613c1d565b600a6020526000908152604090205460ff1681565b426103da565b600454610436906001600160a01b031681565b6009546103a590600160a01b900460ff1681565b6106c16106a2366004613c1d565b6008602052600090815260409020805460019091015460ff9091169082565b6040805192151583526020830191909152016103b1565b6103a56106e6366004613e9e565b611e16565b6103a56106f93660046140c7565b6001600160a01b038082166000908152600860209081526040808320938616835260029093019052205460ff1692915050565b6103da61073a366004613c1d565b611efd565b601054610436906001600160a01b031681565b610765610760366004613c1d565b612058565b6040516103b191906140f5565b6009546103a590600160a81b900460ff1681565b601254610436906001600160a01b031681565b6107656120ce565b6103fb612130565b600254610436906001600160a01b031681565b6103da6107ca366004613e4d565b612196565b6107e26107dd366004614142565b612224565b6040516103b19190614177565b6108026107fd366004613f6a565b6122ec565b604080519283526020830191909152016103b1565b6103da610825366004614063565b61250a565b6103fb610838366004613c1d565b612692565b6103da61084b366004613f6a565b6126f4565b6103fb61085e3660046141af565b612a48565b6104366108713660046141af565b612bba565b600354610436906001600160a01b031681565b6103da6108973660046141af565b612bf2565b6103da60055481565b6103da6108b3366004613f6a565b612d84565b6103da6108c6366004613c1d565b612da1565b600054610436906001600160a01b031681565b6012546001600160a01b03166109455760405162461bcd60e51b815260206004820152602160248201527f4e6f20726577617264206469737472696275746f7220636f6e666967757265646044820152602160f81b60648201526084015b60405180910390fd5b60005b8351811015610c05576000848281518110610965576109656141db565b6020908102919091018101516001600160a01b0381166000908152600890925260409091205490915060ff166109d55760405162461bcd60e51b81526020600482015260156024820152741b585c9ad95d081b5d5cdd081899481b1a5cdd1959605a1b604482015260640161093c565b821515600103610ae457601254604051638220d7cf60e01b81526001600160a01b03838116600483015290911690638220d7cf90602401600060405180830381600087803b158015610a2657600080fd5b505af1158015610a3a573d6000803e3d6000fd5b5050505060005b8651811015610ae25760125487516001600160a01b03909116906310d47dc79084908a9085908110610a7557610a756141db565b602002602001015160016040518463ffffffff1660e01b8152600401610a9d939291906141f1565b600060405180830381600087803b158015610ab757600080fd5b505af1158015610acb573d6000803e3d6000fd5b505050508080610ada9061422b565b915050610a41565b505b831515600103610bf25760125460405162c823eb60e11b81526001600160a01b0383811660048301529091169063019047d690602401600060405180830381600087803b158015610b3457600080fd5b505af1158015610b48573d6000803e3d6000fd5b5050505060005b8651811015610bf05760125487516001600160a01b03909116906314f557739084908a9085908110610b8357610b836141db565b602002602001015160016040518463ffffffff1660e01b8152600401610bab939291906141f1565b600060405180830381600087803b158015610bc557600080fd5b505af1158015610bd9573d6000803e3d6000fd5b505050508080610be89061422b565b915050610b4f565b505b5080610bfd8161422b565b915050610948565b5050505050565b6001600160a01b03821660009081526008602052604081205460ff16610c445760405162461bcd60e51b815260040161093c90614244565b6009546001600160a01b0316331480610c6757506000546001600160a01b031633145b610c835760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480610c9e57506001821515145b610cba5760405162461bcd60e51b815260040161093c906142d3565b6001600160a01b0383166000818152600b6020908152604091829020805460ff19168615159081179091558251938452606091840182905260069184019190915265426f72726f7760d01b6080840152908201527f71aec636243f9709bb0007ae15e9afb8150ab01716d75fd7573be5cc096e03b09060a0015b60405180910390a150805b92915050565b806001600160a01b031663f851a4406040518163ffffffff1660e01b8152600401602060405180830381865afa158015610d83573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610da79190614303565b6001600160a01b0316336001600160a01b031614610e175760405162461bcd60e51b815260206004820152602760248201527f6f6e6c7920756e6974726f6c6c65722061646d696e2063616e206368616e676560448201526620627261696e7360c81b606482015260840161093c565b806001600160a01b031663c1e803346040518163ffffffff1660e01b81526004016020604051808303816000875af1158015610e57573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e7b9190614320565b15610ec05760405162461bcd60e51b815260206004820152601560248201527418da185b99d9481b9bdd08185d5d1a1bdc9a5e9959605a1b604482015260640161093c565b50565b6001600160a01b03841660009081526008602052604081205460ff16610eeb57506009610ef9565b610ef58584613106565b5060005b949350505050565b6009546000906001600160a01b0316331480610f2757506000546001600160a01b031633145b610f435760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480610f5e57506001821515145b610f7a5760405162461bcd60e51b815260040161093c906142d3565b60098054831515600160a81b0260ff60a81b199091161790556040517fef159d9a32b2472e32b098f954f3ce62d232939f1c207070b584df1814de2de090610fe59084906040808252600590820152645365697a6560d81b6060820152901515602082015260800190565b60405180910390a15090565b600080546001600160a01b0316331461104c5760405162461bcd60e51b815260206004820152601f60248201527f6f6e6c792061646d696e2063616e2073657420636c6f736520666163746f7200604482015260640161093c565b600580549083905560408051828152602081018590527f3b9670cf975d26958e754b57098eaa2ac914d8d2a31b83257997b9f346110fd991015b60405180910390a160005b9392505050565b6040805160018082528183019092526000916020808301908036833701905050905082816000815181106110ce576110ce6141db565b60200260200101906001600160a01b031690816001600160a01b0316815250506110fb81836001806108de565b505050565b6000546001600160a01b031633146111695760405162461bcd60e51b815260206004820152602660248201527f6f6e6c792061646d696e2063616e2073657420737570706c792063617020677560448201526530b93234b0b760d11b606482015260840161093c565b601080546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527fb0d3622c24ac9bd967d8f37a25808b3e668fe7ed4f3075bbe82842d3e287c04491015b60405180910390a15050565b6000546001600160a01b031633146112345760405162461bcd60e51b815260206004820152602660248201527f6f6e6c792061646d696e2063616e2073657420626f72726f772063617020677560448201526530b93234b0b760d11b606482015260840161093c565b600d80546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527feda98690e518e9a05f8ec6837663e188211b2da8f4906648b323f2c1d4434e2991016111bf565b6001600160a01b03821660009081526008602052604081205460ff166112c65760405162461bcd60e51b815260040161093c90614244565b6009546001600160a01b03163314806112e957506000546001600160a01b031633145b6113055760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b031633148061132057506001821515145b61133c5760405162461bcd60e51b815260040161093c906142d3565b6001600160a01b0383166000818152600a6020908152604091829020805460ff19168615159081179091558251938452606091840182905260049184019190915263135a5b9d60e21b6080840152908201527f71aec636243f9709bb0007ae15e9afb8150ab01716d75fd7573be5cc096e03b09060a001610d34565b6000806000806000806113cd8a8a8a8a613183565b9250925092508260118111156113e5576113e5614339565b95509093509150505b9450945094915050565b6001600160a01b0383166000908152600a602052604081205460ff16156114525760405162461bcd60e51b815260206004820152600e60248201526d1b5a5b9d081a5cc81c185d5cd95960921b604482015260640161093c565b6001600160a01b03841660009081526008602052604090205460ff1661147c5760095b9050611091565b6001600160a01b0384166000908152601160205260409020548015611647576000856001600160a01b0316633b1d21a26040518163ffffffff1660e01b8152600401602060405180830381865afa1580156114db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114ff9190614320565b90506000866001600160a01b03166347bd37186040518163ffffffff1660e01b8152600401602060405180830381865afa158015611541573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115659190614320565b90506000876001600160a01b0316638f840ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156115a7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115cb9190614320565b905060006115e26115dc85856134b3565b836134e9565b905060006115f082896134b3565b90508581106116415760405162461bcd60e51b815260206004820152601960248201527f6d61726b657420737570706c7920636170207265616368656400000000000000604482015260640161093c565b50505050505b6116518585613523565b60005b95945050505050565b600080546001600160a01b0316331461167c57610d3f6001600b613569565b600680549083905560408051828152602081018590527faeba5a6c40a8ac138134bff1aaa65debf25971188a58804bad717f82f0ec13169101611086565b6000546001600160a01b03163314806116dd57506010546001600160a01b031633145b6117475760405162461bcd60e51b815260206004820152603560248201527f6f6e6c792061646d696e206f7220737570706c792063617020677561726469616044820152746e2063616e2073657420737570706c79206361707360581b606482015260840161093c565b8281811580159061175757508082145b6117935760405162461bcd60e51b815260206004820152600d60248201526c1a5b9d985b1a59081a5b9c1d5d609a1b604482015260640161093c565b60005b8281101561188f578484828181106117b0576117b06141db565b90506020020135601160008989858181106117cd576117cd6141db565b90506020020160208101906117e29190613c1d565b6001600160a01b0316815260208101919091526040016000205586868281811061180e5761180e6141db565b90506020020160208101906118239190613c1d565b6001600160a01b03167f9e0ad9cee10bdf36b7fbd38910c0bdff0f275ace679b45b922381c2723d676f886868481811061185f5761185f6141db565b9050602002013560405161187591815260200190565b60405180910390a2806118878161422b565b915050611796565b50505050505050565b801580156118a65750600082115b156118e75760405162461bcd60e51b815260206004820152601160248201527072656465656d546f6b656e73207a65726f60781b604482015260640161093c565b50505050565b600c81815481106118fd57600080fd5b6000918252602090912001546001600160a01b0316905081565b600080546001600160a01b0316331461193657610d3f60016010613569565b600480546001600160a01b038481166001600160a01b031983168117909355604080519190921680825260208201939093527fd52b2b9b7e9ee655fcb95d2e5b9e0c9f69e7ef2b8e9d2d0ea78402d576d22e229101611086565b6000546001600160a01b031633146119d95760405162461bcd60e51b815260206004820152600c60248201526b155b985d5d1a1bdc9a5e995960a21b604482015260640161093c565b601280546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527f8ddca872a7a62d68235cff1a03badc845dc3007cfaa6145379f7bf3452ecb9b991016111bf565b600080600080600080611a4a876000806000613183565b925092509250826011811115611a6257611a62614339565b97919650945092505050565b600080546001600160a01b03163314611a8d57610d3f60016013613569565b600980546001600160a01b038481166001600160a01b031983168117909355604080519190921680825260208201939093527f0613b6ee6a04f0d09f390e4d9318894b9f6ac7fd83897cd8d18896ba579c401e9101611086565b6001600160a01b03851660009081526008602052604081205460ff161580611b2857506001600160a01b03851660009081526008602052604090205460ff16155b15611b375760095b9050611654565b600080611b43856135e2565b91935090915060009050826011811115611b5f57611b5f614339565b14611b7f57816011811115611b7657611b76614339565b92505050611654565b80600003611b8e576003611b76565b6040516395dd919360e01b81526001600160a01b038681166004830152600091908a16906395dd919390602401602060405180830381865afa158015611bd8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bfc9190614320565b90506000611c1a604051806020016040528060055481525083613602565b905080861115611c31576011945050505050611654565b5060009998505050505050505050565b6000546001600160a01b0316331480611c645750600d546001600160a01b031633145b611cce5760405162461bcd60e51b815260206004820152603560248201527f6f6e6c792061646d696e206f7220626f72726f772063617020677561726469616044820152746e2063616e2073657420626f72726f77206361707360581b606482015260840161093c565b82818115801590611cde57508082145b611d1a5760405162461bcd60e51b815260206004820152600d60248201526c1a5b9d985b1a59081a5b9c1d5d609a1b604482015260640161093c565b60005b8281101561188f57848482818110611d3757611d376141db565b90506020020135600e6000898985818110611d5457611d546141db565b9050602002016020810190611d699190613c1d565b6001600160a01b03168152602081019190915260400160002055868682818110611d9557611d956141db565b9050602002016020810190611daa9190613c1d565b6001600160a01b03167f6f1951b2aad10f3fc81b86d91105b413a5b3f847a34bbc5ce1904201b14438f6868684818110611de657611de66141db565b90506020020135604051611dfc91815260200190565b60405180910390a280611e0e8161422b565b915050611d1d565b6009546000906001600160a01b0316331480611e3c57506000546001600160a01b031633145b611e585760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480611e7357506001821515145b611e8f5760405162461bcd60e51b815260040161093c906142d3565b60098054831515600160a01b0260ff60a01b199091161790556040517fef159d9a32b2472e32b098f954f3ce62d232939f1c207070b584df1814de2de090610fe59084906040808252600890820152672a3930b739b332b960c11b6060820152901515602082015260800190565b600080546001600160a01b03163314611f1c57610d3f60016012613569565b6001600160a01b03821660009081526008602052604090205460ff1615611f4957610d3f600a6011613569565b816001600160a01b031663699cd5e26040518163ffffffff1660e01b8152600401602060405180830381865afa158015611f87573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611fab919061434f565b611feb5760405162461bcd60e51b815260206004820152601160248201527026bab9ba1031329030b71026aa37b5b2b760791b604482015260640161093c565b6001600160a01b0382166000908152600860205260408120805460ff19166001908117825581019190915561201f8361361a565b6040516001600160a01b03841681527fcf583bb0c569eb967f806b11601c4cb93c10310485c67add5f8362c2f212321f90602001611086565b6001600160a01b03811660009081526007602090815260408083208054825181850281018501909352808352606094938301828280156120c157602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116120a3575b5093979650505050505050565b6060600c80548060200260200160405190810160405280929190818152602001828054801561212657602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311612108575b5050505050905090565b61219433600c80548060200260200160405190810160405280929190818152602001828054801561218a57602002820191906000526020600020905b81546001600160a01b0316815260019091019060200180831161216c575b5050505050611098565b565b600954600090600160a01b900460ff16156121e85760405162461bcd60e51b81526020600482015260126024820152711d1c985b9cd9995c881a5cc81c185d5cd95960721b604482015260640161093c565b60006121f5868685613702565b90508015612204579050610ef9565b61220e8686613523565b6122188685613523565b60009695505050505050565b805160609060008167ffffffffffffffff81111561224457612244613c3a565b60405190808252806020026020018201604052801561226d578160200160208202803683370190505b50905060005b828110156122e457600085828151811061228f5761228f6141db565b602002602001015190506122a381336137ba565b60118111156122b4576122b4614339565b8383815181106122c6576122c66141db565b602090810291909101015250806122dc8161422b565b915050612273565b509392505050565b6004805460405163fc57d4df60e01b81526001600160a01b03868116938201939093526000928392839291169063fc57d4df90602401602060405180830381865afa15801561233f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123639190614320565b6004805460405163fc57d4df60e01b81526001600160a01b038981169382019390935292935060009291169063fc57d4df90602401602060405180830381865afa1580156123b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123d99190614320565b90508115806123e6575080155b156123fa57600d6000935093505050612502565b6000866001600160a01b031663182df0f56040518163ffffffff1660e01b8152600401602060405180830381865afa15801561243a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061245e9190614320565b905060006124786040518060200160405280600081525090565b6040805160208082018352600080835283518083018552908152835180830185526006548152845192830190945288825291926124b4916138ae565b92506124dc6040518060200160405280888152506040518060200160405280888152506138ae565b91506124e883836138f6565b90506124f4818b613602565b600099509750505050505050505b935093915050565b600954600090600160a81b900460ff16156125595760405162461bcd60e51b815260206004820152600f60248201526e1cd95a5e99481a5cc81c185d5cd959608a1b604482015260640161093c565b6001600160a01b03861660009081526008602052604090205460ff16158061259a57506001600160a01b03851660009081526008602052604090205460ff16155b156125a6576009611b30565b846001600160a01b0316635fe3b5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156125e4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126089190614303565b6001600160a01b0316866001600160a01b0316635fe3b5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801561264f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126739190614303565b6001600160a01b031614612688576002611b30565b61220e8684613523565b610ec081600c80548060200260200160405190810160405280929190818152602001828054801561218a576020028201919060005260206000209081546001600160a01b0316815260019091019060200180831161216c575050505050611098565b6001600160a01b0383166000908152600b602052604081205460ff16156127505760405162461bcd60e51b815260206004820152601060248201526f189bdc9c9bddc81a5cc81c185d5cd95960821b604482015260640161093c565b6001600160a01b03841660009081526008602052604090205460ff16612777576009611475565b6001600160a01b038085166000908152600860209081526040808320938716835260029093019052205460ff1661287457336001600160a01b038516146127f85760405162461bcd60e51b815260206004820152601560248201527439b2b73232b91036bab9ba1031329036aa37b5b2b760591b604482015260640161093c565b600061280433856137ba565b9050600081601181111561281a5761281a614339565b146128395780601181111561283157612831614339565b915050611091565b6001600160a01b038086166000908152600860209081526040808320938816835260029093019052205460ff166128725761287261436c565b505b6004805460405163fc57d4df60e01b81526001600160a01b038781169382019390935291169063fc57d4df90602401602060405180830381865afa1580156128c0573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128e49190614320565b6000036128f257600d611475565b6001600160a01b0384166000908152600e602052604090205480156129d7576000856001600160a01b03166347bd37186040518163ffffffff1660e01b8152600401602060405180830381865afa158015612951573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906129759190614320565b9050600061298382866134b3565b90508281106129d45760405162461bcd60e51b815260206004820152601960248201527f6d61726b657420626f72726f7720636170207265616368656400000000000000604482015260640161093c565b50505b6000806129e78688600088613183565b91935090915060009050826011811115612a0357612a03614339565b14612a2457816011811115612a1a57612a1a614339565b9350505050611091565b8015612a31576004612a1a565b612a3b8787613106565b6000979650505050505050565b6000546001600160a01b03163314612a915760405162461bcd60e51b815260206004820152600c60248201526b155b985d5d1a1bdc9a5e995960a21b604482015260640161093c565b8160018201612b81576000546040516370a0823160e01b81523060048201526001600160a01b038381169263a9059cbb9291169083906370a0823190602401602060405180830381865afa158015612aed573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b119190614320565b6040516001600160e01b031960e085901b1681526001600160a01b03909216600483015260248201526044015b6020604051808303816000875af1158015612b5d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118e7919061434f565b60005460405163a9059cbb60e01b81526001600160a01b039182166004820152602481018490529082169063a9059cbb90604401612b3e565b60076020528160005260406000208181548110612bd657600080fd5b6000918252602090912001546001600160a01b03169150829050565b600080546001600160a01b03163314612c1857612c1160016006613569565b9050610d3f565b6001600160a01b0383166000908152600860205260409020805460ff16612c4d57612c4560096007613569565b915050610d3f565b60408051602080820183528582528251908101909252670c7d713b49da0000825290612c7b81835190511090565b15612c9657612c8c60066008613569565b9350505050610d3f565b8415801590612d1257506004805460405163fc57d4df60e01b81526001600160a01b038981169382019390935291169063fc57d4df90602401602060405180830381865afa158015612cec573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d109190614320565b155b15612d2357612c8c600d6009613569565b60018301805490869055604080516001600160a01b0389168152602081018390529081018790527f70483e6592cd5182d45ac970e05bc62cdcc90e9d8ef2c2dbe686cf383bcd7fc59060600160405180910390a16000979650505050505050565b600080612d92858585613702565b90508015611647579050611091565b6040516361bfb47160e11b815233600482015260009082908290819081906001600160a01b0385169063c37f68e290602401608060405180830381865afa158015612df0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e149190614382565b5092509250925082600014612e795760405162461bcd60e51b815260206004820152602560248201527f657869744d61726b65743a206765744163636f756e74536e617073686f742066604482015264185a5b195960da1b606482015260840161093c565b8015612e9557612e8b600c6002613569565b9695505050505050565b6000612ea2873385613702565b90508015612ec257612eb7600e600383613930565b979650505050505050565b6001600160a01b0385166000908152600860209081526040808320338452600281019092529091205460ff16612f0057600098975050505050505050565b3360009081526002820160209081526040808320805460ff191690556007825280832080548251818502810185019093528083529192909190830182828015612f7257602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311612f54575b5050835193945083925060009150505b82811015612fd657896001600160a01b0316848281518110612fa657612fa66141db565b60200260200101516001600160a01b031603612fc457809150612fd6565b80612fce8161422b565b915050612f82565b50818110612fe657612fe661436c565b33600090815260076020526040902080548190613005906001906143b8565b81548110613015576130156141db565b9060005260206000200160009054906101000a90046001600160a01b0316818381548110613045576130456141db565b9060005260206000200160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555080805480613083576130836143cb565b600082815260209020810160001990810180546001600160a01b03191690550190556040517fe699a64c18b07ac5b7301aa273f36a2287239eb9501d81950672794afba29a0d906130ec908c9033906001600160a01b0392831681529116602082015260400190565b60405180910390a160009c9b505050505050505050505050565b6012546001600160a01b03161561317f576012546040516356e3af2960e11b81526001600160a01b039091169063adc75e529061314c90859085906000906004016141f1565b600060405180830381600087803b15801561316657600080fd5b505af115801561317a573d6000803e3d6000fd5b505050505b5050565b6000806000613190613b66565b6001600160a01b0388166000908152600760209081526040808320805482518185028101850190935280835284938301828280156131f757602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116131d9575b5050505050905060005b815181101561345d57600082828151811061321e5761321e6141db565b60209081029190910101516040516361bfb47160e11b81526001600160a01b038e811660048301529192509082169063c37f68e290602401608060405180830381865afa158015613273573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132979190614382565b608089015260608801526040870152935083156132c357600f60008097509750975050505050506113ee565b60408051602080820183526001600160a01b0384811660008181526008845285902060010154845260c08a01939093528351808301855260808a0151815260e08a015260048054855163fc57d4df60e01b815291820194909452935192169263fc57d4df92602480830193928290030181865afa158015613348573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061336c9190614320565b60a0860181905260000361338f57600d60008097509750975050505050506113ee565b604080516020810190915260a0860151815261010086015260c085015160e08601516133c9916133be916138ae565b8661010001516138ae565b6101208601819052604086015186516133e39291906139a8565b8552610100850151606086015160208701516134009291906139a8565b60208601526001600160a01b03808c169082160361344a5761342c8561012001518b87602001516139a8565b60208601819052610100860151613444918b906139a8565b60208601525b50806134558161422b565b915050613201565b5060208301518351111561348e576020830151835160009161347e916143b8565b60009550955095505050506113ee565b600080846000015185602001516134a591906143b8565b9550955095505050506113ee565b60006110918383604051806040016040528060118152602001706164646974696f6e206f766572666c6f7760781b8152506139c9565b60006110918383604051806040016040528060158152602001747375627472616374696f6e20756e646572666c6f7760581b815250613a03565b6012546001600160a01b03161561317f57601254604051637076c88b60e11b81526001600160a01b039091169063e0ed91169061314c90859085906000906004016141f1565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa083601181111561359e5761359e614339565b8360148111156135b0576135b0614339565b60408051928352602083019190915260009082015260600160405180910390a182601181111561109157611091614339565b60008060006135f5846000806000613183565b9250925092509193909250565b60008061360f8484613a32565b9050610ef981613a5a565b60005b600c548110156136af57816001600160a01b0316600c8281548110613644576136446141db565b6000918252602090912001546001600160a01b03160361369d5760405162461bcd60e51b81526020600482015260146024820152731b585c9ad95d08185b1c9958591e48185919195960621b604482015260640161093c565b806136a78161422b565b91505061361d565b50600c80546001810182556000919091527fdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c70180546001600160a01b0319166001600160a01b0392909216919091179055565b6001600160a01b03831660009081526008602052604081205460ff16613729576009611475565b6001600160a01b038085166000908152600860209081526040808320938716835260029093019052205460ff16613761576000611475565b6000806137718587866000613183565b9193509091506000905082601181111561378d5761378d614339565b146137ad578160118111156137a4576137a4614339565b92505050611091565b80156122185760046137a4565b6001600160a01b0382166000908152600860205260408120805460ff166137e5576009915050610d3f565b6001600160a01b038316600090815260028201602052604090205460ff161515600103613816576000915050610d3f565b6001600160a01b03838116600081815260028401602090815260408083208054600160ff199091168117909155600783528184208054918201815584529282902090920180546001600160a01b031916948916948517905581519384528301919091527f3ab23ab0d51cccc0c3085aec51f99228625aa1a922b3a8ca89a26b0f2027a1a5910160405180910390a15060009392505050565b6040805160208101909152600081526040518060200160405280670de0b6b3a76400006138e386600001518660000151613a72565b6138ed91906143e1565b90529392505050565b60408051602081019091526000815260405180602001604052806138ed6139298660000151670de0b6b3a7640000613a72565b8551613ab4565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa084601181111561396557613965614339565b84601481111561397757613977614339565b604080519283526020830191909152810184905260600160405180910390a1836011811115610ef957610ef9614339565b6000806139b58585613a32565b90506116546139c382613a5a565b846134b3565b6000806139d68486614403565b905082858210156139fa5760405162461bcd60e51b815260040161093c9190614416565b50949350505050565b60008184841115613a275760405162461bcd60e51b815260040161093c9190614416565b50610ef983856143b8565b60408051602081019091526000815260405180602001604052806138ed856000015185613a72565b8051600090610d3f90670de0b6b3a7640000906143e1565b600061109183836040518060400160405280601781526020017f6d756c7469706c69636174696f6e206f766572666c6f77000000000000000000815250613ae7565b600061109183836040518060400160405280600e81526020016d646976696465206279207a65726f60901b815250613b3a565b6000831580613af4575082155b15613b0157506000611091565b6000613b0d8486614464565b905083613b1a86836143e1565b1483906139fa5760405162461bcd60e51b815260040161093c9190614416565b60008183613b5b5760405162461bcd60e51b815260040161093c9190614416565b50610ef983856143e1565b604051806101400160405280600081526020016000815260200160008152602001600081526020016000815260200160008152602001613bb26040518060200160405280600081525090565b8152602001613bcd6040518060200160405280600081525090565b8152602001613be86040518060200160405280600081525090565b8152602001613c036040518060200160405280600081525090565b905290565b6001600160a01b0381168114610ec057600080fd5b600060208284031215613c2f57600080fd5b813561109181613c08565b634e487b7160e01b600052604160045260246000fd5b604051601f8201601f1916810167ffffffffffffffff81118282101715613c7957613c79613c3a565b604052919050565b600067ffffffffffffffff821115613c9b57613c9b613c3a565b5060051b60200190565b600082601f830112613cb657600080fd5b81356020613ccb613cc683613c81565b613c50565b82815260059290921b84018101918181019086841115613cea57600080fd5b8286015b84811015613d0e578035613d0181613c08565b8352918301918301613cee565b509695505050505050565b600082601f830112613d2a57600080fd5b81356020613d3a613cc683613c81565b82815260059290921b84018101918181019086841115613d5957600080fd5b8286015b84811015613d0e578035613d7081613c08565b8352918301918301613d5d565b8015158114610ec057600080fd5b60008060008060808587031215613da157600080fd5b843567ffffffffffffffff80821115613db957600080fd5b613dc588838901613ca5565b95506020870135915080821115613ddb57600080fd5b50613de887828801613d19565b9350506040850135613df981613d7d565b91506060850135613e0981613d7d565b939692955090935050565b60008060408385031215613e2757600080fd5b8235613e3281613c08565b91506020830135613e4281613d7d565b809150509250929050565b60008060008060808587031215613e6357600080fd5b8435613e6e81613c08565b93506020850135613e7e81613c08565b92506040850135613e8e81613c08565b9396929550929360600135925050565b600060208284031215613eb057600080fd5b813561109181613d7d565b600060208284031215613ecd57600080fd5b5035919050565b60008060408385031215613ee757600080fd5b8235613ef281613c08565b9150602083013567ffffffffffffffff811115613f0e57600080fd5b613f1a85828601613d19565b9150509250929050565b60008060008060808587031215613f3a57600080fd5b8435613f4581613c08565b93506020850135613f5581613c08565b93969395505050506040820135916060013590565b600080600060608486031215613f7f57600080fd5b8335613f8a81613c08565b92506020840135613f9a81613c08565b929592945050506040919091013590565b60008083601f840112613fbd57600080fd5b50813567ffffffffffffffff811115613fd557600080fd5b6020830191508360208260051b8501011115613ff057600080fd5b9250929050565b6000806000806040858703121561400d57600080fd5b843567ffffffffffffffff8082111561402557600080fd5b61403188838901613fab565b9096509450602087013591508082111561404a57600080fd5b5061405787828801613fab565b95989497509550505050565b600080600080600060a0868803121561407b57600080fd5b853561408681613c08565b9450602086013561409681613c08565b935060408601356140a681613c08565b925060608601356140b681613c08565b949793965091946080013592915050565b600080604083850312156140da57600080fd5b82356140e581613c08565b91506020830135613e4281613c08565b6020808252825182820181905260009190848201906040850190845b818110156141365783516001600160a01b031683529284019291840191600101614111565b50909695505050505050565b60006020828403121561415457600080fd5b813567ffffffffffffffff81111561416b57600080fd5b610ef984828501613ca5565b6020808252825182820181905260009190848201906040850190845b8181101561413657835183529284019291840191600101614193565b600080604083850312156141c257600080fd5b82356141cd81613c08565b946020939093013593505050565b634e487b7160e01b600052603260045260246000fd5b6001600160a01b039384168152919092166020820152901515604082015260600190565b634e487b7160e01b600052601160045260246000fd5b60006001820161423d5761423d614215565b5060010190565b60208082526028908201527f63616e6e6f742070617573652061206d61726b65742074686174206973206e6f6040820152671d081b1a5cdd195960c21b606082015260800190565b60208082526027908201527f6f6e6c7920706175736520677561726469616e20616e642061646d696e2063616040820152666e20706175736560c81b606082015260800190565b6020808252601690820152756f6e6c792061646d696e2063616e20756e706175736560501b604082015260600190565b60006020828403121561431557600080fd5b815161109181613c08565b60006020828403121561433257600080fd5b5051919050565b634e487b7160e01b600052602160045260246000fd5b60006020828403121561436157600080fd5b815161109181613d7d565b634e487b7160e01b600052600160045260246000fd5b6000806000806080858703121561439857600080fd5b505082516020840151604085015160609095015191969095509092509050565b81810381811115610d3f57610d3f614215565b634e487b7160e01b600052603160045260246000fd5b6000826143fe57634e487b7160e01b600052601260045260246000fd5b500490565b80820180821115610d3f57610d3f614215565b600060208083528351808285015260005b8181101561444357858101830151858201604001528201614427565b506000604082860101526040601f19601f8301168501019250505092915050565b8082028115828204841417610d3f57610d3f61421556fea2646970667358221220f3406a2a069958691bfb6e07578a8ce72fb0a794e27cb468a909b4357be4667f64736f6c63430008130033
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106103985760003560e01c80636d154ea5116101e9578063bb82aa5e1161010f578063dbc7779b116100ad578063e87554461161007c578063e87554461461089c578063eabe7d91146108a5578063ede4edd0146108b8578063f851a440146108cb57600080fd5b8063dbc7779b14610850578063dce1544914610863578063dcfbc0c714610876578063e4028eee1461088957600080fd5b8063c488847b116100e9578063c488847b146107ef578063d02f735114610817578063d279c1911461082a578063da3d454c1461083d57600080fd5b8063bb82aa5e146107a9578063bdcdc258146107bc578063c2998238146107cf57600080fd5b8063929fe9a111610187578063ac0b0bb711610156578063ac0b0bb714610772578063acc2166a14610786578063b0772d0b14610799578063b88a802f146107a157600080fd5b8063929fe9a1146106eb578063a76b3fda1461072c578063a979f0c51461073f578063abfceffc1461075257600080fd5b80637dc0d1d0116101c35780637dc0d1d01461066d57806387f76303146106805780638e8f294b146106945780638ebf6364146106d857600080fd5b80636d154ea514610621578063731f0c2b14610644578063796b89b91461066757600080fd5b80633bcf7ec1116102ce57806351dff9891161026c5780635ec88c791161023b5780635ec88c79146105d55780635f5af1aa146105e85780635fc7e71e146105fb578063607ef6c11461060e57600080fd5b806351dff9891461058957806352d84d1e1461059c57806355ee1fe1146105af5780635c254d11146105c257600080fd5b80634e79238f116102a85780634e79238f146105225780634ef4c3e1146105505780634fd42e171461056357806351a485e41461057657600080fd5b80633bcf7ec1146104e65780634a584432146104f95780634ada90af1461051957600080fd5b806324a3d6221161033b578063317b0b7711610315578063317b0b771461049a5780633685ffe7146104ad57806338b8f4c3146104c0578063391957d7146104d357600080fd5b806324a3d6221461046157806326782247146104745780632d70db781461048757600080fd5b806318c882a51161037757806318c882a5146103fd5780631d504dc61461041057806321af45691461042357806324008a621461044e57600080fd5b80627e3dd21461039d57806302c3bcbb146103ba578063114b9d19146103e8575b600080fd5b6103a5600181565b60405190151581526020015b60405180910390f35b6103da6103c8366004613c1d565b60116020526000908152604090205481565b6040519081526020016103b1565b6103fb6103f6366004613d8b565b6108de565b005b6103a561040b366004613e14565b610c0c565b6103fb61041e366004613c1d565b610d45565b600d54610436906001600160a01b031681565b6040516001600160a01b0390911681526020016103b1565b6103da61045c366004613e4d565b610ec3565b600954610436906001600160a01b031681565b600154610436906001600160a01b031681565b6103a5610495366004613e9e565b610f01565b6103da6104a8366004613ebb565b610ff1565b6103fb6104bb366004613ed4565b611098565b6103fb6104ce366004613c1d565b611100565b6103fb6104e1366004613c1d565b6111cb565b6103a56104f4366004613e14565b61128e565b6103da610507366004613c1d565b600e6020526000908152604090205481565b6103da60065481565b610535610530366004613f24565b6113b8565b604080519384526020840192909252908201526060016103b1565b6103da61055e366004613f6a565b6113f8565b6103da610571366004613ebb565b61165d565b6103fb610584366004613ff7565b6116ba565b6103fb610597366004613f24565b611898565b6104366105aa366004613ebb565b6118ed565b6103da6105bd366004613c1d565b611917565b6103fb6105d0366004613c1d565b611990565b6105356105e3366004613c1d565b611a33565b6103da6105f6366004613c1d565b611a6e565b6103da610609366004614063565b611ae7565b6103fb61061c366004613ff7565b611c41565b6103a561062f366004613c1d565b600b6020526000908152604090205460ff1681565b6103a5610652366004613c1d565b600a6020526000908152604090205460ff1681565b426103da565b600454610436906001600160a01b031681565b6009546103a590600160a01b900460ff1681565b6106c16106a2366004613c1d565b6008602052600090815260409020805460019091015460ff9091169082565b6040805192151583526020830191909152016103b1565b6103a56106e6366004613e9e565b611e16565b6103a56106f93660046140c7565b6001600160a01b038082166000908152600860209081526040808320938616835260029093019052205460ff1692915050565b6103da61073a366004613c1d565b611efd565b601054610436906001600160a01b031681565b610765610760366004613c1d565b612058565b6040516103b191906140f5565b6009546103a590600160a81b900460ff1681565b601254610436906001600160a01b031681565b6107656120ce565b6103fb612130565b600254610436906001600160a01b031681565b6103da6107ca366004613e4d565b612196565b6107e26107dd366004614142565b612224565b6040516103b19190614177565b6108026107fd366004613f6a565b6122ec565b604080519283526020830191909152016103b1565b6103da610825366004614063565b61250a565b6103fb610838366004613c1d565b612692565b6103da61084b366004613f6a565b6126f4565b6103fb61085e3660046141af565b612a48565b6104366108713660046141af565b612bba565b600354610436906001600160a01b031681565b6103da6108973660046141af565b612bf2565b6103da60055481565b6103da6108b3366004613f6a565b612d84565b6103da6108c6366004613c1d565b612da1565b600054610436906001600160a01b031681565b6012546001600160a01b03166109455760405162461bcd60e51b815260206004820152602160248201527f4e6f20726577617264206469737472696275746f7220636f6e666967757265646044820152602160f81b60648201526084015b60405180910390fd5b60005b8351811015610c05576000848281518110610965576109656141db565b6020908102919091018101516001600160a01b0381166000908152600890925260409091205490915060ff166109d55760405162461bcd60e51b81526020600482015260156024820152741b585c9ad95d081b5d5cdd081899481b1a5cdd1959605a1b604482015260640161093c565b821515600103610ae457601254604051638220d7cf60e01b81526001600160a01b03838116600483015290911690638220d7cf90602401600060405180830381600087803b158015610a2657600080fd5b505af1158015610a3a573d6000803e3d6000fd5b5050505060005b8651811015610ae25760125487516001600160a01b03909116906310d47dc79084908a9085908110610a7557610a756141db565b602002602001015160016040518463ffffffff1660e01b8152600401610a9d939291906141f1565b600060405180830381600087803b158015610ab757600080fd5b505af1158015610acb573d6000803e3d6000fd5b505050508080610ada9061422b565b915050610a41565b505b831515600103610bf25760125460405162c823eb60e11b81526001600160a01b0383811660048301529091169063019047d690602401600060405180830381600087803b158015610b3457600080fd5b505af1158015610b48573d6000803e3d6000fd5b5050505060005b8651811015610bf05760125487516001600160a01b03909116906314f557739084908a9085908110610b8357610b836141db565b602002602001015160016040518463ffffffff1660e01b8152600401610bab939291906141f1565b600060405180830381600087803b158015610bc557600080fd5b505af1158015610bd9573d6000803e3d6000fd5b505050508080610be89061422b565b915050610b4f565b505b5080610bfd8161422b565b915050610948565b5050505050565b6001600160a01b03821660009081526008602052604081205460ff16610c445760405162461bcd60e51b815260040161093c90614244565b6009546001600160a01b0316331480610c6757506000546001600160a01b031633145b610c835760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480610c9e57506001821515145b610cba5760405162461bcd60e51b815260040161093c906142d3565b6001600160a01b0383166000818152600b6020908152604091829020805460ff19168615159081179091558251938452606091840182905260069184019190915265426f72726f7760d01b6080840152908201527f71aec636243f9709bb0007ae15e9afb8150ab01716d75fd7573be5cc096e03b09060a0015b60405180910390a150805b92915050565b806001600160a01b031663f851a4406040518163ffffffff1660e01b8152600401602060405180830381865afa158015610d83573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610da79190614303565b6001600160a01b0316336001600160a01b031614610e175760405162461bcd60e51b815260206004820152602760248201527f6f6e6c7920756e6974726f6c6c65722061646d696e2063616e206368616e676560448201526620627261696e7360c81b606482015260840161093c565b806001600160a01b031663c1e803346040518163ffffffff1660e01b81526004016020604051808303816000875af1158015610e57573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e7b9190614320565b15610ec05760405162461bcd60e51b815260206004820152601560248201527418da185b99d9481b9bdd08185d5d1a1bdc9a5e9959605a1b604482015260640161093c565b50565b6001600160a01b03841660009081526008602052604081205460ff16610eeb57506009610ef9565b610ef58584613106565b5060005b949350505050565b6009546000906001600160a01b0316331480610f2757506000546001600160a01b031633145b610f435760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480610f5e57506001821515145b610f7a5760405162461bcd60e51b815260040161093c906142d3565b60098054831515600160a81b0260ff60a81b199091161790556040517fef159d9a32b2472e32b098f954f3ce62d232939f1c207070b584df1814de2de090610fe59084906040808252600590820152645365697a6560d81b6060820152901515602082015260800190565b60405180910390a15090565b600080546001600160a01b0316331461104c5760405162461bcd60e51b815260206004820152601f60248201527f6f6e6c792061646d696e2063616e2073657420636c6f736520666163746f7200604482015260640161093c565b600580549083905560408051828152602081018590527f3b9670cf975d26958e754b57098eaa2ac914d8d2a31b83257997b9f346110fd991015b60405180910390a160005b9392505050565b6040805160018082528183019092526000916020808301908036833701905050905082816000815181106110ce576110ce6141db565b60200260200101906001600160a01b031690816001600160a01b0316815250506110fb81836001806108de565b505050565b6000546001600160a01b031633146111695760405162461bcd60e51b815260206004820152602660248201527f6f6e6c792061646d696e2063616e2073657420737570706c792063617020677560448201526530b93234b0b760d11b606482015260840161093c565b601080546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527fb0d3622c24ac9bd967d8f37a25808b3e668fe7ed4f3075bbe82842d3e287c04491015b60405180910390a15050565b6000546001600160a01b031633146112345760405162461bcd60e51b815260206004820152602660248201527f6f6e6c792061646d696e2063616e2073657420626f72726f772063617020677560448201526530b93234b0b760d11b606482015260840161093c565b600d80546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527feda98690e518e9a05f8ec6837663e188211b2da8f4906648b323f2c1d4434e2991016111bf565b6001600160a01b03821660009081526008602052604081205460ff166112c65760405162461bcd60e51b815260040161093c90614244565b6009546001600160a01b03163314806112e957506000546001600160a01b031633145b6113055760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b031633148061132057506001821515145b61133c5760405162461bcd60e51b815260040161093c906142d3565b6001600160a01b0383166000818152600a6020908152604091829020805460ff19168615159081179091558251938452606091840182905260049184019190915263135a5b9d60e21b6080840152908201527f71aec636243f9709bb0007ae15e9afb8150ab01716d75fd7573be5cc096e03b09060a001610d34565b6000806000806000806113cd8a8a8a8a613183565b9250925092508260118111156113e5576113e5614339565b95509093509150505b9450945094915050565b6001600160a01b0383166000908152600a602052604081205460ff16156114525760405162461bcd60e51b815260206004820152600e60248201526d1b5a5b9d081a5cc81c185d5cd95960921b604482015260640161093c565b6001600160a01b03841660009081526008602052604090205460ff1661147c5760095b9050611091565b6001600160a01b0384166000908152601160205260409020548015611647576000856001600160a01b0316633b1d21a26040518163ffffffff1660e01b8152600401602060405180830381865afa1580156114db573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114ff9190614320565b90506000866001600160a01b03166347bd37186040518163ffffffff1660e01b8152600401602060405180830381865afa158015611541573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115659190614320565b90506000876001600160a01b0316638f840ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156115a7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115cb9190614320565b905060006115e26115dc85856134b3565b836134e9565b905060006115f082896134b3565b90508581106116415760405162461bcd60e51b815260206004820152601960248201527f6d61726b657420737570706c7920636170207265616368656400000000000000604482015260640161093c565b50505050505b6116518585613523565b60005b95945050505050565b600080546001600160a01b0316331461167c57610d3f6001600b613569565b600680549083905560408051828152602081018590527faeba5a6c40a8ac138134bff1aaa65debf25971188a58804bad717f82f0ec13169101611086565b6000546001600160a01b03163314806116dd57506010546001600160a01b031633145b6117475760405162461bcd60e51b815260206004820152603560248201527f6f6e6c792061646d696e206f7220737570706c792063617020677561726469616044820152746e2063616e2073657420737570706c79206361707360581b606482015260840161093c565b8281811580159061175757508082145b6117935760405162461bcd60e51b815260206004820152600d60248201526c1a5b9d985b1a59081a5b9c1d5d609a1b604482015260640161093c565b60005b8281101561188f578484828181106117b0576117b06141db565b90506020020135601160008989858181106117cd576117cd6141db565b90506020020160208101906117e29190613c1d565b6001600160a01b0316815260208101919091526040016000205586868281811061180e5761180e6141db565b90506020020160208101906118239190613c1d565b6001600160a01b03167f9e0ad9cee10bdf36b7fbd38910c0bdff0f275ace679b45b922381c2723d676f886868481811061185f5761185f6141db565b9050602002013560405161187591815260200190565b60405180910390a2806118878161422b565b915050611796565b50505050505050565b801580156118a65750600082115b156118e75760405162461bcd60e51b815260206004820152601160248201527072656465656d546f6b656e73207a65726f60781b604482015260640161093c565b50505050565b600c81815481106118fd57600080fd5b6000918252602090912001546001600160a01b0316905081565b600080546001600160a01b0316331461193657610d3f60016010613569565b600480546001600160a01b038481166001600160a01b031983168117909355604080519190921680825260208201939093527fd52b2b9b7e9ee655fcb95d2e5b9e0c9f69e7ef2b8e9d2d0ea78402d576d22e229101611086565b6000546001600160a01b031633146119d95760405162461bcd60e51b815260206004820152600c60248201526b155b985d5d1a1bdc9a5e995960a21b604482015260640161093c565b601280546001600160a01b038381166001600160a01b031983168117909355604080519190921680825260208201939093527f8ddca872a7a62d68235cff1a03badc845dc3007cfaa6145379f7bf3452ecb9b991016111bf565b600080600080600080611a4a876000806000613183565b925092509250826011811115611a6257611a62614339565b97919650945092505050565b600080546001600160a01b03163314611a8d57610d3f60016013613569565b600980546001600160a01b038481166001600160a01b031983168117909355604080519190921680825260208201939093527f0613b6ee6a04f0d09f390e4d9318894b9f6ac7fd83897cd8d18896ba579c401e9101611086565b6001600160a01b03851660009081526008602052604081205460ff161580611b2857506001600160a01b03851660009081526008602052604090205460ff16155b15611b375760095b9050611654565b600080611b43856135e2565b91935090915060009050826011811115611b5f57611b5f614339565b14611b7f57816011811115611b7657611b76614339565b92505050611654565b80600003611b8e576003611b76565b6040516395dd919360e01b81526001600160a01b038681166004830152600091908a16906395dd919390602401602060405180830381865afa158015611bd8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bfc9190614320565b90506000611c1a604051806020016040528060055481525083613602565b905080861115611c31576011945050505050611654565b5060009998505050505050505050565b6000546001600160a01b0316331480611c645750600d546001600160a01b031633145b611cce5760405162461bcd60e51b815260206004820152603560248201527f6f6e6c792061646d696e206f7220626f72726f772063617020677561726469616044820152746e2063616e2073657420626f72726f77206361707360581b606482015260840161093c565b82818115801590611cde57508082145b611d1a5760405162461bcd60e51b815260206004820152600d60248201526c1a5b9d985b1a59081a5b9c1d5d609a1b604482015260640161093c565b60005b8281101561188f57848482818110611d3757611d376141db565b90506020020135600e6000898985818110611d5457611d546141db565b9050602002016020810190611d699190613c1d565b6001600160a01b03168152602081019190915260400160002055868682818110611d9557611d956141db565b9050602002016020810190611daa9190613c1d565b6001600160a01b03167f6f1951b2aad10f3fc81b86d91105b413a5b3f847a34bbc5ce1904201b14438f6868684818110611de657611de66141db565b90506020020135604051611dfc91815260200190565b60405180910390a280611e0e8161422b565b915050611d1d565b6009546000906001600160a01b0316331480611e3c57506000546001600160a01b031633145b611e585760405162461bcd60e51b815260040161093c9061428c565b6000546001600160a01b0316331480611e7357506001821515145b611e8f5760405162461bcd60e51b815260040161093c906142d3565b60098054831515600160a01b0260ff60a01b199091161790556040517fef159d9a32b2472e32b098f954f3ce62d232939f1c207070b584df1814de2de090610fe59084906040808252600890820152672a3930b739b332b960c11b6060820152901515602082015260800190565b600080546001600160a01b03163314611f1c57610d3f60016012613569565b6001600160a01b03821660009081526008602052604090205460ff1615611f4957610d3f600a6011613569565b816001600160a01b031663699cd5e26040518163ffffffff1660e01b8152600401602060405180830381865afa158015611f87573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611fab919061434f565b611feb5760405162461bcd60e51b815260206004820152601160248201527026bab9ba1031329030b71026aa37b5b2b760791b604482015260640161093c565b6001600160a01b0382166000908152600860205260408120805460ff19166001908117825581019190915561201f8361361a565b6040516001600160a01b03841681527fcf583bb0c569eb967f806b11601c4cb93c10310485c67add5f8362c2f212321f90602001611086565b6001600160a01b03811660009081526007602090815260408083208054825181850281018501909352808352606094938301828280156120c157602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116120a3575b5093979650505050505050565b6060600c80548060200260200160405190810160405280929190818152602001828054801561212657602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311612108575b5050505050905090565b61219433600c80548060200260200160405190810160405280929190818152602001828054801561218a57602002820191906000526020600020905b81546001600160a01b0316815260019091019060200180831161216c575b5050505050611098565b565b600954600090600160a01b900460ff16156121e85760405162461bcd60e51b81526020600482015260126024820152711d1c985b9cd9995c881a5cc81c185d5cd95960721b604482015260640161093c565b60006121f5868685613702565b90508015612204579050610ef9565b61220e8686613523565b6122188685613523565b60009695505050505050565b805160609060008167ffffffffffffffff81111561224457612244613c3a565b60405190808252806020026020018201604052801561226d578160200160208202803683370190505b50905060005b828110156122e457600085828151811061228f5761228f6141db565b602002602001015190506122a381336137ba565b60118111156122b4576122b4614339565b8383815181106122c6576122c66141db565b602090810291909101015250806122dc8161422b565b915050612273565b509392505050565b6004805460405163fc57d4df60e01b81526001600160a01b03868116938201939093526000928392839291169063fc57d4df90602401602060405180830381865afa15801561233f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123639190614320565b6004805460405163fc57d4df60e01b81526001600160a01b038981169382019390935292935060009291169063fc57d4df90602401602060405180830381865afa1580156123b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123d99190614320565b90508115806123e6575080155b156123fa57600d6000935093505050612502565b6000866001600160a01b031663182df0f56040518163ffffffff1660e01b8152600401602060405180830381865afa15801561243a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061245e9190614320565b905060006124786040518060200160405280600081525090565b6040805160208082018352600080835283518083018552908152835180830185526006548152845192830190945288825291926124b4916138ae565b92506124dc6040518060200160405280888152506040518060200160405280888152506138ae565b91506124e883836138f6565b90506124f4818b613602565b600099509750505050505050505b935093915050565b600954600090600160a81b900460ff16156125595760405162461bcd60e51b815260206004820152600f60248201526e1cd95a5e99481a5cc81c185d5cd959608a1b604482015260640161093c565b6001600160a01b03861660009081526008602052604090205460ff16158061259a57506001600160a01b03851660009081526008602052604090205460ff16155b156125a6576009611b30565b846001600160a01b0316635fe3b5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156125e4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126089190614303565b6001600160a01b0316866001600160a01b0316635fe3b5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801561264f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126739190614303565b6001600160a01b031614612688576002611b30565b61220e8684613523565b610ec081600c80548060200260200160405190810160405280929190818152602001828054801561218a576020028201919060005260206000209081546001600160a01b0316815260019091019060200180831161216c575050505050611098565b6001600160a01b0383166000908152600b602052604081205460ff16156127505760405162461bcd60e51b815260206004820152601060248201526f189bdc9c9bddc81a5cc81c185d5cd95960821b604482015260640161093c565b6001600160a01b03841660009081526008602052604090205460ff16612777576009611475565b6001600160a01b038085166000908152600860209081526040808320938716835260029093019052205460ff1661287457336001600160a01b038516146127f85760405162461bcd60e51b815260206004820152601560248201527439b2b73232b91036bab9ba1031329036aa37b5b2b760591b604482015260640161093c565b600061280433856137ba565b9050600081601181111561281a5761281a614339565b146128395780601181111561283157612831614339565b915050611091565b6001600160a01b038086166000908152600860209081526040808320938816835260029093019052205460ff166128725761287261436c565b505b6004805460405163fc57d4df60e01b81526001600160a01b038781169382019390935291169063fc57d4df90602401602060405180830381865afa1580156128c0573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128e49190614320565b6000036128f257600d611475565b6001600160a01b0384166000908152600e602052604090205480156129d7576000856001600160a01b03166347bd37186040518163ffffffff1660e01b8152600401602060405180830381865afa158015612951573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906129759190614320565b9050600061298382866134b3565b90508281106129d45760405162461bcd60e51b815260206004820152601960248201527f6d61726b657420626f72726f7720636170207265616368656400000000000000604482015260640161093c565b50505b6000806129e78688600088613183565b91935090915060009050826011811115612a0357612a03614339565b14612a2457816011811115612a1a57612a1a614339565b9350505050611091565b8015612a31576004612a1a565b612a3b8787613106565b6000979650505050505050565b6000546001600160a01b03163314612a915760405162461bcd60e51b815260206004820152600c60248201526b155b985d5d1a1bdc9a5e995960a21b604482015260640161093c565b8160018201612b81576000546040516370a0823160e01b81523060048201526001600160a01b038381169263a9059cbb9291169083906370a0823190602401602060405180830381865afa158015612aed573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b119190614320565b6040516001600160e01b031960e085901b1681526001600160a01b03909216600483015260248201526044015b6020604051808303816000875af1158015612b5d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118e7919061434f565b60005460405163a9059cbb60e01b81526001600160a01b039182166004820152602481018490529082169063a9059cbb90604401612b3e565b60076020528160005260406000208181548110612bd657600080fd5b6000918252602090912001546001600160a01b03169150829050565b600080546001600160a01b03163314612c1857612c1160016006613569565b9050610d3f565b6001600160a01b0383166000908152600860205260409020805460ff16612c4d57612c4560096007613569565b915050610d3f565b60408051602080820183528582528251908101909252670c7d713b49da0000825290612c7b81835190511090565b15612c9657612c8c60066008613569565b9350505050610d3f565b8415801590612d1257506004805460405163fc57d4df60e01b81526001600160a01b038981169382019390935291169063fc57d4df90602401602060405180830381865afa158015612cec573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d109190614320565b155b15612d2357612c8c600d6009613569565b60018301805490869055604080516001600160a01b0389168152602081018390529081018790527f70483e6592cd5182d45ac970e05bc62cdcc90e9d8ef2c2dbe686cf383bcd7fc59060600160405180910390a16000979650505050505050565b600080612d92858585613702565b90508015611647579050611091565b6040516361bfb47160e11b815233600482015260009082908290819081906001600160a01b0385169063c37f68e290602401608060405180830381865afa158015612df0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e149190614382565b5092509250925082600014612e795760405162461bcd60e51b815260206004820152602560248201527f657869744d61726b65743a206765744163636f756e74536e617073686f742066604482015264185a5b195960da1b606482015260840161093c565b8015612e9557612e8b600c6002613569565b9695505050505050565b6000612ea2873385613702565b90508015612ec257612eb7600e600383613930565b979650505050505050565b6001600160a01b0385166000908152600860209081526040808320338452600281019092529091205460ff16612f0057600098975050505050505050565b3360009081526002820160209081526040808320805460ff191690556007825280832080548251818502810185019093528083529192909190830182828015612f7257602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311612f54575b5050835193945083925060009150505b82811015612fd657896001600160a01b0316848281518110612fa657612fa66141db565b60200260200101516001600160a01b031603612fc457809150612fd6565b80612fce8161422b565b915050612f82565b50818110612fe657612fe661436c565b33600090815260076020526040902080548190613005906001906143b8565b81548110613015576130156141db565b9060005260206000200160009054906101000a90046001600160a01b0316818381548110613045576130456141db565b9060005260206000200160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555080805480613083576130836143cb565b600082815260209020810160001990810180546001600160a01b03191690550190556040517fe699a64c18b07ac5b7301aa273f36a2287239eb9501d81950672794afba29a0d906130ec908c9033906001600160a01b0392831681529116602082015260400190565b60405180910390a160009c9b505050505050505050505050565b6012546001600160a01b03161561317f576012546040516356e3af2960e11b81526001600160a01b039091169063adc75e529061314c90859085906000906004016141f1565b600060405180830381600087803b15801561316657600080fd5b505af115801561317a573d6000803e3d6000fd5b505050505b5050565b6000806000613190613b66565b6001600160a01b0388166000908152600760209081526040808320805482518185028101850190935280835284938301828280156131f757602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116131d9575b5050505050905060005b815181101561345d57600082828151811061321e5761321e6141db565b60209081029190910101516040516361bfb47160e11b81526001600160a01b038e811660048301529192509082169063c37f68e290602401608060405180830381865afa158015613273573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132979190614382565b608089015260608801526040870152935083156132c357600f60008097509750975050505050506113ee565b60408051602080820183526001600160a01b0384811660008181526008845285902060010154845260c08a01939093528351808301855260808a0151815260e08a015260048054855163fc57d4df60e01b815291820194909452935192169263fc57d4df92602480830193928290030181865afa158015613348573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061336c9190614320565b60a0860181905260000361338f57600d60008097509750975050505050506113ee565b604080516020810190915260a0860151815261010086015260c085015160e08601516133c9916133be916138ae565b8661010001516138ae565b6101208601819052604086015186516133e39291906139a8565b8552610100850151606086015160208701516134009291906139a8565b60208601526001600160a01b03808c169082160361344a5761342c8561012001518b87602001516139a8565b60208601819052610100860151613444918b906139a8565b60208601525b50806134558161422b565b915050613201565b5060208301518351111561348e576020830151835160009161347e916143b8565b60009550955095505050506113ee565b600080846000015185602001516134a591906143b8565b9550955095505050506113ee565b60006110918383604051806040016040528060118152602001706164646974696f6e206f766572666c6f7760781b8152506139c9565b60006110918383604051806040016040528060158152602001747375627472616374696f6e20756e646572666c6f7760581b815250613a03565b6012546001600160a01b03161561317f57601254604051637076c88b60e11b81526001600160a01b039091169063e0ed91169061314c90859085906000906004016141f1565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa083601181111561359e5761359e614339565b8360148111156135b0576135b0614339565b60408051928352602083019190915260009082015260600160405180910390a182601181111561109157611091614339565b60008060006135f5846000806000613183565b9250925092509193909250565b60008061360f8484613a32565b9050610ef981613a5a565b60005b600c548110156136af57816001600160a01b0316600c8281548110613644576136446141db565b6000918252602090912001546001600160a01b03160361369d5760405162461bcd60e51b81526020600482015260146024820152731b585c9ad95d08185b1c9958591e48185919195960621b604482015260640161093c565b806136a78161422b565b91505061361d565b50600c80546001810182556000919091527fdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c70180546001600160a01b0319166001600160a01b0392909216919091179055565b6001600160a01b03831660009081526008602052604081205460ff16613729576009611475565b6001600160a01b038085166000908152600860209081526040808320938716835260029093019052205460ff16613761576000611475565b6000806137718587866000613183565b9193509091506000905082601181111561378d5761378d614339565b146137ad578160118111156137a4576137a4614339565b92505050611091565b80156122185760046137a4565b6001600160a01b0382166000908152600860205260408120805460ff166137e5576009915050610d3f565b6001600160a01b038316600090815260028201602052604090205460ff161515600103613816576000915050610d3f565b6001600160a01b03838116600081815260028401602090815260408083208054600160ff199091168117909155600783528184208054918201815584529282902090920180546001600160a01b031916948916948517905581519384528301919091527f3ab23ab0d51cccc0c3085aec51f99228625aa1a922b3a8ca89a26b0f2027a1a5910160405180910390a15060009392505050565b6040805160208101909152600081526040518060200160405280670de0b6b3a76400006138e386600001518660000151613a72565b6138ed91906143e1565b90529392505050565b60408051602081019091526000815260405180602001604052806138ed6139298660000151670de0b6b3a7640000613a72565b8551613ab4565b60007f45b96fe442630264581b197e84bbada861235052c5a1aadfff9ea4e40a969aa084601181111561396557613965614339565b84601481111561397757613977614339565b604080519283526020830191909152810184905260600160405180910390a1836011811115610ef957610ef9614339565b6000806139b58585613a32565b90506116546139c382613a5a565b846134b3565b6000806139d68486614403565b905082858210156139fa5760405162461bcd60e51b815260040161093c9190614416565b50949350505050565b60008184841115613a275760405162461bcd60e51b815260040161093c9190614416565b50610ef983856143b8565b60408051602081019091526000815260405180602001604052806138ed856000015185613a72565b8051600090610d3f90670de0b6b3a7640000906143e1565b600061109183836040518060400160405280601781526020017f6d756c7469706c69636174696f6e206f766572666c6f77000000000000000000815250613ae7565b600061109183836040518060400160405280600e81526020016d646976696465206279207a65726f60901b815250613b3a565b6000831580613af4575082155b15613b0157506000611091565b6000613b0d8486614464565b905083613b1a86836143e1565b1483906139fa5760405162461bcd60e51b815260040161093c9190614416565b60008183613b5b5760405162461bcd60e51b815260040161093c9190614416565b50610ef983856143e1565b604051806101400160405280600081526020016000815260200160008152602001600081526020016000815260200160008152602001613bb26040518060200160405280600081525090565b8152602001613bcd6040518060200160405280600081525090565b8152602001613be86040518060200160405280600081525090565b8152602001613c036040518060200160405280600081525090565b905290565b6001600160a01b0381168114610ec057600080fd5b600060208284031215613c2f57600080fd5b813561109181613c08565b634e487b7160e01b600052604160045260246000fd5b604051601f8201601f1916810167ffffffffffffffff81118282101715613c7957613c79613c3a565b604052919050565b600067ffffffffffffffff821115613c9b57613c9b613c3a565b5060051b60200190565b600082601f830112613cb657600080fd5b81356020613ccb613cc683613c81565b613c50565b82815260059290921b84018101918181019086841115613cea57600080fd5b8286015b84811015613d0e578035613d0181613c08565b8352918301918301613cee565b509695505050505050565b600082601f830112613d2a57600080fd5b81356020613d3a613cc683613c81565b82815260059290921b84018101918181019086841115613d5957600080fd5b8286015b84811015613d0e578035613d7081613c08565b8352918301918301613d5d565b8015158114610ec057600080fd5b60008060008060808587031215613da157600080fd5b843567ffffffffffffffff80821115613db957600080fd5b613dc588838901613ca5565b95506020870135915080821115613ddb57600080fd5b50613de887828801613d19565b9350506040850135613df981613d7d565b91506060850135613e0981613d7d565b939692955090935050565b60008060408385031215613e2757600080fd5b8235613e3281613c08565b91506020830135613e4281613d7d565b809150509250929050565b60008060008060808587031215613e6357600080fd5b8435613e6e81613c08565b93506020850135613e7e81613c08565b92506040850135613e8e81613c08565b9396929550929360600135925050565b600060208284031215613eb057600080fd5b813561109181613d7d565b600060208284031215613ecd57600080fd5b5035919050565b60008060408385031215613ee757600080fd5b8235613ef281613c08565b9150602083013567ffffffffffffffff811115613f0e57600080fd5b613f1a85828601613d19565b9150509250929050565b60008060008060808587031215613f3a57600080fd5b8435613f4581613c08565b93506020850135613f5581613c08565b93969395505050506040820135916060013590565b600080600060608486031215613f7f57600080fd5b8335613f8a81613c08565b92506020840135613f9a81613c08565b929592945050506040919091013590565b60008083601f840112613fbd57600080fd5b50813567ffffffffffffffff811115613fd557600080fd5b6020830191508360208260051b8501011115613ff057600080fd5b9250929050565b6000806000806040858703121561400d57600080fd5b843567ffffffffffffffff8082111561402557600080fd5b61403188838901613fab565b9096509450602087013591508082111561404a57600080fd5b5061405787828801613fab565b95989497509550505050565b600080600080600060a0868803121561407b57600080fd5b853561408681613c08565b9450602086013561409681613c08565b935060408601356140a681613c08565b925060608601356140b681613c08565b949793965091946080013592915050565b600080604083850312156140da57600080fd5b82356140e581613c08565b91506020830135613e4281613c08565b6020808252825182820181905260009190848201906040850190845b818110156141365783516001600160a01b031683529284019291840191600101614111565b50909695505050505050565b60006020828403121561415457600080fd5b813567ffffffffffffffff81111561416b57600080fd5b610ef984828501613ca5565b6020808252825182820181905260009190848201906040850190845b8181101561413657835183529284019291840191600101614193565b600080604083850312156141c257600080fd5b82356141cd81613c08565b946020939093013593505050565b634e487b7160e01b600052603260045260246000fd5b6001600160a01b039384168152919092166020820152901515604082015260600190565b634e487b7160e01b600052601160045260246000fd5b60006001820161423d5761423d614215565b5060010190565b60208082526028908201527f63616e6e6f742070617573652061206d61726b65742074686174206973206e6f6040820152671d081b1a5cdd195960c21b606082015260800190565b60208082526027908201527f6f6e6c7920706175736520677561726469616e20616e642061646d696e2063616040820152666e20706175736560c81b606082015260800190565b6020808252601690820152756f6e6c792061646d696e2063616e20756e706175736560501b604082015260600190565b60006020828403121561431557600080fd5b815161109181613c08565b60006020828403121561433257600080fd5b5051919050565b634e487b7160e01b600052602160045260246000fd5b60006020828403121561436157600080fd5b815161109181613d7d565b634e487b7160e01b600052600160045260246000fd5b6000806000806080858703121561439857600080fd5b505082516020840151604085015160609095015191969095509092509050565b81810381811115610d3f57610d3f614215565b634e487b7160e01b600052603160045260246000fd5b6000826143fe57634e487b7160e01b600052601260045260246000fd5b500490565b80820180821115610d3f57610d3f614215565b600060208083528351808285015260005b8181101561444357858101830151858201604001528201614427565b506000604082860101526040601f19601f8301168501019250505092915050565b8082028115828204841417610d3f57610d3f61421556fea2646970667358221220f3406a2a069958691bfb6e07578a8ce72fb0a794e27cb468a909b4357be4667f64736f6c63430008130033
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 30 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.