-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·136 lines (113 loc) · 4.16 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require('dotenv').config()
const { Kafka } = require('kafkajs');
const clientId = process.env.CLIENT_ID;
const brokers = new Array(process.env.KAFKA_BROKER_ADDRESS);
const topicName = process.env.KAFKA_TOPIC_NAME;
const kafka = new Kafka({ clientId, brokers });
const producer = kafka.producer();
const args = process.argv.slice(2);
const { logger } = require('./src/utils/logging.js');
const { initCLI } = require('./src/utils/args.js');
const { readCachedBlockNumber, cacheBlockNumber } = require('./src/utils/file.js');
const option = initCLI(args);
const log = new logger(option);
const Web3 = require('web3');
const net = require('net');
const smartContractAbi = require('./src/contracts/UniversityDiploma.json');
const smartContractAddress = process.env.SMART_CONTRACT_ADDRESS;
const nodeIpcPath = process.env.NODE_IPC_PATH;
let web3;
if (nodeIpcPath.includes('.ipc')) {
web3 = new Web3(new Web3.providers.IpcProvider(nodeIpcPath, net));
} else {
web3 = new Web3(nodeIpcPath);
}
async function init() {
await initKafka();
let contract;
let computedBlock;
let latestBlock = 0;
const blockSize = parseInt(process.env.BLOCK_SIZE) || 1000;
try {
contract = new web3.eth.Contract(smartContractAbi, smartContractAddress);
latestBlock = await web3.eth.getBlockNumber();
log.logging().debug(`[CRX] latest: ${latestBlock}`);
computedBlock = await readCachedBlockNumber();
if (computedBlock) {
log.logging().info(`[SC] Listen events from cache(latest cached block number + 1): ${computedBlock + 1}`);
} else {
computedBlock = 1;
log.logging().info(`[SC] Listen events from historical_block + 1: ${computedBlock}`);
}
} catch (error) {
console.error(error);
process.exit(1);
}
while (latestBlock - computedBlock > blockSize) {
log.logging().info(`[SC] Parse events from : ${computedBlock + 1} to: ${computedBlock + blockSize}`);
const data = await contract.getPastEvents('Issued', {fromBlock: computedBlock + 1, toBlock: computedBlock + blockSize});
for (const item of data) {
await getTransferDetails(item);
}
computedBlock = computedBlock + blockSize;
}
// Эвентийг сонсох хэсэг
contract.events.Issued({
fromBlock: computedBlock + 1,
}, async (error, event) => {
if (error) console.error(error);
else await getTransferDetails(event);
}).on('error', (error, receipt) => {
log.logging().error(error);
log.logging().debug(receipt);
});
}
async function getTransferDetails(dataEvent) {
log.logging().info(`[SC] new event at block number: ${dataEvent.blockNumber}`);
log.logging().debug(`[SC] event details: ${JSON.stringify(dataEvent['returnValues'])}`);
let message = {
blockNumber: dataEvent.blockNumber,
issuer: dataEvent[ 'returnValues' ][ 'issuer' ],
hash: dataEvent[ 'returnValues' ][ 'hash' ],
metaHash: dataEvent[ 'returnValues' ][ 'metaHash' ],
certNum: dataEvent[ 'returnValues' ][ 'certNum' ],
timestamp: dataEvent[ 'returnValues' ][ 'timestamp' ]
};
await messageProducer(message);
}
async function messageProducer(message) {
try {
await producer.connect();
const responses = await producer.send({
topic: topicName,
messages: [
{ key: message.certNum, value: JSON.stringify(message) }
],
});
log.logging().debug(`[KAFKA] Published message: ${JSON.stringify(responses)}`);
await cacheBlockNumber(message.blockNumber);
} catch(error) {
log.logging().error(error);
}
await producer.disconnect();
}
async function initKafka() {
const admin = kafka.admin();
try {
await admin.connect();
const list = await admin.listTopics();
if (!list.find(item => item === topicName)) {
await admin.createTopics({
waitForLeaders: true,
topics: [
{
topic: topicName
}
],
});
}
} catch(error) {
log.logging().error(error);
}
}
init();