-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWavesContractCache.js
More file actions
86 lines (72 loc) · 3.17 KB
/
WavesContractCache.js
File metadata and controls
86 lines (72 loc) · 3.17 KB
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 BaseStorage = require('./storage/BaseStorage');
const RedisStorage = require('./storage/RedisStorage');
const TransactionListener = require('./lesteners/TransactionListener');
const HeightListener = require('./lesteners/HeightListener');
const _get = require('lodash/get');
const winston = require('winston');
module.exports = class WavesContractCache {
constructor(params) {
params = params || {};
this.nodeUrl = params.nodeUrl;
this.dApp = params.dApp;
this.updateHandler = params.updateHandler || null;
// Create logger
this.logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(info => `${info.timestamp} ${info.level} ${info.message}`)
),
transports: [
new winston.transports.Console(),
],
level: 'info',
...params.logger,
});
// Create storage
this.storage = params.storage instanceof BaseStorage
? params.storage
: new RedisStorage(params.storage);
// Create transaction listener
this.transactionListener = params.transactionListener instanceof TransactionListener
? params.transactionListener
: new TransactionListener(params.transactionListener);
this.transactionListener.app = this;
this.transactionListener.storage = this.storage;
this.transactionListener.transactionsHandler = this._onTransactions.bind(this);
// Create height listener
this.heightListener = params.heightListener instanceof HeightListener
? params.heightListener
: new HeightListener(params.heightListener);
this.heightListener.app = this;
this.heightListener.storage = this.storage;
this.heightListener.heightsHandler = this._onHeight.bind(this);
}
async start() {
// this.logger.info('Start listen income transactions and height updates... Node ' + this.nodeUrl + ', DApp ' + this.dApp);
// await this.heightListener.start();
// await this.transactionListener.start();
}
_onTransactions(transactions) {
transactions.forEach(transaction => {
const method = transaction.call.function;
const args = transaction.call.args.map(item => item.value);
const sender = transaction.sender;
if (_get(transaction, 'info.stateChanges.data')) {
const keys = transaction.info ? transaction.info.stateChanges.data.map(item => item.key) : [];
if (keys.length > 0) {
this.logger.debug('Income transaction: ' + method + '(' + args.join(', ') + '), sender ' + sender + ', keys: ' + JSON.stringify(keys));
if (this.updateHandler) {
this.updateHandler(keys);
}
}
}
});
}
_onHeight() {
if (this.updateHandler) {
this.logger.debug('Height updated: ' + this.heightListener.getHeight());
this.updateHandler(['height']);
}
}
};