From 919585cd7972bae1667f67c9fa034251f27b14a8 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 29 May 2017 23:51:27 +0200 Subject: Add TokenRegistryWrapper and getTokensAsync method --- src/0x.js.ts | 3 +++ src/contract_wrappers/token_registry_wrapper.ts | 33 +++++++++++++++++++++++++ src/types.ts | 13 ++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/contract_wrappers/token_registry_wrapper.ts (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index d708a8db6..7e48bc0a5 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -11,6 +11,7 @@ import {assert} from './utils/assert'; import findVersions = require('find-versions'); import compareVersions = require('compare-versions'); import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; +import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; import {SolidityTypes, ECSignature, ZeroExError} from './types'; @@ -19,6 +20,7 @@ const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; export class ZeroEx { public web3Wrapper: Web3Wrapper; public exchange: ExchangeWrapper; + public tokenRegistry: TokenRegistryWrapper; /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ @@ -132,6 +134,7 @@ export class ZeroEx { constructor(web3: Web3) { this.web3Wrapper = new Web3Wrapper(web3); this.exchange = new ExchangeWrapper(this.web3Wrapper); + this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); } /** * Signs an orderHash and returns it's elliptic curve signature diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts new file mode 100644 index 000000000..4d6eacb51 --- /dev/null +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -0,0 +1,33 @@ +import * as _ from 'lodash'; +import {Web3Wrapper} from '../web3_wrapper'; +import {ZeroExError, Token, TokenRegistryContract} from '../types'; +import {assert} from '../utils/assert'; +import {ContractWrapper} from './contract_wrapper'; +import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; + +export class TokenRegistryWrapper extends ContractWrapper { + constructor(web3Wrapper: Web3Wrapper) { + super(web3Wrapper); + } + public async getTokensAsync(): Promise { + const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); + const tokenRegistryContract = contractInstance as TokenRegistryContract; + + const addresses = await tokenRegistryContract.getTokenAddresses.call(); + const tokenMetadataPromises: Array> = _.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; + } +} diff --git a/src/types.ts b/src/types.ts index 3bed01547..273389480 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,8 +30,21 @@ export interface ExchangeContract { isValidSignature: any; } +export interface TokenRegistryContract { + getTokenMetaData: any; + getTokenAddresses: any; +} + export const SolidityTypes = strEnum([ 'address', 'uint256', ]); export type SolidityTypes = keyof typeof SolidityTypes; + +export interface Token { + name: string; + address: string; + symbol: string; + decimals: number; + url: string; +}; -- cgit v1.2.3