-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-listener.js
86 lines (74 loc) · 2.85 KB
/
event-listener.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
const path = require('path')
const fs = require('fs-extra')
const abiDecoder = require('abi-decoder')
const Web3 = require("web3")
const EEAClient = require("web3-eea")
const config = require('./config')
const eventHandler = require('./eventHandler')
const { logger } = require('./utils/logger')
const chainId = 1337
// Add ABI contract
const contractPath = path.resolve(__dirname, 'contract', config.besu.contractABIFile)
const contractJSON = JSON.parse(fs.readFileSync(contractPath))
logger.info(`Contract ABI file used: ${config.besu.contractABIFile}`)
abiDecoder.addABI(contractJSON.abi)
// Connect with node
logger.info(`Connecting to node ${config.besu.node.wsUrl}`)
logger.info(`ChainId = ${chainId}`)
const node = new EEAClient(new Web3(config.besu.node.wsUrl), chainId)
function initializeEventMonitor() {
const filter = {
// address: contract,
fromBlock: '0x01'
}
logger.info(`Installing filter ${JSON.stringify(filter)} with privacyGroupId ${config.besu.privacyGroupId}`)
// Create subscription
return node.priv.subscribe(config.besu.privacyGroupId, filter, (error, result) => {
if (! error) {
logger.info("Installed filter", result)
logger.info(`Watching privacyGroupId ${config.besu.privacyGroupId}`)
} else {
logger.error("Problem installing filter:", error)
throw error
}
})
.then(async subscription => {
// Add handlers for incoming events
subscription
.on("data", async log => {
if (log.result != null) {
// Logs from subscription are nested in `result` key
logger.debug("LOG =>", log.result)
// Decode event
logger.info("Decoding received log...")
const decodedLogs = abiDecoder.decodeLogs([ log.result ])
logger.info(JSON.stringify(decodedLogs))
// Execute actions
try {
await eventHandler.manage(decodedLogs[0])
} catch(error) {
logger.error(error)
}
} else {
logger.debug("LOG =>", log)
}
})
.on("error", logger.error)
// Unsubscribe and disconnect on interrupt
process
.on("SIGINT", async () => {
logger.info("unsubscribing")
await subscription.unsubscribe((error, success) => {
if (!error) {
logger.info("Unsubscribed:", success)
} else {
logger.error("Failed to unsubscribe:", error)
}
node.currentProvider.disconnect()
})
})
})
}
module.exports = {
initializeEventMonitor
}