From 131236305bff6cdf85a7edb7e3c61c50ffbb1956 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 21 Nov 2017 16:08:45 -0600 Subject: Store networkId in web3Wrapper --- packages/0x.js/src/0x.ts | 2 +- packages/0x.js/src/web3_wrapper.ts | 27 ++++++++------------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 1f4af9fea..a853aca1c 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -178,7 +178,7 @@ export class ZeroEx { const defaults = { gasPrice: config.gasPrice, }; - this._web3Wrapper = new Web3Wrapper(provider, defaults); + this._web3Wrapper = new Web3Wrapper(provider, config.networkId, defaults); this.token = new TokenWrapper( this._web3Wrapper, this._abiDecoder, diff --git a/packages/0x.js/src/web3_wrapper.ts b/packages/0x.js/src/web3_wrapper.ts index 7bd8ea093..8ce696513 100644 --- a/packages/0x.js/src/web3_wrapper.ts +++ b/packages/0x.js/src/web3_wrapper.ts @@ -18,10 +18,11 @@ interface RawLogEntry { export class Web3Wrapper { private web3: Web3; + private networkId: number; private defaults: Partial; private networkIdIfExists?: number; private jsonRpcRequestId: number; - constructor(provider: Web3.Provider, defaults?: Partial) { + constructor(provider: Web3.Provider, networkId: number, defaults?: Partial) { if (_.isUndefined((provider as any).sendAsync)) { // Web3@1.0 provider doesn't support synchronous http requests, // so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x` @@ -29,6 +30,7 @@ export class Web3Wrapper { (provider as any).sendAsync = (provider as any).send; } this.web3 = new Web3(); + this.networkId = networkId; this.web3.setProvider(provider); this.defaults = defaults || {}; this.jsonRpcRequestId = 0; @@ -58,31 +60,18 @@ export class Web3Wrapper { public getCurrentProvider(): Web3.Provider { return this.web3.currentProvider; } - public async getNetworkIdIfExistsAsync(): Promise { - if (!_.isUndefined(this.networkIdIfExists)) { - return this.networkIdIfExists; - } - - try { - const networkId = await this.getNetworkAsync(); - this.networkIdIfExists = Number(networkId); - return this.networkIdIfExists; - } catch (err) { - return undefined; - } + public getNetworkId(): number { + return this.networkId; } public async getContractInstanceFromArtifactAsync(artifact: Artifact, address?: string): Promise { let contractAddress: string; if (_.isUndefined(address)) { - const networkIdIfExists = await this.getNetworkIdIfExistsAsync(); - if (_.isUndefined(networkIdIfExists)) { - throw new Error(ZeroExError.NoNetworkId); - } - if (_.isUndefined(artifact.networks[networkIdIfExists])) { + const networkId = this.getNetworkId(); + if (_.isUndefined(artifact.networks[networkId])) { throw new Error(ZeroExError.ContractNotDeployedOnNetwork); } - contractAddress = artifact.networks[networkIdIfExists].address.toLowerCase(); + contractAddress = artifact.networks[networkId].address.toLowerCase(); } else { contractAddress = address; } -- cgit