diff options
author | Fabio Berger <me@fabioberger.com> | 2017-05-30 05:51:27 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-05-30 05:51:27 +0800 |
commit | 919585cd7972bae1667f67c9fa034251f27b14a8 (patch) | |
tree | 3562cb97333bcb42e62ef575fce321f3c1b416f8 | |
parent | 6cb9bd4426bff5885f3b453d60a6a00f048abb6b (diff) | |
download | dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar.gz dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar.bz2 dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar.lz dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar.xz dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.tar.zst dexon-sol-tools-919585cd7972bae1667f67c9fa034251f27b14a8.zip |
Add TokenRegistryWrapper and getTokensAsync method
-rw-r--r-- | src/0x.js.ts | 3 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 33 | ||||
-rw-r--r-- | src/types.ts | 13 |
3 files changed, 49 insertions, 0 deletions
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<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; + } +} 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; +}; |