-
Notifications
You must be signed in to change notification settings - Fork 4
/
LPStaking.sol
498 lines (383 loc) · 15.3 KB
/
LPStaking.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
/**
* Standard SafeMath, stripped down to just add/sub/mul/div
*/
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
}
/**
* BEP20 standard interface.
*/
interface IBEP20 {
function totalSupply() external view returns (uint256);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function name() external view returns (string memory);
function getOwner() external view returns (address);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address _owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/**
* Provides ownable & authorized contexts
*/
abstract contract BOGAuth {
address owner;
mapping (address => bool) private authorizations;
constructor(address _owner) {
owner = _owner;
authorizations[_owner] = true;
}
/**
* Function modifier to require caller to be contract owner
*/
modifier onlyOwner() {
require(isOwner(msg.sender)); _;
}
/**
* Function modifier to require caller to be authorized
*/
modifier authorized() {
require(isAuthorized(msg.sender)); _;
}
/**
* Authorize address. Any authorized address
*/
function authorize(address adr) public authorized {
authorizations[adr] = true;
emit Authorized(adr);
}
/**
* Remove address' authorization. Owner only
*/
function unauthorize(address adr) public onlyOwner {
authorizations[adr] = false;
emit Unauthorized(adr);
}
/**
* Check if address is owner
*/
function isOwner(address account) public view returns (bool) {
return account == owner;
}
/**
* Return address' authorization status
*/
function isAuthorized(address adr) public view returns (bool) {
return authorizations[adr];
}
/**
* Transfer ownership to new address. Caller must be owner.
*/
function transferOwnership(address payable adr) public onlyOwner {
owner = adr;
authorizations[adr] = true;
emit OwnershipTransferred(adr);
}
event OwnershipTransferred(address owner);
event Authorized(address adr);
event Unauthorized(address adr);
}
abstract contract BOGPausable is BOGAuth {
bool public paused;
modifier whenPaused() {
require(paused || isAuthorized(msg.sender)); _;
}
modifier notPaused() {
require(!paused || isAuthorized(msg.sender)); _;
}
function pause() external notPaused authorized {
paused = true;
emit Paused();
}
function unpause() public whenPaused authorized {
paused = false;
emit Unpaused();
}
event Paused();
event Unpaused();
}
abstract contract BOGFinalizable is BOGAuth {
bool public isFinalized;
modifier unfinalized() {
require(!isFinalized, "FINALIZED"); _;
}
modifier finalized() {
require(isFinalized, "!FINALIZED"); _;
}
function finalize() public authorized unfinalized {
isFinalized = true;
emit Finalized();
}
event Finalized();
}
interface IBOGMigrationData {
function totalHolders() external view returns (uint256);
function totalBalances() external view returns (uint256);
function totalStakes() external view returns (uint256);
function getInfo(address holder) external view returns (bool migrated, uint256 stake, uint256 balance, bool preExploitHolder);
function hasBeenMigrated(address holder) external view returns (bool);
function getBalance(address holder) external view returns (uint256);
function getStake(address holder) external view returns (uint256);
function isPreExploitHolder(address holder) external view returns (bool);
}
interface IBOGWhitelister {
function isWhitelisted(address adr) external view returns (bool);
}
contract BOGWhitelistPassthrough is IBOGWhitelister {
IBOGMigrationData migration;
constructor (address _migration) {
migration = IBOGMigrationData(_migration);
}
function isWhitelisted(address adr) external view override returns (bool) {
return migration.isPreExploitHolder(adr);
}
}
abstract contract BOGWhitelisted is BOGAuth {
IBOGWhitelister whitelister;
bool public whitelistEnabled;
constructor (bool _enabled, address _whitelister) {
whitelistEnabled = _enabled;
whitelister = IBOGWhitelister(_whitelister);
}
function isWhitelisted(address adr) public view returns (bool) {
return whitelister.isWhitelisted(adr);
}
modifier whitelisted(address adr) {
require(!whitelistEnabled || isWhitelisted(adr), "!WHITELISTED"); _;
}
function migrateWhitelister(address _whitelister) external authorized {
whitelister = IBOGWhitelister(_whitelister);
}
function setWhitelistEnabled(bool _enabled) external authorized {
whitelistEnabled = _enabled;
}
}
interface IBOGStaking {
function stakingToken() external view returns (address);
function rewardToken() external view returns (address);
function totalStaked() external view returns (uint256);
function totalRealised() external view returns (uint256);
function getTotalRewards() external view returns (uint256);
function getCumulativeRewardsPerLP() external view returns (uint256);
function getLastContractBalance() external view returns (uint256);
function getAccuracyFactor() external view returns (uint256);
function getStake(address staker) external view returns (uint256);
function getRealisedEarnings(address staker) external returns (uint256);
function getUnrealisedEarnings(address staker) external view returns (uint256);
function stake(uint256 amount) external;
function stakeFor(address staker, uint256 amount) external;
function stakeAll() external;
function unstake(uint256 amount) external;
function unstakeAll() external;
function realise() external;
event Realised(address account, uint amount);
event Staked(address account, uint amount);
event Unstaked(address account, uint amount);
}
contract BOGStaking is BOGAuth, BOGPausable, BOGFinalizable, BOGWhitelisted, IBOGStaking {
using SafeMath for uint256;
struct Stake {
uint256 amount;
uint256 totalExcluded;
uint256 totalRealised;
}
address public override stakingToken;
address public override rewardToken;
uint256 public override totalRealised;
uint256 public override totalStaked;
mapping (address => Stake) public stakes;
constructor (address _stakingToken, address _rewardToken, bool _whitelisted, address _whitelister, bool _final) BOGAuth(msg.sender) BOGWhitelisted(_whitelisted, _whitelister) {
stakingToken = _stakingToken;
rewardToken = _rewardToken;
if(_final){ finalize(); }
}
uint256 _accuracyFactor = 10 ** 36;
uint256 _rewardsPerLP;
uint256 _lastContractBalance;
/**
* Total rewards realised and to be realised
*/
function getTotalRewards() external override view returns (uint256) {
return totalRealised + IBEP20(rewardToken).balanceOf(address(this));
}
/**
* Total rewards per LP cumulatively, inflated by _accuracyFactor
*/
function getCumulativeRewardsPerLP() external override view returns (uint256) {
return _rewardsPerLP;
}
/**
* The last balance the contract had
*/
function getLastContractBalance() external override view returns (uint256) {
return _lastContractBalance;
}
/**
* Total amount of transaction fees sent or to be sent to stakers
*/
function getAccuracyFactor() external override view returns (uint256) {
return _accuracyFactor;
}
/**
* Returns amount of LP that address has staked
*/
function getStake(address account) public override view returns (uint256) {
return stakes[account].amount;
}
/**
* Returns total earnings (realised + unrealised)
*/
function getRealisedEarnings(address staker) external view override returns (uint256) {
return stakes[staker].totalRealised; // realised gains plus outstanding earnings
}
/**
* Returns unrealised earnings
*/
function getUnrealisedEarnings(address staker) external view override returns (uint256) {
if(stakes[staker].amount == 0){ return 0; }
uint256 stakerTotalRewards = stakes[staker].amount.mul(getCurrentRewardsPerLP()).div(_accuracyFactor);
uint256 stakerTotalExcluded = stakes[staker].totalExcluded;
if(stakerTotalRewards <= stakerTotalExcluded){ return 0; }
return stakerTotalRewards.sub(stakerTotalExcluded);
}
function getCumulativeRewards(uint256 amount) public view returns (uint256) {
return amount.mul(_rewardsPerLP).div(_accuracyFactor);
}
function stake(uint amount) external override {
require(amount > 0);
IBEP20(stakingToken).transferFrom(msg.sender, address(this), amount);
_stake(msg.sender, amount);
}
function stakeFor(address staker, uint256 amount) external override {
require(amount > 0);
IBEP20(stakingToken).transferFrom(msg.sender, address(this), amount);
_stake(staker, amount);
}
function stakeAll() external override {
uint256 amount = IBEP20(stakingToken).balanceOf(msg.sender);
require(amount > 0);
IBEP20(stakingToken).transferFrom(msg.sender, address(this), amount);
_stake(msg.sender, amount);
}
function unstake(uint amount) external override {
require(amount > 0);
_unstake(msg.sender, amount);
}
function unstakeAll() external override {
uint256 amount = getStake(msg.sender);
require(amount > 0);
_unstake(msg.sender, amount);
}
function realise() external override notPaused {
_realise(msg.sender);
}
function _realise(address staker) internal {
_updateRewards();
uint amount = earnt(staker);
if (getStake(staker) == 0 || amount == 0) {
return;
}
stakes[staker].totalRealised = stakes[staker].totalRealised.add(amount);
stakes[staker].totalExcluded = stakes[staker].totalExcluded.add(amount);
totalRealised = totalRealised.add(amount);
IBEP20(rewardToken).transfer(staker, amount);
_updateRewards();
emit Realised(staker, amount);
}
function earnt(address staker) internal view returns (uint256) {
if(stakes[staker].amount == 0){ return 0; }
uint256 stakerTotalRewards = getCumulativeRewards(stakes[staker].amount);
uint256 stakerTotalExcluded = stakes[staker].totalExcluded;
if(stakerTotalRewards <= stakerTotalExcluded){ return 0; }
return stakerTotalRewards.sub(stakerTotalExcluded);
}
function _stake(address staker, uint256 amount) internal notPaused whitelisted(msg.sender) {
require(amount > 0);
_realise(staker);
// add to current address' stake
stakes[staker].amount = stakes[staker].amount.add(amount);
stakes[staker].totalExcluded = getCumulativeRewards(stakes[staker].amount);
totalStaked = totalStaked.add(amount);
emit Staked(staker, amount);
}
function _unstake(address staker, uint256 amount) internal notPaused {
require(stakes[staker].amount >= amount, "Insufficient Stake");
_realise(staker); // realise staking gains
// remove stake
stakes[staker].amount = stakes[staker].amount.sub(amount);
stakes[staker].totalExcluded = getCumulativeRewards(stakes[staker].amount);
totalStaked = totalStaked.sub(amount);
IBEP20(stakingToken).transfer(staker, amount);
emit Unstaked(staker, amount);
}
function _updateRewards() internal {
uint tokenBalance = IBEP20(rewardToken).balanceOf(address(this));
if(tokenBalance > _lastContractBalance && totalStaked != 0) {
uint256 newRewards = tokenBalance.sub(_lastContractBalance);
uint256 additionalAmountPerLP = newRewards.mul(_accuracyFactor).div(totalStaked);
_rewardsPerLP = _rewardsPerLP.add(additionalAmountPerLP);
}
if(totalStaked > 0){ _lastContractBalance = tokenBalance; }
}
function getCurrentRewardsPerLP() public view returns (uint256 currentRewardsPerLP) {
uint tokenBalance = IBEP20(rewardToken).balanceOf(address(this));
if(tokenBalance > _lastContractBalance && totalStaked != 0){
uint256 newRewards = tokenBalance.sub(_lastContractBalance);
uint256 additionalAmountPerLP = newRewards.mul(_accuracyFactor).div(totalStaked);
currentRewardsPerLP = _rewardsPerLP.add(additionalAmountPerLP);
}
}
function setAccuracyFactor(uint256 newFactor) external authorized {
_rewardsPerLP = _rewardsPerLP.mul(newFactor).div(_accuracyFactor); // switch _rewardsPerLP to be inflated by the new factor instead
_accuracyFactor = newFactor;
}
function emergencyUnstakeAll() external {
require(stakes[msg.sender].amount > 0, "No Stake");
IBEP20(stakingToken).transfer(msg.sender, stakes[msg.sender].amount);
totalStaked = totalStaked.sub(stakes[msg.sender].amount);
stakes[msg.sender].amount = 0;
}
function migrateStakingToken(address newToken) external authorized unfinalized {
IBEP20(newToken).transferFrom(msg.sender, address(this), totalStaked);
assert(IBEP20(newToken).balanceOf(address(this)) == totalStaked);
IBEP20(stakingToken).transfer(msg.sender, totalStaked);
stakingToken = newToken;
finalize();
}
}