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 ++++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'src') 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; } } -- cgit v1.2.3