Add backstop tests

This commit is contained in:
Amir Bandeali
2019-10-06 13:17:02 -07:00
parent a7ef54dbff
commit 9e41c648dc
10 changed files with 93 additions and 6 deletions

View File

@@ -90,7 +90,7 @@ blockchainTests.resets('Catastrophe Tests', env => {
);
expect(undelegatedStakeBalance.currentEpochBalance).to.be.bignumber.equal(amountToStake);
});
it('should emit event when setting read-only mode', async () => {
it('should emit event and store correct configuration when setting read-only mode', async () => {
// set to read-only mode
const txReceipt = await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(
true,
@@ -98,6 +98,11 @@ blockchainTests.resets('Catastrophe Tests', env => {
expect(txReceipt.logs.length).to.be.equal(1);
const trueLog = txReceipt.logs[0] as LogWithDecodedArgs<StakingProxyReadOnlyModeSetEventArgs>;
expect(trueLog.args.readOnlyMode).to.be.true();
const timestamp = await env.web3Wrapper.getBlockTimestampAsync(txReceipt.blockNumber);
expect(trueLog.args.timestamp).to.bignumber.equal(timestamp);
const readOnlyState = await stakingApiWrapper.stakingProxyContract.readOnlyState.callAsync();
expect(readOnlyState[0]).to.be.true();
expect(readOnlyState[1]).to.bignumber.equal(timestamp);
});
});
});

View File

@@ -336,6 +336,17 @@ blockchainTests.resets('ZrxVault unit tests', env => {
expect(tx).to.revertWith(expectedError);
expect(await zrxVault.isInCatastrophicFailure.callAsync()).to.be.false();
});
it('Catastrophic Failure Mode can only be turned on once', async () => {
const authorized = nonOwnerAddresses[0];
await zrxVault.addAuthorizedAddress.awaitTransactionSuccessAsync(authorized, { from: owner });
await zrxVault.enterCatastrophicFailure.awaitTransactionSuccessAsync({
from: authorized,
});
const expectedError = new StakingRevertErrors.OnlyCallableIfNotInCatastrophicFailureError();
return expect(zrxVault.enterCatastrophicFailure.awaitTransactionSuccessAsync()).to.revertWith(
expectedError,
);
});
});
describe('Affected functionality', () => {

View File

@@ -15,6 +15,7 @@ import {
TestCobbDouglasContract,
TestStakingContract,
TestStakingEvents,
ZrxVaultBackstopContract,
ZrxVaultContract,
} from '../../src';
@@ -32,6 +33,7 @@ export class StakingApiWrapper {
public zrxTokenContract: DummyERC20TokenContract;
public wethContract: WETH9Contract;
public cobbDouglasContract: TestCobbDouglasContract;
public zrxVaultBackstopContract: ZrxVaultBackstopContract;
public utils = {
// Epoch Utils
fastForwardToNextEpochAsync: async (): Promise<void> => {
@@ -174,13 +176,14 @@ export class StakingApiWrapper {
zrxTokenContract: DummyERC20TokenContract,
wethContract: WETH9Contract,
cobbDouglasContract: TestCobbDouglasContract,
zrxVaultBackstopContract: ZrxVaultBackstopContract,
) {
this._web3Wrapper = env.web3Wrapper;
this.zrxVaultContract = zrxVaultContract;
this.zrxTokenContract = zrxTokenContract;
this.wethContract = wethContract;
this.cobbDouglasContract = cobbDouglasContract;
this.zrxVaultBackstopContract = zrxVaultBackstopContract;
this.stakingContractAddress = stakingContract.address;
this.stakingProxyContract = stakingProxyContract;
// disguise the staking proxy as a StakingContract
@@ -274,10 +277,23 @@ export async function deployAndConfigureContractsAsync(
artifacts,
);
const zrxVaultBackstopContract = await ZrxVaultBackstopContract.deployFrom0xArtifactAsync(
artifacts.ZrxVaultBackstop,
env.provider,
env.txDefaults,
artifacts,
stakingProxyContract.address,
zrxVaultContract.address,
);
// configure erc20 proxy to accept calls from zrx vault
await erc20ProxyContract.addAuthorizedAddress.awaitTransactionSuccessAsync(zrxVaultContract.address);
// set staking proxy contract in zrx vault
await zrxVaultContract.setStakingProxy.awaitTransactionSuccessAsync(stakingProxyContract.address);
// add zrxVaultBackstop as an authorized address
await zrxVaultContract.addAuthorizedAddress.awaitTransactionSuccessAsync(zrxVaultBackstopContract.address, {
from: ownerAddress,
});
return new StakingApiWrapper(
env,
ownerAddress,
@@ -287,5 +303,6 @@ export async function deployAndConfigureContractsAsync(
zrxTokenContract,
wethContract,
cobbDouglasContract,
zrxVaultBackstopContract,
);
}

View File

@@ -0,0 +1,50 @@
import { ERC20Wrapper } from '@0x/contracts-asset-proxy';
import { blockchainTests, describe, expect, increaseTimeAndMineBlockAsync } from '@0x/contracts-test-utils';
import { StringRevertError } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import { ZrxVaultInCatastrophicFailureModeEventArgs } from '../src';
import { deployAndConfigureContractsAsync, StakingApiWrapper } from './utils/api_wrapper';
blockchainTests.resets('ZrxVaultBackstop', env => {
let stakingApiWrapper: StakingApiWrapper;
let authorizedAddress: string;
let notAuthorizedAddress: string;
before(async () => {
const accounts = await env.web3Wrapper.getAvailableAddressesAsync();
[authorizedAddress, notAuthorizedAddress] = accounts;
const erc20Wrapper = new ERC20Wrapper(env.provider, [], authorizedAddress);
stakingApiWrapper = await deployAndConfigureContractsAsync(env, authorizedAddress, erc20Wrapper);
});
describe('enterCatastrophicFailureIfProlongedReadOnlyMode', () => {
it('should revert if read-only mode is not set', async () => {
const expectedError = new StringRevertError('READ_ONLY_MODE_NOT_SET');
expect(
stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(),
).to.revertWith(expectedError);
});
it('should revert if read-only mode has been set for less than 40 days', async () => {
await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(true, {
from: authorizedAddress,
});
const expectedError = new StringRevertError('READ_ONLY_MODE_DURATION_TOO_SHORT');
expect(
stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(),
).to.revertWith(expectedError);
});
it('should enter catastophic failure mode if read-only mode has been set for 40 days', async () => {
await stakingApiWrapper.stakingProxyContract.setReadOnlyMode.awaitTransactionSuccessAsync(true, {
from: authorizedAddress,
});
const fourtyDaysInSec = 40 * 24 * 60 * 60;
await increaseTimeAndMineBlockAsync(fourtyDaysInSec);
const txReceipt = await stakingApiWrapper.zrxVaultBackstopContract.enterCatastrophicFailureIfProlongedReadOnlyMode.awaitTransactionSuccessAsync(
{ from: notAuthorizedAddress },
);
expect(txReceipt.logs.length).to.equal(1);
const logArgs = (txReceipt.logs[0] as LogWithDecodedArgs<ZrxVaultInCatastrophicFailureModeEventArgs>).args;
expect(logArgs.sender).to.equal(stakingApiWrapper.zrxVaultBackstopContract.address);
});
});
});