Update 0x.js to run tests against in-process ganache

This commit is contained in:
Fabio Berger 2018-04-04 16:31:57 +09:00
parent 5e4e27fed5
commit 43e07e7ce3
11 changed files with 60 additions and 19 deletions

View File

@ -21,7 +21,7 @@
"test": "run-s clean test:commonjs", "test": "run-s clean test:commonjs",
"test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov",
"coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info",
"update_contracts": "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;", "update_contracts": "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/src/artifacts/$i.json test/artifacts; done;",
"clean": "shx rm -rf _bundles lib test_temp scripts", "clean": "shx rm -rf _bundles lib test_temp scripts",
"build:umd:prod": "NODE_ENV=production webpack", "build:umd:prod": "NODE_ENV=production webpack",
"build:commonjs": "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "build:commonjs": "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts",
@ -33,7 +33,7 @@
"upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json" "upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json"
}, },
"config": { "config": {
"artifacts": "TokenTransferProxy Exchange TokenRegistry Token EtherToken", "artifacts": "Exchange DummyToken ZRXToken Token WETH9 TokenTransferProxy MultiSigWallet MultiSigWalletWithTimeLock MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress MaliciousToken TokenRegistry Arbitrage EtherDelta AccountLevels",
"postpublish": { "postpublish": {
"assets": [ "assets": [
"packages/0x.js/_bundles/index.js", "packages/0x.js/_bundles/index.js",
@ -60,8 +60,10 @@
"node": ">=6.0.0" "node": ">=6.0.0"
}, },
"devDependencies": { "devDependencies": {
"@0xproject/deployer": "^0.3.5",
"@0xproject/dev-utils": "^0.3.4", "@0xproject/dev-utils": "^0.3.4",
"@0xproject/monorepo-scripts": "^0.1.16", "@0xproject/monorepo-scripts": "^0.1.16",
"@0xproject/subproviders": "^0.8.4",
"@0xproject/tslint-config": "^0.4.14", "@0xproject/tslint-config": "^0.4.14",
"@types/bintrees": "^1.0.2", "@types/bintrees": "^1.0.2",
"@types/lodash": "4.14.104", "@types/lodash": "4.14.104",
@ -90,6 +92,7 @@
"tslint": "5.8.0", "tslint": "5.8.0",
"typedoc": "0xProject/typedoc", "typedoc": "0xProject/typedoc",
"typescript": "2.7.1", "typescript": "2.7.1",
"web3-provider-engine": "^13.0.1",
"webpack": "^3.1.0" "webpack": "^3.1.0"
}, },
"dependencies": { "dependencies": {

View File

@ -1,17 +1,31 @@
import { Deployer } from '@0xproject/deployer';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import * as _ from 'lodash'; import * as _ from 'lodash';
import 'mocha'; import 'mocha';
import * as path from 'path';
import * as Sinon from 'sinon'; import * as Sinon from 'sinon';
import { ApprovalContractEventArgs, LogWithDecodedArgs, Order, TokenEvents, ZeroEx } from '../src'; import { ApprovalContractEventArgs, LogWithDecodedArgs, Order, TokenEvents, ZeroEx } from '../src';
import { runMigrationsAsync } from './migrations/migrate';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3, web3Wrapper } from './utils/web3_wrapper'; import { web3, web3Wrapper } from './utils/web3_wrapper';
const artifactsDir = path.resolve('test', 'artifacts');
const deployerOpts = {
artifactsDir,
web3Provider: web3.currentProvider,
networkId: constants.TESTRPC_NETWORK_ID,
defaults: {
gas: devConstants.GAS_ESTIMATE,
},
};
const deployer = new Deployer(deployerOpts);
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
@ -19,10 +33,14 @@ const expect = chai.expect;
const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false; const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false;
describe('ZeroEx library', () => { describe('ZeroEx library', () => {
const config = { let zeroEx: ZeroEx;
networkId: constants.TESTRPC_NETWORK_ID, before(async () => {
}; await runMigrationsAsync(deployer);
const zeroEx = new ZeroEx(web3.currentProvider, config); const config = {
networkId: constants.TESTRPC_NETWORK_ID,
};
zeroEx = new ZeroEx(web3.currentProvider, config);
});
describe('#setProvider', () => { describe('#setProvider', () => {
it('overrides provider in nested web3s and invalidates contractInstances', async () => { it('overrides provider in nested web3s and invalidates contractInstances', async () => {
// Instantiate the contract instances with the current provider // Instantiate the contract instances with the current provider
@ -31,7 +49,7 @@ describe('ZeroEx library', () => {
expect((zeroEx.exchange as any)._exchangeContractIfExists).to.not.be.undefined(); expect((zeroEx.exchange as any)._exchangeContractIfExists).to.not.be.undefined();
expect((zeroEx.tokenRegistry as any)._tokenRegistryContractIfExists).to.not.be.undefined(); expect((zeroEx.tokenRegistry as any)._tokenRegistryContractIfExists).to.not.be.undefined();
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3.currentProvider;
// Add property to newProvider so that we can differentiate it from old provider // Add property to newProvider so that we can differentiate it from old provider
(newProvider as any).zeroExTestId = 1; (newProvider as any).zeroExTestId = 1;
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);

View File

@ -6,11 +6,11 @@ import { ZeroEx } from '../src';
import { assert } from '../src/utils/assert'; import { assert } from '../src/utils/assert';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { web3 } from './utils/web3_wrapper';
const expect = chai.expect; const expect = chai.expect;
describe('Assertion library', () => { describe('Assertion library', () => {
const web3 = web3Factory.create();
const config = { const config = {
networkId: constants.TESTRPC_NETWORK_ID, networkId: constants.TESTRPC_NETWORK_ID,
}; };

View File

@ -260,7 +260,7 @@ describe('EtherTokenWrapper', () => {
callbackNeverToBeCalled, callbackNeverToBeCalled,
); );
const callbackToBeCalled = reportNodeCallbackErrors(done)(); const callbackToBeCalled = reportNodeCallbackErrors(done)();
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3.currentProvider;
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
zeroEx.etherToken.subscribe( zeroEx.etherToken.subscribe(

View File

@ -13,12 +13,12 @@ import { DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { reportNodeCallbackErrors } from './utils/report_callback_errors'; import { reportNodeCallbackErrors } from './utils/report_callback_errors';
import { web3 } from './utils/web3_wrapper';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
describe('EventWatcher', () => { describe('EventWatcher', () => {
let web3: Web3;
let stubs: Sinon.SinonStub[] = []; let stubs: Sinon.SinonStub[] = [];
let eventWatcher: EventWatcher; let eventWatcher: EventWatcher;
let web3Wrapper: Web3Wrapper; let web3Wrapper: Web3Wrapper;
@ -53,7 +53,6 @@ describe('EventWatcher', () => {
transactionIndex: 0, transactionIndex: 0,
}; };
before(async () => { before(async () => {
web3 = web3Factory.create();
const pollingIntervalMs = 10; const pollingIntervalMs = 10;
web3Wrapper = new Web3Wrapper(web3.currentProvider); web3Wrapper = new Web3Wrapper(web3.currentProvider);
eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs);

View File

@ -977,7 +977,7 @@ describe('ExchangeWrapper', () => {
); );
zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled); zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled);
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3.currentProvider;
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
const callback = reportNodeCallbackErrors(done)( const callback = reportNodeCallbackErrors(done)(

View File

@ -5,10 +5,10 @@ import { ZeroEx } from '../src';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { web3 } from './utils/web3_wrapper';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
const web3 = web3Factory.create();
describe('TokenTransferProxyWrapper', () => { describe('TokenTransferProxyWrapper', () => {
let zeroEx: ZeroEx; let zeroEx: ZeroEx;

View File

@ -1,9 +1,11 @@
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { EmptyWalletSubprovider } from '@0xproject/subproviders';
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai'; import * as chai from 'chai';
import 'mocha'; import 'mocha';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
import Web3ProviderEngine = require('web3-provider-engine');
import { import {
ApprovalContractEventArgs, ApprovalContractEventArgs,
@ -191,7 +193,8 @@ describe('TokenWrapper', () => {
let zeroExWithoutAccounts: ZeroEx; let zeroExWithoutAccounts: ZeroEx;
before(async () => { before(async () => {
const hasAddresses = false; const hasAddresses = false;
const web3WithoutAccounts = web3Factory.create({ hasAddresses }); const emptyWalletProvider = addEmptyWalletSubprovider(web3.currentProvider);
const web3WithoutAccounts = new Web3(emptyWalletProvider);
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config); zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config);
}); });
it('should return balance even when called with Web3 provider instance without addresses', async () => { it('should return balance even when called with Web3 provider instance without addresses', async () => {
@ -303,7 +306,8 @@ describe('TokenWrapper', () => {
let zeroExWithoutAccounts: ZeroEx; let zeroExWithoutAccounts: ZeroEx;
before(async () => { before(async () => {
const hasAddresses = false; const hasAddresses = false;
const web3WithoutAccounts = web3Factory.create({ hasAddresses }); const emptyWalletProvider = addEmptyWalletSubprovider(web3.currentProvider);
const web3WithoutAccounts = new Web3(emptyWalletProvider);
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config); zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config);
}); });
it('should get the proxy allowance', async () => { it('should get the proxy allowance', async () => {
@ -424,7 +428,7 @@ describe('TokenWrapper', () => {
); );
zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled);
const callbackToBeCalled = reportNodeCallbackErrors(done)(); const callbackToBeCalled = reportNodeCallbackErrors(done)();
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3.currentProvider;
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled);
await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
@ -515,3 +519,14 @@ describe('TokenWrapper', () => {
}); });
}); });
// tslint:disable:max-file-line-count // tslint:disable:max-file-line-count
function addEmptyWalletSubprovider(provider: Web3.Provider): Web3.Provider {
const providerEngine = new Web3ProviderEngine();
providerEngine.addProvider(new EmptyWalletSubprovider());
const currentSubproviders = (provider as any)._providers;
for (const subprovider of currentSubproviders) {
providerEngine.addProvider(subprovider);
}
providerEngine.start();
return providerEngine;
}

View File

@ -6,4 +6,5 @@ export const constants = {
KOVAN_RPC_URL: 'https://kovan.infura.io/', KOVAN_RPC_URL: 'https://kovan.infura.io/',
ROPSTEN_RPC_URL: 'https://ropsten.infura.io/', ROPSTEN_RPC_URL: 'https://ropsten.infura.io/',
ZRX_DECIMALS: 18, ZRX_DECIMALS: 18,
NULL_BYTES: '0x',
}; };

View File

@ -33,6 +33,7 @@ export class FillScenarios {
} }
public async initTokenBalancesAsync() { public async initTokenBalancesAsync() {
const web3Wrapper = (this._zeroEx as any)._web3Wrapper as Web3Wrapper; const web3Wrapper = (this._zeroEx as any)._web3Wrapper as Web3Wrapper;
const networkId = await web3Wrapper.getNetworkIdAsync();
for (const token of this._tokens) { for (const token of this._tokens) {
if (token.symbol !== 'ZRX' && token.symbol !== 'WETH') { if (token.symbol !== 'ZRX' && token.symbol !== 'WETH') {
const defaults = {}; const defaults = {};

View File

@ -1,6 +1,10 @@
import { web3Factory } from '@0xproject/dev-utils'; import { devConstants, web3Factory } from '@0xproject/dev-utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
export const web3 = web3Factory.create(); import { constants } from './constants';
export const web3Wrapper = new Web3Wrapper(web3.currentProvider);
const web3 = web3Factory.create({ shouldUseInProcessGanache: true });
const web3Wrapper = new Web3Wrapper(web3.currentProvider);
export { web3, web3Wrapper };