@ensuro/core 1.2.6 → 2.0.0-beta2
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 +49 -17
- package/build/contracts/EToken.sol/EToken.json +163 -173
- package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +112 -315
- package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +34 -2
- package/build/contracts/Policy.sol/Policy.json +2 -2
- package/build/contracts/PolicyNFT.sol/PolicyNFT.json +16 -3
- package/build/contracts/PolicyPool.sol/PolicyPool.json +105 -351
- package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +32 -0
- package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +77 -98
- package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +761 -0
- package/build/contracts/{BaseAssetManager.sol/BaseAssetManager.json → Reserve.sol/Reserve.json} +23 -191
- package/build/contracts/RiskModule.sol/RiskModule.json +94 -302
- package/build/contracts/TimeScaled.sol/TimeScaled.json +24 -0
- package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +142 -325
- package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +15 -2
- 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/{interfaces → contracts/interfaces}/IEToken.sol/IEToken.json +32 -97
- 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}/IPolicyNFT.sol/IPolicyNFT.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyPool.sol/IPolicyPool.json +121 -108
- package/build/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyPoolConfig.sol/IPolicyPoolConfig.json +20 -49
- package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +302 -0
- package/build/{interfaces → contracts/interfaces}/IRiskModule.sol/IRiskModule.json +58 -34
- 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/LinkTokenMock.sol/LinkTokenMock.json +370 -0
- package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +202 -0
- package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +35 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +603 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +92 -0
- package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +343 -0
- package/build/contracts/mocks/TestNFT.sol/TestNFT.json +388 -0
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +16 -3
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +16 -3
- package/contracts/EToken.sol +210 -267
- package/contracts/FlightDelayRiskModule.sol +30 -21
- package/contracts/LPManualWhitelist.sol +4 -4
- package/contracts/Policy.sol +48 -44
- package/contracts/PolicyNFT.sol +2 -2
- package/contracts/PolicyPool.sol +114 -467
- package/contracts/PolicyPoolComponent.sol +14 -3
- package/contracts/PolicyPoolConfig.sol +29 -91
- package/contracts/PremiumsAccount.sol +274 -0
- package/contracts/Reserve.sol +34 -0
- package/contracts/RiskModule.sol +216 -164
- package/contracts/TimeScaled.sol +134 -0
- package/contracts/TrustfulRiskModule.sol +23 -21
- package/contracts/dependencies/ERC1967Proxy.sol +19 -0
- package/contracts/dependencies/IERC20Metadata.sol +19 -0
- package/contracts/interfaces/IEToken.sol +172 -0
- package/{interfaces → contracts/interfaces}/ILPWhitelist.sol +0 -0
- package/{interfaces → contracts/interfaces}/IPolicyHolder.sol +0 -0
- package/{interfaces → contracts/interfaces}/IPolicyNFT.sol +0 -0
- package/contracts/interfaces/IPolicyPool.sol +138 -0
- package/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol +0 -0
- package/{interfaces → contracts/interfaces}/IPolicyPoolConfig.sol +31 -22
- package/contracts/interfaces/IPremiumsAccount.sol +71 -0
- package/contracts/interfaces/IRiskModule.sol +60 -0
- package/contracts/mocks/ForwardProxy.sol +66 -0
- package/contracts/mocks/IMintableERC20.sol +13 -0
- package/contracts/mocks/LinkTokenMock.sol +30 -0
- package/contracts/mocks/PolicyHolderMock.sol +68 -0
- package/contracts/mocks/PolicyPoolComponentMock.sol +17 -0
- package/contracts/mocks/PolicyPoolMock.sol +122 -0
- package/contracts/mocks/TestCurrency.sol +35 -0
- package/contracts/mocks/TestNFT.sol +23 -0
- package/contracts/upgraded/PolicyNFTv1.sol +1 -1
- package/js/test-utils.js +278 -0
- package/package.json +2 -2
- package/build/contracts/AaveAssetManager.sol/AaveAssetManager.json +0 -723
- package/build/contracts/DataTypes.sol/DataTypes.json +0 -10
- package/build/contracts/Exchange.sol/Exchange.json +0 -546
- package/build/contracts/extras/IPriceRiskModule.sol/IPriceRiskModule.json +0 -253
- package/build/contracts/extras/PriceRiskModule.sol/PriceRiskModule.json +0 -1036
- package/build/interfaces/IAssetManager.sol/IAssetManager.json +0 -31
- package/build/interfaces/IExchange.sol/IPriceOracle.json +0 -30
- package/contracts/AaveAssetManager.sol +0 -298
- package/contracts/BaseAssetManager.sol +0 -301
- package/contracts/DataTypes.sol +0 -287
- package/contracts/Exchange.sol +0 -173
- package/contracts/extras/IPriceRiskModule.sol +0 -41
- package/contracts/extras/PriceRiskModule.sol +0 -243
- 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/IPolicyPool.sol +0 -44
- package/interfaces/IRiskModule.sol +0 -29
package/contracts/DataTypes.sol
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
|
|
3
|
-
pragma solidity ^0.8.0;
|
|
4
|
-
|
|
5
|
-
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
|
|
6
|
-
import {IEToken} from "../interfaces/IEToken.sol";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @title DataTypes library
|
|
10
|
-
* @dev Library with two EnumerableSet:
|
|
11
|
-
* - ETokenToWadMap: enumerable mapping(EToken ==> amount)
|
|
12
|
-
* - ETokenStatusMap: enumerable mapping(EToken ==> ETokenStatus)
|
|
13
|
-
* @custom:security-contact security@ensuro.co
|
|
14
|
-
* @author Ensuro
|
|
15
|
-
*/
|
|
16
|
-
library DataTypes {
|
|
17
|
-
using EnumerableSet for EnumerableSet.AddressSet;
|
|
18
|
-
|
|
19
|
-
enum ETokenStatus {
|
|
20
|
-
inactive, // doesn't exists - All operations rejected
|
|
21
|
-
active, // deposit / withdraw / lockScr / unlockScr OK
|
|
22
|
-
deprecated, // withdraw OK, unlockScr OK, deposit rejected, no new policies
|
|
23
|
-
suspended // all operations temporarily rejected
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Copied from OpenZeppelin's EnumerableMap but with address as keys
|
|
27
|
-
// and reimplemented with interfaces as keys
|
|
28
|
-
// To implement this library for multiple types with as little code
|
|
29
|
-
// repetition as possible, we write it in terms of a generic Map type with
|
|
30
|
-
// address keys and uint256 values.
|
|
31
|
-
// The Map implementation uses private functions, and user-facing
|
|
32
|
-
// implementations (such as Uint256ToAddressMap) are just wrappers around
|
|
33
|
-
// the underlying Map.
|
|
34
|
-
|
|
35
|
-
struct Map {
|
|
36
|
-
// Storage of keys
|
|
37
|
-
EnumerableSet.AddressSet _keys;
|
|
38
|
-
mapping(address => uint256) _values;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
|
43
|
-
* key. O(1).
|
|
44
|
-
*
|
|
45
|
-
* Returns true if the key was added to the map, that is if it was not
|
|
46
|
-
* already present.
|
|
47
|
-
*/
|
|
48
|
-
function _set(
|
|
49
|
-
Map storage map,
|
|
50
|
-
address key,
|
|
51
|
-
uint256 value
|
|
52
|
-
) private returns (bool) {
|
|
53
|
-
map._values[key] = value;
|
|
54
|
-
return map._keys.add(key);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @dev Removes a key-value pair from a map. O(1).
|
|
59
|
-
*
|
|
60
|
-
* Returns true if the key was removed from the map, that is if it was present.
|
|
61
|
-
*/
|
|
62
|
-
function _remove(Map storage map, address key) private returns (bool) {
|
|
63
|
-
delete map._values[key];
|
|
64
|
-
return map._keys.remove(key);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @dev Returns true if the key is in the map. O(1).
|
|
69
|
-
*/
|
|
70
|
-
function _contains(Map storage map, address key) private view returns (bool) {
|
|
71
|
-
return map._keys.contains(key);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @dev Returns the number of key-value pairs in the map. O(1).
|
|
76
|
-
*/
|
|
77
|
-
function _length(Map storage map) private view returns (uint256) {
|
|
78
|
-
return map._keys.length();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @dev Returns the key-value pair stored at position `index` in the map. O(1).
|
|
83
|
-
*
|
|
84
|
-
* Note that there are no guarantees on the ordering of entries inside the
|
|
85
|
-
* array, and it may change when more entries are added or removed.
|
|
86
|
-
*
|
|
87
|
-
* Requirements:
|
|
88
|
-
*
|
|
89
|
-
* - `index` must be strictly less than {length}.
|
|
90
|
-
*/
|
|
91
|
-
function _at(Map storage map, uint256 index) private view returns (address, uint256) {
|
|
92
|
-
address key = map._keys.at(index);
|
|
93
|
-
return (key, map._values[key]);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @dev Tries to returns the value associated with `key`. O(1).
|
|
98
|
-
* Does not revert if `key` is not in the map.
|
|
99
|
-
*/
|
|
100
|
-
function _tryGet(Map storage map, address key) private view returns (bool, uint256) {
|
|
101
|
-
uint256 value = map._values[key];
|
|
102
|
-
if (value == 0) {
|
|
103
|
-
return (_contains(map, key), 0);
|
|
104
|
-
} else {
|
|
105
|
-
return (true, value);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @dev Returns the value associated with `key`. O(1).
|
|
111
|
-
*
|
|
112
|
-
* Requirements:
|
|
113
|
-
*
|
|
114
|
-
* - `key` must be in the map.
|
|
115
|
-
*/
|
|
116
|
-
function _get(Map storage map, address key) private view returns (uint256) {
|
|
117
|
-
uint256 value = map._values[key];
|
|
118
|
-
require(value != 0 || _contains(map, key), "EnumerableMap: nonexistent key");
|
|
119
|
-
return value;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// ETokenToWadMap
|
|
123
|
-
struct ETokenToWadMap {
|
|
124
|
-
Map _inner;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
|
129
|
-
* key. O(1).
|
|
130
|
-
*
|
|
131
|
-
* Returns true if the key was added to the map, that is if it was not
|
|
132
|
-
* already present.
|
|
133
|
-
*/
|
|
134
|
-
function set(
|
|
135
|
-
ETokenToWadMap storage map,
|
|
136
|
-
IEToken key,
|
|
137
|
-
uint256 value
|
|
138
|
-
) internal returns (bool) {
|
|
139
|
-
return _set(map._inner, address(key), value);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @dev Removes a value from a set. O(1).
|
|
144
|
-
*
|
|
145
|
-
* Returns true if the key was removed from the map, that is if it was present.
|
|
146
|
-
*/
|
|
147
|
-
function remove(ETokenToWadMap storage map, IEToken key) internal returns (bool) {
|
|
148
|
-
return _remove(map._inner, address(key));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @dev Returns true if the key is in the map. O(1).
|
|
153
|
-
*/
|
|
154
|
-
function contains(ETokenToWadMap storage map, IEToken key) internal view returns (bool) {
|
|
155
|
-
return _contains(map._inner, address(key));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* @dev Returns the number of elements in the map. O(1).
|
|
160
|
-
*/
|
|
161
|
-
function length(ETokenToWadMap storage map) internal view returns (uint256) {
|
|
162
|
-
return _length(map._inner);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* @dev Returns the element stored at position `index` in the set. O(1).
|
|
167
|
-
* Note that there are no guarantees on the ordering of values inside the
|
|
168
|
-
* array, and it may change when more values are added or removed.
|
|
169
|
-
*
|
|
170
|
-
* Requirements:
|
|
171
|
-
*
|
|
172
|
-
* - `index` must be strictly less than {length}.
|
|
173
|
-
*/
|
|
174
|
-
function at(ETokenToWadMap storage map, uint256 index) internal view returns (IEToken, uint256) {
|
|
175
|
-
(address key, uint256 value) = _at(map._inner, index);
|
|
176
|
-
return (IEToken(key), value);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* @dev Tries to returns the value associated with `key`. O(1).
|
|
181
|
-
* Does not revert if `key` is not in the map.
|
|
182
|
-
*
|
|
183
|
-
* _Available since v3.4._
|
|
184
|
-
*/
|
|
185
|
-
function tryGet(ETokenToWadMap storage map, IEToken key) internal view returns (bool, uint256) {
|
|
186
|
-
(bool success, uint256 value) = _tryGet(map._inner, address(key));
|
|
187
|
-
return (success, value);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* @dev Returns the value associated with `key`. O(1).
|
|
192
|
-
*
|
|
193
|
-
* Requirements:
|
|
194
|
-
*
|
|
195
|
-
* - `key` must be in the map.
|
|
196
|
-
*/
|
|
197
|
-
function get(ETokenToWadMap storage map, IEToken key) internal view returns (uint256) {
|
|
198
|
-
return uint256(_get(map._inner, address(key)));
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// ETokenStatusMap
|
|
202
|
-
struct ETokenStatusMap {
|
|
203
|
-
Map _inner;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
|
208
|
-
* key. O(1).
|
|
209
|
-
*
|
|
210
|
-
* Returns true if the key was added to the map, that is if it was not
|
|
211
|
-
* already present.
|
|
212
|
-
*/
|
|
213
|
-
function set(
|
|
214
|
-
ETokenStatusMap storage map,
|
|
215
|
-
IEToken key,
|
|
216
|
-
ETokenStatus value
|
|
217
|
-
) internal returns (bool) {
|
|
218
|
-
return _set(map._inner, address(key), uint256(value));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* @dev Removes a value from a set. O(1).
|
|
223
|
-
*
|
|
224
|
-
* Returns true if the key was removed from the map, that is if it was present.
|
|
225
|
-
*/
|
|
226
|
-
function remove(ETokenStatusMap storage map, IEToken key) internal returns (bool) {
|
|
227
|
-
return _remove(map._inner, address(key));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* @dev Returns true if the key is in the map. O(1).
|
|
232
|
-
*/
|
|
233
|
-
function contains(ETokenStatusMap storage map, IEToken key) internal view returns (bool) {
|
|
234
|
-
return _contains(map._inner, address(key));
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* @dev Returns the number of elements in the map. O(1).
|
|
239
|
-
*/
|
|
240
|
-
function length(ETokenStatusMap storage map) internal view returns (uint256) {
|
|
241
|
-
return _length(map._inner);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* @dev Returns the element stored at position `index` in the set. O(1).
|
|
246
|
-
* Note that there are no guarantees on the ordering of values inside the
|
|
247
|
-
* array, and it may change when more values are added or removed.
|
|
248
|
-
*
|
|
249
|
-
* Requirements:
|
|
250
|
-
*
|
|
251
|
-
* - `index` must be strictly less than {length}.
|
|
252
|
-
*/
|
|
253
|
-
function at(ETokenStatusMap storage map, uint256 index)
|
|
254
|
-
internal
|
|
255
|
-
view
|
|
256
|
-
returns (IEToken, ETokenStatus)
|
|
257
|
-
{
|
|
258
|
-
(address key, uint256 value) = _at(map._inner, index);
|
|
259
|
-
return (IEToken(key), ETokenStatus(value));
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* @dev Tries to returns the value associated with `key`. O(1).
|
|
264
|
-
* Does not revert if `key` is not in the map.
|
|
265
|
-
*
|
|
266
|
-
* _Available since v3.4._
|
|
267
|
-
*/
|
|
268
|
-
function tryGet(ETokenStatusMap storage map, IEToken key)
|
|
269
|
-
internal
|
|
270
|
-
view
|
|
271
|
-
returns (bool, ETokenStatus)
|
|
272
|
-
{
|
|
273
|
-
(bool success, uint256 value) = _tryGet(map._inner, address(key));
|
|
274
|
-
return (success, ETokenStatus(value));
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* @dev Returns the value associated with `key`. O(1).
|
|
279
|
-
*
|
|
280
|
-
* Requirements:
|
|
281
|
-
*
|
|
282
|
-
* - `key` must be in the map.
|
|
283
|
-
*/
|
|
284
|
-
function get(ETokenStatusMap storage map, IEToken key) internal view returns (ETokenStatus) {
|
|
285
|
-
return ETokenStatus(_get(map._inner, address(key)));
|
|
286
|
-
}
|
|
287
|
-
}
|
package/contracts/Exchange.sol
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
|
5
|
-
import {IUniswapV2Router02} from "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
|
|
6
|
-
import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
|
|
7
|
-
import {IPolicyPoolConfig} from "../interfaces/IPolicyPoolConfig.sol";
|
|
8
|
-
import {PolicyPoolComponent} from "./PolicyPoolComponent.sol";
|
|
9
|
-
import {IExchange, IPriceOracle} from "../interfaces/IExchange.sol";
|
|
10
|
-
import {WadRayMath} from "./WadRayMath.sol";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @title Exchange contract
|
|
14
|
-
* @dev Helper contract that handles exchange operations between assets
|
|
15
|
-
* @custom:security-contact security@ensuro.co
|
|
16
|
-
* @author Ensuro
|
|
17
|
-
*/
|
|
18
|
-
contract Exchange is IExchange, PolicyPoolComponent {
|
|
19
|
-
using WadRayMath for uint256;
|
|
20
|
-
|
|
21
|
-
IPriceOracle internal _oracle;
|
|
22
|
-
IUniswapV2Router02 internal _swapRouter; // We will use SushiSwap in Polygon
|
|
23
|
-
uint256 internal _maxSlippage; // Maximum slippage in WAD
|
|
24
|
-
|
|
25
|
-
/// @custom:oz-upgrades-unsafe-allow constructor
|
|
26
|
-
// solhint-disable-next-line no-empty-blocks
|
|
27
|
-
constructor(IPolicyPool policyPool_) PolicyPoolComponent(policyPool_) {}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @dev Initializes the RiskModule
|
|
31
|
-
* @param oracle_ Oracle that provides the price of the assets
|
|
32
|
-
* @param swapRouter_ Uniswap compatible exchange
|
|
33
|
-
* @param maxSlippage_ Maximum slippage admitted for exchange operations
|
|
34
|
-
*/
|
|
35
|
-
function initialize(
|
|
36
|
-
IPriceOracle oracle_,
|
|
37
|
-
IUniswapV2Router02 swapRouter_,
|
|
38
|
-
uint256 maxSlippage_
|
|
39
|
-
) public initializer {
|
|
40
|
-
__PolicyPoolComponent_init();
|
|
41
|
-
__Exchange_init(oracle_, swapRouter_, maxSlippage_);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// solhint-disable-next-line func-name-mixedcase
|
|
45
|
-
function __Exchange_init(
|
|
46
|
-
IPriceOracle oracle_,
|
|
47
|
-
IUniswapV2Router02 swapRouter_,
|
|
48
|
-
uint256 maxSlippage_
|
|
49
|
-
) internal initializer {
|
|
50
|
-
_oracle = oracle_;
|
|
51
|
-
_swapRouter = swapRouter_;
|
|
52
|
-
_maxSlippage = maxSlippage_;
|
|
53
|
-
_validateParameters();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function _validateParameters() internal view override {
|
|
57
|
-
require(_maxSlippage <= 1e17, "maxSlippage can't be more than 10%");
|
|
58
|
-
require(address(_oracle) != address(0), "I need a price oracle");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function convert(
|
|
62
|
-
address assetFrom,
|
|
63
|
-
address assetTo,
|
|
64
|
-
uint256 amount
|
|
65
|
-
) public view override returns (uint256) {
|
|
66
|
-
return amount.wadMul(getExchangeRate(assetFrom, assetTo));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getExchangeRate(address asset, address expressedInAsset)
|
|
70
|
-
public
|
|
71
|
-
view
|
|
72
|
-
override
|
|
73
|
-
returns (uint256)
|
|
74
|
-
{
|
|
75
|
-
uint256 exchangeRate = _oracle.getAssetPrice(asset).wadDiv(
|
|
76
|
-
_oracle.getAssetPrice(expressedInAsset)
|
|
77
|
-
);
|
|
78
|
-
uint8 decFrom = IERC20Metadata(asset).decimals();
|
|
79
|
-
uint8 decTo = IERC20Metadata(expressedInAsset).decimals();
|
|
80
|
-
if (decFrom > decTo) {
|
|
81
|
-
exchangeRate /= 10**(decFrom - decTo);
|
|
82
|
-
} else {
|
|
83
|
-
exchangeRate *= 10**(decTo - decFrom);
|
|
84
|
-
}
|
|
85
|
-
return exchangeRate;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function getAmountIn(
|
|
89
|
-
address assetIn,
|
|
90
|
-
address assetOut,
|
|
91
|
-
uint256 amountOut
|
|
92
|
-
) external view override returns (uint256) {
|
|
93
|
-
return _swapRouter.getAmountsIn(amountOut, _exchangePath(assetIn, assetOut))[0];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function getSwapRouter() external view override returns (address) {
|
|
97
|
-
return address(_swapRouter);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function getPriceOracle() external view override returns (IPriceOracle) {
|
|
101
|
-
return IPriceOracle(_oracle);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function _exchangePath(address assetIn, address assetOut)
|
|
105
|
-
internal
|
|
106
|
-
pure
|
|
107
|
-
returns (address[] memory)
|
|
108
|
-
{
|
|
109
|
-
address[] memory path = new address[](2);
|
|
110
|
-
path[0] = assetIn;
|
|
111
|
-
path[1] = assetOut;
|
|
112
|
-
return path;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function sell(
|
|
116
|
-
address assetIn,
|
|
117
|
-
address assetOut,
|
|
118
|
-
uint256 amountInExact,
|
|
119
|
-
address outAddr,
|
|
120
|
-
uint256 deadline
|
|
121
|
-
) external view override returns (bytes memory) {
|
|
122
|
-
uint256 amountOutMin = convert(assetIn, assetOut, amountInExact).wadMul(1e18 - _maxSlippage);
|
|
123
|
-
|
|
124
|
-
return
|
|
125
|
-
abi.encodeWithSignature(
|
|
126
|
-
"swapExactTokensForTokens(uint256,uint256,address[],address,uint256)",
|
|
127
|
-
amountInExact,
|
|
128
|
-
amountOutMin,
|
|
129
|
-
_exchangePath(assetIn, assetOut),
|
|
130
|
-
outAddr,
|
|
131
|
-
deadline
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function buy(
|
|
136
|
-
address assetIn,
|
|
137
|
-
address assetOut,
|
|
138
|
-
uint256 amountOutExact,
|
|
139
|
-
address outAddr,
|
|
140
|
-
uint256 deadline
|
|
141
|
-
) external view override returns (bytes memory) {
|
|
142
|
-
uint256 amountInMax = convert(assetOut, assetIn, amountOutExact).wadMul(1e18 + _maxSlippage);
|
|
143
|
-
|
|
144
|
-
return
|
|
145
|
-
abi.encodeWithSignature(
|
|
146
|
-
"swapTokensForExactTokens(uint256,uint256,address[],address,uint256)",
|
|
147
|
-
amountOutExact,
|
|
148
|
-
amountInMax,
|
|
149
|
-
_exchangePath(assetIn, assetOut),
|
|
150
|
-
outAddr,
|
|
151
|
-
deadline
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function decodeSwapOut(bytes memory responseData) external pure override returns (uint256) {
|
|
156
|
-
return abi.decode(responseData, (uint256[]))[1];
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function maxSlippage() external view override returns (uint256) {
|
|
160
|
-
return _maxSlippage;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function setMaxSlippage(uint256 newValue) external onlyPoolRole2(LEVEL2_ROLE, LEVEL3_ROLE) {
|
|
164
|
-
require(newValue <= 1e17, "maxSlippage can't be more than 10%");
|
|
165
|
-
bool tweak = !hasPoolRole(LEVEL2_ROLE);
|
|
166
|
-
require(
|
|
167
|
-
!tweak || _isTweakWad(_maxSlippage, newValue, 3e26),
|
|
168
|
-
"Tweak exceeded: maxSlippage tweaks only up to 30%"
|
|
169
|
-
);
|
|
170
|
-
_maxSlippage = newValue;
|
|
171
|
-
_parameterChanged(IPolicyPoolConfig.GovernanceActions.setMaxSlippage, newValue, tweak);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
import {IRiskModule} from "../../interfaces/IRiskModule.sol";
|
|
5
|
-
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @title IPriceRiskModule interface
|
|
9
|
-
* @dev Interface for price risk module
|
|
10
|
-
* @author Ensuro
|
|
11
|
-
*/
|
|
12
|
-
interface IPriceRiskModule is IRiskModule {
|
|
13
|
-
/**
|
|
14
|
-
* @dev Returns the premium and lossProb of the policy
|
|
15
|
-
* @param triggerPrice Price of the asset_ that will trigger the policy (expressed in _currency)
|
|
16
|
-
* @param lower If true -> triggers if the price is lower, If false -> triggers if the price is higher
|
|
17
|
-
* @param payout Expressed in policyPool.currency()
|
|
18
|
-
* @param expiration Expiration of the policy
|
|
19
|
-
* @return premium Premium that needs to be paid
|
|
20
|
-
* @return lossProb Probability of paying the maximum payout
|
|
21
|
-
*/
|
|
22
|
-
function pricePolicy(
|
|
23
|
-
uint256 triggerPrice,
|
|
24
|
-
bool lower,
|
|
25
|
-
uint256 payout,
|
|
26
|
-
uint40 expiration
|
|
27
|
-
) external view returns (uint256 premium, uint256 lossProb);
|
|
28
|
-
|
|
29
|
-
function newPolicy(
|
|
30
|
-
uint256 triggerPrice,
|
|
31
|
-
bool lower,
|
|
32
|
-
uint256 payout,
|
|
33
|
-
uint40 expiration
|
|
34
|
-
) external returns (uint256);
|
|
35
|
-
|
|
36
|
-
function triggerPolicy(uint256 policyId) external;
|
|
37
|
-
|
|
38
|
-
function referenceCurrency() external view returns (IERC20Metadata);
|
|
39
|
-
|
|
40
|
-
function asset() external view returns (IERC20Metadata);
|
|
41
|
-
}
|