@ensuro/core 1.2.7 → 2.0.0-beta10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -27
- package/build/contracts/AccessManager.sol/AccessManager.json +559 -0
- package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +219 -0
- package/build/contracts/EToken.sol/EToken.json +217 -190
- package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +17 -4
- package/build/contracts/{extras/IPriceRiskModule.sol/IPriceRiskModule.json → LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json} +81 -131
- package/build/contracts/Policy.sol/Policy.json +2 -2
- package/build/contracts/PolicyPool.sol/PolicyPool.json +425 -295
- package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +15 -2
- package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +868 -0
- package/build/contracts/{BaseAssetManager.sol/BaseAssetManager.json → Reserve.sol/Reserve.json} +37 -166
- package/build/contracts/RiskModule.sol/RiskModule.json +85 -312
- package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +960 -0
- package/build/contracts/{DataTypes.sol/DataTypes.json → TimeScaled.sol/TimeScaled.json} +4 -4
- package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +134 -336
- package/build/contracts/dependencies/ERC1967Proxy.sol/ERC1967Proxy.json +64 -0
- package/build/{interfaces/IExchange.sol/IExchange.json → contracts/dependencies/IERC20Metadata.sol/IERC20Metadata.json} +83 -79
- package/build/contracts/{WadRayMath.sol → dependencies/WadRayMath.sol}/WadRayMath.json +3 -3
- package/build/{interfaces/IPolicyPoolConfig.sol/IPolicyPoolConfig.json → contracts/interfaces/IAccessManager.sol/IAccessManager.json} +79 -77
- package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +109 -0
- package/build/{interfaces → contracts/interfaces}/IEToken.sol/IEToken.json +39 -91
- package/build/{interfaces → contracts/interfaces}/ILPWhitelist.sol/ILPWhitelist.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyHolder.sol/IPolicyHolder.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyPool.sol/IPolicyPool.json +137 -100
- package/build/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +1 -1
- package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +315 -0
- package/build/{interfaces → contracts/interfaces}/IRiskModule.sol/IRiskModule.json +58 -34
- package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +736 -0
- package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +43 -0
- package/build/{interfaces/IInsolvencyHook.sol/IInsolvencyHook.json → contracts/mocks/IMintableERC20.sol/IMintableERC20.json} +13 -8
- package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +228 -0
- package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +35 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +702 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +92 -0
- package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +764 -0
- package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +343 -0
- package/build/contracts/mocks/TestNFT.sol/TestNFT.json +388 -0
- package/contracts/AccessManager.sol +103 -0
- package/contracts/ERC4626AssetManager.sol +52 -0
- package/contracts/EToken.sol +254 -299
- package/contracts/LPManualWhitelist.sol +4 -4
- package/contracts/LiquidityThresholdAssetManager.sol +160 -0
- package/contracts/Policy.sol +49 -45
- package/contracts/PolicyPool.sol +379 -514
- package/contracts/PolicyPoolComponent.sol +41 -41
- package/contracts/PremiumsAccount.sol +480 -0
- package/contracts/Reserve.sol +213 -0
- package/contracts/RiskModule.sol +233 -174
- package/contracts/SignedQuoteRiskModule.sol +241 -0
- package/contracts/TimeScaled.sol +138 -0
- package/contracts/TrustfulRiskModule.sol +85 -23
- package/contracts/dependencies/ERC1967Proxy.sol +19 -0
- package/contracts/dependencies/IERC20Metadata.sol +19 -0
- package/contracts/dependencies/WadRayMath.sol +126 -0
- package/contracts/interfaces/IAccessManager.sol +148 -0
- package/contracts/interfaces/IAssetManager.sol +84 -0
- package/contracts/interfaces/IEToken.sol +195 -0
- package/contracts/interfaces/ILPWhitelist.sol +40 -0
- package/{interfaces → contracts/interfaces}/IPolicyHolder.sol +0 -0
- package/contracts/interfaces/IPolicyPool.sol +145 -0
- package/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol +3 -0
- package/contracts/interfaces/IPremiumsAccount.sol +76 -0
- package/contracts/interfaces/IRiskModule.sol +123 -0
- package/contracts/mocks/FixedRateVault.sol +85 -0
- package/contracts/mocks/ForwardProxy.sol +66 -0
- package/contracts/mocks/IMintableERC20.sol +13 -0
- package/contracts/mocks/PolicyHolderMock.sol +89 -0
- package/contracts/mocks/PolicyPoolComponentMock.sol +17 -0
- package/contracts/mocks/PolicyPoolMock.sol +148 -0
- package/contracts/mocks/RiskModuleMock.sol +75 -0
- package/contracts/mocks/TestCurrency.sol +35 -0
- package/contracts/mocks/TestNFT.sol +23 -0
- package/js/test-utils.js +356 -0
- package/package.json +2 -2
- package/build/contracts/AaveAssetManager.sol/AaveAssetManager.json +0 -742
- package/build/contracts/Exchange.sol/Exchange.json +0 -591
- package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +0 -1112
- package/build/contracts/PolicyNFT.sol/PolicyNFT.json +0 -570
- package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +0 -686
- package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +0 -50
- package/build/contracts/extras/PriceRiskModule.sol/PriceRiskModule.json +0 -1055
- package/build/contracts/upgraded/IPolicyNFTv1.sol/IPolicyNFTv1.json +0 -322
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +0 -571
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +0 -602
- package/build/interfaces/IAssetManager.sol/IAssetManager.json +0 -31
- package/build/interfaces/IExchange.sol/IPriceOracle.json +0 -30
- package/build/interfaces/IPolicyNFT.sol/IPolicyNFT.json +0 -321
- package/contracts/AaveAssetManager.sol +0 -298
- package/contracts/BaseAssetManager.sol +0 -301
- package/contracts/DataTypes.sol +0 -287
- package/contracts/Exchange.sol +0 -189
- package/contracts/FlightDelayRiskModule.sol +0 -208
- package/contracts/PolicyNFT.sol +0 -96
- package/contracts/PolicyPoolConfig.sol +0 -247
- package/contracts/WadRayMath.sol +0 -135
- package/contracts/dependencies/ChainlinkClientUpgradeable.sol +0 -371
- package/contracts/extras/IPriceRiskModule.sol +0 -41
- package/contracts/extras/PriceRiskModule.sol +0 -243
- package/contracts/upgraded/IPolicyNFTv1.sol +0 -15
- package/contracts/upgraded/PolicyNFTv1.sol +0 -126
- package/interfaces/IAssetManager.sol +0 -23
- package/interfaces/IEToken.sol +0 -48
- package/interfaces/IExchange.sol +0 -59
- package/interfaces/IInsolvencyHook.sol +0 -27
- package/interfaces/ILPWhitelist.sol +0 -23
- package/interfaces/IPolicyNFT.sol +0 -15
- package/interfaces/IPolicyPool.sol +0 -44
- package/interfaces/IPolicyPoolConfig.sol +0 -87
- package/interfaces/IRiskModule.sol +0 -29
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
|
|
5
|
-
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
|
|
6
|
-
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
|
|
7
|
-
import {IAssetManager} from "../interfaces/IAssetManager.sol";
|
|
8
|
-
import {IInsolvencyHook} from "../interfaces/IInsolvencyHook.sol";
|
|
9
|
-
import {IPolicyPoolConfig} from "../interfaces/IPolicyPoolConfig.sol";
|
|
10
|
-
import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
|
|
11
|
-
import {IRiskModule} from "../interfaces/IRiskModule.sol";
|
|
12
|
-
import {ILPWhitelist} from "../interfaces/ILPWhitelist.sol";
|
|
13
|
-
import {IExchange} from "../interfaces/IExchange.sol";
|
|
14
|
-
import {IPolicyPoolComponent} from "../interfaces/IPolicyPoolComponent.sol";
|
|
15
|
-
import {WadRayMath} from "./WadRayMath.sol";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @title PolicyPoolConfig - Protocol access roles and other settings/components
|
|
19
|
-
* @dev Contract that holds the access roles for PolicyPool and other components of the protocol.
|
|
20
|
-
* @custom:security-contact security@ensuro.co
|
|
21
|
-
* @author Ensuro
|
|
22
|
-
*/
|
|
23
|
-
contract PolicyPoolConfig is
|
|
24
|
-
Initializable,
|
|
25
|
-
AccessControlUpgradeable,
|
|
26
|
-
UUPSUpgradeable,
|
|
27
|
-
IPolicyPoolConfig
|
|
28
|
-
{
|
|
29
|
-
using WadRayMath for uint256;
|
|
30
|
-
|
|
31
|
-
// Core governance roles
|
|
32
|
-
bytes32 public constant GUARDIAN_ROLE = keccak256("GUARDIAN_ROLE");
|
|
33
|
-
bytes32 public constant LEVEL1_ROLE = keccak256("LEVEL1_ROLE");
|
|
34
|
-
bytes32 public constant LEVEL2_ROLE = keccak256("LEVEL2_ROLE");
|
|
35
|
-
bytes32 public constant LEVEL3_ROLE = keccak256("LEVEL3_ROLE");
|
|
36
|
-
|
|
37
|
-
uint256 public constant L2_RM_LIMIT = 5e16; // 5% in WAD
|
|
38
|
-
|
|
39
|
-
address internal _treasury; // address of Ensuro treasury
|
|
40
|
-
IAssetManager internal _assetManager; // asset manager
|
|
41
|
-
IInsolvencyHook internal _insolvencyHook; // Contract that handles insolvency situations
|
|
42
|
-
IPolicyPool internal _policyPool;
|
|
43
|
-
ILPWhitelist internal _lpWhitelist; // Contract that handles whitelisting of Liquidity Providers
|
|
44
|
-
IExchange internal _exchange; // Contract that handles exchange operations between assets
|
|
45
|
-
|
|
46
|
-
mapping(IRiskModule => RiskModuleStatus) private _riskModules;
|
|
47
|
-
|
|
48
|
-
event ComponentChanged(IPolicyPoolConfig.GovernanceActions indexed action, address value);
|
|
49
|
-
|
|
50
|
-
modifier onlyRole2(bytes32 role1, bytes32 role2) {
|
|
51
|
-
if (!hasRole(role1, _msgSender())) _checkRole(role2, _msgSender());
|
|
52
|
-
_;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
modifier onlyRole3(
|
|
56
|
-
bytes32 role1,
|
|
57
|
-
bytes32 role2,
|
|
58
|
-
bytes32 role3
|
|
59
|
-
) {
|
|
60
|
-
if (!hasRole(role1, _msgSender()) && !hasRole(role2, _msgSender())) {
|
|
61
|
-
_checkRole(role3, _msgSender());
|
|
62
|
-
}
|
|
63
|
-
_;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function initialize(IPolicyPool policyPool_, address treasury_) public initializer {
|
|
67
|
-
__AccessControl_init();
|
|
68
|
-
__UUPSUpgradeable_init();
|
|
69
|
-
__PolicyPoolConfig_init_unchained(policyPool_, treasury_);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// solhint-disable-next-line func-name-mixedcase
|
|
73
|
-
function __PolicyPoolConfig_init_unchained(IPolicyPool policyPool_, address treasury_)
|
|
74
|
-
internal
|
|
75
|
-
initializer
|
|
76
|
-
{
|
|
77
|
-
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
|
|
78
|
-
_policyPool = policyPool_;
|
|
79
|
-
_treasury = treasury_;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @dev This function can be called only once in contract's lifetime. It links the PolicyPoolConfig with the
|
|
84
|
-
* PolicyPool contract. It's called in PolicyPool initialization.
|
|
85
|
-
*/
|
|
86
|
-
function connect() external override {
|
|
87
|
-
require(
|
|
88
|
-
address(_policyPool) == address(0) || address(_policyPool) == _msgSender(),
|
|
89
|
-
"PolicyPool already connected"
|
|
90
|
-
);
|
|
91
|
-
_policyPool = IPolicyPool(_msgSender());
|
|
92
|
-
// Not possible to do this validation because connect is called in _policyPool initialize :'(
|
|
93
|
-
// require(_policyPool.config() == this, "PolicyPool not connected to this config");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function policyPool() external view returns (IPolicyPool) {
|
|
97
|
-
return _policyPool;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// solhint-disable-next-line no-empty-blocks
|
|
101
|
-
function _authorizeUpgrade(address) internal override onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {}
|
|
102
|
-
|
|
103
|
-
function checkRole(bytes32 role, address account) external view override {
|
|
104
|
-
_checkRole(role, account);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function checkRole2(
|
|
108
|
-
bytes32 role1,
|
|
109
|
-
bytes32 role2,
|
|
110
|
-
address account
|
|
111
|
-
) external view override {
|
|
112
|
-
if (!hasRole(role1, account)) _checkRole(role2, account);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function setAssetManager(IAssetManager assetManager_) external onlyRole(LEVEL1_ROLE) {
|
|
116
|
-
require(
|
|
117
|
-
address(assetManager_) == address(0) ||
|
|
118
|
-
IPolicyPoolComponent(address(assetManager_)).policyPool() == _policyPool,
|
|
119
|
-
"Component not linked to this PolicyPool"
|
|
120
|
-
);
|
|
121
|
-
_policyPool.setAssetManager(assetManager_);
|
|
122
|
-
_assetManager = assetManager_;
|
|
123
|
-
emit ComponentChanged(GovernanceActions.setAssetManager, address(_assetManager));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function assetManager() external view virtual override returns (IAssetManager) {
|
|
127
|
-
return _assetManager;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function setTreasury(address treasury_) external onlyRole(LEVEL1_ROLE) {
|
|
131
|
-
_treasury = treasury_;
|
|
132
|
-
emit ComponentChanged(GovernanceActions.setTreasury, _treasury);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function treasury() external view override returns (address) {
|
|
136
|
-
return _treasury;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function setInsolvencyHook(IInsolvencyHook insolvencyHook_)
|
|
140
|
-
external
|
|
141
|
-
onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE)
|
|
142
|
-
{
|
|
143
|
-
require(
|
|
144
|
-
address(insolvencyHook_) == address(0) ||
|
|
145
|
-
IPolicyPoolComponent(address(insolvencyHook_)).policyPool() == _policyPool,
|
|
146
|
-
"Component not linked to this PolicyPool"
|
|
147
|
-
);
|
|
148
|
-
_insolvencyHook = insolvencyHook_;
|
|
149
|
-
emit ComponentChanged(GovernanceActions.setInsolvencyHook, address(_insolvencyHook));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function insolvencyHook() external view override returns (IInsolvencyHook) {
|
|
153
|
-
return _insolvencyHook;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function setLPWhitelist(ILPWhitelist lpWhitelist_)
|
|
157
|
-
external
|
|
158
|
-
onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE)
|
|
159
|
-
{
|
|
160
|
-
require(
|
|
161
|
-
address(lpWhitelist_) == address(0) ||
|
|
162
|
-
IPolicyPoolComponent(address(lpWhitelist_)).policyPool() == _policyPool,
|
|
163
|
-
"Component not linked to this PolicyPool"
|
|
164
|
-
);
|
|
165
|
-
_lpWhitelist = lpWhitelist_;
|
|
166
|
-
emit ComponentChanged(GovernanceActions.setLPWhitelist, address(_lpWhitelist));
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function lpWhitelist() external view override returns (ILPWhitelist) {
|
|
170
|
-
return _lpWhitelist;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function setExchange(IExchange exchange_) external onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {
|
|
174
|
-
require(
|
|
175
|
-
address(exchange_) == address(0) ||
|
|
176
|
-
IPolicyPoolComponent(address(exchange_)).policyPool() == _policyPool,
|
|
177
|
-
"Component not linked to this PolicyPool"
|
|
178
|
-
);
|
|
179
|
-
_exchange = exchange_;
|
|
180
|
-
emit ComponentChanged(GovernanceActions.setExchange, address(_exchange));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function exchange() external view override returns (IExchange) {
|
|
184
|
-
return _exchange;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function addRiskModule(IRiskModule riskModule) external onlyRole2(LEVEL1_ROLE, LEVEL2_ROLE) {
|
|
188
|
-
require(
|
|
189
|
-
_riskModules[riskModule] == RiskModuleStatus.inactive,
|
|
190
|
-
"Risk Module already in the pool"
|
|
191
|
-
);
|
|
192
|
-
require(address(riskModule) != address(0), "riskModule can't be zero");
|
|
193
|
-
require(
|
|
194
|
-
IPolicyPoolComponent(address(riskModule)).policyPool() == _policyPool,
|
|
195
|
-
"RiskModule not linked to this pool"
|
|
196
|
-
);
|
|
197
|
-
require(
|
|
198
|
-
hasRole(LEVEL1_ROLE, msg.sender) ||
|
|
199
|
-
_policyPool.totalETokenSupply() > (riskModule.scrLimit().wadMul(L2_RM_LIMIT)),
|
|
200
|
-
"RiskModule SCR Limit exceeds the limit for LEVEL2 user"
|
|
201
|
-
);
|
|
202
|
-
_riskModules[riskModule] = RiskModuleStatus.active;
|
|
203
|
-
emit RiskModuleStatusChanged(riskModule, RiskModuleStatus.active);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function removeRiskModule(IRiskModule riskModule) external onlyRole(LEVEL2_ROLE) {
|
|
207
|
-
require(_riskModules[riskModule] != RiskModuleStatus.inactive, "Risk Module not found");
|
|
208
|
-
require(riskModule.totalScr() == 0, "Can't remove a module with active policies");
|
|
209
|
-
delete _riskModules[riskModule];
|
|
210
|
-
emit RiskModuleStatusChanged(riskModule, RiskModuleStatus.inactive);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// #if_succeeds_disabled _riskModules.get(riskModule) == newStatus;
|
|
214
|
-
function changeRiskModuleStatus(IRiskModule riskModule, RiskModuleStatus newStatus)
|
|
215
|
-
external
|
|
216
|
-
onlyRole3(GUARDIAN_ROLE, LEVEL1_ROLE, LEVEL2_ROLE)
|
|
217
|
-
{
|
|
218
|
-
require(_riskModules[riskModule] != RiskModuleStatus.inactive, "Risk Module not found");
|
|
219
|
-
require(
|
|
220
|
-
newStatus != RiskModuleStatus.suspended || hasRole(GUARDIAN_ROLE, msg.sender),
|
|
221
|
-
"Only GUARDIAN can suspend modules"
|
|
222
|
-
);
|
|
223
|
-
// To activate LEVEL1 required or LEVEL2 if <5% of total liquidity
|
|
224
|
-
require(
|
|
225
|
-
newStatus != RiskModuleStatus.active ||
|
|
226
|
-
hasRole(LEVEL1_ROLE, msg.sender) ||
|
|
227
|
-
_policyPool.totalETokenSupply() > (riskModule.scrLimit().wadMul(L2_RM_LIMIT)),
|
|
228
|
-
"RiskModule SCR Limit exceeds the limit for LEVEL2 user"
|
|
229
|
-
);
|
|
230
|
-
// Anyone (LEVEL1, LEVEL2, GUARDIAN) can deprecate
|
|
231
|
-
_riskModules[riskModule] = newStatus;
|
|
232
|
-
emit RiskModuleStatusChanged(riskModule, newStatus);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function checkAcceptsNewPolicy(IRiskModule riskModule) external view override {
|
|
236
|
-
RiskModuleStatus rmStatus = _riskModules[riskModule];
|
|
237
|
-
require(rmStatus == RiskModuleStatus.active, "RM module not found or not active");
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function checkAcceptsResolvePolicy(IRiskModule riskModule) external view override {
|
|
241
|
-
RiskModuleStatus rmStatus = _riskModules[riskModule];
|
|
242
|
-
require(
|
|
243
|
-
rmStatus == RiskModuleStatus.active || rmStatus == RiskModuleStatus.deprecated,
|
|
244
|
-
"Module must be active or deprecated to process resolutions"
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
}
|
package/contracts/WadRayMath.sol
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @title WadRayMath library
|
|
6
|
-
* @author Aave / Ensuro
|
|
7
|
-
* @dev Provides mul and div function for wads (decimal numbers with 18 digits precision) and rays
|
|
8
|
-
* (decimals with 27 digits)
|
|
9
|
-
* @custom:security-contact security@ensuro.co
|
|
10
|
-
**/
|
|
11
|
-
|
|
12
|
-
library WadRayMath {
|
|
13
|
-
uint256 internal constant WAD = 1e18;
|
|
14
|
-
uint256 internal constant HALF_WAD = WAD / 2;
|
|
15
|
-
|
|
16
|
-
uint256 internal constant RAY = 1e27;
|
|
17
|
-
uint256 internal constant HALF_RAY = RAY / 2;
|
|
18
|
-
|
|
19
|
-
uint256 internal constant WAD_RAY_RATIO = 1e9;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @return One ray, 1e27
|
|
23
|
-
**/
|
|
24
|
-
function ray() internal pure returns (uint256) {
|
|
25
|
-
return RAY;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @return One wad, 1e18
|
|
30
|
-
**/
|
|
31
|
-
|
|
32
|
-
function wad() internal pure returns (uint256) {
|
|
33
|
-
return WAD;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @return Half ray, 1e27/2
|
|
38
|
-
**/
|
|
39
|
-
function halfRay() internal pure returns (uint256) {
|
|
40
|
-
return HALF_RAY;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @return Half ray, 1e18/2
|
|
45
|
-
**/
|
|
46
|
-
function halfWad() internal pure returns (uint256) {
|
|
47
|
-
return HALF_WAD;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @dev Multiplies two wad, rounding half up to the nearest wad
|
|
52
|
-
* @param a Wad
|
|
53
|
-
* @param b Wad
|
|
54
|
-
* @return The result of a*b, in wad
|
|
55
|
-
**/
|
|
56
|
-
function wadMul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
57
|
-
if (a == 0 || b == 0) {
|
|
58
|
-
return 0;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
require(a <= (type(uint256).max - HALF_WAD) / b, "wadMul: Math Multiplication Overflow");
|
|
62
|
-
|
|
63
|
-
return (a * b + HALF_WAD) / WAD;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @dev Divides two wad, rounding half up to the nearest wad
|
|
68
|
-
* @param a Wad
|
|
69
|
-
* @param b Wad
|
|
70
|
-
* @return The result of a/b, in wad
|
|
71
|
-
**/
|
|
72
|
-
function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
73
|
-
require(b != 0, "wadDiv: Division by zero");
|
|
74
|
-
uint256 halfB = b / 2;
|
|
75
|
-
|
|
76
|
-
require(a <= (type(uint256).max - halfB) / WAD, "wadDiv: Math Multiplication Overflow");
|
|
77
|
-
|
|
78
|
-
return (a * WAD + halfB) / b;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @dev Multiplies two ray, rounding half up to the nearest ray
|
|
83
|
-
* @param a Ray
|
|
84
|
-
* @param b Ray
|
|
85
|
-
* @return The result of a*b, in ray
|
|
86
|
-
**/
|
|
87
|
-
function rayMul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
88
|
-
if (a == 0 || b == 0) {
|
|
89
|
-
return 0;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
require(a <= (type(uint256).max - HALF_RAY) / b, "rayMul: Math Multiplication Overflow");
|
|
93
|
-
|
|
94
|
-
return (a * b + HALF_RAY) / RAY;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @dev Divides two ray, rounding half up to the nearest ray
|
|
99
|
-
* @param a Ray
|
|
100
|
-
* @param b Ray
|
|
101
|
-
* @return The result of a/b, in ray
|
|
102
|
-
**/
|
|
103
|
-
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
104
|
-
require(b != 0, "rayDiv: Division by zero");
|
|
105
|
-
uint256 halfB = b / 2;
|
|
106
|
-
|
|
107
|
-
require(a <= (type(uint256).max - halfB) / RAY, "rayDiv: Math Multiplication Overflow");
|
|
108
|
-
|
|
109
|
-
return (a * RAY + halfB) / b;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @dev Casts ray down to wad
|
|
114
|
-
* @param a Ray
|
|
115
|
-
* @return a casted to wad, rounded half up to the nearest wad
|
|
116
|
-
**/
|
|
117
|
-
function rayToWad(uint256 a) internal pure returns (uint256) {
|
|
118
|
-
uint256 halfRatio = WAD_RAY_RATIO / 2;
|
|
119
|
-
uint256 result = halfRatio + a;
|
|
120
|
-
require(result >= halfRatio, "rayToWad: Math Addition Overflow");
|
|
121
|
-
|
|
122
|
-
return result / WAD_RAY_RATIO;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @dev Converts wad up to ray
|
|
127
|
-
* @param a Wad
|
|
128
|
-
* @return a converted in ray
|
|
129
|
-
**/
|
|
130
|
-
function wadToRay(uint256 a) internal pure returns (uint256) {
|
|
131
|
-
uint256 result = a * WAD_RAY_RATIO;
|
|
132
|
-
require(result / WAD_RAY_RATIO == a, "wadToRad: Math Multiplication Overflow");
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
}
|