This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJUSDView.sol
205 lines (186 loc) · 7.21 KB
/
JUSDView.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
/*
Copyright 2022 JOJO Exchange
SPDX-License-Identifier: BUSL-1.1*/
pragma solidity ^0.8.9;
import "./JUSDBankStorage.sol";
import {DecimalMath} from "../lib/DecimalMath.sol";
import "../Interface/IJUSDBank.sol";
import {IPriceChainLink} from "../Interface/IPriceChainLink.sol";
abstract contract JUSDView is JUSDBankStorage, IJUSDBank {
using DecimalMath for uint256;
function getReservesList() external view returns (address[] memory) {
return reservesList;
}
function getDepositMaxMintAmount(
address user
) external view returns (uint256) {
DataTypes.UserInfo storage userInfo = userInfo[user];
return _maxMintAmountBorrow(userInfo);
}
function getCollateralMaxMintAmount(
address collateral,
uint256 amount
) external view returns (uint256 maxAmount) {
DataTypes.ReserveInfo memory reserve = reserveInfo[collateral];
return _getMintAmountBorrow(reserve, amount);
}
function getMaxWithdrawAmount(
address collateral,
address user
) external view returns (uint256 maxAmount) {
DataTypes.UserInfo storage userInfo = userInfo[user];
uint256 JUSDBorrow = userInfo.t0BorrowBalance.decimalMul(getTRate());
if (JUSDBorrow == 0) {
return userInfo.depositBalance[collateral];
}
uint256 maxMintAmount = _maxMintAmount(userInfo);
if (maxMintAmount <= JUSDBorrow) {
maxAmount = 0;
} else {
DataTypes.ReserveInfo memory reserve = reserveInfo[collateral];
uint256 remainAmount = (maxMintAmount - JUSDBorrow).decimalDiv(
reserve.initialMortgageRate.decimalMul(
IPriceChainLink(reserve.oracle).getAssetPrice()
)
);
remainAmount >= userInfo.depositBalance[collateral]
? maxAmount = userInfo.depositBalance[collateral]
: maxAmount = remainAmount;
}
}
function isAccountSafe(address user) external view returns (bool) {
DataTypes.UserInfo storage userInfo = userInfo[user];
return !_isStartLiquidation(userInfo, getTRate());
}
function getCollateralPrice(
address collateral
) external view returns (uint256) {
return IPriceChainLink(reserveInfo[collateral].oracle).getAssetPrice();
}
function getIfHasCollateral(
address from,
address collateral
) external view returns (bool) {
return userInfo[from].hasCollateral[collateral];
}
function getDepositBalance(
address collateral,
address from
) external view returns (uint256) {
return userInfo[from].depositBalance[collateral];
}
function getBorrowBalance(address from) external view returns (uint256) {
return (userInfo[from].t0BorrowBalance * getTRate()) / 1e18;
}
function getUserCollateralList(
address from
) external view returns (address[] memory) {
return userInfo[from].collateralList;
}
/// @notice get the JUSD mint amount
function _getMintAmount(
uint256 balance,
address oracle,
uint256 rate
) internal view returns (uint256) {
return
IPriceChainLink(oracle)
.getAssetPrice()
.decimalMul(balance)
.decimalMul(rate);
}
function _getMintAmountBorrow(
DataTypes.ReserveInfo memory reserve,
uint256 amount
) internal view returns (uint256) {
uint256 depositAmount = IPriceChainLink(reserve.oracle)
.getAssetPrice()
.decimalMul(amount)
.decimalMul(reserve.initialMortgageRate);
if (depositAmount >= reserve.maxColBorrowPerAccount) {
depositAmount = reserve.maxColBorrowPerAccount;
}
return depositAmount;
}
/// @notice according to the initialMortgageRate to judge whether the user's account is safe after borrow, withdraw, flashloan
/// If the collateral is not allowed to be borrowed. When calculating max mint JUSD amount, treat the value of collateral as 0
/// maxMintAmount = sum(collateral amount * price * initialMortgageRate)
function _isAccountSafe(
DataTypes.UserInfo storage user,
uint256 tRate
) internal view returns (bool) {
return user.t0BorrowBalance.decimalMul(tRate) <= _maxMintAmount(user);
}
function _isAccountSafeAfterBorrow(
DataTypes.UserInfo storage user,
uint256 tRate
) internal view returns (bool) {
return
user.t0BorrowBalance.decimalMul(tRate) <=
_maxMintAmountBorrow(user);
}
function _maxMintAmount(
DataTypes.UserInfo storage user
) internal view returns (uint256) {
address[] memory collaterals = user.collateralList;
uint256 maxMintAmount;
for (uint256 i; i < collaterals.length; i = i + 1) {
address collateral = collaterals[i];
DataTypes.ReserveInfo memory reserve = reserveInfo[collateral];
if (!reserve.isBorrowAllowed) {
continue;
}
maxMintAmount += _getMintAmount(
user.depositBalance[collateral],
reserve.oracle,
reserve.initialMortgageRate
);
}
return maxMintAmount;
}
function _maxMintAmountBorrow(
DataTypes.UserInfo storage user
) internal view returns (uint256) {
address[] memory collaterals = user.collateralList;
uint256 maxMintAmount;
for (uint256 i; i < collaterals.length; i = i + 1) {
address collateral = collaterals[i];
DataTypes.ReserveInfo memory reserve = reserveInfo[collateral];
if (!reserve.isBorrowAllowed) {
continue;
}
uint256 colMintAmount = _getMintAmountBorrow(
reserve,
user.depositBalance[collateral]
);
maxMintAmount += colMintAmount;
}
return maxMintAmount;
}
/// @notice Determine whether the account is safe by liquidationMortgageRate
// If the collateral delisted. When calculating the boundary conditions for collateral to be liquidated, treat the value of collateral as 0
// liquidationMaxMintAmount = sum(depositAmount * price * liquidationMortgageRate)
function _isStartLiquidation(
DataTypes.UserInfo storage liquidatedTraderInfo,
uint256 tRate
) internal view returns (bool) {
uint256 JUSDBorrow = (liquidatedTraderInfo.t0BorrowBalance).decimalMul(
tRate
);
uint256 liquidationMaxMintAmount;
address[] memory collaterals = liquidatedTraderInfo.collateralList;
for (uint256 i; i < collaterals.length; i = i + 1) {
address collateral = collaterals[i];
DataTypes.ReserveInfo memory reserve = reserveInfo[collateral];
if (reserve.isFinalLiquidation) {
continue;
}
liquidationMaxMintAmount += _getMintAmount(
liquidatedTraderInfo.depositBalance[collateral],
reserve.oracle,
reserve.liquidationMortgageRate
);
}
return liquidationMaxMintAmount < JUSDBorrow;
}
}