Skip to content

Commit a94dd9f

Browse files
Tests for DoubleSidePool
1 parent 0422f79 commit a94dd9f

File tree

2 files changed

+590
-23
lines changed

2 files changed

+590
-23
lines changed

contracts/staking/token/DoubleSidePool.sol

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
106106
hasSecondSide = false;
107107

108108
mainSideConfig = mainSideConfig_;
109+
mainSideInfo.lastInterestUpdate = block.timestamp;
109110

110111
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
111112
}
@@ -116,6 +117,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
116117
require(!hasSecondSide, "Second side already exists");
117118
hasSecondSide = true;
118119
dependantSideConfig = config_;
120+
dependantSideInfo.lastInterestUpdate = block.timestamp;
119121
}
120122

121123
function activate() public onlyRole(DEFAULT_ADMIN_ROLE) {
@@ -273,7 +275,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
273275
}
274276

275277
function getUserDependantSideRewards(address user) public view returns (uint) {
276-
uint rewardsAmount = _calcRewards(false, dependantSideStakers[user].stake);
278+
uint rewardsAmount = _calcRewards(true, dependantSideStakers[user].stake);
277279
if (rewardsAmount + dependantSideStakers[user].claimableRewards <= dependantSideStakers[user].rewardsDebt)
278280
return 0;
279281

@@ -285,7 +287,6 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
285287
// MAIN SIDE METHODS
286288

287289
function _addInterestMainSide() internal {
288-
if (mainSideInfo.lastInterestUpdate + mainSideConfig.interestRate > block.timestamp) return;
289290
uint timePassed = block.timestamp - mainSideInfo.lastInterestUpdate;
290291
uint newRewards = mainSideInfo.totalStake * mainSideConfig.interest * timePassed / BILLION / mainSideConfig.interestRate;
291292

@@ -308,7 +309,6 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
308309
uint rewardsAmount = _calcRewards(false, amount);
309310

310311
mainSideStakers[msg.sender].stake += amount;
311-
mainSideStakers[msg.sender].stakedAt = block.timestamp;
312312
mainSideInfo.totalStake += amount;
313313

314314
mainSideInfo.totalRewards += rewardsAmount;
@@ -335,16 +335,20 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
335335
canceledAmount = lockKeeper.cancelLock(mainSideStakers[msg.sender].lockedWithdrawal);
336336

337337
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+
);
339343
} 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+
);
341350
}
342351

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-
);
348352

349353
_claimRewards(false, msg.sender);
350354

@@ -380,6 +384,7 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
380384
// DEPENDANT SIDE METHODS
381385

382386
function _addInterestDependantSide() internal {
387+
if (!hasSecondSide) return;
383388
if (dependantSideInfo.lastInterestUpdate + dependantSideConfig.interestRate > block.timestamp) return;
384389
uint timePassed = block.timestamp - dependantSideInfo.lastInterestUpdate;
385390
uint newRewards = dependantSideInfo.totalStake * dependantSideConfig.interest * timePassed / BILLION / dependantSideConfig.interestRate;
@@ -396,21 +401,26 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
396401
require(mainSideConfig.token == address(0), "Pool: does not accept native coin");
397402
require(msg.value == amount, "Pool: wrong amount of native coin");
398403
} else {
399-
SafeERC20.safeTransferFrom(IERC20(mainSideConfig.token), msg.sender, address(this), amount);
404+
SafeERC20.safeTransferFrom(IERC20(dependantSideConfig.token), msg.sender, address(this), amount);
400405
}
401406

407+
console.log("Staking dependant side");
408+
console.log("max user stake: ", _maxUserStakeValue(msg.sender));
409+
console.log("amount: ", amount);
410+
402411
uint rewardsAmount = _calcRewards(true, amount);
403412

404413
dependantSideStakers[msg.sender].stake += amount;
405414
dependantSideInfo.totalStake += amount;
415+
dependantSideStakers[msg.sender].stakedAt = block.timestamp;
406416

407-
require(dependantSideInfo.totalStake <= dependantSideConfig.maxTotalStakeValue, "Pool: max stake value exceeded");
408417
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");
409419
require(dependantSideStakers[msg.sender].stake <= dependantSideConfig.maxStakePerUserValue, "Pool: max stake per user exceeded");
410420

411421
dependantSideInfo.totalRewards += rewardsAmount;
412422

413-
_updateRewardsDebt(true, user, _calcRewards(true, mainSideStakers[user].stake));
423+
_updateRewardsDebt(true, user, _calcRewards(true, dependantSideStakers[user].stake));
414424
emit StakeChanged(true, msg.sender, amount);
415425
}
416426

@@ -432,16 +442,20 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
432442
canceledAmount = lockKeeper.cancelLock(dependantSideStakers[msg.sender].lockedWithdrawal);
433443

434444
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+
);
436450
} 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+
);
438457
}
439458

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-
);
445459

446460
_claimRewards(true, msg.sender);
447461

@@ -476,7 +490,9 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
476490
}
477491

478492
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;
480496
}
481497

482498
//COMMON METHODS
@@ -531,10 +547,10 @@ contract DoubleSidePool is Initializable, AccessControl, IOnBlockListener {
531547
function _calcRewards(bool dependant, uint amount) internal view returns (uint) {
532548
if (dependant) {
533549
if (dependantSideInfo.totalStake == 0 && dependantSideInfo.totalRewards == 0) return amount;
534-
amount * dependantSideInfo.totalRewards / dependantSideInfo.totalStake;
550+
return amount * dependantSideInfo.totalRewards / dependantSideInfo.totalStake;
535551
} else {
536552
if (mainSideInfo.totalStake == 0 && mainSideInfo.totalRewards == 0) return amount;
537-
amount * mainSideInfo.totalRewards / mainSideInfo.totalStake;
553+
return amount * mainSideInfo.totalRewards / mainSideInfo.totalStake;
538554
}
539555
}
540556

0 commit comments

Comments
 (0)