From b362e2c28e9cafa7335bced17ec61fba93b018e6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 12:51:46 +0300 Subject: Refactor networkId out of web3Wrapper --- packages/0x.js/src/0x.ts | 13 ++++++++++--- packages/0x.js/src/contract_wrappers/contract_wrapper.ts | 12 ++++++------ packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts | 5 +++-- packages/0x.js/src/contract_wrappers/exchange_wrapper.ts | 4 ++-- .../0x.js/src/contract_wrappers/token_registry_wrapper.ts | 4 ++-- .../src/contract_wrappers/token_transfer_proxy_wrapper.ts | 4 ++-- packages/0x.js/src/contract_wrappers/token_wrapper.ts | 4 ++-- packages/0x.js/test/event_watcher_test.ts | 2 +- packages/0x.js/test/token_wrapper_test.ts | 2 +- packages/contracts/deploy/src/deployer.ts | 2 +- packages/contracts/deploy/src/utils/network.ts | 5 +++-- packages/web3-wrapper/src/index.ts | 13 ++++++------- 12 files changed, 39 insertions(+), 31 deletions(-) (limited to 'packages') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 1cbfaed0c..935e4ac1a 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -179,24 +179,31 @@ export class ZeroEx { const defaults = { gasPrice: config.gasPrice, }; - this._web3Wrapper = new Web3Wrapper(provider, config.networkId, defaults); + this._web3Wrapper = new Web3Wrapper(provider, defaults); this.proxy = new TokenTransferProxyWrapper( this._web3Wrapper, + config.networkId, config.tokenTransferProxyContractAddress, ); this.token = new TokenWrapper( this._web3Wrapper, + config.networkId, this._abiDecoder, this.proxy, ); this.exchange = new ExchangeWrapper( this._web3Wrapper, + config.networkId, this._abiDecoder, this.token, config.exchangeContractAddress, ); - this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, config.tokenRegistryContractAddress); - this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, config.etherTokenContractAddress); + this.tokenRegistry = new TokenRegistryWrapper( + this._web3Wrapper, config.networkId, config.tokenRegistryContractAddress, + ); + this.etherToken = new EtherTokenWrapper( + this._web3Wrapper, config.networkId, this.token, config.etherTokenContractAddress, + ); this.orderStateWatcher = new OrderStateWatcher( this._web3Wrapper, this._abiDecoder, this.token, this.exchange, config.orderWatcherConfig, ); diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 0b6fc031a..d56b8632d 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -32,6 +32,7 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = export class ContractWrapper { protected _web3Wrapper: Web3Wrapper; + private _networkId: number; private _abiDecoder?: AbiDecoder; private _blockAndLogStreamer: BlockAndLogStreamer|undefined; private _blockAndLogStreamInterval: NodeJS.Timer; @@ -39,8 +40,9 @@ export class ContractWrapper { private _filterCallbacks: {[filterToken: string]: EventCallback}; private _onLogAddedSubscriptionToken: string|undefined; private _onLogRemovedSubscriptionToken: string|undefined; - constructor(web3Wrapper: Web3Wrapper, abiDecoder?: AbiDecoder) { + constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder?: AbiDecoder) { this._web3Wrapper = web3Wrapper; + this._networkId = networkId; this._abiDecoder = abiDecoder; this._filters = {}; this._filterCallbacks = {}; @@ -104,11 +106,10 @@ export class ContractWrapper { ): Promise { let contractAddress: string; if (_.isUndefined(addressIfExists)) { - const networkId = this._web3Wrapper.getNetworkId(); - if (_.isUndefined(artifact.networks[networkId])) { + if (_.isUndefined(artifact.networks[this._networkId])) { throw new Error(ZeroExError.ContractNotDeployedOnNetwork); } - contractAddress = artifact.networks[networkId].address.toLowerCase(); + contractAddress = artifact.networks[this._networkId].address.toLowerCase(); } else { contractAddress = addressIfExists; } @@ -123,8 +124,7 @@ export class ContractWrapper { } protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string { if (_.isUndefined(addressIfExists)) { - const networkId = this._web3Wrapper.getNetworkId(); - const contractAddress = artifact.networks[networkId].address; + const contractAddress = artifact.networks[this._networkId].address; if (_.isUndefined(contractAddress)) { throw new Error(ZeroExError.ExchangeContractDoesNotExist); } diff --git a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts index 685ae9a9e..896cfde3d 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -18,8 +18,9 @@ export class EtherTokenWrapper extends ContractWrapper { private _etherTokenContractIfExists?: EtherTokenContract; private _tokenWrapper: TokenWrapper; private _contractAddressIfExists?: string; - constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) { - super(web3Wrapper); + constructor(web3Wrapper: Web3Wrapper, networkId: number, tokenWrapper: TokenWrapper, + contractAddressIfExists?: string) { + super(web3Wrapper, networkId); this._tokenWrapper = tokenWrapper; this._contractAddressIfExists = contractAddressIfExists; } diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 433d99e4c..1e9865395 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -84,9 +84,9 @@ export class ExchangeWrapper extends ContractWrapper { ]; return [orderAddresses, orderValues]; } - constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder, + constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder: AbiDecoder, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) { - super(web3Wrapper, abiDecoder); + super(web3Wrapper, networkId, abiDecoder); this._tokenWrapper = tokenWrapper; this._orderValidationUtils = new OrderValidationUtils(tokenWrapper, this); this._contractAddressIfExists = contractAddressIfExists; diff --git a/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts index 80b4c0f85..064b826d8 100644 --- a/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts @@ -27,8 +27,8 @@ export class TokenRegistryWrapper extends ContractWrapper { }; return token; } - constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) { - super(web3Wrapper); + constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) { + super(web3Wrapper, networkId); this._contractAddressIfExists = contractAddressIfExists; } /** diff --git a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts index 7d6943aea..1a16e3540 100644 --- a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts @@ -13,8 +13,8 @@ import {TokenTransferProxyContract} from './generated/token_transfer_proxy'; export class TokenTransferProxyWrapper extends ContractWrapper { private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract; private _contractAddressIfExists?: string; - constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) { - super(web3Wrapper); + constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) { + super(web3Wrapper, networkId); this._contractAddressIfExists = contractAddressIfExists; } /** diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 1ae26edaa..684f291a5 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -34,9 +34,9 @@ export class TokenWrapper extends ContractWrapper { public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; private _tokenContractsByAddress: {[address: string]: TokenContract}; private _tokenTransferProxyWrapper: TokenTransferProxyWrapper; - constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder, + constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder: AbiDecoder, tokenTransferProxyWrapper: TokenTransferProxyWrapper) { - super(web3Wrapper, abiDecoder); + super(web3Wrapper, networkId, abiDecoder); this._tokenContractsByAddress = {}; this._tokenTransferProxyWrapper = tokenTransferProxyWrapper; } diff --git a/packages/0x.js/test/event_watcher_test.ts b/packages/0x.js/test/event_watcher_test.ts index 41fca4e97..3d92c62a3 100644 --- a/packages/0x.js/test/event_watcher_test.ts +++ b/packages/0x.js/test/event_watcher_test.ts @@ -60,7 +60,7 @@ describe('EventWatcher', () => { before(async () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; - web3Wrapper = new Web3Wrapper(web3.currentProvider, constants.TESTRPC_NETWORK_ID); + web3Wrapper = new Web3Wrapper(web3.currentProvider); eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); }); afterEach(() => { diff --git a/packages/0x.js/test/token_wrapper_test.ts b/packages/0x.js/test/token_wrapper_test.ts index 57cfbea18..ae6016869 100644 --- a/packages/0x.js/test/token_wrapper_test.ts +++ b/packages/0x.js/test/token_wrapper_test.ts @@ -46,7 +46,7 @@ describe('TokenWrapper', () => { before(async () => { web3 = web3Factory.create(); zeroEx = new ZeroEx(web3.currentProvider, config); - web3Wrapper = new Web3Wrapper(web3.currentProvider, config.networkId); + web3Wrapper = new Web3Wrapper(web3.currentProvider); userAddresses = await zeroEx.getAvailableAddressesAsync(); tokens = await zeroEx.tokenRegistry.getTokensAsync(); tokenUtils = new TokenUtils(tokens); diff --git a/packages/contracts/deploy/src/deployer.ts b/packages/contracts/deploy/src/deployer.ts index 2d4f31949..4c8018ecc 100644 --- a/packages/contracts/deploy/src/deployer.ts +++ b/packages/contracts/deploy/src/deployer.ts @@ -31,7 +31,7 @@ export class Deployer { const jsonrpcUrl = `http://localhost:${this.jsonrpcPort}`; const web3Provider = new Web3.providers.HttpProvider(jsonrpcUrl); this.defaults = opts.defaults; - this.web3Wrapper = new Web3Wrapper(web3Provider, this.networkId, this.defaults); + this.web3Wrapper = new Web3Wrapper(web3Provider, this.defaults); } /** * Loads contract artifact and deploys contract with given arguments. diff --git a/packages/contracts/deploy/src/utils/network.ts b/packages/contracts/deploy/src/utils/network.ts index fc8709e0f..f9776bb97 100644 --- a/packages/contracts/deploy/src/utils/network.ts +++ b/packages/contracts/deploy/src/utils/network.ts @@ -1,4 +1,5 @@ import {promisify} from '@0xproject/utils'; +import {Web3Wrapper} from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -6,8 +7,8 @@ export const network = { async getNetworkIdIfExistsAsync(port: number): Promise { const url = `http://localhost:${port}`; const web3Provider = new Web3.providers.HttpProvider(url); - const web3 = new Web3(web3Provider); - const networkId = _.parseInt(await promisify(web3.version.getNetwork)()); + const web3Wrapper = new Web3Wrapper(web3Provider); + const networkId = web3Wrapper.getNetworkIdAsync(); return networkId; }, }; diff --git a/packages/web3-wrapper/src/index.ts b/packages/web3-wrapper/src/index.ts index b823c5c15..7df24e9a5 100644 --- a/packages/web3-wrapper/src/index.ts +++ b/packages/web3-wrapper/src/index.ts @@ -17,10 +17,9 @@ interface RawLogEntry { export class Web3Wrapper { private web3: Web3; - private networkId: number; private defaults: Partial; private jsonRpcRequestId: number; - constructor(provider: Web3.Provider, networkId: number, defaults?: Partial) { + constructor(provider: Web3.Provider, 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` @@ -28,7 +27,6 @@ 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; @@ -37,7 +35,6 @@ export class Web3Wrapper { return this.defaults; } public setProvider(provider: Web3.Provider, networkId: number) { - this.networkId = networkId; this.web3.setProvider(provider); } public isAddress(address: string): boolean { @@ -51,6 +48,11 @@ export class Web3Wrapper { const nodeVersion = await promisify(this.web3.version.getNode)(); return nodeVersion; } + public async getNetworkIdAsync(): Promise { + const networkIdStr = await promisify(this.web3.version.getNetwork)(); + const networkId = _.parseInt(networkIdStr); + return networkId; + } public async getTransactionReceiptAsync(txHash: string): Promise { const transactionReceipt = await promisify(this.web3.eth.getTransactionReceipt)(txHash); if (!_.isNull(transactionReceipt)) { @@ -61,9 +63,6 @@ export class Web3Wrapper { public getCurrentProvider(): Web3.Provider { return this.web3.currentProvider; } - public getNetworkId(): number { - return this.networkId; - } public toWei(ethAmount: BigNumber): BigNumber { const balanceWei = this.web3.toWei(ethAmount, 'ether'); return balanceWei; -- cgit v1.2.3