Rich reverts in tests plus readability

This commit is contained in:
Greg Hysen 2019-09-04 00:17:12 -07:00
parent 293c428186
commit 4bc84cd526
6 changed files with 37 additions and 32 deletions

View File

@ -247,8 +247,8 @@ contract MixinExchangeFees is
initialContractBalance, initialContractBalance,
activePools[i].feesCollected, activePools[i].feesCollected,
totalFeesCollected, totalFeesCollected,
activePools[i].weightedStake != 0 ? activePools[i].weightedStake : 1, totalWeightedStake != 0 ? activePools[i].weightedStake : 1, // only rewards are accounted for if no one has staked
totalWeightedStake != 0 ? totalWeightedStake : 1, totalWeightedStake != 0 ? totalWeightedStake : 1 // this is to avoid divide-by-zero in cobb douglas
cobbDouglasAlphaNumerator, cobbDouglasAlphaNumerator,
cobbDouglasAlphaDenomintor cobbDouglasAlphaDenomintor
); );

View File

@ -62,10 +62,14 @@ export class FinalizerActor extends BaseActor {
await this._stakingWrapper.skipToNextEpochAsync(); await this._stakingWrapper.skipToNextEpochAsync();
// assert reward vault changes // assert reward vault changes
const finalRewardVaultBalanceByPoolId = await this._getRewardVaultBalanceByPoolIdAsync(this._poolIds); const finalRewardVaultBalanceByPoolId = await this._getRewardVaultBalanceByPoolIdAsync(this._poolIds);
expect(finalRewardVaultBalanceByPoolId).to.be.deep.equal(expectedRewardVaultBalanceByPoolId); expect(finalRewardVaultBalanceByPoolId, 'final pool balances in reward vault').to.be.deep.equal(
expectedRewardVaultBalanceByPoolId,
);
// assert member balances // assert member balances
const finalMemberBalancesByPoolId = await this._getMemberBalancesByPoolIdAsync(this._membersByPoolId); const finalMemberBalancesByPoolId = await this._getMemberBalancesByPoolIdAsync(this._membersByPoolId);
expect(finalMemberBalancesByPoolId).to.be.deep.equal(expectedMemberBalancesByPoolId); expect(finalMemberBalancesByPoolId, 'final delegator balances in reward vault').to.be.deep.equal(
expectedMemberBalancesByPoolId,
);
} }
private async _computeExpectedMemberBalancesByPoolIdAsync( private async _computeExpectedMemberBalancesByPoolIdAsync(

View File

@ -21,7 +21,7 @@ export class StakerActor extends BaseActor {
// deposit stake // deposit stake
const txReceiptPromise = this._stakingWrapper.stakeAsync(this._owner, amount); const txReceiptPromise = this._stakingWrapper.stakeAsync(this._owner, amount);
if (revertError !== undefined) { if (revertError !== undefined) {
await expect(txReceiptPromise).to.revertWith(revertError); await expect(txReceiptPromise, 'expected revert error').to.revertWith(revertError);
return; return;
} }
await txReceiptPromise; await txReceiptPromise;
@ -35,7 +35,9 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances); await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault // check zrx balance of vault
const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync(); const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync();
expect(finalZrxBalanceOfVault).to.be.bignumber.equal(initZrxBalanceOfVault.plus(amount)); expect(finalZrxBalanceOfVault, 'final balance of zrx vault').to.be.bignumber.equal(
initZrxBalanceOfVault.plus(amount),
);
} }
public async unstakeAsync(amount: BigNumber, revertError?: RevertError): Promise<void> { public async unstakeAsync(amount: BigNumber, revertError?: RevertError): Promise<void> {
@ -44,7 +46,7 @@ export class StakerActor extends BaseActor {
// deposit stake // deposit stake
const txReceiptPromise = this._stakingWrapper.unstakeAsync(this._owner, amount); const txReceiptPromise = this._stakingWrapper.unstakeAsync(this._owner, amount);
if (revertError !== undefined) { if (revertError !== undefined) {
await expect(txReceiptPromise).to.revertWith(revertError); await expect(txReceiptPromise, 'expected revert error').to.revertWith(revertError);
return; return;
} }
await txReceiptPromise; await txReceiptPromise;
@ -61,7 +63,9 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances); await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault // check zrx balance of vault
const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync(); const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync();
expect(finalZrxBalanceOfVault).to.be.bignumber.equal(initZrxBalanceOfVault.minus(amount)); expect(finalZrxBalanceOfVault, 'final balance of zrx vault').to.be.bignumber.equal(
initZrxBalanceOfVault.minus(amount),
);
} }
public async moveStakeAsync( public async moveStakeAsync(
@ -134,7 +138,7 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances); await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault // check zrx balance of vault
const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync(); const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync();
expect(finalZrxBalanceOfVault).to.be.bignumber.equal(initZrxBalanceOfVault); expect(finalZrxBalanceOfVault, 'final balance of zrx vault').to.be.bignumber.equal(initZrxBalanceOfVault);
} }
public async goToNextEpochAsync(): Promise<void> { public async goToNextEpochAsync(): Promise<void> {
@ -148,7 +152,7 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances); await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault // check zrx balance of vault
const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync(); const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync();
expect(finalZrxBalanceOfVault).to.be.bignumber.equal(initZrxBalanceOfVault); expect(finalZrxBalanceOfVault, 'final balance of zrx vault').to.be.bignumber.equal(initZrxBalanceOfVault);
} }
public getNextEpochBalances(balances: StakeBalances): StakeBalances { public getNextEpochBalances(balances: StakeBalances): StakeBalances {

View File

@ -11,7 +11,7 @@ import { MembersByPoolId, OperatorByPoolId, StakeState } from './utils/types';
// tslint:disable:no-unnecessary-type-assertion // tslint:disable:no-unnecessary-type-assertion
// tslint:disable:max-file-line-count // tslint:disable:max-file-line-count
blockchainTests.resets.only('Testing Rewards', () => { blockchainTests.resets('Testing Rewards', () => {
// constants // constants
const ZRX_TOKEN_DECIMALS = new BigNumber(18); const ZRX_TOKEN_DECIMALS = new BigNumber(18);
// tokens & addresses // tokens & addresses

View File

@ -1,7 +1,8 @@
import { ERC20ProxyContract, ERC20Wrapper } from '@0x/contracts-asset-proxy'; import { ERC20ProxyContract, ERC20Wrapper } from '@0x/contracts-asset-proxy';
import { DummyERC20TokenContract } from '@0x/contracts-erc20'; import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { blockchainTests, describe, provider, web3Wrapper } from '@0x/contracts-test-utils'; import { blockchainTests, describe, provider, web3Wrapper } from '@0x/contracts-test-utils';
import { BigNumber, StringRevertError } from '@0x/utils'; import { StakingRevertErrors } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { StakerActor } from './actors/staker_actor'; import { StakerActor } from './actors/staker_actor';
@ -9,7 +10,7 @@ import { StakingWrapper } from './utils/staking_wrapper';
import { StakeState, StakeStateInfo } from './utils/types'; import { StakeState, StakeStateInfo } from './utils/types';
// tslint:disable:no-unnecessary-type-assertion // tslint:disable:no-unnecessary-type-assertion
blockchainTests.resets.only('Stake States', () => { blockchainTests.resets('Stake States', () => {
// constants // constants
const ZRX_TOKEN_DECIMALS = new BigNumber(18); const ZRX_TOKEN_DECIMALS = new BigNumber(18);
const ZERO = new BigNumber(0); const ZERO = new BigNumber(0);
@ -94,7 +95,7 @@ blockchainTests.resets.only('Stake States', () => {
{ state: StakeState.Active }, { state: StakeState.Active },
{ state: StakeState.Inactive }, { state: StakeState.Inactive },
amount, amount,
new StringRevertError('Insufficient Balance'), new StakingRevertErrors.InsufficientBalanceError(amount, ZERO),
); );
}); });
it('should fail to reassign stake', async () => { it('should fail to reassign stake', async () => {
@ -113,7 +114,7 @@ blockchainTests.resets.only('Stake States', () => {
{ state: StakeState.Inactive }, { state: StakeState.Inactive },
{ state: StakeState.Active }, { state: StakeState.Active },
amount, amount,
new StringRevertError('Insufficient Balance'), new StakingRevertErrors.InsufficientBalanceError(amount, ZERO),
); );
}); });
}); });
@ -254,20 +255,20 @@ blockchainTests.resets.only('Stake States', () => {
it('should fail to unstake with insufficient balance', async () => { it('should fail to unstake with insufficient balance', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
await staker.stakeAsync(amount); await staker.stakeAsync(amount);
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
it('should fail to unstake in the same epoch as stake was set to inactive', async () => { it('should fail to unstake in the same epoch as stake was set to inactive', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
await staker.stakeAsync(amount); await staker.stakeAsync(amount);
await staker.moveStakeAsync({ state: StakeState.Active }, { state: StakeState.Inactive }, amount); await staker.moveStakeAsync({ state: StakeState.Active }, { state: StakeState.Inactive }, amount);
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
it('should fail to unstake after being inactive for <1 epoch', async () => { it('should fail to unstake after being inactive for <1 epoch', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
await staker.stakeAsync(amount); await staker.stakeAsync(amount);
await staker.moveStakeAsync({ state: StakeState.Active }, { state: StakeState.Inactive }, amount); await staker.moveStakeAsync({ state: StakeState.Active }, { state: StakeState.Inactive }, amount);
await staker.goToNextEpochAsync(); await staker.goToNextEpochAsync();
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
it('should fail to unstake in same epoch that inactive/withdrawable stake has been reactivated', async () => { it('should fail to unstake in same epoch that inactive/withdrawable stake has been reactivated', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
@ -276,7 +277,7 @@ blockchainTests.resets.only('Stake States', () => {
await staker.goToNextEpochAsync(); // stake is now inactive await staker.goToNextEpochAsync(); // stake is now inactive
await staker.goToNextEpochAsync(); // stake is now withdrawable await staker.goToNextEpochAsync(); // stake is now withdrawable
await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount); await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount);
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
it('should fail to unstake one epoch after inactive/withdrawable stake has been reactivated', async () => { it('should fail to unstake one epoch after inactive/withdrawable stake has been reactivated', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
@ -286,7 +287,7 @@ blockchainTests.resets.only('Stake States', () => {
await staker.goToNextEpochAsync(); // stake is now withdrawable await staker.goToNextEpochAsync(); // stake is now withdrawable
await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount); await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount);
await staker.goToNextEpochAsync(); // stake is active and not withdrawable await staker.goToNextEpochAsync(); // stake is active and not withdrawable
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
it('should fail to unstake >1 epoch after inactive/withdrawable stake has been reactivated', async () => { it('should fail to unstake >1 epoch after inactive/withdrawable stake has been reactivated', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10); const amount = StakingWrapper.toBaseUnitAmount(10);
@ -297,7 +298,7 @@ blockchainTests.resets.only('Stake States', () => {
await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount); await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount);
await staker.goToNextEpochAsync(); // stake is active and not withdrawable await staker.goToNextEpochAsync(); // stake is active and not withdrawable
await staker.goToNextEpochAsync(); // stake is active and not withdrawable await staker.goToNextEpochAsync(); // stake is active and not withdrawable
await staker.unstakeAsync(amount, new StringRevertError('INSUFFICIENT_FUNDS')); await staker.unstakeAsync(amount, new StakingRevertErrors.InsufficientBalanceError(amount, ZERO));
}); });
}); });
describe('Simulations', () => { describe('Simulations', () => {

View File

@ -194,28 +194,24 @@ export class StakingWrapper {
public async moveStakeAsync( public async moveStakeAsync(
owner: string, owner: string,
_fromStatus: { _fromStatus: {
status: number, status: number;
poolId?: string poolId?: string;
}, },
_toStatus: { _toStatus: {
status: number, status: number;
poolId?: string poolId?: string;
}, },
amount: BigNumber, amount: BigNumber,
): Promise<TransactionReceiptWithDecodedLogs> { ): Promise<TransactionReceiptWithDecodedLogs> {
const fromStatus = { const fromStatus = {
status: _fromStatus.status, status: _fromStatus.status,
poolId: _fromStatus.poolId !== undefined ? _fromStatus.poolId : constants.NIL_POOL_ID poolId: _fromStatus.poolId !== undefined ? _fromStatus.poolId : constants.NIL_POOL_ID,
}; };
const toStatus = { const toStatus = {
status: _toStatus.status, status: _toStatus.status,
poolId: _toStatus.poolId !== undefined ? _toStatus.poolId : constants.NIL_POOL_ID poolId: _toStatus.poolId !== undefined ? _toStatus.poolId : constants.NIL_POOL_ID,
}; };
const calldata = this.getStakingContract().moveStake.getABIEncodedTransactionData( const calldata = this.getStakingContract().moveStake.getABIEncodedTransactionData(fromStatus, toStatus, amount);
fromStatus,
toStatus,
amount,
);
const txReceipt = await this._executeTransactionAsync(calldata, owner); const txReceipt = await this._executeTransactionAsync(calldata, owner);
return txReceipt; return txReceipt;
} }