|
| 1 | +import { flattenTimestamp } from '../services/priceService' |
| 2 | +import prisma from 'prisma/clientInstance' |
| 3 | +import { connectTransactionsListToPrices } from 'services/transactionService' |
| 4 | +import { Transaction } from '@prisma/client' |
| 5 | +import moment from 'moment' |
| 6 | +import { exit } from 'process' |
| 7 | + |
| 8 | +async function misconnectTxs (txsIds: string[]): Promise<void> { |
| 9 | + if (process.env.ENVIRONMENT === 'production') { |
| 10 | + return |
| 11 | + } |
| 12 | + if (txsIds.length === 0) { |
| 13 | + return |
| 14 | + } |
| 15 | + console.log('Misconnecting', txsIds.length, 'for testing purposes') |
| 16 | + for (const txId of txsIds) { |
| 17 | + const tx = await prisma.transaction.findUniqueOrThrow({ |
| 18 | + where: { |
| 19 | + id: txId |
| 20 | + } |
| 21 | + }) |
| 22 | + const txDayTimestamp = flattenTimestamp(tx.timestamp) |
| 23 | + let signal = 1 |
| 24 | + if (txDayTimestamp === flattenTimestamp(moment.utc().unix())) { |
| 25 | + signal = -1 |
| 26 | + } |
| 27 | + await prisma.transaction.update({ |
| 28 | + where: { |
| 29 | + id: txId |
| 30 | + }, |
| 31 | + data: { |
| 32 | + timestamp: tx.timestamp + (signal * 86400) |
| 33 | + } |
| 34 | + }) |
| 35 | + } |
| 36 | + console.log('Finished misconnecting txs') |
| 37 | +} |
| 38 | + |
| 39 | +async function fixMisconnectedTxs (): Promise<void> { |
| 40 | + const total = await prisma.transaction.count() |
| 41 | + const pageSize = 1000 |
| 42 | + let page = 0 |
| 43 | + |
| 44 | + await misconnectTxs([ |
| 45 | + // ADD TXS IDS HERE TO TEST IT |
| 46 | + ]) |
| 47 | + |
| 48 | + console.log('Fixing misconnected txs...') |
| 49 | + while (true) { |
| 50 | + const txsToFix: Transaction[] = [] |
| 51 | + // Get txs page |
| 52 | + const txs = await prisma.transaction.findMany({ |
| 53 | + orderBy: { |
| 54 | + timestamp: 'asc' |
| 55 | + }, |
| 56 | + include: { |
| 57 | + prices: { |
| 58 | + select: { |
| 59 | + price: { |
| 60 | + select: { |
| 61 | + timestamp: true |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + skip: page * pageSize, |
| 68 | + take: pageSize |
| 69 | + }) |
| 70 | + |
| 71 | + const viewedCount = page * pageSize + txs.length |
| 72 | + |
| 73 | + // Finish if empty |
| 74 | + if (txs.length === 0) { |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + // Find txs with misaligned timestamps |
| 79 | + txs.forEach(tx => { |
| 80 | + const txFlattenedTimestamp = flattenTimestamp(tx.timestamp) |
| 81 | + const txPriceTimestamps = tx.prices.map(p => p.price.timestamp) |
| 82 | + if (txPriceTimestamps.filter(t => t !== txFlattenedTimestamp).length > 0) { |
| 83 | + txsToFix.push(tx) |
| 84 | + } |
| 85 | + }) |
| 86 | + if (txsToFix.length !== 0) { |
| 87 | + console.log(`[${viewedCount}/${total}] Fixing ${txsToFix.length} txs...`) |
| 88 | + console.log('Tx ids:\n', txsToFix.map(t => t.id).join('\n')) |
| 89 | + await connectTransactionsListToPrices(txsToFix) |
| 90 | + console.log(`[${viewedCount}/${total}] Finished fixing ${txsToFix.length} txs.`) |
| 91 | + } |
| 92 | + console.log(`[${viewedCount}/${total}]`) |
| 93 | + page++ |
| 94 | + } |
| 95 | + console.log('FINISHED') |
| 96 | + exit() |
| 97 | +} |
| 98 | + |
| 99 | +async function run (): Promise<void> { |
| 100 | + await fixMisconnectedTxs() |
| 101 | +} |
| 102 | + |
| 103 | +void run() |
0 commit comments