blob: 4e4c12dd069f6a4748998e843ce279c817d1ec0c (
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
|
// tslint:disable:no-console
import 'reflect-metadata';
import { Connection, ConnectionOptions, createConnection, Repository } from 'typeorm';
import { BloxySource } from '../data_sources/bloxy';
import { DexTrade } from '../entities';
import * as ormConfig from '../ormconfig';
import { parseBloxyTrades } from '../parsers/bloxy';
import { handleError } from '../utils';
// Number of trades to save at once.
const BATCH_SAVE_SIZE = 1000;
let connection: Connection;
(async () => {
connection = await createConnection(ormConfig as ConnectionOptions);
await getAndSaveTrades();
process.exit(0);
})().catch(handleError);
async function getAndSaveTrades(): Promise<void> {
const apiKey = process.env.BLOXY_API_KEY;
if (apiKey === undefined) {
throw new Error('Missing required env var: BLOXY_API_KEY');
}
const bloxySource = new BloxySource(apiKey);
const tradesRepository = connection.getRepository(DexTrade);
const lastSeenTimestamp = await getLastSeenTimestampAsync(tradesRepository);
console.log(`Last seen timestamp: ${lastSeenTimestamp === 0 ? 'none' : lastSeenTimestamp}`);
console.log('Getting latest dex trades...');
const rawTrades = await bloxySource.getDexTradesAsync(lastSeenTimestamp);
console.log(`Parsing ${rawTrades.length} trades...`);
const trades = parseBloxyTrades(rawTrades);
console.log(`Saving ${trades.length} trades...`);
await tradesRepository.save(trades, { chunk: Math.ceil(trades.length / BATCH_SAVE_SIZE) });
console.log('Done saving trades.');
}
async function getLastSeenTimestampAsync(tradesRepository: Repository<DexTrade>): Promise<number> {
if ((await tradesRepository.count()) === 0) {
return 0;
}
const response = (await connection.query(
'SELECT tx_timestamp FROM raw.dex_trades ORDER BY tx_timestamp DESC LIMIT 1',
)) as Array<{ tx_timestamp: number }>;
if (response.length === 0) {
return 0;
}
return response[0].tx_timestamp;
}
|