Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Rich reverts in tests plus readability
Browse files Browse the repository at this point in the history
  • Loading branch information
hysz committed Sep 5, 2019
1 parent d0487b3 commit 369ffc5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 35 deletions.
4 changes: 2 additions & 2 deletions contracts/staking/contracts/src/fees/MixinExchangeFees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ contract MixinExchangeFees is
initialContractBalance,
activePools[i].feesCollected,
totalFeesCollected,
totalWeightedStake != 0 ? activePools[i].weightedStake : 1,
totalWeightedStake != 0 ? totalWeightedStake : 1
totalWeightedStake != 0 ? activePools[i].weightedStake : 1, // only rewards are accounted for if no one has staked
totalWeightedStake != 0 ? totalWeightedStake : 1 // this is to avoid divide-by-zero in cobb douglas
);

// record reward in vault
Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/contracts/test/LibFeeMathTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

pragma solidity ^0.5.5;
pragma solidity ^0.5.9;


contract LibFeeMathTest {
Expand Down
8 changes: 6 additions & 2 deletions contracts/staking/test/actors/finalizer_actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ export class FinalizerActor extends BaseActor {
await this._stakingWrapper.skipToNextEpochAsync();
// assert reward vault changes
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
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(
Expand Down
16 changes: 10 additions & 6 deletions contracts/staking/test/actors/staker_actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class StakerActor extends BaseActor {
// deposit stake
const txReceiptPromise = this._stakingWrapper.stakeAsync(this._owner, amount);
if (revertError !== undefined) {
await expect(txReceiptPromise).to.revertWith(revertError);
await expect(txReceiptPromise, 'expected revert error').to.revertWith(revertError);
return;
}
await txReceiptPromise;
Expand All @@ -35,7 +35,9 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault
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> {
Expand All @@ -44,7 +46,7 @@ export class StakerActor extends BaseActor {
// deposit stake
const txReceiptPromise = this._stakingWrapper.unstakeAsync(this._owner, amount);
if (revertError !== undefined) {
await expect(txReceiptPromise).to.revertWith(revertError);
await expect(txReceiptPromise, 'expected revert error').to.revertWith(revertError);
return;
}
await txReceiptPromise;
Expand All @@ -61,7 +63,9 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault
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(
Expand Down Expand Up @@ -134,7 +138,7 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault
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> {
Expand All @@ -148,7 +152,7 @@ export class StakerActor extends BaseActor {
await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault
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 {
Expand Down
5 changes: 3 additions & 2 deletions contracts/staking/test/math_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ERC20ProxyContract, ERC20Wrapper } from '@0x/contracts-asset-proxy';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { chaiSetup, provider, web3Wrapper } from '@0x/contracts-test-utils';
import { blockchainTests, chaiSetup, provider, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
Expand All @@ -12,7 +12,8 @@ chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
// tslint:disable:no-unnecessary-type-assertion
describe('Math Libraries', () => {
// @TODO: This is deprecated by New Fee Math
blockchainTests.skip('Math Libraries', () => {
// constants
const ZRX_TOKEN_DECIMALS = new BigNumber(18);
// tokens & addresses
Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/test/rewards_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MembersByPoolId, OperatorByPoolId, StakeState } from './utils/types';

// tslint:disable:no-unnecessary-type-assertion
// tslint:disable:max-file-line-count
blockchainTests.resets.only('Testing Rewards', () => {
blockchainTests.resets('Testing Rewards', () => {
// constants
const ZRX_TOKEN_DECIMALS = new BigNumber(18);
// tokens & addresses
Expand Down
21 changes: 11 additions & 10 deletions contracts/staking/test/stake_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ERC20ProxyContract, ERC20Wrapper } from '@0x/contracts-asset-proxy';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
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 { StakerActor } from './actors/staker_actor';
import { StakingWrapper } from './utils/staking_wrapper';
import { StakeState, StakeStateInfo } from './utils/types';

// tslint:disable:no-unnecessary-type-assertion
blockchainTests.resets.only('Stake States', () => {
blockchainTests.resets('Stake States', () => {
// constants
const ZRX_TOKEN_DECIMALS = new BigNumber(18);
const ZERO = new BigNumber(0);
Expand Down Expand Up @@ -94,7 +95,7 @@ blockchainTests.resets.only('Stake States', () => {
{ state: StakeState.Active },
{ state: StakeState.Inactive },
amount,
new StringRevertError('Insufficient Balance'),
new StakingRevertErrors.InsufficientBalanceError(amount, ZERO),
);
});
it('should fail to reassign stake', async () => {
Expand All @@ -113,7 +114,7 @@ blockchainTests.resets.only('Stake States', () => {
{ state: StakeState.Inactive },
{ state: StakeState.Active },
amount,
new StringRevertError('Insufficient Balance'),
new StakingRevertErrors.InsufficientBalanceError(amount, ZERO),
);
});
});
Expand Down Expand Up @@ -254,20 +255,20 @@ blockchainTests.resets.only('Stake States', () => {
it('should fail to unstake with insufficient balance', async () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
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 () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
await staker.stakeAsync(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 () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
await staker.stakeAsync(amount);
await staker.moveStakeAsync({ state: StakeState.Active }, { state: StakeState.Inactive }, amount);
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 () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
Expand All @@ -276,7 +277,7 @@ blockchainTests.resets.only('Stake States', () => {
await staker.goToNextEpochAsync(); // stake is now inactive
await staker.goToNextEpochAsync(); // stake is now withdrawable
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 () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
Expand All @@ -286,7 +287,7 @@ blockchainTests.resets.only('Stake States', () => {
await staker.goToNextEpochAsync(); // stake is now withdrawable
await staker.moveStakeAsync({ state: StakeState.Inactive }, { state: StakeState.Active }, amount);
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 () => {
const amount = StakingWrapper.toBaseUnitAmount(10);
Expand All @@ -297,7 +298,7 @@ blockchainTests.resets.only('Stake States', () => {
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));
});
});
describe('Simulations', () => {
Expand Down
18 changes: 7 additions & 11 deletions contracts/staking/test/utils/staking_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,28 +207,24 @@ export class StakingWrapper {
public async moveStakeAsync(
owner: string,
_fromStatus: {
status: number,
poolId?: string
status: number;
poolId?: string;
},
_toStatus: {
status: number,
poolId?: string
status: number;
poolId?: string;
},
amount: BigNumber,
): Promise<TransactionReceiptWithDecodedLogs> {
const fromStatus = {
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 = {
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(
fromStatus,
toStatus,
amount,
);
const calldata = this.getStakingContract().moveStake.getABIEncodedTransactionData(fromStatus, toStatus, amount);
const txReceipt = await this._executeTransactionAsync(calldata, owner);
return txReceipt;
}
Expand Down

0 comments on commit 369ffc5

Please sign in to comment.