diff options
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 14 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 44 |
2 files changed, 57 insertions, 1 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 7a80c5800..66e53a7d4 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -26,9 +26,13 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrs.ERROR_FILL_TRUNCATION]: 'The rounding error was too large when filling this order', [ExchangeContractErrs.ERROR_FILL_BALANCE_ALLOWANCE]: 'Maker or taker has insufficient balance or allowance', }; + 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); @@ -36,7 +40,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.isETHAddressHex('signerAddressHex', signerAddressHex); const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); - const exchangeInstance = await this.getExchangeInstanceOrThrowAsync(); + const exchangeInstance = await this.getExchangeContractAsync(); const isValidSignature = await exchangeInstance.isValidSignature.call( signerAddressHex, @@ -102,4 +106,12 @@ export class ExchangeWrapper extends ContractWrapper { throw new Error(humanReadableErrMessage); } } + 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 new file mode 100644 index 000000000..86bea1c5d --- /dev/null +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -0,0 +1,44 @@ +import * as _ from 'lodash'; +import {Web3Wrapper} from '../web3_wrapper'; +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 tokenRegistryContract = await this.getTokenRegistryContractAsync(); + + const addresses = await tokenRegistryContract.getTokenAddresses.call(); + const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map( + addresses, + (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)), + ); + const tokensMetadata = await Promise.all(tokenMetadataPromises); + const tokens = _.map(tokensMetadata, metadata => { + return { + address: metadata[0], + name: metadata[1], + symbol: metadata[2], + url: metadata[3], + decimals: metadata[4].toNumber(), + }; + }); + 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; + } +} |