-
Notifications
You must be signed in to change notification settings - Fork 3
/
DepositWatcher.js
111 lines (91 loc) · 3.56 KB
/
DepositWatcher.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
const EventEmitter = require('events');
class DepositWatcher extends EventEmitter {
/**
* @param {Indexer} indexer Instance of the Indexer client
* @param {Array<string>} addresses Addresses to watch
* @param {Array<string>} ingoreSenders Sender addresses to ignore
* @param {Number} [interval=1000] Query interval (default: 1 second)
*/
constructor(indexer, addresses, ignoreSenders = [], interval = 1000) {
super();
this.indexer = indexer;
this.addresses = new Set(addresses);
this.ignoreSenders = new Set(ignoreSenders);
this.interval = interval;
this.seenRounds = new Map();
this.lastRound = null;
this.startTime = Date.now();
this.processTransactions();
}
async processTransactions() {
try {
// We want to get all transactions since last round we've seen
// or since current time if we don't know current round number yet.
let query = this.indexer.searchForTransactions();
if (this.lastRound) {
query = query.minRound(this.lastRound);
} else {
query = query.afterTime(new Date().toISOString());
}
const queryResult = await query.do();
// Types of transactions we want to process:
// `pay` – Algo transfer
// `axfer` – ASA transfer
const txTypes = new Set(['pay', 'axfer']);
for (const tx of queryResult.transactions) {
const {
id,
sender,
'tx-type': txType,
'confirmed-round': confirmedRound,
'round-time': roundTime
} = tx;
// Skip transactions happened before start of the watcher
if (roundTime * 1000 < this.startTime) continue;
// Skip transactions types we are not interested in
if (!txTypes.has(txType)) continue;
const { amount, receiver } =
tx['payment-transaction'] || tx['asset-transfer-transaction'];
// Process only deposits to our addresses
if (!this.addresses.has(receiver)) continue;
// Skip transactions from addresses we ignore, e.g. our own
if (this.ignoreSenders.has(sender)) continue;
// Skip ASA opt-in transactions
if (txType === 'axfer' && sender === receiver && amount === 0) continue;
// Ensure skipping already processed transactions
const hasBeenSeen =
this.seenRounds.has(confirmedRound) &&
this.seenRounds.get(confirmedRound).has(id);
if (hasBeenSeen) continue;
// Remember transaction round and id
if (!this.seenRounds.has(confirmedRound)) {
this.seenRounds.set(confirmedRound, new Set());
}
this.seenRounds.get(confirmedRound).add(id);
// Finally, emit deposit event
if (txType === 'axfer') {
// ASA transfer
const assetID = tx['asset-transfer-transaction']['asset-id'];
this.emit('deposit_asa', { id, receiver, sender, assetID, amount });
} else {
// Algo transfer
this.emit('deposit_algo', { id, receiver, sender, amount });
}
}
// Clear seen transactions from previous rounds
const currentRound = queryResult['current-round'];
if (currentRound !== this.lastRound) {
for (const key of this.seenRounds.keys()) {
if (key < currentRound) this.seenRounds.delete(key);
}
}
// Save round number to limit further queries
this.lastRound = currentRound;
} catch (error) {
console.error(error);
}
// Repeat on given interval
setTimeout(() => this.processTransactions(), this.interval);
}
}
module.exports = DepositWatcher;