Changes made as per comments - still have two minor things to fix

This commit is contained in:
askeluv
2019-02-13 20:23:07 +08:00
parent 82dffe9d0e
commit c36e1ad056
7 changed files with 50 additions and 46 deletions

View File

@@ -1,8 +1,10 @@
import * as R from 'ramda';
import { Connection, ConnectionOptions, createConnection } from 'typeorm';
import { logUtils } from '@0x/utils';
import { EdpsSource} from '../data_sources/slippage';
import { CryptoCompareOHLCVSource } from '../data_sources/ohlcv_external/crypto_compare';
import { EdpsSource } from '../data_sources/dex_prices';
import { Slippage } from '../entities';
import * as ormConfig from '../ormconfig';
import { calculateSlippage } from '../parsers/slippage';
@@ -11,35 +13,43 @@ import { handleError } from '../utils';
// Number of orders to save at once.
const BATCH_SAVE_SIZE = 1000;
// Maximum requests per second to CryptoCompare
const CRYPTO_COMPARE_MAX_REQS_PER_SECOND = 60;
// USD amounts for slippage depths
// tslint:disable-next-line:custom-no-magic-numbers
const USD_AMOUNTS = [10, 100, 1000, 10000];
// TODO: fetch from database
const TOKENS = ['BAT', 'DAI', 'FUN', 'MANA', 'OMG', 'REP', 'TUSD', 'ZRX', 'MKR', 'BNB', 'USDC'];
const TOKENS = ['BAT', 'DAI', 'FUN', 'MANA', 'OMG', 'REP', 'TUSD', 'ZRX', 'MKR', 'BNB', 'USDC', 'LOOM', 'DNT', 'CVC'];
let connection: Connection;
(async () => {
connection = await createConnection(ormConfig as ConnectionOptions);
const edpsSource = new EdpsSource();
const cryptoCompareSource = new CryptoCompareOHLCVSource(CRYPTO_COMPARE_MAX_REQS_PER_SECOND);
logUtils.log('Fetching slippage records');
const nestedSlippages: Slippage[][][] = await Promise.all(TOKENS.map(async symbol => {
const usdPrice = await edpsSource.getUsdPriceAsync(symbol);
return Promise.all(USD_AMOUNTS.map(async usdAmount => {
const amount = usdAmount / usdPrice;
const buyEdps = await edpsSource.getEdpsAsync('buy', symbol, amount);
const sellEdps = await edpsSource.getEdpsAsync('sell', symbol, amount);
return Array.from(buyEdps.keys()).map(exchange => {
const slippage: Slippage = calculateSlippage(usdAmount, exchange, buyEdps, sellEdps);
return slippage;
});
}));
}));
const nestedSlippages: Slippage[][][] = await Promise.all(
TOKENS.map(async symbol => {
const usdPrice = await cryptoCompareSource.getUsdPriceAsync(symbol);
return Promise.all(
USD_AMOUNTS.map(async usdAmount => {
const amount = usdAmount / usdPrice;
const buyEdps = await edpsSource.getEdpsAsync('buy', symbol, amount);
const sellEdps = await edpsSource.getEdpsAsync('sell', symbol, amount);
return Array.from(buyEdps.keys()).map(exchange => {
const slippage: Slippage = calculateSlippage(usdAmount, exchange, buyEdps, sellEdps);
return slippage;
});
}),
);
}),
);
const slippagesWithEmptyRecords = nestedSlippages
.reduce((acc, val) => acc.concat(val))
.reduce((acc, val) => acc.concat(val));
.reduce((acc, val) => acc.concat(val))
.reduce((acc, val) => acc.concat(val));
const slippages = slippagesWithEmptyRecords.filter(slippage => slippage.observedTimestamp);
const SlippageRepository = connection.getRepository(Slippage);
logUtils.log(`Saving ${slippages.length} records to database`);