aozhiwei 57fea86b5d 1
2022-04-22 09:40:37 +08:00

74 lines
1.6 KiB
JavaScript

const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const log = require('j7/log');
const bc = require('../blockchain');
class EventProcess extends BaseService {
async init(instance, cb) {
this.instance = instance;
this.cb = cb;
}
async start() {
while (true) {
await this.pullEvent();
await utils.sleep(8000 + utils.randRange(500, 1500));
}
}
async pullEvent() {
const logClass = this.getInstanceName() + ' pullEvent:';
while (true) {
try {
const fromBlock = await getFromBlock();
const toBlock = fromBlock + 3000;
const events = await bc[this.getInstanceName()].getPastEvents(
this.instance['eventName'],
{
fromBlock: fromBlock,
toBlock: toBlock,
},
);
await this.processEvents(events, toBlock);
await this.saveLastBlockNumber(toBlock);
return;
} catch (err) {
log.error(logClass + err);
}
}
await utils.sleep(2000 + utils.randRange(500, 1500));
}
getInstanceName() {
return this.instance['name'];
}
async processEvents(events, toBlock) {
this.instance['last_block_number'] = toBlock;
for (let i in events) {
while (true) {
try {
await cb(event);
break;
} catch (err) {
}
await utils.sleep(8000 + utils.randRange(500, 1500));
}
}
}
async getFromBlock() {
}
async saveLastBlockNumber(blockNumber) {
}
}
module.exports = EventProcess;