-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaltDataLab.sol
285 lines (239 loc) · 8.97 KB
/
MaltDataLab.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
pragma solidity >=0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "./interfaces/IStabilizerNode.sol";
import "./interfaces/IMovingAverage.sol";
import "./interfaces/IDAO.sol";
import "./interfaces/IBurnMintableERC20.sol";
import "./interfaces/ILiquidityExtension.sol";
import "./libraries/UniswapV2Library.sol";
import "./libraries/SafeBurnMintableERC20.sol";
import "./Permissions.sol";
/// @title Malt Data Lab
/// @author 0xScotch <scotch@malt.money>
/// @notice The central source of all of Malt protocol's internal data needs
/// @dev Over time usage of MovingAverage will likely be replaced with more reliable oracles
contract MaltDataLab is Initializable, Permissions {
using SafeMath for uint256;
using SafeBurnMintableERC20 for IBurnMintableERC20;
bytes32 public constant UPDATER_ROLE = keccak256("UPDATER_ROLE");
IBurnMintableERC20 public rewardToken;
IBurnMintableERC20 public malt;
ILiquidityExtension public liquidityExtension;
IUniswapV2Pair public stakeToken;
IMovingAverage public reserveRatioMA;
IMovingAverage public maltPriceMA;
IMovingAverage public poolMaltReserveMA;
uint256 public priceTarget = 10**18; // $1
uint256 public reserveRatioLookback = 10 minutes;
uint256 public maltPriceLookback = 10 minutes;
uint256 public reserveLookback = 10 minutes;
event TrackMaltPrice(uint256 price);
event TrackPoolReserves(uint256 maltReserves, uint256 rewardReserves);
event TrackReserveRatio(uint256 rRatio, uint256 decimals);
function initialize(
address _timelock,
address initialAdmin,
address _malt,
address _rewardToken,
address _stakeToken,
uint256 _priceTarget,
address _liquidityExtension,
address _reserveRatioMA,
address _maltPriceMA,
address _poolMaltReserveMA,
address _updater
) external initializer {
_adminSetup(_timelock);
_setupRole(ADMIN_ROLE, initialAdmin);
_roleSetup(UPDATER_ROLE, _updater);
_roleSetup(UPDATER_ROLE, initialAdmin);
stakeToken = IUniswapV2Pair(_stakeToken);
malt = IBurnMintableERC20(_malt);
rewardToken = IBurnMintableERC20(_rewardToken);
priceTarget = _priceTarget;
liquidityExtension = ILiquidityExtension(_liquidityExtension);
reserveRatioMA = IMovingAverage(_reserveRatioMA);
maltPriceMA = IMovingAverage(_maltPriceMA);
poolMaltReserveMA = IMovingAverage(_poolMaltReserveMA);
}
function smoothedReserveRatio() public view returns (uint256) {
return reserveRatioMA.getValueWithLookback(reserveRatioLookback);
}
function smoothedMaltPrice() public view returns (uint256) {
return maltPriceMA.getValueWithLookback(maltPriceLookback);
}
function smoothedMaltInPool() public view returns (uint256) {
return poolMaltReserveMA.getValueWithLookback(reserveLookback);
}
function smoothedReserves() public view returns (uint256 maltReserves, uint256 collateralReserves) {
maltReserves = poolMaltReserveMA.getValueWithLookback(reserveLookback);
uint256 price = smoothedMaltPrice();
return (maltReserves, maltReserves.mul(price).div(priceTarget));
}
function reserveRatioAverage(uint256 _lookback) public view returns (uint256) {
return reserveRatioMA.getValueWithLookback(_lookback);
}
function maltPriceAverage(uint256 _lookback) public view returns (uint256) {
return maltPriceMA.getValueWithLookback(_lookback);
}
function maltInPoolAverage(uint256 _lookback) public view returns (uint256) {
return poolMaltReserveMA.getValueWithLookback(_lookback);
}
function realValueOfLPToken(uint256 amount) external view returns (uint256) {
uint256 maltPrice = smoothedMaltPrice();
(uint256 maltReserves, uint256 rewardReserves) = smoothedReserves();
if (maltReserves == 0) {
return 0;
}
uint256 totalLPSupply = stakeToken.totalSupply();
uint256 maltValue = amount.mul(maltReserves).div(totalLPSupply);
uint256 rewardValue = amount.mul(rewardReserves).div(totalLPSupply);
return rewardValue.add(maltValue.mul(maltPrice).div(priceTarget));
}
/*
* Public mutation methods
*/
function trackReserveRatio() public {
(uint256 reserveRatio, uint256 decimals) = liquidityExtension.reserveRatio();
reserveRatioMA.update(reserveRatio);
emit TrackReserveRatio(reserveRatio, decimals);
}
function trackMaltPrice()
external
onlyRole(UPDATER_ROLE, "Must have updater role")
{
(uint256 reserve0, uint256 reserve1,) = stakeToken.getReserves();
(address token0,) = UniswapV2Library.sortTokens(address(malt), address(rewardToken));
uint256 rewardDecimals = rewardToken.decimals();
if (token0 == address(rewardToken)) {
uint256 price = _normalizedPrice(reserve0, reserve1, rewardDecimals);
maltPriceMA.update(price);
emit TrackMaltPrice(price);
} else {
uint256 price = _normalizedPrice(reserve1, reserve0, rewardDecimals);
maltPriceMA.update(price);
emit TrackMaltPrice(price);
}
}
function trackPoolReserves()
external
onlyRole(UPDATER_ROLE, "Must have updater role")
{
(uint256 reserve0, uint256 reserve1,) = stakeToken.getReserves();
(address token0,) = UniswapV2Library.sortTokens(address(malt), address(rewardToken));
uint256 rewardDecimals = rewardToken.decimals();
if (token0 == address(rewardToken)) {
// Args are (maltReserve)
poolMaltReserveMA.update(reserve1);
emit TrackPoolReserves(reserve1, reserve0);
} else {
// Args are (maltReserve)
poolMaltReserveMA.update(reserve0);
emit TrackPoolReserves(reserve0, reserve1);
}
}
function trackPool()
external
onlyRole(UPDATER_ROLE, "Must have updater role")
{
(uint256 reserve0, uint256 reserve1,) = stakeToken.getReserves();
(address token0,) = UniswapV2Library.sortTokens(address(malt), address(rewardToken));
uint256 rewardDecimals = rewardToken.decimals();
if (token0 == address(rewardToken)) {
uint256 price = _normalizedPrice(reserve0, reserve1, rewardDecimals);
maltPriceMA.update(price);
// Args are (maltReserve)
poolMaltReserveMA.update(reserve1);
emit TrackMaltPrice(price);
emit TrackPoolReserves(reserve1, reserve0);
} else {
uint256 price = _normalizedPrice(reserve1, reserve0, rewardDecimals);
maltPriceMA.update(price);
// Args are (maltReserve)
poolMaltReserveMA.update(reserve0);
emit TrackMaltPrice(price);
emit TrackPoolReserves(reserve0, reserve1);
}
}
/*
* INTERNAL METHODS
*/
function _normalizedPrice(
uint256 numerator,
uint256 denominator,
uint256 decimals
) internal view returns(uint256 price) {
// Malt is 18 decimals
if (decimals > 18) {
uint256 diff = decimals - 18;
price = numerator.mul(10**decimals).div(denominator.mul(10**diff));
} else if (decimals < 18) {
uint256 diff = 18 - decimals;
price = (numerator.mul(10**diff)).mul(10**decimals).div(denominator);
} else {
price = numerator.mul(10**decimals).div(denominator);
}
}
/*
* PRIVILEDGED METHODS
*/
function setLiquidityExtension(address _liquidityExtension)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_liquidityExtension != address(0), "Must be a valid address");
liquidityExtension = ILiquidityExtension(_liquidityExtension);
}
function setPriceTarget(uint256 _price)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_price > 0, "Cannot have 0 price");
priceTarget = _price;
}
function setReserveRatioLookback(uint256 _lookback)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_lookback > 0, "Cannot have 0 lookback");
reserveRatioLookback = _lookback;
}
function setMaltPriceLookback(uint256 _lookback)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_lookback > 0, "Cannot have 0 lookback");
maltPriceLookback = _lookback;
}
function setReserveLookback(uint256 _lookback)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_lookback > 0, "Cannot have 0 lookback");
reserveLookback = _lookback;
}
function setReserveAverageContract(address _reserveRatioMA)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_reserveRatioMA != address(0), "Cannot use 0 address");
reserveRatioMA = IMovingAverage(_reserveRatioMA);
}
function setMaltPriceAverageContract(address _maltPriceMA)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_maltPriceMA != address(0), "Cannot use 0 address");
maltPriceMA = IMovingAverage(_maltPriceMA);
}
function setMaltReservesAverageContract(address _poolMaltReserveMA)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_poolMaltReserveMA != address(0), "Cannot use 0 address");
poolMaltReserveMA = IMovingAverage(_poolMaltReserveMA);
}
}