diff options
Diffstat (limited to 'src/0x.ts')
-rw-r--r-- | src/0x.ts | 35 |
1 files changed, 30 insertions, 5 deletions
@@ -32,6 +32,7 @@ import { TransactionReceiptWithDecodedLogs, LogWithDecodedArgs, } from './types'; +import {zeroExConfigSchema} from './schemas/zero_ex_config_schema'; // Customize our BigNumber instances bigNumberConfigs.configure(); @@ -180,6 +181,9 @@ export class ZeroEx { */ constructor(provider: Web3Provider, config?: ZeroExConfig) { assert.isWeb3Provider('provider', provider); + if (!_.isUndefined(config)) { + assert.doesConformToSchema('config', config, zeroExConfigSchema); + } 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` @@ -194,11 +198,22 @@ export class ZeroEx { gasPrice, }; this._web3Wrapper = new Web3Wrapper(provider, defaults); - this.token = new TokenWrapper(this._web3Wrapper); - this.proxy = new TokenTransferProxyWrapper(this._web3Wrapper); - this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token); - this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper); - this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token); + this.token = new TokenWrapper( + this._web3Wrapper, + this._getTokenTransferProxyAddressAsync.bind(this), + ); + const exchageContractAddressIfExists = _.isUndefined(config) ? undefined : config.exchangeContractAddress; + this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token, exchageContractAddressIfExists); + this.proxy = new TokenTransferProxyWrapper( + this._web3Wrapper, + this._getTokenTransferProxyAddressAsync.bind(this), + ); + const tokenRegistryContractAddressIfExists = _.isUndefined(config) ? + undefined : + config.tokenRegistryContractAddress; + this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, tokenRegistryContractAddressIfExists); + const etherTokenContractAddressIfExists = _.isUndefined(config) ? undefined : config.etherTokenContractAddress; + this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, etherTokenContractAddressIfExists); } /** * Sets a new web3 provider for 0x.js. Updating the provider will stop all @@ -306,4 +321,14 @@ export class ZeroEx { }); return txReceiptPromise; } + /* + * HACK: `TokenWrapper` needs a token transfer proxy address. `TokenTransferProxy` address is fetched from + * an `ExchangeWrapper`. `ExchangeWrapper` needs `TokenWrapper` to validate orders, creating a dependency cycle. + * In order to break this - we create this function here and pass it as a parameter to the `TokenWrapper` + * and `ProxyWrapper`. + */ + private async _getTokenTransferProxyAddressAsync(): Promise<string> { + const tokenTransferProxyAddress = await (this.exchange as any)._getTokenTransferProxyAddressAsync(); + return tokenTransferProxyAddress; + } } |