Introduction
The Unitroller is the risk management layer of the Venus protocol; it determines how much collateral a user is required to maintain, and whether (and by now much) a user can be liquidated. Each time a user interacts with vToken, the Unitroller is asked to approve or deny the transaction. The Unitroller maps user balances to price (via the Price Oracle) to risk weights (Called Collateral Factors) to make its determinations. Users explicitly list which assets they would like included in their risk scoring, by calling Enter Markets and Exit Market.
Architecture
The Unitroller is implemented as an upgradeable proxy. The Unitroller proxies all logic to the Unitroller implementation, but storage values are set on the Unitroller. To call Unitroller functions, use the Unitroller ABI on the Unitroller address.
Enter Markets
Enter into a list of markets - it is not an error to enter the same market more than once. In order to supply collateral or borrow in a market, it must be entered first.
Unitroller
1 function enterMarkets(address[] calldata vTokens) returns (uint[] memory)
-
msg.sender: The account which shall enter the given markets.
-
vTokens: The addresses of the vToken markets to enter.
-
RETURN: For each market, returns an error code indicating whether or not it was entered. Each is 0 on success, otherwise an Error code
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 vToken[] memory vTokens = new vToken[](2);
3 vTokens[0] = CErc20(0x3FDA...);
4 vTokens[1] = CEther(0x3FDB...);
5 uint[] memory errors = troll.enterMarkets(vTokens);
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const vTokens = [CErc20.at(0x3FDA...), CEther.at(0x3FDB...)];
3 const errors = await troll.methods.enterMarkets(vTokens).
send({from: ...});
Exit Market
Exit a market - it is not an error to exit a market which is not currently entered. Exited markets will not count towards account liquidity calculations.
Unitroller
1 function exitMarket(address vToken) returns (uint)
-
msg.sender: The account which shall exit the given market.
-
vTokens: The addresses of the vToken market to exit.
-
RETURN: 0 on success, otherwise an Error code
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 vToken[] memory vTokens = new vToken[](2);
3 vTokens[0] = CErc20(0x3FDA...);
4 vTokens[1] = CEther(0x3FDB...);
5 uint[] memory errors = troll.enterMarkets(vTokens);
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const vTokens = [CErc20.at(0x3FDA...), CEther.at(0x3FDB...)];
3 const errors = await troll.methods.enterMarkets(vTokens).
send({from: ...});
Get Assets In
Get the list of markets an account is currently entered into. In order to supply collateral or borrow in a market, it must be entered first. Entered markets count towards account liquidity calculations.
Unitroller
1 function getAssetsIn(address account) returns (address[] memory)
-
account: The account whose list of entered markets shall be queried.
-
RETURN: The address of each market which is currently entered into.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 address[] memory markets = troll.getAssetsIn(0xMyAccount);
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const markets = await troll.methods.getAssetsIn(vTokens).call();
Collateral Factor
A vToken's collateral factor can range from 0-90%, and represents the proportionate increase in liquidity (borrow limit) that an account receives by minting the vToken.
Generally, large or liquid assets have high collateral factors, while small or illiquid assets have low collateral factors. If an asset has a 0% collateral factor, it can't be used as collateral (or seized in liquidation), though it can still be borrowed.
Collateral factors can be increased (or decreased) through Venus Governance, as market conditions change.
Unitroller
1 function markets(address vTokenAddress) view returns (bool,uint,bool)
-
vTokenAddress: The address of the vToken to check if listed and get the collateral factor for.
-
RETURN: Tuple of values (isListed, collateralFactorMantissa, isXvsed); isListed represents whether the unitroller recognizes this vToken; collateralFactorMantissa, scaled by 1e18, is multiplied by a supply balance to determine how much value can be borrowed. The isXvsed boolean indicates whether or not suppliers and borrowers are distributed XVS tokens.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 (bool isListed, uint collateralFactorMantissa, bool isXvsed) =
troll.markets(0x3FDA...);
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const result = await troll.methods.markets(0x3FDA...).call();
3 const {0: isListed, 1: collateralFactorMantissa, 2: isXvsed} = result;
Get Account Liquidity
Account Liquidity represents the USD value borrowable by a user, before it reaches liquidation. Users with a shortfall (negative liquidity) are subject to liquidation, and can’t withdraw or borrow assets until Account Liquidity is positive again.
For each market the user has entered into, their supplied balance is multiplied by the market’s collateral factor, and summed; borrow balances are then subtracted, to equal Account Liquidity. Borrowing an asset reduces Account Liquidity for each USD borrowed; withdrawing an asset reduces Account Liquidity by the asset’s collateral factor times each USD withdrawn.
Because the Venus Protocol exclusively uses unsigned integers, Account Liquidity returns either a surplus or shortfall.
Unitroller
1 function getAccountLiquidity(address account) view returns (
uint,uint,uint)
-
account: The account whose liquidity shall be calculated.
-
RETURN: Tuple of values (error, liquidity, shortfall). The error shall be 0 on success, otherwise an Error code. A non-zero liquidity value indicates the account has available account liquidity. A non-zero shortfall value indicates the account is currently below his/her collateral requirement and is subject to liquidation. At most one of liquidity or shortfall shall be non-zero.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 (uint error, uint liquidity, uint shortfall) = troll.getAccountLiquidity(msg.caller);
3 require(error == 0, "join the Discord");
4 require(shortfall == 0, "account underwater");
5 require(liquidity > 0, "account has excess collateral");
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const result = await troll.methods.getAccountLiquidity(0xBorrower).call();
3 const {0: error, 1: liquidity, 2: shortfall} = result;
Close Factor
The percent, ranging from 0% to 100%, of a liquidatable account's borrow that can be repaid in a single liquidate transaction. If a user has multiple borrowed assets, the closeFactor applies to any single borrowed asset, not the aggregated value of a user’s outstanding borrowing.
Unitroller
1 function closeFactorMantissa() view returns (uint)
-
RETURN: The closeFactor, scaled by 1e18, is multiplied by an outstanding borrow balance to determine how much could be closed.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 uint closeFactor = troll.closeFactorMantissa();
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const closeFactor= await troll.methods.closeFactoreMantissa().call();
Liquidation Incentive
The additional collateral given to liquidators as an incentive to perform liquidation of underwater accounts. For example, if the liquidation incentive is 1.1, liquidators receive an extra 10% of the borrowers collateral for every unit they close.
Unitroller
1 function liquidationIncentiveMantissa() view returns (uint)
-
RETURN: The liquidationIncentive, scaled by 1e18, is multiplied by the closed borrow amount from the liquidator to determine how much collateral can be seized.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 uint closeFactor = troll.liquidationIncentiveMantissa();
Web3 1.0
1 const troll = Unitroller.at(0xABCD...);
2 const closeFactor= await troll.methods.liquidationIncentiveMantissa().
call();
Error Codes
Code
Name
Description
0
NO_ERROR
Not a failure.
1
UNAUTHORIZED
The sender is not authorized to perform this action.
2
XVSTROLLER_MISMATCH
Liquidation cannot be performed in markets with different unitrollers.
3
INSUFFICIENT_SHORTFALL
The account does not have sufficient shortfall to perform this action.
4
INSUFFICIENT_LIQUIDITY
The account does not have sufficient liquidity to perform this action.
5
INVALID_CLOSE_FACTOR
The close factor is not valid.
6
INVALID_COLLATERAL_FACTOR
The collateral factor is not valid.
7
INVALID_LIQUIDATION_INCENTIVE
The liquidation incentive is invalid.
8
MARKET_NOT_ENTERED
The market has not been entered by the account.
9
MARKET_NOT_LISTED
The market is not currently listed by the unitroller.
10
MARKET_ALREADY_LISTED
An admin tried to list the same market more than once.
11
MATH_ERROR
A math calculation error occurred.
12
NONZERO_BORROW_BALANCE
The action cannot be performed since the account carries a borrow balance.
13
PRICE_ERROR
The unitroller could not obtain a required price of an asset.
14
REJECTION
The unitroller rejects the action requested by the market.
15
SNAPSHOT_ERROR
The unitroller could not get the account borrows and exchange rate from the market.
16
TOO_MANY_ASSETS
Attempted to enter more markets than are currently supported.
17
TOO_MUCH_REPAY
Attempted to repay more than is allowed by the protocol.
Failure Info
Code
Name
0
ACCEPT_ADMIN_PENDING_ADMIN_CHECK
1
ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK
2
EXIT_MARKET_BALANCE_OWED
3
EXIT_MARKET_REJECTION
4
SET_CLOSE_FACTOR_OWNER_CHECK
5
SET_CLOSE_FACTOR_VALIDATION
6
SET_COLLATERAL_FACTOR_OWNER_CHECK
7
SET_COLLATERAL_FACTOR_NO_EXISTS
8
SET_COLLATERAL_FACTOR_VALIDATION
9
SET_COLLATERAL_FACTOR_WITHOUT_PRICE
10
SET_IMPLEMENTATION_OWNER_CHECK
11
SET_LIQUIDATION_INCENTIVE_OWNER_CHECK
12
SET_LIQUIDATION_INCENTIVE_VALIDATION
13
SET_MAX_ASSETS_OWNER_CHECK
14
SET_PENDING_ADMIN_OWNER_CHECK
15
SET_PENDING_IMPLEMENTATION_OWNER_CHECK
16
SET_PRICE_ORACLE_OWNER_CHECK
17
SUPPORT_MARKET_EXISTS
18
SUPPORT_MARKET_OWNER_CHECK
MWX Distribution Speeds
MWX Speed
The “Venus speed” unique to each market is an unsigned integer that specifies the amount of XVS that is distributed, per block, to suppliers and borrowers in each market. As market conditions change, Venus speeds are updated to ensure XVS is distributed proportional to the utility of each market. Any user can call the Unitroller’s refreshVenusSpeeds method at any time in order to update market Venus speeds.
The following is the formula for calculating the rate that XVS is distributed to each supported market.
1 utility = vTokenTotalBorrows * assetPrice
2 utilityFraction = utility / sumOfAllVenusedMarketUtilities
3 marketVenusSpeed = venusRate * utilityFraction
Unitroller
1 function refreshVenusSpeeds(address account) public
-
RETURN: None
-
events: VenusSpeedUpdated - An event is emitted for each vToken with the address of the vToken and the new XVS distribution speed per block.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 troll.refreshVenusSpeeds();
Web3 1.0
1 const unitroller = new web3.eth.Contract(unitrollerAbi, unitrollerAddress);
2 await unitroller.methods.refreshVenusSpeeds().send({ from: sender });
XVS Distributed Per Block (All Markets)
The Unitroller Storage contract’s venusRate is an unsigned integer that indicates the rate at which the protocol distributes XVS to markets’ suppliers or borrowers, every BSC block. The value is the amount of XVS (in wei), per block, allocated for the markets. Note that not every market has XVS distributed to its participants (see Market Metadata).
The venusRate indicates how much XVS goes to the suppliers or borrowers, so doubling this number shows how much XVS goes to all suppliers and borrowers combined. The code examples implement reading the amount of XVS distributed, per BSC block, to all markets.
Unitroller
1 uint public venusRate;
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2
3 // XVS issued per block to suppliers OR borrowers * (1 * 10 ^ 18)
4 uint venusRate = troll.venusRate();
5
6 // Approximate XVS issued per day to suppliers OR borrowers *
(1 * 10 ^ 18)
7 uint venusRatePerDay = venusRate * 4 * 60 * 24;
8
9 // Approximate XVS issued per day to suppliers AND borrowers *
(1 * 10 ^ 18)
10 uint venusRatePerDayTotal = venusRatePerDay * 2;
Web3 1.0
1 const unitroller = new web3.eth.Contract(unitrollerAbi, unitrollerAddress);
2
3 let venusRate = await unitroller.methods.venusRate().call();
4 venusRate = venusRate / 1e18;
5
6 // XVS issued to suppliers OR borrowers
7 const venusRatePerDay = venusRate * 4 * 60 * 24;
8
9 // XVS issued to suppliers AND borrowers
10 const venusRatePerDayTotal = venusRatePerDay * 2;
XVS Distributed Per Block (Single Market)
The Unitroller Storage contract has a mapping called venusSpeeds. It maps vToken addresses to an integer of each market’s XVS distribution per BSC block. The integer indicates the rate at which the protocol distributes XVS to markets’ suppliers or borrowers. The value is the amount of XVS (in wei), per block, allocated for the market. Note that not every market has XVS distributed to its participants (see Market Metadata).
The speed indicates how much XVS goes to the suppliers or the borrowers, so doubling this number shows how much XVS goes to market suppliers and borrowers combined. The code examples implement reading the amount of XVS distributed, per BSC block, to a single market.
Unitroller
1 mapping(address => uint) public venusSpeeds;
Solidity
1 Unitroller troll = Unitroller(0x123...);
2 address vToken = 0xabc...;
3
4 // XVS issued per block to suppliers OR borrowers * (1 * 10 ^ 18)
5 uint venusSpeed = troll.venusSpeeds(vToken);
6
7 // Approximate XVS issued per day to suppliers OR borrowers *
(1 * 10 ^ 18)
8 uint venusSpeedPerDay = venusSpeed * 4 * 60 * 24;
9
10 // Approximate XVS issued per day to suppliers AND borrowers *
(1 * 10 ^ 18)
11 uint venusSpeedPerDayTotal = venusSpeedPerDay * 2;
Web3 1.0
1 const vTokenAddress = '0xabc...';
2
3 const unitroller = new web3.eth.Contract(unitrollerAbi, unitrollerAddress);
4
5 let venusSpeed = await unitroller.methods.venusSpeeds(vTokenAddress).call();
6 venusSpeed = venusSpeed / 1e18;
7
8 // XVS issued to suppliers OR borrowers
9 const venusSpeedPerDay = venusSpeed * 4 * 60 * 24;
10
11 // XVS issued to suppliers AND borrowers
12 const venusSpeedPerDayTotal = venusSpeedPerDay * 2;
Claim Moonwxlk
Every Venus user accrues XVS for each block they are supplying to or borrowing from the protocol. The protocol automatically transfers accrued XVS to a user’s address when the total amount of XVS accrued that address (in a market) is greater than the claimVenusThreshold, and and the address executes any of the mint, borrow, transfer, liquidateBorrow, repayBorrow, or redeem functions on that market. Separately, users may call the claimVenus method on any vToken contract at any time for finer grained control over which markets to claim from.
Unitroller
1 // Claim all the XVS accrued by holder in all markets
2 function claimVenus(address holder) public
3
4 // Claim all the XVS accrued by holder in specific markets
5 function claimVenus(address holder, vToken[] memory vTokens) public
6
7 // Claim all the XVS accrued by specific holders in specific markets
for their supplies and/or borrows
8 function claimVenus(address[] memory holders, vToken[] memory vTokens,
bool borrowers, bool suppliers) public
-
RETURN: The liquidationIncentive, scaled by 1e18, is multiplied by the closed borrow amount from the liquidator to determine how much collateral can be seized.
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 troll.claimVenus(0x1234...);
Web3 1.0
1 const unitroller = new web3.eth.Contract(unitrollerAbi,
unitrollerAddress);
2 await unitroller.methods.claimVenus("0x1234...").send({ from: sender });
Market Metadata
The Unitroller contract has an array called allMarkets that contains the addresses of each vToken contract. Each address in the allMarkets array can be used to fetch a metadata struct in the Unitroller’s markets constant. See the Unitroller Storage contract for the Market struct definition.
Unitroller
1 vToken[] public allMarkets;
Solidity
1 Unitroller troll = Unitroller(0xABCD...);
2 vToken vTokens[] = troll.allMarkets();
Web3 1.0
1 const unitroller = new web3.eth.Contract(unitrollerAbi,
unitrollerAddress);
2 const vTokens = await unitroller.methods.allMarkets().call();
3 const vToken = vTokens[0]; // address of a vToken
Key Events
Function
Description
MarketEntered(vToken vToken, address account)
Emitted upon a successful Enter Market.
MarketExited(vToken vToken, address account)
Emitted upon a successful Exit Market.