diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-08-24 16:45:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-24 16:45:31 +0800 |
commit | 640825d18d197f6950f1eede0b43bb93be21a6ce (patch) | |
tree | da7ace561f495b033dc5f977a2bde7de6afdb827 /src | |
parent | e216f2ff6de9b2dd998c37c919b8786f7dff1101 (diff) | |
parent | 933242bd9d83ccbf92ce29aa2b4606336e64eed7 (diff) | |
download | dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar.gz dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar.bz2 dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar.lz dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar.xz dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.tar.zst dexon-sol-tools-640825d18d197f6950f1eede0b43bb93be21a6ce.zip |
Merge pull request #132 from 0xProject/token-registry
Add public Token Registry getters
Diffstat (limited to 'src')
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 47 | ||||
-rw-r--r-- | src/types.ts | 12 |
2 files changed, 58 insertions, 1 deletions
diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 58c191ea5..5fee1304e 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -21,7 +21,7 @@ export class TokenRegistryWrapper extends ContractWrapper { public async getTokensAsync(): Promise<Token[]> { const tokenRegistryContract = await this._getTokenRegistryContractAsync(); - const addresses = await tokenRegistryContract.getTokenAddresses.call(); + const addresses = await this.getTokenAddressesAsync(); const tokenPromises: Array<Promise<Token|undefined>> = _.map( addresses, (address: string) => (this.getTokenIfExistsAsync(address)), @@ -30,6 +30,15 @@ export class TokenRegistryWrapper extends ContractWrapper { return tokens as Token[]; } /** + * Retrieves all the addresses of the tokens currently listed in the Token Registry smart contract + * @return An array of token addresses. + */ + public async getTokenAddressesAsync(): Promise<string[]> { + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const addresses = await tokenRegistryContract.getTokenAddresses.call(); + return addresses; + } + /** * 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. */ @@ -38,6 +47,42 @@ export class TokenRegistryWrapper extends ContractWrapper { const tokenRegistryContract = await this._getTokenRegistryContractAsync(); const metadata = await tokenRegistryContract.getTokenMetaData.call(address); + const token = this._createTokenFromMetadata(metadata); + return token; + } + public async getTokenAddressBySymbolIfExistsAsync(symbol: string): Promise<string|undefined> { + assert.isString('symbol', symbol); + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const addressIfExists = await tokenRegistryContract.getTokenAddressBySymbol.call(symbol); + if (addressIfExists === constants.NULL_ADDRESS) { + return undefined; + } + return addressIfExists; + } + public async getTokenAddressByNameIfExistsAsync(name: string): Promise<string|undefined> { + assert.isString('name', name); + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const addressIfExists = await tokenRegistryContract.getTokenAddressByName.call(name); + if (addressIfExists === constants.NULL_ADDRESS) { + return undefined; + } + return addressIfExists; + } + public async getTokenBySymbolIfExistsAsync(symbol: string): Promise<Token|undefined> { + assert.isString('symbol', symbol); + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const metadata = await tokenRegistryContract.getTokenBySymbol.call(symbol); + const token = this._createTokenFromMetadata(metadata); + return token; + } + public async getTokenByNameIfExistsAsync(name: string): Promise<Token|undefined> { + assert.isString('name', name); + const tokenRegistryContract = await this._getTokenRegistryContractAsync(); + const metadata = await tokenRegistryContract.getTokenByName.call(name); + const token = this._createTokenFromMetadata(metadata); + return token; + } + private _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined { if (metadata[0] === constants.NULL_ADDRESS) { return undefined; } diff --git a/src/types.ts b/src/types.ts index 5ba85a7d5..71227647b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -145,6 +145,18 @@ export interface TokenRegistryContract extends ContractInstance { getTokenAddresses: { call: () => Promise<string[]>; }; + getTokenAddressBySymbol: { + call: (symbol: string) => Promise<string>; + }; + getTokenAddressByName: { + call: (name: string) => Promise<string>; + }; + getTokenBySymbol: { + call: (symbol: string) => Promise<TokenMetadata>; + }; + getTokenByName: { + call: (name: string) => Promise<TokenMetadata>; + }; } export interface EtherTokenContract extends ContractInstance { |