-
Notifications
You must be signed in to change notification settings - Fork 47
/
LoanTokenSettingsLowerAdmin.sol
335 lines (303 loc) · 12.1 KB
/
LoanTokenSettingsLowerAdmin.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/
pragma solidity 0.5.17;
pragma experimental ABIEncoderV2;
import "../../AdvancedToken.sol";
import "../../interfaces/ProtocolSettingsLike.sol";
import "../../LoanTokenLogicStorage.sol";
contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage {
using SafeMath for uint256;
/// @dev TODO: Check for restrictions in this contract.
modifier onlyAdmin() {
require(isOwner() || msg.sender == admin, "unauthorized");
_;
}
/* Events */
event SetTransactionLimits(address[] addresses, uint256[] limits);
event ToggledFunctionPaused(string functionId, bool prevFlag, bool newFlag);
event WithdrawRBTCTo(address indexed to, uint256 amount);
/* Functions */
/**
* @notice This function is MANDATORY, which will be called by LoanTokenLogicBeacon and be registered.
* Every new public function, the signature needs to be included in this function.
*
* @dev This function will return the list of function signature in this contract that are available for public call
* Then this function will be called by LoanTokenLogicBeacon, and the function signatures will be registred in LoanTokenLogicBeacon.
* @dev To save the gas we can just directly return the list of function signature from this pure function.
* The other workaround (fancy way) is we can create a storage for the list of the function signature, and then we can store each function signature to that storage from the constructor.
* Then, in this function we just need to return that storage variable.
*
* @return The list of function signatures (bytes4[])
*/
function getListFunctionSignatures()
external
pure
returns (bytes4[] memory functionSignatures, bytes32 moduleName)
{
bytes4[] memory res = new bytes4[](15);
res[0] = this.setAdmin.selector;
res[1] = this.setPauser.selector;
res[2] = this.setupLoanParams.selector;
res[3] = this.disableLoanParams.selector;
res[4] = this.setDemandCurve.selector;
res[5] = this.toggleFunctionPause.selector;
res[6] = this.setTransactionLimits.selector;
res[7] = this.changeLoanTokenNameAndSymbol.selector;
res[8] = this.pauser.selector;
res[9] = this.setLiquidityMiningAddress.selector;
res[10] = this.withdrawRBTCTo.selector;
res[11] = this.getLiquidityMiningAddress.selector;
res[12] = this.checkPause.selector;
res[13] = this.setStakingContractAddress.selector;
res[14] = this.getStakingContractAddress.selector;
return (res, stringToBytes32("LoanTokenSettingsLowerAdmin"));
}
/**
* @notice Set admin account.
* @param _admin The address of the account to grant admin permissions.
* */
function setAdmin(address _admin) public onlyOwner {
admin = _admin;
}
/**
* @notice Set pauser account.
* @param _pauser The address of the account to grant pause permissions.
* */
function setPauser(address _pauser) public onlyOwner {
pauser = _pauser;
}
/**
* @notice Fallback function not allowed
* */
function() external {
revert("LoanTokenSettingsLowerAdmin - fallback not allowed");
}
/**
* @notice Set loan token parameters.
*
* @param loanParamsList The array of loan parameters.
* @param areTorqueLoans Whether the loan is a torque loan.
* */
function setupLoanParams(
LoanParamsStruct.LoanParams[] memory loanParamsList,
bool areTorqueLoans
) public onlyAdmin {
bytes32[] memory loanParamsIdList;
address _loanTokenAddress = loanTokenAddress;
for (uint256 i = 0; i < loanParamsList.length; i++) {
loanParamsList[i].loanToken = _loanTokenAddress;
loanParamsList[i].maxLoanTerm = areTorqueLoans ? 0 : 28 days;
}
loanParamsIdList = ProtocolSettingsLike(sovrynContractAddress).setupLoanParams(
loanParamsList
);
for (uint256 i = 0; i < loanParamsIdList.length; i++) {
loanParamsIds[
uint256(
keccak256(
abi.encodePacked(
loanParamsList[i].collateralToken,
areTorqueLoans /// isTorqueLoan
)
)
)
] = loanParamsIdList[i];
}
}
/**
* @notice Disable loan token parameters.
*
* @param collateralTokens The array of collateral tokens.
* @param isTorqueLoans Whether the loan is a torque loan.
* */
function disableLoanParams(
address[] calldata collateralTokens,
bool[] calldata isTorqueLoans
) external onlyAdmin {
require(collateralTokens.length == isTorqueLoans.length, "count mismatch");
bytes32[] memory loanParamsIdList = new bytes32[](collateralTokens.length);
for (uint256 i = 0; i < collateralTokens.length; i++) {
uint256 id = uint256(
keccak256(abi.encodePacked(collateralTokens[i], isTorqueLoans[i]))
);
loanParamsIdList[i] = loanParamsIds[id];
delete loanParamsIds[id];
}
ProtocolSettingsLike(sovrynContractAddress).disableLoanParams(loanParamsIdList);
}
/**
* @notice Set loan token parameters about the demand curve.
*
* @dev These params should be percentages represented
* like so: 5% = 5000000000000000000 /// 18 digits precision.
* rateMultiplier + baseRate can't exceed 100%
*
* To maintain a healthy credit score, it's important to keep your
* credit utilization rate (CUR) low (_lowUtilBaseRate). In general
* you don't want your CUR to exceed 30%, but increasingly financial
* experts are recommending that you don't want to go above 10% if you
* really want an excellent credit score.
*
* Interest rates tend to cluster around the kink level of a kinked
* interest rate model. More info at https://arxiv.org/pdf/2006.13922.pdf
* and https://compound.finance/governance/proposals/12
*
* @param _baseRate The interest rate.
* @param _rateMultiplier The precision multiplier for base rate.
* @param _lowUtilBaseRate The credit utilization rate (CUR) low value.
* @param _lowUtilRateMultiplier The precision multiplier for low util base rate.
* @param _targetLevel The target level.
* @param _kinkLevel The level that interest rates cluster on kinked model.
* @param _maxScaleRate The maximum rate of the scale.
* */
function setDemandCurve(
uint256 _baseRate,
uint256 _rateMultiplier,
uint256 _lowUtilBaseRate,
uint256 _lowUtilRateMultiplier,
uint256 _targetLevel,
uint256 _kinkLevel,
uint256 _maxScaleRate
) public onlyAdmin {
require(_rateMultiplier.add(_baseRate) <= WEI_PERCENT_PRECISION, "curve params too high");
require(
_lowUtilRateMultiplier.add(_lowUtilBaseRate) <= WEI_PERCENT_PRECISION,
"curve params too high"
);
require(
_targetLevel <= WEI_PERCENT_PRECISION && _kinkLevel <= WEI_PERCENT_PRECISION,
"levels too high"
);
baseRate = _baseRate;
rateMultiplier = _rateMultiplier;
lowUtilBaseRate = _lowUtilBaseRate;
lowUtilRateMultiplier = _lowUtilRateMultiplier;
targetLevel = _targetLevel; /// 80 ether
kinkLevel = _kinkLevel; /// 90 ether
maxScaleRate = _maxScaleRate; /// 100 ether
}
/**
* @notice Set the pause flag for a function to true or false.
*
* @dev Combining the hash of "iToken_FunctionPause" string and a function
* selector gets a slot to write a flag for pause state.
*
* @param funcId The ID of a function, the selector.
* @param isPaused true/false value of the flag.
* */
function toggleFunctionPause(
string memory funcId, /// example: "mint(uint256,uint256)"
bool isPaused
) public onlyPauserOrOwner {
bool paused;
/// keccak256("iToken_FunctionPause")
bytes32 slot = keccak256(
abi.encodePacked(
bytes4(keccak256(abi.encodePacked(funcId))),
uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2)
)
);
assembly {
paused := sload(slot)
}
require(paused != isPaused, "isPaused is already set to that value");
assembly {
sstore(slot, isPaused)
}
emit ToggledFunctionPaused(funcId, !isPaused, isPaused);
}
/**
* Set the transaction limit per token address.
* @param addresses The token addresses.
* @param limits The limit denominated in the currency of the token address.
* */
function setTransactionLimits(
address[] memory addresses,
uint256[] memory limits
) public onlyAdmin {
require(addresses.length == limits.length, "mismatched array lengths");
for (uint256 i = 0; i < addresses.length; i++) {
transactionLimit[addresses[i]] = limits[i];
}
emit SetTransactionLimits(addresses, limits);
}
/**
* @notice Update the loan token parameters.
* @param _name The new name of the loan token.
* @param _symbol The new symbol of the loan token.
* */
function changeLoanTokenNameAndSymbol(
string memory _name,
string memory _symbol
) public onlyAdmin {
name = _name;
symbol = _symbol;
}
/**
* @notice Withdraws RBTC from the contract by Multisig.
* @param _receiverAddress The address where the rBTC has to be transferred.
* @param _amount The amount of rBTC to be transferred.
*/
function withdrawRBTCTo(address payable _receiverAddress, uint256 _amount) external onlyOwner {
require(_receiverAddress != address(0), "receiver address invalid");
require(_amount > 0, "non-zero withdraw amount expected");
require(_amount <= address(this).balance, "withdraw amount cannot exceed balance");
_receiverAddress.transfer(_amount);
emit WithdrawRBTCTo(_receiverAddress, _amount);
}
/**
* @notice sets the liquidity mining contract address
* @param LMAddress the address of the liquidity mining contract
*/
function setLiquidityMiningAddress(address LMAddress) external onlyOwner {
liquidityMiningAddress = LMAddress;
}
/**
* @notice We need separate getter for newly added storage variable
* @notice Getter for liquidityMiningAddress
* @return liquidityMiningAddress
*/
function getLiquidityMiningAddress() public view returns (address) {
return liquidityMiningAddress;
}
/**
* @notice sets the staking contract address
* @param _stakingContractAddress the address of the staking contract
*/
function setStakingContractAddress(address _stakingContractAddress) external onlyOwner {
stakingContractAddress = _stakingContractAddress;
}
/**
* @notice We need separate getter for newly added storage variable
* @notice Getter for stakingContractAddress
* @return stakingContractAddress
*/
function getStakingContractAddress() public view returns (address) {
return stakingContractAddress;
}
/**
* @notice Check whether a function is paused.
*
* @dev Used to read externally from the smart contract to see if a
* function is paused.
*
* @param funcId The function ID, the selector.
*
* @return isPaused Whether the function is paused: true or false.
* */
function checkPause(string memory funcId) public view returns (bool isPaused) {
bytes4 sig = bytes4(keccak256(abi.encodePacked(funcId)));
bytes32 slot = keccak256(
abi.encodePacked(
sig,
uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2)
)
);
assembly {
isPaused := sload(slot)
}
return isPaused;
}
}