Add TokenRegistryWrapper and getTokensAsync method

This commit is contained in:
Fabio Berger 2017-05-29 23:51:27 +02:00
parent 6cb9bd4426
commit 919585cd79
3 changed files with 49 additions and 0 deletions

View File

@ -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

View File

@ -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<Token[]> {
const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
const tokenRegistryContract = contractInstance as TokenRegistryContract;
const addresses = await tokenRegistryContract.getTokenAddresses.call();
const tokenMetadataPromises: Array<Promise<any[]>> = _.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;
}
}

View File

@ -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;
};