diff options
author | Fabio Berger <me@fabioberger.com> | 2017-08-23 23:27:54 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-08-23 23:27:54 +0800 |
commit | fedded3ec1d3051589777a61aba14720432f1d51 (patch) | |
tree | 28ad7ff17a979930dacdc8a7ffd9fdeaed27edf2 /src | |
parent | d837e27739614ce6a132e8583409425f726bef89 (diff) | |
download | dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar.gz dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar.bz2 dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar.lz dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar.xz dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.tar.zst dexon-sol-tools-fedded3ec1d3051589777a61aba14720432f1d51.zip |
Add public method `getTokenMetadataIfExistsAsync` to TokenRegistry wrapper, refactor `getTokensAsync` to use `getTokenMetadataIfExistsAsync` under the hood and added unit tests
Diffstat (limited to 'src')
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index eaaea150d..c78b86a88 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -1,6 +1,7 @@ import * as _ from 'lodash'; import {Web3Wrapper} from '../web3_wrapper'; 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 +21,30 @@ 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.getTokenMetadataIfExistsAsync(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 getTokenMetadataIfExistsAsync(address: string): Promise<Token|undefined> { + 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; |