diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-11-30 03:40:09 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-12-05 06:25:42 +0800 |
commit | 7198b441e0d85785eec7244dd60bcd92269d954e (patch) | |
tree | b1169b274f113fb92e1d748a97eea631c2f77420 /packages/pipeline/src/scripts | |
parent | 3d211c415b58a67f84332ff512bf9372cac5a3ac (diff) | |
download | dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar.gz dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar.bz2 dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar.lz dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar.xz dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.tar.zst dexon-sol-tools-7198b441e0d85785eec7244dd60bcd92269d954e.zip |
Add script for parsing competing dex trades from Bloxy (#1355)
Diffstat (limited to 'packages/pipeline/src/scripts')
-rw-r--r-- | packages/pipeline/src/scripts/pull_competing_dex_trades.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/pipeline/src/scripts/pull_competing_dex_trades.ts b/packages/pipeline/src/scripts/pull_competing_dex_trades.ts new file mode 100644 index 000000000..4e4c12dd0 --- /dev/null +++ b/packages/pipeline/src/scripts/pull_competing_dex_trades.ts @@ -0,0 +1,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; +} |