-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrencyController.sol
394 lines (343 loc) · 14.1 KB
/
CurrencyController.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.19;
// dependencies
import {AggregatorV3Interface} from "../dependencies/chainlink/AggregatorV3Interface.sol";
import {EnumerableSet} from "../dependencies/openzeppelin/utils/structs/EnumerableSet.sol";
import {SafeCast} from "../dependencies/openzeppelin/utils/math/SafeCast.sol";
import {Multicall} from "../dependencies/openzeppelin/utils/Multicall.sol";
// interfaces
import {ICurrencyController} from "./interfaces/ICurrencyController.sol";
// libraries
import {Constants} from "./libraries/Constants.sol";
import {RoundingUint256} from "./libraries/math/RoundingUint256.sol";
import {RoundingInt256} from "./libraries/math/RoundingInt256.sol";
// utils
import {Ownable} from "./utils/Ownable.sol";
import {Proxyable} from "./utils/Proxyable.sol";
// storages
import {CurrencyControllerStorage as Storage, PriceFeed} from "./storages/CurrencyControllerStorage.sol";
/**
* @notice Implements managing of the supported currencies in the protocol.
*
* This contract stores the currency settings and links the currency to the external price feeds.
* It uses those settings and also has the functions to change prices to other currencies.
*/
contract CurrencyController is ICurrencyController, Ownable, Proxyable, Multicall {
using EnumerableSet for EnumerableSet.Bytes32Set;
using SafeCast for uint256;
using SafeCast for int256;
using RoundingUint256 for uint256;
using RoundingInt256 for int256;
uint256 public constant HEARTBEAT_BUFFER = 5 minutes;
/// @dev Used for decimals of the base currency
uint256 immutable BASE_CURRENCY_DECIMALS;
/**
* @notice Modifier to check if the currency is supported.
* @param _ccy Currency name in bytes32
*/
modifier onlySupportedCurrency(bytes32 _ccy) {
if (!currencyExists(_ccy)) revert InvalidCurrency();
_;
}
/**
* @notice Contract constructor function.
* @param _baseCurrencyDecimals The decimals of the base currency
*/
constructor(uint256 _baseCurrencyDecimals) {
BASE_CURRENCY_DECIMALS = _baseCurrencyDecimals;
}
/**
* @notice Initializes the contract.
* @dev Function is invoked by the proxy contract when the contract is added to the ProxyController.
* @param _owner The address of the contract owner
*/
function initialize(address _owner) public initializer onlyProxy {
_transferOwnership(_owner);
}
/**
* @notice Gets aggregated and cached decimals of the price feeds for the selected currency.
* @param _ccy Currency name in bytes32
*/
function getDecimals(bytes32 _ccy) external view override returns (uint8) {
return Storage.slot().decimalsCaches[_ccy];
}
/**
* @notice Gets all currencies.
* @return The array of the currency
*/
function getCurrencies() external view override returns (bytes32[] memory) {
return Storage.slot().currencies.values();
}
/**
* @notice Gets haircut ratio for the selected currency.
* Haircut is used in bilateral netting cross-calculation.
* @param _ccy Currency name in bytes32
*/
function getHaircut(bytes32 _ccy) external view override returns (uint256) {
return Storage.slot().haircuts[_ccy];
}
/**
* @notice Gets price feed for the selected currency.
* @param _ccy Currency name in bytes32
*/
function getPriceFeed(bytes32 _ccy) external view override returns (PriceFeed memory) {
return Storage.slot().priceFeeds[_ccy];
}
/**
* @notice Gets if the selected currency is supported.
* @param _ccy Currency name in bytes32
* @return The boolean if the selected currency is supported or not
*/
function currencyExists(bytes32 _ccy) public view override returns (bool) {
return Storage.slot().currencies.contains(_ccy);
}
/**
* @notice Adds new currency into the protocol and links with existing price feed.
* @param _ccy Currency name in bytes32k
* @param _decimals Currency decimals
* @param _haircut Remaining ratio after haircut
* @param _priceFeeds Array with the contract address of price feed
* @param _heartbeats Array with the heartbeat of price feed
*/
function addCurrency(
bytes32 _ccy,
uint8 _decimals,
uint256 _haircut,
address[] calldata _priceFeeds,
uint256[] calldata _heartbeats
) public override onlyOwner {
Storage.slot().currencies.add(_ccy);
_updateHaircut(_ccy, _haircut);
_updatePriceFeed(_ccy, _decimals, _priceFeeds, _heartbeats);
emit CurrencyAdded(_ccy, _haircut);
}
/**
* @notice Updates the flag indicating if the currency is supported in the protocol.
* @param _ccy Currency name in bytes32
*/
function removeCurrency(bytes32 _ccy) public override onlyOwner {
Storage.slot().currencies.remove(_ccy);
emit CurrencyRemoved(_ccy);
}
/**
* @notice Updates the haircut ratio for supported currency
* @param _ccy Currency name in bytes32
* @param _haircut Remaining ratio after haircut
*/
function updateHaircut(
bytes32 _ccy,
uint256 _haircut
) public override onlyOwner onlySupportedCurrency(_ccy) {
_updateHaircut(_ccy, _haircut);
}
/**
* @notice Update the price feed contract addresses.
* @param _ccy Currency name in bytes32
* @param _decimals Currency decimals
* @param _priceFeeds Array with the contract address of price feed
* @param _heartbeats Array with the heartbeat of price feed
*/
function updatePriceFeed(
bytes32 _ccy,
uint8 _decimals,
address[] calldata _priceFeeds,
uint256[] calldata _heartbeats
) public override onlyOwner onlySupportedCurrency(_ccy) {
_updatePriceFeed(_ccy, _decimals, _priceFeeds, _heartbeats);
}
/**
* @notice Gets the last price of the selected currency in the base currency.
* @param _ccy Currency name in bytes32
* @return price The last price
*/
function getLastPrice(bytes32 _ccy) public view override returns (int256 price) {
price = _getAggregatedLastPrice(_ccy);
PriceFeed memory priceFeeds = Storage.slot().priceFeeds[_ccy];
for (uint256 i; i < priceFeeds.instances.length - 1; i++) {
uint8 decimals = priceFeeds.instances[i].decimals();
price = price.div((10 ** decimals).toInt256());
}
}
/**
* @notice Gets the last price of the selected currency in the base currency.
* This price is returned in the aggregated decimals of all price feeds.
* @param _ccy Currency name in bytes32
* @return price The last price
*/
function getAggregatedLastPrice(bytes32 _ccy) public view override returns (int256 price) {
price = _getAggregatedLastPrice(_ccy);
}
/**
* @notice Gets the converted amount of currency.
* @param _fromCcy Currency to convert from
* @param _toCcy Currency to convert to
* @param _amount Amount to be converted
* @return amount The converted amount
*/
function convert(
bytes32 _fromCcy,
bytes32 _toCcy,
uint256 _amount
) external view override returns (uint256 amount) {
if (_fromCcy == _toCcy) return _amount;
if (_amount == 0) return 0;
int256 fromPrice = _getAggregatedLastPrice(_fromCcy);
int256 toPrice = _getAggregatedLastPrice(_toCcy);
amount = (_amount * uint256(fromPrice) * 10 ** Storage.slot().decimalsCaches[_toCcy]).div(
10 ** Storage.slot().decimalsCaches[_fromCcy] * uint256(toPrice)
);
}
/**
* @notice Gets the converted amounts of currency.
* @param _fromCcy Currency to convert from
* @param _toCcy Currency to convert to
* @param _amounts Amounts to be converted
* @return amounts The converted amounts
*/
function convert(
bytes32 _fromCcy,
bytes32 _toCcy,
uint256[] calldata _amounts
) external view override returns (uint256[] memory amounts) {
if (_fromCcy == _toCcy) return _amounts;
int256 fromPrice = _getAggregatedLastPrice(_fromCcy);
int256 toPrice = _getAggregatedLastPrice(_toCcy);
uint256 toDecimals = Storage.slot().decimalsCaches[_toCcy];
uint256 fromDecimals = Storage.slot().decimalsCaches[_fromCcy];
amounts = new uint256[](_amounts.length);
for (uint256 i; i < _amounts.length; i++) {
if (_amounts[i] == 0) continue;
amounts[i] = (_amounts[i] * uint256(fromPrice) * 10 ** toDecimals).div(
10 ** fromDecimals * uint256(toPrice)
);
}
}
/**
* @notice Gets the converted amount in the base currency.
* @param _ccy Currency that has to be converted to the base currency
* @param _amount Amount to be converted
* @return amount The converted amount
*/
function convertToBaseCurrency(
bytes32 _ccy,
uint256 _amount
) public view override returns (uint256 amount) {
if (_amount == 0) return 0;
amount = (_amount * _getAggregatedLastPrice(_ccy).toUint256()).div(
10 ** Storage.slot().decimalsCaches[_ccy]
);
}
/**
* @notice Gets the converted amount in the base currency.
* @param _ccy Currency that has to be converted to the base currency.
* @param _amount Amount to be converted
* @return amount The converted amount
*/
function convertToBaseCurrency(
bytes32 _ccy,
int256 _amount
) external view override returns (int256 amount) {
if (_amount == 0) return 0;
amount = (_amount * _getAggregatedLastPrice(_ccy)).div(
(10 ** Storage.slot().decimalsCaches[_ccy]).toInt256()
);
}
/**
* @notice Gets the converted amounts in the base currency.
* @param _ccy Currency that has to be converted to the base currency.
* @param _amounts Amounts to be converted
* @return amounts The converted amounts
*/
function convertToBaseCurrency(
bytes32 _ccy,
uint256[] calldata _amounts
) public view override returns (uint256[] memory amounts) {
amounts = new uint256[](_amounts.length);
uint256 price = _getAggregatedLastPrice(_ccy).toUint256();
uint256 decimals = Storage.slot().decimalsCaches[_ccy];
for (uint256 i; i < _amounts.length; i++) {
if (_amounts[i] == 0) continue;
amounts[i] = (_amounts[i] * price).div(10 ** decimals);
}
}
/**
* @notice Gets the converted amount to the selected currency from the base currency.
* @param _ccy Currency that has to be converted from the base currency.
* @param _amount Amount in the base currency to be converted
* @return amount The converted amount
*/
function convertFromBaseCurrency(
bytes32 _ccy,
uint256 _amount
) public view override returns (uint256 amount) {
if (_amount == 0) return 0;
amount = (_amount * 10 ** Storage.slot().decimalsCaches[_ccy]).div(
_getAggregatedLastPrice(_ccy).toUint256()
);
}
/**
* @notice Gets the converted amounts to the selected currency from the base currency.
* @param _ccy Currency that has to be converted to the base currency.
* @param _amounts Amounts in the base currency to be converted
* @return amounts The converted amounts
*/
function convertFromBaseCurrency(
bytes32 _ccy,
uint256[] calldata _amounts
) public view override returns (uint256[] memory amounts) {
amounts = new uint256[](_amounts.length);
uint256 price = _getAggregatedLastPrice(_ccy).toUint256();
uint256 decimals = Storage.slot().decimalsCaches[_ccy];
for (uint256 i; i < _amounts.length; i++) {
if (_amounts[i] == 0) continue;
amounts[i] = (_amounts[i] * 10 ** decimals).div(price);
}
}
function _getAggregatedLastPrice(bytes32 _ccy) internal view returns (int256 totalPrice) {
PriceFeed memory priceFeeds = Storage.slot().priceFeeds[_ccy];
totalPrice = 1;
for (uint256 i; i < priceFeeds.instances.length; i++) {
(, int256 price, , uint256 updatedAt, ) = priceFeeds.instances[i].latestRoundData();
if (
price <= 0 ||
updatedAt < block.timestamp - priceFeeds.heartbeats[i] - HEARTBEAT_BUFFER
) {
revert StalePriceFeed(
address(priceFeeds.instances[i]),
priceFeeds.heartbeats[i],
updatedAt,
block.timestamp
);
}
totalPrice = totalPrice * price;
}
}
function _updateHaircut(bytes32 _ccy, uint256 _haircut) internal {
if (_haircut > Constants.PCT_DIGIT) revert InvalidHaircut();
Storage.slot().haircuts[_ccy] = _haircut;
emit HaircutUpdated(_ccy, _haircut);
}
function _updatePriceFeed(
bytes32 _ccy,
uint8 _decimals,
address[] calldata _priceFeeds,
uint256[] calldata _heartbeats
) internal {
if (_priceFeeds.length == 0 || _priceFeeds.length != _heartbeats.length)
revert InvalidPriceFeedInputs();
AggregatorV3Interface[] memory priceFeeds = new AggregatorV3Interface[](_priceFeeds.length);
for (uint256 i; i < _priceFeeds.length; i++) {
priceFeeds[i] = AggregatorV3Interface(_priceFeeds[i]);
uint8 decimals = priceFeeds[i].decimals();
if (
decimals > 18 || (i == _priceFeeds.length - 1 && decimals != BASE_CURRENCY_DECIMALS)
) revert InvalidDecimals(_priceFeeds[i], decimals);
(, int256 price, , uint256 updatedAt, ) = priceFeeds[i].latestRoundData();
if (price <= 0 || updatedAt < block.timestamp - _heartbeats[i] - HEARTBEAT_BUFFER)
revert InvalidPriceFeed();
}
Storage.slot().priceFeeds[_ccy] = PriceFeed(priceFeeds, _heartbeats);
Storage.slot().decimalsCaches[_ccy] = _decimals;
emit PriceFeedUpdated(_ccy, _decimals, _priceFeeds);
}
}