#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const transaction_1 = require("../build/src/models/transaction");
const storage_1 = require("../build/src/services/storage");
function usage(errMsg) {
    console.log('USAGE: ./findReplacedTx.js <options>');
    console.log('OPTIONS:');
    console.log('  --chain <value>      REQUIRED - e.g. BTC, BCH, DOGE, LTC...');
    console.log('  --network <value>    REQUIRED - e.g. mainnet, testnet3, regtest...');
    console.log('  --txid <value>       REQUIRED Transaction Id that replaced');
    console.log('  --window [value]     Minutes to look back (default: 10)');
    if (errMsg) {
        console.log('\nERROR: ' + errMsg);
    }
    process.exit();
}
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
    usage();
}
const chainIdx = args.indexOf('--chain');
const networkIdx = args.indexOf('--network');
const txidIdx = args.indexOf('--txid');
const chain = args[chainIdx + 1]?.toUpperCase();
const network = args[networkIdx + 1]?.toLowerCase();
const txid = args[txidIdx + 1];
if (chainIdx === -1 || networkIdx === -1 || txidIdx === -1 || !chain || !network || !txid) {
    usage('Missing required options.');
}
const windowIdx = args.indexOf('--window');
const windowMins = (windowIdx > -1 && parseInt(args[windowIdx + 1])) || 10;
console.log('Connecting to database...');
storage_1.Storage.start()
    .then(async () => {
    const confirmedTx = await transaction_1.TransactionStorage.collection.findOne({ chain, network, txid });
    if (!confirmedTx) {
        console.log('Tx not found in db:', txid);
        return;
    }
    const $lt = new Date(confirmedTx.blockTimeNormalized);
    const $gt = new Date($lt.getTime() - (1000 * 60 * windowMins));
    const related = transaction_1.TransactionStorage.collection.find({ chain, network, blockTimeNormalized: { $lt, $gt }, blockHeight: -3 });
    for await (const tx of related) {
        if (tx.replacedByTxid === txid) {
            console.log(tx);
            break;
        }
    }
})
    .catch(console.error)
    .finally(() => {
    storage_1.Storage.stop();
});
//# sourceMappingURL=findReplacedTx.js.map