aboutsummaryrefslogtreecommitdiffstats
path: root/test/token_registry_wrapper_test.ts
blob: 6b5dd517ef6968f573aea76f44422533d2db787f (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
import * as _ from 'lodash';
import 'mocha';
import * as chai from 'chai';
import {SchemaValidator, schemas} from '0x-json-schemas';
import {chaiSetup} from './utils/chai_setup';
import {web3Factory} from './utils/web3_factory';
import {ZeroEx, Token} from '../src';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';

chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle();

const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7;

describe('TokenRegistryWrapper', () => {
    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 () => {
        const web3 = web3Factory.create();
        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 () => {
        await blockchainLifecycle.startAsync();
    });
    afterEach(async () => {
        await blockchainLifecycle.revertAsync();
    });
    describe('#getTokensAsync', () => {
        it('should return all the tokens added to the tokenRegistry during the migration', async () => {
            expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);

            const schemaValidator = new SchemaValidator();
            _.each(tokens, token => {
                const validationResult = schemaValidator.validate(token, schemas.tokenSchema);
                expect(validationResult.errors).to.have.lengthOf(0);
            });
        });
    });
    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, schemas.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', () => {
        it('should return the token added to the tokenRegistry during the migration', async () => {
            const aToken = tokens[0];

            const token = await zeroEx.tokenRegistry.getTokenIfExistsAsync(aToken.address);
            const schemaValidator = new SchemaValidator();
            const validationResult = schemaValidator.validate(token, schemas.tokenSchema);
            expect(validationResult.errors).to.have.lengthOf(0);
        });
        it('should return return undefined when passed a token address not in the tokenRegistry', async () => {
            const unregisteredTokenAddress = '0x5409ed021d9299bf6814279a6a1411a7e866a631';
            const tokenIfExists = await zeroEx.tokenRegistry.getTokenIfExistsAsync(unregisteredTokenAddress);
            expect(tokenIfExists).to.be.undefined();
        });
    });
});