aave-protocol-v2/contracts/libraries/UserLogic.sol

82 lines
2.4 KiB
Solidity
Raw Normal View History

2020-06-20 23:40:03 +00:00
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
2020-06-30 12:09:28 +00:00
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
2020-06-20 23:40:03 +00:00
2020-06-30 12:09:28 +00:00
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
2020-07-13 08:54:08 +00:00
import '../tokenization/base/DebtTokenBase.sol';
import './ReserveLogic.sol';
2020-07-23 15:18:06 +00:00
import './ReserveConfiguration.sol';
2020-06-20 23:40:03 +00:00
2020-06-27 02:13:32 +00:00
/**
2020-06-30 12:09:28 +00:00
* @title UserLogic library
* @author Aave
* @notice Implements user specific logic.
*/
2020-06-20 23:40:03 +00:00
library UserLogic {
2020-06-30 12:09:28 +00:00
using SafeMath for uint256;
2020-07-23 15:18:06 +00:00
using ReserveConfiguration for ReserveConfiguration.Map;
2020-06-30 12:09:28 +00:00
2020-07-08 22:16:05 +00:00
struct UserReserveData {
//defines if a specific deposit should or not be used as a collateral in borrows
bool useAsCollateral;
}
2020-06-30 12:09:28 +00:00
/**
* @dev checks if a user is allowed to borrow at a stable rate
* @param _reserve the reserve address
* @param _user the user
* @param _amount the amount the the user wants to borrow
* @return true if the user is allowed to borrow at a stable rate, false otherwise
**/
function isAllowedToBorrowAtStable(
2020-07-08 22:16:05 +00:00
UserReserveData storage _user,
ReserveLogic.ReserveData storage _reserve,
2020-06-30 12:09:28 +00:00
address _userAddress,
uint256 _amount
) external view returns (bool) {
if (!_reserve.isStableBorrowRateEnabled) return false;
return
!_user.useAsCollateral ||
2020-07-23 15:18:06 +00:00
_reserve.configuration.getLtv() == 0 ||
2020-06-30 12:09:28 +00:00
_amount > IERC20(_reserve.aTokenAddress).balanceOf(_userAddress);
}
2020-07-09 14:55:09 +00:00
/**
* @dev fetches the user current stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
2020-07-13 08:54:08 +00:00
function getUserCurrentDebt(address _user, ReserveLogic.ReserveData storage _reserve)
2020-06-30 12:09:28 +00:00
internal
view
returns (uint256, uint256)
{
return (
IERC20(_reserve.stableDebtTokenAddress).balanceOf(_user),
IERC20(_reserve.variableDebtTokenAddress).balanceOf(_user)
);
}
2020-07-09 14:55:09 +00:00
/**
* @dev fetches the user principal stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
2020-07-13 08:54:08 +00:00
function getUserPrincipalDebt(address _user, ReserveLogic.ReserveData storage _reserve)
2020-06-30 12:09:28 +00:00
internal
view
returns (uint256, uint256)
{
return (
DebtTokenBase(_reserve.stableDebtTokenAddress).principalBalanceOf(_user),
DebtTokenBase(_reserve.variableDebtTokenAddress).principalBalanceOf(_user)
);
}
2020-06-20 23:40:03 +00:00
}