Skip to content

Commit

Permalink
Merge pull request #157 from liquity/reduce_liquidation_penalty
Browse files Browse the repository at this point in the history
Reduce liquidation penalty
  • Loading branch information
bingen authored May 15, 2024
2 parents a5fc3be + 2de444e commit 13b2ab0
Show file tree
Hide file tree
Showing 22 changed files with 739 additions and 103 deletions.
32 changes: 18 additions & 14 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
// --- Connected contract declarations ---

IERC20 public immutable ETH;
ITroveManager public troveManager;
ITroveManager public immutable troveManager;
address gasPoolAddress;
ICollSurplusPool collSurplusPool;
IBoldToken public boldToken;
// A doubly linked list of Troves, sorted by their collateral ratios
ISortedTroves public sortedTroves;

// Minimum collateral ratio for individual troves
uint256 public immutable MCR;

/* --- Variable container structs ---
Used to hold, return and assign variables inside a function, in order to avoid the error:
Expand Down Expand Up @@ -97,15 +100,21 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
);
event BoldBorrowingFeePaid(uint256 indexed _troveId, uint256 _boldFee);

constructor(address _ETHAddress) {
checkContract(_ETHAddress);
ETH = IERC20(_ETHAddress);
constructor(IERC20 _ETH, ITroveManager _troveManager) {
checkContract(address(_ETH));
checkContract(address(_troveManager));

ETH = _ETH;
troveManager = _troveManager;

MCR = _troveManager.MCR();

emit TroveManagerAddressChanged(address(_troveManager));
}

// --- Dependency setters ---

function setAddresses(
address _troveManagerAddress,
address _activePoolAddress,
address _defaultPoolAddress,
address _gasPoolAddress,
Expand All @@ -117,7 +126,6 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
// This makes impossible to open a trove with zero withdrawn Bold
assert(MIN_NET_DEBT > 0);

checkContract(_troveManagerAddress);
checkContract(_activePoolAddress);
checkContract(_defaultPoolAddress);
checkContract(_gasPoolAddress);
Expand All @@ -126,7 +134,6 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
checkContract(_sortedTrovesAddress);
checkContract(_boldTokenAddress);

troveManager = ITroveManager(_troveManagerAddress);
activePool = IActivePool(_activePoolAddress);
defaultPool = IDefaultPool(_defaultPoolAddress);
gasPoolAddress = _gasPoolAddress;
Expand All @@ -135,7 +142,6 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
sortedTroves = ISortedTroves(_sortedTrovesAddress);
boldToken = IBoldToken(_boldTokenAddress);

emit TroveManagerAddressChanged(_troveManagerAddress);
emit ActivePoolAddressChanged(_activePoolAddress);
emit DefaultPoolAddressChanged(_defaultPoolAddress);
emit GasPoolAddressChanged(_gasPoolAddress);
Expand Down Expand Up @@ -501,13 +507,11 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
}

/**
* Claim remaining collateral from a redemption or from a liquidation with ICR > MCR in Recovery Mode
* Claim remaining collateral from a liquidation with ICR exceeding the liquidation penalty
*/
function claimCollateral(uint256 _troveId) external override {
_requireIsOwner(_troveId);

function claimCollateral() external override {
// send ETH from CollSurplus Pool to owner
collSurplusPool.claimColl(msg.sender, _troveId);
collSurplusPool.claimColl(msg.sender);
}

// --- Helper functions ---
Expand Down Expand Up @@ -726,7 +730,7 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
}
}

function _requireICRisAboveMCR(uint256 _newICR) internal pure {
function _requireICRisAboveMCR(uint256 _newICR) internal view {
require(_newICR >= MCR, "BorrowerOps: An operation that would result in ICR < MCR is not permitted");
}

Expand Down
32 changes: 13 additions & 19 deletions contracts/src/CollSurplusPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";

import "./Interfaces/ICollSurplusPool.sol";
import "./Dependencies/Ownable.sol";
import "./Dependencies/CheckContract.sol";

contract CollSurplusPool is Ownable, CheckContract, ICollSurplusPool {
contract CollSurplusPool is Ownable, ICollSurplusPool {
using SafeERC20 for IERC20;

string public constant NAME = "CollSurplusPool";
Expand All @@ -21,19 +20,18 @@ contract CollSurplusPool is Ownable, CheckContract, ICollSurplusPool {
// deposited ether tracker
uint256 internal ETHBalance;
// Collateral surplus claimable by trove owners
mapping(uint256 => uint256) internal balances;
mapping(address => uint256) internal balances;

// --- Events ---

event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress);
event TroveManagerAddressChanged(address _newTroveManagerAddress);
event ActivePoolAddressChanged(address _newActivePoolAddress);

event CollBalanceUpdated(uint256 indexed _troveId, uint256 _newBalance);
event CollBalanceUpdated(address indexed _account, uint256 _newBalance);
event EtherSent(address _to, uint256 _amount);

constructor(address _ETHAddress) {
checkContract(_ETHAddress);
ETH = IERC20(_ETHAddress);
}

Expand All @@ -44,10 +42,6 @@ contract CollSurplusPool is Ownable, CheckContract, ICollSurplusPool {
override
onlyOwner
{
checkContract(_borrowerOperationsAddress);
checkContract(_troveManagerAddress);
checkContract(_activePoolAddress);

borrowerOperationsAddress = _borrowerOperationsAddress;
troveManagerAddress = _troveManagerAddress;
activePoolAddress = _activePoolAddress;
Expand All @@ -65,29 +59,29 @@ contract CollSurplusPool is Ownable, CheckContract, ICollSurplusPool {
return ETHBalance;
}

function getCollateral(uint256 _troveId) external view override returns (uint256) {
return balances[_troveId];
function getCollateral(address _account) external view override returns (uint256) {
return balances[_account];
}

// --- Pool functionality ---

function accountSurplus(uint256 _troveId, uint256 _amount) external override {
function accountSurplus(address _account, uint256 _amount) external override {
_requireCallerIsTroveManager();

uint256 newAmount = balances[_troveId] + _amount;
balances[_troveId] = newAmount;
uint256 newAmount = balances[_account] + _amount;
balances[_account] = newAmount;
ETHBalance = ETHBalance + _amount;

emit CollBalanceUpdated(_troveId, newAmount);
emit CollBalanceUpdated(_account, newAmount);
}

function claimColl(address _account, uint256 _troveId) external override {
function claimColl(address _account) external override {
_requireCallerIsBorrowerOperations();
uint256 claimableColl = balances[_troveId];
uint256 claimableColl = balances[_account];
require(claimableColl > 0, "CollSurplusPool: No collateral available to claim");

balances[_troveId] = 0;
emit CollBalanceUpdated(_troveId, 0);
balances[_account] = 0;
emit CollBalanceUpdated(_account, 0);

ETHBalance = ETHBalance - claimableColl;
emit EtherSent(_account, claimableColl);
Expand Down
3 changes: 0 additions & 3 deletions contracts/src/Dependencies/LiquityBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ contract LiquityBase is BaseMath, ILiquityBase {

uint256 public constant _100pct = 1000000000000000000; // 1e18 == 100%

// Minimum collateral ratio for individual troves
uint256 public constant MCR = 1100000000000000000; // 110%

// Critical system collateral ratio. If the system's total collateral ratio (TCR) falls below the CCR, some borrowing operation restrictions are applied
uint256 public constant CCR = 1500000000000000000; // 150%

Expand Down
4 changes: 4 additions & 0 deletions contracts/src/Dependencies/LiquityMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ library LiquityMath {
return (_a >= _b) ? _a : _b;
}

function _sub_min_0(uint256 _a, uint256 _b) internal pure returns (uint256) {
return (_a > _b) ? _a - _b : 0;
}

/*
* Multiply two decimal numbers and use normal rounding rules:
* -round product up if 19'th mantissa digit >= 5
Expand Down
3 changes: 1 addition & 2 deletions contracts/src/Interfaces/IBorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface IBorrowerOperations is ILiquityBase {
function sortedTroves() external view returns (ISortedTroves);

function setAddresses(
address _troveManagerAddress,
address _activePoolAddress,
address _defaultPoolAddress,
address _gasPoolAddress,
Expand Down Expand Up @@ -61,7 +60,7 @@ interface IBorrowerOperations is ILiquityBase {
uint256 _lowerHint
) external;

function claimCollateral(uint256 _troveId) external;
function claimCollateral() external;

function setAddManager(uint256 _troveId, address _manager) external;
function setRemoveManager(uint256 _troveId, address _manager) external;
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/Interfaces/ICollSurplusPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ interface ICollSurplusPool {

function getETHBalance() external view returns (uint256);

function getCollateral(uint256 _troveId) external view returns (uint256);
function getCollateral(address _account) external view returns (uint256);

function accountSurplus(uint256 _troveId, uint256 _amount) external;
function accountSurplus(address _account, uint256 _amount) external;

function claimColl(address _account, uint256 _troveId) external;
function claimColl(address _account) external;
}
1 change: 0 additions & 1 deletion contracts/src/Interfaces/ILiquityBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ interface ILiquityBase {
function priceFeed() external view returns (IPriceFeed);
function BOLD_GAS_COMPENSATION() external view returns (uint256);
function MIN_NET_DEBT() external view returns (uint256);
function MCR() external view returns (uint256);
function getEntireSystemDebt() external view returns (uint256);
}
2 changes: 2 additions & 0 deletions contracts/src/Interfaces/ITroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "./ISortedTroves.sol";

// Common interface for the Trove Manager.
interface ITroveManager is IERC721, ILiquityBase {
function MCR() external view returns (uint256);

function setAddresses(
address _borrowerOperationsAddress,
address _activePoolAddress,
Expand Down
Loading

0 comments on commit 13b2ab0

Please sign in to comment.