2020-12-21 17:08:34 +01:00
|
|
|
import bitcoinApi from './bitcoin/bitcoin-api-factory';
|
|
|
|
import logger from '../logger';
|
2020-12-27 22:47:22 +01:00
|
|
|
import { TransactionExtended, TransactionMinerInfo } from '../mempool.interfaces';
|
|
|
|
import { IEsploraApi } from './bitcoin/esplora-api.interface';
|
2020-12-21 17:08:34 +01:00
|
|
|
|
|
|
|
class TransactionUtils {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
public stripCoinbaseTransaction(tx: TransactionExtended): TransactionMinerInfo {
|
|
|
|
return {
|
|
|
|
vin: [{
|
|
|
|
scriptsig: tx.vin[0].scriptsig || tx.vin[0]['coinbase']
|
|
|
|
}],
|
|
|
|
vout: tx.vout
|
|
|
|
.map((vout) => ({
|
|
|
|
scriptpubkey_address: vout.scriptpubkey_address,
|
|
|
|
value: vout.value
|
|
|
|
}))
|
|
|
|
.filter((vout) => vout.value)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-23 22:15:06 +01:00
|
|
|
public async $getTransactionExtended(txId: string, addPrevouts = false): Promise<TransactionExtended> {
|
|
|
|
const transaction: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(txId, false, addPrevouts);
|
2021-01-23 20:51:22 +01:00
|
|
|
return this.extendTransaction(transaction);
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 14:17:32 +01:00
|
|
|
private extendTransaction(transaction: IEsploraApi.Transaction): TransactionExtended {
|
2021-03-19 13:10:11 +01:00
|
|
|
// @ts-ignore
|
|
|
|
if (transaction.vsize) {
|
|
|
|
// @ts-ignore
|
|
|
|
return transaction;
|
|
|
|
}
|
2021-03-18 17:47:40 +01:00
|
|
|
const feePerVbytes = Math.max(1, (transaction.fee || 0) / (transaction.weight / 4));
|
2020-12-28 14:17:32 +01:00
|
|
|
const transactionExtended: TransactionExtended = Object.assign({
|
|
|
|
vsize: Math.round(transaction.weight / 4),
|
2021-03-18 17:47:40 +01:00
|
|
|
feePerVsize: feePerVbytes,
|
|
|
|
effectiveFeePerVsize: feePerVbytes,
|
2020-12-28 14:17:32 +01:00
|
|
|
}, transaction);
|
|
|
|
if (!transaction.status.confirmed) {
|
|
|
|
transactionExtended.firstSeen = Math.round((new Date().getTime() / 1000));
|
|
|
|
}
|
|
|
|
return transactionExtended;
|
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new TransactionUtils();
|