-
Notifications
You must be signed in to change notification settings - Fork 23
/
agent.js
62 lines (56 loc) · 1.95 KB
/
agent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const SocketClient = require('socket.io-client')
module.exports = function(agent) {
let tip = null
agent.messenger.on('egg-ready', () => {
let io = SocketClient(`http://localhost:${agent.config.qtuminfo.port}`)
io.on('tip', newTip => {
tip = newTip
agent.messenger.sendToApp('block-tip', tip)
agent.messenger.sendRandom('socket/block-tip', tip)
})
io.on('block', block => {
tip = block
agent.messenger.sendToApp('new-block', block)
agent.messenger.sendRandom('update-stakeweight')
agent.messenger.sendRandom('update-dgpinfo')
agent.messenger.sendRandom('socket/block-tip', block)
})
io.on('reorg', block => {
tip = block
agent.messenger.sendToApp('reorg-to-block', block)
agent.messenger.sendRandom('socket/reorg/block-tip', block)
})
io.on('mempool-transaction', id => {
if (id) {
agent.messenger.sendRandom('socket/mempool-transaction', id)
}
})
})
let lastTipHash = Buffer.alloc(0)
function updateStatistics() {
if (tip && Buffer.compare(lastTipHash, tip.hash) !== 0) {
agent.messenger.sendRandom('update-richlist')
agent.messenger.sendRandom('update-qrc20-statistics')
agent.messenger.sendRandom('update-daily-transactions')
agent.messenger.sendRandom('update-block-interval')
agent.messenger.sendRandom('update-address-growth')
lastTipHash = tip.hash
}
}
setInterval(updateStatistics, 2 * 60 * 1000).unref()
agent.messenger.on('blockchain-info', () => {
agent.messenger.sendToApp('blockchain-info', {tip})
})
agent.messenger.on('egg-ready', () => {
let interval = setInterval(() => {
if (tip) {
agent.messenger.sendToApp('blockchain-info', {tip})
clearInterval(interval)
updateStatistics()
}
}, 0)
agent.messenger.sendRandom('update-stakeweight')
agent.messenger.sendRandom('update-feerate')
agent.messenger.sendRandom('update-dgpinfo')
})
}