-
Notifications
You must be signed in to change notification settings - Fork 5
/
Engine.sol
365 lines (298 loc) · 13.5 KB
/
Engine.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import {StableCoin} from "./StableCoin.sol";
import {OracleLib} from "./libraries/OracleLib.sol";
/**
* @title Stablecoin Engine
* @notice This contract is required to manage mint() and burn() of the stablecoin.
* This mechanism is necessary to maintain the peg of 1 DSC = 1$.
*
* Important! Our system must always be overcollateralized.
* Threshold: 150%
*/
contract Engine is ReentrancyGuard {
using OracleLib for AggregatorV3Interface;
uint256 public constant ADDITIONAL_FEED_PRECISION = 1e10;
uint256 public constant PRECISION = 1e18;
uint256 public constant LIQUIDATION_THRESHOLD = 50; // Это значение будет требовать 200% сверх обеспечения.
uint256 public constant LIQUIDATION_PRECISION = 100;
uint256 public constant LIQUIDATION_BONUS = 10; // 10%
uint256 public constant MIN_HEALTH_FACTOR = 1e18;
mapping(address token => address priceFeeds) private _priceFeeds;
mapping(address user => mapping(address token => uint256 amount)) private _collateralDeposited;
mapping(address user => uint256 amountDscMinted) private _dscMinted;
address[] private _collateralTokens;
StableCoin private _dsc;
event CollateralDeposited(address indexed user, address indexed collateralToken, uint256 collateralAmount);
event CollateralRedeemed(
address indexed redeemedFrom,
address indexed redeemedTo,
address indexed collateralToken,
uint256 collateralAmount
);
error ZeroAmount();
error TokenAddressesAndPriceFeedAddressesShouldBeSameLength();
error NotAllowedToken();
error TransferFailed();
error BreaksHealthFactor(uint256 healthFactor);
error MintFailed();
error HealthFactorIsPositive();
error HealthFactorNotImproved();
modifier notZeroAmount(uint256 amount) {
if (amount == 0) {
revert ZeroAmount();
}
_;
}
modifier isAllowedToken(address token) {
if (_priceFeeds[token] == address(0)) {
revert NotAllowedToken();
}
_;
}
constructor(address[] memory tokens, address[] memory priceFeeds, address dsc) {
if (tokens.length != priceFeeds.length) {
revert TokenAddressesAndPriceFeedAddressesShouldBeSameLength();
}
for (uint256 i = 0; i < tokens.length; i++) {
_priceFeeds[tokens[i]] = priceFeeds[i];
_collateralTokens.push(tokens[i]);
}
_dsc = StableCoin(dsc);
}
// region - External and public functions -
/**
* @notice Allows sending collateral to receive the stablecoin
* @param collateralToken The address of the collateral token
* @param collateralAmount The amount of collateral to deposit
* @param amountToMint The amount to mint the stablecoin
*/
function depositCollateralAndMintDsc(address collateralToken, uint256 collateralAmount, uint256 amountToMint)
external
{
depositCollateral(collateralToken, collateralAmount);
mintDsc(amountToMint);
}
/**
* @notice Allows depositing collateral to secure the stablecoin
* @param collateralToken The address of the collateral token
* @param collateralAmount The amount of collateral to deposit
*/
function depositCollateral(address collateralToken, uint256 collateralAmount)
public
notZeroAmount(collateralAmount)
isAllowedToken(collateralToken)
nonReentrant
{
_collateralDeposited[msg.sender][collateralToken] += collateralAmount;
bool success = IERC20(collateralToken).transferFrom(msg.sender, address(this), collateralAmount);
if (!success) {
revert TransferFailed();
}
emit CollateralDeposited(msg.sender, collateralToken, collateralAmount);
}
/**
* @notice Allows you to retrieve collateral in exchange for stablecoin
* @param collateralToken The address of the collateral token
* @param collateralAmount The amount of collateral being deposited
* @param amountToBurn The amount to burn in stablecoin
*/
function redeemCollateralAndBurnDsc(address collateralToken, uint256 collateralAmount, uint256 amountToBurn)
external
{
// First, burn the stablecoin
burnDsc(amountToBurn);
redeemCollateral(collateralToken, collateralAmount);
}
/**
* @notice Allows you to retrieve collateral
* @param collateralToken The address of the collateral token
* @param collateralAmount The amount of collateral being deposited
*/
function redeemCollateral(address collateralToken, uint256 collateralAmount)
public
notZeroAmount(collateralAmount)
isAllowedToken(collateralToken)
nonReentrant
{
_redeemCollateral(collateralToken, collateralAmount, msg.sender, msg.sender);
_revertIfHealthFactorIsBroken(msg.sender);
}
/**
* @notice Минтит стейблкоин
* @param amountToMint The amount to mint the stablecoin
*/
function mintDsc(uint256 amountToMint) public notZeroAmount(amountToMint) nonReentrant {
_dscMinted[msg.sender] += amountToMint;
_revertIfHealthFactorIsBroken(msg.sender);
bool minted = _dsc.mint(msg.sender, amountToMint);
if (!minted) {
revert MintFailed();
}
}
/**
* @notice Burns the stablecoin
* @param amountToBurn The amount to burn the stablecoin
*/
function burnDsc(uint256 amountToBurn) public notZeroAmount(amountToBurn) nonReentrant {
_burnDsc(amountToBurn, msg.sender, msg.sender);
_revertIfHealthFactorIsBroken(msg.sender);
}
/**
* @notice Allows liquidating a user's collateral and receiving a reward
* @param collateralToken The address of the collateral token
* @param user The user whose collateral can be liquidated due to insufficient collateralization
* @param debtToCover The amount of stablecoin to be burned to adjust the user's health factor
*/
function liquidate(address collateralToken, address user, uint256 debtToCover)
external
notZeroAmount(debtToCover)
nonReentrant
{
uint256 startingUserHealthFactor = _healthFactor(user);
if (startingUserHealthFactor >= MIN_HEALTH_FACTOR) {
revert HealthFactorIsPositive();
}
uint256 tokenAmountFromDebtCovered = getTokenAmountFromUsd(collateralToken, debtToCover);
uint256 bonusCollateral = tokenAmountFromDebtCovered * LIQUIDATION_BONUS / LIQUIDATION_PRECISION;
uint256 totalCollateralToRedeem = tokenAmountFromDebtCovered + bonusCollateral;
_redeemCollateral(collateralToken, totalCollateralToRedeem, user, msg.sender);
_burnDsc(debtToCover, user, msg.sender);
uint256 endingUserHealthFactor = _healthFactor(user);
if (endingUserHealthFactor <= startingUserHealthFactor) {
revert HealthFactorNotImproved();
}
_revertIfHealthFactorIsBroken(msg.sender);
}
// endregion
// region - Public and external view functions -
/**
* @notice Returns the total collateral amount in USD
* @param user The user for whom to calculate the collateral
* @dev The amount is calculated for each token that can be used as collateral on the protocol
*/
function getAccountCollateralValue(address user) public view returns (uint256 totalCollateralValueInUsd) {
for (uint256 i = 0; i < _collateralTokens.length; i++) {
address token = _collateralTokens[i];
uint256 amount = _collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}
}
/**
* @notice Returns the amount in USD
* @param token The token address for which the amount needs to be converted to USD
* @param amount The amount of the token
* @dev The USD amount is calculated based on the obtained price from the Chainlink oracle
*/
function getUsdValue(address token, uint256 amount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(_priceFeeds[token]);
(, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
return uint256(price) * ADDITIONAL_FEED_PRECISION * amount / PRECISION;
}
/**
* @notice Returns the equivalent amount of tokens in USD
* @param token The token address for which the amount should be converted to USD
* @param usdAmount The amount in USD, in wei format
* @dev Token to USD conversion is determined using data from the Chainlink oracle
*/
function getTokenAmountFromUsd(address token, uint256 usdAmount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(_priceFeeds[token]);
(, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
return (usdAmount * PRECISION) / (uint256(price) * ADDITIONAL_FEED_PRECISION);
}
/**
* @notice Returns the health factor
* @param user The user's address for which to retrieve the health factor
* @dev The health factor indicates the possibility of liquidating a user's collateral
*/
function getHealthFactor(address user) external view returns (uint256) {
return _healthFactor(user);
}
/**
* @notice Returns the addresses of tokens allowed for use as collateral
*/
function getCollateralTokens() external view returns (address[] memory) {
return _collateralTokens;
}
/**
* @notice Returns the address of the Chainlink price feed for the token
*/
function getCollateralTokenPriceFeed(address token) external view returns (address) {
return _priceFeeds[token];
}
/**
* @notice Returns the collateral amount deposited by the user on the protocol in the specified token
* @param user The user's address for whom the collateral amount is requested
* @param token The address of the token for which the collateral amount needs to be returned
*/
function getCollateralBalanceOfUser(address user, address token) external view returns (uint256) {
return _collateralDeposited[user][token];
}
/**
* @notice Returns information about the account: the total amount of stablecoin and the collateral amount in USD
* @param user The user's address for whom to retrieve account information
*/
function getAccountInformation(address user)
external
view
returns (uint256 totalDscMinted, uint256 collateralValueInUsd)
{
(totalDscMinted, collateralValueInUsd) = _getAccountInformation(user);
}
// endregion
// region - Private and internal view functions -
function _redeemCollateral(address collateralToken, uint256 collateralAmount, address from, address to) private {
_collateralDeposited[from][collateralToken] -= collateralAmount;
bool success = IERC20(collateralToken).transfer(to, collateralAmount);
if (!success) {
revert TransferFailed();
}
emit CollateralRedeemed(from, to, collateralToken, collateralAmount);
}
function _burnDsc(uint256 amountToBurn, address onBehalfOf, address from) private {
_dscMinted[onBehalfOf] -= amountToBurn;
bool success = _dsc.transferFrom(from, address(this), amountToBurn);
if (!success) {
revert TransferFailed();
}
_dsc.burn(amountToBurn);
}
function _getAccountInformation(address user)
private
view
returns (uint256 totalDscMinted, uint256 collateralValueInUsd)
{
totalDscMinted = _dscMinted[user];
collateralValueInUsd = getAccountCollateralValue(user);
}
/**
* @notice Returns the liquidation factor of a user's collateral
* @dev If the factor for a user is below 1, their collateral can be liquidated
* LIQUIDATION_THRESHOLD = 50 // Essentially, this is the requirement that you can borrow a maximum of 50% of the collateral
* or conversely, the requirement that the borrowed stablecoin must have collateral of 200%
* LIQUIDATION_PRECISION = 100
* So, for example, if the total collateral = $1000. Then the maximum you can borrow is 500 stablecoins.
* Or, to borrow 500 stablecoins, you need to have collateral = $1000. This is equivalent to 200%.
* You can check this by substituting values into the formula of the function:
* threshold = totalCollateral * 0.5 / totalMinted.
* If threshold < 1, liquidation is possible, otherwise not.
*/
function _healthFactor(address user) private view returns (uint256) {
(uint256 totalDscMinted, uint256 collateralValueInUsd) = _getAccountInformation(user);
if (totalDscMinted == 0) {
return type(uint256).max;
}
uint256 collateralAdjustedForThreshold = collateralValueInUsd * LIQUIDATION_THRESHOLD / LIQUIDATION_PRECISION;
return collateralAdjustedForThreshold * PRECISION / totalDscMinted;
}
function _revertIfHealthFactorIsBroken(address user) internal view {
uint256 userHealthFactor = _healthFactor(user);
if (userHealthFactor < MIN_HEALTH_FACTOR) {
revert BreaksHealthFactor(userHealthFactor);
}
}
// endregion
}