-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBonding.sol
357 lines (277 loc) · 10 KB
/
Bonding.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
pragma solidity >=0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "./interfaces/IDAO.sol";
import "./interfaces/IMiningService.sol";
import "./interfaces/IDexHandler.sol";
import "./interfaces/IMaltDataLab.sol";
import "./Permissions.sol";
struct UserState {
uint256 bonded;
// TODO calculate average bonded period for log calc Tue 03 Aug 2021 11:22:39 BST
uint256 bondedEpoch;
}
struct EpochState {
uint256 lastTotalBonded;
uint256 lastUpdateTime;
uint256 cumulativeTotalBonded;
}
/// @title LP Bonding
/// @author 0xScotch <scotch@malt.money>
/// @notice The contract which LP tokens are bonded to to make a user eligible for protocol rewards
contract Bonding is Initializable, Permissions {
ERC20 public malt;
ERC20 public rewardToken;
ERC20 public stakeToken;
IDAO public dao;
IMiningService public miningService;
IDexHandler public dexHandler;
IMaltDataLab public maltDataLab;
uint256 internal _globalBonded;
uint256 internal _currentEpoch;
address internal offering;
mapping(address => UserState) internal userState;
mapping(uint256 => EpochState) internal epochState;
event Bond(address indexed account, uint256 value);
event Unbond(address indexed account, uint256 value);
event UnbondAndBreak(address indexed account, uint256 amountLPToken, uint256 amountMalt, uint256 amountReward);
function initialize(
address _timelock,
address initialAdmin,
address _malt,
address _rewardToken,
address _stakeToken,
address _dao,
address _miningService,
address _offering,
address _dexHandler,
address _maltDataLab
) external initializer {
_adminSetup(_timelock);
_setupRole(ADMIN_ROLE, initialAdmin);
dao = IDAO(_dao);
offering = _offering;
stakeToken = ERC20(_stakeToken);
miningService = IMiningService(_miningService);
dexHandler = IDexHandler(_dexHandler);
malt = ERC20(_malt);
rewardToken = ERC20(_rewardToken);
maltDataLab = IMaltDataLab(_maltDataLab);
}
function bond(uint256 amount) external {
bondToAccount(msg.sender, amount);
}
function bondToAccount(address account, uint256 amount)
public
{
if (msg.sender != offering) {
_notSameBlock();
}
require(amount > 0, "Cannot bond 0");
miningService.onBond(account, amount);
_bond(account, amount);
}
function unbond(uint256 amount)
external
{
require(amount > 0, "Cannot unbond 0");
uint256 bondedBalance = balanceOfBonded(msg.sender);
require(bondedBalance > 0, "< bonded balance");
require(amount <= bondedBalance, "< bonded balance");
// Avoid leaving dust behind
if (amount.add(1e16) > bondedBalance) {
amount = bondedBalance;
}
miningService.onUnbond(msg.sender, amount);
_unbond(amount);
}
function unbondAndBreak(uint256 amount)
external
{
require(amount > 0, "Cannot unbond 0");
uint256 bondedBalance = balanceOfBonded(msg.sender);
require(bondedBalance > 0, "< bonded balance");
require(amount <= bondedBalance, "< bonded balance");
// Avoid leaving dust behind
if (amount.add(1e16) > bondedBalance) {
amount = bondedBalance;
}
miningService.onUnbond(msg.sender, amount);
_unbondAndBreak(amount);
}
/*
* PUBLIC VIEW FUNCTIONS
*/
function averageBondedValue(uint256 epoch) public view returns (uint256) {
EpochState storage state = epochState[epoch];
uint256 epochLength = dao.epochLength();
uint256 timeElapsed = epochLength;
uint256 epochStartTime = dao.getEpochStartTime(epoch);
uint256 diff;
uint256 lastUpdateTime = state.lastUpdateTime;
uint256 lastTotalBonded = state.lastTotalBonded;
if (lastUpdateTime == 0) {
lastUpdateTime = epochStartTime;
}
if (lastTotalBonded == 0) {
lastTotalBonded = _globalBonded;
}
if (block.timestamp < epochStartTime) {
return 0;
}
if (epochStartTime + epochLength <= lastUpdateTime) {
return maltDataLab.realValueOfLPToken((state.cumulativeTotalBonded) / epochLength);
}
if (epochStartTime + epochLength < block.timestamp) {
// The desired epoch is in the past
diff = (epochStartTime + epochLength) - lastUpdateTime;
} else {
diff = block.timestamp - lastUpdateTime;
timeElapsed = block.timestamp - epochStartTime;
}
if (timeElapsed == 0) {
// Only way timeElapsed should == 0 is when block.timestamp == epochStartTime
// Therefore just return the lastTotalBonded value
return maltDataLab.realValueOfLPToken(lastTotalBonded);
}
uint256 endValue = state.cumulativeTotalBonded + (lastTotalBonded.mul(diff));
return maltDataLab.realValueOfLPToken((endValue) / timeElapsed);
}
function totalBonded() public view returns (uint256) {
return _globalBonded;
}
function balanceOfBonded(address account) public view returns (uint256) {
return userState[account].bonded;
}
function bondedEpoch(address account) public view returns (uint256) {
return userState[account].bondedEpoch;
}
function epochData(uint256 epoch) public view returns(uint256, uint256, uint256) {
return (epochState[epoch].lastTotalBonded, epochState[epoch].lastUpdateTime, epochState[epoch].cumulativeTotalBonded);
}
/*
* INTERNAL VIEW FUNCTIONS
*/
function _balanceCheck() internal view {
require(stakeToken.balanceOf(address(this)) >= totalBonded(), "Balance inconsistency");
}
/*
* INTERNAL FUNCTIONS
*/
function _bond(address account, uint256 amount) internal {
stakeToken.safeTransferFrom(msg.sender, address(this), amount);
_addToBonded(account, amount);
_balanceCheck();
emit Bond(account, amount);
}
function _unbond(uint256 amountLPToken) internal notSameBlock {
_removeFromBonded(msg.sender, amountLPToken, "LP: Insufficient bonded balance");
stakeToken.safeTransfer(msg.sender, amountLPToken);
_balanceCheck();
emit Unbond(msg.sender, amountLPToken);
}
function _unbondAndBreak(uint256 amountLPToken) internal notSameBlock {
_removeFromBonded(msg.sender, amountLPToken, "LP: Insufficient bonded balance");
stakeToken.safeTransfer(address(dexHandler), amountLPToken);
(uint256 amountMalt, uint256 amountReward) = dexHandler.removeLiquidity();
malt.safeTransfer(msg.sender, amountMalt);
rewardToken.safeTransfer(msg.sender, amountReward);
_balanceCheck();
emit UnbondAndBreak(msg.sender, amountLPToken, amountMalt, amountReward);
}
function _addToBonded(address account, uint256 amount) internal {
userState[account].bonded = userState[account].bonded.add(amount);
_updateEpochState(_globalBonded.add(amount));
if (userState[account].bondedEpoch == 0) {
userState[account].bondedEpoch = dao.epoch();
}
}
function _removeFromBonded(address account, uint256 amount, string memory reason) internal {
userState[account].bonded = userState[account].bonded.sub(amount, reason);
_updateEpochState(_globalBonded.sub(amount, reason));
}
function _updateEpochState(uint256 newTotalBonded) internal {
EpochState storage state = epochState[_currentEpoch];
uint256 epoch = dao.epoch();
uint256 epochStartTime = dao.getEpochStartTime(_currentEpoch);
uint256 lastUpdateTime = state.lastUpdateTime;
uint256 lengthOfEpoch = dao.epochLength();
uint256 epochEndTime = epochStartTime + lengthOfEpoch;
if (lastUpdateTime == 0) {
lastUpdateTime = epochStartTime;
}
if (lastUpdateTime > epochEndTime) {
lastUpdateTime = epochEndTime;
}
if (epoch == _currentEpoch) {
// We are still in the same epoch. Just update
uint256 finalTime = block.timestamp;
if (block.timestamp > epochEndTime) {
// We are past the end of the epoch so cap to end of epoch
finalTime = epochEndTime;
}
uint256 diff = finalTime - lastUpdateTime;
if (diff > 0) {
state.cumulativeTotalBonded = state.cumulativeTotalBonded + (state.lastTotalBonded.mul(diff));
state.lastUpdateTime = finalTime;
state.lastTotalBonded = newTotalBonded;
}
} else {
// We have crossed at least 1 epoch boundary
// Won't underflow due to check on lastUpdateTime above
uint256 diff = epochEndTime - lastUpdateTime;
state.cumulativeTotalBonded = state.cumulativeTotalBonded + (state.lastTotalBonded.mul(diff));
state.lastUpdateTime = epochEndTime;
state.lastTotalBonded = _globalBonded;
for (uint256 i = _currentEpoch + 1; i <= epoch; i += 1) {
state = epochState[i];
epochStartTime = dao.getEpochStartTime(i);
epochEndTime = epochStartTime + lengthOfEpoch;
state.lastTotalBonded = _globalBonded;
if (epochEndTime < block.timestamp) {
// The desired epoch is in the past
diff = lengthOfEpoch;
state.lastUpdateTime = epochEndTime;
} else {
diff = block.timestamp - epochStartTime;
state.lastUpdateTime = block.timestamp;
}
state.cumulativeTotalBonded = state.lastTotalBonded.mul(diff);
}
state.lastTotalBonded = newTotalBonded;
_currentEpoch = epoch;
}
_globalBonded = newTotalBonded;
}
/*
* PRIVILEDGED FUNCTIONS
*/
function setMiningService(address _miningService)
public
onlyRole(ADMIN_ROLE, "Must have admin privs")
{
require(_miningService != address(0), "Cannot set 0 address");
miningService = IMiningService(_miningService);
}
function setDAO(address _dao)
public
onlyRole(ADMIN_ROLE, "Must have admin privs")
{
require(_dao != address(0), "Cannot set 0 address");
dao = IDAO(_dao);
}
function setDexHandler(address _dexHandler)
public
onlyRole(ADMIN_ROLE, "Must have admin privs")
{
require(_dexHandler != address(0), "Cannot set 0 address");
dexHandler = IDexHandler(_dexHandler);
}
function setCurrentEpoch(uint256 _epoch)
public
onlyRole(ADMIN_ROLE, "Must have admin privs")
{
_currentEpoch = _epoch;
}
}