diff options
author | Fabio Berger <me@fabioberger.com> | 2017-08-24 00:22:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-24 00:22:50 +0800 |
commit | 18f2a93950500e3efec7ae25e32ff644ebb7efcc (patch) | |
tree | 7447497305248c6ac6d061ce590f5ed5222f2aa0 /src/contract_wrappers | |
parent | bd7102efbee0833bd0274edbe248db27b53aa950 (diff) | |
parent | 98be7867649337a18441e517790c748c722ee730 (diff) | |
download | dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar.gz dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar.bz2 dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar.lz dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar.xz dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.tar.zst dexon-sol-tools-18f2a93950500e3efec7ae25e32ff644ebb7efcc.zip |
Merge pull request #131 from 0xProject/addTokenRegistryMethod
Add public method to TokenRegistry
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index eaaea150d..58c191ea5 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -1,6 +1,8 @@ import * as _ from 'lodash'; import {Web3Wrapper} from '../web3_wrapper'; +import {assert} from '../utils/assert'; import {Token, TokenRegistryContract, TokenMetadata} from '../types'; +import {constants} from '../utils/constants'; import {ContractWrapper} from './contract_wrapper'; import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; @@ -20,20 +22,32 @@ export class TokenRegistryWrapper extends ContractWrapper { const tokenRegistryContract = await this._getTokenRegistryContractAsync(); const addresses = await tokenRegistryContract.getTokenAddresses.call(); - const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map( + const tokenPromises: Array<Promise<Token|undefined>> = _.map( addresses, - (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)), + (address: string) => (this.getTokenIfExistsAsync(address)), ); - const tokensMetadata = await Promise.all(tokenMetadataPromises); - const tokens = _.map(tokensMetadata, metadata => { - return { - address: metadata[0], - name: metadata[1], - symbol: metadata[2], - decimals: metadata[3].toNumber(), - }; - }); - return tokens; + const tokens = await Promise.all(tokenPromises); + return tokens as Token[]; + } + /** + * Retrieves a token by address currently listed in the Token Registry smart contract + * @return An object that conforms to the Token interface or undefined if token not found. + */ + public async getTokenIfExistsAsync(address: string): Promise<Token|undefined> { + assert.isETHAddressHex('address', address); + + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const metadata = await tokenRegistryContract.getTokenMetaData.call(address); + if (metadata[0] === constants.NULL_ADDRESS) { + return undefined; + } + const token = { + address: metadata[0], + name: metadata[1], + symbol: metadata[2], + decimals: metadata[3].toNumber(), + }; + return token; } private _invalidateContractInstance(): void { delete this._tokenRegistryContractIfExists; |