@@ -106,6 +106,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
106
106
hasSecondSide = false ;
107
107
108
108
mainSideConfig = mainSideConfig_;
109
+ mainSideInfo.lastInterestUpdate = block .timestamp ;
109
110
110
111
_setupRole (DEFAULT_ADMIN_ROLE, msg .sender );
111
112
}
@@ -116,6 +117,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
116
117
require (! hasSecondSide, "Second side already exists " );
117
118
hasSecondSide = true ;
118
119
dependantSideConfig = config_;
120
+ dependantSideInfo.lastInterestUpdate = block .timestamp ;
119
121
}
120
122
121
123
function activate () public onlyRole (DEFAULT_ADMIN_ROLE) {
@@ -273,7 +275,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
273
275
}
274
276
275
277
function getUserDependantSideRewards (address user ) public view returns (uint ) {
276
- uint rewardsAmount = _calcRewards (false , dependantSideStakers[user].stake);
278
+ uint rewardsAmount = _calcRewards (true , dependantSideStakers[user].stake);
277
279
if (rewardsAmount + dependantSideStakers[user].claimableRewards <= dependantSideStakers[user].rewardsDebt)
278
280
return 0 ;
279
281
@@ -285,7 +287,6 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
285
287
// MAIN SIDE METHODS
286
288
287
289
function _addInterestMainSide () internal {
288
- if (mainSideInfo.lastInterestUpdate + mainSideConfig.interestRate > block .timestamp ) return ;
289
290
uint timePassed = block .timestamp - mainSideInfo.lastInterestUpdate;
290
291
uint newRewards = mainSideInfo.totalStake * mainSideConfig.interest * timePassed / BILLION / mainSideConfig.interestRate;
291
292
@@ -308,7 +309,6 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
308
309
uint rewardsAmount = _calcRewards (false , amount);
309
310
310
311
mainSideStakers[msg .sender ].stake += amount;
311
- mainSideStakers[msg .sender ].stakedAt = block .timestamp ;
312
312
mainSideInfo.totalStake += amount;
313
313
314
314
mainSideInfo.totalRewards += rewardsAmount;
@@ -335,16 +335,20 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
335
335
canceledAmount = lockKeeper.cancelLock (mainSideStakers[msg .sender ].lockedWithdrawal);
336
336
337
337
if (mainSideConfig.token == address (0 )) {
338
- payable (msg .sender ).transfer (amount - canceledAmount);
338
+ // lock funds
339
+ mainSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle {value: amount + canceledAmount}(
340
+ msg .sender , address (mainSideConfig.token), uint64 (block .timestamp + mainSideConfig.lockPeriod), amount + canceledAmount,
341
+ string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (mainSideConfig.token))))
342
+ );
339
343
} else {
340
- IERC20 (mainSideConfig.token).approve (address (lockKeeper), amount - canceledAmount);
344
+ IERC20 (mainSideConfig.token).approve (address (lockKeeper), amount + canceledAmount);
345
+ // lock funds
346
+ mainSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle (
347
+ msg .sender , address (mainSideConfig.token), uint64 (block .timestamp + mainSideConfig.lockPeriod), amount + canceledAmount,
348
+ string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (mainSideConfig.token))))
349
+ );
341
350
}
342
351
343
- // lock funds
344
- mainSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle (
345
- msg .sender , address (mainSideConfig.token), uint64 (block .timestamp + mainSideConfig.lockPeriod), amount + canceledAmount,
346
- string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (mainSideConfig.token))))
347
- );
348
352
349
353
_claimRewards (false , msg .sender );
350
354
@@ -380,6 +384,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
380
384
// DEPENDANT SIDE METHODS
381
385
382
386
function _addInterestDependantSide () internal {
387
+ if (! hasSecondSide) return ;
383
388
if (dependantSideInfo.lastInterestUpdate + dependantSideConfig.interestRate > block .timestamp ) return ;
384
389
uint timePassed = block .timestamp - dependantSideInfo.lastInterestUpdate;
385
390
uint newRewards = dependantSideInfo.totalStake * dependantSideConfig.interest * timePassed / BILLION / dependantSideConfig.interestRate;
@@ -396,21 +401,26 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
396
401
require (mainSideConfig.token == address (0 ), "Pool: does not accept native coin " );
397
402
require (msg .value == amount, "Pool: wrong amount of native coin " );
398
403
} else {
399
- SafeERC20.safeTransferFrom (IERC20 (mainSideConfig .token), msg .sender , address (this ), amount);
404
+ SafeERC20.safeTransferFrom (IERC20 (dependantSideConfig .token), msg .sender , address (this ), amount);
400
405
}
401
406
407
+ console.log ("Staking dependant side " );
408
+ console.log ("max user stake: " , _maxUserStakeValue (msg .sender ));
409
+ console.log ("amount: " , amount);
410
+
402
411
uint rewardsAmount = _calcRewards (true , amount);
403
412
404
413
dependantSideStakers[msg .sender ].stake += amount;
405
414
dependantSideInfo.totalStake += amount;
415
+ dependantSideStakers[msg .sender ].stakedAt = block .timestamp ;
406
416
407
- require (dependantSideInfo.totalStake <= dependantSideConfig.maxTotalStakeValue, "Pool: max stake value exceeded " );
408
417
require (dependantSideStakers[msg .sender ].stake <= _maxUserStakeValue (msg .sender ), "Pool: user max stake value exceeded " );
418
+ require (dependantSideInfo.totalStake <= dependantSideConfig.maxTotalStakeValue, "Pool: max stake value exceeded " );
409
419
require (dependantSideStakers[msg .sender ].stake <= dependantSideConfig.maxStakePerUserValue, "Pool: max stake per user exceeded " );
410
420
411
421
dependantSideInfo.totalRewards += rewardsAmount;
412
422
413
- _updateRewardsDebt (true , user, _calcRewards (true , mainSideStakers [user].stake));
423
+ _updateRewardsDebt (true , user, _calcRewards (true , dependantSideStakers [user].stake));
414
424
emit StakeChanged (true , msg .sender , amount);
415
425
}
416
426
@@ -432,16 +442,20 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
432
442
canceledAmount = lockKeeper.cancelLock (dependantSideStakers[msg .sender ].lockedWithdrawal);
433
443
434
444
if (mainSideConfig.token == address (0 )) {
435
- payable (msg .sender ).transfer (amount - canceledAmount);
445
+ // lock funds
446
+ dependantSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle {value: amount + canceledAmount}(
447
+ msg .sender , address (dependantSideConfig.token), uint64 (block .timestamp + dependantSideConfig.lockPeriod), amount + canceledAmount,
448
+ string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (dependantSideConfig.token))))
449
+ );
436
450
} else {
437
- IERC20 (mainSideConfig.token).approve (address (lockKeeper), amount - canceledAmount);
451
+ IERC20 (dependantSideConfig.token).approve (address (lockKeeper), amount + canceledAmount);
452
+ // lock funds
453
+ dependantSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle (
454
+ msg .sender , address (dependantSideConfig.token), uint64 (block .timestamp + dependantSideConfig.lockPeriod), amount + canceledAmount,
455
+ string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (dependantSideConfig.token))))
456
+ );
438
457
}
439
458
440
- // lock funds
441
- dependantSideStakers[msg .sender ].lockedWithdrawal = lockKeeper.lockSingle (
442
- msg .sender , address (mainSideConfig.token), uint64 (block .timestamp + dependantSideConfig.lockPeriod), amount + canceledAmount,
443
- string (abi.encodePacked ("TokenStaking unstake: " , _addressToString (address (dependantSideConfig.token))))
444
- );
445
459
446
460
_claimRewards (true , msg .sender );
447
461
@@ -476,7 +490,9 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
476
490
}
477
491
478
492
function _maxUserStakeValue (address user ) internal view returns (uint ) {
479
- return mainSideStakers[user].stake * dependantSideConfig.stakeLimitsMultiplier / BILLION;
493
+ console.log ("main side stake: " , mainSideStakers[user].stake);
494
+ console.log ("stake limits multiplier: " , dependantSideConfig.stakeLimitsMultiplier);
495
+ return mainSideStakers[user].stake * dependantSideConfig.stakeLimitsMultiplier;
480
496
}
481
497
482
498
//COMMON METHODS
@@ -531,10 +547,10 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
531
547
function _calcRewards (bool dependant , uint amount ) internal view returns (uint ) {
532
548
if (dependant) {
533
549
if (dependantSideInfo.totalStake == 0 && dependantSideInfo.totalRewards == 0 ) return amount;
534
- amount * dependantSideInfo.totalRewards / dependantSideInfo.totalStake;
550
+ return amount * dependantSideInfo.totalRewards / dependantSideInfo.totalStake;
535
551
} else {
536
552
if (mainSideInfo.totalStake == 0 && mainSideInfo.totalRewards == 0 ) return amount;
537
- amount * mainSideInfo.totalRewards / mainSideInfo.totalStake;
553
+ return amount * mainSideInfo.totalRewards / mainSideInfo.totalStake;
538
554
}
539
555
}
540
556
0 commit comments