aboutsummaryrefslogtreecommitdiffstats
path: root/src/contract_wrappers/token_registry_wrapper.ts
blob: 8dd79171e747b99581d88f54fb63f106e7c93a5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as _ from 'lodash';
import {Web3Wrapper} from '../web3_wrapper';
import {Token, TokenRegistryContract, TokenMetadata} 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<TokenMetadata>> = _.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;
    }
}