Merge pull request #2389 from 0xProject/feat/contracts/mainnet-fork

Allow mainnet fork to be used for contract tests
This commit is contained in:
Amir Bandeali
2019-12-11 22:50:09 -08:00
committed by GitHub
9 changed files with 273 additions and 41 deletions

View File

@@ -18,6 +18,7 @@ export type BlockchainSuiteCallback = (this: ISuiteCallbackContext, env: Blockch
export type BlockchainContextDefinitionCallback<T> = (description: string, callback: BlockchainSuiteCallback) => T;
export interface ContextDefinition extends mocha.IContextDefinition {
optional: ContextDefinitionCallback<ISuite | void>;
fork: ContextDefinitionCallback<ISuite | void>;
}
/**
@@ -31,6 +32,7 @@ interface BlockchainContextDefinitionPartial {
only: BlockchainContextDefinitionCallback<ISuite>;
skip: BlockchainContextDefinitionCallback<void>;
optional: BlockchainContextDefinitionCallback<ISuite | void>;
fork: BlockchainContextDefinitionCallback<ISuite | void>;
(description: string, callback: BlockchainSuiteCallback): ISuite;
}
@@ -97,6 +99,10 @@ export const describe = _.assign(mochaDescribe, {
const describeCall = process.env.TEST_ALL ? mochaDescribe : mochaDescribe.skip;
return describeCall(description, callback);
},
fork(description: string, callback: SuiteCallback): ISuite | void {
const describeCall = process.env.FORK_RPC_URL ? mochaDescribe.only : mochaDescribe.skip;
return describeCall(description, callback);
},
}) as ContextDefinition;
/**
@@ -116,6 +122,13 @@ export const blockchainTests: BlockchainContextDefinition = _.assign(
optional(description: string, callback: BlockchainSuiteCallback): ISuite | void {
return defineBlockchainSuite(description, callback, process.env.TEST_ALL ? describe : describe.skip);
},
fork(description: string, callback: BlockchainSuiteCallback): ISuite | void {
return defineBlockchainSuite(
description,
callback,
process.env.FORK_RPC_URL ? describe.only : describe.skip,
);
},
resets: _.assign(
function(description: string, callback: BlockchainSuiteCallback): ISuite {
return defineBlockchainSuite(description, callback, function(
@@ -150,6 +163,14 @@ export const blockchainTests: BlockchainContextDefinition = _.assign(
return defineResetsSuite(_description, _callback, describe.optional);
});
},
fork(description: string, callback: BlockchainSuiteCallback): ISuite | void {
return defineBlockchainSuite(description, callback, function(
_description: string,
_callback: SuiteCallback,
): ISuite | void {
return defineResetsSuite(_description, _callback, describe.fork);
});
},
},
),
},

View File

@@ -1,4 +1,4 @@
import { devConstants, env, EnvVars, web3Factory } from '@0x/dev-utils';
import { devConstants, env, EnvVars, Web3Config, web3Factory } from '@0x/dev-utils';
import { prependSubprovider, Web3ProviderEngine } from '@0x/subproviders';
import { logUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
@@ -9,47 +9,31 @@ import { coverage } from './coverage';
import { profiler } from './profiler';
import { revertTrace } from './revert_trace';
enum ProviderType {
Ganache = 'ganache',
Geth = 'geth',
}
let testProvider: ProviderType;
switch (process.env.TEST_PROVIDER) {
case undefined:
testProvider = ProviderType.Ganache;
break;
case 'ganache':
testProvider = ProviderType.Ganache;
break;
case 'geth':
testProvider = ProviderType.Geth;
break;
default:
throw new Error(`Unknown TEST_PROVIDER: ${process.env.TEST_PROVIDER}`);
}
const ganacheTxDefaults = {
export const txDefaults = {
from: devConstants.TESTRPC_FIRST_ADDRESS,
gas: devConstants.GAS_LIMIT,
gasPrice: constants.DEFAULT_GAS_PRICE,
};
const gethTxDefaults = {
from: devConstants.TESTRPC_FIRST_ADDRESS,
};
export const txDefaults = testProvider === ProviderType.Ganache ? ganacheTxDefaults : gethTxDefaults;
const gethConfigs = {
shouldUseInProcessGanache: false,
rpcUrl: 'http://localhost:8501',
shouldUseFakeGasEstimate: false,
};
const ganacheConfigs = {
let providerConfigs: Web3Config = {
total_accounts: constants.NUM_TEST_ACCOUNTS,
shouldUseInProcessGanache: true,
shouldAllowUnlimitedContractSize: true,
};
const providerConfigs = testProvider === ProviderType.Ganache ? ganacheConfigs : gethConfigs;
if (process.env.FORK_RPC_URL !== undefined) {
providerConfigs = {
...providerConfigs,
fork: process.env.FORK_RPC_URL,
blockTime: 0,
// ZeroExGovernor signer addresses
unlocked_accounts: [
'0x257619b7155d247e43c8b6d90c8c17278ae481f0',
'0x5ee2a00f8f01d099451844af7f894f26a57fcbf2',
'0x894d623e0e0e8ed12c4a73dada999e275684a37d',
],
};
}
export const provider: Web3ProviderEngine = web3Factory.getRpcProvider(providerConfigs);
provider.stop();