-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLiquityBase.sol
89 lines (65 loc) · 2.92 KB
/
LiquityBase.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
import "./BaseMath.sol";
import "./LiquityMath.sol";
import "../Interfaces/IActivePool.sol";
import "../Interfaces/IDefaultPool.sol";
import "../Interfaces/IPriceFeed.sol";
import "../Interfaces/ILiquityBase.sol";
import "../Interfaces/ILiquityBaseParams.sol";
/**
* Base contract for TroveManager, BorrowerOperations and StabilityPool. Contains global system constants and
* common functions.
*/
contract LiquityBase is BaseMath, ILiquityBase {
using SafeMath for uint256;
uint256 public constant _100pct = 1000000000000000000; // 1e18 == 100%
/// Amount of ZUSD to be locked in gas pool on opening troves
uint256 public constant ZUSD_GAS_COMPENSATION = 20e18;
/// Minimum amount of net ZUSD debt a trove must have
uint256 public constant MIN_NET_DEBT = 180e18;
IActivePool public activePool;
IDefaultPool public defaultPool;
IPriceFeed public override priceFeed;
ILiquityBaseParams public override liquityBaseParams;
// --- Gas compensation functions ---
// Returns the composite debt (drawn debt + gas compensation) of a trove, for the purpose of ICR calculation
function _getCompositeDebt(uint256 _debt) internal pure returns (uint256) {
return _debt.add(ZUSD_GAS_COMPENSATION);
}
function _getNetDebt(uint256 _debt) internal pure returns (uint256) {
return _debt.sub(ZUSD_GAS_COMPENSATION);
}
/// Return the amount of ETH to be drawn from a trove's collateral and sent as gas compensation.
function _getCollGasCompensation(uint256 _entireColl) internal view returns (uint256) {
return _entireColl / liquityBaseParams.PERCENT_DIVISOR();
}
function getEntireSystemColl() public view returns (uint256 entireSystemColl) {
uint256 activeColl = activePool.getETH();
uint256 liquidatedColl = defaultPool.getETH();
return activeColl.add(liquidatedColl);
}
function getEntireSystemDebt() public view returns (uint256 entireSystemDebt) {
uint256 activeDebt = activePool.getZUSDDebt();
uint256 closedDebt = defaultPool.getZUSDDebt();
return activeDebt.add(closedDebt);
}
function _getTCR(uint256 _price) internal view returns (uint256 TCR) {
uint256 entireSystemColl = getEntireSystemColl();
uint256 entireSystemDebt = getEntireSystemDebt();
TCR = LiquityMath._computeCR(entireSystemColl, entireSystemDebt, _price);
return TCR;
}
function _checkRecoveryMode(uint256 _price) internal view returns (bool) {
uint256 TCR = _getTCR(_price);
return TCR < liquityBaseParams.CCR();
}
function _requireUserAcceptsFee(
uint256 _fee,
uint256 _amount,
uint256 _maxFeePercentage
) internal pure {
uint256 feePercentage = _fee.mul(DECIMAL_PRECISION).div(_amount);
require(feePercentage <= _maxFeePercentage, "Fee exceeded provided maximum");
}
}