-
Notifications
You must be signed in to change notification settings - Fork 0
/
depositStatusUpdator.js
83 lines (71 loc) · 2.71 KB
/
depositStatusUpdator.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
const { JsonRpc } = require('eosjs');
const fetch = require('node-fetch');
const mysqlPromise = require('mysql2/promise');
const config = require('./config');
const { URL_ENDPOINT } = require('./setting');
const rpc = new JsonRpc(URL_ENDPOINT, { fetch }); //required to read blockchain state
require('array-foreach-async');
const pool = mysqlPromise.createPool({
host: config.db.host,
user: config.db.user,
port: config.db.port,
password: config.db.password,
database: config.db.dbName,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
const getAllUsers = async () => {
let [rows, fields] = await pool.query("SELECT name FROM eosAddresses");
let addresses = [];
for (let i = 0; i < rows.length; i++) {
if (rows[i].name.length == 12) {
addresses.push(rows[i].name);
}
}
return addresses;
}
const isExistInTransactions = async (txid) => {
let [rows, fields] = await pool.query("SELECT txid FROM eosTransactions");
for (let i = 0; i < rows.length; i++) {
if (rows[i].txid == txid) return true;
}
return false;
}
let blockNumber = config.blockNumber;
async function main() {
try {
console.log('[processing block]', blockNumber);
let users = await getAllUsers();
let block = await rpc.get_block(blockNumber);
if (block.transactions.length) {
for (let transaction of block.transactions) {
let trx = transaction.trx.transaction;
if (!Object.keys(trx).length) continue;
let isExist = await isExistInTransactions(transaction.trx.id);
if (!isExist) {
for (let action of trx.actions) {
// If someone send EOS to me
if (action.name = 'transfer' && users.indexOf(action.data.to) >= 0) {
await pool.query('INSERT INTO eosTransactions (txid, blockid, status, toAddress, amount, type, fromAddress) VALUES (?, ?, ?, ?, ?, ?, ?)', [transaction.trx.id, blockNumber, transaction.status, action.data.to, action.data.quantity, 'pending', action.data.from]);
console.log('[New Transaction Inserted]');
}
}
} else {
console.log('[Same transaction is exsit in DB, skipping this...]');
}
}
}
blockNumber += 1;
await sleep(2000);
setImmediate(main);
} catch (err) {
console.log(err);
await sleep(2000);
setImmediate(main);
}
}
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
main().then(console.log);