aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts
blob: 35337fa35a3fffe31a393f6f31885311ed363b28 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as _ from 'lodash';

import {artifacts} from '../artifacts';
import {Token, TokenMetadata, TokenRegistryContract, ZeroExError} from '../types';
import {assert} from '../utils/assert';
import {constants} from '../utils/constants';
import {Web3Wrapper} from '../web3_wrapper';

import {ContractWrapper} from './contract_wrapper';

/**
 * This class includes all the functionality related to interacting with the 0x Token Registry smart contract.
 */
export class TokenRegistryWrapper extends ContractWrapper {
    private _tokenRegistryContractIfExists?: TokenRegistryContract;
    private _contractAddressIfExists?: string;
    private static _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined {
        if (metadata[0] === constants.NULL_ADDRESS) {
            return undefined;
        }
        const token = {
            address: metadata[0],
            name: metadata[1],
            symbol: metadata[2],
            decimals: metadata[3].toNumber(),
        };
        return token;
    }
    constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
        super(web3Wrapper);
        this._contractAddressIfExists = contractAddressIfExists;
    }
    /**
     * Retrieves all the tokens currently listed in the Token Registry smart contract
     * @return  An array of objects that conform to the Token interface.
     */
    public async getTokensAsync(): Promise<Token[]> {
        const tokenRegistryContract = await this._getTokenRegistryContractAsync();

        const addresses = await this.getTokenAddressesAsync();
        const tokenPromises: Array<Promise<Token|undefined>> = _.map(
            addresses,
            async (address: string) => this.getTokenIfExistsAsync(address),
        );
        const tokens = await Promise.all(tokenPromises);
        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.callAsync();
        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.
     */
    public async getTokenIfExistsAsync(address: string): Promise<Token|undefined> {
        assert.isETHAddressHex('address', address);

        const tokenRegistryContract = await this._getTokenRegistryContractAsync();
        const metadata = await tokenRegistryContract.getTokenMetaData.callAsync(address);
        const token = TokenRegistryWrapper._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.callAsync(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.callAsync(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.callAsync(symbol);
        const token = TokenRegistryWrapper._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.callAsync(name);
        const token = TokenRegistryWrapper._createTokenFromMetadata(metadata);
        return token;
    }
    /**
     * Retrieves the Ethereum address of the TokenRegistry contract deployed on the network
     * that the user-passed web3 provider is connected to.
     * @returns The Ethereum address of the TokenRegistry contract being used.
     */
    public getContractAddress(): string {
        const contractAddress = this._getContractAddress(
            artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
        );
        return contractAddress;
    }
    private _invalidateContractInstance(): void {
        delete this._tokenRegistryContractIfExists;
    }
    private async _getTokenRegistryContractAsync(): Promise<TokenRegistryContract> {
        if (!_.isUndefined(this._tokenRegistryContractIfExists)) {
            return this._tokenRegistryContractIfExists;
        }
        const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>(
            artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
        );
        this._tokenRegistryContractIfExists = contractInstance;
        return this._tokenRegistryContractIfExists;
    }
}