diff --git a/lib/node/index.js b/lib/node/index.js index 96851f3..e55a1ab 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -13,8 +13,7 @@ let refreshBlocks = false; // Super Hacky but better than inline Maths. setInterval(() => { refreshBlocks = true; -}, 5000); // Only refresh sockets after 5s passes - +}, 10000); // Only refresh sockets after 5s passes function start() { node.open() @@ -26,8 +25,7 @@ function start() { }); node.chain.on('connect', (entry, block) => { - logger.log('debug', - 'New Block & Ledger Entry'); + BlockParser.parse(entry, block); TxParser.parse(entry, block.txs); addrParser.parse(entry, block.txs); @@ -49,14 +47,9 @@ function start() { `${err}`); }); - // node.mempool.on('tx' ...) -} - -// Super Hack -function setSocket(client) { - console.log('setting socket for node'); - //socket = client; - + node.mempool.on('tx', (tx) => { + console.log(tx) + }); } module.exports = { diff --git a/lib/parser/transaction.js b/lib/parser/transaction.js index b21e450..a875bda 100644 --- a/lib/parser/transaction.js +++ b/lib/parser/transaction.js @@ -4,11 +4,32 @@ const OutputModel = require('../../models/output'); const config = require('../../config'); const util = require('../../lib/util'); const logger = require('../logger'); +const io = require('../api').io; + +const socketThrottle = 100; +let counter = 0; function parse(entry, txs) { txs.forEach((tx) => { const txJSON = tx.toJSON(); + counter++; + + if (counter % socketThrottle === 0) { + io.sockets.emit('tx', { + txid: txJSON.hash, + valueOut: tx.outputs.reduce((a, b) => { + a = a.toJSON(); + b = b.toJSON(); + + const valA = (a.value || a.valueOut.value || 0) / 1e8; + const valB = (b.value || b.valueOut.value || 0) / 1e8; + + return valA + valB; + }), + }, 0); + } + const t = new TxModel({ hash: txJSON.hash, witnessHash: txJSON.witnessHash,