From 43e07e7ce3dd61af8048c20aa2355010f0c72c22 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 4 Apr 2018 16:31:57 +0900 Subject: Update 0x.js to run tests against in-process ganache --- packages/0x.js/package.json | 7 ++++-- packages/0x.js/test/0x.js_test.ts | 28 ++++++++++++++++++---- packages/0x.js/test/assert_test.ts | 2 +- packages/0x.js/test/ether_token_wrapper_test.ts | 2 +- packages/0x.js/test/event_watcher_test.ts | 3 +-- packages/0x.js/test/exchange_wrapper_test.ts | 2 +- .../test/token_transfer_proxy_wrapper_test.ts | 2 +- packages/0x.js/test/token_wrapper_test.ts | 21 +++++++++++++--- packages/0x.js/test/utils/constants.ts | 1 + packages/0x.js/test/utils/fill_scenarios.ts | 1 + packages/0x.js/test/utils/web3_wrapper.ts | 10 +++++--- 11 files changed, 60 insertions(+), 19 deletions(-) diff --git a/packages/0x.js/package.json b/packages/0x.js/package.json index 882b40733..0a91343c7 100644 --- a/packages/0x.js/package.json +++ b/packages/0x.js/package.json @@ -21,7 +21,7 @@ "test": "run-s clean test:commonjs", "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "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", "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", @@ -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" }, "config": { - "artifacts": "TokenTransferProxy Exchange TokenRegistry Token EtherToken", + "artifacts": "Exchange DummyToken ZRXToken Token WETH9 TokenTransferProxy MultiSigWallet MultiSigWalletWithTimeLock MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress MaliciousToken TokenRegistry Arbitrage EtherDelta AccountLevels", "postpublish": { "assets": [ "packages/0x.js/_bundles/index.js", @@ -60,8 +60,10 @@ "node": ">=6.0.0" }, "devDependencies": { + "@0xproject/deployer": "^0.3.5", "@0xproject/dev-utils": "^0.3.4", "@0xproject/monorepo-scripts": "^0.1.16", + "@0xproject/subproviders": "^0.8.4", "@0xproject/tslint-config": "^0.4.14", "@types/bintrees": "^1.0.2", "@types/lodash": "4.14.104", @@ -90,6 +92,7 @@ "tslint": "5.8.0", "typedoc": "0xProject/typedoc", "typescript": "2.7.1", + "web3-provider-engine": "^13.0.1", "webpack": "^3.1.0" }, "dependencies": { diff --git a/packages/0x.js/test/0x.js_test.ts b/packages/0x.js/test/0x.js_test.ts index 70e85aa52..2ae944a99 100644 --- a/packages/0x.js/test/0x.js_test.ts +++ b/packages/0x.js/test/0x.js_test.ts @@ -1,17 +1,31 @@ +import { Deployer } from '@0xproject/deployer'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; import * as _ from 'lodash'; import 'mocha'; +import * as path from 'path'; import * as Sinon from 'sinon'; import { ApprovalContractEventArgs, LogWithDecodedArgs, Order, TokenEvents, ZeroEx } from '../src'; +import { runMigrationsAsync } from './migrations/migrate'; import { chaiSetup } from './utils/chai_setup'; import { constants } from './utils/constants'; import { TokenUtils } from './utils/token_utils'; 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); chaiSetup.configure(); const expect = chai.expect; @@ -19,10 +33,14 @@ const expect = chai.expect; const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false; describe('ZeroEx library', () => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - const zeroEx = new ZeroEx(web3.currentProvider, config); + let zeroEx: ZeroEx; + before(async () => { + await runMigrationsAsync(deployer); + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + zeroEx = new ZeroEx(web3.currentProvider, config); + }); describe('#setProvider', () => { it('overrides provider in nested web3s and invalidates contractInstances', async () => { // 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.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 (newProvider as any).zeroExTestId = 1; zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); diff --git a/packages/0x.js/test/assert_test.ts b/packages/0x.js/test/assert_test.ts index c4451742f..4f3894a59 100644 --- a/packages/0x.js/test/assert_test.ts +++ b/packages/0x.js/test/assert_test.ts @@ -6,11 +6,11 @@ import { ZeroEx } from '../src'; import { assert } from '../src/utils/assert'; import { constants } from './utils/constants'; +import { web3 } from './utils/web3_wrapper'; const expect = chai.expect; describe('Assertion library', () => { - const web3 = web3Factory.create(); const config = { networkId: constants.TESTRPC_NETWORK_ID, }; diff --git a/packages/0x.js/test/ether_token_wrapper_test.ts b/packages/0x.js/test/ether_token_wrapper_test.ts index 6237ccf23..6687c48e1 100644 --- a/packages/0x.js/test/ether_token_wrapper_test.ts +++ b/packages/0x.js/test/ether_token_wrapper_test.ts @@ -260,7 +260,7 @@ describe('EtherTokenWrapper', () => { callbackNeverToBeCalled, ); const callbackToBeCalled = reportNodeCallbackErrors(done)(); - const newProvider = web3Factory.getRpcProvider(); + const newProvider = web3.currentProvider; zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); zeroEx.etherToken.subscribe( diff --git a/packages/0x.js/test/event_watcher_test.ts b/packages/0x.js/test/event_watcher_test.ts index 2fa6c0580..269cd5bdb 100644 --- a/packages/0x.js/test/event_watcher_test.ts +++ b/packages/0x.js/test/event_watcher_test.ts @@ -13,12 +13,12 @@ import { DoneCallback } from '../src/types'; import { chaiSetup } from './utils/chai_setup'; import { reportNodeCallbackErrors } from './utils/report_callback_errors'; +import { web3 } from './utils/web3_wrapper'; chaiSetup.configure(); const expect = chai.expect; describe('EventWatcher', () => { - let web3: Web3; let stubs: Sinon.SinonStub[] = []; let eventWatcher: EventWatcher; let web3Wrapper: Web3Wrapper; @@ -53,7 +53,6 @@ describe('EventWatcher', () => { transactionIndex: 0, }; before(async () => { - web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index cfc390bae..ba401d952 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -977,7 +977,7 @@ describe('ExchangeWrapper', () => { ); zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled); - const newProvider = web3Factory.getRpcProvider(); + const newProvider = web3.currentProvider; zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); const callback = reportNodeCallbackErrors(done)( diff --git a/packages/0x.js/test/token_transfer_proxy_wrapper_test.ts b/packages/0x.js/test/token_transfer_proxy_wrapper_test.ts index fb003634a..092ac51ad 100644 --- a/packages/0x.js/test/token_transfer_proxy_wrapper_test.ts +++ b/packages/0x.js/test/token_transfer_proxy_wrapper_test.ts @@ -5,10 +5,10 @@ import { ZeroEx } from '../src'; import { chaiSetup } from './utils/chai_setup'; import { constants } from './utils/constants'; +import { web3 } from './utils/web3_wrapper'; chaiSetup.configure(); const expect = chai.expect; -const web3 = web3Factory.create(); describe('TokenTransferProxyWrapper', () => { let zeroEx: ZeroEx; diff --git a/packages/0x.js/test/token_wrapper_test.ts b/packages/0x.js/test/token_wrapper_test.ts index 0c6335d5e..988532abd 100644 --- a/packages/0x.js/test/token_wrapper_test.ts +++ b/packages/0x.js/test/token_wrapper_test.ts @@ -1,9 +1,11 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { EmptyWalletSubprovider } from '@0xproject/subproviders'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import 'mocha'; import * as Web3 from 'web3'; +import Web3ProviderEngine = require('web3-provider-engine'); import { ApprovalContractEventArgs, @@ -191,7 +193,8 @@ describe('TokenWrapper', () => { let zeroExWithoutAccounts: ZeroEx; before(async () => { const hasAddresses = false; - const web3WithoutAccounts = web3Factory.create({ hasAddresses }); + const emptyWalletProvider = addEmptyWalletSubprovider(web3.currentProvider); + const web3WithoutAccounts = new Web3(emptyWalletProvider); zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config); }); it('should return balance even when called with Web3 provider instance without addresses', async () => { @@ -303,7 +306,8 @@ describe('TokenWrapper', () => { let zeroExWithoutAccounts: ZeroEx; before(async () => { const hasAddresses = false; - const web3WithoutAccounts = web3Factory.create({ hasAddresses }); + const emptyWalletProvider = addEmptyWalletSubprovider(web3.currentProvider); + const web3WithoutAccounts = new Web3(emptyWalletProvider); zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config); }); it('should get the proxy allowance', async () => { @@ -424,7 +428,7 @@ describe('TokenWrapper', () => { ); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled); const callbackToBeCalled = reportNodeCallbackErrors(done)(); - const newProvider = web3Factory.getRpcProvider(); + const newProvider = web3.currentProvider; zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled); await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); @@ -515,3 +519,14 @@ describe('TokenWrapper', () => { }); }); // 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; +} diff --git a/packages/0x.js/test/utils/constants.ts b/packages/0x.js/test/utils/constants.ts index cf030259c..3b74fa863 100644 --- a/packages/0x.js/test/utils/constants.ts +++ b/packages/0x.js/test/utils/constants.ts @@ -6,4 +6,5 @@ export const constants = { KOVAN_RPC_URL: 'https://kovan.infura.io/', ROPSTEN_RPC_URL: 'https://ropsten.infura.io/', ZRX_DECIMALS: 18, + NULL_BYTES: '0x', }; diff --git a/packages/0x.js/test/utils/fill_scenarios.ts b/packages/0x.js/test/utils/fill_scenarios.ts index 8b1308298..25e2aa36f 100644 --- a/packages/0x.js/test/utils/fill_scenarios.ts +++ b/packages/0x.js/test/utils/fill_scenarios.ts @@ -33,6 +33,7 @@ export class FillScenarios { } public async initTokenBalancesAsync() { const web3Wrapper = (this._zeroEx as any)._web3Wrapper as Web3Wrapper; + const networkId = await web3Wrapper.getNetworkIdAsync(); for (const token of this._tokens) { if (token.symbol !== 'ZRX' && token.symbol !== 'WETH') { const defaults = {}; diff --git a/packages/0x.js/test/utils/web3_wrapper.ts b/packages/0x.js/test/utils/web3_wrapper.ts index 4b374fc7e..35898f553 100644 --- a/packages/0x.js/test/utils/web3_wrapper.ts +++ b/packages/0x.js/test/utils/web3_wrapper.ts @@ -1,6 +1,10 @@ -import { web3Factory } from '@0xproject/dev-utils'; +import { devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as Web3 from 'web3'; -export const web3 = web3Factory.create(); -export const web3Wrapper = new Web3Wrapper(web3.currentProvider); +import { constants } from './constants'; + +const web3 = web3Factory.create({ shouldUseInProcessGanache: true }); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); + +export { web3, web3Wrapper }; -- cgit