From 709026bf1a49d468850b4ebed845c8598fa4fd75 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 15:34:28 +0100 Subject: Refactor contracts tests to not use injected web3 instance --- packages/dev-utils/package.json | 5 ++- packages/dev-utils/src/constants.ts | 4 +++ packages/dev-utils/src/globals.d.ts | 2 ++ packages/dev-utils/src/index.ts | 2 ++ .../src/subproviders/empty_wallet_subprovider.ts | 27 ++++++++++++++ .../subproviders/fake_gas_estimate_subprovider.ts | 34 ++++++++++++++++++ packages/dev-utils/src/web3_factory.ts | 42 ++++++++++++++++++++++ packages/dev-utils/tsconfig.json | 1 + 8 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 packages/dev-utils/src/constants.ts create mode 100644 packages/dev-utils/src/globals.d.ts create mode 100644 packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts create mode 100644 packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts create mode 100644 packages/dev-utils/src/web3_factory.ts (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index bd77b7c20..96f21ec17 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -21,6 +21,7 @@ "devDependencies": { "@0xproject/tslint-config": "^0.4.5", "@types/lodash": "^4.14.86", + "@0xproject/types": "^0.1.4", "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", @@ -32,6 +33,8 @@ "@0xproject/utils": "^0.2.3", "ethereumjs-util": "^5.1.2", "lodash": "^4.17.4", - "request-promise-native": "^1.0.5" + "request-promise-native": "^1.0.5", + "web3": "^0.20.0", + "web3-provider-engine": "^13.0.1" } } diff --git a/packages/dev-utils/src/constants.ts b/packages/dev-utils/src/constants.ts new file mode 100644 index 000000000..14cb5b937 --- /dev/null +++ b/packages/dev-utils/src/constants.ts @@ -0,0 +1,4 @@ +export const constants = { + RPC_URL: 'http://localhost:8545', + GAS_ESTIMATE: 500000, +}; diff --git a/packages/dev-utils/src/globals.d.ts b/packages/dev-utils/src/globals.d.ts new file mode 100644 index 000000000..7b132ee28 --- /dev/null +++ b/packages/dev-utils/src/globals.d.ts @@ -0,0 +1,2 @@ +declare module 'web3-provider-engine'; +declare module 'web3-provider-engine/subproviders/rpc'; diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index 9ba0cb5cf..e899ac206 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -1,2 +1,4 @@ export { RPC } from './rpc'; export { BlockchainLifecycle } from './blockchain_lifecycle'; +export { web3Factory } from './web3_factory'; +export { constants as devConstants } from './constants'; diff --git a/packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts b/packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts new file mode 100644 index 000000000..8c1fdfdb2 --- /dev/null +++ b/packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts @@ -0,0 +1,27 @@ +import { JSONRPCPayload } from '@0xproject/types'; + +/* + * This class implements the web3-provider-engine subprovider interface and returns + * that the provider has no addresses when queried. + * Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js + */ +export class EmptyWalletSubprovider { + // This method needs to be here to satisfy the interface but linter wants it to be static. + // tslint:disable-next-line:prefer-function-over-method + public handleRequest(payload: JSONRPCPayload, next: () => void, end: (err: Error | null, result: any) => void) { + switch (payload.method) { + case 'eth_accounts': + end(null, []); + return; + + default: + next(); + return; + } + } + // Required to implement this method despite not needing it for this subprovider + // tslint:disable-next-line:prefer-function-over-method + public setEngine(engine: any) { + // noop + } +} diff --git a/packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts b/packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts new file mode 100644 index 000000000..b455a0ed7 --- /dev/null +++ b/packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts @@ -0,0 +1,34 @@ +import { JSONRPCPayload } from '@0xproject/types'; + +/* + * This class implements the web3-provider-engine subprovider interface and returns + * the constant gas estimate when queried. + * HACK: We need this so that our tests don't use testrpc gas estimation which sometimes kills the node. + * Source: https://github.com/trufflesuite/ganache-cli/issues/417 + * Source: https://github.com/trufflesuite/ganache-cli/issues/437 + * Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js + */ +export class FakeGasEstimateSubprovider { + private _constantGasAmount: number; + constructor(constantGasAmount: number) { + this._constantGasAmount = constantGasAmount; + } + // This method needs to be here to satisfy the interface but linter wants it to be static. + // tslint:disable-next-line:prefer-function-over-method + public handleRequest(payload: JSONRPCPayload, next: () => void, end: (err: Error | null, result: any) => void) { + switch (payload.method) { + case 'eth_estimateGas': + end(null, this._constantGasAmount); + return; + + default: + next(); + return; + } + } + // Required to implement this method despite not needing it for this subprovider + // tslint:disable-next-line:prefer-function-over-method + public setEngine(engine: any) { + // noop + } +} diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts new file mode 100644 index 000000000..26c26e03d --- /dev/null +++ b/packages/dev-utils/src/web3_factory.ts @@ -0,0 +1,42 @@ +// HACK: web3 injects XMLHttpRequest into the global scope and ProviderEngine checks XMLHttpRequest +// to know whether it is running in a browser or node environment. We need it to be undefined since +// we are not running in a browser env. +// Filed issue: https://github.com/ethereum/web3.js/issues/844 +(global as any).XMLHttpRequest = undefined; +import ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); + +import { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider'; +import { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider'; + +import { constants } from './constants'; + +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// importing web3 after subproviders fixes this issue +// Filed issue: https://github.com/ethereum/web3.js/issues/844 +// tslint:disable-next-line:ordered-imports +import * as Web3 from 'web3'; + +export const web3Factory = { + create(hasAddresses: boolean = true): Web3 { + const provider = this.getRpcProvider(hasAddresses); + const web3 = new Web3(); + web3.setProvider(provider); + return web3; + }, + getRpcProvider(hasAddresses: boolean = true): Web3.Provider { + const provider = new ProviderEngine(); + if (!hasAddresses) { + provider.addProvider(new EmptyWalletSubprovider()); + } + provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE)); + provider.addProvider( + new RpcSubprovider({ + rpcUrl: constants.RPC_URL, + }), + ); + provider.start(); + return provider; + }, +}; diff --git a/packages/dev-utils/tsconfig.json b/packages/dev-utils/tsconfig.json index b28e45170..bdf315d59 100644 --- a/packages/dev-utils/tsconfig.json +++ b/packages/dev-utils/tsconfig.json @@ -6,6 +6,7 @@ "include": [ "./src/**/*", "../../node_modules/types-bn/index.d.ts", + "../../node_modules/web3-typescript-typings/index.d.ts", "../../node_modules/types-ethereumjs-util/index.d.ts" ] } -- cgit v1.2.3 From 387363283ca03ac1d6c9be5b7be2107790bbf79d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 22 Jan 2018 21:53:32 +0100 Subject: Remove truffle from tests --- packages/dev-utils/src/constants.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/constants.ts b/packages/dev-utils/src/constants.ts index 14cb5b937..4f7b4202a 100644 --- a/packages/dev-utils/src/constants.ts +++ b/packages/dev-utils/src/constants.ts @@ -1,4 +1,5 @@ export const constants = { RPC_URL: 'http://localhost:8545', - GAS_ESTIMATE: 500000, + RPC_PORT: 8545, + GAS_ESTIMATE: 1000000, }; -- cgit v1.2.3 From 1e9147b8bb45a251eefdead23573a9e8d68fc34b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 Jan 2018 15:12:46 +0100 Subject: Normalize the dependencies --- packages/dev-utils/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index 96f21ec17..ec0872a46 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -21,12 +21,12 @@ "devDependencies": { "@0xproject/tslint-config": "^0.4.5", "@types/lodash": "^4.14.86", - "@0xproject/types": "^0.1.4", + "@0xproject/types": "^0.1.5", "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", "types-bn": "^0.0.1", - "types-ethereumjs-util": "0xProject/types-ethereumjs-util", + "types-ethereumjs-util": "0xproject/types-ethereumjs-util", "typescript": "~2.6.1" }, "dependencies": { -- cgit v1.2.3 From 6205209fbbb5dbebbf225e1cef76c3e86117d356 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 Jan 2018 14:03:54 +0100 Subject: Make an RPC constructor param implicit --- packages/dev-utils/src/blockchain_lifecycle.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 18f5d5c61..bb4908a0d 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,10 +1,11 @@ +import { constants } from './constants'; import { RPC } from './rpc'; export class BlockchainLifecycle { private _rpc: RPC; private _snapshotIdsStack: number[]; - constructor(url: string) { - this._rpc = new RPC(url); + constructor() { + this._rpc = new RPC(constants.RPC_URL); this._snapshotIdsStack = []; } // TODO: In order to run these tests on an actual node, we should check if we are running against -- cgit v1.2.3 From d0e7046a8998bc131f7e486da5501efda53fed53 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 Jan 2018 14:24:56 +0100 Subject: Remove constructor arg --- packages/dev-utils/src/blockchain_lifecycle.ts | 3 +-- packages/dev-utils/src/rpc.ts | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index bb4908a0d..c46902f76 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,11 +1,10 @@ -import { constants } from './constants'; import { RPC } from './rpc'; export class BlockchainLifecycle { private _rpc: RPC; private _snapshotIdsStack: number[]; constructor() { - this._rpc = new RPC(constants.RPC_URL); + this._rpc = new RPC(); this._snapshotIdsStack = []; } // TODO: In order to run these tests on an actual node, we should check if we are running against diff --git a/packages/dev-utils/src/rpc.ts b/packages/dev-utils/src/rpc.ts index 36f8b1ef9..47a359263 100644 --- a/packages/dev-utils/src/rpc.ts +++ b/packages/dev-utils/src/rpc.ts @@ -1,11 +1,13 @@ import * as ethUtil from 'ethereumjs-util'; import * as request from 'request-promise-native'; +import { constants } from './constants'; + export class RPC { private _url: string; private _id: number; - constructor(url: string) { - this._url = url; + constructor() { + this._url = constants.RPC_URL; this._id = 0; } public async takeSnapshotAsync(): Promise { -- cgit v1.2.3 From 09659cc3041e8662e64beab2728477f3437c1ced Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 12:30:41 +0100 Subject: Add build:watch command to all TS packages --- packages/dev-utils/package.json | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index ec0872a46..9bf48c23c 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -5,6 +5,7 @@ "main": "lib/index.js", "types": "lib/index.d.ts", "scripts": { + "build:watch": "tsc -w", "build": "tsc", "clean": "shx rm -rf lib", "lint": "tslint --project . 'src/**/*.ts'" -- cgit v1.2.3 From 39e3733be4b2a8bb6f163c0aa02fbe57db213e61 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 12:50:55 +0100 Subject: Upgrade TS to the newest version --- packages/dev-utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index 9bf48c23c..a79cc3c1e 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -28,7 +28,7 @@ "tslint": "5.8.0", "types-bn": "^0.0.1", "types-ethereumjs-util": "0xproject/types-ethereumjs-util", - "typescript": "~2.6.1" + "typescript": "2.7.1" }, "dependencies": { "@0xproject/utils": "^0.2.3", -- cgit v1.2.3 From d4631e14b2203bfd95b995d25819d8d9cb834336 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 16:39:06 +0100 Subject: Publish - 0x.js@0.31.1 - @0xproject/abi-gen@0.1.6 - @0xproject/assert@0.0.15 - chai-as-promised-typescript-typings@0.0.8 - @0xproject/connect@0.5.4 - contracts@2.1.8 - @0xproject/deployer@0.0.5 - @0xproject/dev-utils@0.0.9 - @0xproject/json-schemas@0.7.7 - @0xproject/monorepo-scripts@0.1.8 - @0xproject/subproviders@0.3.5 - @0xproject/testnet-faucets@1.0.9 - @0xproject/tslint-config@0.4.6 - @0xproject/types@0.1.8 - @0xproject/utils@0.2.4 - web3-typescript-typings@0.9.8 - @0xproject/web3-wrapper@0.1.9 - @0xproject/website@0.0.11 --- packages/dev-utils/package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index ec0872a46..719426491 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -1,6 +1,6 @@ { "name": "@0xproject/dev-utils", - "version": "0.0.8", + "version": "0.0.9", "description": "0x dev TS utils", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -19,9 +19,9 @@ }, "homepage": "https://github.com/0xProject/0x.js/packages/dev-utils/README.md", "devDependencies": { - "@0xproject/tslint-config": "^0.4.5", + "@0xproject/tslint-config": "^0.4.6", + "@0xproject/types": "^0.1.8", "@types/lodash": "^4.14.86", - "@0xproject/types": "^0.1.5", "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", @@ -30,7 +30,7 @@ "typescript": "~2.6.1" }, "dependencies": { - "@0xproject/utils": "^0.2.3", + "@0xproject/utils": "^0.2.4", "ethereumjs-util": "^5.1.2", "lodash": "^4.17.4", "request-promise-native": "^1.0.5", -- cgit v1.2.3