-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.ts
76 lines (70 loc) · 2.36 KB
/
agent.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Application } from 'egg'
import SocketClient from 'socket.io-client'
import { ITip } from 'vipsinfo/node/services/db'
export default function (agent: Application) {
let tip: ITip | null = null
agent.messenger.on('egg-ready', () => {
const io = SocketClient(`http://localhost:${agent.config.vipsinfo.port}`)
io.on('tip', (newTip: ITip) => {
tip = newTip
agent.messenger.sendToApp('block-tip', tip)
agent.messenger.sendRandom('socket/block-tip', tip)
})
io.on('block', (block: ITip) => {
tip = block
agent.messenger.sendToApp('new-block', block)
// @ts-ignore
agent.messenger.sendRandom('update-stakeweight')
// @ts-ignore
agent.messenger.sendRandom('update-dgpinfo')
agent.messenger.sendRandom('socket/block-tip', block)
})
io.on('reorg', (block: ITip) => {
tip = block
agent.messenger.sendToApp('reorg-to-block', block)
agent.messenger.sendRandom('socket/reorg/block-tip', block)
})
io.on('mempool-transaction', (id: Buffer | undefined) => {
if (id) {
agent.messenger.sendRandom('socket/mempool-transaction', id)
}
})
})
let lastTipHash = Buffer.alloc(0)
function updateStatistics() {
if (tip && Buffer.compare(lastTipHash, tip.hash) !== 0) {
// @ts-ignore
agent.messenger.sendRandom('update-richlist')
// @ts-ignore
agent.messenger.sendRandom('update-qrc20-statistics')
// @ts-ignore
agent.messenger.sendRandom('update-daily-transactions')
// @ts-ignore
agent.messenger.sendRandom('update-block-interval')
// @ts-ignore
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', () => {
const interval = setInterval(() => {
if (tip) {
agent.messenger.sendToApp('blockchain-info', { tip })
clearInterval(interval)
updateStatistics()
}
}, 0)
// @ts-ignore
agent.messenger.sendRandom('update-stakeweight')
// @ts-ignore
agent.messenger.sendRandom('update-feerate')
// @ts-ignore
agent.messenger.sendRandom('update-price')
// @ts-ignore
agent.messenger.sendRandom('update-dgpinfo')
})
}