aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/scripts/pull_radar_relay_orders.ts
blob: 03fc764f224990b07b06519b072b33ff8e5ae566 (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
// tslint:disable:no-console
import { HttpClient } from '@0x/connect';
import * as R from 'ramda';
import 'reflect-metadata';
import { Connection, ConnectionOptions, createConnection, EntityManager } from 'typeorm';

import { createObservedTimestampForOrder, SraOrder } from '../entities';
import * as ormConfig from '../ormconfig';
import { parseSraOrders } from '../parsers/sra_orders';
import { handleError } from '../utils';

const RADAR_RELAY_URL = 'https://api.radarrelay.com/0x/v2';
const ORDERS_PER_PAGE = 10000; // Number of orders to get per request.

let connection: Connection;

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

async function getOrderbookAsync(): Promise<void> {
    console.log('Getting all orders...');
    const connectClient = new HttpClient(RADAR_RELAY_URL);
    const rawOrders = await connectClient.getOrdersAsync({
        perPage: ORDERS_PER_PAGE,
    });
    console.log(`Got ${rawOrders.records.length} orders.`);
    console.log('Parsing orders...');
    // Parse the sra orders, then add source url to each.
    const orders = R.pipe(
        parseSraOrders,
        R.map(setSourceUrl(RADAR_RELAY_URL)),
    )(rawOrders);
    // Save all the orders and update the observed time stamps in a single
    // transaction.
    console.log('Saving orders and updating timestamps...');
    const observedTimestamp = Date.now();
    await connection.transaction(
        async (manager: EntityManager): Promise<void> => {
            for (const order of orders) {
                await manager.save(SraOrder, order);
                const orderObservation = createObservedTimestampForOrder(order, observedTimestamp);
                await manager.save(orderObservation);
            }
        },
    );
}

const sourceUrlProp = R.lensProp('sourceUrl');

/**
 * Sets the source url for a single order. Returns a new order instead of
 * mutating the given one.
 */
const setSourceUrl = R.curry(
    (sourceURL: string, order: SraOrder): SraOrder => {
        return R.set(sourceUrlProp, sourceURL, order);
    },
);