// SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.25; import { Ownable2StepUpgradeable } from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol"; import { ResilientOracleInterface } from "@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol"; import { AccessControlledV8 } from "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol"; import { IPrime } from "@venusprotocol/venus-protocol/contracts/Tokens/Prime/Interfaces/IPrime.sol"; import { ComptrollerInterface, Action } from "./ComptrollerInterface.sol"; import { ComptrollerStorage } from "./ComptrollerStorage.sol"; import { ExponentialNoError } from "./ExponentialNoError.sol"; import { VToken } from "./VToken.sol"; import { RewardsDistributor } from "./Rewards/RewardsDistributor.sol"; import { MaxLoopsLimitHelper } from "./MaxLoopsLimitHelper.sol"; import { ensureNonzeroAddress } from "./lib/validators.sol"; /** * @title Comptroller * @author Venus * @notice The Comptroller is designed to provide checks for all minting, redeeming, transferring, borrowing, lending, repaying, liquidating, * and seizing done by the `vToken` contract. Each pool has one `Comptroller` checking these interactions across markets. When a user interacts * with a given market by one of these main actions, a call is made to a corresponding hook in the associated `Comptroller`, which either allows * or reverts the transaction. These hooks also update supply and borrow rewards as they are called. The comptroller holds the logic for assessing * liquidity snapshots of an account via the collateral factor and liquidation threshold. This check determines the collateral needed for a borrow, * as well as how much of a borrow may be liquidated. A user may borrow a portion of their collateral with the maximum amount determined by the * markets collateral factor. However, if their borrowed amount exceeds an amount calculated using the market’s corresponding liquidation threshold, * the borrow is eligible for liquidation. * * The `Comptroller` also includes two functions `liquidateAccount()` and `healAccount()`, which are meant to handle accounts that do not exceed * the `minLiquidatableCollateral` for the `Comptroller`: * * - `healAccount()`: This function is called to seize all of a given user’s collateral, requiring the `msg.sender` repay a certain percentage * of the debt calculated by `collateral/(borrows*liquidationIncentive)`. The function can only be called if the calculated percentage does not exceed * 100%, because otherwise no `badDebt` would be created and `liquidateAccount()` should be used instead. The difference in the actual amount of debt * and debt paid off is recorded as `badDebt` for each market, which can then be auctioned off for the risk reserves of the associated pool. * - `liquidateAccount()`: This function can only be called if the collateral seized will cover all borrows of an account, as well as the liquidation * incentive. Otherwise, the pool will incur bad debt, in which case the function `healAccount()` should be used instead. This function skips the logic * verifying that the repay amount does not exceed the close factor. */ contract Comptroller is Ownable2StepUpgradeable, AccessControlledV8, ComptrollerStorage, ComptrollerInterface, ExponentialNoError, MaxLoopsLimitHelper { // PoolRegistry, immutable to save on gas /// @custom:oz-upgrades-unsafe-allow state-variable-immutable address public immutable poolRegistry; /// @notice Emitted when an account enters a market event MarketEntered(VToken indexed vToken, address indexed account); /// @notice Emitted when an account exits a market event MarketExited(VToken indexed vToken, address indexed account); /// @notice Emitted when close factor is changed by admin event NewCloseFactor(uint256 oldCloseFactorMantissa, uint256 newCloseFactorMantissa); /// @notice Emitted when a collateral factor is changed by admin event NewCollateralFactor(VToken vToken, uint256 oldCollateralFactorMantissa, uint256 newCollateralFactorMantissa); /// @notice Emitted when liquidation threshold is changed by admin event NewLiquidationThreshold( VToken vToken, uint256 oldLiquidationThresholdMantissa, uint256 newLiquidationThresholdMantissa ); /// @notice Emitted when liquidation incentive is changed by admin event NewLiquidationIncentive(uint256 oldLiquidationIncentiveMantissa, uint256 newLiquidationIncentiveMantissa); /// @notice Emitted when price oracle is changed event NewPriceOracle(ResilientOracleInterface oldPriceOracle, ResilientOracleInterface newPriceOracle); /// @notice Emitted when an action is paused on a market event ActionPausedMarket(VToken vToken, Action action, bool pauseState); /// @notice Emitted when borrow cap for a vToken is changed event NewBorrowCap(VToken indexed vToken, uint256 newBorrowCap); /// @notice Emitted when the collateral threshold (in USD) for non-batch liquidations is changed event NewMinLiquidatableCollateral(uint256 oldMinLiquidatableCollateral, uint256 newMinLiquidatableCollateral); /// @notice Emitted when supply cap for a vToken is changed event NewSupplyCap(VToken indexed vToken, uint256 newSupplyCap); /// @notice Emitted when a rewards distributor is added event NewRewardsDistributor(address indexed rewardsDistributor, address indexed rewardToken); /// @notice Emitted when a market is supported event MarketSupported(VToken vToken); /// @notice Emitted when prime token contract address is changed event NewPrimeToken(IPrime oldPrimeToken, IPrime newPrimeToken); /// @notice Emitted when forced liquidation is enabled or disabled for a market event IsForcedLiquidationEnabledUpdated(address indexed vToken, bool enable); /// @notice Emitted when a market is unlisted event MarketUnlisted(address indexed vToken); /// @notice Emitted when the borrowing or redeeming delegate rights are updated for an account event DelegateUpdated(address indexed approver, address indexed delegate, bool approved); /// @notice Thrown when collateral factor exceeds the upper bound error InvalidCollateralFactor(); /// @notice Thrown when liquidation threshold exceeds the collateral factor error InvalidLiquidationThreshold(); /// @notice Thrown when the action is only available to specific sender, but the real sender was different error UnexpectedSender(address expectedSender, address actualSender); /// @notice Thrown when the oracle returns an invalid price for some asset error PriceError(address vToken); /// @notice Thrown if VToken unexpectedly returned a nonzero error code while trying to get account snapshot error SnapshotError(address vToken, address user); /// @notice Thrown when the market is not listed error MarketNotListed(address market); /// @notice Thrown when a market has an unexpected comptroller error ComptrollerMismatch(); /// @notice Thrown when user is not member of market error MarketNotCollateral(address vToken, address user); /// @notice Thrown when borrow action is not paused error BorrowActionNotPaused(); /// @notice Thrown when mint action is not paused error MintActionNotPaused(); /// @notice Thrown when redeem action is not paused error RedeemActionNotPaused(); /// @notice Thrown when repay action is not paused error RepayActionNotPaused(); /// @notice Thrown when seize action is not paused error SeizeActionNotPaused(); /// @notice Thrown when exit market action is not paused error ExitMarketActionNotPaused(); /// @notice Thrown when transfer action is not paused error TransferActionNotPaused(); /// @notice Thrown when enter market action is not paused error EnterMarketActionNotPaused(); /// @notice Thrown when liquidate action is not paused error LiquidateActionNotPaused(); /// @notice Thrown when borrow cap is not zero error BorrowCapIsNotZero(); /// @notice Thrown when supply cap is not zero error SupplyCapIsNotZero(); /// @notice Thrown when collateral factor is not zero error CollateralFactorIsNotZero(); /** * @notice Thrown during the liquidation if user's total collateral amount is lower than * a predefined threshold. In this case only batch liquidations (either liquidateAccount * or healAccount) are available. */ error MinimalCollateralViolated(uint256 expectedGreaterThan, uint256 actual); error CollateralExceedsThreshold(uint256 expectedLessThanOrEqualTo, uint256 actual); error InsufficientCollateral(uint256 collateralToSeize, uint256 availableCollateral); /// @notice Thrown when the account doesn't have enough liquidity to redeem or borrow error InsufficientLiquidity(); /// @notice Thrown when trying to liquidate a healthy account error InsufficientShortfall(); /// @notice Thrown when trying to repay more than allowed by close factor error TooMuchRepay(); /// @notice Thrown if the user is trying to exit a market in which they have an outstanding debt error NonzeroBorrowBalance(); /// @notice Thrown when trying to perform an action that is paused error ActionPaused(address market, Action action); /// @notice Thrown when trying to add a market that is already listed error MarketAlreadyListed(address market); /// @notice Thrown if the supply cap is exceeded error SupplyCapExceeded(address market, uint256 cap); /// @notice Thrown if the borrow cap is exceeded error BorrowCapExceeded(address market, uint256 cap); /// @notice Thrown if delegate approval status is already set to the requested value error DelegationStatusUnchanged(); /// @param poolRegistry_ Pool registry address /// @custom:oz-upgrades-unsafe-allow constructor /// @custom:error ZeroAddressNotAllowed is thrown when pool registry address is zero constructor(address poolRegistry_) { ensureNonzeroAddress(poolRegistry_); poolRegistry = poolRegistry_; _disableInitializers(); } /** * @param loopLimit Limit for the loops can iterate to avoid the DOS * @param accessControlManager Access control manager contract address */ function initialize(uint256 loopLimit, address accessControlManager) external initializer { __Ownable2Step_init(); __AccessControlled_init_unchained(accessControlManager); _setMaxLoopsLimit(loopLimit); } /** * @notice Add assets to be included in account liquidity calculation; enabling them to be used as collateral * @param vTokens The list of addresses of the vToken markets to be enabled * @return errors An array of NO_ERROR for compatibility with Venus core tooling * @custom:event MarketEntered is emitted for each market on success * @custom:error ActionPaused error is thrown if entering any of the markets is paused * @custom:error MarketNotListed error is thrown if any of the markets is not listed * @custom:access Not restricted */ function enterMarkets(address[] memory vTokens) external override returns (uint256[] memory) { uint256 len = vTokens.length; uint256[] memory results = new uint256[](len); for (uint256 i; i < len; ++i) { VToken vToken = VToken(vTokens[i]); _addToMarket(vToken, msg.sender); results[i] = NO_ERROR; } return results; } /** * @notice Unlist a market by setting isListed to false * @dev Checks if all actions are paused, borrow/supply caps is set to 0 and collateral factor is to 0. * @param market The address of the market (token) to unlist * @return uint256 Always NO_ERROR for compatibility with Venus core tooling * @custom:event MarketUnlisted is emitted on success * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error BorrowActionNotPaused error is thrown if borrow action is not paused * @custom:error MintActionNotPaused error is thrown if mint action is not paused * @custom:error RedeemActionNotPaused error is thrown if redeem action is not paused * @custom:error RepayActionNotPaused error is thrown if repay action is not paused * @custom:error EnterMarketActionNotPaused error is thrown if enter market action is not paused * @custom:error LiquidateActionNotPaused error is thrown if liquidate action is not paused * @custom:error BorrowCapIsNotZero error is thrown if borrow cap is not zero * @custom:error SupplyCapIsNotZero error is thrown if supply cap is not zero * @custom:error CollateralFactorIsNotZero error is thrown if collateral factor is not zero */ function unlistMarket(address market) external returns (uint256) { _checkAccessAllowed("unlistMarket(address)"); Market storage _market = markets[market]; if (!_market.isListed) { revert MarketNotListed(market); } if (!actionPaused(market, Action.BORROW)) { revert BorrowActionNotPaused(); } if (!actionPaused(market, Action.MINT)) { revert MintActionNotPaused(); } if (!actionPaused(market, Action.REDEEM)) { revert RedeemActionNotPaused(); } if (!actionPaused(market, Action.REPAY)) { revert RepayActionNotPaused(); } if (!actionPaused(market, Action.SEIZE)) { revert SeizeActionNotPaused(); } if (!actionPaused(market, Action.ENTER_MARKET)) { revert EnterMarketActionNotPaused(); } if (!actionPaused(market, Action.LIQUIDATE)) { revert LiquidateActionNotPaused(); } if (!actionPaused(market, Action.TRANSFER)) { revert TransferActionNotPaused(); } if (!actionPaused(market, Action.EXIT_MARKET)) { revert ExitMarketActionNotPaused(); } if (borrowCaps[market] != 0) { revert BorrowCapIsNotZero(); } if (supplyCaps[market] != 0) { revert SupplyCapIsNotZero(); } if (_market.collateralFactorMantissa != 0) { revert CollateralFactorIsNotZero(); } _market.isListed = false; emit MarketUnlisted(market); return NO_ERROR; } /** * @notice Grants or revokes the borrowing or redeeming delegate rights to / from an account * If allowed, the delegate will be able to borrow funds on behalf of the sender * Upon a delegated borrow, the delegate will receive the funds, and the borrower * will see the debt on their account * Upon a delegated redeem, the delegate will receive the redeemed amount and the approver * will see a deduction in his vToken balance * @param delegate The address to update the rights for * @param approved Whether to grant (true) or revoke (false) the borrowing or redeeming rights * @custom:event DelegateUpdated emits on success * @custom:error ZeroAddressNotAllowed is thrown when delegate address is zero * @custom:error DelegationStatusUnchanged is thrown if approval status is already set to the requested value * @custom:access Not restricted */ function updateDelegate(address delegate, bool approved) external { ensureNonzeroAddress(delegate); if (approvedDelegates[msg.sender][delegate] == approved) { revert DelegationStatusUnchanged(); } approvedDelegates[msg.sender][delegate] = approved; emit DelegateUpdated(msg.sender, delegate, approved); } /** * @notice Removes asset from sender's account liquidity calculation; disabling them as collateral * @dev Sender must not have an outstanding borrow balance in the asset, * or be providing necessary collateral for an outstanding borrow. * @param vTokenAddress The address of the asset to be removed * @return error Always NO_ERROR for compatibility with Venus core tooling * @custom:event MarketExited is emitted on success * @custom:error ActionPaused error is thrown if exiting the market is paused * @custom:error NonzeroBorrowBalance error is thrown if the user has an outstanding borrow in this market * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error InsufficientLiquidity error is thrown if exiting the market would lead to user's insolvency * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted */ function exitMarket(address vTokenAddress) external override returns (uint256) { _checkActionPauseState(vTokenAddress, Action.EXIT_MARKET); VToken vToken = VToken(vTokenAddress); /* Get sender tokensHeld and amountOwed underlying from the vToken */ (uint256 tokensHeld, uint256 amountOwed, ) = _safeGetAccountSnapshot(vToken, msg.sender); /* Fail if the sender has a borrow balance */ if (amountOwed != 0) { revert NonzeroBorrowBalance(); } /* Fail if the sender is not permitted to redeem all of their tokens */ _checkRedeemAllowed(vTokenAddress, msg.sender, tokensHeld); Market storage marketToExit = markets[address(vToken)]; /* Return true if the sender is not already ‘in’ the market */ if (!marketToExit.accountMembership[msg.sender]) { return NO_ERROR; } /* Set vToken account membership to false */ delete marketToExit.accountMembership[msg.sender]; /* Delete vToken from the account’s list of assets */ // load into memory for faster iteration VToken[] memory userAssetList = accountAssets[msg.sender]; uint256 len = userAssetList.length; uint256 assetIndex = len; for (uint256 i; i < len; ++i) { if (userAssetList[i] == vToken) { 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 VToken[] storage storedList = accountAssets[msg.sender]; storedList[assetIndex] = storedList[storedList.length - 1]; storedList.pop(); emit MarketExited(vToken, msg.sender); return NO_ERROR; } /*** Policy Hooks ***/ /** * @notice Checks if the account should be allowed to mint tokens in the given market * @param vToken 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 * @custom:error ActionPaused error is thrown if supplying to this market is paused * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error SupplyCapExceeded error is thrown if the total supply exceeds the cap after minting * @custom:access Not restricted */ function preMintHook(address vToken, address minter, uint256 mintAmount) external override { _checkActionPauseState(vToken, Action.MINT); if (!markets[vToken].isListed) { revert MarketNotListed(address(vToken)); } uint256 supplyCap = supplyCaps[vToken]; // Skipping the cap check for uncapped coins to save some gas if (supplyCap != type(uint256).max) { uint256 vTokenSupply = VToken(vToken).totalSupply(); Exp memory exchangeRate = Exp({ mantissa: VToken(vToken).exchangeRateStored() }); uint256 nextTotalSupply = mul_ScalarTruncateAddUInt(exchangeRate, vTokenSupply, mintAmount); if (nextTotalSupply > supplyCap) { revert SupplyCapExceeded(vToken, supplyCap); } } // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenSupplyIndex(vToken); rewardsDistributor.distributeSupplierRewardToken(vToken, minter); } } /** * @notice Validates mint, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vToken Asset being minted * @param minter The address minting the tokens * @param actualMintAmount The amount of the underlying asset being minted * @param mintTokens The number of tokens being minted */ // solhint-disable-next-line no-unused-vars function mintVerify(address vToken, address minter, uint256 actualMintAmount, uint256 mintTokens) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(minter, vToken); } } /** * @notice Checks if the account should be allowed to redeem tokens in the given market * @param vToken The market to verify the redeem against * @param redeemer The account which would redeem the tokens * @param redeemTokens The number of vTokens to exchange for the underlying asset in the market * @custom:error ActionPaused error is thrown if withdrawals are paused in this market * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error InsufficientLiquidity error is thrown if the withdrawal would lead to user's insolvency * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted */ function preRedeemHook(address vToken, address redeemer, uint256 redeemTokens) external override { _checkActionPauseState(vToken, Action.REDEEM); _checkRedeemAllowed(vToken, redeemer, redeemTokens); // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenSupplyIndex(vToken); rewardsDistributor.distributeSupplierRewardToken(vToken, redeemer); } } /** * @notice Validates redeem, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vToken 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 vToken, address redeemer, uint256 redeemAmount, uint256 redeemTokens) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(redeemer, vToken); } } /** * @notice Validates repayBorrow, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vToken Asset being repaid * @param payer The address repaying the borrow * @param borrower The address of the borrower * @param actualRepayAmount The amount of underlying being repaid */ function repayBorrowVerify( address vToken, address payer, // solhint-disable-line no-unused-vars address borrower, uint256 actualRepayAmount, // solhint-disable-line no-unused-vars uint256 borrowerIndex // solhint-disable-line no-unused-vars ) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(borrower, vToken); } } /** * @notice Validates liquidateBorrow, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vTokenBorrowed Asset which was borrowed by the borrower * @param vTokenCollateral 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 actualRepayAmount The amount of underlying being repaid * @param seizeTokens The amount of collateral token that will be seized */ function liquidateBorrowVerify( address vTokenBorrowed, address vTokenCollateral, // solhint-disable-line no-unused-vars address liquidator, address borrower, uint256 actualRepayAmount, // solhint-disable-line no-unused-vars uint256 seizeTokens // solhint-disable-line no-unused-vars ) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(borrower, vTokenBorrowed); prime.accrueInterestAndUpdateScore(liquidator, vTokenBorrowed); } } /** * @notice Validates seize, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vTokenCollateral Asset which was used as collateral and will be seized * @param vTokenBorrowed 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 seizeVerify( address vTokenCollateral, address vTokenBorrowed, // solhint-disable-line no-unused-vars address liquidator, address borrower, uint256 seizeTokens // solhint-disable-line no-unused-vars ) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(borrower, vTokenCollateral); prime.accrueInterestAndUpdateScore(liquidator, vTokenCollateral); } } /** * @notice Validates transfer, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vToken Asset being transferred * @param src The account which sources the tokens * @param dst The account which receives the tokens * @param transferTokens The number of vTokens to transfer */ // solhint-disable-next-line no-unused-vars function transferVerify(address vToken, address src, address dst, uint256 transferTokens) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(src, vToken); prime.accrueInterestAndUpdateScore(dst, vToken); } } /** * @notice Checks if the account should be allowed to borrow the underlying asset of the given market * @param vToken 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 * @custom:error ActionPaused error is thrown if borrowing is paused in this market * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error InsufficientLiquidity error is thrown if there is not enough collateral to borrow * @custom:error BorrowCapExceeded is thrown if the borrow cap will be exceeded should this borrow succeed * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted if vToken is enabled as collateral, otherwise only vToken */ /// disable-eslint function preBorrowHook(address vToken, address borrower, uint256 borrowAmount) external override { _checkActionPauseState(vToken, Action.BORROW); if (!markets[vToken].isListed) { revert MarketNotListed(address(vToken)); } if (!markets[vToken].accountMembership[borrower]) { // only vTokens may call borrowAllowed if borrower not in market _checkSenderIs(vToken); // attempt to add borrower to the market or revert _addToMarket(VToken(msg.sender), borrower); } // Update the prices of tokens updatePrices(borrower); if (oracle.getUnderlyingPrice(vToken) == 0) { revert PriceError(address(vToken)); } uint256 borrowCap = borrowCaps[vToken]; // Skipping the cap check for uncapped coins to save some gas if (borrowCap != type(uint256).max) { uint256 totalBorrows = VToken(vToken).totalBorrows(); uint256 badDebt = VToken(vToken).badDebt(); uint256 nextTotalBorrows = totalBorrows + borrowAmount + badDebt; if (nextTotalBorrows > borrowCap) { revert BorrowCapExceeded(vToken, borrowCap); } } AccountLiquiditySnapshot memory snapshot = _getHypotheticalLiquiditySnapshot( borrower, VToken(vToken), 0, borrowAmount, _getCollateralFactor ); if (snapshot.shortfall > 0) { revert InsufficientLiquidity(); } Exp memory borrowIndex = Exp({ mantissa: VToken(vToken).borrowIndex() }); // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenBorrowIndex(vToken, borrowIndex); rewardsDistributor.distributeBorrowerRewardToken(vToken, borrower, borrowIndex); } } /** * @notice Validates borrow, accrues interest and updates score in prime. Reverts on rejection. May emit logs. * @param vToken Asset whose underlying is being borrowed * @param borrower The address borrowing the underlying * @param borrowAmount The amount of the underlying asset requested to borrow */ // solhint-disable-next-line no-unused-vars function borrowVerify(address vToken, address borrower, uint256 borrowAmount) external { if (address(prime) != address(0)) { prime.accrueInterestAndUpdateScore(borrower, vToken); } } /** * @notice Checks if the account should be allowed to repay a borrow in the given market * @param vToken The market to verify the repay against * @param borrower The account which would borrowed the asset * @custom:error ActionPaused error is thrown if repayments are paused in this market * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:access Not restricted */ function preRepayHook(address vToken, address borrower) external override { _checkActionPauseState(vToken, Action.REPAY); oracle.updatePrice(vToken); if (!markets[vToken].isListed) { revert MarketNotListed(address(vToken)); } // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { Exp memory borrowIndex = Exp({ mantissa: VToken(vToken).borrowIndex() }); RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenBorrowIndex(vToken, borrowIndex); rewardsDistributor.distributeBorrowerRewardToken(vToken, borrower, borrowIndex); } } /** * @notice Checks if the liquidation should be allowed to occur * @param vTokenBorrowed Asset which was borrowed by the borrower * @param vTokenCollateral Asset which was used as collateral and will be seized * @param borrower The address of the borrower * @param repayAmount The amount of underlying being repaid * @param skipLiquidityCheck Allows the borrow to be liquidated regardless of the account liquidity * @custom:error ActionPaused error is thrown if liquidations are paused in this market * @custom:error MarketNotListed error is thrown if either collateral or borrowed token is not listed * @custom:error TooMuchRepay error is thrown if the liquidator is trying to repay more than allowed by close factor * @custom:error MinimalCollateralViolated is thrown if the users' total collateral is lower than the threshold for non-batch liquidations * @custom:error InsufficientShortfall is thrown when trying to liquidate a healthy account * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset */ function preLiquidateHook( address vTokenBorrowed, address vTokenCollateral, address borrower, uint256 repayAmount, bool skipLiquidityCheck ) external override { // Pause Action.LIQUIDATE on BORROWED TOKEN to prevent liquidating it. // If we want to pause liquidating to vTokenCollateral, we should pause // Action.SEIZE on it _checkActionPauseState(vTokenBorrowed, Action.LIQUIDATE); // Update the prices of tokens updatePrices(borrower); if (!markets[vTokenBorrowed].isListed) { revert MarketNotListed(address(vTokenBorrowed)); } if (!markets[vTokenCollateral].isListed) { revert MarketNotListed(address(vTokenCollateral)); } uint256 borrowBalance = VToken(vTokenBorrowed).borrowBalanceStored(borrower); /* Allow accounts to be liquidated if it is a forced liquidation */ if (skipLiquidityCheck || isForcedLiquidationEnabled[vTokenBorrowed]) { if (repayAmount > borrowBalance) { revert TooMuchRepay(); } return; } /* The borrower must have shortfall and collateral > threshold in order to be liquidatable */ AccountLiquiditySnapshot memory snapshot = _getCurrentLiquiditySnapshot(borrower, _getLiquidationThreshold); if (snapshot.totalCollateral <= minLiquidatableCollateral) { /* The liquidator should use either liquidateAccount or healAccount */ revert MinimalCollateralViolated(minLiquidatableCollateral, snapshot.totalCollateral); } if (snapshot.shortfall == 0) { revert InsufficientShortfall(); } /* The liquidator may not repay more than what is allowed by the closeFactor */ uint256 maxClose = mul_ScalarTruncate(Exp({ mantissa: closeFactorMantissa }), borrowBalance); if (repayAmount > maxClose) { revert TooMuchRepay(); } } /** * @notice Checks if the seizing of assets should be allowed to occur * @param vTokenCollateral Asset which was used as collateral and will be seized * @param seizerContract Contract that tries to seize the asset (either borrowed vToken or Comptroller) * @param liquidator The address repaying the borrow and seizing the collateral * @param borrower The address of the borrower * @custom:error ActionPaused error is thrown if seizing this type of collateral is paused * @custom:error MarketNotListed error is thrown if either collateral or borrowed token is not listed * @custom:error ComptrollerMismatch error is when seizer contract or seized asset belong to different pools * @custom:access Not restricted */ function preSeizeHook( address vTokenCollateral, address seizerContract, address liquidator, address borrower ) external override { // Pause Action.SEIZE on COLLATERAL to prevent seizing it. // If we want to pause liquidating vTokenBorrowed, we should pause // Action.LIQUIDATE on it _checkActionPauseState(vTokenCollateral, Action.SEIZE); Market storage market = markets[vTokenCollateral]; if (!market.isListed) { revert MarketNotListed(vTokenCollateral); } if (seizerContract == address(this)) { // If Comptroller is the seizer, just check if collateral's comptroller // is equal to the current address if (address(VToken(vTokenCollateral).comptroller()) != address(this)) { revert ComptrollerMismatch(); } } else { // If the seizer is not the Comptroller, check that the seizer is a // listed market, and that the markets' comptrollers match if (!markets[seizerContract].isListed) { revert MarketNotListed(seizerContract); } if (VToken(vTokenCollateral).comptroller() != VToken(seizerContract).comptroller()) { revert ComptrollerMismatch(); } } if (!market.accountMembership[borrower]) { revert MarketNotCollateral(vTokenCollateral, borrower); } // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenSupplyIndex(vTokenCollateral); rewardsDistributor.distributeSupplierRewardToken(vTokenCollateral, borrower); rewardsDistributor.distributeSupplierRewardToken(vTokenCollateral, liquidator); } } /** * @notice Checks if the account should be allowed to transfer tokens in the given market * @param vToken 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 vTokens to transfer * @custom:error ActionPaused error is thrown if withdrawals are paused in this market * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error InsufficientLiquidity error is thrown if the withdrawal would lead to user's insolvency * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted */ function preTransferHook(address vToken, address src, address dst, uint256 transferTokens) external override { _checkActionPauseState(vToken, Action.TRANSFER); // Currently the only consideration is whether or not // the src is allowed to redeem this many tokens _checkRedeemAllowed(vToken, src, transferTokens); // Keep the flywheel moving uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; rewardsDistributor.updateRewardTokenSupplyIndex(vToken); rewardsDistributor.distributeSupplierRewardToken(vToken, src); rewardsDistributor.distributeSupplierRewardToken(vToken, dst); } } /*** Pool-level operations ***/ /** * @notice Seizes all the remaining collateral, makes msg.sender repay the existing * borrows, and treats the rest of the debt as bad debt (for each market). * The sender has to repay a certain percentage of the debt, computed as * collateral / (borrows * liquidationIncentive). * @param user account to heal * @custom:error CollateralExceedsThreshold error is thrown when the collateral is too big for healing * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted */ function healAccount(address user) external { VToken[] memory userAssets = getAssetsIn(user); uint256 userAssetsCount = userAssets.length; address liquidator = msg.sender; { ResilientOracleInterface oracle_ = oracle; // We need all user's markets to be fresh for the computations to be correct for (uint256 i; i < userAssetsCount; ++i) { userAssets[i].accrueInterest(); oracle_.updatePrice(address(userAssets[i])); } } AccountLiquiditySnapshot memory snapshot = _getCurrentLiquiditySnapshot(user, _getLiquidationThreshold); if (snapshot.totalCollateral > minLiquidatableCollateral) { revert CollateralExceedsThreshold(minLiquidatableCollateral, snapshot.totalCollateral); } if (snapshot.shortfall == 0) { revert InsufficientShortfall(); } // percentage = collateral / (borrows * liquidation incentive) Exp memory collateral = Exp({ mantissa: snapshot.totalCollateral }); Exp memory scaledBorrows = mul_( Exp({ mantissa: snapshot.borrows }), Exp({ mantissa: liquidationIncentiveMantissa }) ); Exp memory percentage = div_(collateral, scaledBorrows); if (lessThanExp(Exp({ mantissa: MANTISSA_ONE }), percentage)) { revert CollateralExceedsThreshold(scaledBorrows.mantissa, collateral.mantissa); } for (uint256 i; i < userAssetsCount; ++i) { VToken market = userAssets[i]; (uint256 tokens, uint256 borrowBalance, ) = _safeGetAccountSnapshot(market, user); uint256 repaymentAmount = mul_ScalarTruncate(percentage, borrowBalance); // Seize the entire collateral if (tokens != 0) { market.seize(liquidator, user, tokens); } // Repay a certain percentage of the borrow, forgive the rest if (borrowBalance != 0) { market.healBorrow(liquidator, user, repaymentAmount); } } } /** * @notice Liquidates all borrows of the borrower. Callable only if the collateral is less than * a predefined threshold, and the account collateral can be seized to cover all borrows. If * the collateral is higher than the threshold, use regular liquidations. If the collateral is * below the threshold, and the account is insolvent, use healAccount. * @param borrower the borrower address * @param orders an array of liquidation orders * @custom:error CollateralExceedsThreshold error is thrown when the collateral is too big for a batch liquidation * @custom:error InsufficientCollateral error is thrown when there is not enough collateral to cover the debt * @custom:error SnapshotError is thrown if some vToken fails to return the account's supply and borrows * @custom:error PriceError is thrown if the oracle returns an incorrect price for some asset * @custom:access Not restricted */ function liquidateAccount(address borrower, LiquidationOrder[] calldata orders) external { // We will accrue interest and update the oracle prices later during the liquidation AccountLiquiditySnapshot memory snapshot = _getCurrentLiquiditySnapshot(borrower, _getLiquidationThreshold); if (snapshot.totalCollateral > minLiquidatableCollateral) { // You should use the regular vToken.liquidateBorrow(...) call revert CollateralExceedsThreshold(minLiquidatableCollateral, snapshot.totalCollateral); } uint256 collateralToSeize = mul_ScalarTruncate( Exp({ mantissa: liquidationIncentiveMantissa }), snapshot.borrows ); if (collateralToSeize >= snapshot.totalCollateral) { // There is not enough collateral to seize. Use healBorrow to repay some part of the borrow // and record bad debt. revert InsufficientCollateral(collateralToSeize, snapshot.totalCollateral); } if (snapshot.shortfall == 0) { revert InsufficientShortfall(); } uint256 ordersCount = orders.length; _ensureMaxLoops(ordersCount / 2); for (uint256 i; i < ordersCount; ++i) { if (!markets[address(orders[i].vTokenBorrowed)].isListed) { revert MarketNotListed(address(orders[i].vTokenBorrowed)); } if (!markets[address(orders[i].vTokenCollateral)].isListed) { revert MarketNotListed(address(orders[i].vTokenCollateral)); } LiquidationOrder calldata order = orders[i]; order.vTokenBorrowed.forceLiquidateBorrow( msg.sender, borrower, order.repayAmount, order.vTokenCollateral, true ); } VToken[] memory borrowMarkets = getAssetsIn(borrower); uint256 marketsCount = borrowMarkets.length; for (uint256 i; i < marketsCount; ++i) { (, uint256 borrowBalance, ) = _safeGetAccountSnapshot(borrowMarkets[i], borrower); require(borrowBalance == 0, "Nonzero borrow balance after liquidation"); } } /** * @notice Sets the closeFactor to use when liquidating borrows * @param newCloseFactorMantissa New close factor, scaled by 1e18 * @custom:event Emits NewCloseFactor on success * @custom:access Controlled by AccessControlManager */ function setCloseFactor(uint256 newCloseFactorMantissa) external { _checkAccessAllowed("setCloseFactor(uint256)"); require(MAX_CLOSE_FACTOR_MANTISSA >= newCloseFactorMantissa, "Close factor greater than maximum close factor"); require(MIN_CLOSE_FACTOR_MANTISSA <= newCloseFactorMantissa, "Close factor smaller than minimum close factor"); uint256 oldCloseFactorMantissa = closeFactorMantissa; closeFactorMantissa = newCloseFactorMantissa; emit NewCloseFactor(oldCloseFactorMantissa, newCloseFactorMantissa); } /** * @notice Sets the collateralFactor for a market * @dev This function is restricted by the AccessControlManager * @param vToken The market to set the factor on * @param newCollateralFactorMantissa The new collateral factor, scaled by 1e18 * @param newLiquidationThresholdMantissa The new liquidation threshold, scaled by 1e18 * @custom:event Emits NewCollateralFactor when collateral factor is updated * and NewLiquidationThreshold when liquidation threshold is updated * @custom:error MarketNotListed error is thrown when the market is not listed * @custom:error InvalidCollateralFactor error is thrown when collateral factor is too high * @custom:error InvalidLiquidationThreshold error is thrown when liquidation threshold is lower than collateral factor * @custom:error PriceError is thrown when the oracle returns an invalid price for the asset * @custom:access Controlled by AccessControlManager */ function setCollateralFactor( VToken vToken, uint256 newCollateralFactorMantissa, uint256 newLiquidationThresholdMantissa ) external { _checkAccessAllowed("setCollateralFactor(address,uint256,uint256)"); // Verify market is listed Market storage market = markets[address(vToken)]; if (!market.isListed) { revert MarketNotListed(address(vToken)); } // Check collateral factor <= 0.9 if (newCollateralFactorMantissa > MAX_COLLATERAL_FACTOR_MANTISSA) { revert InvalidCollateralFactor(); } // Ensure that liquidation threshold <= 1 if (newLiquidationThresholdMantissa > MANTISSA_ONE) { revert InvalidLiquidationThreshold(); } // Ensure that liquidation threshold >= CF if (newLiquidationThresholdMantissa < newCollateralFactorMantissa) { revert InvalidLiquidationThreshold(); } // If collateral factor != 0, fail if price == 0 if (newCollateralFactorMantissa != 0 && oracle.getUnderlyingPrice(address(vToken)) == 0) { revert PriceError(address(vToken)); } uint256 oldCollateralFactorMantissa = market.collateralFactorMantissa; if (newCollateralFactorMantissa != oldCollateralFactorMantissa) { market.collateralFactorMantissa = newCollateralFactorMantissa; emit NewCollateralFactor(vToken, oldCollateralFactorMantissa, newCollateralFactorMantissa); } uint256 oldLiquidationThresholdMantissa = market.liquidationThresholdMantissa; if (newLiquidationThresholdMantissa != oldLiquidationThresholdMantissa) { market.liquidationThresholdMantissa = newLiquidationThresholdMantissa; emit NewLiquidationThreshold(vToken, oldLiquidationThresholdMantissa, newLiquidationThresholdMantissa); } } /** * @notice Sets liquidationIncentive * @dev This function is restricted by the AccessControlManager * @param newLiquidationIncentiveMantissa New liquidationIncentive scaled by 1e18 * @custom:event Emits NewLiquidationIncentive on success * @custom:access Controlled by AccessControlManager */ function setLiquidationIncentive(uint256 newLiquidationIncentiveMantissa) external { require(newLiquidationIncentiveMantissa >= MANTISSA_ONE, "liquidation incentive should be greater than 1e18"); _checkAccessAllowed("setLiquidationIncentive(uint256)"); // Save current value for use in log uint256 oldLiquidationIncentiveMantissa = liquidationIncentiveMantissa; // Set liquidation incentive to new incentive liquidationIncentiveMantissa = newLiquidationIncentiveMantissa; // Emit event with old incentive, new incentive emit NewLiquidationIncentive(oldLiquidationIncentiveMantissa, newLiquidationIncentiveMantissa); } /** * @notice Add the market to the markets mapping and set it as listed * @dev Only callable by the PoolRegistry * @param vToken The address of the market (token) to list * @custom:error MarketAlreadyListed is thrown if the market is already listed in this pool * @custom:access Only PoolRegistry */ function supportMarket(VToken vToken) external { _checkSenderIs(poolRegistry); if (markets[address(vToken)].isListed) { revert MarketAlreadyListed(address(vToken)); } require(vToken.isVToken(), "Comptroller: Invalid vToken"); // Sanity check to make sure its really a VToken Market storage newMarket = markets[address(vToken)]; newMarket.isListed = true; newMarket.collateralFactorMantissa = 0; newMarket.liquidationThresholdMantissa = 0; _addMarket(address(vToken)); uint256 rewardDistributorsCount = rewardsDistributors.length; for (uint256 i; i < rewardDistributorsCount; ++i) { rewardsDistributors[i].initializeMarket(address(vToken)); } emit MarketSupported(vToken); } /** * @notice Set the given borrow caps for the given vToken markets. Borrowing that brings total borrows to or above borrow cap will revert. * @dev This function is restricted by the AccessControlManager * @dev A borrow cap of type(uint256).max corresponds to unlimited borrowing. * @dev Borrow caps smaller than the current total borrows are accepted. This way, new borrows will not be allowed until the total borrows amount goes below the new borrow cap * @param vTokens 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 type(uint256).max corresponds to unlimited borrowing. * @custom:access Controlled by AccessControlManager */ function setMarketBorrowCaps(VToken[] calldata vTokens, uint256[] calldata newBorrowCaps) external { _checkAccessAllowed("setMarketBorrowCaps(address[],uint256[])"); uint256 numMarkets = vTokens.length; uint256 numBorrowCaps = newBorrowCaps.length; require(numMarkets != 0 && numMarkets == numBorrowCaps, "invalid input"); _ensureMaxLoops(numMarkets); for (uint256 i; i < numMarkets; ++i) { borrowCaps[address(vTokens[i])] = newBorrowCaps[i]; emit NewBorrowCap(vTokens[i], newBorrowCaps[i]); } } /** * @notice Set the given supply caps for the given vToken markets. Supply that brings total Supply to or above supply cap will revert. * @dev This function is restricted by the AccessControlManager * @dev A supply cap of type(uint256).max corresponds to unlimited supply. * @dev Supply caps smaller than the current total supplies are accepted. This way, new supplies will not be allowed until the total supplies amount goes below the new supply cap * @param vTokens 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 type(uint256).max corresponds to unlimited supply. * @custom:access Controlled by AccessControlManager */ function setMarketSupplyCaps(VToken[] calldata vTokens, uint256[] calldata newSupplyCaps) external { _checkAccessAllowed("setMarketSupplyCaps(address[],uint256[])"); uint256 vTokensCount = vTokens.length; require(vTokensCount != 0, "invalid number of markets"); require(vTokensCount == newSupplyCaps.length, "invalid number of markets"); _ensureMaxLoops(vTokensCount); for (uint256 i; i < vTokensCount; ++i) { supplyCaps[address(vTokens[i])] = newSupplyCaps[i]; emit NewSupplyCap(vTokens[i], newSupplyCaps[i]); } } /** * @notice Pause/unpause specified actions * @dev This function is restricted by the AccessControlManager * @param marketsList Markets to pause/unpause the actions on * @param actionsList List of action ids to pause/unpause * @param paused The new paused state (true=paused, false=unpaused) * @custom:access Controlled by AccessControlManager */ function setActionsPaused(VToken[] calldata marketsList, Action[] calldata actionsList, bool paused) external { _checkAccessAllowed("setActionsPaused(address[],uint256[],bool)"); uint256 marketsCount = marketsList.length; uint256 actionsCount = actionsList.length; _ensureMaxLoops(marketsCount * actionsCount); for (uint256 marketIdx; marketIdx < marketsCount; ++marketIdx) { for (uint256 actionIdx; actionIdx < actionsCount; ++actionIdx) { _setActionPaused(address(marketsList[marketIdx]), actionsList[actionIdx], paused); } } } /** * @notice Set the given collateral threshold for non-batch liquidations. Regular liquidations * will fail if the collateral amount is less than this threshold. Liquidators should use batch * operations like liquidateAccount or healAccount. * @dev This function is restricted by the AccessControlManager * @param newMinLiquidatableCollateral The new min liquidatable collateral (in USD). * @custom:access Controlled by AccessControlManager */ function setMinLiquidatableCollateral(uint256 newMinLiquidatableCollateral) external { _checkAccessAllowed("setMinLiquidatableCollateral(uint256)"); uint256 oldMinLiquidatableCollateral = minLiquidatableCollateral; minLiquidatableCollateral = newMinLiquidatableCollateral; emit NewMinLiquidatableCollateral(oldMinLiquidatableCollateral, newMinLiquidatableCollateral); } /** * @notice Add a new RewardsDistributor and initialize it with all markets. We can add several RewardsDistributor * contracts with the same rewardToken, and there could be overlaping among them considering the last reward slot (block or second) * @dev Only callable by the admin * @param _rewardsDistributor Address of the RewardDistributor contract to add * @custom:access Only Governance * @custom:event Emits NewRewardsDistributor with distributor address */ function addRewardsDistributor(RewardsDistributor _rewardsDistributor) external onlyOwner { require(!rewardsDistributorExists[address(_rewardsDistributor)], "already exists"); uint256 rewardsDistributorsLen = rewardsDistributors.length; _ensureMaxLoops(rewardsDistributorsLen + 1); rewardsDistributors.push(_rewardsDistributor); rewardsDistributorExists[address(_rewardsDistributor)] = true; uint256 marketsCount = allMarkets.length; for (uint256 i; i < marketsCount; ++i) { _rewardsDistributor.initializeMarket(address(allMarkets[i])); } emit NewRewardsDistributor(address(_rewardsDistributor), address(_rewardsDistributor.rewardToken())); } /** * @notice Sets a new price oracle for the Comptroller * @dev Only callable by the admin * @param newOracle Address of the new price oracle to set * @custom:event Emits NewPriceOracle on success * @custom:error ZeroAddressNotAllowed is thrown when the new oracle address is zero */ function setPriceOracle(ResilientOracleInterface newOracle) external onlyOwner { ensureNonzeroAddress(address(newOracle)); ResilientOracleInterface oldOracle = oracle; oracle = newOracle; emit NewPriceOracle(oldOracle, newOracle); } /** * @notice Set the for loop iteration limit to avoid DOS * @param limit Limit for the max loops can execute at a time */ function setMaxLoopsLimit(uint256 limit) external onlyOwner { _setMaxLoopsLimit(limit); } /** * @notice Sets the prime token contract for the comptroller * @param _prime Address of the Prime contract */ function setPrimeToken(IPrime _prime) external onlyOwner { ensureNonzeroAddress(address(_prime)); emit NewPrimeToken(prime, _prime); prime = _prime; } /** * @notice Enables forced liquidations for a market. If forced liquidation is enabled, * borrows in the market may be liquidated regardless of the account liquidity * @param vTokenBorrowed Borrowed vToken * @param enable Whether to enable forced liquidations */ function setForcedLiquidation(address vTokenBorrowed, bool enable) external { _checkAccessAllowed("setForcedLiquidation(address,bool)"); ensureNonzeroAddress(vTokenBorrowed); if (!markets[vTokenBorrowed].isListed) { revert MarketNotListed(vTokenBorrowed); } isForcedLiquidationEnabled[vTokenBorrowed] = enable; emit IsForcedLiquidationEnabledUpdated(vTokenBorrowed, enable); } /** * @notice Determine the current account liquidity with respect to liquidation threshold requirements * @dev The interface of this function is intentionally kept compatible with Compound and Venus Core * @param account The account get liquidity for * @return error Always NO_ERROR for compatibility with Venus core tooling * @return liquidity Account liquidity in excess of liquidation threshold requirements, * @return shortfall Account shortfall below liquidation threshold requirements */ function getAccountLiquidity( address account ) external view returns (uint256 error, uint256 liquidity, uint256 shortfall) { AccountLiquiditySnapshot memory snapshot = _getCurrentLiquiditySnapshot(account, _getLiquidationThreshold); return (NO_ERROR, snapshot.liquidity, snapshot.shortfall); } /** * @notice Determine the current account liquidity with respect to collateral requirements * @dev The interface of this function is intentionally kept compatible with Compound and Venus Core * @param account The account get liquidity for * @return error Always NO_ERROR for compatibility with Venus core tooling * @return liquidity Account liquidity in excess of collateral requirements, * @return shortfall Account shortfall below collateral requirements */ function getBorrowingPower( address account ) external view returns (uint256 error, uint256 liquidity, uint256 shortfall) { AccountLiquiditySnapshot memory snapshot = _getCurrentLiquiditySnapshot(account, _getCollateralFactor); return (NO_ERROR, snapshot.liquidity, snapshot.shortfall); } /** * @notice Determine what the account liquidity would be if the given amounts were redeemed/borrowed * @dev The interface of this function is intentionally kept compatible with Compound and Venus Core * @param vTokenModify 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 error Always NO_ERROR for compatibility with Venus core tooling * @return liquidity Hypothetical account liquidity in excess of collateral requirements, * @return shortfall Hypothetical account shortfall below collateral requirements */ function getHypotheticalAccountLiquidity( address account, address vTokenModify, uint256 redeemTokens, uint256 borrowAmount ) external view returns (uint256 error, uint256 liquidity, uint256 shortfall) { AccountLiquiditySnapshot memory snapshot = _getHypotheticalLiquiditySnapshot( account, VToken(vTokenModify), redeemTokens, borrowAmount, _getCollateralFactor ); return (NO_ERROR, snapshot.liquidity, snapshot.shortfall); } /** * @notice Return all of the markets * @dev The automatic getter may be used to access an individual market. * @return markets The list of market addresses */ function getAllMarkets() external view override returns (VToken[] memory) { return allMarkets; } /** * @notice Check if a market is marked as listed (active) * @param vToken vToken Address for the market to check * @return listed True if listed otherwise false */ function isMarketListed(VToken vToken) external view returns (bool) { return markets[address(vToken)].isListed; } /*** Assets You Are In ***/ /** * @notice Returns whether the given account is entered in a given market * @param account The address of the account to check * @param vToken The vToken to check * @return True if the account is in the market specified, otherwise false. */ function checkMembership(address account, VToken vToken) external view returns (bool) { return markets[address(vToken)].accountMembership[account]; } /** * @notice Calculate number of tokens of collateral asset to seize given an underlying amount * @dev Used in liquidation (called in vToken.liquidateBorrowFresh) * @param vTokenBorrowed The address of the borrowed vToken * @param vTokenCollateral The address of the collateral vToken * @param actualRepayAmount The amount of vTokenBorrowed underlying to convert into vTokenCollateral tokens * @return error Always NO_ERROR for compatibility with Venus core tooling * @return tokensToSeize Number of vTokenCollateral tokens to be seized in a liquidation * @custom:error PriceError if the oracle returns an invalid price */ function liquidateCalculateSeizeTokens( address vTokenBorrowed, address vTokenCollateral, uint256 actualRepayAmount ) external view override returns (uint256 error, uint256 tokensToSeize) { /* Read oracle prices for borrowed and collateral markets */ uint256 priceBorrowedMantissa = _safeGetUnderlyingPrice(VToken(vTokenBorrowed)); uint256 priceCollateralMantissa = _safeGetUnderlyingPrice(VToken(vTokenCollateral)); /* * 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) */ uint256 exchangeRateMantissa = VToken(vTokenCollateral).exchangeRateStored(); // Note: reverts on error // All factors are multiplied before the single division, so the result is truncated only once. // Dividing first would round the ratio to 18 decimals, which drops part or all of the seize amount // when the borrowed asset has a small price mantissa, e.g. a 24-decimal token priced at 1e12. uint256 numerator = mul_(mul_(actualRepayAmount, liquidationIncentiveMantissa), priceBorrowedMantissa); uint256 denominator = mul_(priceCollateralMantissa, exchangeRateMantissa); uint256 seizeTokens = div_(numerator, denominator); return (NO_ERROR, seizeTokens); } /** * @notice Returns reward speed given a vToken * @param vToken The vToken to get the reward speeds for * @return rewardSpeeds Array of total supply and borrow speeds and reward token for all reward distributors */ function getRewardsByMarket(address vToken) external view returns (RewardSpeeds[] memory rewardSpeeds) { uint256 rewardsDistributorsLength = rewardsDistributors.length; rewardSpeeds = new RewardSpeeds[](rewardsDistributorsLength); for (uint256 i; i < rewardsDistributorsLength; ++i) { RewardsDistributor rewardsDistributor = rewardsDistributors[i]; address rewardToken = address(rewardsDistributor.rewardToken()); rewardSpeeds[i] = RewardSpeeds({ rewardToken: rewardToken, supplySpeed: rewardsDistributor.rewardTokenSupplySpeeds(vToken), borrowSpeed: rewardsDistributor.rewardTokenBorrowSpeeds(vToken) }); } return rewardSpeeds; } /** * @notice Return all reward distributors for this pool * @return Array of RewardDistributor addresses */ function getRewardDistributors() external view returns (RewardsDistributor[] memory) { return rewardsDistributors; } /** * @notice A marker method that returns true for a valid Comptroller contract * @return Always true */ function isComptroller() external pure override returns (bool) { return true; } /** * @notice Update the prices of all the tokens associated with the provided account * @param account Address of the account to get associated tokens with */ function updatePrices(address account) public { VToken[] memory vTokens = getAssetsIn(account); uint256 vTokensCount = vTokens.length; ResilientOracleInterface oracle_ = oracle; for (uint256 i; i < vTokensCount; ++i) { oracle_.updatePrice(address(vTokens[i])); } } /** * @notice Checks if a certain action is paused on a market * @param market vToken address * @param action Action to check * @return paused True if the action is paused otherwise false */ function actionPaused(address market, Action action) public view returns (bool) { return _actionPaused[market][action]; } /** * @notice Returns the assets an account has entered * @param account The address of the account to pull assets for * @return A list with the assets the account has entered */ function getAssetsIn(address account) public view returns (VToken[] memory) { uint256 len; VToken[] memory _accountAssets = accountAssets[account]; uint256 _accountAssetsLength = _accountAssets.length; VToken[] memory assetsIn = new VToken[](_accountAssetsLength); for (uint256 i; i < _accountAssetsLength; ++i) { Market storage market = markets[address(_accountAssets[i])]; if (market.isListed) { assetsIn[len] = _accountAssets[i]; ++len; } } assembly { mstore(assetsIn, len) } return assetsIn; } /** * @notice Add the market to the borrower's "assets in" for liquidity calculations * @param vToken The market to enter * @param borrower The address of the account to modify */ function _addToMarket(VToken vToken, address borrower) internal { _checkActionPauseState(address(vToken), Action.ENTER_MARKET); Market storage marketToJoin = markets[address(vToken)]; if (!marketToJoin.isListed) { revert MarketNotListed(address(vToken)); } if (marketToJoin.accountMembership[borrower]) { // already joined return; } // 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(vToken); emit MarketEntered(vToken, borrower); } /** * @notice Internal function to validate that a market hasn't already been added * and if it hasn't adds it * @param vToken The market to support */ function _addMarket(address vToken) internal { uint256 marketsCount = allMarkets.length; for (uint256 i; i < marketsCount; ++i) { if (allMarkets[i] == VToken(vToken)) { revert MarketAlreadyListed(vToken); } } allMarkets.push(VToken(vToken)); marketsCount = allMarkets.length; _ensureMaxLoops(marketsCount); } /** * @dev Pause/unpause an action on a market * @param market Market to pause/unpause the action on * @param action Action id to pause/unpause * @param paused The new paused state (true=paused, false=unpaused) */ function _setActionPaused(address market, Action action, bool paused) internal { require(markets[market].isListed, "cannot pause a market that is not listed"); _actionPaused[market][action] = paused; emit ActionPausedMarket(VToken(market), action, paused); } /** * @dev Internal function to check that vTokens can be safely redeemed for the underlying asset. * @param vToken Address of the vTokens to redeem * @param redeemer Account redeeming the tokens * @param redeemTokens The number of tokens to redeem */ function _checkRedeemAllowed(address vToken, address redeemer, uint256 redeemTokens) internal { Market storage market = markets[vToken]; if (!market.isListed) { revert MarketNotListed(address(vToken)); } /* If the redeemer is not 'in' the market, then we can bypass the liquidity check */ if (!market.accountMembership[redeemer]) { return; } // Update the prices of tokens updatePrices(redeemer); /* Otherwise, perform a hypothetical liquidity check to guard against shortfall */ AccountLiquiditySnapshot memory snapshot = _getHypotheticalLiquiditySnapshot( redeemer, VToken(vToken), redeemTokens, 0, _getCollateralFactor ); if (snapshot.shortfall > 0) { revert InsufficientLiquidity(); } } /** * @notice Get the total collateral, weighted collateral, borrow balance, liquidity, shortfall * @param account The account to get the snapshot for * @param weight The function to compute the weight of the collateral – either collateral factor or * liquidation threshold. Accepts the address of the vToken and returns the weight as Exp. * @dev Note that we calculate the exchangeRateStored for each collateral vToken using stored data, * without calculating accumulated interest. * @return snapshot Account liquidity snapshot */ function _getCurrentLiquiditySnapshot( address account, function(VToken) internal view returns (Exp memory) weight ) internal view returns (AccountLiquiditySnapshot memory snapshot) { return _getHypotheticalLiquiditySnapshot(account, VToken(address(0)), 0, 0, weight); } /** * @notice Determine what the supply/borrow balances would be if the given amounts were redeemed/borrowed * @param vTokenModify 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 * @param weight The function to compute the weight of the collateral – either collateral factor or liquidation threshold. Accepts the address of the VToken and returns the weight * @dev Note that we calculate the exchangeRateStored for each collateral vToken using stored data, * without calculating accumulated interest. * @return snapshot Account liquidity snapshot */ function _getHypotheticalLiquiditySnapshot( address account, VToken vTokenModify, uint256 redeemTokens, uint256 borrowAmount, function(VToken) internal view returns (Exp memory) weight ) internal view returns (AccountLiquiditySnapshot memory snapshot) { // For each asset the account is in VToken[] memory assets = getAssetsIn(account); uint256 assetsCount = assets.length; for (uint256 i; i < assetsCount; ++i) { VToken asset = assets[i]; // Read the balances and exchange rate from the vToken (uint256 vTokenBalance, uint256 borrowBalance, uint256 exchangeRateMantissa) = _safeGetAccountSnapshot( asset, account ); // Get the normalized price of the asset Exp memory oraclePrice = Exp({ mantissa: _safeGetUnderlyingPrice(asset) }); // Pre-compute conversion factors from vTokens -> usd Exp memory vTokenPrice = mul_(Exp({ mantissa: exchangeRateMantissa }), oraclePrice); Exp memory weightedVTokenPrice = mul_(weight(asset), vTokenPrice); // weightedCollateral += weightedVTokenPrice * vTokenBalance snapshot.weightedCollateral = mul_ScalarTruncateAddUInt( weightedVTokenPrice, vTokenBalance, snapshot.weightedCollateral ); // totalCollateral += vTokenPrice * vTokenBalance snapshot.totalCollateral = mul_ScalarTruncateAddUInt(vTokenPrice, vTokenBalance, snapshot.totalCollateral); // borrows += oraclePrice * borrowBalance snapshot.borrows = mul_ScalarTruncateAddUInt(oraclePrice, borrowBalance, snapshot.borrows); // Calculate effects of interacting with vTokenModify if (asset == vTokenModify) { // redeem effect // effects += tokensToDenom * redeemTokens snapshot.effects = mul_ScalarTruncateAddUInt(weightedVTokenPrice, redeemTokens, snapshot.effects); // borrow effect // effects += oraclePrice * borrowAmount snapshot.effects = mul_ScalarTruncateAddUInt(oraclePrice, borrowAmount, snapshot.effects); } } uint256 borrowPlusEffects = snapshot.borrows + snapshot.effects; // These are safe, as the underflow condition is checked first unchecked { if (snapshot.weightedCollateral > borrowPlusEffects) { snapshot.liquidity = snapshot.weightedCollateral - borrowPlusEffects; snapshot.shortfall = 0; } else { snapshot.liquidity = 0; snapshot.shortfall = borrowPlusEffects - snapshot.weightedCollateral; } } return snapshot; } /** * @dev Retrieves price from oracle for an asset and checks it is nonzero * @param asset Address for asset to query price * @return Underlying price */ function _safeGetUnderlyingPrice(VToken asset) internal view returns (uint256) { uint256 oraclePriceMantissa = oracle.getUnderlyingPrice(address(asset)); if (oraclePriceMantissa == 0) { revert PriceError(address(asset)); } return oraclePriceMantissa; } /** * @dev Return collateral factor for a market * @param asset Address for asset * @return Collateral factor as exponential */ function _getCollateralFactor(VToken asset) internal view returns (Exp memory) { return Exp({ mantissa: markets[address(asset)].collateralFactorMantissa }); } /** * @dev Retrieves liquidation threshold for a market as an exponential * @param asset Address for asset to liquidation threshold * @return Liquidation threshold as exponential */ function _getLiquidationThreshold(VToken asset) internal view returns (Exp memory) { return Exp({ mantissa: markets[address(asset)].liquidationThresholdMantissa }); } /** * @dev Returns supply and borrow balances of user in vToken, reverts on failure * @param vToken Market to query * @param user Account address * @return vTokenBalance Balance of vTokens, the same as vToken.balanceOf(user) * @return borrowBalance Borrowed amount, including the interest * @return exchangeRateMantissa Stored exchange rate */ function _safeGetAccountSnapshot( VToken vToken, address user ) internal view returns (uint256 vTokenBalance, uint256 borrowBalance, uint256 exchangeRateMantissa) { uint256 err; (err, vTokenBalance, borrowBalance, exchangeRateMantissa) = vToken.getAccountSnapshot(user); if (err != 0) { revert SnapshotError(address(vToken), user); } return (vTokenBalance, borrowBalance, exchangeRateMantissa); } /// @notice Reverts if the call is not from expectedSender /// @param expectedSender Expected transaction sender function _checkSenderIs(address expectedSender) internal view { if (msg.sender != expectedSender) { revert UnexpectedSender(expectedSender, msg.sender); } } /// @notice Reverts if a certain action is paused on a market /// @param market Market to check /// @param action Action to check function _checkActionPauseState(address market, Action action) private view { if (actionPaused(market, action)) { revert ActionPaused(market, action); } } }