-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.js
104 lines (91 loc) · 3.69 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module.exports = app => {
app.blockchainInfo = {
tip: null
}
const namespace = app.io.of('/')
app.messenger.on('egg-ready', () => {
app.messenger.sendToAgent('blockchain-info')
})
app.messenger.on('update-richlist', async () => {
let ctx = app.createAnonymousContext()
await ctx.service.balance.updateRichList()
})
app.messenger.on('update-qrc20-statistics', async () => {
let ctx = app.createAnonymousContext()
await ctx.service.qrc20.updateQRC20Statistics()
})
app.messenger.on('update-daily-transactions', async () => {
let ctx = app.createAnonymousContext()
let dailyTransactions = await ctx.service.statistics.getDailyTransactions()
await app.redis.hset(app.name, 'daily-transactions', JSON.stringify(dailyTransactions))
})
app.messenger.on('update-block-interval', async () => {
let ctx = app.createAnonymousContext()
let blockInterval = await ctx.service.statistics.getBlockIntervalStatistics()
await app.redis.hset(app.name, 'block-interval', JSON.stringify(blockInterval))
})
app.messenger.on('update-address-growth', async () => {
let ctx = app.createAnonymousContext()
let addressGrowth = await ctx.service.statistics.getAddressGrowth()
await app.redis.hset(app.name, 'address-growth', JSON.stringify(addressGrowth))
})
app.messenger.on('update-stakeweight', async () => {
let ctx = app.createAnonymousContext()
let stakeWeight = await ctx.service.info.getStakeWeight()
await app.redis.hset(app.name, 'stakeweight', JSON.stringify(stakeWeight))
namespace.to('blockchain').emit('stakeweight', stakeWeight)
})
app.messenger.on('update-feerate', async () => {
await app.runSchedule('update-feerate')
})
app.messenger.on('update-dgpinfo', async () => {
let ctx = app.createAnonymousContext()
let dgpInfo = await ctx.service.info.getDGPInfo()
await app.redis.hset(app.name, 'dgpinfo', JSON.stringify(dgpInfo))
namespace.to('blockchain').emit('dgpinfo', dgpInfo)
})
app.messenger.on('blockchain-info', info => {
app.blockchainInfo = info
})
app.messenger.on('block-tip', tip => {
app.blockchainInfo.tip = tip
})
app.messenger.on('new-block', block => {
app.blockchainInfo.tip = block
})
app.messenger.on('reorg-to-block', block => {
app.blockchainInfo.tip = block
})
app.messenger.on('socket/block-tip', async tip => {
app.blockchainInfo.tip = tip
namespace.emit('tip', tip)
let ctx = app.createAnonymousContext()
let transactions = (await ctx.service.block.getBlockTransactions(tip.height)).map(id => id.toString('hex'))
for (let id of transactions) {
namespace.to(`transaction/${id}`).emit('transaction/confirm', id)
}
let list = await ctx.service.block.getBlockAddressTransactions(tip.height)
for (let i = 0; i < transactions.length; ++i) {
for (let address of list[i] || []) {
namespace.to(`address/${address}`).emit('address/transaction', {address, id: transactions[i]})
}
}
})
app.messenger.on('socket/reorg/block-tip', tip => {
app.blockchainInfo.tip = tip
namespace.emit('reorg', tip)
})
app.messenger.on('socket/mempool-transaction', async id => {
id = Buffer.from(id)
let ctx = app.createAnonymousContext()
let transaction = await ctx.service.transaction.getTransaction(id)
if (!transaction) {
return
}
namespace.to('mempool').emit('mempool/transaction', await ctx.service.transaction.transformTransaction(transaction, {brief: true}))
let addresses = await ctx.service.transaction.getMempoolTransactionAddresses(id)
for (let address of addresses) {
namespace.to(`address/${address}`).emit('address/transaction', {address, id: id.toString('hex')})
}
})
}