diff options
author | Leonid <logvinov.leon@gmail.com> | 2018-01-31 03:27:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-31 03:27:14 +0800 |
commit | 2bcb7d56394c43acd68d1823092de2ad047abc6d (patch) | |
tree | 55684889214373fcd66ca52efa079f381cfb388b /packages/dev-utils | |
parent | d004df56e38726ed7f1b775da33d8f37aae270db (diff) | |
parent | 7ad314472d27e8a6c6ee8242f4f335d414709f4d (diff) | |
download | dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.gz dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.zst dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.zip |
Merge pull request #332 from 0xProject/feature/contracts-abi-gen
Contracts refactor. Bye, truffle!!! :clap:
Diffstat (limited to 'packages/dev-utils')
-rw-r--r-- | packages/dev-utils/package.json | 7 | ||||
-rw-r--r-- | packages/dev-utils/src/blockchain_lifecycle.ts | 4 | ||||
-rw-r--r-- | packages/dev-utils/src/constants.ts | 5 | ||||
-rw-r--r-- | packages/dev-utils/src/globals.d.ts | 2 | ||||
-rw-r--r-- | packages/dev-utils/src/index.ts | 2 | ||||
-rw-r--r-- | packages/dev-utils/src/rpc.ts | 6 | ||||
-rw-r--r-- | packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts | 27 | ||||
-rw-r--r-- | packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts | 34 | ||||
-rw-r--r-- | packages/dev-utils/src/web3_factory.ts | 42 | ||||
-rw-r--r-- | packages/dev-utils/tsconfig.json | 1 |
10 files changed, 124 insertions, 6 deletions
diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index bd77b7c20..ec0872a46 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -21,17 +21,20 @@ "devDependencies": { "@0xproject/tslint-config": "^0.4.5", "@types/lodash": "^4.14.86", + "@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": { "@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/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 18f5d5c61..c46902f76 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -3,8 +3,8 @@ 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(); 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/constants.ts b/packages/dev-utils/src/constants.ts new file mode 100644 index 000000000..4f7b4202a --- /dev/null +++ b/packages/dev-utils/src/constants.ts @@ -0,0 +1,5 @@ +export const constants = { + RPC_URL: 'http://localhost:8545', + RPC_PORT: 8545, + GAS_ESTIMATE: 1000000, +}; 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/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<number> { 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" ] } |