diff options
author | Fabio Berger <me@fabioberger.com> | 2017-05-30 18:27:27 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-05-30 18:27:27 +0800 |
commit | 87491e67414aad0fc30b3df4610bfd8e2ac4b96d (patch) | |
tree | 4954842ce1744a891d796691ccf9187e62cd05ba /src/contract_wrappers/exchange_wrapper.ts | |
parent | c536114b1638e30e5053dfc33d8e16465c0d373c (diff) | |
download | dexon-0x-contracts-87491e67414aad0fc30b3df4610bfd8e2ac4b96d.tar.gz dexon-0x-contracts-87491e67414aad0fc30b3df4610bfd8e2ac4b96d.tar.zst dexon-0x-contracts-87491e67414aad0fc30b3df4610bfd8e2ac4b96d.zip |
Lazily instantiate exchangeContract and store as instance variable instead of re-instantiating on every call
Diffstat (limited to 'src/contract_wrappers/exchange_wrapper.ts')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 17 |
1 files changed, 14 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<boolean> { 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; + } } |