Merge pull request #132 from 0xProject/token-registry
Add public Token Registry getters
This commit is contained in:
		@@ -1,12 +1,17 @@
 | 
				
			|||||||
# CHANGELOG
 | 
					# CHANGELOG
 | 
				
			||||||
 | 
					
 | 
				
			||||||
v0.10.0 - _TBD_
 | 
					v0.10.0 - _Aug 24, 2017_
 | 
				
			||||||
------------------------
 | 
					------------------------
 | 
				
			||||||
    * Added `zeroEx.exchange.validateFillOrderThrowIfInvalidAsync` (#128)
 | 
					    * Added `zeroEx.exchange.validateFillOrderThrowIfInvalidAsync` (#128)
 | 
				
			||||||
    * Added `zeroEx.exchange.validateFillOrKillOrderThrowIfInvalidAsync` (#128)
 | 
					    * Added `zeroEx.exchange.validateFillOrKillOrderThrowIfInvalidAsync` (#128)
 | 
				
			||||||
    * Added `zeroEx.exchange.validateCancelOrderThrowIfInvalidAsync` (#128)
 | 
					    * Added `zeroEx.exchange.validateCancelOrderThrowIfInvalidAsync` (#128)
 | 
				
			||||||
    * Added `zeroEx.exchange.isRoundingErrorAsync` (#128)
 | 
					    * Added `zeroEx.exchange.isRoundingErrorAsync` (#128)
 | 
				
			||||||
    * Added `zeroEx.proxy.getContractAddressAsync` (#130)
 | 
					    * Added `zeroEx.proxy.getContractAddressAsync` (#130)
 | 
				
			||||||
 | 
					    * Added `zeroEx.tokenRegistry.getTokenAddressesAsync` (#132)
 | 
				
			||||||
 | 
					    * Added `zeroEx.tokenRegistry.getTokenAddressBySymbolIfExistsAsync` (#132)
 | 
				
			||||||
 | 
					    * Added `zeroEx.tokenRegistry.getTokenAddressByNameIfExistsAsync` (#132)
 | 
				
			||||||
 | 
					    * Added `zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync` (#132)
 | 
				
			||||||
 | 
					    * Added `zeroEx.tokenRegistry.getTokenByNameIfExistsAsync` (#132)
 | 
				
			||||||
    * Added clear error message when checksummed address is passed to a public method (#124)
 | 
					    * Added clear error message when checksummed address is passed to a public method (#124)
 | 
				
			||||||
    * Fixes the description of `shouldThrowOnInsufficientBalanceOrAllowance` in docs (#127)
 | 
					    * Fixes the description of `shouldThrowOnInsufficientBalanceOrAllowance` in docs (#127)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,7 +21,7 @@ export class TokenRegistryWrapper extends ContractWrapper {
 | 
				
			|||||||
    public async getTokensAsync(): Promise<Token[]> {
 | 
					    public async getTokensAsync(): Promise<Token[]> {
 | 
				
			||||||
        const tokenRegistryContract = await this._getTokenRegistryContractAsync();
 | 
					        const tokenRegistryContract = await this._getTokenRegistryContractAsync();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const addresses = await tokenRegistryContract.getTokenAddresses.call();
 | 
					        const addresses = await this.getTokenAddressesAsync();
 | 
				
			||||||
        const tokenPromises: Array<Promise<Token|undefined>> = _.map(
 | 
					        const tokenPromises: Array<Promise<Token|undefined>> = _.map(
 | 
				
			||||||
            addresses,
 | 
					            addresses,
 | 
				
			||||||
            (address: string) => (this.getTokenIfExistsAsync(address)),
 | 
					            (address: string) => (this.getTokenIfExistsAsync(address)),
 | 
				
			||||||
@@ -29,6 +29,15 @@ export class TokenRegistryWrapper extends ContractWrapper {
 | 
				
			|||||||
        const tokens = await Promise.all(tokenPromises);
 | 
					        const tokens = await Promise.all(tokenPromises);
 | 
				
			||||||
        return tokens as Token[];
 | 
					        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.call();
 | 
				
			||||||
 | 
					        return addresses;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Retrieves a token by address currently listed in the Token Registry smart contract
 | 
					     * 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.
 | 
					     * @return  An object that conforms to the Token interface or undefined if token not found.
 | 
				
			||||||
@@ -38,6 +47,42 @@ export class TokenRegistryWrapper extends ContractWrapper {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const tokenRegistryContract = await this._getTokenRegistryContractAsync();
 | 
					        const tokenRegistryContract = await this._getTokenRegistryContractAsync();
 | 
				
			||||||
        const metadata = await tokenRegistryContract.getTokenMetaData.call(address);
 | 
					        const metadata = await tokenRegistryContract.getTokenMetaData.call(address);
 | 
				
			||||||
 | 
					        const token = this._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.call(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.call(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.call(symbol);
 | 
				
			||||||
 | 
					        const token = this._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.call(name);
 | 
				
			||||||
 | 
					        const token = this._createTokenFromMetadata(metadata);
 | 
				
			||||||
 | 
					        return token;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    private _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined {
 | 
				
			||||||
        if (metadata[0] === constants.NULL_ADDRESS) {
 | 
					        if (metadata[0] === constants.NULL_ADDRESS) {
 | 
				
			||||||
            return undefined;
 | 
					            return undefined;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								src/types.ts
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/types.ts
									
									
									
									
									
								
							@@ -145,6 +145,18 @@ export interface TokenRegistryContract extends ContractInstance {
 | 
				
			|||||||
    getTokenAddresses: {
 | 
					    getTokenAddresses: {
 | 
				
			||||||
        call: () => Promise<string[]>;
 | 
					        call: () => Promise<string[]>;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					    getTokenAddressBySymbol: {
 | 
				
			||||||
 | 
					        call: (symbol: string) => Promise<string>;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    getTokenAddressByName: {
 | 
				
			||||||
 | 
					        call: (name: string) => Promise<string>;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    getTokenBySymbol: {
 | 
				
			||||||
 | 
					        call: (symbol: string) => Promise<TokenMetadata>;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    getTokenByName: {
 | 
				
			||||||
 | 
					        call: (name: string) => Promise<TokenMetadata>;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface EtherTokenContract extends ContractInstance {
 | 
					export interface EtherTokenContract extends ContractInstance {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,10 +3,11 @@ import 'mocha';
 | 
				
			|||||||
import * as chai from 'chai';
 | 
					import * as chai from 'chai';
 | 
				
			||||||
import {chaiSetup} from './utils/chai_setup';
 | 
					import {chaiSetup} from './utils/chai_setup';
 | 
				
			||||||
import {web3Factory} from './utils/web3_factory';
 | 
					import {web3Factory} from './utils/web3_factory';
 | 
				
			||||||
import {ZeroEx} from '../src';
 | 
					import {ZeroEx, Token} from '../src';
 | 
				
			||||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
 | 
					import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
 | 
				
			||||||
import {SchemaValidator} from '../src/utils/schema_validator';
 | 
					import {SchemaValidator} from '../src/utils/schema_validator';
 | 
				
			||||||
import {tokenSchema} from '../src/schemas/token_schema';
 | 
					import {tokenSchema} from '../src/schemas/token_schema';
 | 
				
			||||||
 | 
					import {addressSchema} from '../src/schemas/basic_type_schemas';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
chaiSetup.configure();
 | 
					chaiSetup.configure();
 | 
				
			||||||
const expect = chai.expect;
 | 
					const expect = chai.expect;
 | 
				
			||||||
@@ -16,9 +17,25 @@ const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
describe('TokenRegistryWrapper', () => {
 | 
					describe('TokenRegistryWrapper', () => {
 | 
				
			||||||
    let zeroEx: ZeroEx;
 | 
					    let zeroEx: ZeroEx;
 | 
				
			||||||
 | 
					    let tokens: Token[];
 | 
				
			||||||
 | 
					    const tokenAddressBySymbol: {[symbol: string]: string} = {};
 | 
				
			||||||
 | 
					    const tokenAddressByName: {[symbol: string]: string} = {};
 | 
				
			||||||
 | 
					    const tokenBySymbol: {[symbol: string]: Token} = {};
 | 
				
			||||||
 | 
					    const tokenByName: {[symbol: string]: Token} = {};
 | 
				
			||||||
 | 
					    const registeredSymbol = 'ZRX';
 | 
				
			||||||
 | 
					    const registeredName = '0x Protocol Token';
 | 
				
			||||||
 | 
					    const unregisteredSymbol = 'MAL';
 | 
				
			||||||
 | 
					    const unregisteredName = 'Malicious Token';
 | 
				
			||||||
    before(async () => {
 | 
					    before(async () => {
 | 
				
			||||||
        const web3 = web3Factory.create();
 | 
					        const web3 = web3Factory.create();
 | 
				
			||||||
        zeroEx = new ZeroEx(web3.currentProvider);
 | 
					        zeroEx = new ZeroEx(web3.currentProvider);
 | 
				
			||||||
 | 
					        tokens = await zeroEx.tokenRegistry.getTokensAsync();
 | 
				
			||||||
 | 
					        _.map(tokens, token => {
 | 
				
			||||||
 | 
					            tokenAddressBySymbol[token.symbol] = token.address;
 | 
				
			||||||
 | 
					            tokenAddressByName[token.name] = token.address;
 | 
				
			||||||
 | 
					            tokenBySymbol[token.symbol] = token;
 | 
				
			||||||
 | 
					            tokenByName[token.name] = token;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    beforeEach(async () => {
 | 
					    beforeEach(async () => {
 | 
				
			||||||
        await blockchainLifecycle.startAsync();
 | 
					        await blockchainLifecycle.startAsync();
 | 
				
			||||||
@@ -28,7 +45,6 @@ describe('TokenRegistryWrapper', () => {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
    describe('#getTokensAsync', () => {
 | 
					    describe('#getTokensAsync', () => {
 | 
				
			||||||
        it('should return all the tokens added to the tokenRegistry during the migration', async () => {
 | 
					        it('should return all the tokens added to the tokenRegistry during the migration', async () => {
 | 
				
			||||||
            const tokens = await zeroEx.tokenRegistry.getTokensAsync();
 | 
					 | 
				
			||||||
            expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);
 | 
					            expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const schemaValidator = new SchemaValidator();
 | 
					            const schemaValidator = new SchemaValidator();
 | 
				
			||||||
@@ -38,9 +54,61 @@ describe('TokenRegistryWrapper', () => {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    describe('#getTokenAddressesAsync', () => {
 | 
				
			||||||
 | 
					        it('should return all the token addresses added to the tokenRegistry during the migration', async () => {
 | 
				
			||||||
 | 
					            const tokenAddresses = await zeroEx.tokenRegistry.getTokenAddressesAsync();
 | 
				
			||||||
 | 
					            expect(tokenAddresses).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const schemaValidator = new SchemaValidator();
 | 
				
			||||||
 | 
					            _.each(tokenAddresses, tokenAddress => {
 | 
				
			||||||
 | 
					                const validationResult = schemaValidator.validate(tokenAddress, addressSchema);
 | 
				
			||||||
 | 
					                expect(validationResult.errors).to.have.lengthOf(0);
 | 
				
			||||||
 | 
					                expect(tokenAddress).to.not.be.equal(ZeroEx.NULL_ADDRESS);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    describe('#getTokenAddressBySymbol', () => {
 | 
				
			||||||
 | 
					        it('should return correct address for a token in the registry', async () => {
 | 
				
			||||||
 | 
					            const tokenAddress = await zeroEx.tokenRegistry.getTokenAddressBySymbolIfExistsAsync(registeredSymbol);
 | 
				
			||||||
 | 
					            expect(tokenAddress).to.be.equal(tokenAddressBySymbol[registeredSymbol]);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it('should return undefined for a token out of registry', async () => {
 | 
				
			||||||
 | 
					            const tokenAddress = await zeroEx.tokenRegistry.getTokenAddressBySymbolIfExistsAsync(unregisteredSymbol);
 | 
				
			||||||
 | 
					            expect(tokenAddress).to.be.undefined();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    describe('#getTokenAddressByName', () => {
 | 
				
			||||||
 | 
					        it('should return correct address for a token in the registry', async () => {
 | 
				
			||||||
 | 
					            const tokenAddress = await zeroEx.tokenRegistry.getTokenAddressByNameIfExistsAsync(registeredName);
 | 
				
			||||||
 | 
					            expect(tokenAddress).to.be.equal(tokenAddressByName[registeredName]);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it('should return undefined for a token out of registry', async () => {
 | 
				
			||||||
 | 
					            const tokenAddress = await zeroEx.tokenRegistry.getTokenAddressByNameIfExistsAsync(unregisteredName);
 | 
				
			||||||
 | 
					            expect(tokenAddress).to.be.undefined();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    describe('#getTokenBySymbol', () => {
 | 
				
			||||||
 | 
					        it('should return correct token for a token in the registry', async () => {
 | 
				
			||||||
 | 
					            const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(registeredSymbol);
 | 
				
			||||||
 | 
					            expect(token).to.be.deep.equal(tokenBySymbol[registeredSymbol]);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it('should return undefined for a token out of registry', async () => {
 | 
				
			||||||
 | 
					            const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(unregisteredSymbol);
 | 
				
			||||||
 | 
					            expect(token).to.be.undefined();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    describe('#getTokenByName', () => {
 | 
				
			||||||
 | 
					        it('should return correct token for a token in the registry', async () => {
 | 
				
			||||||
 | 
					            const token = await zeroEx.tokenRegistry.getTokenByNameIfExistsAsync(registeredName);
 | 
				
			||||||
 | 
					            expect(token).to.be.deep.equal(tokenByName[registeredName]);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it('should return undefined for a token out of registry', async () => {
 | 
				
			||||||
 | 
					            const token = await zeroEx.tokenRegistry.getTokenByNameIfExistsAsync(unregisteredName);
 | 
				
			||||||
 | 
					            expect(token).to.be.undefined();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
    describe('#getTokenIfExistsAsync', () => {
 | 
					    describe('#getTokenIfExistsAsync', () => {
 | 
				
			||||||
        it('should return the token added to the tokenRegistry during the migration', async () => {
 | 
					        it('should return the token added to the tokenRegistry during the migration', async () => {
 | 
				
			||||||
            const tokens = await zeroEx.tokenRegistry.getTokensAsync();
 | 
					 | 
				
			||||||
            const aToken = tokens[0];
 | 
					            const aToken = tokens[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const token = await zeroEx.tokenRegistry.getTokenIfExistsAsync(aToken.address);
 | 
					            const token = await zeroEx.tokenRegistry.getTokenIfExistsAsync(aToken.address);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user