aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/index.ts
blob: 165ff97800f6eb7bf3b72d5ab0521ff0fe352070 (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
import { web3Factory } from '@0x/dev-utils';
import { Web3ProviderEngine } from '@0x/subproviders';
import 'reflect-metadata';
import { Connection, createConnection } from 'typeorm';

import { ExchangeEventsSource } from './data_sources/contract-wrappers/exchange_events';
import { RelayerRegistrySource } from './data_sources/relayer-registry';
import { Web3Source } from './data_sources/web3';
import { Block } from './entities/Block';
import { ExchangeFillEvent } from './entities/ExchangeFillEvent';
import { Relayer } from './entities/Relayer';
import { Transaction } from './entities/Transaction';
import { deployConfig } from './ormconfig';
import { parseExchangeEvents } from './parsers/events';
import { parseRelayers } from './parsers/relayer_registry';
import { parseBlock, parseTransaction } from './parsers/web3';

const EXCHANGE_START_BLOCK = 6271590; // Block number when the Exchange contract was deployed to mainnet.
// NOTE(albrow): We need to manually update this URL for now. Fix this when we
// have the relayer-registry behind semantic versioning.
const RELAYER_REGISTRY_URL =
    'https://raw.githubusercontent.com/0xProject/0x-relayer-registry/4701c85677d161ea729a466aebbc1826c6aa2c0b/relayers.json';

let connection: Connection;

(async () => {
    connection = await createConnection(deployConfig);
    const provider = web3Factory.getRpcProvider({
        rpcUrl: 'https://mainnet.infura.io',
    });
    await getExchangeEventsAsync(provider);
    await getBlockAsync(provider);
    await getTransactionAsync(provider);
    await getRelayers(RELAYER_REGISTRY_URL);
    process.exit(0);
})();

async function getExchangeEventsAsync(provider: Web3ProviderEngine): Promise<void> {
    console.log('Getting event logs...');
    const eventsRepository = connection.getRepository(ExchangeFillEvent);
    const exchangeEvents = new ExchangeEventsSource(provider, 1);
    const eventLogs = await exchangeEvents.getFillEventsAsync(EXCHANGE_START_BLOCK, EXCHANGE_START_BLOCK + 100000);
    console.log('Parsing events...');
    const events = parseExchangeEvents(eventLogs);
    console.log(`Retrieved and parsed ${events.length} total events.`);
    console.log('Saving events...');
    for (const event of events) {
        await eventsRepository.save(event);
    }
    console.log('Saved events.');
}

async function getBlockAsync(provider: Web3ProviderEngine): Promise<void> {
    console.log('Getting block info...');
    const blocksRepository = connection.getRepository(Block);
    const web3Source = new Web3Source(provider);
    const rawBlock = await web3Source.getBlockInfoAsync(EXCHANGE_START_BLOCK);
    const block = parseBlock(rawBlock);
    console.log('Saving block info...');
    await blocksRepository.save(block);
    console.log('Done saving block.');
}

async function getTransactionAsync(provider: Web3ProviderEngine): Promise<void> {
    console.log('Getting tx info...');
    const txsRepository = connection.getRepository(Transaction);
    const web3Source = new Web3Source(provider);
    const rawTx = await web3Source.getTransactionInfoAsync(
        '0x6dd106d002873746072fc5e496dd0fb2541b68c77bcf9184ae19a42fd33657fe',
    );
    const tx = parseTransaction(rawTx);
    console.log('Saving tx info...');
    await txsRepository.save(tx);
    console.log('Done saving tx.');
}

async function getRelayers(url: string): Promise<void> {
    console.log('Getting relayer info...');
    const relayerRepository = connection.getRepository(Relayer);
    const relayerSource = new RelayerRegistrySource(RELAYER_REGISTRY_URL);
    const relayersResp = await relayerSource.getRelayerInfoAsync();
    const relayers = parseRelayers(relayersResp);
    console.log('Saving relayer info...');
    // for (const relayer of relayers) {
    await relayerRepository.save(relayers);
    // }
    console.log('Done saving relayers.');
}