2018-08-29 19:46:17 +00:00
|
|
|
class TransactionTracker {
|
|
|
|
constructor(embark, _options) {
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.transactions = {};
|
2018-08-31 20:47:58 +00:00
|
|
|
this.embark = embark;
|
2018-08-31 19:12:27 +00:00
|
|
|
this.startTimestamp = Date.now() / 1000;
|
2018-08-29 19:46:17 +00:00
|
|
|
|
|
|
|
embark.events.on("block:pending:transaction", this.onPendingTransaction.bind(this));
|
|
|
|
embark.events.on("block:header", this.onBlockHeader.bind(this));
|
2018-08-31 20:47:58 +00:00
|
|
|
this.registerAPICalls();
|
2018-08-29 19:46:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:16:02 +00:00
|
|
|
onPendingTransaction(pendingTransactionHash) {
|
|
|
|
this.transactions[pendingTransactionHash] = {
|
2018-08-29 19:46:17 +00:00
|
|
|
startTimestamp: Date.now() / 1000
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlockHeader(blockHeader) {
|
|
|
|
this.events.request("blockchain:block:byNumber", blockHeader.hash, (err, block) => {
|
2018-08-30 19:48:50 +00:00
|
|
|
if (err) {
|
|
|
|
return this.logger.error('Error getting block header', err);
|
|
|
|
}
|
|
|
|
// Don't know why, but sometimes we receive nothing
|
|
|
|
if (!block || !block.transactions) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-29 19:46:17 +00:00
|
|
|
block.transactions.forEach(transaction => {
|
|
|
|
if (this.transactions[transaction.hash]) {
|
|
|
|
Object.assign(this.transactions[transaction.hash], transaction, {endTimestamp: block.timestamp, wait: block.timestamp - this.transactions[transaction.hash].startTimestamp});
|
|
|
|
}
|
|
|
|
});
|
2018-08-31 20:47:58 +00:00
|
|
|
this.events.emit('blockchain:gas:oracle:new');
|
2018-08-31 19:12:27 +00:00
|
|
|
this.cullOldTransactions();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cullOldTransactions() {
|
|
|
|
const timeLimit = (Date.now() / 1000) - 600; // Transactions old of 10 minutes are not to be counted anymore
|
|
|
|
if (this.startTimestamp > timeLimit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.keys(this.transactions).forEach(transactionHash => {
|
|
|
|
if (this.transactions[transactionHash].startTimestamp < timeLimit) {
|
|
|
|
delete this.transactions[transactionHash];
|
|
|
|
}
|
2018-08-29 19:46:17 +00:00
|
|
|
});
|
|
|
|
}
|
2018-08-30 19:48:50 +00:00
|
|
|
|
|
|
|
calculateGasPriceSpeeds() {
|
|
|
|
return Object.keys(this.transactions).reduce((acc, transactionHash) => {
|
|
|
|
const transaction = this.transactions[transactionHash];
|
|
|
|
if (!transaction.gasPrice) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
if (!acc[transaction.gasPrice]) {
|
|
|
|
acc[transaction.gasPrice] = {
|
|
|
|
nbTxs: 0,
|
|
|
|
totalWait: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
acc[transaction.gasPrice].nbTxs++;
|
|
|
|
acc[transaction.gasPrice].totalWait += transaction.wait;
|
|
|
|
acc[transaction.gasPrice].averageWait = acc[transaction.gasPrice].totalWait / acc[transaction.gasPrice].nbTxs;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
2018-08-31 20:47:58 +00:00
|
|
|
|
|
|
|
registerAPICalls() {
|
|
|
|
const self = this;
|
2018-08-31 20:48:59 +00:00
|
|
|
self.embark.registerAPICall(
|
|
|
|
'get',
|
|
|
|
'/embark-api/blockchain/gas/oracle',
|
|
|
|
(req, res) => {
|
|
|
|
res.send(self.calculateGasPriceSpeeds());
|
|
|
|
}
|
|
|
|
);
|
2018-08-31 20:47:58 +00:00
|
|
|
self.embark.registerAPICall(
|
|
|
|
'ws',
|
|
|
|
'/embark-api/blockchain/gas/oracle',
|
|
|
|
(ws) => {
|
|
|
|
self.events.on('blockchain:gas:oracle:new', () => {
|
|
|
|
ws.send(JSON.stringify(self.calculateGasPriceSpeeds()), () => {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-08-29 19:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TransactionTracker;
|