diff options
author | Fabio Berger <me@fabioberger.com> | 2017-05-30 20:48:10 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-05-30 20:48:10 +0800 |
commit | 1f09470838342e4b9d493b9ddc2eecf65409483e (patch) | |
tree | 4ef197dc2e45fa79f6b5c7686fd88037d519c290 /src/contract_wrappers | |
parent | f4bc7ecf5f72ee32931cf4fa44fffb50505d696d (diff) | |
parent | 6b321ca1c70b9dcf188b2a112015386cba0ad5f2 (diff) | |
download | dexon-sol-tools-1f09470838342e4b9d493b9ddc2eecf65409483e.tar.gz dexon-sol-tools-1f09470838342e4b9d493b9ddc2eecf65409483e.tar.zst dexon-sol-tools-1f09470838342e4b9d493b9ddc2eecf65409483e.zip |
Merge branch 'master' into erc20Wrapper
# Conflicts:
# src/0x.js.ts
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 17 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 19 |
2 files changed, 29 insertions, 7 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index f0f153c2b..3f6eb0dab 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -7,9 +7,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 invalidateContractInstance(): void { + delete this.exchangeContractIfExists; + } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise<boolean> { assert.isHexString('dataHex', dataHex); @@ -19,10 +23,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; + const exchangeContract = await this.getExchangeContractAsync(); - const isValidSignature = await exchangeInstance.isValidSignature.call( + const isValidSignature = await exchangeContract.isValidSignature.call( signerAddressHex, dataHex, ecSignature.v, @@ -34,4 +37,12 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } + private async getExchangeContractAsync(): Promise<ExchangeContract> { + if (!_.isUndefined(this.exchangeContractIfExists)) { + 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 4d6eacb51..86bea1c5d 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -1,20 +1,23 @@ import * as _ from 'lodash'; import {Web3Wrapper} from '../web3_wrapper'; -import {ZeroExError, Token, TokenRegistryContract} from '../types'; +import {Token, TokenRegistryContract, TokenMetadata} from '../types'; import {assert} from '../utils/assert'; 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(): void { + delete this.tokenRegistryContractIfExists; + } public async getTokensAsync(): Promise<Token[]> { - const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); - const tokenRegistryContract = contractInstance as TokenRegistryContract; + const tokenRegistryContract = await this.getTokenRegistryContractAsync(); const addresses = await tokenRegistryContract.getTokenAddresses.call(); - const tokenMetadataPromises: Array<Promise<any[]>> = _.map( + const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map( addresses, (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)), ); @@ -30,4 +33,12 @@ export class TokenRegistryWrapper extends ContractWrapper { }); return tokens; } + private async getTokenRegistryContractAsync(): Promise<TokenRegistryContract> { + if (!_.isUndefined(this.tokenRegistryContractIfExists)) { + return this.tokenRegistryContractIfExists; + } + const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); + this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; + return this.tokenRegistryContractIfExists; + } } |