@0x/contracts-staking: Create MixinStakingPoolRewards unit tests.

This commit is contained in:
Lawrence Forman 2019-10-31 15:53:46 -04:00
parent 4e05e41f7f
commit 8c4c3d56c6
6 changed files with 221 additions and 1 deletions

View File

@ -0,0 +1,181 @@
/*
Copyright 2019 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;
import "../src/interfaces/IStructs.sol";
import "./TestStakingNoWETH.sol";
contract TestMixinStakingPoolRewards is
TestStakingNoWETH
{
event UpdateCumulativeReward(
bytes32 poolId
);
struct UnfinalizedPoolReward {
uint256 reward;
uint256 membersStake;
}
constructor() public {
_addAuthorizedAddress(msg.sender);
init();
_removeAuthorizedAddressAtIndex(msg.sender, 0);
}
// Rewards returned by `_computeMemberRewardOverInterval()`, indexed
// by `_getMemberRewardOverIntervalHash()`.
mapping (bytes32 => uint256) private _memberRewardsOverInterval;
// Rewards returned by `_getUnfinalizedPoolRewards()`, indexed by pool ID.
mapping (bytes32 => UnfinalizedPoolReward) private _unfinalizedPoolRewards;
// Set the rewards returned by a call to `_computeMemberRewardOverInterval()`.
function setMemberRewardsOverInterval(
bytes32 poolId,
uint256 memberStakeOverInterval,
uint256 beginEpoch,
uint256 endEpoch,
uint256 reward
)
external
{
bytes32 rewardHash = _getMemberRewardOverIntervalHash(
poolId,
memberStakeOverInterval,
beginEpoch,
endEpoch
);
_memberRewardsOverInterval[rewardHash] = reward;
}
// Set the rewards returned by `_getUnfinalizedPoolRewards()`.
function setUnfinalizedPoolRewards(
bytes32 poolId,
uint256 reward,
uint256 membersStake
)
external
{
_unfinalizedPoolRewards[poolId] = UnfinalizedPoolReward(
reward,
membersStake
);
}
// Advance the epoch.
function advanceEpoch() external {
currentEpoch += 1;
}
// Set `_delegatedStakeToPoolByOwner`
function setDelegatedStakeToPoolByOwner(
address member,
bytes32 poolId,
IStructs.StoredBalance memory balance
)
public
{
_delegatedStakeToPoolByOwner[member][poolId] = balance;
}
// Set `_poolById`.
function setPool(
bytes32 poolId,
IStructs.Pool memory pool
)
public
{
_poolById[poolId] = pool;
}
// Overridden to use `_memberRewardsOverInterval`
function _computeMemberRewardOverInterval(
bytes32 poolId,
uint256 memberStakeOverInterval,
uint256 beginEpoch,
uint256 endEpoch
)
internal
view
returns (uint256 reward)
{
bytes32 rewardHash = _getMemberRewardOverIntervalHash(
poolId,
memberStakeOverInterval,
beginEpoch,
endEpoch
);
return _memberRewardsOverInterval[rewardHash];
}
// Overridden to use `_unfinalizedPoolRewards`
function _getUnfinalizedPoolRewards(
bytes32 poolId
)
internal
view
returns (uint256 reward, uint256 membersStake)
{
(reward, membersStake) = (
_unfinalizedPoolRewards[poolId].reward,
_unfinalizedPoolRewards[poolId].membersStake
);
}
// Overridden to revert if a pool has unfinalized rewards.
function _assertPoolFinalizedLastEpoch(bytes32 poolId)
internal
view
{
require(
_unfinalizedPoolRewards[poolId].membersStake == 0,
"POOL_NOT_FINALIZED"
);
}
// Overridden to just emit an event.
function _updateCumulativeReward(bytes32 poolId)
internal
{
emit UpdateCumulativeReward(poolId);
}
// Compute a hash to index `_memberRewardsOverInterval`
function _getMemberRewardOverIntervalHash(
bytes32 poolId,
uint256 memberStakeOverInterval,
uint256 beginEpoch,
uint256 endEpoch
)
private
pure
returns (bytes32 rewardHash)
{
return keccak256(
abi.encode(
poolId,
memberStakeOverInterval,
beginEpoch,
endEpoch
)
);
}
}

View File

@ -37,7 +37,7 @@
},
"config": {
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./generated-artifacts/@(IStaking|IStakingEvents|IStakingProxy|IStorage|IStorageInit|IStructs|IZrxVault|LibCobbDouglas|LibFixedMath|LibFixedMathRichErrors|LibSafeDowncast|LibStakingRichErrors|MixinAbstract|MixinConstants|MixinCumulativeRewards|MixinDeploymentConstants|MixinExchangeFees|MixinExchangeManager|MixinFinalizer|MixinParams|MixinScheduler|MixinStake|MixinStakeBalances|MixinStakeStorage|MixinStakingPool|MixinStakingPoolRewards|MixinStorage|Staking|StakingProxy|TestAssertStorageParams|TestCobbDouglas|TestCumulativeRewardTracking|TestDelegatorRewards|TestExchangeManager|TestFinalizer|TestInitTarget|TestLibFixedMath|TestLibSafeDowncast|TestMixinParams|TestMixinStake|TestMixinStakeBalances|TestMixinStakeStorage|TestMixinStakingPool|TestProtocolFees|TestProxyDestination|TestStaking|TestStakingNoWETH|TestStakingProxy|TestStakingProxyUnit|TestStorageLayoutAndConstants|ZrxVault).json"
"abis": "./generated-artifacts/@(IStaking|IStakingEvents|IStakingProxy|IStorage|IStorageInit|IStructs|IZrxVault|LibCobbDouglas|LibFixedMath|LibFixedMathRichErrors|LibProxy|LibSafeDowncast|LibStakingRichErrors|MixinAbstract|MixinConstants|MixinCumulativeRewards|MixinDeploymentConstants|MixinExchangeFees|MixinExchangeManager|MixinFinalizer|MixinParams|MixinScheduler|MixinStake|MixinStakeBalances|MixinStakeStorage|MixinStakingPool|MixinStakingPoolRewards|MixinStorage|Staking|StakingProxy|TestAssertStorageParams|TestCobbDouglas|TestCumulativeRewardTracking|TestDelegatorRewards|TestExchangeManager|TestFinalizer|TestInitTarget|TestLibFixedMath|TestLibProxy|TestLibProxyReceiver|TestLibSafeDowncast|TestMixinParams|TestMixinStake|TestMixinStakeStorage|TestMixinStakingPool|TestMixinStakingPoolRewards|TestProtocolFees|TestStaking|TestStakingNoWETH|TestStakingProxy|TestStorageLayoutAndConstants|ZrxVault).json"
},
"repository": {
"type": "git",

View File

@ -48,6 +48,7 @@ import * as TestMixinStake from '../generated-artifacts/TestMixinStake.json';
import * as TestMixinStakeBalances from '../generated-artifacts/TestMixinStakeBalances.json';
import * as TestMixinStakeStorage from '../generated-artifacts/TestMixinStakeStorage.json';
import * as TestMixinStakingPool from '../generated-artifacts/TestMixinStakingPool.json';
import * as TestMixinStakingPoolRewards from '../generated-artifacts/TestMixinStakingPoolRewards.json';
import * as TestProtocolFees from '../generated-artifacts/TestProtocolFees.json';
import * as TestProxyDestination from '../generated-artifacts/TestProxyDestination.json';
import * as TestStaking from '../generated-artifacts/TestStaking.json';
@ -101,6 +102,7 @@ export const artifacts = {
TestMixinStakeBalances: TestMixinStakeBalances as ContractArtifact,
TestMixinStakeStorage: TestMixinStakeStorage as ContractArtifact,
TestMixinStakingPool: TestMixinStakingPool as ContractArtifact,
TestMixinStakingPoolRewards: TestMixinStakingPoolRewards as ContractArtifact,
TestProtocolFees: TestProtocolFees as ContractArtifact,
TestProxyDestination: TestProxyDestination as ContractArtifact,
TestStaking: TestStaking as ContractArtifact,

View File

@ -46,6 +46,7 @@ export * from '../generated-wrappers/test_mixin_stake';
export * from '../generated-wrappers/test_mixin_stake_balances';
export * from '../generated-wrappers/test_mixin_stake_storage';
export * from '../generated-wrappers/test_mixin_staking_pool';
export * from '../generated-wrappers/test_mixin_staking_pool_rewards';
export * from '../generated-wrappers/test_protocol_fees';
export * from '../generated-wrappers/test_proxy_destination';
export * from '../generated-wrappers/test_staking';

View File

@ -0,0 +1,35 @@
import { blockchainTests, expect, Numberish } from '@0x/contracts-test-utils';
import { StakingRevertErrors } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
import { StoredBalance } from '../utils/types';
import {
artifacts,
TestMixinStakingPoolRewardsContract,
TestMixinStakingPoolRewardsEvents,
TestMixinStakingPoolRewardsUpdateCumulativeRewardEventArgs as UpdateCumulativeReward,
} from '../../src';
import { constants } from '../utils/constants';
blockchainTests.resets.only('MixinStakingPoolRewards unit tests', env => {
let testContract: TestMixinStakingPoolRewardsContract;
const INITIAL_EPOCH = 0;
const NEXT_EPOCH = 1;
before(async () => {
testContract = await TestMixinStakingPoolRewardsContract.deployFrom0xArtifactAsync(
artifacts.TestMixinStakingPoolRewards,
env.provider,
env.txDefaults,
artifacts,
);
});
describe('withdrawAndSyncDelegatorRewards()', () => {
it('poop', async () => {
// no-op
});
});
});

View File

@ -46,6 +46,7 @@
"generated-artifacts/TestMixinStakeBalances.json",
"generated-artifacts/TestMixinStakeStorage.json",
"generated-artifacts/TestMixinStakingPool.json",
"generated-artifacts/TestMixinStakingPoolRewards.json",
"generated-artifacts/TestProtocolFees.json",
"generated-artifacts/TestProxyDestination.json",
"generated-artifacts/TestStaking.json",