Skip to content

Commit

Permalink
fix: stakerReward being updated properly (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamAg19 authored Mar 30, 2022
1 parent e2895f0 commit 1e19b51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
12 changes: 9 additions & 3 deletions contracts/Core/RewardManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ contract RewardManager is Initializable, Constants, RewardManagerParams, IReward
uint256 stakerSRZR = sToken.balanceOf(staker._address);
uint256 delegatorShare = blockReward - ((blockReward * stakerSRZR) / totalSupply);
uint8 commissionApplicable = staker.commission < maxCommission ? staker.commission : maxCommission;
uint256 commission = (delegatorShare * commissionApplicable) / 100;
stakeManager.setStakerStake(epoch, stakerId, StakeChanged.BlockReward, staker.stake, staker.stake + (blockReward - commission));
stakeManager.setStakerStakerReward(epoch, stakerId, StakerRewardChanged.StakerRewardAdded, staker.commission, commission);
uint256 stakerReward = (delegatorShare * commissionApplicable) / 100;
stakeManager.setStakerStake(epoch, stakerId, StakeChanged.BlockReward, staker.stake, staker.stake + (blockReward - stakerReward));
stakeManager.setStakerStakerReward(
epoch,
stakerId,
StakerRewardChanged.StakerRewardAdded,
staker.stakerReward,
staker.stakerReward + stakerReward
);
}

/// @inheritdoc IRewardManager
Expand Down
43 changes: 23 additions & 20 deletions test/StakeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2039,32 +2039,35 @@ describe('StakeManager', function () {
await razor.connect(signers[5]).approve(stakeManager.address, delegatedStake);
await stakeManager.connect(signers[5]).delegate(staker.id, delegatedStake);

await mineToNextEpoch();
await commit(signers[4], 0, voteManager, collectionManager, secret, blockManager);
for (let i = 0; i < 5; i++) {
await mineToNextEpoch();
await commit(signers[4], 0, voteManager, collectionManager, secret, blockManager);

await mineToNextState(); // reveal
await reveal(signers[4], 0, voteManager, stakeManager, collectionManager);
await mineToNextState(); // reveal
await reveal(signers[4], 0, voteManager, stakeManager, collectionManager);

await mineToNextState(); // propose
await propose(signers[4], stakeManager, blockManager, voteManager, collectionManager);
await mineToNextState(); // propose
await propose(signers[4], stakeManager, blockManager, voteManager, collectionManager);

await mineToNextState(); // dispute
await mineToNextState(); // confirm
await mineToNextState(); // dispute
await mineToNextState(); // confirm

staker = await stakeManager.getStaker(stakerId);
prevStake = staker.stake;
staker = await stakeManager.getStaker(stakerId);
prevStake = staker.stake;
const prevStakeReward = staker.stakerReward;

await blockManager.connect(signers[4]).claimBlockReward();
await blockManager.connect(signers[4]).claimBlockReward();

staker = await stakeManager.getStaker(stakerId);
const sToken = await stakedToken.attach(staker.tokenAddress);
const totalSupply = await sToken.totalSupply();
const stakerSRZR = await sToken.balanceOf(signers[4].address);
const stakerShare = blockReward.mul(stakerSRZR).div(totalSupply);
const delegatorShare = blockReward.sub(stakerShare);
const stakerReward = delegatorShare.mul(toBigNumber(staker.commission)).div(toBigNumber('100'));
assertBNEqual(staker.stakerReward, stakerReward, 'Incorrect Commission Calculation');
assertBNEqual(prevStake.add(blockReward.sub(stakerReward)), staker.stake, 'Incorrect RAZOR rewarded');
staker = await stakeManager.getStaker(stakerId);
const sToken = await stakedToken.attach(staker.tokenAddress);
const totalSupply = await sToken.totalSupply();
const stakerSRZR = await sToken.balanceOf(signers[4].address);
const stakerShare = blockReward.mul(stakerSRZR).div(totalSupply);
const delegatorShare = blockReward.sub(stakerShare);
const stakerReward = delegatorShare.mul(toBigNumber(staker.commission)).div(toBigNumber('100'));
assertBNEqual(staker.stakerReward, prevStakeReward.add(stakerReward), 'Incorrect Commission Calculation');
assertBNEqual(prevStake.add(blockReward.sub(stakerReward)), staker.stake, 'Incorrect RAZOR rewarded');
}
});
it('staker should not claim stakerReward if stakerReward is 0', async function () {
const tx = stakeManager.connect(signers[1]).claimStakerReward();
Expand Down

0 comments on commit 1e19b51

Please sign in to comment.