Merge pull request #24 from 0xProject/tokenRegistry

TokenRegistryWrapper
This commit is contained in:
Fabio Berger 2017-05-30 12:37:25 +02:00 committed by GitHub
commit 911ab437b8
6 changed files with 113 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import {assert} from './utils/assert';
import findVersions = require('find-versions'); import findVersions = require('find-versions');
import compareVersions = require('compare-versions'); import compareVersions = require('compare-versions');
import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
import {ecSignatureSchema} from './schemas/ec_signature_schema'; import {ecSignatureSchema} from './schemas/ec_signature_schema';
import {SolidityTypes, ECSignature, ZeroExError} from './types'; import {SolidityTypes, ECSignature, ZeroExError} from './types';
@ -18,6 +19,7 @@ const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
export class ZeroEx { export class ZeroEx {
public exchange: ExchangeWrapper; public exchange: ExchangeWrapper;
public tokenRegistry: TokenRegistryWrapper;
private web3Wrapper: Web3Wrapper; private web3Wrapper: Web3Wrapper;
/** /**
* Computes the orderHash given the order parameters and returns it as a hex encoded string. * 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) { constructor(web3: Web3) {
this.web3Wrapper = new Web3Wrapper(web3); this.web3Wrapper = new Web3Wrapper(web3);
this.exchange = new ExchangeWrapper(this.web3Wrapper); this.exchange = new ExchangeWrapper(this.web3Wrapper);
this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper);
} }
/** /**
* Signs an orderHash and returns it's elliptic curve signature * 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 {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;
}
}

View File

@ -0,0 +1,12 @@
export const tokenSchema = {
id: '/token',
properties: {
name: {type: 'string'},
symbol: {type: 'string'},
decimals: {type: 'number'},
address: {type: 'string'},
url: {type: 'string'},
},
required: ['name', 'symbol', 'decimals', 'address', 'url'],
type: 'object',
};

View File

@ -30,8 +30,28 @@ export interface ExchangeContract {
isValidSignature: any; isValidSignature: any;
} }
export interface TokenRegistryContract {
getTokenMetaData: {
call: (address: string) => Promise<TokenMetadata>;
};
getTokenAddresses: {
call: () => Promise<string[]>;
};
}
export const SolidityTypes = strEnum([ export const SolidityTypes = strEnum([
'address', 'address',
'uint256', 'uint256',
]); ]);
export type SolidityTypes = keyof typeof SolidityTypes; export type SolidityTypes = keyof typeof SolidityTypes;
// [address, name, symbol, projectUrl, decimals, ipfsHash, swarmHash]
export type TokenMetadata = [string, string, string, string, BigNumber.BigNumber, string, string];
export interface Token {
name: string;
address: string;
symbol: string;
decimals: number;
url: string;
};

View File

@ -1,5 +1,6 @@
import {Validator, ValidatorResult} from 'jsonschema'; import {Validator, ValidatorResult} from 'jsonschema';
import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema'; import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema';
import {tokenSchema} from '../schemas/token_schema';
export class SchemaValidator { export class SchemaValidator {
private validator: Validator; private validator: Validator;
@ -7,6 +8,7 @@ export class SchemaValidator {
this.validator = new Validator(); this.validator = new Validator();
this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id); this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id);
this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id); this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id);
this.validator.addSchema(tokenSchema, tokenSchema.id);
} }
public validate(instance: object, schema: Schema): ValidatorResult { public validate(instance: object, schema: Schema): ValidatorResult {
return this.validator.validate(instance, schema); return this.validator.validate(instance, schema);

View File

@ -0,0 +1,43 @@
import * as _ from 'lodash';
import 'mocha';
import * as chai from 'chai';
import chaiAsPromised = require('chai-as-promised');
import * as Web3 from 'web3';
import {web3Factory} from './utils/web3_factory';
import {ZeroEx} from '../src/0x.js';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
import {Token} from '../src/types';
import {SchemaValidator} from '../src/utils/schema_validator';
import {tokenSchema} from '../src/schemas/token_schema';
const expect = chai.expect;
chai.use(chaiAsPromised);
const blockchainLifecycle = new BlockchainLifecycle();
const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7;
describe('TokenRegistryWrapper', () => {
let zeroEx: ZeroEx;
before(async () => {
const web3 = web3Factory.create();
zeroEx = new ZeroEx(web3);
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
});
afterEach(async () => {
await blockchainLifecycle.revertAsync();
});
describe('#getTokensAsync', () => {
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);
const schemaValidator = new SchemaValidator();
_.each(tokens, token => {
const validationResult = schemaValidator.validate(token, tokenSchema);
expect(validationResult.errors).to.have.lengthOf(0);
});
});
});
});