-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevents-manager.js
78 lines (74 loc) · 3.06 KB
/
events-manager.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
const events = require('events');
const {EventEmitter} = events;
const AWS = require('aws-sdk');
const blockchain = require('./blockchain.js');
const flowConstants = require('./flow-constants.js');
class EventsManager extends EventEmitter {
constructor() {
super();
let lastCheckedBlock = 0;
const _recurse = async () => {
if (blockchain.getIsLoaded()) {
// console.log('check 1');
const latestBlock = await blockchain.getLatestBlock();
// console.log('check 2');
if (!lastCheckedBlock) {
lastCheckedBlock = latestBlock;
}
if (latestBlock !== lastCheckedBlock) {
// console.log('check 3');
const {WebaverseToken, WebaverseNFT, WebaverseAccount} = await flowConstants.load();
const txs = {};
const eventTypes = [
`A.${WebaverseToken.slice(2)}.WebaverseToken.TokensWithdrawn`,
`A.${WebaverseToken.slice(2)}.WebaverseToken.TokensDeposited`,
`A.${WebaverseNFT.slice(2)}.WebaverseNFT.Withdraw`,
`A.${WebaverseNFT.slice(2)}.WebaverseNFT.Deposit`,
`A.${WebaverseNFT.slice(2)}.WebaverseNFT.MetadataChanged`,
`A.${WebaverseAccount.slice(2)}.WebaverseAccount.MetadataChanged`,
];
for (const eventType of eventTypes) {
const events = await blockchain.getEvents(eventType, lastCheckedBlock, latestBlock);
// console.log('check 4', eventType);
// console.log('got events', events);
for (const event of events) {
const {payload: {value: {fields}}, transactionId} = event;
/* const amountField = fields.find(field => field.name === 'amount');
const fromField = fields.find(field => field.name === 'from');
const toField = fields.find(field => field.name === 'to'); */
let tx = txs[transactionId];
if (!tx) {
tx = {};
txs[transactionId] = tx;
}
for (const field of fields) {
const value = field.value.type === 'Optional' ? field.value.value : field.value;
tx[field.name] = value.value;
if (value.type === 'UFix64') {
tx[field.name] = parseFloat(tx[field.name]);
} else if (value.type === 'UInt64') {
tx[field.name] = parseInt(tx[field.name], 10);
}
}
// console.log('got tx', tx, JSON.stringify(event, null, 2));
}
}
// console.log('got txs', txs);
for (const k in txs) {
const tx = txs[k];
console.log('got tx', tx);
// if (tx.from && tx.to) {
this.emit('transaction', tx);
// uiManager.popupMesh.addMessage(`${tx.from} sent ${tx.to} ${tx.amount}`);
// }
}
lastCheckedBlock = latestBlock;
}
}
setTimeout(_recurse, 1000);
};
setTimeout(_recurse, 1000);
}
}
const eventsManager = new EventsManager();
module.exports = eventsManager;