From d789aa24aa2c9f4e0d17a382fe9cb8d56ab27957 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 15 Jun 2017 17:05:03 +0200 Subject: Make ZeroEx constructor accept Web3Provider instead of Web3 instance --- src/0x.ts | 14 +++++++------- src/index.ts | 1 + src/types.ts | 3 +++ src/web3_wrapper.ts | 11 +++++------ 4 files changed, 16 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/0x.ts b/src/0x.ts index 74e4ef7e6..a9b2c41af 100644 --- a/src/0x.ts +++ b/src/0x.ts @@ -14,7 +14,7 @@ import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; import {TokenWrapper} from './contract_wrappers/token_wrapper'; -import {ECSignature, ZeroExError, Order, SignedOrder} from './types'; +import {ECSignature, ZeroExError, Order, SignedOrder, Web3Provider} from './types'; import * as ExchangeArtifacts from './artifacts/Exchange.json'; import {orderSchema} from './schemas/order_schemas'; @@ -122,12 +122,12 @@ export class ZeroEx { } /** * Instantiates a new ZeroEx instance that provides the public interface to the 0x.js library. - * @param web3 The Web3.js instance you would like the 0x.js library to use for interacting with - * the Ethereum network. + * @param provider The Web3.js Provider instance you would like the 0x.js library to use for interacting with + * the Ethereum network. * @return An instance of the 0x.js ZeroEx class. */ - constructor(web3: Web3) { - this._web3Wrapper = new Web3Wrapper(web3); + constructor(provider: Web3Provider) { + this._web3Wrapper = new Web3Wrapper(provider); this.token = new TokenWrapper(this._web3Wrapper); this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token); this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper); @@ -135,9 +135,9 @@ export class ZeroEx { /** * Sets a new provider for the web3 instance used by 0x.js. Updating the provider will stop all * subscriptions so you will need to re-subscribe to all events relevant to your app after this call. - * @param provider The Web3.Provider you would like the 0x.js library to use from now on. + * @param provider The Web3Provider you would like the 0x.js library to use from now on. */ - public async setProviderAsync(provider: Web3.Provider) { + public async setProviderAsync(provider: Web3Provider) { this._web3Wrapper.setProvider(provider); await this.exchange.invalidateContractInstanceAsync(); this.tokenRegistry.invalidateContractInstance(); diff --git a/src/index.ts b/src/index.ts index 48e83fc19..7b1a16125 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,4 +19,5 @@ export { OrderCancellationRequest, OrderFillRequest, ContractEventEmitter, + Web3Provider, } from './types'; diff --git a/src/types.ts b/src/types.ts index 11d3182d8..8c9e9ad5d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; // Utility function to create a K:V from a list of strings // Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html @@ -280,3 +281,5 @@ export interface ContractEventEmitter { watch: (eventCallback: EventCallback) => void; stopWatchingAsync: () => Promise; } + +export type Web3Provider = Web3.Provider; diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 05a8dc063..5bff25233 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -2,16 +2,15 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); -import {ZeroExError} from './types'; -import {assert} from './utils/assert'; +import {Web3Provider} from './types'; export class Web3Wrapper { private web3: Web3; - constructor(web3: Web3) { + constructor(provider: Web3Provider) { this.web3 = new Web3(); - this.web3.setProvider(web3.currentProvider); + this.web3.setProvider(provider); } - public setProvider(provider: Web3.Provider) { + public setProvider(provider: Web3Provider) { this.web3.setProvider(provider); } public isAddress(address: string): boolean { @@ -25,7 +24,7 @@ export class Web3Wrapper { const nodeVersion = await promisify(this.web3.version.getNode)(); return nodeVersion; } - public getCurrentProvider(): Web3.Provider { + public getCurrentProvider(): Web3Provider { return this.web3.currentProvider; } public async getNetworkIdIfExistsAsync(): Promise { -- cgit v1.2.3 From 209ca3046058e51ffc301737564029de5511967e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 15 Jun 2017 17:27:21 +0200 Subject: Add explaining comment for the web3Provider type --- src/types.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/types.ts b/src/types.ts index 8c9e9ad5d..5be64da53 100644 --- a/src/types.ts +++ b/src/types.ts @@ -281,5 +281,9 @@ export interface ContractEventEmitter { watch: (eventCallback: EventCallback) => void; stopWatchingAsync: () => Promise; } - +/** + * We re-export the `Web3.Provider` type specified in the Web3 Typescript typings + * since it is the type of the `provider` argument to the `ZeroEx` constructor. + * It is however a `Web3` library type, not a native `0x.js` type. + */ export type Web3Provider = Web3.Provider; -- cgit v1.2.3 From 04698313a385003f5c1d545fff9c1a619faea4ff Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 15 Jun 2017 17:27:58 +0200 Subject: Switch back to using Web3.Provider, not Web3Provider in internal classes --- src/web3_wrapper.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 5bff25233..6bdca499f 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -2,15 +2,14 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); -import {Web3Provider} from './types'; export class Web3Wrapper { private web3: Web3; - constructor(provider: Web3Provider) { + constructor(provider: Web3.Provider) { this.web3 = new Web3(); this.web3.setProvider(provider); } - public setProvider(provider: Web3Provider) { + public setProvider(provider: Web3.Provider) { this.web3.setProvider(provider); } public isAddress(address: string): boolean { @@ -24,7 +23,7 @@ export class Web3Wrapper { const nodeVersion = await promisify(this.web3.version.getNode)(); return nodeVersion; } - public getCurrentProvider(): Web3Provider { + public getCurrentProvider(): Web3.Provider { return this.web3.currentProvider; } public async getNetworkIdIfExistsAsync(): Promise { -- cgit v1.2.3