aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/scripts/pull_trusted_tokens.ts
blob: 67d9e08d137b216168bfdf33624a0f4c49d5c771 (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
import 'reflect-metadata';
import { Connection, ConnectionOptions, createConnection } from 'typeorm';

import { MetamaskTrustedTokenMeta, TrustedTokenSource, ZeroExTrustedTokenMeta } from '../data_sources/trusted_tokens';
import { TrustedToken } from '../entities';
import * as ormConfig from '../ormconfig';
import { parseMetamaskTrustedTokens, parseZeroExTrustedTokens } from '../parsers/trusted_tokens';
import { handleError } from '../utils';

const METAMASK_TRUSTED_TOKENS_URL =
    'https://raw.githubusercontent.com/MetaMask/eth-contract-metadata/master/contract-map.json';

const ZEROEX_TRUSTED_TOKENS_URL =
    'https://website-api.0xproject.com/tokens';

let connection: Connection;

(async () => {
    connection = await createConnection(ormConfig as ConnectionOptions);
    await getMetamaskTrustedTokens();
    await getZeroExTrustedTokens();
    process.exit(0);
})().catch(handleError);

async function getMetamaskTrustedTokens(): Promise<void> {
    // tslint:disable-next-line
    console.log('Getting latest metamask trusted tokens list ...');
    const trustedTokensRepository = connection.getRepository(TrustedToken);
    const trustedTokensSource = new TrustedTokenSource<Map<string, MetamaskTrustedTokenMeta>>(METAMASK_TRUSTED_TOKENS_URL);
    const resp = await trustedTokensSource.getTrustedTokenMetaAsync();
    const trustedTokens = parseMetamaskTrustedTokens(resp);
    // tslint:disable-next-line
    console.log('Saving metamask trusted tokens list');
    await trustedTokensRepository.save(trustedTokens);
    // tslint:disable-next-line
    console.log('Done saving metamask trusted tokens.')
}

async function getZeroExTrustedTokens(): Promise<void> {
    // tslint:disable-next-line
    console.log('Getting latest 0x trusted tokens list ...');
    const trustedTokensRepository = connection.getRepository(TrustedToken);
    const trustedTokensSource = new TrustedTokenSource<ZeroExTrustedTokenMeta[]>(ZEROEX_TRUSTED_TOKENS_URL);
    const resp = await trustedTokensSource.getTrustedTokenMetaAsync();
    const trustedTokens = parseZeroExTrustedTokens(resp);
    // tslint:disable-next-line
    console.log('Saving metamask trusted tokens list');
    await trustedTokensRepository.save(trustedTokens);
    // tslint:disable-next-line
    console.log('Done saving metamask trusted tokens.');
}