-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCollSurplusPool.sol
More file actions
143 lines (112 loc) · 5.51 KB
/
Copy pathCollSurplusPool.sol
File metadata and controls
143 lines (112 loc) · 5.51 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
import "./Interfaces/ICollSurplusPool.sol";
import "./Dependencies/SafeMath.sol";
import "./Dependencies/CheckContract.sol";
import "./Dependencies/console.sol";
import "./CollSurplusPoolStorage.sol";
contract CollSurplusPool is CollSurplusPoolStorage, CheckContract, ICollSurplusPool {
using SafeMath for uint256;
// --- Events ---
event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress);
event TroveManagerAddressChanged(address _newTroveManagerAddress);
event ActivePoolAddressChanged(address _newActivePoolAddress);
event CollBalanceUpdated(address indexed _account, uint256 _newBalance);
event EtherSent(address _to, uint256 _amount);
// --- Contract setters ---
function setAddresses(
address _borrowerOperationsAddress,
address _troveManagerAddress,
address _activePoolAddress
) external override onlyOwner {
checkContract(_borrowerOperationsAddress);
checkContract(_troveManagerAddress);
checkContract(_activePoolAddress);
borrowerOperationsAddress = _borrowerOperationsAddress;
troveManagerAddress = _troveManagerAddress;
activePoolAddress = _activePoolAddress;
emit BorrowerOperationsAddressChanged(_borrowerOperationsAddress);
emit TroveManagerAddressChanged(_troveManagerAddress);
emit ActivePoolAddressChanged(_activePoolAddress);
}
/** Returns the ETH state variable at ActivePool address.
Not necessarily equal to the raw ether balance - ether can be forcibly sent to contracts. */
function getETH() external view override returns (uint256) {
return ETH;
}
function getCollateral(address _account) external view override returns (uint256) {
return balances[_account];
}
// --- Pool functionality ---
function accountSurplus(address _account, uint256 _amount) external override {
_requireCallerIsTroveManager();
uint256 newAmount = balances[_account].add(_amount);
balances[_account] = newAmount;
emit CollBalanceUpdated(_account, newAmount);
}
function claimColl(address _account) external override {
_requireCallerIsBorrowerOperations();
uint256 claimableColl = balances[_account];
require(claimableColl > 0, "CollSurplusPool: No collateral available to claim");
balances[_account] = 0;
emit CollBalanceUpdated(_account, 0);
ETH = ETH.sub(claimableColl);
emit EtherSent(_account, claimableColl);
(bool success, ) = _account.call{ value: claimableColl }("");
require(success, "CollSurplusPool: sending ETH failed");
}
/// @dev Gas forwarded to the fee receiver's receive hook. Ample for a receiver
/// that only accepts the transfer and logs, while guaranteeing a
/// gas-sinking receiver can never starve the claimant leg: a receiver
/// needing more gas makes the fee leg fail, which is fail-open — the
/// claimant then receives the full balance.
uint256 private constant FEE_LEG_GAS_CAP = 100_000;
/// @notice Two-leg claim: `_feeAmount` to `_feeReceiver`, remainder to `_account`.
/// Only callable by BorrowerOperations (the Perimeter surplus-claim hook);
/// `claimColl` remains the untouched non-charging path.
/// CEI: all effects (balance zeroing, ETH accounting) precede both external
/// calls, so a reentrant claim sees balances == 0 and reverts. The single
/// `ETH` decrement equals fee + net exactly. The fee leg is fail-open —
/// if it fails, the claimant receives the full balance; the user leg stays
/// fail-closed like `claimColl`.
/// @return feePaid true iff the fee transfer succeeded (caller emits the matching event)
function claimCollWithFee(
address _account,
address _feeReceiver,
uint256 _feeAmount
) external override returns (bool feePaid) {
_requireCallerIsBorrowerOperations();
uint256 claimableColl = balances[_account];
require(claimableColl > 0, "CollSurplusPool: No collateral available to claim");
require(_feeAmount <= claimableColl, "CollSurplusPool: fee exceeds claimable");
balances[_account] = 0;
emit CollBalanceUpdated(_account, 0);
ETH = ETH.sub(claimableColl);
(feePaid, ) = _feeReceiver.call{ value: _feeAmount, gas: FEE_LEG_GAS_CAP }("");
uint256 userAmount = feePaid ? claimableColl.sub(_feeAmount) : claimableColl;
if (feePaid) {
emit EtherSent(_feeReceiver, _feeAmount);
}
emit EtherSent(_account, userAmount);
(bool success, ) = _account.call{ value: userAmount }("");
require(success, "CollSurplusPool: sending ETH failed");
}
// --- 'require' functions ---
function _requireCallerIsBorrowerOperations() internal view {
require(
msg.sender == borrowerOperationsAddress,
"CollSurplusPool: Caller is not Borrower Operations"
);
}
function _requireCallerIsTroveManager() internal view {
require(msg.sender == troveManagerAddress, "CollSurplusPool: Caller is not TroveManager");
}
function _requireCallerIsActivePool() internal view {
require(msg.sender == activePoolAddress, "CollSurplusPool: Caller is not Active Pool");
}
// --- Fallback function ---
receive() external payable {
_requireCallerIsActivePool();
ETH = ETH.add(msg.value);
}
}