From 87491e67414aad0fc30b3df4610bfd8e2ac4b96d Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:27:27 +0200 Subject: Lazily instantiate exchangeContract and store as instance variable instead of re-instantiating on every call --- src/contract_wrappers/exchange_wrapper.ts | 17 ++++++++++++++--- src/types.ts | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index f0f153c2b..a7abbb078 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {ECSignature, ZeroExError, ExchangeContract} from '../types'; import {assert} from '../utils/assert'; @@ -7,9 +8,13 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; export class ExchangeWrapper extends ContractWrapper { + private exchangeContractIfExists: ExchangeContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } + public invalidateExchangeContract() { + delete this.exchangeContractIfExists; + } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise { assert.isHexString('dataHex', dataHex); @@ -19,10 +24,9 @@ export class ExchangeWrapper extends ContractWrapper { const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); - const exchangeInstance = contractInstance as ExchangeContract; + await this.instantiateExchangeContractIfDoesntExistAsync(); - const isValidSignature = await exchangeInstance.isValidSignature.call( + const isValidSignature = await this.exchangeContractIfExists.isValidSignature.call( signerAddressHex, dataHex, ecSignature.v, @@ -34,4 +38,11 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } + private async instantiateExchangeContractIfDoesntExistAsync() { + if (!_.isUndefined(this.exchangeContractIfExists)) { + return; + } + const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + this.exchangeContractIfExists = contractInstance as ExchangeContract; + } } diff --git a/src/types.ts b/src/types.ts index 3bed01547..50008404b 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 @@ -28,6 +29,8 @@ export interface ECSignature { export interface ExchangeContract { isValidSignature: any; + currentProvider: Web3.Provider; + setProvider: (provider: Web3.Provider) => void; } export const SolidityTypes = strEnum([ -- cgit v1.2.3 From 55b00ab3809973e385b6baffd9c44d6db5540b12 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:28:07 +0200 Subject: Add setProvider method to 0x.js that updates the web3 provider and invalidates any contractInstances instantiated with the old provider --- src/0x.js.ts | 8 ++++++++ src/web3_wrapper.ts | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/0x.js.ts b/src/0x.js.ts index 11dbe3058..4b7edc0e9 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -133,6 +133,14 @@ export class ZeroEx { this.web3Wrapper = new Web3Wrapper(web3); this.exchange = new ExchangeWrapper(this.web3Wrapper); } + /** + * Sets a new provider for the web3 instance used by 0x.js internally and invalidates any instantiated + * contract instances created with the old provider. + */ + public setProvider(provider: Web3.Provider) { + this.web3Wrapper.setProvider(provider); + this.exchange.invalidateExchangeContract(); + } /** * Signs an orderHash and returns it's elliptic curve signature * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 97d04db8c..a532085ce 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -9,6 +9,9 @@ export class Web3Wrapper { this.web3 = new Web3(); this.web3.setProvider(web3.currentProvider); } + public setProvider(provider: Web3.Provider) { + this.web3.setProvider(provider); + } public isAddress(address: string): boolean { return this.web3.isAddress(address); } -- cgit v1.2.3 From d15002a1109ea00f4d92c7ae6259b5b657f5e4b3 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:30:38 +0200 Subject: Add tests for setProvider --- test/0x.js_test.ts | 23 +++++++++++++++++++++++ test/utils/web3_factory.ts | 10 +++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index c45c70991..1e76ae71a 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -4,6 +4,7 @@ import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); import * as Sinon from 'sinon'; +import ProviderEngine = require('web3-provider-engine'); import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; @@ -13,6 +14,28 @@ chai.use(ChaiBigNumber()); const expect = chai.expect; describe('ZeroEx library', () => { + describe('#setProvider', () => { + it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { + const web3 = web3Factory.create(); + const zeroEx = new ZeroEx(web3); + // Instantiate the exchangeContract instance with the current provider + await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); + + const newProvider = web3Factory.getRpcProvider(); + // Add property to newProvider so that we can differentiate it from old provider + (newProvider as any).zeroExTestId = 1; + zeroEx.setProvider(newProvider); + + // Check that exchangeContract instance removed after provider update + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); + + // Check that all nested web3 instances return the updated provider + const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const contractWrapperWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + }); + }); describe('#getOrderHash', () => { const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; it('defaults takerAddress to NULL address', () => { diff --git a/test/utils/web3_factory.ts b/test/utils/web3_factory.ts index 493fbc2df..ffdc0e4cf 100644 --- a/test/utils/web3_factory.ts +++ b/test/utils/web3_factory.ts @@ -10,14 +10,18 @@ import {constants} from './constants'; export const web3Factory = { create(): Web3 { + const provider = this.getRpcProvider(); + const web3 = new Web3(); + web3.setProvider(provider); + return web3; + }, + getRpcProvider(): Web3.Provider { const provider = new ProviderEngine(); const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`; provider.addProvider(new RpcSubprovider({ rpcUrl, })); provider.start(); - const web3 = new Web3(); - web3.setProvider(provider); - return web3; + return provider; }, }; -- cgit v1.2.3 From cb31b21d28c20a0153199e9388202301e1385bfe Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:43:06 +0200 Subject: rename invalidateExchangeContract to invalidateContractInstance --- src/0x.js.ts | 2 +- src/contract_wrappers/exchange_wrapper.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/0x.js.ts b/src/0x.js.ts index 3dfdfbfcd..506ac4187 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -142,7 +142,7 @@ export class ZeroEx { */ public setProvider(provider: Web3.Provider) { this.web3Wrapper.setProvider(provider); - this.exchange.invalidateExchangeContract(); + this.exchange.invalidateContractInstance(); } /** * Signs an orderHash and returns it's elliptic curve signature diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index a7abbb078..d2f7a8f42 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -12,7 +12,7 @@ export class ExchangeWrapper extends ContractWrapper { constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public invalidateExchangeContract() { + public invalidateContractInstance() { delete this.exchangeContractIfExists; } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, -- cgit v1.2.3 From a3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:43:50 +0200 Subject: Make tokenRegistry contract instantiation lazy and clear it on provider update --- src/0x.js.ts | 1 + src/contract_wrappers/token_registry_wrapper.ts | 18 ++++++++++++++---- test/0x.js_test.ts | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/0x.js.ts b/src/0x.js.ts index 506ac4187..c621a15b6 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -143,6 +143,7 @@ export class ZeroEx { public setProvider(provider: Web3.Provider) { this.web3Wrapper.setProvider(provider); this.exchange.invalidateContractInstance(); + this.tokenRegistry.invalidateContractInstance(); } /** * Signs an orderHash and returns it's elliptic curve signature diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 8dd79171e..69bad2bac 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -6,17 +6,20 @@ import {ContractWrapper} from './contract_wrapper'; import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; export class TokenRegistryWrapper extends ContractWrapper { + private tokenRegistryContractIfExists: TokenRegistryContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } + public invalidateContractInstance() { + delete this.tokenRegistryContractIfExists; + } public async getTokensAsync(): Promise { - const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); - const tokenRegistryContract = contractInstance as TokenRegistryContract; + await this.instantiateTokenRegistryContractIfDoesntExistAsync(); - const addresses = await tokenRegistryContract.getTokenAddresses.call(); + const addresses = await this.tokenRegistryContractIfExists.getTokenAddresses.call(); const tokenMetadataPromises: Array> = _.map( addresses, - (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)), + (address: string) => (this.tokenRegistryContractIfExists.getTokenMetaData.call(address)), ); const tokensMetadata = await Promise.all(tokenMetadataPromises); const tokens = _.map(tokensMetadata, metadata => { @@ -30,4 +33,11 @@ export class TokenRegistryWrapper extends ContractWrapper { }); return tokens; } + private async instantiateTokenRegistryContractIfDoesntExistAsync() { + if (!_.isUndefined(this.tokenRegistryContractIfExists)) { + return; + } + const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); + this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; + } } diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 1e76ae71a..4125b83fe 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -26,8 +26,9 @@ describe('ZeroEx library', () => { (newProvider as any).zeroExTestId = 1; zeroEx.setProvider(newProvider); - // Check that exchangeContract instance removed after provider update + // Check that contractInstances with old provider are removed after provider update expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.an('undefined'); // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); -- cgit v1.2.3 From b837021e6ec7de6fc9b6591d40456cb5aba2f748 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:47:46 +0200 Subject: improve comment --- src/0x.js.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/0x.js.ts b/src/0x.js.ts index c621a15b6..4c67027fc 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -138,7 +138,7 @@ export class ZeroEx { } /** * Sets a new provider for the web3 instance used by 0x.js internally and invalidates any instantiated - * contract instances created with the old provider. + * contract instances instantiated with the old provider. */ public setProvider(provider: Web3.Provider) { this.web3Wrapper.setProvider(provider); -- cgit v1.2.3 From e2d0fd9cf9a712d44470e29dc2c7b95262e34d2c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:47:53 +0200 Subject: remove unused import --- test/exchange_wrapper.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/exchange_wrapper.ts b/test/exchange_wrapper.ts index 4bfc49374..55b84ce36 100644 --- a/test/exchange_wrapper.ts +++ b/test/exchange_wrapper.ts @@ -1,7 +1,6 @@ import 'mocha'; import * as chai from 'chai'; import chaiAsPromised = require('chai-as-promised'); -import * as Web3 from 'web3'; import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -- cgit v1.2.3 From f78f3b1bcd7614feee61d828f25c6c12b229d4ac Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:48:46 +0200 Subject: remove unused import --- src/contract_wrappers/exchange_wrapper.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index d2f7a8f42..90b9a39d2 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,5 +1,4 @@ import * as _ from 'lodash'; -import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {ECSignature, ZeroExError, ExchangeContract} from '../types'; import {assert} from '../utils/assert'; -- cgit v1.2.3 From 3d3297834c0799051e47ce4c0185614d5ae81ae3 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:50:29 +0200 Subject: remove unused declarations --- src/types.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/types.ts b/src/types.ts index f17beafd2..6fce95706 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,4 @@ 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 @@ -29,8 +28,6 @@ export interface ECSignature { export interface ExchangeContract { isValidSignature: any; - currentProvider: Web3.Provider; - setProvider: (provider: Web3.Provider) => void; } export interface TokenRegistryContract { -- cgit v1.2.3 From 6aded80b2f1fd630323cdc97a4d66f1e82ce1632 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:51:34 +0200 Subject: remove unused import --- test/0x.js_test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 4125b83fe..496b2780a 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -4,7 +4,6 @@ import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); import * as Sinon from 'sinon'; -import ProviderEngine = require('web3-provider-engine'); import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; -- cgit v1.2.3 From 007c57ec4049ce4c929cbf97ca000a375570aeb4 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:52:28 +0200 Subject: Also instantiate tokenRegistry contract instance --- test/0x.js_test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 496b2780a..21e4b5a3b 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -17,8 +17,9 @@ describe('ZeroEx library', () => { it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); - // Instantiate the exchangeContract instance with the current provider + // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); + await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider -- cgit v1.2.3 From eefc8ca7ecb0543da3dad54ad1146eacb5a446f6 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:53:42 +0200 Subject: add assertions that contractInstances are not undefined before provider update --- test/0x.js_test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 21e4b5a3b..041c5b433 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -20,6 +20,8 @@ describe('ZeroEx library', () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider -- cgit v1.2.3 From 8561258ded12bc7d284933e61f6d052941258f40 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 12:56:10 +0200 Subject: Also test that web3Wrapper nested under tokenRegistryWrapper has updated provider --- test/0x.js_test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 041c5b433..0c2afe879 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -35,8 +35,10 @@ describe('ZeroEx library', () => { // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); - const contractWrapperWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); - expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const exchangeWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + expect((exchangeWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const tokenRegistryWeb3WrapperProvider = zeroEx.tokenRegistry.web3Wrapper.getCurrentProvider(); + expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); }); }); describe('#getOrderHash', () => { -- cgit v1.2.3 From ad14c307cec8e0e7621b47e9e1ecb2652985190f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:23:44 +0200 Subject: Refactor exchangeWrapper and tokenRegistryWrapper to use contract getter that instantiates contract instance if not already instantiated --- src/contract_wrappers/exchange_wrapper.ts | 13 +++++++------ src/contract_wrappers/token_registry_wrapper.ts | 15 ++++++++------- test/0x.js_test.ts | 4 ++-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 90b9a39d2..3f6eb0dab 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -7,11 +7,11 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; export class ExchangeWrapper extends ContractWrapper { - private exchangeContractIfExists: ExchangeContract; + private exchangeContractIfExists?: ExchangeContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public invalidateContractInstance() { + public invalidateContractInstance(): void { delete this.exchangeContractIfExists; } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, @@ -23,9 +23,9 @@ export class ExchangeWrapper extends ContractWrapper { const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - await this.instantiateExchangeContractIfDoesntExistAsync(); + const exchangeContract = await this.getExchangeContractAsync(); - const isValidSignature = await this.exchangeContractIfExists.isValidSignature.call( + const isValidSignature = await exchangeContract.isValidSignature.call( signerAddressHex, dataHex, ecSignature.v, @@ -37,11 +37,12 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } - private async instantiateExchangeContractIfDoesntExistAsync() { + private async getExchangeContractAsync(): Promise { if (!_.isUndefined(this.exchangeContractIfExists)) { - return; + return this.exchangeContractIfExists; } const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); this.exchangeContractIfExists = contractInstance as ExchangeContract; + return this.exchangeContractIfExists; } } diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 69bad2bac..86bea1c5d 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -6,20 +6,20 @@ import {ContractWrapper} from './contract_wrapper'; import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; export class TokenRegistryWrapper extends ContractWrapper { - private tokenRegistryContractIfExists: TokenRegistryContract; + private tokenRegistryContractIfExists?: TokenRegistryContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public invalidateContractInstance() { + public invalidateContractInstance(): void { delete this.tokenRegistryContractIfExists; } public async getTokensAsync(): Promise { - await this.instantiateTokenRegistryContractIfDoesntExistAsync(); + const tokenRegistryContract = await this.getTokenRegistryContractAsync(); - const addresses = await this.tokenRegistryContractIfExists.getTokenAddresses.call(); + const addresses = await tokenRegistryContract.getTokenAddresses.call(); const tokenMetadataPromises: Array> = _.map( addresses, - (address: string) => (this.tokenRegistryContractIfExists.getTokenMetaData.call(address)), + (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)), ); const tokensMetadata = await Promise.all(tokenMetadataPromises); const tokens = _.map(tokensMetadata, metadata => { @@ -33,11 +33,12 @@ export class TokenRegistryWrapper extends ContractWrapper { }); return tokens; } - private async instantiateTokenRegistryContractIfDoesntExistAsync() { + private async getTokenRegistryContractAsync(): Promise { if (!_.isUndefined(this.tokenRegistryContractIfExists)) { - return; + return this.tokenRegistryContractIfExists; } const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; + return this.tokenRegistryContractIfExists; } } diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 0c2afe879..b404b27ba 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -18,8 +18,8 @@ describe('ZeroEx library', () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); // Instantiate the contract instances with the current provider - await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); - await (zeroEx.tokenRegistry as any).instantiateTokenRegistryContractIfDoesntExistAsync(); + await (zeroEx.exchange as any).getExchangeContractAsync(); + await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync(); expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); -- cgit v1.2.3 From 3522f94ff6cde1aad83299def7308025da9432d5 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:26:07 +0200 Subject: use .be.undefined instead of .be.an('undefined') --- test/0x.js_test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index b404b27ba..5d23d7094 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -20,8 +20,8 @@ describe('ZeroEx library', () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).getExchangeContractAsync(); await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync(); - expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.an('undefined'); - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.an('undefined'); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined; + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined; const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider @@ -29,8 +29,8 @@ describe('ZeroEx library', () => { zeroEx.setProvider(newProvider); // Check that contractInstances with old provider are removed after provider update - expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.an('undefined'); + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined; + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.undefined; // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); -- cgit v1.2.3