aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils/src
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2018-02-05 19:33:40 +0800
committerGitHub <noreply@github.com>2018-02-05 19:33:40 +0800
commit7b4f2b47de393b7ed6d5c264c8e80864d196180c (patch)
tree8e86af8000e0aedd7241a00c2189d8f7ca0fc2ad /packages/dev-utils/src
parent400a97e7a8f76d894d47368425bbe1e33fa5b255 (diff)
parentc7ad6ebad6ab65a4b1e4a2084e744c6ca2bc09b8 (diff)
downloaddexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.gz
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.bz2
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.lz
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.xz
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.zst
dexon-sol-tools-7b4f2b47de393b7ed6d5c264c8e80864d196180c.zip
Merge branch 'development' into fix/ether_token_address
Diffstat (limited to 'packages/dev-utils/src')
-rw-r--r--packages/dev-utils/src/blockchain_lifecycle.ts4
-rw-r--r--packages/dev-utils/src/constants.ts5
-rw-r--r--packages/dev-utils/src/globals.d.ts2
-rw-r--r--packages/dev-utils/src/index.ts2
-rw-r--r--packages/dev-utils/src/rpc.ts6
-rw-r--r--packages/dev-utils/src/subproviders/empty_wallet_subprovider.ts27
-rw-r--r--packages/dev-utils/src/subproviders/fake_gas_estimate_subprovider.ts34
-rw-r--r--packages/dev-utils/src/web3_factory.ts42
8 files changed, 118 insertions, 4 deletions
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;
+ },
+};