Adapt to Bloxy API change (#1659)
* Adapt to Bloxy API change We were using the `days` parameter, but that's no longer available in the API, so our fetching of previous days (before "today") was broken. This change gets rid of the concept of "days", and uses the API's new `from_date` and `till_date` parameters to fetch previous days' trades. The change to the query in `getLastSeenTimestampAsync()` was necessary because it was returning a string (despite the `as Array<{ tx_timestamp: number }>` cast), which was later causing problems trying to pass that value into `new Date()`. It worked before because we were doing some math operations on it (multiplying it by some numbers), so coercion was saving us. With the change from a raw query to a `typeorm` call, the `numberToBigIntTransformer` specified in the `DexTrade` entity is now doing the proper type conversion for us. The new `MAX_DAYS` constant in `pull_competing_dex_trades.ts` is necessary in order to avoid trying to pull data all the way back to 1969 :D, which induces the API to return an HTTP 503. * Increase trades per query to match current API doc This is needed in order to be able to pull more than a handful of days of data at a time (at least, with the script the way its currently written).
This commit is contained in:
@@ -12,6 +12,11 @@ import { handleError } from '../utils';
|
||||
// Number of trades to save at once.
|
||||
const BATCH_SAVE_SIZE = 1000;
|
||||
|
||||
// maximum number of days into the past for which trades will be pulled. this
|
||||
// is arbitrary and can be tweaked, but deep history takes a significant amount
|
||||
// of time to pull.
|
||||
const MAX_DAYS = 30;
|
||||
|
||||
let connection: Connection;
|
||||
|
||||
(async () => {
|
||||
@@ -39,14 +44,29 @@ async function getAndSaveTradesAsync(): Promise<void> {
|
||||
}
|
||||
|
||||
async function getLastSeenTimestampAsync(tradesRepository: Repository<DexTrade>): Promise<number> {
|
||||
const hoursPerDay = 24;
|
||||
const minutesPerHour = 60;
|
||||
const secondsPerMinute = 60;
|
||||
const millisecondsPerSecond = 1000;
|
||||
const millisecondsPerDay = millisecondsPerSecond * secondsPerMinute * minutesPerHour * hoursPerDay;
|
||||
|
||||
const oldestTimestampWilling = Date.now() - MAX_DAYS * millisecondsPerDay;
|
||||
|
||||
if ((await tradesRepository.count()) === 0) {
|
||||
return 0;
|
||||
return oldestTimestampWilling;
|
||||
}
|
||||
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;
|
||||
|
||||
const lastTrade: DexTrade | undefined = await tradesRepository.manager
|
||||
.createQueryBuilder()
|
||||
.select('trade')
|
||||
.from(DexTrade, 'trade')
|
||||
.orderBy('tx_timestamp', 'DESC')
|
||||
.limit(1)
|
||||
.getOne();
|
||||
|
||||
if (lastTrade === undefined) {
|
||||
return oldestTimestampWilling;
|
||||
}
|
||||
return response[0].tx_timestamp;
|
||||
|
||||
return lastTrade.txTimestamp;
|
||||
}
|
||||
|
Reference in New Issue
Block a user