protocol/contracts/staking/test/unit_tests/mixin_scheduler_test.ts
F. Eugene Aumson f11d8a5bd8
@0x/order-utils refactors for v3: orderParsingUtils, signatureUtils, orderHashUtils, RevertErrors, transactionHashUtils (#2321)
* move orderParsingUtils from order-utils to connect

* Remove many functions from signatureUtils

Removed from the exported object, that is.  All of them are used in
other existing code, so they were all moved to be as local to their
usage as possible.

* remove orderHashUtils.isValidOrderHash()

* Move all *RevertErrors from order-utils...

...into their respective @0x/contracts- packages.

* Refactor @0x/order-utils' orderHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed getOrderHash()
method in DevUtils.

* Move all *RevertErrors from @0x/utils...

...into their respective @0x/contracts- packages.

* rm transactionHashUtils.isValidTransactionHash()

* DevUtils.sol: Fail yarn test if too big to deploy

* Refactor @0x/order-utils transactionHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed
getTransactionHash() method in DevUtils.

* Consolidate `Removed export...` CHANGELOG entries

* Rm EthBalanceChecker from devutils wrapper exports

* Stop importing from '.' or '.../src'

* fix builds

* fix prettier; dangling promise

* increase max bundle size
2019-11-14 17:14:24 -05:00

110 lines
5.5 KiB
TypeScript

import { blockchainTests, constants, expect, verifyEventsFromLogs } from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import { constants as stakingConstants } from '../../src/constants';
import StakingRevertErrors = require('../../src/staking_revert_errors');
import { artifacts } from '../artifacts';
import {
TestMixinSchedulerContract,
TestMixinSchedulerEvents,
TestMixinSchedulerGoToNextEpochTestInfoEventArgs,
} from '../wrappers';
blockchainTests.resets('MixinScheduler unit tests', env => {
let testContract: TestMixinSchedulerContract;
before(async () => {
// Deploy contracts
testContract = await TestMixinSchedulerContract.deployFrom0xArtifactAsync(
artifacts.TestMixinScheduler,
env.provider,
env.txDefaults,
artifacts,
stakingConstants.NIL_ADDRESS,
stakingConstants.NIL_ADDRESS,
);
});
describe('getCurrentEpochEarliestEndTimeInSeconds', () => {
it('Should return the sum of `epoch start time + epoch duration`', async () => {
const testDeployedTimestamp = await testContract.testDeployedTimestamp().callAsync();
const epochDurationInSeconds = await testContract.epochDurationInSeconds().callAsync();
const expectedCurrentEpochEarliestEndTimeInSeconds = testDeployedTimestamp.plus(epochDurationInSeconds);
const currentEpochEarliestEndTimeInSeconds = await testContract
.getCurrentEpochEarliestEndTimeInSeconds()
.callAsync();
expect(currentEpochEarliestEndTimeInSeconds).to.bignumber.equal(
expectedCurrentEpochEarliestEndTimeInSeconds,
);
});
});
describe('_initMixinScheduler', () => {
it('Should succeed if scheduler is not yet initialized (`currentEpochStartTimeInSeconds == 0`)', async () => {
const initCurrentEpochStartTimeInSeconds = constants.ZERO_AMOUNT;
const txReceipt = await testContract
.initMixinSchedulerTest(initCurrentEpochStartTimeInSeconds)
.awaitTransactionSuccessAsync();
// Assert `currentEpochStartTimeInSeconds` was properly initialized
const blockTimestamp = await env.web3Wrapper.getBlockTimestampAsync(txReceipt.blockNumber);
const currentEpochStartTimeInSeconds = await testContract.currentEpochStartTimeInSeconds().callAsync();
expect(currentEpochStartTimeInSeconds).to.bignumber.equal(blockTimestamp);
// Assert `currentEpoch` was properly initialized
const currentEpoch = await testContract.currentEpoch().callAsync();
expect(currentEpoch).to.bignumber.equal(1);
});
it('Should revert if scheduler is already initialized (`currentEpochStartTimeInSeconds != 0`)', async () => {
const initCurrentEpochStartTimeInSeconds = new BigNumber(10);
const tx = testContract
.initMixinSchedulerTest(initCurrentEpochStartTimeInSeconds)
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(
new StakingRevertErrors.InitializationError(
StakingRevertErrors.InitializationErrorCodes.MixinSchedulerAlreadyInitialized,
),
);
});
});
describe('_goToNextEpoch', () => {
it('Should succeed if epoch end time is strictly less than to block timestamp', async () => {
const epochEndTimeDelta = new BigNumber(-10);
const txReceipt = await testContract.goToNextEpochTest(epochEndTimeDelta).awaitTransactionSuccessAsync();
const currentEpoch = await testContract.currentEpoch().callAsync();
const currentEpochStartTimeInSeconds = await testContract.currentEpochStartTimeInSeconds().callAsync();
verifyEventsFromLogs(
txReceipt.logs,
[
{
oldEpoch: currentEpoch.minus(1),
blockTimestamp: currentEpochStartTimeInSeconds,
},
],
TestMixinSchedulerEvents.GoToNextEpochTestInfo,
);
});
it('Should succeed if epoch end time is equal to block timestamp', async () => {
const epochEndTimeDelta = constants.ZERO_AMOUNT;
const txReceipt = await testContract.goToNextEpochTest(epochEndTimeDelta).awaitTransactionSuccessAsync();
// tslint:disable-next-line no-unnecessary-type-assertion
const testLog: TestMixinSchedulerGoToNextEpochTestInfoEventArgs = (txReceipt.logs[0] as LogWithDecodedArgs<
TestMixinSchedulerGoToNextEpochTestInfoEventArgs
>).args;
const currentEpoch = await testContract.currentEpoch().callAsync();
const currentEpochStartTimeInSeconds = await testContract.currentEpochStartTimeInSeconds().callAsync();
expect(currentEpoch).to.bignumber.equal(testLog.oldEpoch.plus(1));
expect(currentEpochStartTimeInSeconds).to.bignumber.equal(testLog.blockTimestamp);
});
it('Should revert if epoch end time is strictly greater than block timestamp', async () => {
const epochEndTimeDelta = new BigNumber(10);
const tx = testContract.goToNextEpochTest(epochEndTimeDelta).awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(new StakingRevertErrors.BlockTimestampTooLowError());
});
});
});