@0x/contracts-staking: Transition to V3

This commit is contained in:
Lawrence Forman
2019-09-16 16:02:14 -04:00
committed by Lawrence Forman
parent 0196ce18f3
commit e267a0e855
2 changed files with 23 additions and 38 deletions

View File

@@ -276,7 +276,17 @@ contract MixinStakingPoolRewards is
// 2. `stake.currentEpoch < currentEpoch`. // 2. `stake.currentEpoch < currentEpoch`.
// Get the last epoch where a reward was credited to this pool. // Get the last epoch where a reward was credited to this pool.
uint256 lastRewardEpoch = cumulativeRewardsByPoolLastStored[poolId]; uint256 lastRewardEpoch = lastPoolRewardEpoch[poolId];
// Get the last reward epoch for which we collected rewards from.
uint256 lastCollectedRewardEpoch =
lastCollectedRewardsEpochToPoolByOwner[member][poolId];
// If either of these are true, the most recent reward has already been
// claimed.
if (lastCollectedRewardEpoch == lastRewardEpoch
|| stake.currentEpoch >= lastRewardEpoch) {
return reward = 0;
}
// If there are unfinalized rewards this epoch, compute the member's // If there are unfinalized rewards this epoch, compute the member's
// share. // share.
@@ -291,43 +301,18 @@ contract MixinStakingPoolRewards is
.safeMul(unfinalizedMembersReward) .safeMul(unfinalizedMembersReward)
.safeDiv(unfinalizedDelegatedStake); .safeDiv(unfinalizedDelegatedStake);
} }
// Add rewards up to the last reward epoch. }
if (lastRewardEpoch != 0 && lastRewardEpoch > stake.currentEpoch) {
reward = reward.safeAdd( // Add rewards up to the last reward epoch.
_computeMemberRewardOverInterval( if (lastRewardEpoch != 0) {
poolId, reward = reward.safeAdd(
stake, _computeMemberRewardOverInterval(
stake.currentEpoch,
stake.currentEpoch + 1
)
);
if (lastRewardEpoch > stake.currentEpoch + 1) {
reward = reward.safeAdd(
_computeMemberRewardOverInterval(
poolId,
stake,
stake.currentEpoch + 1,
lastRewardEpoch
)
);
}
}
// Otherwise get the rewards earned up to the last reward epoch.
} else if (stake.currentEpoch < lastRewardEpoch) {
reward = _computeMemberRewardOverInterval(
poolId,
stake,
stake.currentEpoch,
stake.currentEpoch + 1
);
if (lastRewardEpoch > stake.currentEpoch + 1) {
reward = _computeMemberRewardOverInterval(
poolId, poolId,
stake, stake,
stake.currentEpoch + 1, stake.currentEpoch + 1,
lastRewardEpoch lastRewardEpoch
).safeSub(reward); )
} );
} }
} }

View File

@@ -18,7 +18,7 @@ import {
import { assertRoughlyEquals, getRandomInteger, toBaseUnitAmount } from '../utils/number_utils'; import { assertRoughlyEquals, getRandomInteger, toBaseUnitAmount } from '../utils/number_utils';
blockchainTests.resets('delegator unit rewards', env => { blockchainTests.resets.only('delegator unit rewards', env => {
let testContract: TestDelegatorRewardsContract; let testContract: TestDelegatorRewardsContract;
before(async () => { before(async () => {
@@ -304,7 +304,7 @@ blockchainTests.resets('delegator unit rewards', env => {
expect(delegatorReward).to.bignumber.eq(0); expect(delegatorReward).to.bignumber.eq(0);
}); });
it.only('has correct reward immediately after unstaking, restaking, and rewarding fees', async () => { it('has correct reward immediately after unstaking, restaking, and rewarding fees', async () => {
const poolId = hexRandom(); const poolId = hexRandom();
const { delegator, stake } = await delegateStakeAsync(poolId); const { delegator, stake } = await delegateStakeAsync(poolId);
await advanceEpochAsync(); // epoch 1 (stake now active) await advanceEpochAsync(); // epoch 1 (stake now active)
@@ -323,7 +323,7 @@ blockchainTests.resets('delegator unit rewards', env => {
expect(delegatorReward).to.bignumber.eq(reward); expect(delegatorReward).to.bignumber.eq(reward);
}); });
it('computes correct rewards for 2 staggered delegators', async () => { it.only('computes correct rewards for 2 staggered delegators', async () => {
const poolId = hexRandom(); const poolId = hexRandom();
const { delegator: delegatorA, stake: stakeA } = await delegateStakeAsync(poolId); const { delegator: delegatorA, stake: stakeA } = await delegateStakeAsync(poolId);
await advanceEpochAsync(); // epoch 1 (stake A now active) await advanceEpochAsync(); // epoch 1 (stake A now active)
@@ -347,7 +347,7 @@ blockchainTests.resets('delegator unit rewards', env => {
assertRoughlyEquals(delegatorRewardA, expectedDelegatorRewardA); assertRoughlyEquals(delegatorRewardA, expectedDelegatorRewardA);
const delegatorRewardB = await getDelegatorRewardAsync(poolId, delegatorB); const delegatorRewardB = await getDelegatorRewardAsync(poolId, delegatorB);
const expectedDelegatorRewardB = BigNumber.sum( const expectedDelegatorRewardB = BigNumber.sum(
computeDelegatorRewards(reward2, stakeB, totalStake), computeDelegatorRewards(BigNumber.sum(reward1, reward2), stakeB, totalStake),
); );
assertRoughlyEquals(delegatorRewardB, expectedDelegatorRewardB); assertRoughlyEquals(delegatorRewardB, expectedDelegatorRewardB);
}); });