This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SophonFarming.sol
934 lines (805 loc) · 30 KB
/
SophonFarming.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.25;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "./interfaces/IWeth.sol";
import "./interfaces/IstETH.sol";
import "./interfaces/IwstETH.sol";
import "./interfaces/IsDAI.sol";
import "./interfaces/IeETHLiquidityPool.sol";
import "./interfaces/IweETH.sol";
import "../proxies/Upgradeable2Step.sol";
import "./SophonFarmingState.sol";
/**
* @title Sophon Farming Contract
* @author Sophon
*/
contract SophonFarming is Upgradeable2Step, SophonFarmingState {
using SafeERC20 for IERC20;
/// @notice Emitted when a new pool is added
event Add(address indexed lpToken, uint256 indexed pid, uint256 allocPoint);
/// @notice Emitted when a pool is updated
event Set(address indexed lpToken, uint256 indexed pid, uint256 allocPoint);
/// @notice Emitted when a user deposits to a pool
event Deposit(address indexed user, uint256 indexed pid, uint256 depositAmount, uint256 boostAmount);
/// @notice Emitted when a user withdraws from a pool
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
/// @notice Emitted when a user increases the boost of an existing deposit
event IncreaseBoost(address indexed user, uint256 indexed pid, uint256 boostAmount);
/// @notice Emitted when all pool funds are bridged to Sophon blockchain
event Bridge(address indexed user, uint256 indexed pid, uint256 amount);
/// @notice Emitted when the admin withdraws booster proceeds
event WithdrawProceeds(uint256 indexed pid, uint256 proceeds);
error PoolExists();
error PoolDoesNotExist();
error AlreadyInitialized();
error NotFound(address lpToken);
error FarmingIsStarted();
error FarmingIsEnded();
error InvalidStartBlock();
error InvalidEndBlock();
error InvalidDeposit();
error InvalidBooster();
error WithdrawNotAllowed();
error WithdrawTooHigh(uint256 maxAllowed);
error WithdrawIsZero();
error NothingInPool();
error NoEthSent();
error BoostTooHigh(uint256 maxAllowed);
error BoostIsZero();
error BridgeInvalid();
address public immutable dai;
address public immutable sDAI;
address public immutable weth;
address public immutable stETH;
address public immutable wstETH;
address public immutable eETH;
address public immutable eETHLiquidityPool;
address public immutable weETH;
/**
* @notice Construct SophonFarming
* @param tokens_ Immutable token addresses
* @dev 0:dai, 1:sDAI, 2:weth, 3:stETH, 4:wstETH, 5:eETH, 6:eETHLiquidityPool, 7:weETH
*/
constructor(address[8] memory tokens_) {
dai = tokens_[0];
sDAI = tokens_[1];
weth = tokens_[2];
stETH = tokens_[3];
wstETH = tokens_[4];
eETH = tokens_[5];
eETHLiquidityPool = tokens_[6];
weETH = tokens_[7];
}
/**
* @notice Allows direct deposits of ETH for deposit to the wstETH pool
*/
receive() external payable {
if (msg.sender == weth) {
return;
}
depositEth(0, PredefinedPool.wstETH);
}
/**
* @notice Initialize the farm
* @param ethAllocPoint_ eth alloc points
* @param sDAIAllocPoint_ sdai alloc points
* @param _pointsPerBlock points per block
* @param _startBlock start block
* @param _boosterMultiplier booster multiplier
*/
function initialize(uint256 ethAllocPoint_, uint256 sDAIAllocPoint_, uint256 _pointsPerBlock, uint256 _startBlock, uint256 _boosterMultiplier) public virtual onlyOwner {
if (_initialized) {
revert AlreadyInitialized();
}
pointsPerBlock = _pointsPerBlock;
if (_startBlock == 0) {
revert InvalidStartBlock();
}
startBlock = _startBlock;
if (_boosterMultiplier < 1e18) {
revert InvalidBooster();
}
boosterMultiplier = _boosterMultiplier;
poolExists[dai] = true;
poolExists[weth] = true;
poolExists[stETH] = true;
poolExists[eETH] = true;
// sDAI
typeToId[PredefinedPool.sDAI] = add(sDAIAllocPoint_, sDAI, "sDAI", false);
IERC20(dai).approve(sDAI, 2**256-1);
// wstETH
typeToId[PredefinedPool.wstETH] = add(ethAllocPoint_, wstETH, "wstETH", false);
IERC20(stETH).approve(wstETH, 2**256-1);
// weETH
typeToId[PredefinedPool.weETH] = add(ethAllocPoint_, weETH, "weETH", false);
IERC20(eETH).approve(weETH, 2**256-1);
_initialized = true;
}
/**
* @notice Adds a new pool to the farm. Can only be called by the owner.
* @param _allocPoint alloc point for new pool
* @param _lpToken lpToken address
* @param _description description of new pool
* @param _withUpdate True will update accounting for all pools
* @return uint256 The pid of the newly created asset
*/
function add(uint256 _allocPoint, address _lpToken, string memory _description, bool _withUpdate) public onlyOwner returns (uint256) {
if (poolExists[_lpToken]) {
revert PoolExists();
}
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
if (_withUpdate) {
massUpdatePools();
}
uint256 lastRewardBlock =
getBlockNumber() > startBlock ? getBlockNumber() : startBlock;
totalAllocPoint = totalAllocPoint + _allocPoint;
poolExists[_lpToken] = true;
uint256 pid = poolInfo.length;
poolInfo.push(
PoolInfo({
lpToken: IERC20(_lpToken),
l2Farm: address(0),
amount: 0,
boostAmount: 0,
depositAmount: 0,
allocPoint: _allocPoint,
lastRewardBlock: lastRewardBlock,
accPointsPerShare: 0,
description: _description
})
);
emit Add(_lpToken, pid, _allocPoint);
return pid;
}
/**
* @notice Updates the given pool's allocation point. Can only be called by the owner.
* @param _pid The pid to update
* @param _allocPoint The new alloc point to set for the pool
* @param _withUpdate True will update accounting for all pools
*/
function set(uint256 _pid, uint256 _allocPoint, bool _withUpdate) public onlyOwner {
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
if (_withUpdate) {
massUpdatePools();
}
PoolInfo storage pool = poolInfo[_pid];
address lpToken = address(pool.lpToken);
if (lpToken == address(0) || !poolExists[lpToken]) {
revert PoolDoesNotExist();
}
totalAllocPoint = totalAllocPoint - pool.allocPoint + _allocPoint;
pool.allocPoint = _allocPoint;
if (getBlockNumber() < pool.lastRewardBlock) {
pool.lastRewardBlock = startBlock;
}
emit Set(lpToken, _pid, _allocPoint);
}
/**
* @notice Returns the number of pools in the farm
* @return uint256 number of pools
*/
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
/**
* @notice Checks if farming is ended
* @return bool True if farming is ended
*/
function isFarmingEnded() public view returns (bool) {
uint256 _endBlock = endBlock;
if (_endBlock != 0 && getBlockNumber() > _endBlock) {
return true;
} else {
return false;
}
}
/**
* @notice Checks if the withdrawal period is ended
* @return bool True if withdrawal period is ended
*/
function isWithdrawPeriodEnded() public view returns (bool) {
uint256 _endBlockForWithdrawals = endBlockForWithdrawals;
if (_endBlockForWithdrawals != 0 && getBlockNumber() > _endBlockForWithdrawals) {
return true;
} else {
return false;
}
}
/**
* @notice Updates the bridge contract
*/
function setBridge(BridgeLike _bridge) public onlyOwner {
bridge = _bridge;
}
/**
* @notice Updates the L2 Farm for the pool
* @param _pid the pid
* @param _l2Farm the l2Farm address
*/
function setL2FarmForPool(uint256 _pid, address _l2Farm) public onlyOwner {
poolInfo[_pid].l2Farm = _l2Farm;
}
/**
* @notice Set the start block of the farm
* @param _startBlock the start block
*/
function setStartBlock(uint256 _startBlock) public onlyOwner {
if (_startBlock == 0 || (endBlock != 0 && _startBlock >= endBlock)) {
revert InvalidStartBlock();
}
if (getBlockNumber() > startBlock) {
revert FarmingIsStarted();
}
startBlock = _startBlock;
}
/**
* @notice Set the end block of the farm
* @param _endBlock the end block
* @param _withdrawalBlocks the last block that withdrawals are allowed
*/
function setEndBlock(uint256 _endBlock, uint256 _withdrawalBlocks) public onlyOwner {
uint256 _endBlockForWithdrawals;
if (_endBlock != 0) {
if (_endBlock <= startBlock || getBlockNumber() > _endBlock) {
revert InvalidEndBlock();
}
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
_endBlockForWithdrawals = _endBlock + _withdrawalBlocks;
} else {
// withdrawal blocks needs an endBlock
_endBlockForWithdrawals = 0;
}
massUpdatePools();
endBlock = _endBlock;
endBlockForWithdrawals = _endBlockForWithdrawals;
}
/**
* @notice Set points per block
* @param _pointsPerBlock points per block to set
*/
function setPointsPerBlock(uint256 _pointsPerBlock) public onlyOwner {
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
massUpdatePools();
pointsPerBlock = _pointsPerBlock;
}
/**
* @notice Set booster multiplier
* @param _boosterMultiplier booster multiplier to set
*/
function setBoosterMultiplier(uint256 _boosterMultiplier) public onlyOwner {
if (_boosterMultiplier < 1e18) {
revert InvalidBooster();
}
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
massUpdatePools();
boosterMultiplier = _boosterMultiplier;
}
/**
* @notice Returns the block multiplier
* @param _from from block
* @param _to to block
* @return uint256 The block multiplier
*/
function _getBlockMultiplier(uint256 _from, uint256 _to) internal view returns (uint256) {
uint256 _endBlock = endBlock;
if (_endBlock != 0) {
_to = Math.min(_to, _endBlock);
}
if (_to > _from) {
return (_to - _from) * 1e18;
} else {
return 0;
}
}
/**
* @notice Returns pending points for user in a pool
* @param _pid pid of the pool
* @param _user user in the pool
* @return uint256 pendings points
*/
function _pendingPoints(uint256 _pid, address _user) internal view returns (uint256) {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accPointsPerShare = pool.accPointsPerShare * 1e18;
uint256 lpSupply = pool.amount;
if (getBlockNumber() > pool.lastRewardBlock && lpSupply != 0) {
uint256 blockMultiplier = _getBlockMultiplier(pool.lastRewardBlock, getBlockNumber());
uint256 pointReward =
blockMultiplier *
pointsPerBlock *
pool.allocPoint /
totalAllocPoint;
accPointsPerShare = pointReward *
1e18 /
lpSupply +
accPointsPerShare;
}
return user.amount *
accPointsPerShare /
1e36 +
user.rewardSettled -
user.rewardDebt;
}
/**
* @notice Returns pending points for user in a pool
* @param _pid pid of the pool
* @param _user user in the pool
* @return uint256 pendings points
*/
function pendingPoints(uint256 _pid, address _user) external view returns (uint256) {
return _pendingPoints(_pid, _user);
}
/**
* @notice Update accounting of all pools
*/
function massUpdatePools() public {
uint256 length = poolInfo.length;
for(uint256 pid = 0; pid < length;) {
updatePool(pid);
unchecked { ++pid; }
}
}
/**
* @notice Updating accounting of a single pool
* @param _pid pid to update
*/
function updatePool(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
if (getBlockNumber() <= pool.lastRewardBlock) {
return;
}
uint256 lpSupply = pool.amount;
uint256 _pointsPerBlock = pointsPerBlock;
uint256 _allocPoint = pool.allocPoint;
if (lpSupply == 0 || _pointsPerBlock == 0 || _allocPoint == 0) {
pool.lastRewardBlock = getBlockNumber();
return;
}
uint256 blockMultiplier = _getBlockMultiplier(pool.lastRewardBlock, getBlockNumber());
uint256 pointReward =
blockMultiplier *
_pointsPerBlock *
_allocPoint /
totalAllocPoint;
pool.accPointsPerShare = pointReward /
lpSupply +
pool.accPointsPerShare;
pool.lastRewardBlock = getBlockNumber();
}
/**
* @notice Deposit assets to SophonFarming
* @param _pid pid of the pool
* @param _amount amount of the deposit
* @param _boostAmount amount to boost
*/
function deposit(uint256 _pid, uint256 _amount, uint256 _boostAmount) external {
poolInfo[_pid].lpToken.safeTransferFrom(
msg.sender,
address(this),
_amount
);
_deposit(_pid, _amount, _boostAmount);
}
/**
* @notice Deposit DAI to SophonFarming
* @param _amount amount of the deposit
* @param _boostAmount amount to boost
*/
function depositDai(uint256 _amount, uint256 _boostAmount) external {
IERC20(dai).safeTransferFrom(
msg.sender,
address(this),
_amount
);
_depositPredefinedAsset(_amount, _amount, _boostAmount, PredefinedPool.sDAI);
}
/**
* @notice Deposit stETH to SophonFarming
* @param _amount amount of the deposit
* @param _boostAmount amount to boost
*/
function depositStEth(uint256 _amount, uint256 _boostAmount) external {
IERC20(stETH).safeTransferFrom(
msg.sender,
address(this),
_amount
);
_depositPredefinedAsset(_amount, _amount, _boostAmount, PredefinedPool.wstETH);
}
/**
* @notice Deposit eETH to SophonFarming
* @param _amount amount of the deposit
* @param _boostAmount amount to boost
*/
function depositeEth(uint256 _amount, uint256 _boostAmount) external {
IERC20(eETH).safeTransferFrom(
msg.sender,
address(this),
_amount
);
_depositPredefinedAsset(_amount, _amount, _boostAmount, PredefinedPool.weETH);
}
/**
* @notice Deposit ETH to SophonFarming when specifying a pool
* @param _boostAmount amount to boost
* @param _predefinedPool specific pool type to deposit to
*/
function depositEth(uint256 _boostAmount, PredefinedPool _predefinedPool) public payable {
if (msg.value == 0) {
revert NoEthSent();
}
uint256 _finalAmount = msg.value;
if (_predefinedPool == PredefinedPool.wstETH) {
_finalAmount = _ethTOstEth(_finalAmount);
} else if (_predefinedPool == PredefinedPool.weETH) {
_finalAmount = _ethTOeEth(_finalAmount);
}
_depositPredefinedAsset(_finalAmount, msg.value, _boostAmount, _predefinedPool);
}
/**
* @notice Deposit WETH to SophonFarming when specifying a pool
* @param _amount amount of the deposit
* @param _boostAmount amount to boost
* @param _predefinedPool specific pool type to deposit to
*/
function depositWeth(uint256 _amount, uint256 _boostAmount, PredefinedPool _predefinedPool) external {
IERC20(weth).safeTransferFrom(
msg.sender,
address(this),
_amount
);
uint256 _finalAmount = _wethTOEth(_amount);
if (_predefinedPool == PredefinedPool.wstETH) {
_finalAmount = _ethTOstEth(_finalAmount);
} else if (_predefinedPool == PredefinedPool.weETH) {
_finalAmount = _ethTOeEth(_finalAmount);
}
_depositPredefinedAsset(_finalAmount, _amount, _boostAmount, _predefinedPool);
}
/**
* @notice Deposit a predefined asset to SophonFarming
* @param _amount amount of the deposit
* @param _initalAmount amount of the deposit prior to conversions
* @param _boostAmount amount to boost
* @param _predefinedPool specific pool type to deposit to
*/
function _depositPredefinedAsset(uint256 _amount, uint256 _initalAmount, uint256 _boostAmount, PredefinedPool _predefinedPool) internal {
uint256 _finalAmount;
if (_predefinedPool == PredefinedPool.sDAI) {
_finalAmount = _daiTOsDai(_amount);
} else if (_predefinedPool == PredefinedPool.wstETH) {
_finalAmount = _stEthTOwstEth(_amount);
} else if (_predefinedPool == PredefinedPool.weETH) {
_finalAmount = _eethTOweEth(_amount);
} else {
revert InvalidDeposit();
}
// adjust boostAmount for the new asset
_boostAmount = _boostAmount * _finalAmount / _initalAmount;
_deposit(typeToId[_predefinedPool], _finalAmount, _boostAmount);
}
/**
* @notice Deposit an asset to SophonFarming
* @param _pid pid of the deposit
* @param _depositAmount amount of the deposit
* @param _boostAmount amount to boost
*/
function _deposit(uint256 _pid, uint256 _depositAmount, uint256 _boostAmount) internal {
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
if (_depositAmount == 0) {
revert InvalidDeposit();
}
if (_boostAmount > _depositAmount) {
revert BoostTooHigh(_depositAmount);
}
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
updatePool(_pid);
uint256 userAmount = user.amount;
user.rewardSettled =
userAmount *
pool.accPointsPerShare /
1e18 +
user.rewardSettled -
user.rewardDebt;
// booster purchase proceeds
heldProceeds[_pid] = heldProceeds[_pid] + _boostAmount;
// deposit amount is reduced by amount of the deposit to boost
_depositAmount = _depositAmount - _boostAmount;
// set deposit amount
user.depositAmount = user.depositAmount + _depositAmount;
pool.depositAmount = pool.depositAmount + _depositAmount;
// apply the boost multiplier
_boostAmount = _boostAmount * boosterMultiplier / 1e18;
user.boostAmount = user.boostAmount + _boostAmount;
pool.boostAmount = pool.boostAmount + _boostAmount;
// userAmount is increased by remaining deposit amount + full boosted amount
userAmount = userAmount + _depositAmount + _boostAmount;
user.amount = userAmount;
pool.amount = pool.amount + _depositAmount + _boostAmount;
user.rewardDebt = userAmount *
pool.accPointsPerShare /
1e18;
emit Deposit(msg.sender, _pid, _depositAmount, _boostAmount);
}
/**
* @notice Increase boost from existing deposits
* @param _pid pid to pool
* @param _boostAmount amount to boost
*/
function increaseBoost(uint256 _pid, uint256 _boostAmount) external {
if (isFarmingEnded()) {
revert FarmingIsEnded();
}
if (_boostAmount == 0) {
revert BoostIsZero();
}
uint256 maxAdditionalBoost = getMaxAdditionalBoost(msg.sender, _pid);
if (_boostAmount > maxAdditionalBoost) {
revert BoostTooHigh(maxAdditionalBoost);
}
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
updatePool(_pid);
uint256 userAmount = user.amount;
user.rewardSettled =
userAmount *
pool.accPointsPerShare /
1e18 +
user.rewardSettled -
user.rewardDebt;
// booster purchase proceeds
heldProceeds[_pid] = heldProceeds[_pid] + _boostAmount;
// user's remaining deposit is reduced by amount of the deposit to boost
user.depositAmount = user.depositAmount - _boostAmount;
pool.depositAmount = pool.depositAmount - _boostAmount;
// apply the multiplier
uint256 finalBoostAmount = _boostAmount * boosterMultiplier / 1e18;
user.boostAmount = user.boostAmount + finalBoostAmount;
pool.boostAmount = pool.boostAmount + finalBoostAmount;
// user amount is increased by the full boosted amount - deposit amount used to boost
userAmount = userAmount + finalBoostAmount - _boostAmount;
user.amount = userAmount;
pool.amount = pool.amount + finalBoostAmount - _boostAmount;
user.rewardDebt = userAmount *
pool.accPointsPerShare /
1e18;
emit IncreaseBoost(msg.sender, _pid, finalBoostAmount);
}
/**
* @notice Returns max additional boost amount allowed to boost current deposits
* @dev total allowed boost is 100% of total deposit
* @param _user user in pool
* @param _pid pid of pool
* @return uint256 max additional boost
*/
function getMaxAdditionalBoost(address _user, uint256 _pid) public view returns (uint256) {
return userInfo[_pid][_user].depositAmount;
}
/**
* @notice Withdraw an asset to SophonFarming
* @param _pid pid of the withdraw
* @param _withdrawAmount amount of the withdraw
*/
function withdraw(uint256 _pid, uint256 _withdrawAmount) external {
if (isWithdrawPeriodEnded()) {
revert WithdrawNotAllowed();
}
if (_withdrawAmount == 0) {
revert WithdrawIsZero();
}
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
updatePool(_pid);
uint256 userDepositAmount = user.depositAmount;
if (_withdrawAmount == type(uint256).max) {
_withdrawAmount = userDepositAmount;
} else if (_withdrawAmount > userDepositAmount) {
revert WithdrawTooHigh(userDepositAmount);
}
uint256 userAmount = user.amount;
user.rewardSettled =
userAmount *
pool.accPointsPerShare /
1e18 +
user.rewardSettled -
user.rewardDebt;
user.depositAmount = userDepositAmount - _withdrawAmount;
pool.depositAmount = pool.depositAmount - _withdrawAmount;
userAmount = userAmount - _withdrawAmount;
user.amount = userAmount;
pool.amount = pool.amount - _withdrawAmount;
pool.lpToken.safeTransfer(msg.sender, _withdrawAmount);
user.rewardDebt = userAmount *
pool.accPointsPerShare /
1e18;
emit Withdraw(msg.sender, _pid, _withdrawAmount);
}
/**
* @notice Permissionless function to allow anyone to bridge during the correct period
* @param _pid pid to bridge
*/
function bridgePool(uint256 _pid) external {
if (!isFarmingEnded() || !isWithdrawPeriodEnded() || isBridged[_pid]) {
revert Unauthorized();
}
updatePool(_pid);
PoolInfo storage pool = poolInfo[_pid];
uint256 depositAmount = pool.depositAmount;
if (depositAmount == 0 || address(bridge) == address(0) || pool.l2Farm == address(0)) {
revert BridgeInvalid();
}
IERC20 lpToken = pool.lpToken;
lpToken.approve(address(bridge), depositAmount);
// TODO: change _refundRecipient, verify l2Farm, _l2TxGasLimit and _l2TxGasPerPubdataByte
// These are pending the launch of Sophon testnet
bridge.deposit(
pool.l2Farm, // _l2Receiver
address(lpToken), // _l1Token
depositAmount, // _amount
200000, // _l2TxGasLimit
0, // _l2TxGasPerPubdataByte
owner() // _refundRecipient
);
isBridged[_pid] = true;
emit Bridge(msg.sender, _pid, depositAmount);
}
// TODO: does this function need to call claimFailedDeposit on the bridge?
// This is pending the launch of Sophon testnet
/**
* @notice Called by an admin if a bridge process to Sophon fails
* @param _pid pid of the failed bridge to revert
*/
function revertFailedBridge(uint256 _pid) external onlyOwner {
isBridged[_pid] = false;
}
/**
* @notice Converts WETH to ETH
* @dev WETH withdrawl
* @param _amount in amount
* @return uint256 out amount
*/
function _wethTOEth(uint256 _amount) internal returns (uint256) {
// unwrap weth to eth
IWeth(weth).withdraw(_amount);
return _amount;
}
/**
* @notice Converts ETH to stETH
* @dev Lido
* @param _amount in amount
* @return uint256 out amount
*/
function _ethTOstEth(uint256 _amount) internal returns (uint256) {
// submit function does not return exact amount of stETH so we need to check balances
uint256 balanceBefore = IERC20(stETH).balanceOf(address(this));
IstETH(stETH).submit{value: _amount}(address(this));
return (IERC20(stETH).balanceOf(address(this)) - balanceBefore);
}
/**
* @notice Converts stETH to wstETH
* @dev Lido
* @param _amount in amount
* @return uint256 out amount
*/
function _stEthTOwstEth(uint256 _amount) internal returns (uint256) {
// wrap returns exact amount of wstETH
return IwstETH(wstETH).wrap(_amount);
}
/**
* @notice Converts ETH to eETH
* @dev ether.fi
* @param _amount in amount
* @return uint256 out amount
*/
function _ethTOeEth(uint256 _amount) internal returns (uint256) {
// deposit returns exact amount of eETH
return IeETHLiquidityPool(eETHLiquidityPool).deposit{value: _amount}(address(this));
}
/**
* @notice Converts eETH to weETH
* @dev ether.fi
* @param _amount in amount
* @return uint256 out amount
*/
function _eethTOweEth(uint256 _amount) internal returns (uint256) {
// wrap returns exact amount of weETH
return IweETH(weETH).wrap(_amount);
}
/**
* @notice Converts DAI to sDAI
* @dev MakerDao
* @param _amount in amount
* @return uint256 out amount
*/
function _daiTOsDai(uint256 _amount) internal returns (uint256) {
// deposit DAI to sDAI
return IsDAI(sDAI).deposit(_amount, address(this));
}
/**
* @notice Allows an admin to withdraw booster proceeds
* @param _pid pid to withdraw proceeds from
*/
function withdrawProceeds(uint256 _pid) external onlyOwner {
PoolInfo storage pool = poolInfo[_pid];
uint256 _proceeds = heldProceeds[_pid];
heldProceeds[_pid] = 0;
pool.lpToken.safeTransfer(msg.sender, _proceeds);
emit WithdrawProceeds(_pid, _proceeds);
}
/**
* @notice Returns the current block number
* @dev Included to help with testing since it can be overridden for custom functionality
* @return uint256 current block number
*/
function getBlockNumber() virtual public view returns (uint256) {
return block.number;
}
/**
* @notice Returns info about each pool
* @return poolInfos all pool info
*/
function getPoolInfo() external view returns (PoolInfo[] memory poolInfos) {
uint256 length = poolInfo.length;
poolInfos = new PoolInfo[](length);
for(uint256 pid = 0; pid < length;) {
poolInfos[pid] = poolInfo[pid];
unchecked { ++pid; }
}
}
/**
* @notice Returns user info for a list of users
* @param _users list of users
* @return userInfos optimized user info
*/
function getOptimizedUserInfo(address[] memory _users) external view returns (uint256[4][][] memory userInfos) {
userInfos = new uint256[4][][](_users.length);
uint256 len = poolInfo.length;
for(uint256 i = 0; i < _users.length;) {
address _user = _users[i];
userInfos[i] = new uint256[4][](len);
for(uint256 pid = 0; pid < len;) {
UserInfo memory uinfo = userInfo[pid][_user];
userInfos[i][pid][0] = uinfo.amount;
userInfos[i][pid][1] = uinfo.boostAmount;
userInfos[i][pid][2] = uinfo.depositAmount;
userInfos[i][pid][3] = _pendingPoints(pid, _user);
unchecked { ++pid; }
}
unchecked { i++; }
}
}
/**
* @notice Returns accrued points for a list of users
* @param _users list of users
* @return pendings accured points for user
*/
function getPendingPoints(address[] memory _users) external view returns (uint256[][] memory pendings) {
pendings = new uint256[][](_users.length);
uint256 len = poolInfo.length;
for(uint256 i = 0; i < _users.length;) {
address _user = _users[i];
pendings[i] = new uint256[](len);
for(uint256 pid = 0; pid < len;) {
pendings[i][pid] = _pendingPoints(pid, _user);
unchecked { ++pid; }
}
unchecked { i++; }
}
}
}